Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/tests/unit/customizations/ecs/test_executecommand_startsession.py
1569 views
1
# Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License"). You
4
# may not use this file except in compliance with the License. A copy of
5
# the License is located at
6
#
7
# http://aws.amazon.com/apache2.0/
8
#
9
# or in the "license" file accompanying this file. This file is
10
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
# ANY KIND, either express or implied. See the License for the specific
12
# language governing permissions and limitations under the License.
13
import botocore.session
14
import json
15
import errno
16
17
from awscli.testutils import mock, unittest
18
from awscli.customizations.ecs import executecommand
19
20
21
class TestExecuteCommand(unittest.TestCase):
22
23
def setUp(self):
24
self.session = mock.Mock(botocore.session.Session)
25
self.client = mock.Mock()
26
self.region = 'us-west-2'
27
self.profile = 'testProfile'
28
self.endpoint_url = 'testUrl'
29
self.client.meta.region_name = self.region
30
self.client.meta.endpoint_url = self.endpoint_url
31
self.caller = executecommand.ExecuteCommandCaller(self.session)
32
self.session.profile = self.profile
33
self.session.create_client.return_value = self.client
34
self.execute_command_params = {
35
"cluster": "default",
36
"task": "someTaskId",
37
"command": "ls",
38
"interactive": "true"}
39
self.execute_command_response = {
40
"containerName": "someContainerName",
41
"containerArn": "ecs/someContainerArn",
42
"taskArn": "ecs/someTaskArn",
43
"session": {"sessionId": "session-id",
44
"tokenValue": "token-value",
45
"streamUrl": "stream-url"},
46
"clusterArn": "ecs/someClusterArn",
47
"interactive": "true"
48
}
49
self.describe_tasks_response = {
50
"failures": [],
51
"tasks": [
52
{
53
"clusterArn": "ecs/someCLusterArn",
54
"desiredStatus": "RUNNING",
55
"createdAt": "1611619514.46",
56
"taskArn": "someTaskArn",
57
"containers": [
58
{
59
"containerArn": "ecs/someContainerArn",
60
"taskArn": "ecs/someTaskArn",
61
"name": "someContainerName",
62
"managedAgents": [
63
{
64
"reason": "Execute Command Agent started",
65
"lastStatus": "RUNNING",
66
"lastStartedAt": "1611619528.272",
67
"name": "ExecuteCommandAgent"
68
}
69
],
70
"runtimeId": "someRuntimeId"
71
},
72
{
73
"containerArn": "ecs/dummyContainerArn",
74
"taskArn": "ecs/someTaskArn",
75
"name": "dummyContainerName",
76
"managedAgents": [
77
{
78
"reason": "Execute Command Agent started",
79
"lastStatus": "RUNNING",
80
"lastStartedAt": "1611619528.272",
81
"name": "ExecuteCommandAgent"
82
}
83
],
84
"runtimeId": "dummyRuntimeId"
85
}
86
],
87
"lastStatus": "RUNNING",
88
"enableExecuteCommand": "true"
89
}
90
]
91
}
92
self.describe_tasks_response_fail = {
93
"failures": [
94
{
95
"reason": "MISSING",
96
"arn": "someTaskArn"
97
}
98
],
99
"tasks": []
100
}
101
self.ssm_request_parameters = {
102
"Target": "ecs:someClusterArn_someTaskArn_someRuntimeId"
103
}
104
105
@mock.patch('awscli.customizations.ecs.executecommand.check_call')
106
def test_when_calls_fails_from_ecs(self, mock_check_call):
107
self.client.execute_command.side_effect = Exception('some exception')
108
mock_check_call.return_value = 0
109
with self.assertRaisesRegex(Exception, 'some exception'):
110
self.caller.invoke('ecs', 'ExecuteCommand', {}, mock.Mock())
111
112
@mock.patch('awscli.customizations.ecs.executecommand.check_call')
113
def test_when_session_manager_plugin_not_installed(self, mock_check_call):
114
mock_check_call.side_effect = [OSError(errno.ENOENT, 'some error'), 0]
115
116
with self.assertRaises(ValueError):
117
self.caller.invoke('ecs', 'ExecuteCommand', {}, mock.Mock())
118
119
@mock.patch('awscli.customizations.ecs.executecommand.check_call')
120
def test_execute_command_success(self, mock_check_call):
121
mock_check_call.return_value = 0
122
123
self.client.execute_command.return_value = \
124
self.execute_command_response
125
self.client.describe_tasks.return_value = self.describe_tasks_response
126
127
rc = self.caller.invoke('ecs', 'ExecuteCommand',
128
self.execute_command_params, mock.Mock())
129
130
self.assertEqual(rc, 0)
131
self.client.execute_command.\
132
assert_called_with(**self.execute_command_params)
133
134
mock_check_call_list = mock_check_call.call_args[0][0]
135
mock_check_call_list[1] = json.loads(mock_check_call_list[1])
136
self.assertEqual(
137
mock_check_call_list,
138
['session-manager-plugin',
139
self.execute_command_response["session"],
140
self.region,
141
'StartSession',
142
self.profile,
143
json.dumps(self.ssm_request_parameters),
144
self.endpoint_url
145
]
146
)
147
148
@mock.patch('awscli.customizations.ecs.executecommand.check_call')
149
def test_when_describe_task_fails(self, mock_check_call):
150
mock_check_call.return_value = 0
151
152
self.client.execute_command.return_value = \
153
self.execute_command_response
154
self.client.describe_tasks.side_effect = \
155
Exception("Some Server Exception")
156
157
with self.assertRaisesRegex(Exception, 'Some Server Exception'):
158
rc = self.caller.invoke('ecs', 'ExecuteCommand',
159
self.execute_command_params, mock.Mock())
160
self.assertEqual(rc, 0)
161
self.client.execute_command. \
162
assert_called_with(**self.execute_command_params)
163
164
@mock.patch('awscli.customizations.ecs.executecommand.check_call')
165
def test_when_describe_task_returns_no_tasks(self, mock_check_call):
166
mock_check_call.return_value = 0
167
168
self.client.execute_command.return_value = \
169
self.execute_command_response
170
self.client.describe_tasks.return_value = \
171
self.describe_tasks_response_fail
172
173
with self.assertRaises(Exception):
174
rc = self.caller.invoke('ecs', 'ExecuteCommand',
175
self.execute_command_params, mock.Mock())
176
self.assertEqual(rc, 0)
177
self.client.execute_command. \
178
assert_called_with(**self.execute_command_params)
179
180
@mock.patch('awscli.customizations.ecs.executecommand.check_call')
181
def test_when_check_call_fails(self, mock_check_call):
182
mock_check_call.side_effect = [0, Exception('some Exception')]
183
184
self.client.execute_command.return_value = \
185
self.execute_command_response
186
self.client.describe_tasks.return_value = self.describe_tasks_response
187
188
with self.assertRaises(Exception):
189
self.caller.invoke('ecs', 'ExecuteCommand',
190
self.execute_command_params, mock.Mock())
191
192
mock_check_call_list = mock_check_call.call_args[0][0]
193
mock_check_call_list[1] = json.loads(mock_check_call_list[1])
194
self.assertEqual(
195
mock_check_call_list,
196
['session-manager-plugin',
197
self.execute_command_response["session"],
198
self.region,
199
'StartSession',
200
self.profile,
201
json.dumps(self.ssm_request_parameters),
202
self.endpoint_url],
203
)
204
205