Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/tests/unit/customizations/datapipeline/test_commands.py
1569 views
1
#!/usr/bin/env python
2
# Copyright 2014 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
#
4
# Licensed under the Apache License, Version 2.0 (the "License"). You
5
# may not use this file except in compliance with the License. A copy of
6
# the License is located at
7
#
8
# http://aws.amazon.com/apache2.0/
9
#
10
# or in the "license" file accompanying this file. This file is
11
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
12
# ANY KIND, either express or implied. See the License for the specific
13
# language governing permissions and limitations under the License.
14
15
import copy
16
import unittest
17
18
from awscli.testutils import mock, BaseAWSHelpOutputTest, BaseAWSCommandParamsTest
19
20
from awscli.customizations.datapipeline import convert_described_objects
21
from awscli.customizations.datapipeline import ListRunsCommand
22
23
24
API_DESCRIBE_OBJECTS = [
25
{"fields": [
26
{
27
"key": "@componentParent",
28
"refValue": "S3Input"
29
},
30
{
31
"key": "@scheduledStartTime",
32
"stringValue": "2013-08-19T20:00:00"
33
},
34
{
35
"key": "parent",
36
"refValue": "S3Input"
37
},
38
{
39
"key": "@sphere",
40
"stringValue": "INSTANCE"
41
},
42
{
43
"key": "type",
44
"stringValue": "S3DataNode"
45
},
46
{
47
"key": "@version",
48
"stringValue": "1"
49
},
50
{
51
"key": "@status",
52
"stringValue": "FINISHED"
53
},
54
{
55
"key": "@actualEndTime",
56
"stringValue": "2014-02-19T19:44:44"
57
},
58
{
59
"key": "@actualStartTime",
60
"stringValue": "2014-02-19T19:44:43"
61
},
62
{
63
"key": "output",
64
"refValue": "@MyCopyActivity_2013-08-19T20:00:00"
65
},
66
{
67
"key": "@scheduledEndTime",
68
"stringValue": "2013-08-19T21:00:00"
69
}
70
],
71
"id": "@S3Input_2013-08-19T20:00:00",
72
"name": "@S3Input_2013-08-19T20:00:00"
73
},
74
{"fields": [
75
{
76
"key": "@componentParent",
77
"refValue": "MyEC2Resource"
78
},
79
{
80
"key": "@resourceId",
81
"stringValue": "i-12345"
82
},
83
{
84
"key": "@scheduledStartTime",
85
"stringValue": "2013-08-19T23:00:00"
86
},
87
{
88
"key": "parent",
89
"refValue": "MyEC2Resource"
90
},
91
{
92
"key": "@sphere",
93
"stringValue": "INSTANCE"
94
},
95
{
96
"key": "@attemptCount",
97
"stringValue": "1"
98
},
99
{
100
"key": "type",
101
"stringValue": "Ec2Resource"
102
},
103
{
104
"key": "@version",
105
"stringValue": "1"
106
},
107
{
108
"key": "@status",
109
"stringValue": "CREATING"
110
},
111
{
112
"key": "input",
113
"refValue": "@MyCopyActivity_2013-08-19T23:00:00"
114
},
115
{
116
"key": "@triesLeft",
117
"stringValue": "2"
118
},
119
{
120
"key": "@actualStartTime",
121
"stringValue": "2014-02-19T19:59:45"
122
},
123
{
124
"key": "@headAttempt",
125
"refValue": "@MyEC2Resource_2013-08-19T23:00:00_Attempt=1"
126
},
127
{
128
"key": "@scheduledEndTime",
129
"stringValue": "2013-08-20T00:00:00"
130
}
131
],
132
"id": "@MyEC2Resource_2013-08-19T23:00:00",
133
"name": "@MyEC2Resource_2013-08-19T23:00:00"
134
}
135
]
136
137
JSON_FORMATTER_PATH = 'awscli.formatter.JSONFormatter.__call__'
138
LIST_FORMATTER_PATH = 'awscli.customizations.datapipeline.listrunsformatter.ListRunsFormatter.__call__' # noqa
139
140
141
class TestConvertObjects(unittest.TestCase):
142
143
def test_convert_described_objects(self):
144
converted = convert_described_objects(API_DESCRIBE_OBJECTS)
145
self.assertEqual(len(converted), 2)
146
# This comes from a "refValue" value.
147
self.assertEqual(converted[0]['@componentParent'], 'S3Input')
148
# Should also merge in @id and name.
149
self.assertEqual(converted[0]['@id'], "@S3Input_2013-08-19T20:00:00")
150
self.assertEqual(converted[0]['name'], "@S3Input_2013-08-19T20:00:00")
151
# This comes from a "stringValue" value.
152
self.assertEqual(converted[0]['@sphere'], "INSTANCE")
153
154
def test_convert_objects_are_sorted(self):
155
describe_objects = copy.deepcopy(API_DESCRIBE_OBJECTS)
156
# Change the existing @scheduledStartTime from
157
# 20:00:00 to 23:59:00
158
describe_objects[0]['fields'][1]['stringValue'] = (
159
"2013-08-19T23:59:00")
160
converted = convert_described_objects(
161
describe_objects,
162
sort_key_func=lambda x: (x['@scheduledStartTime'], x['name']))
163
self.assertEqual(converted[0]['@scheduledStartTime'],
164
'2013-08-19T23:00:00')
165
self.assertEqual(converted[1]['@scheduledStartTime'],
166
'2013-08-19T23:59:00')
167
168
169
class FakeParsedArgs(object):
170
def __init__(self, **kwargs):
171
self.endpoint_url = None
172
self.region = None
173
self.verify_ssl = None
174
self.output = None
175
self.query = None
176
self.__dict__.update(kwargs)
177
178
179
class TestCommandsRunProperly(BaseAWSCommandParamsTest):
180
def setUp(self):
181
super(TestCommandsRunProperly, self).setUp()
182
self.query_objects = mock.Mock()
183
self.describe_objects = mock.Mock()
184
self.client = mock.Mock()
185
self.client.get_paginator.return_value = self.query_objects
186
self.client.describe_objects = self.describe_objects
187
188
self.driver.session = mock.Mock()
189
self.driver.session.emit_first_non_none_response.return_value = None
190
self.driver.session.create_client.return_value = self.client
191
self.query_objects.paginate.return_value.build_full_result.\
192
return_value = {'ids': ['object-ids']}
193
self.describe_objects.return_value = \
194
{'pipelineObjects': API_DESCRIBE_OBJECTS}
195
self.expected_response = convert_described_objects(
196
API_DESCRIBE_OBJECTS,
197
sort_key_func=lambda x: (x['@scheduledStartTime'], x['name']))
198
199
def test_list_runs(self):
200
command = ListRunsCommand(self.driver.session)
201
command(['--pipeline-id', 'my-pipeline-id'],
202
parsed_globals=FakeParsedArgs(region='us-east-1'))
203
self.assertTrue(self.query_objects.paginate.called)
204
self.describe_objects.assert_called_with(
205
pipelineId='my-pipeline-id', objectIds=['object-ids'])
206
207
@mock.patch(JSON_FORMATTER_PATH)
208
@mock.patch(LIST_FORMATTER_PATH)
209
def test_list_runs_formatter_explicit_choice(
210
self, list_formatter, json_formatter):
211
command = ListRunsCommand(self.driver.session)
212
command(['--pipeline-id', 'my-pipeline-id'],
213
parsed_globals=FakeParsedArgs(
214
region='us-east-1', output='json'))
215
json_formatter.assert_called_once_with(
216
'list-runs', self.expected_response)
217
self.assertFalse(list_formatter.called)
218
219
@mock.patch(JSON_FORMATTER_PATH)
220
@mock.patch(LIST_FORMATTER_PATH)
221
def test_list_runs_formatter_implicit_choice(
222
self, list_formatter, json_formatter):
223
224
command = ListRunsCommand(self.driver.session)
225
command(['--pipeline-id', 'my-pipeline-id'],
226
parsed_globals=FakeParsedArgs(region='us-east-1'))
227
list_formatter.assert_called_once_with(
228
'list-runs', self.expected_response)
229
self.assertFalse(json_formatter.called)
230
231
232
class TestHelpOutput(BaseAWSHelpOutputTest):
233
def test_list_runs_help_output(self):
234
self.driver.main(['datapipeline', 'get-pipeline-definition', 'help'])
235
self.assert_contains('pipeline definition')
236
# The previous API docs should not be in the output
237
self.assert_not_contains('pipelineObjects')
238
239