PageRenderTime 89ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/unit/customizations/datapipeline/test_commands.py

https://gitlab.com/github-cloud-corp/aws-cli
Python | 238 lines | 198 code | 21 blank | 19 comment | 0 complexity | 755ab3340bc98fcdd68bdaeee028a575 MD5 | raw file
  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. import copy
  15. import mock
  16. import unittest
  17. from awscli.testutils import BaseAWSHelpOutputTest, BaseAWSCommandParamsTest
  18. from awscli.customizations.datapipeline import convert_described_objects
  19. from awscli.customizations.datapipeline import ListRunsCommand
  20. API_DESCRIBE_OBJECTS = [
  21. {"fields": [
  22. {
  23. "key": "@componentParent",
  24. "refValue": "S3Input"
  25. },
  26. {
  27. "key": "@scheduledStartTime",
  28. "stringValue": "2013-08-19T20:00:00"
  29. },
  30. {
  31. "key": "parent",
  32. "refValue": "S3Input"
  33. },
  34. {
  35. "key": "@sphere",
  36. "stringValue": "INSTANCE"
  37. },
  38. {
  39. "key": "type",
  40. "stringValue": "S3DataNode"
  41. },
  42. {
  43. "key": "@version",
  44. "stringValue": "1"
  45. },
  46. {
  47. "key": "@status",
  48. "stringValue": "FINISHED"
  49. },
  50. {
  51. "key": "@actualEndTime",
  52. "stringValue": "2014-02-19T19:44:44"
  53. },
  54. {
  55. "key": "@actualStartTime",
  56. "stringValue": "2014-02-19T19:44:43"
  57. },
  58. {
  59. "key": "output",
  60. "refValue": "@MyCopyActivity_2013-08-19T20:00:00"
  61. },
  62. {
  63. "key": "@scheduledEndTime",
  64. "stringValue": "2013-08-19T21:00:00"
  65. }
  66. ],
  67. "id": "@S3Input_2013-08-19T20:00:00",
  68. "name": "@S3Input_2013-08-19T20:00:00"
  69. },
  70. {"fields": [
  71. {
  72. "key": "@componentParent",
  73. "refValue": "MyEC2Resource"
  74. },
  75. {
  76. "key": "@resourceId",
  77. "stringValue": "i-12345"
  78. },
  79. {
  80. "key": "@scheduledStartTime",
  81. "stringValue": "2013-08-19T23:00:00"
  82. },
  83. {
  84. "key": "parent",
  85. "refValue": "MyEC2Resource"
  86. },
  87. {
  88. "key": "@sphere",
  89. "stringValue": "INSTANCE"
  90. },
  91. {
  92. "key": "@attemptCount",
  93. "stringValue": "1"
  94. },
  95. {
  96. "key": "type",
  97. "stringValue": "Ec2Resource"
  98. },
  99. {
  100. "key": "@version",
  101. "stringValue": "1"
  102. },
  103. {
  104. "key": "@status",
  105. "stringValue": "CREATING"
  106. },
  107. {
  108. "key": "input",
  109. "refValue": "@MyCopyActivity_2013-08-19T23:00:00"
  110. },
  111. {
  112. "key": "@triesLeft",
  113. "stringValue": "2"
  114. },
  115. {
  116. "key": "@actualStartTime",
  117. "stringValue": "2014-02-19T19:59:45"
  118. },
  119. {
  120. "key": "@headAttempt",
  121. "refValue": "@MyEC2Resource_2013-08-19T23:00:00_Attempt=1"
  122. },
  123. {
  124. "key": "@scheduledEndTime",
  125. "stringValue": "2013-08-20T00:00:00"
  126. }
  127. ],
  128. "id": "@MyEC2Resource_2013-08-19T23:00:00",
  129. "name": "@MyEC2Resource_2013-08-19T23:00:00"
  130. }
  131. ]
  132. JSON_FORMATTER_PATH = 'awscli.formatter.JSONFormatter.__call__'
  133. LIST_FORMATTER_PATH = 'awscli.customizations.datapipeline.listrunsformatter.ListRunsFormatter.__call__' # noqa
  134. class TestConvertObjects(unittest.TestCase):
  135. def test_convert_described_objects(self):
  136. converted = convert_described_objects(API_DESCRIBE_OBJECTS)
  137. self.assertEqual(len(converted), 2)
  138. # This comes from a "refValue" value.
  139. self.assertEqual(converted[0]['@componentParent'], 'S3Input')
  140. # Should also merge in @id and name.
  141. self.assertEqual(converted[0]['@id'], "@S3Input_2013-08-19T20:00:00")
  142. self.assertEqual(converted[0]['name'], "@S3Input_2013-08-19T20:00:00")
  143. # This comes from a "stringValue" value.
  144. self.assertEqual(converted[0]['@sphere'], "INSTANCE")
  145. def test_convert_objects_are_sorted(self):
  146. describe_objects = copy.deepcopy(API_DESCRIBE_OBJECTS)
  147. # Change the existing @scheduledStartTime from
  148. # 20:00:00 to 23:59:00
  149. describe_objects[0]['fields'][1]['stringValue'] = (
  150. "2013-08-19T23:59:00")
  151. converted = convert_described_objects(
  152. describe_objects,
  153. sort_key_func=lambda x: (x['@scheduledStartTime'], x['name']))
  154. self.assertEqual(converted[0]['@scheduledStartTime'],
  155. '2013-08-19T23:00:00')
  156. self.assertEqual(converted[1]['@scheduledStartTime'],
  157. '2013-08-19T23:59:00')
  158. class FakeParsedArgs(object):
  159. def __init__(self, **kwargs):
  160. self.endpoint_url = None
  161. self.region = None
  162. self.verify_ssl = None
  163. self.output = None
  164. self.query = None
  165. self.__dict__.update(kwargs)
  166. class TestCommandsRunProperly(BaseAWSCommandParamsTest):
  167. def setUp(self):
  168. super(TestCommandsRunProperly, self).setUp()
  169. self.query_objects = mock.Mock()
  170. self.describe_objects = mock.Mock()
  171. self.client = mock.Mock()
  172. self.client.get_paginator.return_value = self.query_objects
  173. self.client.describe_objects = self.describe_objects
  174. self.driver.session = mock.Mock()
  175. self.driver.session.emit_first_non_none_response.return_value = None
  176. self.driver.session.create_client.return_value = self.client
  177. self.query_objects.paginate.return_value.build_full_result.\
  178. return_value = {'ids': ['object-ids']}
  179. self.describe_objects.return_value = \
  180. {'pipelineObjects': API_DESCRIBE_OBJECTS}
  181. self.expected_response = convert_described_objects(
  182. API_DESCRIBE_OBJECTS,
  183. sort_key_func=lambda x: (x['@scheduledStartTime'], x['name']))
  184. def test_list_runs(self):
  185. command = ListRunsCommand(self.driver.session)
  186. command(['--pipeline-id', 'my-pipeline-id'],
  187. parsed_globals=FakeParsedArgs(region='us-east-1'))
  188. self.assertTrue(self.query_objects.paginate.called)
  189. self.describe_objects.assert_called_with(
  190. pipelineId='my-pipeline-id', objectIds=['object-ids'])
  191. @mock.patch(JSON_FORMATTER_PATH)
  192. @mock.patch(LIST_FORMATTER_PATH)
  193. def test_list_runs_formatter_explicit_choice(
  194. self, list_formatter, json_formatter):
  195. command = ListRunsCommand(self.driver.session)
  196. command(['--pipeline-id', 'my-pipeline-id'],
  197. parsed_globals=FakeParsedArgs(
  198. region='us-east-1', output='json'))
  199. json_formatter.assert_called_once_with(
  200. 'list-runs', self.expected_response)
  201. self.assertFalse(list_formatter.called)
  202. @mock.patch(JSON_FORMATTER_PATH)
  203. @mock.patch(LIST_FORMATTER_PATH)
  204. def test_list_runs_formatter_implicit_choice(
  205. self, list_formatter, json_formatter):
  206. command = ListRunsCommand(self.driver.session)
  207. command(['--pipeline-id', 'my-pipeline-id'],
  208. parsed_globals=FakeParsedArgs(region='us-east-1'))
  209. list_formatter.assert_called_once_with(
  210. 'list-runs', self.expected_response)
  211. self.assertFalse(json_formatter.called)
  212. class TestHelpOutput(BaseAWSHelpOutputTest):
  213. def test_list_runs_help_output(self):
  214. self.driver.main(['datapipeline', 'get-pipeline-definition', 'help'])
  215. self.assert_contains('pipeline definition')
  216. # The previous API docs should not be in the output
  217. self.assert_not_contains('pipelineObjects')