Merge pull request #2828 from hashicorp/pr/2827

Updates testing harness.
This commit is contained in:
James Phillips 2017-03-23 17:57:01 -07:00 committed by GitHub
commit c05f81edf1
11 changed files with 90 additions and 57 deletions

View File

@ -8,6 +8,6 @@ branches:
- master - master
script: script:
- make ci - make test vet
sudo: false sudo: false

View File

@ -4,59 +4,51 @@ GOTOOLS = \
github.com/mitchellh/gox \ github.com/mitchellh/gox \
golang.org/x/tools/cmd/cover \ golang.org/x/tools/cmd/cover \
golang.org/x/tools/cmd/stringer golang.org/x/tools/cmd/stringer
PACKAGES=$(shell go list ./... | grep -v '/vendor/') TEST ?= ./...
VETARGS?=-asmdecl -atomic -bool -buildtags -copylocks -methods \ GOTAGS ?= consul
-nilfunc -printf -rangeloops -shift -structtags -unsafeptr GOFILES ?= $(shell go list $(TEST) | grep -v /vendor/)
BUILD_TAGS?=consul
# all builds binaries for all targets # all builds binaries for all targets
all: bin all: bin
ci:
if [ "${TRAVIS_PULL_REQUEST}" = "false" ]; then \
$(MAKE) bin ;\
fi
@$(MAKE) test
bin: tools bin: tools
@mkdir -p bin/ @mkdir -p bin/
@BUILD_TAGS='$(BUILD_TAGS)' sh -c "'$(CURDIR)/scripts/build.sh'" @GOTAGS='$(GOTAGS)' sh -c "'$(CURDIR)/scripts/build.sh'"
# dev creates binaries for testing locally - these are put into ./bin and $GOPATH # dev creates binaries for testing locally - these are put into ./bin and $GOPATH
dev: format dev: format
@CONSUL_DEV=1 BUILD_TAGS='$(BUILD_TAGS)' sh -c "'$(CURDIR)/scripts/build.sh'" @CONSUL_DEV=1 GOTAGS='$(GOTAGS)' sh -c "'$(CURDIR)/scripts/build.sh'"
# dist builds binaries for all platforms and packages them for distribution # dist builds binaries for all platforms and packages them for distribution
dist: dist:
@BUILD_TAGS='$(BUILD_TAGS)' sh -c "'$(CURDIR)/scripts/dist.sh'" @GOTAGS='$(GOTAGS)' sh -c "'$(CURDIR)/scripts/dist.sh'"
cov: cov:
gocov test ./... | gocov-html > /tmp/coverage.html gocov test ${GOFILES} | gocov-html > /tmp/coverage.html
open /tmp/coverage.html open /tmp/coverage.html
test: format test:
@$(MAKE) vet
@./scripts/verify_no_uuid.sh @./scripts/verify_no_uuid.sh
@BUILD_TAGS='$(BUILD_TAGS)' sh -c "'$(CURDIR)/scripts/test.sh'" @env \
GOTAGS="${GOTAGS}" \
GOFILES="${GOFILES}" \
TESTARGS="${TESTARGS}" \
sh -c "'$(CURDIR)/scripts/test.sh'"
cover: cover:
go list ./... | xargs -n1 go test --cover go test ${GOFILES} --cover
format: format:
@echo "--> Running go fmt" @echo "--> Running go fmt"
@go fmt $(PACKAGES) @go fmt ${GOFILES}
vet: vet:
@echo "--> Running go tool vet $(VETARGS) ." @echo "--> Running go vet"
@go list ./... \ @go vet ${GOFILES}; if [ $$? -eq 1 ]; then \
| grep -v /vendor/ \
| cut -d '/' -f 4- \
| xargs -n1 \
go tool vet $(VETARGS) ;\
if [ $$? -ne 0 ]; then \
echo ""; \ echo ""; \
echo "Vet found suspicious constructs. Please check the reported constructs"; \ echo "Vet found suspicious constructs. Please check the reported constructs"; \
echo "and fix them if necessary before submitting the code for reviewal."; \ echo "and fix them if necessary before submitting the code for review."; \
exit 1; \
fi fi
# build the static web ui and build static assets inside a Docker container, the # build the static web ui and build static assets inside a Docker container, the

View File

