2015-09-27 19:02:14 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2016-08-22 16:35:25 +00:00
|
|
|
"fmt"
|
2020-03-06 14:44:43 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2018-01-29 16:06:58 +00:00
|
|
|
"regexp"
|
2015-09-27 19:02:14 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2018-01-24 22:51:22 +00:00
|
|
|
"time"
|
|
|
|
|
2020-03-06 14:44:43 +00:00
|
|
|
"github.com/hashicorp/nomad/command/agent"
|
2018-01-24 22:51:22 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
2017-08-22 20:33:12 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
2016-08-22 16:35:25 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2016-08-19 02:25:32 +00:00
|
|
|
"github.com/hashicorp/nomad/testutil"
|
2015-09-27 19:02:14 +00:00
|
|
|
"github.com/mitchellh/cli"
|
2017-08-15 18:30:23 +00:00
|
|
|
"github.com/posener/complete"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2018-01-24 22:51:22 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2015-09-27 19:02:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAllocStatusCommand_Implements(t *testing.T) {
|
2017-07-21 04:24:21 +00:00
|
|
|
t.Parallel()
|
2015-09-27 19:02:14 +00:00
|
|
|
var _ cli.Command = &AllocStatusCommand{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAllocStatusCommand_Fails(t *testing.T) {
|
2017-07-21 04:24:21 +00:00
|
|
|
t.Parallel()
|
2017-07-21 04:07:32 +00:00
|
|
|
srv, _, url := testServer(t, false, nil)
|
|
|
|
defer srv.Shutdown()
|
2015-09-27 21:42:04 +00:00
|
|
|
|
2015-09-27 19:02:14 +00:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
cmd := &AllocStatusCommand{Meta: Meta{Ui: ui}}
|
|
|
|
|
|
|
|
// Fails on misuse
|
|
|
|
if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 {
|
|
|
|
t.Fatalf("expected exit code 1, got: %d", code)
|
|
|
|
}
|
2018-04-18 17:51:17 +00:00
|
|
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, commandErrorText(cmd)) {
|
2015-09-27 19:02:14 +00:00
|
|
|
t.Fatalf("expected help output, got: %s", out)
|
|
|
|
}
|
|
|
|
ui.ErrorWriter.Reset()
|
|
|
|
|
|
|
|
// Fails on connection failure
|
2016-01-21 19:53:05 +00:00
|
|
|
if code := cmd.Run([]string{"-address=nope", "foobar"}); code != 1 {
|
2015-09-27 19:02:14 +00:00
|
|
|
t.Fatalf("expected exit code 1, got: %d", code)
|
|
|
|
}
|
|
|
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error querying allocation") {
|
|
|
|
t.Fatalf("expected failed query error, got: %s", out)
|
|
|
|
}
|
2015-12-24 10:46:59 +00:00
|
|
|
ui.ErrorWriter.Reset()
|
2015-09-27 21:42:04 +00:00
|
|
|
|
|
|
|
// Fails on missing alloc
|
|
|
|
if code := cmd.Run([]string{"-address=" + url, "26470238-5CF2-438F-8772-DC67CFB0705C"}); code != 1 {
|
|
|
|
t.Fatalf("expected exit 1, got: %d", code)
|
|
|
|
}
|
2015-12-24 10:46:59 +00:00
|
|
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, "No allocation(s) with prefix or id") {
|
2015-09-27 21:42:04 +00:00
|
|
|
t.Fatalf("expected not found error, got: %s", out)
|
|
|
|
}
|
2016-01-21 21:21:35 +00:00
|
|
|
ui.ErrorWriter.Reset()
|
2016-01-21 19:53:05 +00:00
|
|
|
|
2016-01-21 21:21:35 +00:00
|
|
|
// Fail on identifier with too few characters
|
|
|
|
if code := cmd.Run([]string{"-address=" + url, "2"}); code != 1 {
|
2016-01-21 19:53:05 +00:00
|
|
|
t.Fatalf("expected exit 1, got: %d", code)
|
|
|
|
}
|
2016-01-21 21:21:35 +00:00
|
|
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, "must contain at least two characters.") {
|
|
|
|
t.Fatalf("expected too few characters error, got: %s", out)
|
|
|
|
}
|
|
|
|
ui.ErrorWriter.Reset()
|
|
|
|
|
|
|
|
// Identifiers with uneven length should produce a query result
|
|
|
|
if code := cmd.Run([]string{"-address=" + url, "123"}); code != 1 {
|
|
|
|
t.Fatalf("expected exit 1, got: %d", code)
|
2016-01-21 19:53:05 +00:00
|
|
|
}
|
2016-01-21 21:21:35 +00:00
|
|
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, "No allocation(s) with prefix or id") {
|
|
|
|
t.Fatalf("expected not found error, got: %s", out)
|
|
|
|
}
|
2016-08-06 12:38:41 +00:00
|
|
|
ui.ErrorWriter.Reset()
|
2016-01-21 21:21:35 +00:00
|
|
|
|
2016-08-06 12:38:41 +00:00
|
|
|
// Failed on both -json and -t options are specified
|
|
|
|
if code := cmd.Run([]string{"-address=" + url, "-json", "-t", "{{.ID}}"}); code != 1 {
|
|
|
|
t.Fatalf("expected exit 1, got: %d", code)
|
|
|
|
}
|
2017-07-07 02:20:57 +00:00
|
|
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, "Both json and template formatting are not allowed") {
|
2016-08-06 12:38:41 +00:00
|
|
|
t.Fatalf("expected getting formatter error, got: %s", out)
|
|
|
|
}
|
2015-09-27 19:02:14 +00:00
|
|
|
}
|
2016-08-19 02:25:32 +00:00
|
|
|
|
2020-03-22 12:20:42 +00:00
|
|
|
func TestAllocStatusCommand_LifecycleInfo(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
srv, client, url := testServer(t, true, nil)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
|
|
|
|
// Wait for a node to be ready
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
nodes, _, err := client.Nodes().List(nil)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
for _, node := range nodes {
|
|
|
|
if node.Status == structs.NodeStatusReady {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, fmt.Errorf("no ready nodes")
|
|
|
|
}, func(err error) {
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
cmd := &AllocStatusCommand{Meta: Meta{Ui: ui}}
|
|
|
|
state := srv.Agent.Server().State()
|
|
|
|
|
|
|
|
a := mock.Alloc()
|
|
|
|
a.Metrics = &structs.AllocMetric{}
|
|
|
|
tg := a.Job.LookupTaskGroup(a.TaskGroup)
|
|
|
|
|
|
|
|
initTask := tg.Tasks[0].Copy()
|
|
|
|
initTask.Name = "init_task"
|
|
|
|
initTask.Lifecycle = &structs.TaskLifecycleConfig{
|
|
|
|
Hook: "prestart",
|
|
|
|
}
|
|
|
|
|
|
|
|
prestartSidecarTask := tg.Tasks[0].Copy()
|
|
|
|
prestartSidecarTask.Name = "prestart_sidecar"
|
|
|
|
prestartSidecarTask.Lifecycle = &structs.TaskLifecycleConfig{
|
|
|
|
Hook: "prestart",
|
|
|
|
Sidecar: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
tg.Tasks = append(tg.Tasks, initTask, prestartSidecarTask)
|
|
|
|
a.TaskResources["init_task"] = a.TaskResources["web"]
|
|
|
|
a.TaskResources["prestart_sidecar"] = a.TaskResources["web"]
|
|
|
|
a.TaskStates = map[string]*structs.TaskState{
|
|
|
|
"web": &structs.TaskState{State: "pending"},
|
|
|
|
"init_task": &structs.TaskState{State: "running"},
|
|
|
|
"prestart_sidecar": &structs.TaskState{State: "running"},
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Nil(t, state.UpsertAllocs(1000, []*structs.Allocation{a}))
|
|
|
|
|
|
|
|
if code := cmd.Run([]string{"-address=" + url, a.ID}); code != 0 {
|
|
|
|
t.Fatalf("expected exit 0, got: %d", code)
|
|
|
|
}
|
|
|
|
out := ui.OutputWriter.String()
|
|
|
|
|
|
|
|
require.Contains(t, out, `Task "init_task" (prestart) is "running"`)
|
|
|
|
require.Contains(t, out, `Task "prestart_sidecar" (prestart sidecar) is "running"`)
|
|
|
|
require.Contains(t, out, `Task "web" is "pending"`)
|
|
|
|
}
|
|
|
|
|
2016-08-19 02:25:32 +00:00
|
|
|
func TestAllocStatusCommand_Run(t *testing.T) {
|
2017-07-21 04:24:21 +00:00
|
|
|
t.Parallel()
|
2017-07-21 04:07:32 +00:00
|
|
|
srv, client, url := testServer(t, true, nil)
|
|
|
|
defer srv.Shutdown()
|
2016-08-22 16:35:25 +00:00
|
|
|
|
|
|
|
// Wait for a node to be ready
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
nodes, _, err := client.Nodes().List(nil)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
for _, node := range nodes {
|
2018-12-20 06:05:17 +00:00
|
|
|
if _, ok := node.Drivers["mock_driver"]; ok &&
|
|
|
|
node.Status == structs.NodeStatusReady {
|
2016-08-22 16:35:25 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, fmt.Errorf("no ready nodes")
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
})
|
|
|
|
|
2016-08-19 02:25:32 +00:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
cmd := &AllocStatusCommand{Meta: Meta{Ui: ui}}
|
|
|
|
|
|
|
|
jobID := "job1_sfx"
|
|
|
|
job1 := testJob(jobID)
|
2017-05-10 03:52:47 +00:00
|
|
|
resp, _, err := client.Jobs().Register(job1, nil)
|
2016-08-19 02:25:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2017-05-10 03:52:47 +00:00
|
|
|
if code := waitForSuccess(ui, client, fullId, t, resp.EvalID); code != 0 {
|
2016-08-19 02:25:32 +00:00
|
|
|
t.Fatalf("status code non zero saw %d", code)
|
|
|
|
}
|
|
|
|
// get an alloc id
|
|
|
|
allocId1 := ""
|
2019-01-18 11:55:17 +00:00
|
|
|
nodeName := ""
|
2016-12-20 19:32:17 +00:00
|
|
|
if allocs, _, err := client.Jobs().Allocations(jobID, false, nil); err == nil {
|
2016-08-19 02:25:32 +00:00
|
|
|
if len(allocs) > 0 {
|
|
|
|
allocId1 = allocs[0].ID
|
2019-01-18 11:55:17 +00:00
|
|
|
nodeName = allocs[0].NodeName
|
2016-08-19 02:25:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if allocId1 == "" {
|
|
|
|
t.Fatal("unable to find an allocation")
|
|
|
|
}
|
|
|
|
|
|
|
|
if code := cmd.Run([]string{"-address=" + url, allocId1}); code != 0 {
|
|
|
|
t.Fatalf("expected exit 0, got: %d", code)
|
|
|
|
}
|
|
|
|
out := ui.OutputWriter.String()
|
2017-10-27 22:24:42 +00:00
|
|
|
if !strings.Contains(out, "Created") {
|
|
|
|
t.Fatalf("expected to have 'Created' but saw: %s", out)
|
2016-08-19 02:25:32 +00:00
|
|
|
}
|
2017-10-27 22:24:42 +00:00
|
|
|
|
|
|
|
if !strings.Contains(out, "Modified") {
|
|
|
|
t.Fatalf("expected to have 'Modified' but saw: %s", out)
|
|
|
|
}
|
|
|
|
|
2019-01-18 11:55:17 +00:00
|
|
|
nodeNameRegexpStr := fmt.Sprintf(`\nNode Name\s+= %s\n`, regexp.QuoteMeta(nodeName))
|
2019-01-23 17:33:22 +00:00
|
|
|
require.Regexp(t, regexp.MustCompile(nodeNameRegexpStr), out)
|
2019-01-18 11:55:17 +00:00
|
|
|
|
2016-08-19 02:25:32 +00:00
|
|
|
ui.OutputWriter.Reset()
|
|
|
|
|
|
|
|
if code := cmd.Run([]string{"-address=" + url, "-verbose", allocId1}); code != 0 {
|
|
|
|
t.Fatalf("expected exit 0, got: %d", code)
|
|
|
|
}
|
|
|
|
out = ui.OutputWriter.String()
|
|
|
|
if !strings.Contains(out, allocId1) {
|
2016-08-19 23:16:15 +00:00
|
|
|
t.Fatal("expected to find alloc id in output")
|
2016-08-19 02:25:32 +00:00
|
|
|
}
|
2017-10-27 22:24:42 +00:00
|
|
|
if !strings.Contains(out, "Created") {
|
|
|
|
t.Fatalf("expected to have 'Created' but saw: %s", out)
|
2016-08-19 02:25:32 +00:00
|
|
|
}
|
|
|
|
ui.OutputWriter.Reset()
|
2017-08-29 17:09:30 +00:00
|
|
|
|
|
|
|
// Try the query with an even prefix that includes the hyphen
|
|
|
|
if code := cmd.Run([]string{"-address=" + url, allocId1[:13]}); code != 0 {
|
|
|
|
t.Fatalf("expected exit 0, got: %d", code)
|
|
|
|
}
|
|
|
|
out = ui.OutputWriter.String()
|
2017-10-27 22:24:42 +00:00
|
|
|
if !strings.Contains(out, "Created") {
|
|
|
|
t.Fatalf("expected to have 'Created' but saw: %s", out)
|
2017-08-29 17:09:30 +00:00
|
|
|
}
|
|
|
|
ui.OutputWriter.Reset()
|
|
|
|
|
|
|
|
if code := cmd.Run([]string{"-address=" + url, "-verbose", allocId1}); code != 0 {
|
|
|
|
t.Fatalf("expected exit 0, got: %d", code)
|
|
|
|
}
|
|
|
|
out = ui.OutputWriter.String()
|
|
|
|
if !strings.Contains(out, allocId1) {
|
|
|
|
t.Fatal("expected to find alloc id in output")
|
|
|
|
}
|
|
|
|
ui.OutputWriter.Reset()
|
2018-01-24 22:51:22 +00:00
|
|
|
|
2018-01-29 22:31:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAllocStatusCommand_RescheduleInfo(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
srv, client, url := testServer(t, true, nil)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
|
|
|
|
// Wait for a node to be ready
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
nodes, _, err := client.Nodes().List(nil)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
for _, node := range nodes {
|
|
|
|
if node.Status == structs.NodeStatusReady {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, fmt.Errorf("no ready nodes")
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
})
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
cmd := &AllocStatusCommand{Meta: Meta{Ui: ui}}
|
2018-01-24 22:51:22 +00:00
|
|
|
// Test reschedule attempt info
|
|
|
|
require := require.New(t)
|
|
|
|
state := srv.Agent.Server().State()
|
|
|
|
a := mock.Alloc()
|
|
|
|
a.Metrics = &structs.AllocMetric{}
|
|
|
|
nextAllocId := uuid.Generate()
|
|
|
|
a.NextAllocation = nextAllocId
|
|
|
|
a.RescheduleTracker = &structs.RescheduleTracker{
|
|
|
|
Events: []*structs.RescheduleEvent{
|
|
|
|
{
|
2018-01-25 18:48:17 +00:00
|
|
|
RescheduleTime: time.Now().Add(-2 * time.Minute).UTC().UnixNano(),
|
2018-01-24 22:51:22 +00:00
|
|
|
PrevAllocID: uuid.Generate(),
|
|
|
|
PrevNodeID: uuid.Generate(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
require.Nil(state.UpsertAllocs(1000, []*structs.Allocation{a}))
|
|
|
|
|
|
|
|
if code := cmd.Run([]string{"-address=" + url, a.ID}); code != 0 {
|
|
|
|
t.Fatalf("expected exit 0, got: %d", code)
|
|
|
|
}
|
2018-01-29 22:31:25 +00:00
|
|
|
out := ui.OutputWriter.String()
|
|
|
|
require.Contains(out, "Replacement Alloc ID")
|
2018-01-29 16:06:58 +00:00
|
|
|
require.Regexp(regexp.MustCompile(".*Reschedule Attempts\\s*=\\s*1/2"), out)
|
2016-08-19 02:25:32 +00:00
|
|
|
}
|
2017-08-15 18:30:23 +00:00
|
|
|
|
2018-08-08 14:41:56 +00:00
|
|
|
func TestAllocStatusCommand_ScoreMetrics(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
srv, client, url := testServer(t, true, nil)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
|
|
|
|
// Wait for a node to be ready
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
nodes, _, err := client.Nodes().List(nil)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
for _, node := range nodes {
|
|
|
|
if node.Status == structs.NodeStatusReady {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, fmt.Errorf("no ready nodes")
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
})
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
cmd := &AllocStatusCommand{Meta: Meta{Ui: ui}}
|
2018-10-18 02:06:24 +00:00
|
|
|
// Test node metrics
|
2018-08-08 14:41:56 +00:00
|
|
|
require := require.New(t)
|
|
|
|
state := srv.Agent.Server().State()
|
|
|
|
a := mock.Alloc()
|
|
|
|
mockNode1 := mock.Node()
|
|
|
|
mockNode2 := mock.Node()
|
|
|
|
a.Metrics = &structs.AllocMetric{
|
2018-08-18 00:26:43 +00:00
|
|
|
ScoreMetaData: []*structs.NodeScoreMeta{
|
|
|
|
{
|
|
|
|
NodeID: mockNode1.ID,
|
|
|
|
Scores: map[string]float64{
|
|
|
|
"binpack": 0.77,
|
|
|
|
"node-affinity": 0.5,
|
|
|
|
},
|
2018-08-08 14:41:56 +00:00
|
|
|
},
|
2018-08-18 00:26:43 +00:00
|
|
|
{
|
|
|
|
NodeID: mockNode2.ID,
|
|
|
|
Scores: map[string]float64{
|
|
|
|
"binpack": 0.75,
|
|
|
|
"node-affinity": 0.33,
|
|
|
|
},
|
2018-08-08 14:41:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
require.Nil(state.UpsertAllocs(1000, []*structs.Allocation{a}))
|
|
|
|
|
|
|
|
if code := cmd.Run([]string{"-address=" + url, "-verbose", a.ID}); code != 0 {
|
|
|
|
t.Fatalf("expected exit 0, got: %d", code)
|
|
|
|
}
|
|
|
|
out := ui.OutputWriter.String()
|
|
|
|
require.Contains(out, "Placement Metrics")
|
2018-08-18 00:26:43 +00:00
|
|
|
require.Contains(out, mockNode1.ID)
|
|
|
|
require.Contains(out, mockNode2.ID)
|
2019-11-01 17:58:22 +00:00
|
|
|
|
|
|
|
// assert we sort headers alphabetically
|
|
|
|
require.Contains(out, "binpack node-affinity")
|
2018-10-18 02:06:24 +00:00
|
|
|
require.Contains(out, "final score")
|
2018-08-08 14:41:56 +00:00
|
|
|
}
|
|
|
|
|
2017-08-15 18:30:23 +00:00
|
|
|
func TestAllocStatusCommand_AutocompleteArgs(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
t.Parallel()
|
|
|
|
|
2017-08-22 20:33:12 +00:00
|
|
|
srv, _, url := testServer(t, true, nil)
|
2017-08-15 18:30:23 +00:00
|
|
|
defer srv.Shutdown()
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
cmd := &AllocStatusCommand{Meta: Meta{Ui: ui, flagAddress: url}}
|
|
|
|
|
2017-08-22 20:33:12 +00:00
|
|
|
// Create a fake alloc
|
|
|
|
state := srv.Agent.Server().State()
|
|
|
|
a := mock.Alloc()
|
|
|
|
assert.Nil(state.UpsertAllocs(1000, []*structs.Allocation{a}))
|
2017-08-15 18:30:23 +00:00
|
|
|
|
2017-08-22 20:33:12 +00:00
|
|
|
prefix := a.ID[:5]
|
2017-08-15 18:30:23 +00:00
|
|
|
args := complete.Args{Last: prefix}
|
|
|
|
predictor := cmd.AutocompleteArgs()
|
|
|
|
|
|
|
|
res := predictor.Predict(args)
|
|
|
|
assert.Equal(1, len(res))
|
2017-08-22 20:33:12 +00:00
|
|
|
assert.Equal(a.ID, res[0])
|
2017-08-15 18:30:23 +00:00
|
|
|
}
|
2020-03-06 14:44:43 +00:00
|
|
|
|
|
|
|
func TestAllocStatusCommand_HostVolumes(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
// We have to create a tempdir for the host volume even though we're
|
|
|
|
// not going to use it b/c the server validates the config on startup
|
|
|
|
tmpDir, err := ioutil.TempDir("", "vol0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create tempdir for test: %v", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
|
|
|
vol0 := uuid.Generate()
|
|
|
|
srv, _, url := testServer(t, true, func(c *agent.Config) {
|
|
|
|
c.Client.HostVolumes = []*structs.ClientHostVolumeConfig{
|
|
|
|
{
|
|
|
|
Name: vol0,
|
|
|
|
Path: tmpDir,
|
|
|
|
ReadOnly: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer srv.Shutdown()
|
|
|
|
state := srv.Agent.Server().State()
|
|
|
|
|
|
|
|
// Upsert the job and alloc
|
|
|
|
node := mock.Node()
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
alloc.Metrics = &structs.AllocMetric{}
|
|
|
|
alloc.NodeID = node.ID
|
|
|
|
job := alloc.Job
|
|
|
|
job.TaskGroups[0].Volumes = map[string]*structs.VolumeRequest{
|
|
|
|
vol0: {
|
|
|
|
Name: vol0,
|
|
|
|
Type: structs.VolumeTypeHost,
|
|
|
|
Source: tmpDir,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
job.TaskGroups[0].Tasks[0].VolumeMounts = []*structs.VolumeMount{
|
|
|
|
{
|
|
|
|
Volume: vol0,
|
|
|
|
Destination: "/var/www",
|
|
|
|
ReadOnly: true,
|
|
|
|
PropagationMode: "private",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
// fakes the placement enough so that we have something to iterate
|
|
|
|
// on in 'nomad alloc status'
|
|
|
|
alloc.TaskStates = map[string]*structs.TaskState{
|
|
|
|
"web": &structs.TaskState{
|
|
|
|
Events: []*structs.TaskEvent{
|
|
|
|
structs.NewTaskEvent("test event").SetMessage("test msg"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
summary := mock.JobSummary(alloc.JobID)
|
|
|
|
require.NoError(t, state.UpsertJobSummary(1004, summary))
|
|
|
|
require.NoError(t, state.UpsertAllocs(1005, []*structs.Allocation{alloc}))
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
cmd := &AllocStatusCommand{Meta: Meta{Ui: ui}}
|
|
|
|
if code := cmd.Run([]string{"-address=" + url, "-verbose", alloc.ID}); code != 0 {
|
|
|
|
t.Fatalf("expected exit 0, got: %d", code)
|
|
|
|
}
|
|
|
|
out := ui.OutputWriter.String()
|
|
|
|
require.Contains(t, out, "Host Volumes")
|
|
|
|
require.Contains(t, out, fmt.Sprintf("%s true", vol0))
|
|
|
|
require.NotContains(t, out, "CSI Volumes")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAllocStatusCommand_CSIVolumes(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
srv, _, url := testServer(t, true, nil)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
state := srv.Agent.Server().State()
|
|
|
|
|
|
|
|
// Upsert the node, plugin, and volume
|
|
|
|
vol0 := uuid.Generate()
|
|
|
|
node := mock.Node()
|
|
|
|
node.CSINodePlugins = map[string]*structs.CSIInfo{
|
|
|
|
"minnie": {
|
|
|
|
PluginID: "minnie",
|
|
|
|
Healthy: true,
|
|
|
|
NodeInfo: &structs.CSINodeInfo{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := state.UpsertNode(1001, node)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
vols := []*structs.CSIVolume{{
|
|
|
|
ID: vol0,
|
2020-03-17 15:35:34 +00:00
|
|
|
Namespace: structs.DefaultNamespace,
|
2020-03-06 14:44:43 +00:00
|
|
|
PluginID: "minnie",
|
|
|
|
AccessMode: structs.CSIVolumeAccessModeMultiNodeSingleWriter,
|
|
|
|
AttachmentMode: structs.CSIVolumeAttachmentModeFilesystem,
|
|
|
|
Topologies: []*structs.CSITopology{{
|
|
|
|
Segments: map[string]string{"foo": "bar"},
|
|
|
|
}},
|
|
|
|
}}
|
|
|
|
err = state.CSIVolumeRegister(1002, vols)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Upsert the job and alloc
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
alloc.Metrics = &structs.AllocMetric{}
|
|
|
|
alloc.NodeID = node.ID
|
|
|
|
job := alloc.Job
|
|
|
|
job.TaskGroups[0].Volumes = map[string]*structs.VolumeRequest{
|
|
|
|
vol0: {
|
|
|
|
Name: vol0,
|
|
|
|
Type: structs.VolumeTypeCSI,
|
|
|
|
Source: "/tmp/vol0",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
job.TaskGroups[0].Tasks[0].VolumeMounts = []*structs.VolumeMount{
|
|
|
|
{
|
|
|
|
Volume: vol0,
|
|
|
|
Destination: "/var/www",
|
|
|
|
ReadOnly: true,
|
|
|
|
PropagationMode: "private",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
// if we don't set a task state, there's nothing to iterate on alloc status
|
|
|
|
alloc.TaskStates = map[string]*structs.TaskState{
|
|
|
|
"web": &structs.TaskState{
|
|
|
|
Events: []*structs.TaskEvent{
|
|
|
|
structs.NewTaskEvent("test event").SetMessage("test msg"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
summary := mock.JobSummary(alloc.JobID)
|
|
|
|
require.NoError(t, state.UpsertJobSummary(1004, summary))
|
|
|
|
require.NoError(t, state.UpsertAllocs(1005, []*structs.Allocation{alloc}))
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
cmd := &AllocStatusCommand{Meta: Meta{Ui: ui}}
|
|
|
|
if code := cmd.Run([]string{"-address=" + url, "-verbose", alloc.ID}); code != 0 {
|
|
|
|
t.Fatalf("expected exit 0, got: %d", code)
|
|
|
|
}
|
|
|
|
out := ui.OutputWriter.String()
|
|
|
|
require.Contains(t, out, "CSI Volumes")
|
|
|
|
require.Contains(t, out, fmt.Sprintf("%s minnie", vol0))
|
|
|
|
require.NotContains(t, out, "Host Volumes")
|
|
|
|
}
|