Path: blob/develop/tests/unit/customizations/datapipeline/test_arg_serialize.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.13from awscli.testutils import BaseAWSCommandParamsTest, unittest14from awscli.testutils import temporary_file1516from awscli.customizations.datapipeline import QueryArgBuilder17from dateutil.parser import parse181920# We're not interested in testing the def->api21# translation process (that has its own test suite),22# but we need something basic enough that shows us23# that we're serializing arguments properly.24TEST_JSON = """\25{"objects": [26{27"id" : "S3ToS3Copy",28"type" : "CopyActivity",29"schedule" : { "ref" : "CopyPeriod" },30"input" : { "ref" : "InputData" },31"output" : { "ref" : "OutputData" }32}33]}"""343536class TestPutPipelineDefinition(BaseAWSCommandParamsTest):3738prefix = 'datapipeline put-pipeline-definition'3940def test_put_pipeline_definition_with_json(self):41with temporary_file('r+') as f:42f.write(TEST_JSON)43f.flush()44cmdline = self.prefix45cmdline += ' --pipeline-id name'46cmdline += ' --pipeline-definition file://%s' % f.name47result = {48'pipelineId': 'name',49'pipelineObjects': [50{"id": "S3ToS3Copy",51"name": "S3ToS3Copy",52"fields": [53{54"key": "input",55"refValue": "InputData"56},57{58"key": "output",59"refValue": "OutputData"60},61{62"key": "schedule",63"refValue": "CopyPeriod"64},65{66"key": "type",67"stringValue": "CopyActivity"68},69]}]70}71self.assert_params_for_cmd(cmdline, result)727374class TestErrorMessages(BaseAWSCommandParamsTest):75prefix = 'datapipeline list-runs'7677def test_unknown_status(self):78self.assert_params_for_cmd(79self.prefix + ' --pipeline-id foo --status foo',80expected_rc=255,81stderr_contains=('Invalid status: foo, must be one of: waiting, '82'pending, cancelled, running, finished, '83'failed, waiting_for_runner, '84'waiting_on_dependencies'))858687class FakeParsedArgs(object):88def __init__(self, start_interval=None, schedule_interval=None,89status=None):90self.start_interval = start_interval91self.schedule_interval = schedule_interval92self.status = status939495class TestCLIArgumentSerialize(unittest.TestCase):96maxDiff = None9798# These tests verify that we go from --cli-args99# to the proper structure needed for the "Query"100# argument to describe objects.101def test_build_query_args_default(self):102parsed_args = FakeParsedArgs()103current_time = '2014-02-21T00:00:00'104start_time = '2014-02-17T00:00:00'105builder = QueryArgBuilder(current_time=parse(current_time))106query = builder.build_query(parsed_args)107self.assertEqual(query, {108'selectors': [{109'fieldName': '@actualStartTime',110'operator': {111'type': 'BETWEEN',112'values': [start_time, current_time]113}114}]115})116117def test_build_args_with_start_interval(self):118parsed_args = FakeParsedArgs(119start_interval=['2014-02-01T00:00:00',120'2014-02-04T00:00:00',]121)122builder = QueryArgBuilder()123query = builder.build_query(parsed_args)124self.assertEqual(query, {125'selectors': [{126'fieldName': '@actualStartTime',127'operator': {128'type': 'BETWEEN',129'values': ['2014-02-01T00:00:00',130'2014-02-04T00:00:00']131}132}]133})134135def test_build_args_with_end_interval(self):136parsed_args = FakeParsedArgs(137schedule_interval=['2014-02-01T00:00:00',138'2014-02-04T00:00:00',]139)140builder = QueryArgBuilder()141query = builder.build_query(parsed_args)142self.assertEqual(query, {143'selectors': [{144'fieldName': '@scheduledStartTime',145'operator': {146'type': 'BETWEEN',147'values': ['2014-02-01T00:00:00',148'2014-02-04T00:00:00']149}150}]151})152153def test_build_args_with_single_status(self):154# --status pending155parsed_args = FakeParsedArgs(156status=['pending']157)158current_time = '2014-02-21T00:00:00'159start_time = '2014-02-17T00:00:00'160builder = QueryArgBuilder(current_time=parse(current_time))161query = builder.build_query(parsed_args)162self.assertEqual(query, {163'selectors': [{164'fieldName': '@actualStartTime',165'operator': {166'type': 'BETWEEN',167'values': [start_time, current_time]168}169}, {170'fieldName': '@status',171'operator': {172'type': 'EQ',173'values': ['PENDING']174}175},176]177})178179def test_build_args_with_csv_status(self):180# --status pending,waiting_on_dependencies181parsed_args = FakeParsedArgs(182status=['pending', 'waiting_on_dependencies']183)184current_time = '2014-02-21T00:00:00'185start_time = '2014-02-17T00:00:00'186builder = QueryArgBuilder(current_time=parse(current_time))187query = builder.build_query(parsed_args)188self.assertEqual(query, {189'selectors': [{190'fieldName': '@actualStartTime',191'operator': {192'type': 'BETWEEN',193'values': [start_time, current_time]194}195}, {196'fieldName': '@status',197'operator': {198'type': 'EQ',199'values': ['PENDING', 'WAITING_ON_DEPENDENCIES']200}201},202]203})204205def test_build_args_with_all_values_set(self):206# --status pending,waiting_on_dependencies207# --start-interval pending,waiting_on_dependencies208# --schedule-schedule pending,waiting_on_dependencies209parsed_args = FakeParsedArgs(210start_interval=['2014-02-01T00:00:00',211'2014-02-04T00:00:00',],212schedule_interval=['2014-02-05T00:00:00',213'2014-02-09T00:00:00',],214status=['pending', 'waiting_on_dependencies'],215)216builder = QueryArgBuilder()217query = builder.build_query(parsed_args)218self.assertEqual(query, {219'selectors': [{220'fieldName': '@actualStartTime',221'operator': {222'type': 'BETWEEN',223'values': ['2014-02-01T00:00:00',224'2014-02-04T00:00:00',],225}226}, {227'fieldName': '@scheduledStartTime',228'operator': {229'type': 'BETWEEN',230'values': ['2014-02-05T00:00:00',231'2014-02-09T00:00:00']232}233}, {234'fieldName': '@status',235'operator': {236'type': 'EQ',237'values': ['PENDING', 'WAITING_ON_DEPENDENCIES']238}239},]240})241242243