Skip to content

Commit

Permalink
Merge pull request #10230 from hashicorp/f-add-allocation-terminal-st…
Browse files Browse the repository at this point in the history
…atus-funcs-api

api: add Allocation client and server terminal status funcs.
  • Loading branch information
jrasell authored Mar 26, 2021
2 parents 15cf77a + 788f9f2 commit db37617
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
22 changes: 22 additions & 0 deletions api/allocations.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,28 @@ func (a *Allocation) Stub() *AllocationListStub {
}
}

// ServerTerminalStatus returns true if the desired state of the allocation is
// terminal.
func (a *Allocation) ServerTerminalStatus() bool {
switch a.DesiredStatus {
case AllocDesiredStatusStop, AllocDesiredStatusEvict:
return true
default:
return false
}
}

// ClientTerminalStatus returns true if the client status is terminal and will
// therefore no longer transition.
func (a *Allocation) ClientTerminalStatus() bool {
switch a.ClientStatus {
case AllocClientStatusComplete, AllocClientStatusFailed, AllocClientStatusLost:
return true
default:
return false
}
}

// AllocationListStub is used to return a subset of an allocation
// during list operations.
type AllocationListStub struct {
Expand Down
74 changes: 74 additions & 0 deletions api/allocations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,80 @@ func TestAllocations_ExecErrors(t *testing.T) {
require.Equal(t, err.Error(), fmt.Sprintf("Unknown allocation \"%s\"", allocID))
}

func TestAllocation_ServerTerminalStatus(t *testing.T) {
t.Parallel()

testCases := []struct {
inputAllocation *Allocation
expectedOutput bool
name string
}{
{
inputAllocation: &Allocation{DesiredStatus: AllocDesiredStatusEvict},
expectedOutput: true,
name: "alloc desired status evict",
},
{
inputAllocation: &Allocation{DesiredStatus: AllocDesiredStatusStop},
expectedOutput: true,
name: "alloc desired status stop",
},
{
inputAllocation: &Allocation{DesiredStatus: AllocDesiredStatusRun},
expectedOutput: false,
name: "alloc desired status run",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
require.Equal(t, tc.expectedOutput, tc.inputAllocation.ServerTerminalStatus(), tc.name)
})
}
}

func TestAllocation_ClientTerminalStatus(t *testing.T) {
t.Parallel()

testCases := []struct {
inputAllocation *Allocation
expectedOutput bool
name string
}{
{
inputAllocation: &Allocation{ClientStatus: AllocClientStatusLost},
expectedOutput: true,
name: "alloc client status lost",
},
{
inputAllocation: &Allocation{ClientStatus: AllocClientStatusFailed},
expectedOutput: true,
name: "alloc client status failed",
},
{
inputAllocation: &Allocation{ClientStatus: AllocClientStatusComplete},
expectedOutput: true,
name: "alloc client status complete",
},
{
inputAllocation: &Allocation{ClientStatus: AllocClientStatusRunning},
expectedOutput: false,
name: "alloc client status complete",
},
{
inputAllocation: &Allocation{ClientStatus: AllocClientStatusPending},
expectedOutput: false,
name: "alloc client status running",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
require.Equal(t, tc.expectedOutput, tc.inputAllocation.ClientTerminalStatus(), tc.name)
})
}
}

func TestAllocations_ShouldMigrate(t *testing.T) {
t.Parallel()
require.True(t, DesiredTransition{Migrate: boolToPtr(true)}.ShouldMigrate())
Expand Down
22 changes: 22 additions & 0 deletions vendor/github.com/hashicorp/nomad/api/allocations.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit db37617

Please sign in to comment.