From 6719d22cd688f3609c8f5fcdb094d180623912eb Mon Sep 17 00:00:00 2001 From: Ryan Uber Date: Thu, 17 Sep 2015 20:18:33 -0700 Subject: [PATCH] command/monitor: handle more alloc state changes --- command/monitor.go | 39 ++++++++++++++++++++--------------- command/monitor_test.go | 45 +++++++++++++++++++++++++++++++++++------ 2 files changed, 62 insertions(+), 22 deletions(-) diff --git a/command/monitor.go b/command/monitor.go index 2280ec2af..b6b6e1e00 100644 --- a/command/monitor.go +++ b/command/monitor.go @@ -100,25 +100,32 @@ func (m *monitor) update(eval *api.Evaluation, allocs []*api.AllocationListStub) // Check the allocations for allocID, alloc := range update.allocs { if existing, ok := existing.allocs[allocID]; !ok { - // Check if this is a failure indication allocation - if alloc.desired == structs.AllocDesiredStatusFailed { - m.output(fmt.Sprintf("Scheduling failed for task group %q", + switch { + case alloc.desired == structs.AllocDesiredStatusFailed: + // New allocs with desired state failed indicate + // scheduling failure. + m.output(fmt.Sprintf("Scheduling failed for group %q", alloc.group)) - } else { - if alloc.index < update.index { - m.output(fmt.Sprintf( - "Allocation %q updated (node %q, task group %q)", - alloc.id, alloc.node, alloc.group)) - } else { - m.output(fmt.Sprintf( - "Allocation %q created on node %q for task group %q", - alloc.id, alloc.node, alloc.group)) - } + + case alloc.index < update.index: + // New alloc with create index lower than the eval + // create index indicates modification + m.output(fmt.Sprintf( + "Allocation %q modified: node %q, group %q", + alloc.id, alloc.node, alloc.group)) + + case alloc.desired == structs.AllocDesiredStatusRun: + // New allocation with desired status running + m.output(fmt.Sprintf( + "Allocation %q created: node %q, group %q", + alloc.id, alloc.node, alloc.group)) } } else { - if existing.client != alloc.client { + switch { + case existing.client != alloc.client: + // Allocation status has changed m.output(fmt.Sprintf( - "Allocation %q changed status from %q to %q", + "Allocation %q status changed: %q -> %q", alloc.id, existing.client, alloc.client)) } } @@ -126,7 +133,7 @@ func (m *monitor) update(eval *api.Evaluation, allocs []*api.AllocationListStub) // Check if the status changed if existing.status != update.status { - m.output(fmt.Sprintf("Evaluation changed status from %q to %q", + m.output(fmt.Sprintf("Evaluation status changed: %q -> %q", existing.status, eval.Status)) } diff --git a/command/monitor_test.go b/command/monitor_test.go index 19d7000fd..db9291d2e 100644 --- a/command/monitor_test.go +++ b/command/monitor_test.go @@ -16,9 +16,10 @@ func TestMonitor_Update(t *testing.T) { // Basic eval updates work eval := &api.Evaluation{ - Status: "pending", - NodeID: "node1", - Wait: 10 * time.Second, + Status: "pending", + NodeID: "node1", + Wait: 10 * time.Second, + CreateIndex: 2, } mon.update(eval, nil) @@ -50,14 +51,15 @@ func TestMonitor_Update(t *testing.T) { } ui.OutputWriter.Reset() - // Allocations write new logs + // New allocations write new logs allocs := []*api.AllocationListStub{ &api.AllocationListStub{ ID: "alloc1", TaskGroup: "group1", NodeID: "node1", - DesiredStatus: "running", - ClientStatus: "pending", + DesiredStatus: structs.AllocDesiredStatusRun, + ClientStatus: structs.AllocClientStatusPending, + CreateIndex: 3, }, } mon.update(eval, allocs) @@ -73,6 +75,9 @@ func TestMonitor_Update(t *testing.T) { if !strings.Contains(out, "node1") { t.Fatalf("missing node\n\n%s", out) } + if !strings.Contains(out, "created") { + t.Fatalf("missing created\n\n%s", out) + } ui.OutputWriter.Reset() // No change yields no logs @@ -99,8 +104,10 @@ func TestMonitor_Update(t *testing.T) { // New allocs with desired status failed warns allocs = append(allocs, &api.AllocationListStub{ + ID: "alloc2", TaskGroup: "group2", DesiredStatus: structs.AllocDesiredStatusFailed, + CreateIndex: 4, }) mon.update(eval, allocs) @@ -112,6 +119,32 @@ func TestMonitor_Update(t *testing.T) { if !strings.Contains(out, "Scheduling failed") { t.Fatalf("missing failure\n\n%s", out) } + ui.OutputWriter.Reset() + + // New allocs with a create index lower than the + // eval create index are logged as modifications + allocs = append(allocs, &api.AllocationListStub{ + ID: "alloc3", + NodeID: "node1", + TaskGroup: "group2", + CreateIndex: 1, + }) + mon.update(eval, allocs) + + // Modification was logged + out = ui.OutputWriter.String() + if !strings.Contains(out, "alloc3") { + 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, "node1") { + t.Fatalf("missing node\n\n%s", out) + } + if !strings.Contains(out, "modified") { + t.Fatalf("missing modification\n\n%s", out) + } } func TestMonitor_Monitor(t *testing.T) {