@ -46,7 +46,6 @@ func (s *Server) Flood(portFn servers.FloodPortFn, global *serf.Serf) {
}() }()
for { for {
WAIT:
select { select {
case <-s.serfLAN.ShutdownCh(): case <-s.serfLAN.ShutdownCh():
return return
@ -60,7 +59,6 @@ func (s *Server) Flood(portFn servers.FloodPortFn, global *serf.Serf) {
case <-floodCh: case <-floodCh:
goto FLOOD goto FLOOD
} }
goto WAIT
FLOOD: FLOOD:
servers.FloodJoins(s.logger, portFn, s.config.Datacenter, s.serfLAN, global) servers.FloodJoins(s.logger, portFn, s.config.Datacenter, s.serfLAN, global)

View File

@ -309,7 +309,10 @@ func (c *consulFSM) applyTxn(buf []byte, index uint64) interface{} {
} }
defer metrics.MeasureSince([]string{"consul", "fsm", "txn"}, time.Now()) defer metrics.MeasureSince([]string{"consul", "fsm", "txn"}, time.Now())
results, errors := c.state.TxnRW(index, req.Ops) results, errors := c.state.TxnRW(index, req.Ops)
return structs.TxnResponse{results, errors} return structs.TxnResponse{
Results: results,
Errors: errors,
}
} }
func (c *consulFSM) applyAutopilotUpdate(buf []byte, index uint64) interface{} { func (c *consulFSM) applyAutopilotUpdate(buf []byte, index uint64) interface{} {

View File

@ -374,7 +374,7 @@ func TestLeader_Reconcile_Races(t *testing.T) {
// Wait for the server to reconcile the client and register it. // Wait for the server to reconcile the client and register it.
state := s1.fsm.State() state := s1.fsm.State()
var nodeAddr string var nodeAddr string
testutil.WaitForResult(func() (bool, error) { if err := testutil.WaitForResult(func() (bool, error) {
_, node, err := state.GetNode(c1.config.NodeName) _, node, err := state.GetNode(c1.config.NodeName)
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
@ -385,9 +385,9 @@ func TestLeader_Reconcile_Races(t *testing.T) {
} else { } else {
return false, nil return false, nil
} }
}, func(err error) { }); err != nil {
t.Fatalf("client should be registered") t.Fatalf("client should be registered: %v", err)
}) }
// Add in some metadata via the catalog (as if the agent synced it // Add in some metadata via the catalog (as if the agent synced it
// there). We also set the serfHealth check to failing so the reconile // there). We also set the serfHealth check to failing so the reconile
@ -428,15 +428,15 @@ func TestLeader_Reconcile_Races(t *testing.T) {
// Fail the member and wait for the health to go critical. // Fail the member and wait for the health to go critical.
c1.Shutdown() c1.Shutdown()
testutil.WaitForResult(func() (bool, error) { if err := testutil.WaitForResult(func() (bool, error) {
_, checks, err := state.NodeChecks(nil, c1.config.NodeName) _, checks, err := state.NodeChecks(nil, c1.config.NodeName)
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
return checks[0].Status == structs.HealthCritical, errors.New(checks[0].Status) return checks[0].Status == structs.HealthCritical, errors.New(checks[0].Status)
}, func(err error) { }); err != nil {
t.Fatalf("check status is %v, should be critical", err) t.Fatalf("check status should be critical: %v", err)
}) }
// Make sure the metadata didn't get clobbered. // Make sure the metadata didn't get clobbered.
_, node, err = state.GetNode(c1.config.NodeName) _, node, err = state.GetNode(c1.config.NodeName)

View File

@ -405,7 +405,10 @@ func TestRouter_GetDatacenterMaps(t *testing.T) {
Datacenter: "dc0", Datacenter: "dc0",
AreaID: types.AreaWAN, AreaID: types.AreaWAN,
Coordinates: structs.Coordinates{ Coordinates: structs.Coordinates{
&structs.Coordinate{"node0.dc0", lib.GenerateCoordinate(10 * time.Millisecond)}, &structs.Coordinate{
Node: "node0.dc0",
Coord: lib.GenerateCoordinate(10 * time.Millisecond),
},
}, },
}) { }) {
t.Fatalf("bad: %#v", entry) t.Fatalf("bad: %#v", entry)
@ -415,9 +418,18 @@ func TestRouter_GetDatacenterMaps(t *testing.T) {
Datacenter: "dc1", Datacenter: "dc1",
AreaID: types.AreaWAN, AreaID: types.AreaWAN,
Coordinates: structs.Coordinates{ Coordinates: structs.Coordinates{
&structs.Coordinate{"node1.dc1", lib.GenerateCoordinate(3 * time.Millisecond)}, &structs.Coordinate{
&structs.Coordinate{"node2.dc1", lib.GenerateCoordinate(2 * time.Millisecond)}, Node: "node1.dc1",
&structs.Coordinate{"node3.dc1", lib.GenerateCoordinate(5 * time.Millisecond)}, Coord: lib.GenerateCoordinate(3 * time.Millisecond),
},
&structs.Coordinate{
Node: "node2.dc1",
Coord: lib.GenerateCoordinate(2 * time.Millisecond),
},
&structs.Coordinate{
Node: "node3.dc1",
Coord: lib.GenerateCoordinate(5 * time.Millisecond),
},
}, },
}) { }) {
t.Fatalf("bad: %#v", entry) t.Fatalf("bad: %#v", entry)
@ -427,7 +439,10 @@ func TestRouter_GetDatacenterMaps(t *testing.T) {
Datacenter: "dc2", Datacenter: "dc2",
AreaID: types.AreaWAN, AreaID: types.AreaWAN,
Coordinates: structs.Coordinates{ Coordinates: structs.Coordinates{
&structs.Coordinate{"node1.dc2", lib.GenerateCoordinate(8 * time.Millisecond)}, &structs.Coordinate{
Node: "node1.dc2",
Coord: lib.GenerateCoordinate(8 * time.Millisecond),
},
}, },
}) { }) {
t.Fatalf("bad: %#v", entry) t.Fatalf("bad: %#v", entry)

View File

@ -124,7 +124,10 @@ func (s *StateStore) txnDispatch(tx *memdb.Txn, idx uint64, ops structs.TxnOps)
// Capture any error along with the index of the operation that // Capture any error along with the index of the operation that
// failed. // failed.
if err != nil { if err != nil {
errors = append(errors, &structs.TxnError{i, err.Error()}) errors = append(errors, &structs.TxnError{
OpIndex: i,
What: err.Error(),
})
} }
} }

