Esempio n. 1
0
 def test_describe_tasks(self):
     client_return_response = {
         "tasks": [{
             "containers": [{
                 "name":
                 self.TEST_CONTAINER,
                 "exitcode":
                 123,
                 "lastStatus":
                 "RUNNING",
                 "networkInterfaces": [
                     {
                         "privateIpv4Address": self.TEST_IP_ADDRESS,
                     },
                 ],
             }],
             "taskArn":
             self.TEST_TASK_ARN,
         }]
     }
     self.gw.client.describe_tasks = MagicMock(
         return_value=client_return_response)
     tasks = [
         self.TEST_TASK_DEFINITION,
     ]
     tasks = self.gw.describe_tasks(self.TEST_CLUSTER, tasks)
     expected_tasks = [
         ContainerInstance(
             self.TEST_TASK_ARN,
             self.TEST_IP_ADDRESS,
             ContainerInstanceStatus.STARTED,
         ),
     ]
     self.assertEqual(tasks, expected_tasks)
     self.gw.client.describe_tasks.assert_called()
Esempio n. 2
0
 def test_start_containers(self):
     mocked_container_info = [
         ContainerInstance(
             "arn:aws:ecs:region:account_id:task/container_id_1",
             "192.0.2.0",
             ContainerInstanceStatus.STARTED,
         ),
         ContainerInstance(
             "arn:aws:ecs:region:account_id:task/container_id_2",
             "192.0.2.1",
             ContainerInstanceStatus.STARTED,
         ),
     ]
     self.onedocker_svc.container_svc.create_instances_async = AsyncMock(
         return_value=mocked_container_info
     )
     returned_container_info = self.onedocker_svc.start_containers(
         "task_def", "project/exe_name", ["--k1=v1", "--k2=v2"]
     )
     self.assertEqual(returned_container_info, mocked_container_info)
Esempio n. 3
0
 def test_start_container(self):
     mocked_container_info = ContainerInstance(
         "arn:aws:ecs:region:account_id:task/container_id",
         "192.0.2.0",
         ContainerInstanceStatus.STARTED,
     )
     self.onedocker_svc.container_svc.create_instances_async = AsyncMock(
         return_value=[mocked_container_info]
     )
     returned_container_info = self.onedocker_svc.start_container(
         "task_def", "project/exe_name", "cmd_args"
     )
     self.assertEqual(returned_container_info, mocked_container_info)
Esempio n. 4
0
 def test_update_instance(self):
     self.mpc_service.instance_repository.read = MagicMock(
         side_effect=self._read_side_effect_update)
     container_instances = [
         ContainerInstance(
             "arn:aws:ecs:us-west-1:592513842793:task/cd34aed2-321f-49d1-8641-c54baff8b77b",  # noqa
             "10.0.1.130",
             ContainerInstanceStatus.STARTED,
         )
     ]
     self.mpc_service.container_svc.get_instances = MagicMock(
         return_value=container_instances)
     self.mpc_service.update_instance(TEST_INSTANCE_ID)
     self.mpc_service.instance_repository.update.assert_called()
Esempio n. 5
0
File: aws.py Progetto: peking2/FBPCS
def map_ecstask_to_containerinstance(
        task: Dict[str, Any]) -> ContainerInstance:
    container = task["containers"][0]
    ip_v4 = (container["networkInterfaces"][0]["privateIpv4Address"]
             if len(container["networkInterfaces"]) > 0 else None)

    status = container["lastStatus"]
    if status == "RUNNING":
        status = ContainerInstanceStatus.STARTED
    elif status == "STOPPED":
        if container["exitCode"] == 0:
            status = ContainerInstanceStatus.COMPLETED
        else:
            status = ContainerInstanceStatus.FAILED
    else:
        status = ContainerInstanceStatus.UNKNOWN

    return ContainerInstance(task["taskArn"], ip_v4, status)
Esempio n. 6
0
    def _read_side_effect_update(self, instance_id):
        """
        mock MPCInstanceRepository.read for test_update,
        with instance.containers is not None
        """
        if instance_id == TEST_INSTANCE_ID:
            mpc_instance = self._get_sample_mpcinstance()
        else:
            raise RuntimeError(f"{instance_id} does not exist")

        mpc_instance.status = MPCInstanceStatus.STARTED
        mpc_instance.containers = [
            ContainerInstance(
                "arn:aws:ecs:us-west-1:592513842793:task/57850450-7a81-43cc-8c73-2071c52e4a68",  # noqa
                "10.0.1.130",
                ContainerInstanceStatus.STARTED,
            )
        ]
        return mpc_instance
