open-nomad/command/monitor_test.go

219 lines
5.2 KiB
Go
Raw Normal View History

2015-09-17 06:09:39 +00:00
package command
import (
"strings"
"testing"
"time"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/mitchellh/cli"
)
func TestMonitor_Update_Eval(t *testing.T) {
2017-07-21 04:24:21 +00:00
t.Parallel()
2020-10-05 14:07:41 +00:00
ui := cli.NewMockUi()
mon := newMonitor(ui, nil, fullId)
2015-09-17 06:09:39 +00:00
2015-09-21 22:55:10 +00:00
// Evals triggered by jobs log
state := &evalState{
status: structs.EvalStatusPending,
2015-09-21 22:55:10 +00:00
job: "job1",
2015-09-17 06:09:39 +00:00
}
mon.update(state)
2015-09-17 06:09:39 +00:00
out := ui.OutputWriter.String()
2015-09-21 22:55:10 +00:00
if !strings.Contains(out, "job1") {
t.Fatalf("missing job\n\n%s", out)
}
ui.OutputWriter.Reset()
2018-03-11 19:06:15 +00:00
// Evals triggered by nodes log
2015-09-21 22:55:10 +00:00
state = &evalState{
status: structs.EvalStatusPending,
node: "12345678-abcd-efab-cdef-123456789abc",
2015-09-21 22:55:10 +00:00
}
mon.update(state)
out = ui.OutputWriter.String()
if !strings.Contains(out, "12345678-abcd-efab-cdef-123456789abc") {
2015-09-17 06:09:39 +00:00
t.Fatalf("missing node\n\n%s", out)
}
2015-09-21 19:53:48 +00:00
// Transition to pending should not be logged
if strings.Contains(out, structs.EvalStatusPending) {
t.Fatalf("should skip status\n\n%s", out)
}
2015-09-17 06:09:39 +00:00
ui.OutputWriter.Reset()
// No logs sent if no update
mon.update(state)
2015-09-17 06:09:39 +00:00
if out := ui.OutputWriter.String(); out != "" {
t.Fatalf("expected no output\n\n%s", out)
}
// Status change sends more logs
state = &evalState{
status: structs.EvalStatusComplete,
node: "12345678-abcd-efab-cdef-123456789abc",
}
mon.update(state)
2015-09-17 06:09:39 +00:00
out = ui.OutputWriter.String()
if !strings.Contains(out, structs.EvalStatusComplete) {
2015-09-17 06:09:39 +00:00
t.Fatalf("missing status\n\n%s", out)
}
}
func TestMonitor_Update_Allocs(t *testing.T) {
2017-07-21 04:24:21 +00:00
t.Parallel()
2020-10-05 14:07:41 +00:00
ui := cli.NewMockUi()
mon := newMonitor(ui, nil, fullId)
2015-09-17 06:09:39 +00:00
// New allocations write new logs
state := &evalState{
allocs: map[string]*allocState{
2017-09-26 22:26:33 +00:00
"alloc1": {
id: "87654321-abcd-efab-cdef-123456789abc",
group: "group1",
node: "12345678-abcd-efab-cdef-123456789abc",
desired: structs.AllocDesiredStatusRun,
client: structs.AllocClientStatusPending,
index: 1,
},
2015-09-17 06:09:39 +00:00
},
}
mon.update(state)
2015-09-17 06:09:39 +00:00
// Logs were output
out := ui.OutputWriter.String()
if !strings.Contains(out, "87654321-abcd-efab-cdef-123456789abc") {
2015-09-17 06:09:39 +00:00
t.Fatalf("missing alloc\n\n%s", out)
}
if !strings.Contains(out, "group1") {
t.Fatalf("missing group\n\n%s", out)
}
if !strings.Contains(out, "12345678-abcd-efab-cdef-123456789abc") {
2015-09-17 06:09:39 +00:00
t.Fatalf("missing node\n\n%s", out)
}
if !strings.Contains(out, "created") {
t.Fatalf("missing created\n\n%s", out)
}
2015-09-17 06:09:39 +00:00
ui.OutputWriter.Reset()
// No change yields no logs
mon.update(state)
2015-09-17 06:09:39 +00:00
if out := ui.OutputWriter.String(); out != "" {
t.Fatalf("expected no output\n\n%s", out)
}
ui.OutputWriter.Reset()
// Alloc updates cause more log lines
state = &evalState{
allocs: map[string]*allocState{
2017-09-26 22:26:33 +00:00
"alloc1": {
id: "87654321-abcd-efab-cdef-123456789abc",
group: "group1",
node: "12345678-abcd-efab-cdef-123456789abc",
desired: structs.AllocDesiredStatusRun,
client: structs.AllocClientStatusRunning,
index: 2,
},
},
}
mon.update(state)
// Updates were logged
2015-09-17 06:09:39 +00:00
out = ui.OutputWriter.String()
if !strings.Contains(out, "87654321-abcd-efab-cdef-123456789abc") {
2015-09-17 06:09:39 +00:00
t.Fatalf("missing alloc\n\n%s", out)
}
if !strings.Contains(out, "pending") {
t.Fatalf("missing old status\n\n%s", out)
}
if !strings.Contains(out, "running") {
t.Fatalf("missing new status\n\n%s", out)
}
}
func TestMonitor_Update_AllocModification(t *testing.T) {
2017-07-21 04:24:21 +00:00
t.Parallel()
2020-10-05 14:07:41 +00:00
ui := cli.NewMockUi()
mon := newMonitor(ui, nil, fullId)
// New allocs with a create index lower than the
// eval create index are logged as modifications
state := &evalState{
index: 2,
allocs: map[string]*allocState{
2017-09-26 22:26:33 +00:00
"alloc3": {
id: "87654321-abcd-bafe-cdef-123456789abc",
node: "12345678-abcd-efab-cdef-123456789abc",
group: "group2",
index: 1,
},
},
}
mon.update(state)
// Modification was logged
out := ui.OutputWriter.String()
if !strings.Contains(out, "87654321-abcd-bafe-cdef-123456789abc") {
t.Fatalf("missing alloc\n\n%s", out)
}
if !strings.Contains(out, "group2") {
t.Fatalf("missing group\n\n%s", out)
}
if !strings.Contains(out, "12345678-abcd-efab-cdef-123456789abc") {
t.Fatalf("missing node\n\n%s", out)
}
if !strings.Contains(out, "modified") {
t.Fatalf("missing modification\n\n%s", out)
}
2015-09-17 06:09:39 +00:00
}
2015-09-17 17:01:01 +00:00
func TestMonitor_Monitor(t *testing.T) {
2017-07-21 04:24:21 +00:00
t.Parallel()
2017-07-21 04:07:32 +00:00
srv, client, _ := testServer(t, false, nil)
defer srv.Shutdown()
2015-09-17 17:01:01 +00:00
// Create the monitor
2020-10-05 14:07:41 +00:00
ui := cli.NewMockUi()
mon := newMonitor(ui, client, fullId)
2015-09-17 17:01:01 +00:00
// Submit a job - this creates a new evaluation we can monitor
job := testJob("job1")
resp, _, err := client.Jobs().Register(job, nil)
2015-09-17 17:01:01 +00:00
if err != nil {
t.Fatalf("err: %s", err)
}
// Start monitoring the eval
var code int
doneCh := make(chan struct{})
go func() {
defer close(doneCh)
code = mon.monitor(resp.EvalID)
2015-09-17 17:01:01 +00:00
}()
// Wait for completion
select {
case <-doneCh:
case <-time.After(5 * time.Second):
t.Fatalf("eval monitor took too long")
}
// Check the return code. We should get exit code 2 as there
// would be a scheduling problem on the test server (no clients).
if code != 2 {
t.Fatalf("expect exit 2, got: %d", code)
}
// Check the output
out := ui.OutputWriter.String()
if !strings.Contains(out, resp.EvalID) {
t.Fatalf("missing eval\n\n%s", out)
}
if !strings.Contains(out, "finished with status") {
t.Fatalf("missing final status\n\n%s", out)
}
}