Merge pull request #3140 from hashicorp/f-round-trip-test

Integration test for round tripping a job.
This commit is contained in:
Alex Dadgar
2017-09-05 16:38:41 -07:00
committed by GitHub

View File

@@ -1,10 +1,17 @@
package command_test
package command
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"testing"
"github.com/hashicorp/nomad/api"
"github.com/stretchr/testify/assert"
)
func TestIntegration_Command_NomadInit(t *testing.T) {
@@ -32,3 +39,48 @@ 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")
assert.Nil(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
assert.Nil(cmd.Run())
}
{
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()
assert.Nil(err)
var req api.JobRegisterRequest
dec := json.NewDecoder(bytes.NewReader(out))
assert.Nil(dec.Decode(&req))
var resp api.JobRegisterResponse
_, err = client.Raw().Write("/v1/jobs", req, &resp, nil)
assert.Nil(err)
assert.NotZero(resp.EvalID)
}
}