Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api: add Allocation client and server terminal status funcs. #10230

Merged
merged 1 commit into from
Mar 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.