2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2017-08-30 21:01:42 +00:00
|
|
|
package command
|
2017-03-03 20:30:25 +00:00
|
|
|
|
|
|
|
import (
|
2017-08-30 21:01:42 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2017-03-03 20:30:25 +00:00
|
|
|
"os/exec"
|
2017-08-30 21:01:42 +00:00
|
|
|
"strings"
|
2017-03-03 20:30:25 +00:00
|
|
|
"testing"
|
2017-08-30 21:01:42 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/api"
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2022-05-05 17:56:52 +00:00
|
|
|
"github.com/hashicorp/nomad/client/testutil"
|
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) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2022-05-12 15:42:40 +00:00
|
|
|
tmpDir := t.TempDir()
|
2017-03-03 20:30:25 +00:00
|
|
|
|
|
|
|
{
|
2018-12-04 16:50:59 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2023-02-02 17:47:47 +00:00
|
|
|
cmd := exec.Command("nomad", "job", "validate", "example.nomad.hcl")
|
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 {
|
2023-02-02 17:47:47 +00:00
|
|
|
t.Fatalf("error validating example.nomad.hcl: %v", err)
|
2017-03-03 20:30:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-30 21:01:42 +00:00
|
|
|
|
|
|
|
func TestIntegration_Command_RoundTripJob(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2022-05-05 17:56:52 +00:00
|
|
|
testutil.DockerCompatible(t)
|
|
|
|
|
2017-09-05 23:38:15 +00:00
|
|
|
assert := assert.New(t)
|
2022-05-12 15:42:40 +00:00
|
|
|
tmpDir := t.TempDir()
|
2017-08-30 21:01:42 +00:00
|
|
|
|
|
|
|
// Start in dev mode so we get a node registration
|
|
|
|
srv, client, url := testServer(t, true, nil)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
|
|
|
|
{
|
2022-04-20 12:09:13 +00:00
|
|
|
cmd := exec.Command("nomad", "job", "init", "-short")
|
2017-08-30 21:01:42 +00:00
|
|
|
cmd.Dir = tmpDir
|
2017-09-05 23:38:15 +00:00
|
|
|
assert.Nil(cmd.Run())
|
2017-08-30 21:01:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2023-02-02 17:47:47 +00:00
|
|
|
cmd := exec.Command("nomad", "job", "run", "example.nomad.hcl")
|
2017-08-30 21:01:42 +00:00
|
|
|
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") {
|
2023-02-02 17:47:47 +00:00
|
|
|
t.Fatalf("error running example.nomad.hcl: %v", err)
|
2017-08-30 21:01:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-12-04 16:50:59 +00:00
|
|
|
cmd := exec.Command("nomad", "job", "inspect", "example")
|
2017-08-30 21:01:42 +00:00
|
|
|
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)
|
2017-08-30 21:01:42 +00:00
|
|
|
|
|
|
|
var req api.JobRegisterRequest
|
|
|
|
dec := json.NewDecoder(bytes.NewReader(out))
|
2017-09-05 23:38:15 +00:00
|
|
|
assert.Nil(dec.Decode(&req))
|
2017-08-30 21:01:42 +00:00
|
|
|
|
|
|
|
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)
|
2017-08-30 21:01:42 +00:00
|
|
|
}
|
2018-12-04 16:50:59 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
}
|
2017-08-30 21:01:42 +00:00
|
|
|
}
|