From aa410d0530e4e16de84f6fef08676ec2ad3631a8 Mon Sep 17 00:00:00 2001 From: Chelsea Holland Komlo Date: Tue, 24 Oct 2017 21:38:15 -0400 Subject: [PATCH] fixing up code review feedback --- e2e/migrations/migrations_test.go | 130 ++++++++++++++++-------------- 1 file changed, 69 insertions(+), 61 deletions(-) diff --git a/e2e/migrations/migrations_test.go b/e2e/migrations/migrations_test.go index e172138b0..986f113cd 100644 --- a/e2e/migrations/migrations_test.go +++ b/e2e/migrations/migrations_test.go @@ -8,10 +8,67 @@ import ( "strings" "testing" - "github.com/stretchr/testify/assert" "github.com/hashicorp/nomad/testutil" + "github.com/stretchr/testify/assert" ) +const sleepJobOne = `job "sleep" { + type = "batch" + datacenters = ["dc1"] + constraint { + attribute = "${meta.secondary}" + value = 1 + } + group "group1" { + restart { + mode = "fail" + } + count = 1 + ephemeral_disk { + migrate = true + sticky = true + } + task "sleep" { + template { + data = "hello world" + destination = "/local/hello-world" + } + driver = "exec" + config { + command = "/bin/sleep" + args = [ "infinity" ] + } + } + } +}` + +const sleepJobTwo = `job "sleep" { + type = "batch" + datacenters = ["dc1"] + constraint { + attribute = "${meta.secondary}" + value = 0 + } + group "group1" { + restart { + mode = "fail" + } + count = 1 + ephemeral_disk { + migrate = true + sticky = true + } + task "sleep" { + driver = "exec" + + config { + command = "test" + args = [ "-f", "/local/hello-world" ] + } + } + } +}` + func isSuccess(execCmd *exec.Cmd, retries int, keyword string) (string, error) { var successOut string var err error @@ -41,17 +98,23 @@ func isSuccess(execCmd *exec.Cmd, retries int, keyword string) (string, error) { return successOut, err } -func allNomadNodesAreReady(retries int) (string, error) { +// allNodesAreReady attempts to query the status of a cluster a specific number +// of times +func allNodesAreReady(retries int) (string, error) { cmd := exec.Command("nomad", "node-status") return isSuccess(cmd, retries, "initializing") } +// jobIsReady attempts sto query the status of a specific job a fixed number of +// times func jobIsReady(retries int, jobName string) (string, error) { cmd := exec.Command("nomad", "job", "status", jobName) return isSuccess(cmd, retries, "pending") } -// requires nomad executable on the path +// startCluster will create a running cluster, given a list of agent config +// files. In order to have a complete cluster, at least one server and one +// client config file should be included. func startCluster(clusterConfig []string) (func(), error) { cmds := make([]*exec.Cmd, 0) @@ -84,43 +147,14 @@ func TestJobMigrations(t *testing.T) { assert.Nil(err) defer stopCluster() - _, err = allNomadNodesAreReady(10) + _, err = allNodesAreReady(10) assert.Nil(err) fh, err := ioutil.TempFile("", "nomad-sleep-1") assert.Nil(err) defer os.Remove(fh.Name()) - _, err = fh.WriteString(` - job "sleep" { - type = "batch" - datacenters = ["dc1"] - constraint { - attribute = "${meta.secondary}" - value = 1 - } - group "group1" { - restart { - mode = "fail" - } - count = 1 - ephemeral_disk { - migrate = true - sticky = true - } - task "sleep" { - template { - data = "hello world" - destination = "/local/hello-world" - } - driver = "exec" - config { - command = "/bin/sleep" - args = [ "infinity" ] - } - } - } - }`) + _, err = fh.WriteString(sleepJobOne) assert.Nil(err) @@ -136,33 +170,7 @@ func TestJobMigrations(t *testing.T) { assert.Nil(err) defer os.Remove(fh2.Name()) - _, err = fh2.WriteString(` - job "sleep" { - type = "batch" - datacenters = ["dc1"] - constraint { - attribute = "${meta.secondary}" - value = 1 - } - group "group1" { - restart { - mode = "fail" - } - count = 1 - ephemeral_disk { - migrate = true - sticky = true - } - task "sleep" { - driver = "exec" - - config { - command = "test" - args = [ "-f", "/local/hello-world" ] - } - } - } - }`) + _, err = fh2.WriteString(sleepJobTwo) assert.Nil(err)