Path: blob/develop/tests/unit/customizations/datapipeline/test_commands.py
1569 views
#!/usr/bin/env python1# Copyright 2014 Amazon.com, Inc. or its affiliates. All Rights Reserved.2#3# Licensed under the Apache License, Version 2.0 (the "License"). You4# may not use this file except in compliance with the License. A copy of5# the License is located at6#7# http://aws.amazon.com/apache2.0/8#9# or in the "license" file accompanying this file. This file is10# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF11# ANY KIND, either express or implied. See the License for the specific12# language governing permissions and limitations under the License.1314import copy15import unittest1617from awscli.testutils import mock, BaseAWSHelpOutputTest, BaseAWSCommandParamsTest1819from awscli.customizations.datapipeline import convert_described_objects20from awscli.customizations.datapipeline import ListRunsCommand212223API_DESCRIBE_OBJECTS = [24{"fields": [25{26"key": "@componentParent",27"refValue": "S3Input"28},29{30"key": "@scheduledStartTime",31"stringValue": "2013-08-19T20:00:00"32},33{34"key": "parent",35"refValue": "S3Input"36},37{38"key": "@sphere",39"stringValue": "INSTANCE"40},41{42"key": "type",43"stringValue": "S3DataNode"44},45{46"key": "@version",47"stringValue": "1"48},49{50"key": "@status",51"stringValue": "FINISHED"52},53{54"key": "@actualEndTime",55"stringValue": "2014-02-19T19:44:44"56},57{58"key": "@actualStartTime",59"stringValue": "2014-02-19T19:44:43"60},61{62"key": "output",63"refValue": "@MyCopyActivity_2013-08-19T20:00:00"64},65{66"key": "@scheduledEndTime",67"stringValue": "2013-08-19T21:00:00"68}69],70"id": "@S3Input_2013-08-19T20:00:00",71"name": "@S3Input_2013-08-19T20:00:00"72},73{"fields": [74{75"key": "@componentParent",76"refValue": "MyEC2Resource"77},78{79"key": "@resourceId",80"stringValue": "i-12345"81},82{83"key": "@scheduledStartTime",84"stringValue": "2013-08-19T23:00:00"85},86{87"key": "parent",88"refValue": "MyEC2Resource"89},90{91"key": "@sphere",92"stringValue": "INSTANCE"93},94{95"key": "@attemptCount",96"stringValue": "1"97},98{99"key": "type",100"stringValue": "Ec2Resource"101},102{103"key": "@version",104"stringValue": "1"105},106{107"key": "@status",108"stringValue": "CREATING"109},110{111"key": "input",112"refValue": "@MyCopyActivity_2013-08-19T23:00:00"113},114{115"key": "@triesLeft",116"stringValue": "2"117},118{119"key": "@actualStartTime",120"stringValue": "2014-02-19T19:59:45"121},122{123"key": "@headAttempt",124"refValue": "@MyEC2Resource_2013-08-19T23:00:00_Attempt=1"125},126{127"key": "@scheduledEndTime",128"stringValue": "2013-08-20T00:00:00"129}130],131"id": "@MyEC2Resource_2013-08-19T23:00:00",132"name": "@MyEC2Resource_2013-08-19T23:00:00"133}134]135136JSON_FORMATTER_PATH = 'awscli.formatter.JSONFormatter.__call__'137LIST_FORMATTER_PATH = 'awscli.customizations.datapipeline.listrunsformatter.ListRunsFormatter.__call__' # noqa138139140class TestConvertObjects(unittest.TestCase):141142def test_convert_described_objects(self):143converted = convert_described_objects(API_DESCRIBE_OBJECTS)144self.assertEqual(len(converted), 2)145# This comes from a "refValue" value.146self.assertEqual(converted[0]['@componentParent'], 'S3Input')147# Should also merge in @id and name.148self.assertEqual(converted[0]['@id'], "@S3Input_2013-08-19T20:00:00")149self.assertEqual(converted[0]['name'], "@S3Input_2013-08-19T20:00:00")150# This comes from a "stringValue" value.151self.assertEqual(converted[0]['@sphere'], "INSTANCE")152153def test_convert_objects_are_sorted(self):154describe_objects = copy.deepcopy(API_DESCRIBE_OBJECTS)155# Change the existing @scheduledStartTime from156# 20:00:00 to 23:59:00157describe_objects[0]['fields'][1]['stringValue'] = (158"2013-08-19T23:59:00")159converted = convert_described_objects(160describe_objects,161sort_key_func=lambda x: (x['@scheduledStartTime'], x['name']))162self.assertEqual(converted[0]['@scheduledStartTime'],163'2013-08-19T23:00:00')164self.assertEqual(converted[1]['@scheduledStartTime'],165'2013-08-19T23:59:00')166167168class FakeParsedArgs(object):169def __init__(self, **kwargs):170self.endpoint_url = None171self.region = None172self.verify_ssl = None173self.output = None174self.query = None175self.__dict__.update(kwargs)176177178class TestCommandsRunProperly(BaseAWSCommandParamsTest):179def setUp(self):180super(TestCommandsRunProperly, self).setUp()181self.query_objects = mock.Mock()182self.describe_objects = mock.Mock()183self.client = mock.Mock()184self.client.get_paginator.return_value = self.query_objects185self.client.describe_objects = self.describe_objects186187self.driver.session = mock.Mock()188self.driver.session.emit_first_non_none_response.return_value = None189self.driver.session.create_client.return_value = self.client190self.query_objects.paginate.return_value.build_full_result.\191return_value = {'ids': ['object-ids']}192self.describe_objects.return_value = \193{'pipelineObjects': API_DESCRIBE_OBJECTS}194self.expected_response = convert_described_objects(195API_DESCRIBE_OBJECTS,196sort_key_func=lambda x: (x['@scheduledStartTime'], x['name']))197198def test_list_runs(self):199command = ListRunsCommand(self.driver.session)200command(['--pipeline-id', 'my-pipeline-id'],201parsed_globals=FakeParsedArgs(region='us-east-1'))202self.assertTrue(self.query_objects.paginate.called)203self.describe_objects.assert_called_with(204pipelineId='my-pipeline-id', objectIds=['object-ids'])205206@mock.patch(JSON_FORMATTER_PATH)207@mock.patch(LIST_FORMATTER_PATH)208def test_list_runs_formatter_explicit_choice(209self, list_formatter, json_formatter):210command = ListRunsCommand(self.driver.session)211command(['--pipeline-id', 'my-pipeline-id'],212parsed_globals=FakeParsedArgs(213region='us-east-1', output='json'))214json_formatter.assert_called_once_with(215'list-runs', self.expected_response)216self.assertFalse(list_formatter.called)217218@mock.patch(JSON_FORMATTER_PATH)219@mock.patch(LIST_FORMATTER_PATH)220def test_list_runs_formatter_implicit_choice(221self, list_formatter, json_formatter):222223command = ListRunsCommand(self.driver.session)224command(['--pipeline-id', 'my-pipeline-id'],225parsed_globals=FakeParsedArgs(region='us-east-1'))226list_formatter.assert_called_once_with(227'list-runs', self.expected_response)228self.assertFalse(json_formatter.called)229230231class TestHelpOutput(BaseAWSHelpOutputTest):232def test_list_runs_help_output(self):233self.driver.main(['datapipeline', 'get-pipeline-definition', 'help'])234self.assert_contains('pipeline definition')235# The previous API docs should not be in the output236self.assert_not_contains('pipelineObjects')237238239