From e13f8680c6b4bc0243b63298e6707c035765cbb8 Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Wed, 30 Aug 2017 14:01:42 -0700 Subject: [PATCH 1/2] Integration test for round tripping a job. This PR adds a test that does: 1) nomad init 2) nomad run example.nomad 3) nomad inspect example 4) Equivalent of `curl -XPUT -d @job.json nomad/v1/job/example --- command/integration_test.go | 64 ++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/command/integration_test.go b/command/integration_test.go index 83fae4a4c..ace6f6b4d 100644 --- a/command/integration_test.go +++ b/command/integration_test.go @@ -1,10 +1,16 @@ -package command_test +package command import ( + "bytes" + "encoding/json" + "fmt" "io/ioutil" "os" "os/exec" + "strings" "testing" + + "github.com/hashicorp/nomad/api" ) func TestIntegration_Command_NomadInit(t *testing.T) { @@ -32,3 +38,59 @@ func TestIntegration_Command_NomadInit(t *testing.T) { } } } + +func TestIntegration_Command_RoundTripJob(t *testing.T) { + t.Parallel() + tmpDir, err := ioutil.TempDir("", "nomadtest-rootsecretdir") + if err != nil { + t.Fatalf("unable to create tempdir for test: %v", err) + } + defer os.RemoveAll(tmpDir) + + // Start in dev mode so we get a node registration + srv, client, url := testServer(t, true, nil) + defer srv.Shutdown() + + { + cmd := exec.Command("nomad", "init") + cmd.Dir = tmpDir + if err := cmd.Run(); err != nil { + t.Fatalf("error running init: %v", err) + } + } + + { + cmd := exec.Command("nomad", "run", "example.nomad") + cmd.Dir = tmpDir + cmd.Env = []string{fmt.Sprintf("NOMAD_ADDR=%s", url)} + err := cmd.Run() + if err != nil && !strings.Contains(err.Error(), "exit status 2") { + t.Fatalf("error running example.nomad: %v", err) + } + } + + { + cmd := exec.Command("nomad", "inspect", "example") + cmd.Dir = tmpDir + cmd.Env = []string{fmt.Sprintf("NOMAD_ADDR=%s", url)} + out, err := cmd.Output() + if err != nil { + t.Fatalf("error validating example.nomad: %v", err) + } + + var req api.JobRegisterRequest + dec := json.NewDecoder(bytes.NewReader(out)) + if err := dec.Decode(&req); err != nil { + t.Fatalf("failed to demarshal into register request: %v", err) + } + + var resp api.JobRegisterResponse + if _, err := client.Raw().Write("/v1/jobs", req, &resp, nil); err != nil { + t.Fatalf("failed to put job: %v", err) + } + + if resp.EvalID == "" { + t.Fatalf("didn't create an eval") + } + } +} From 0f1b5ba705c62d9ee52c3c3f8140826ee2c64164 Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Tue, 5 Sep 2017 16:38:15 -0700 Subject: [PATCH 2/2] update test to use assert --- command/integration_test.go | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/command/integration_test.go b/command/integration_test.go index ace6f6b4d..44c1d71c4 100644 --- a/command/integration_test.go +++ b/command/integration_test.go @@ -11,6 +11,7 @@ import ( "testing" "github.com/hashicorp/nomad/api" + "github.com/stretchr/testify/assert" ) func TestIntegration_Command_NomadInit(t *testing.T) { @@ -40,11 +41,10 @@ func TestIntegration_Command_NomadInit(t *testing.T) { } func TestIntegration_Command_RoundTripJob(t *testing.T) { + assert := assert.New(t) t.Parallel() tmpDir, err := ioutil.TempDir("", "nomadtest-rootsecretdir") - if err != nil { - t.Fatalf("unable to create tempdir for test: %v", err) - } + assert.Nil(err) defer os.RemoveAll(tmpDir) // Start in dev mode so we get a node registration @@ -54,9 +54,7 @@ func TestIntegration_Command_RoundTripJob(t *testing.T) { { cmd := exec.Command("nomad", "init") cmd.Dir = tmpDir - if err := cmd.Run(); err != nil { - t.Fatalf("error running init: %v", err) - } + assert.Nil(cmd.Run()) } { @@ -74,23 +72,15 @@ func TestIntegration_Command_RoundTripJob(t *testing.T) { cmd.Dir = tmpDir cmd.Env = []string{fmt.Sprintf("NOMAD_ADDR=%s", url)} out, err := cmd.Output() - if err != nil { - t.Fatalf("error validating example.nomad: %v", err) - } + assert.Nil(err) var req api.JobRegisterRequest dec := json.NewDecoder(bytes.NewReader(out)) - if err := dec.Decode(&req); err != nil { - t.Fatalf("failed to demarshal into register request: %v", err) - } + assert.Nil(dec.Decode(&req)) var resp api.JobRegisterResponse - if _, err := client.Raw().Write("/v1/jobs", req, &resp, nil); err != nil { - t.Fatalf("failed to put job: %v", err) - } - - if resp.EvalID == "" { - t.Fatalf("didn't create an eval") - } + _, err = client.Raw().Write("/v1/jobs", req, &resp, nil) + assert.Nil(err) + assert.NotZero(resp.EvalID) } }