From 9dbd0175ea289600212e3ca3862eb3ff6e5ffbf9 Mon Sep 17 00:00:00 2001 From: Alexander Michaud <131308558+armichaud@users.noreply.github.com> Date: Wed, 16 Oct 2024 04:54:30 -0400 Subject: [PATCH] [ECS] Force New Deployment (#8232) --- moto/ecs/models.py | 7 +++++++ tests/test_ecs/test_ecs_boto3.py | 16 ++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/moto/ecs/models.py b/moto/ecs/models.py index 302fa2efd271..fec148d86b51 100644 --- a/moto/ecs/models.py +++ b/moto/ecs/models.py @@ -1692,6 +1692,7 @@ def update_service(self, service_properties: Dict[str, Any]) -> Service: task_definition_str = service_properties.pop("task_definition", None) cluster = self._get_cluster(cluster_str) service_name = service_properties.pop("service").split("/")[-1] + force_new_deployment = service_properties.pop("force_new_deployment", False) cluster_service_pair = f"{cluster.name}:{service_name}" if cluster_service_pair in self.services: @@ -1705,6 +1706,12 @@ def update_service(self, service_properties: Dict[str, Any]) -> Service: if task_definition_str: self.describe_task_definition(task_definition_str) current_service.task_definition = task_definition_str + if force_new_deployment and current_service.deployments: + deployment = current_service.deployments[0] + deployment["id"] = f"ecs-svc/{mock_random.randint(0, 32**12)}" + now = datetime.now(timezone.utc) + deployment["createdAt"] = now + deployment["updatedAt"] = now return current_service else: raise ServiceNotFoundException diff --git a/tests/test_ecs/test_ecs_boto3.py b/tests/test_ecs/test_ecs_boto3.py index 0aaec121d1a0..5f6baba84c7c 100644 --- a/tests/test_ecs/test_ecs_boto3.py +++ b/tests/test_ecs/test_ecs_boto3.py @@ -1225,6 +1225,8 @@ def test_update_service(): desiredCount=2, ) assert response["service"]["desiredCount"] == 2 + id = response["service"]["deployments"][0]["id"] + created_at = response["service"]["deployments"][0]["createdAt"] response = client.update_service( cluster="test_ecs_cluster", @@ -1248,6 +1250,20 @@ def test_update_service(): assert response["service"]["runningCount"] == 1 assert response["service"]["pendingCount"] == 0 + response = client.update_service( + cluster="test_ecs_cluster", + service="test_ecs_service", + taskDefinition="test_ecs_task", + desiredCount=1, + forceNewDeployment=True, + ) + assert response["service"]["deployments"][0]["id"] != id + assert response["service"]["deployments"][0]["createdAt"] != created_at + assert ( + response["service"]["deployments"][0]["createdAt"] + == response["service"]["deployments"][0]["updatedAt"] + ) + @mock_aws def test_update_missing_service():