View File

@ -24,10 +24,16 @@ func (t *Txn) preCheck(acl acl.ACL, ops structs.TxnOps) structs.TxnErrors {
if op.KV != nil { if op.KV != nil {
ok, err := kvsPreApply(t.srv, acl, op.KV.Verb, &op.KV.DirEnt) ok, err := kvsPreApply(t.srv, acl, op.KV.Verb, &op.KV.DirEnt)
if err != nil { if err != nil {
errors = append(errors, &structs.TxnError{i, err.Error()}) errors = append(errors, &structs.TxnError{
OpIndex: i,
What: err.Error(),
})
} else if !ok { } else if !ok {
err = fmt.Errorf("failed to lock key %q due to lock delay", op.KV.DirEnt.Key) err = fmt.Errorf("failed to lock key %q due to lock delay", op.KV.DirEnt.Key)
errors = append(errors, &structs.TxnError{i, err.Error()}) errors = append(errors, &structs.TxnError{
OpIndex: i,
What: err.Error(),
})
} }
} }
} }

View File

@ -251,7 +251,10 @@ func TestTxn_Apply_ACLDeny(t *testing.T) {
// These get filtered but won't result in an error. // These get filtered but won't result in an error.
default: default:
expected.Errors = append(expected.Errors, &structs.TxnError{i, permissionDeniedErr.Error()}) expected.Errors = append(expected.Errors, &structs.TxnError{
OpIndex: i,
What: permissionDeniedErr.Error(),
})
} }
} }
if !reflect.DeepEqual(out, expected) { if !reflect.DeepEqual(out, expected) {
@ -509,7 +512,10 @@ func TestTxn_Read_ACLDeny(t *testing.T) {
// These get filtered but won't result in an error. // These get filtered but won't result in an error.
default: default:
expected.Errors = append(expected.Errors, &structs.TxnError{i, permissionDeniedErr.Error()}) expected.Errors = append(expected.Errors, &structs.TxnError{
OpIndex: i,
What: permissionDeniedErr.Error(),
})
} }
} }
if !reflect.DeepEqual(out, expected) { if !reflect.DeepEqual(out, expected) {

View File

@ -39,7 +39,7 @@ type Config struct {
func Setup(config *Config, ui cli.Ui) (*logutils.LevelFilter, *GatedWriter, *LogWriter, io.Writer, bool) { func Setup(config *Config, ui cli.Ui) (*logutils.LevelFilter, *GatedWriter, *LogWriter, io.Writer, bool) {
// The gated writer buffers logs at startup and holds until it's flushed. // The gated writer buffers logs at startup and holds until it's flushed.
logGate := &GatedWriter{ logGate := &GatedWriter{
Writer: &cli.UiWriter{ui}, Writer: &cli.UiWriter{Ui: ui},
} }
// Set up the level filter. // Set up the level filter.

View File

@ -1,14 +1,24 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -e set -e
# Create a temp dir and clean it up on exit if [ -n "$TRAVIS" ]; then
TEMPDIR=`mktemp -d -t consul-test.XXX` # Install all packages - this will make running the suite faster
trap "rm -rf $TEMPDIR" EXIT HUP INT QUIT TERM echo "--> Installing packages for faster tests"
go install -tags="${GOTAGS}" -a ./...
fi
# Build the Consul binary for the API tests # If we are testing the API, build and install consul
echo "--> Building consul" if grep -q "/consul/api" <<< "${GOFILES}"; then
go build -tags="${BUILD_TAGS}" -o $TEMPDIR/consul || exit 1 # Create a temp dir and clean it up on exit
TEMPDIR=`mktemp -d -t consul-test.XXX`
trap "rm -rf ${TEMPDIR}" EXIT HUP INT QUIT TERM
# Build the Consul binary for the API tests
echo "--> Building consul"
go build -tags="${GOTAGS}" -o $TEMPDIR/consul
PATH="${TEMPDIR}:${PATH}"
fi
# Run the tests # Run the tests
echo "--> Running tests" echo "--> Running tests"
go list ./... | grep -v '/vendor/' | PATH=$TEMPDIR:$PATH xargs -n1 go test -tags="${BUILD_TAGS}" ${GOTEST_FLAGS:--cover -timeout=360s} go test -timeout=360s -parallel=20 -tags="${GOTAGS}" ${GOFILES} ${TESTARGS}