From 953d5d9df3b23609582316a69ba34bc55c7c0832 Mon Sep 17 00:00:00 2001 From: Preetha Appan Date: Mon, 29 Jan 2018 16:31:25 -0600 Subject: [PATCH] Code review feedback --- api/allocations.go | 16 ++++++++-------- command/alloc_status.go | 2 +- command/alloc_status_test.go | 30 +++++++++++++++++++++++++++--- 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/api/allocations.go b/api/allocations.go index 1a34b7041..cf4400486 100644 --- a/api/allocations.go +++ b/api/allocations.go @@ -171,14 +171,15 @@ func (a Allocation) RescheduleInfo(t time.Time) (int, int) { reschedulePolicy = tg.ReschedulePolicy } } - var availableAttempts int - var interval time.Duration - if reschedulePolicy != nil { - availableAttempts = *reschedulePolicy.Attempts - interval = *reschedulePolicy.Interval + if reschedulePolicy == nil { + return 0, 0 } + availableAttempts := *reschedulePolicy.Attempts + interval := *reschedulePolicy.Interval + attempted := 0 + + // Loop over reschedule tracker to find attempts within the restart policy's interval if a.RescheduleTracker != nil && availableAttempts > 0 && interval > 0 { - attempted := 0 for j := len(a.RescheduleTracker.Events) - 1; j >= 0; j-- { lastAttempt := a.RescheduleTracker.Events[j].RescheduleTime timeDiff := t.UTC().UnixNano() - lastAttempt @@ -186,9 +187,8 @@ func (a Allocation) RescheduleInfo(t time.Time) (int, int) { attempted += 1 } } - return attempted, availableAttempts } - return 0, availableAttempts + return attempted, availableAttempts } // RescheduleTracker encapsulates previous reschedule events diff --git a/command/alloc_status.go b/command/alloc_status.go index 0eed217f5..8ba7bcb95 100644 --- a/command/alloc_status.go +++ b/command/alloc_status.go @@ -281,7 +281,7 @@ func formatAllocBasicInfo(alloc *api.Allocation, client *api.Client, uuidLength } if alloc.NextAllocation != "" { basic = append(basic, - fmt.Sprintf("Rescheduled Alloc ID|%s", limit(alloc.NextAllocation, uuidLength))) + fmt.Sprintf("Replacement Alloc ID|%s", limit(alloc.NextAllocation, uuidLength))) } if verbose { diff --git a/command/alloc_status_test.go b/command/alloc_status_test.go index 3e5165bb2..e30a3ff7c 100644 --- a/command/alloc_status_test.go +++ b/command/alloc_status_test.go @@ -173,6 +173,31 @@ func TestAllocStatusCommand_Run(t *testing.T) { } ui.OutputWriter.Reset() +} + +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}} // Test reschedule attempt info require := require.New(t) state := srv.Agent.Server().State() @@ -194,10 +219,9 @@ func TestAllocStatusCommand_Run(t *testing.T) { 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(out, "Rescheduled Alloc ID") + out := ui.OutputWriter.String() + require.Contains(out, "Replacement Alloc ID") require.Regexp(regexp.MustCompile(".*Reschedule Attempts\\s*=\\s*1/2"), out) - } func TestAllocStatusCommand_AutocompleteArgs(t *testing.T) {