Skip to content

Simplify ContainerStatus.BackendStatusString() method #4168

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

Merged
merged 2 commits into from
May 8, 2024
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

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

17 changes: 2 additions & 15 deletions ecs-agent/api/container/status/containerstatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@ func (cs *ContainerStatus) ShouldReportToBackend(steadyStateStatus ContainerStat

// BackendStatus maps the internal container status in the agent to that in the
// backend
//
// Deprecated: Use BackendStatusString instead
func (cs *ContainerStatus) BackendStatus(steadyStateStatus ContainerStatus) ContainerStatus {
if *cs == steadyStateStatus {
return ContainerRunning
Expand All @@ -120,20 +118,9 @@ func (cs *ContainerStatus) BackendStatus(steadyStateStatus ContainerStatus) Cont

// BackendStatusString maps the internal container status in Agent to a backend recognized
// status string.
//
// Container steady state can be provided as an option. If provided, it will be used to
// determine if the backend status is "RUNNING". If not provided, then ContainerRunning is
// used as the default steady state.
func (cs ContainerStatus) BackendStatusString(steadyStateStatusPtr *ContainerStatus) string {
var steadyState ContainerStatus
if steadyStateStatusPtr != nil {
steadyState = *steadyStateStatusPtr
} else {
steadyState = ContainerRunning
}

func (cs ContainerStatus) BackendStatusString() string {
switch cs {
case steadyState:
case ContainerRunning:
return "RUNNING"
case ContainerStopped:
return "STOPPED"
Expand Down
42 changes: 1 addition & 41 deletions ecs-agent/api/container/status/containerstatus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,47 +340,7 @@ func TestContainerBackendStatusStringDefaultSteadyState(t *testing.T) {
}
for _, tc := range tcs {
t.Run(fmt.Sprintf("%d", tc.status), func(t *testing.T) {
assert.Equal(t, tc.expected, tc.status.BackendStatusString(nil))
assert.Equal(t, tc.expected, tc.status.BackendStatusString())
})
}
}

// Tests for BackendStatusString method when a steady state is provided.
func TestBackendStatusSteadyStateProvided(t *testing.T) {
containerRunning := ContainerRunning
containerResourcesProvisioned := ContainerResourcesProvisioned

// Test states that should map to PENDING regardless of steady state
pendingStates := []ContainerStatus{
ContainerStatusNone, ContainerManifestPulled, ContainerPulled,
ContainerCreated, ContainerZombie,
}
for _, tc := range pendingStates {
t.Run(fmt.Sprintf("pending - %d", tc), func(t *testing.T) {
assert.Equal(t, "PENDING", tc.BackendStatusString(&containerRunning))
assert.Equal(t, "PENDING", tc.BackendStatusString(&containerResourcesProvisioned))
})
}

// Test that ContainerStopped maps to STOPPED regardless of steady state
t.Run("ContainerStopped maps to STOPPED", func(t *testing.T) {
assert.Equal(t, "STOPPED", ContainerStopped.BackendStatusString(&containerRunning))
assert.Equal(t, "STOPPED", ContainerStopped.BackendStatusString(&containerResourcesProvisioned))
})

// Test that steady state maps to RUNNING
t.Run("ContainerRunning maps to RUNNING when steady state is ContainerRunning", func(t *testing.T) {
assert.Equal(t, "RUNNING", ContainerRunning.BackendStatusString(&containerRunning))
})
t.Run("ContainerResourcesProvisioned maps to RUNNING when steady state is ContainerResourcesProvisioned", func(t *testing.T) {
assert.Equal(t, "RUNNING", ContainerResourcesProvisioned.BackendStatusString(&containerResourcesProvisioned))
})

// Test that non-steady non-STOPPED state maps to PENDING
t.Run("ContainerRunning maps to PENDING when steady state is ContainerResourcesProvisioned", func(t *testing.T) {
assert.Equal(t, "PENDING", ContainerRunning.BackendStatusString(&containerResourcesProvisioned))
})
t.Run("ContainerResourcesProvisioned maps to PENDING when steady state is ContainerRunning", func(t *testing.T) {
assert.Equal(t, "PENDING", ContainerResourcesProvisioned.BackendStatusString(&containerRunning))
})
}