Esempio n. 7
0
 def test_start_instance(self):
     self.mpc_service.instance_repository.read = MagicMock(
         side_effect=self._read_side_effect_start)
     created_instances = [
         ContainerInstance(
             "arn:aws:ecs:us-west-1:592513842793:task/57850450-7a81-43cc-8c73-2071c52e4a68",  # noqa
             "10.0.1.130",
             ContainerInstanceStatus.STARTED,
         )
     ]
     self.mpc_service.container_svc.create_instances_async = AsyncMock(
         return_value=created_instances)
     built_one_docker_args = ("private_lift/lift",
                              "test one docker arguments")
     self.mpc_service.mpc_game_svc.build_one_docker_args = MagicMock(
         return_value=built_one_docker_args)
     # check that update is called with correct status
     self.mpc_service.start_instance(TEST_INSTANCE_ID)
     self.mpc_service.instance_repository.update.assert_called()
     latest_update = self.mpc_service.instance_repository.update.call_args_list[
         -1]
     updated_status = latest_update[0][0].status
     self.assertEqual(updated_status, MPCInstanceStatus.STARTED)
Esempio n. 8
0
 def test_run_task(self):
     client_return_response = {
         "tasks": [{
             "containers": [{
                 "name":
                 "container_1",
                 "exitcode":
                 123,
                 "lastStatus":
                 "RUNNING",
                 "networkInterfaces": [
                     {
                         "privateIpv4Address": self.TEST_IP_ADDRESS,
                     },
                 ],
             }],
             "taskArn":
             self.TEST_TASK_ARN,
         }]
     }
     self.gw.client.run_task = MagicMock(
         return_value=client_return_response)
     task = self.gw.run_task(
         self.TEST_TASK_DEFINITION,
         self.TEST_CONTAINER,
         self.TEST_CMD,
         self.TEST_CLUSTER,
         self.TEST_SUBNET,
     )
     expected_task = ContainerInstance(
         self.TEST_TASK_ARN,
         self.TEST_IP_ADDRESS,
         ContainerInstanceStatus.STARTED,
     )
     self.assertEqual(task, expected_task)
     self.gw.client.run_task.assert_called()
Esempio n. 9
0
    def test_map_ecstask_to_containerinstance(self):
        ecs_task_response = {
            "tasks": [
                {
                    "containers": [
                        {
                            "exitCode":
                            None,
                            "lastStatus":
                            "RUNNING",
                            "networkInterfaces": [
                                {
                                    "privateIpv4Address": self.TEST_IP_ADDRESS,
                                },
                            ],
                        },
                    ],
                    "taskArn":
                    self.TEST_TASK_ARN,
                },
                {
                    "containers": [
                        {
                            "exitCode": 0,
                            "lastStatus": "STOPPED",
                            "networkInterfaces": [],
                        },
                    ],
                    "taskArn":
                    self.TEST_TASK_ARN,
                },
                {
                    "containers": [
                        {
                            "exitCode": 1,
                            "lastStatus": "STOPPED",
                            "networkInterfaces": [],
                        },
                    ],
                    "taskArn":
                    self.TEST_TASK_ARN,
                },
                {
                    "containers": [
                        {
                            "exitCode": -1,
                            "lastStatus": "UNKNOWN",
                            "networkInterfaces": [],
                        },
                    ],
                    "taskArn":
                    self.TEST_TASK_ARN,
                },
            ]
        }

        expected_task_list = [
            ContainerInstance(
                self.TEST_TASK_ARN,
                self.TEST_IP_ADDRESS,
                ContainerInstanceStatus.STARTED,
            ),
            ContainerInstance(
                self.TEST_TASK_ARN,
                None,
                ContainerInstanceStatus.COMPLETED,
            ),
            ContainerInstance(
                self.TEST_TASK_ARN,
                None,
                ContainerInstanceStatus.FAILED,
            ),
            ContainerInstance(
                self.TEST_TASK_ARN,
                None,
                ContainerInstanceStatus.UNKNOWN,
            ),
        ]
        tasks_list = [
            map_ecstask_to_containerinstance(task)
            for task in ecs_task_response["tasks"]
        ]

        self.assertEqual(tasks_list, expected_task_list)