Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix flaky tests #3434

Merged
merged 9 commits into from
Oct 25, 2017
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -221,15 +221,14 @@ test: ## Run the Nomad test suite and/or the Nomad UI test suite
fi

.PHONY: test-nomad
test-nomad: LOCAL_PACKAGES = $(shell go list ./... | grep -v '/vendor/')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good riddance grep -v vendor!

test-nomad: dev ## Run Nomad test suites
@echo "==> Running Nomad test suites:"
@NOMAD_TEST_RKT=1 \
go test \
-cover \
-timeout=900s \
-tags="nomad_test $(if $(HAS_LXC),lxc)" \
$(LOCAL_PACKAGES)
./...

.PHONY: clean
clean: GOPATH=$(shell go env GOPATH)
Expand Down
10 changes: 4 additions & 6 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"testing"
"time"

"github.com/hashicorp/consul/lib/freeport"
memdb "github.com/hashicorp/go-memdb"
"github.com/hashicorp/nomad/client/config"
"github.com/hashicorp/nomad/client/fingerprint"
Expand All @@ -28,10 +29,6 @@ import (
ctestutil "github.com/hashicorp/nomad/client/testutil"
)

func getPort() int {
return 1030 + int(rand.Int31n(6440))
}

func testACLServer(t *testing.T, cb func(*nomad.Config)) (*nomad.Server, string, *structs.ACLToken) {
server, addr := testServer(t, func(c *nomad.Config) {
c.ACLEnabled = true
Expand Down Expand Up @@ -78,12 +75,13 @@ func testServer(t *testing.T, cb func(*nomad.Config)) (*nomad.Server, string) {
}

for i := 10; i >= 0; i-- {
ports := freeport.GetT(t, 2)
config.RPCAddr = &net.TCPAddr{
IP: []byte{127, 0, 0, 1},
Port: getPort(),
Port: ports[0],
}
config.NodeName = fmt.Sprintf("Node %d", config.RPCAddr.Port)
config.SerfConfig.MemberlistConfig.BindPort = getPort()
config.SerfConfig.MemberlistConfig.BindPort = ports[1]

// Create server
server, err := nomad.NewServer(config, catalog, logger)
Expand Down
44 changes: 20 additions & 24 deletions client/driver/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"time"

docker "github.com/fsouza/go-dockerclient"
"github.com/hashicorp/consul/lib/freeport"
sockaddr "github.com/hashicorp/go-sockaddr"
"github.com/hashicorp/nomad/client/allocdir"
"github.com/hashicorp/nomad/client/config"
Expand All @@ -42,17 +43,12 @@ func dockerIsRemote(t *testing.T) bool {
return false
}

// Ports used by tests
var (
docker_reserved = 2000 + int(rand.Int31n(10000))
docker_dynamic = 2000 + int(rand.Int31n(10000))
)

// Returns a task with a reserved and dynamic port. The ports are returned
// respectively.
func dockerTask() (*structs.Task, int, int) {
docker_reserved += 1
docker_dynamic += 1
func dockerTask(t *testing.T) (*structs.Task, int, int) {
ports := freeport.GetT(t, 2)
docker_reserved := ports[0]
docker_dynamic := ports[1]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're touching this mind updating the variable naming to match regular Go style?

return &structs.Task{
Name: "redis-demo",
Driver: "docker",
Expand Down Expand Up @@ -563,9 +559,9 @@ func TestDockerDriver_StartN(t *testing.T) {
t.Skip("Docker not connected")
}

task1, _, _ := dockerTask()
task2, _, _ := dockerTask()
task3, _, _ := dockerTask()
task1, _, _ := dockerTask(t)
task2, _, _ := dockerTask(t)
task3, _, _ := dockerTask(t)
taskList := []*structs.Task{task1, task2, task3}

handles := make([]DriverHandle, len(taskList))
Expand Down Expand Up @@ -617,15 +613,15 @@ func TestDockerDriver_StartNVersions(t *testing.T) {
t.Skip("Docker not connected")
}

task1, _, _ := dockerTask()
task1, _, _ := dockerTask(t)
task1.Config["image"] = "busybox"
task1.Config["load"] = "busybox.tar"

task2, _, _ := dockerTask()
task2, _, _ := dockerTask(t)
task2.Config["image"] = "busybox:musl"
task2.Config["load"] = "busybox_musl.tar"

task3, _, _ := dockerTask()
task3, _, _ := dockerTask(t)
task3.Config["image"] = "busybox:glibc"
task3.Config["load"] = "busybox_glibc.tar"

Expand Down Expand Up @@ -795,7 +791,7 @@ func TestDockerDriver_Labels(t *testing.T) {
t.Skip("Docker not connected")
}

task, _, _ := dockerTask()
task, _, _ := dockerTask(t)
task.Config["labels"] = []map[string]string{
{
"label1": "value1",
Expand Down Expand Up @@ -830,7 +826,7 @@ func TestDockerDriver_ForcePull_IsInvalidConfig(t *testing.T) {
t.Skip("Docker not connected")
}

task, _, _ := dockerTask()
task, _, _ := dockerTask(t)
task.Config["force_pull"] = "nothing"

ctx := testDockerDriverContexts(t, task)
Expand All @@ -851,7 +847,7 @@ func TestDockerDriver_ForcePull(t *testing.T) {
t.Skip("Docker not connected")
}

task, _, _ := dockerTask()
task, _, _ := dockerTask(t)
task.Config["force_pull"] = "true"

client, handle, cleanup := dockerSetup(t, task)
Expand All @@ -873,7 +869,7 @@ func TestDockerDriver_SecurityOpt(t *testing.T) {
t.Skip("Docker not connected")
}

task, _, _ := dockerTask()
task, _, _ := dockerTask(t)
task.Config["security_opt"] = []string{"seccomp=unconfined"}

client, handle, cleanup := dockerSetup(t, task)
Expand All @@ -899,7 +895,7 @@ func TestDockerDriver_DNS(t *testing.T) {
t.Skip("Docker not connected")
}

task, _, _ := dockerTask()
task, _, _ := dockerTask(t)
task.Config["dns_servers"] = []string{"8.8.8.8", "8.8.4.4"}
task.Config["dns_search_domains"] = []string{"example.com", "example.org", "example.net"}
task.Config["dns_options"] = []string{"ndots:1"}
Expand Down Expand Up @@ -935,7 +931,7 @@ func TestDockerDriver_MACAddress(t *testing.T) {
t.Skip("Docker not connected")
}

task, _, _ := dockerTask()
task, _, _ := dockerTask(t)
task.Config["mac_address"] = "00:16:3e:00:00:00"

client, handle, cleanup := dockerSetup(t, task)
Expand All @@ -961,7 +957,7 @@ func TestDockerWorkDir(t *testing.T) {
t.Skip("Docker not connected")
}

task, _, _ := dockerTask()
task, _, _ := dockerTask(t)
task.Config["work_dir"] = "/some/path"

client, handle, cleanup := dockerSetup(t, task)
Expand Down Expand Up @@ -994,7 +990,7 @@ func TestDockerDriver_PortsNoMap(t *testing.T) {
t.Skip("Docker not connected")
}

task, res, dyn := dockerTask()
task, res, dyn := dockerTask(t)

client, handle, cleanup := dockerSetup(t, task)
defer cleanup()
Expand Down Expand Up @@ -1051,7 +1047,7 @@ func TestDockerDriver_PortsMapping(t *testing.T) {
t.Skip("Docker not connected")
}

task, res, dyn := dockerTask()
task, res, dyn := dockerTask(t)
task.Config["port_map"] = []map[string]string{
{
"main": "8080",
Expand Down
8 changes: 7 additions & 1 deletion client/driver/raw_exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ func TestRawExecDriver_HandlerExec(t *testing.T) {
Driver: "raw_exec",
Config: map[string]interface{}{
"command": testtask.Path(),
"args": []string{"sleep", "9000"},
"args": []string{"sleep", "9000s"},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it was fun to debug. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Took me so long! Literally went home after I found the bug. I was just done haha

},
LogConfig: &structs.LogConfig{
MaxFiles: 10,
Expand Down Expand Up @@ -310,6 +310,12 @@ func TestRawExecDriver_HandlerExec(t *testing.T) {
t.Fatalf("expected output to contain %q but found: %q", expected, out)
}

select {
case res := <-resp.Handle.WaitCh():
t.Fatalf("Shouldn't be exited: %v", res.String())
default:
}

if err := resp.Handle.Kill(); err != nil {
t.Fatalf("error killing exec handle: %v", err)
}
Expand Down
17 changes: 1 addition & 16 deletions command/agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"io/ioutil"
"log"
"net"
"os"
"strings"
"testing"
Expand All @@ -15,20 +14,6 @@ import (
"github.com/stretchr/testify/assert"
)

func getPort() int {
addr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:0")
if err != nil {
panic(err)
}

l, err := net.ListenTCP("tcp", addr)
if err != nil {
panic(err)
}
defer l.Close()
return l.Addr().(*net.TCPAddr).Port
}

func tmpDir(t testing.TB) string {
dir, err := ioutil.TempDir("", "nomad")
if err != nil {
Expand All @@ -39,7 +24,7 @@ func tmpDir(t testing.TB) string {

func TestAgent_RPCPing(t *testing.T) {
t.Parallel()
agent := NewTestAgent(t.Name(), nil)
agent := NewTestAgent(t, t.Name(), nil)
defer agent.Shutdown()

var out struct{}
Expand Down
2 changes: 1 addition & 1 deletion command/agent/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func TestCommand_Args(t *testing.T) {
// TODO Why is this failing
func TestRetryJoin(t *testing.T) {
t.Parallel()
agent := NewTestAgent(t.Name(), nil)
agent := NewTestAgent(t, t.Name(), nil)
defer agent.Shutdown()

doneCh := make(chan struct{})
Expand Down
2 changes: 1 addition & 1 deletion command/agent/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
// makeHTTPServer returns a test server whose logs will be written to
// the passed writer. If the writer is nil, the logs are written to stderr.
func makeHTTPServer(t testing.TB, cb func(c *Config)) *TestAgent {
return NewTestAgent(t.Name(), cb)
return NewTestAgent(t, t.Name(), cb)
}

func BenchmarkHTTPRequests(b *testing.B) {
Expand Down
3 changes: 2 additions & 1 deletion command/agent/keyring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestAgent_LoadKeyrings(t *testing.T) {
key := "tbLJg26ZJyJ9pK3qhc9jig=="

// Should be no configured keyring file by default
agent1 := NewTestAgent(t.Name(), nil)
agent1 := NewTestAgent(t, t.Name(), nil)
defer agent1.Shutdown()

c := agent1.server.GetConfig()
Expand All @@ -26,6 +26,7 @@ func TestAgent_LoadKeyrings(t *testing.T) {

// Server should auto-load WAN keyring files
agent2 := &TestAgent{
T: t,
Name: t.Name() + "2",
Key: key,
}
Expand Down
20 changes: 13 additions & 7 deletions command/agent/metrics_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

metrics "github.com/armon/go-metrics"
"github.com/hashicorp/nomad/testutil"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -41,12 +42,17 @@ func TestHTTP_Metrics(t *testing.T) {
assert.Nil(err)
respW = httptest.NewRecorder()

resp, err := s.Server.MetricsRequest(respW, req)
assert.Nil(err)

res := resp.(metrics.MetricsSummary)

gauges := res.Gauges
assert.NotEqual(0, len(gauges))
testutil.WaitForResult(func() (bool, error) {
resp, err := s.Server.MetricsRequest(respW, req)
if err != nil {
return false, err
}
respW.Flush()

res := resp.(metrics.MetricsSummary)
return len(res.Gauges) != 0, nil
}, func(err error) {
t.Fatalf("should have metrics: %v", err)
})
})
}
Loading