open-nomad/command/integration_test.go

96 lines
2.1 KiB
Go
Raw Normal View History

package command
2017-03-03 20:30:25 +00:00
import (
"bytes"
"encoding/json"
"fmt"
2017-03-03 20:30:25 +00:00
"io/ioutil"
"os"
"os/exec"
"strings"
2017-03-03 20:30:25 +00:00
"testing"
"github.com/hashicorp/nomad/api"
2017-09-05 23:38:15 +00:00
"github.com/stretchr/testify/assert"
2017-03-03 20:30:25 +00:00
)
func TestIntegration_Command_NomadInit(t *testing.T) {
2017-07-21 04:24:21 +00:00
t.Parallel()
2017-03-03 20:30:25 +00:00
tmpDir, err := ioutil.TempDir("", "nomadtest-rootsecretdir")
if err != nil {
t.Fatalf("unable to create tempdir for test: %v", err)
}
defer os.RemoveAll(tmpDir)
{
cmd := exec.Command("nomad", "job", "init")
2017-03-03 20:30:25 +00:00
cmd.Dir = tmpDir
if err := cmd.Run(); err != nil {
t.Fatalf("error running init: %v", err)
}
}
{
cmd := exec.Command("nomad", "job", "validate", "example.nomad")
2017-03-03 20:30:25 +00:00
cmd.Dir = tmpDir
2017-07-21 04:24:21 +00:00
cmd.Env = []string{`NOMAD_ADDR=http://127.0.0.1:0`}
2017-03-03 20:30:25 +00:00
if err := cmd.Run(); err != nil {
t.Fatalf("error validating example.nomad: %v", err)
}
}
}
func TestIntegration_Command_RoundTripJob(t *testing.T) {
2017-09-05 23:38:15 +00:00
assert := assert.New(t)
t.Parallel()
tmpDir, err := ioutil.TempDir("", "nomadtest-rootsecretdir")
2017-09-05 23:38:15 +00:00
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", "job", "init")
cmd.Dir = tmpDir
2017-09-05 23:38:15 +00:00
assert.Nil(cmd.Run())
}
{
cmd := exec.Command("nomad", "job", "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", "job", "inspect", "example")
cmd.Dir = tmpDir
cmd.Env = []string{fmt.Sprintf("NOMAD_ADDR=%s", url)}
out, err := cmd.Output()
2017-09-05 23:38:15 +00:00
assert.Nil(err)
var req api.JobRegisterRequest
dec := json.NewDecoder(bytes.NewReader(out))
2017-09-05 23:38:15 +00:00
assert.Nil(dec.Decode(&req))
var resp api.JobRegisterResponse
2017-09-05 23:38:15 +00:00
_, err = client.Raw().Write("/v1/jobs", req, &resp, nil)
assert.Nil(err)
assert.NotZero(resp.EvalID)
}
{
cmd := exec.Command("nomad", "job", "stop", "example")
cmd.Dir = tmpDir
cmd.Env = []string{fmt.Sprintf("NOMAD_ADDR=%s", url)}
_, err := cmd.Output()
assert.Nil(err)
}
}