Path: blob/develop/tests/unit/customizations/ecs/test_executecommand_startsession.py
1569 views
# Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.1#2# Licensed under the Apache License, Version 2.0 (the "License"). You3# may not use this file except in compliance with the License. A copy of4# the License is located at5#6# http://aws.amazon.com/apache2.0/7#8# or in the "license" file accompanying this file. This file is9# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF10# ANY KIND, either express or implied. See the License for the specific11# language governing permissions and limitations under the License.12import botocore.session13import json14import errno1516from awscli.testutils import mock, unittest17from awscli.customizations.ecs import executecommand181920class TestExecuteCommand(unittest.TestCase):2122def setUp(self):23self.session = mock.Mock(botocore.session.Session)24self.client = mock.Mock()25self.region = 'us-west-2'26self.profile = 'testProfile'27self.endpoint_url = 'testUrl'28self.client.meta.region_name = self.region29self.client.meta.endpoint_url = self.endpoint_url30self.caller = executecommand.ExecuteCommandCaller(self.session)31self.session.profile = self.profile32self.session.create_client.return_value = self.client33self.execute_command_params = {34"cluster": "default",35"task": "someTaskId",36"command": "ls",37"interactive": "true"}38self.execute_command_response = {39"containerName": "someContainerName",40"containerArn": "ecs/someContainerArn",41"taskArn": "ecs/someTaskArn",42"session": {"sessionId": "session-id",43"tokenValue": "token-value",44"streamUrl": "stream-url"},45"clusterArn": "ecs/someClusterArn",46"interactive": "true"47}48self.describe_tasks_response = {49"failures": [],50"tasks": [51{52"clusterArn": "ecs/someCLusterArn",53"desiredStatus": "RUNNING",54"createdAt": "1611619514.46",55"taskArn": "someTaskArn",56"containers": [57{58"containerArn": "ecs/someContainerArn",59"taskArn": "ecs/someTaskArn",60"name": "someContainerName",61"managedAgents": [62{63"reason": "Execute Command Agent started",64"lastStatus": "RUNNING",65"lastStartedAt": "1611619528.272",66"name": "ExecuteCommandAgent"67}68],69"runtimeId": "someRuntimeId"70},71{72"containerArn": "ecs/dummyContainerArn",73"taskArn": "ecs/someTaskArn",74"name": "dummyContainerName",75"managedAgents": [76{77"reason": "Execute Command Agent started",78"lastStatus": "RUNNING",79"lastStartedAt": "1611619528.272",80"name": "ExecuteCommandAgent"81}82],83"runtimeId": "dummyRuntimeId"84}85],86"lastStatus": "RUNNING",87"enableExecuteCommand": "true"88}89]90}91self.describe_tasks_response_fail = {92"failures": [93{94"reason": "MISSING",95"arn": "someTaskArn"96}97],98"tasks": []99}100self.ssm_request_parameters = {101"Target": "ecs:someClusterArn_someTaskArn_someRuntimeId"102}103104@mock.patch('awscli.customizations.ecs.executecommand.check_call')105def test_when_calls_fails_from_ecs(self, mock_check_call):106self.client.execute_command.side_effect = Exception('some exception')107mock_check_call.return_value = 0108with self.assertRaisesRegex(Exception, 'some exception'):109self.caller.invoke('ecs', 'ExecuteCommand', {}, mock.Mock())110111@mock.patch('awscli.customizations.ecs.executecommand.check_call')112def test_when_session_manager_plugin_not_installed(self, mock_check_call):113mock_check_call.side_effect = [OSError(errno.ENOENT, 'some error'), 0]114115with self.assertRaises(ValueError):116self.caller.invoke('ecs', 'ExecuteCommand', {}, mock.Mock())117118@mock.patch('awscli.customizations.ecs.executecommand.check_call')119def test_execute_command_success(self, mock_check_call):120mock_check_call.return_value = 0121122self.client.execute_command.return_value = \123self.execute_command_response124self.client.describe_tasks.return_value = self.describe_tasks_response125126rc = self.caller.invoke('ecs', 'ExecuteCommand',127self.execute_command_params, mock.Mock())128129self.assertEqual(rc, 0)130self.client.execute_command.\131assert_called_with(**self.execute_command_params)132133mock_check_call_list = mock_check_call.call_args[0][0]134mock_check_call_list[1] = json.loads(mock_check_call_list[1])135self.assertEqual(136mock_check_call_list,137['session-manager-plugin',138self.execute_command_response["session"],139self.region,140'StartSession',141self.profile,142json.dumps(self.ssm_request_parameters),143self.endpoint_url144]145)146147@mock.patch('awscli.customizations.ecs.executecommand.check_call')148def test_when_describe_task_fails(self, mock_check_call):149mock_check_call.return_value = 0150151self.client.execute_command.return_value = \152self.execute_command_response153self.client.describe_tasks.side_effect = \154Exception("Some Server Exception")155156with self.assertRaisesRegex(Exception, 'Some Server Exception'):157rc = self.caller.invoke('ecs', 'ExecuteCommand',158self.execute_command_params, mock.Mock())159self.assertEqual(rc, 0)160self.client.execute_command. \161assert_called_with(**self.execute_command_params)162163@mock.patch('awscli.customizations.ecs.executecommand.check_call')164def test_when_describe_task_returns_no_tasks(self, mock_check_call):165mock_check_call.return_value = 0166167self.client.execute_command.return_value = \168self.execute_command_response169self.client.describe_tasks.return_value = \170self.describe_tasks_response_fail171172with self.assertRaises(Exception):173rc = self.caller.invoke('ecs', 'ExecuteCommand',174self.execute_command_params, mock.Mock())175self.assertEqual(rc, 0)176self.client.execute_command. \177assert_called_with(**self.execute_command_params)178179@mock.patch('awscli.customizations.ecs.executecommand.check_call')180def test_when_check_call_fails(self, mock_check_call):181mock_check_call.side_effect = [0, Exception('some Exception')]182183self.client.execute_command.return_value = \184self.execute_command_response185self.client.describe_tasks.return_value = self.describe_tasks_response186187with self.assertRaises(Exception):188self.caller.invoke('ecs', 'ExecuteCommand',189self.execute_command_params, mock.Mock())190191mock_check_call_list = mock_check_call.call_args[0][0]192mock_check_call_list[1] = json.loads(mock_check_call_list[1])193self.assertEqual(194mock_check_call_list,195['session-manager-plugin',196self.execute_command_response["session"],197self.region,198'StartSession',199self.profile,200json.dumps(self.ssm_request_parameters),201self.endpoint_url],202)203204205