Skip to content

Commit

Permalink
Stop tasks via task engine AddTask for full task sync
Browse files Browse the repository at this point in the history
  • Loading branch information
danehlim committed Aug 19, 2024
1 parent 6e5e459 commit d88c4ba
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 19 deletions.
5 changes: 3 additions & 2 deletions agent/acs/session/task_stop_verification_ack_responder.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ func (ts *taskStopper) StopTask(taskARN string) {
logger.Info("Stopping task from task stop verification ACK: %s", logger.Fields{
loggerfield.TaskARN: task.Arn,
})
task.SetDesiredStatus(apitaskstatus.TaskStopped)
task.UpdateDesiredStatus()
taskWithDesiredStatusStopped := *task
taskWithDesiredStatusStopped.SetDesiredStatus(apitaskstatus.TaskStopped)
ts.taskEngine.AddTask(&taskWithDesiredStatusStopped)
if err := ts.dataClient.SaveTask(task); err != nil {
logger.Error("Failed to save data for task", logger.Fields{
loggerfield.TaskARN: task.Arn,
Expand Down
19 changes: 19 additions & 0 deletions agent/acs/session/task_stop_verification_ack_responder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/aws/amazon-ecs-agent/agent/api/container"
"github.com/aws/amazon-ecs-agent/agent/api/task"
apitask "github.com/aws/amazon-ecs-agent/agent/api/task"
"github.com/aws/amazon-ecs-agent/agent/data"
mock_engine "github.com/aws/amazon-ecs-agent/agent/engine/mocks"
"github.com/aws/amazon-ecs-agent/ecs-agent/acs/model/ecsacs"
Expand Down Expand Up @@ -120,7 +121,9 @@ func TestTaskStopVerificationAckResponderStopsMultipleTasks(t *testing.T) {
}

tester.taskEngine.EXPECT().GetTaskByArn(taskARN2).Return(tasksOnInstance[taskARN2], true)
mockTaskEngineExpectAddTaskWithArnAndDesiredStatus(t, tester, tasksOnInstance, taskARN2, apitaskstatus.TaskStopped)
tester.taskEngine.EXPECT().GetTaskByArn(taskARN3).Return(tasksOnInstance[taskARN3], true)
mockTaskEngineExpectAddTaskWithArnAndDesiredStatus(t, tester, tasksOnInstance, taskARN3, apitaskstatus.TaskStopped)

handleTaskStopVerificationAck :=
tester.taskStopVerificationAckResponder.HandlerFunc().(func(message *ecsacs.TaskStopVerificationAck))
Expand Down Expand Up @@ -172,8 +175,11 @@ func TestTaskStopVerificationAckResponderStopsAllTasks(t *testing.T) {
}

tester.taskEngine.EXPECT().GetTaskByArn(taskARN1).Return(tasksOnInstance[taskARN1], true)
mockTaskEngineExpectAddTaskWithArnAndDesiredStatus(t, tester, tasksOnInstance, taskARN1, apitaskstatus.TaskStopped)
tester.taskEngine.EXPECT().GetTaskByArn(taskARN2).Return(tasksOnInstance[taskARN2], true)
mockTaskEngineExpectAddTaskWithArnAndDesiredStatus(t, tester, tasksOnInstance, taskARN2, apitaskstatus.TaskStopped)
tester.taskEngine.EXPECT().GetTaskByArn(taskARN3).Return(tasksOnInstance[taskARN3], true)
mockTaskEngineExpectAddTaskWithArnAndDesiredStatus(t, tester, tasksOnInstance, taskARN3, apitaskstatus.TaskStopped)

handleTaskStopVerificationAck :=
tester.taskStopVerificationAckResponder.HandlerFunc().(func(message *ecsacs.TaskStopVerificationAck))
Expand All @@ -186,3 +192,16 @@ func TestTaskStopVerificationAckResponderStopsAllTasks(t *testing.T) {
assert.Equal(t, apicontainerstatus.ContainerStopped, task.Containers[0].GetDesiredStatus())
}
}

// mockTaskEngineExpectAddTaskWithArnAndDesiredStatus expects the mock task engine to call AddTask and asserts that the
// added task has task ARN `taskARN` and desired status `desiredStatus` before setting and updating the desired status
// of the task and its containers on the instance.
func mockTaskEngineExpectAddTaskWithArnAndDesiredStatus(t *testing.T, tester *taskStopVerificationAckTestHelper,
tasksOnInstance map[string]*task.Task, taskARN string, desiredStatus apitaskstatus.TaskStatus) {
tester.taskEngine.EXPECT().AddTask(gomock.Any()).Do(func(addedTask *apitask.Task) {
assert.Equal(t, taskARN, addedTask.Arn)
assert.Equal(t, desiredStatus, addedTask.GetDesiredStatus())
tasksOnInstance[taskARN].SetDesiredStatus(desiredStatus)
tasksOnInstance[taskARN].UpdateDesiredStatus()
})
}
13 changes: 0 additions & 13 deletions agent/engine/common_integ_testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@ var (
sdkClientFactory sdkclientfactory.Factory
)

const (
taskSteadyStatePollInterval = 100 * time.Millisecond
taskSteadyStatePollIntervalJitter = 10 * time.Millisecond
)

func init() {
sdkClientFactory = sdkclientfactory.NewFactory(context.TODO(), dockerEndpoint)
}
Expand Down Expand Up @@ -124,10 +119,6 @@ func setupGMSALinux(cfg *config.Config, state dockerstate.TaskEngineState, t *te
taskEngine := NewDockerTaskEngine(cfg, dockerClient, credentialsManager,
eventstream.NewEventStream("ENGINEINTEGTEST", context.Background()), imageManager, &hostResourceManager, state, metadataManager,
resourceFields, execcmd.NewManager(), engineserviceconnect.NewManager(), daemonManagers)
// Set the steady state poll interval to a low value so that tasks transition from their current state to their
// desired state faster. This prevents tests from appearing to hang while waiting for state change events.
taskEngine.taskSteadyStatePollInterval = taskSteadyStatePollInterval
taskEngine.taskSteadyStatePollIntervalJitter = taskSteadyStatePollIntervalJitter
taskEngine.MustInit(context.TODO())
return taskEngine, func() {
taskEngine.Shutdown()
Expand Down Expand Up @@ -269,10 +260,6 @@ func SetupIntegTestTaskEngine(cfg *config.Config, state dockerstate.TaskEngineSt
taskEngine := NewDockerTaskEngine(cfg, dockerClient, credentialsManager,
eventstream.NewEventStream("ENGINEINTEGTEST", context.Background()), imageManager, &hostResourceManager, state, metadataManager,
nil, execcmd.NewManager(), engineserviceconnect.NewManager(), daemonManagers)
// Set the steady state poll interval to a low value so that tasks transition from their current state to their
// desired state faster. This prevents tests from appearing to hang while waiting for state change events.
taskEngine.taskSteadyStatePollInterval = taskSteadyStatePollInterval
taskEngine.taskSteadyStatePollIntervalJitter = taskSteadyStatePollIntervalJitter
taskEngine.MustInit(context.TODO())
return taskEngine, func() {
taskEngine.Shutdown()
Expand Down
4 changes: 0 additions & 4 deletions agent/engine/engine_windows_integ_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,10 +560,6 @@ func setupGMSA(cfg *config.Config, state dockerstate.TaskEngineState, t *testing
taskEngine := NewDockerTaskEngine(cfg, dockerClient, credentialsManager,
eventstream.NewEventStream("ENGINEINTEGTEST", context.Background()), imageManager, &hostResourceManager, state, metadataManager,
resourceFields, execcmd.NewManager(), engineserviceconnect.NewManager())
// Set the steady state poll interval to a low value so that tasks transition from their current state to their
// desired state faster. This prevents tests from appearing to hang while waiting for state change events.
taskEngine.taskSteadyStatePollInterval = taskSteadyStatePollInterval
taskEngine.taskSteadyStatePollIntervalJitter = taskSteadyStatePollIntervalJitter
taskEngine.MustInit(context.TODO())
return taskEngine, func() {
taskEngine.Shutdown()
Expand Down

0 comments on commit d88c4ba

Please sign in to comment.