97d1bc735c
* test: use `T.TempDir` to create temporary test directory This commit replaces `ioutil.TempDir` with `t.TempDir` in tests. The directory created by `t.TempDir` is automatically removed when the test and all its subtests complete. Prior to this commit, temporary directory created using `ioutil.TempDir` needs to be removed manually by calling `os.RemoveAll`, which is omitted in some tests. The error handling boilerplate e.g. defer func() { if err := os.RemoveAll(dir); err != nil { t.Fatal(err) } } is also tedious, but `t.TempDir` handles this for us nicely. Reference: https://pkg.go.dev/testing#T.TempDir Signed-off-by: Eng Zer Jun <engzerjun@gmail.com> * test: fix TestLogmon_Start_restart on Windows Signed-off-by: Eng Zer Jun <engzerjun@gmail.com> * test: fix failing TestConsul_Integration t.TempDir fails to perform the cleanup properly because the folder is still in use testing.go:967: TempDir RemoveAll cleanup: unlinkat /tmp/TestConsul_Integration2837567823/002/191a6f1a-5371-cf7c-da38-220fe85d10e5/web/secrets: device or resource busy Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
88 lines
1.9 KiB
Go
88 lines
1.9 KiB
Go
package command
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os/exec"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/nomad/api"
|
|
"github.com/hashicorp/nomad/ci"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestIntegration_Command_NomadInit(t *testing.T) {
|
|
ci.Parallel(t)
|
|
tmpDir := t.TempDir()
|
|
|
|
{
|
|
cmd := exec.Command("nomad", "job", "init")
|
|
cmd.Dir = tmpDir
|
|
if err := cmd.Run(); err != nil {
|
|
t.Fatalf("error running init: %v", err)
|
|
}
|
|
}
|
|
|
|
{
|
|
cmd := exec.Command("nomad", "job", "validate", "example.nomad")
|
|
cmd.Dir = tmpDir
|
|
cmd.Env = []string{`NOMAD_ADDR=http://127.0.0.1:0`}
|
|
if err := cmd.Run(); err != nil {
|
|
t.Fatalf("error validating example.nomad: %v", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestIntegration_Command_RoundTripJob(t *testing.T) {
|
|
ci.Parallel(t)
|
|
assert := assert.New(t)
|
|
tmpDir := t.TempDir()
|
|
|
|
// 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", "-short")
|
|
cmd.Dir = tmpDir
|
|
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()
|
|
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)
|
|
}
|
|
|
|
{
|
|
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)
|
|
}
|
|
}
|