| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 import unittest | 2 import unittest |
| 3 import httplib | 3 import httplib |
| 4 from datetime import datetime | 4 from datetime import datetime |
| 5 try: | 5 try: |
| 6 import json | 6 import json |
| 7 except ImportError: | 7 except ImportError: |
| 8 import simplejson as json | 8 import simplejson as json |
| 9 | 9 |
| 10 from mock import Mock | 10 from mock import Mock |
| 11 | 11 |
| 12 from tests.unit import AWSMockServiceTestCase | 12 from tests.unit import AWSMockServiceTestCase |
| 13 from boto.cloudformation.connection import CloudFormationConnection | 13 from boto.cloudformation.connection import CloudFormationConnection |
| 14 from boto.exception import BotoServerError |
| 14 | 15 |
| 15 | 16 |
| 16 SAMPLE_TEMPLATE = r""" | 17 SAMPLE_TEMPLATE = r""" |
| 17 { | 18 { |
| 18 "AWSTemplateFormatVersion" : "2010-09-09", | 19 "AWSTemplateFormatVersion" : "2010-09-09", |
| 19 "Description" : "Sample template", | 20 "Description" : "Sample template", |
| 20 "Parameters" : { | 21 "Parameters" : { |
| 21 "KeyName" : { | 22 "KeyName" : { |
| 22 "Description" : "key pair", | 23 "Description" : "key pair", |
| 23 "Type" : "String" | 24 "Type" : "String" |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 self.assert_request_parameters({ | 102 self.assert_request_parameters({ |
| 102 'Action': 'CreateStack', | 103 'Action': 'CreateStack', |
| 103 'ContentType': 'JSON', | 104 'ContentType': 'JSON', |
| 104 'DisableRollback': 'false', | 105 'DisableRollback': 'false', |
| 105 'StackName': 'stack_name', | 106 'StackName': 'stack_name', |
| 106 'Version': '2010-05-15', | 107 'Version': '2010-05-15', |
| 107 }) | 108 }) |
| 108 | 109 |
| 109 def test_create_stack_fails(self): | 110 def test_create_stack_fails(self): |
| 110 self.set_http_response(status_code=400, reason='Bad Request', | 111 self.set_http_response(status_code=400, reason='Bad Request', |
| 111 body='Invalid arg.') | 112 body='{"Error": {"Code": 1, "Message": "Invalid arg."}}') |
| 112 with self.assertRaises(self.service_connection.ResponseError): | 113 with self.assertRaisesRegexp(self.service_connection.ResponseError, |
| 114 'Invalid arg.'): |
| 113 api_response = self.service_connection.create_stack( | 115 api_response = self.service_connection.create_stack( |
| 114 'stack_name', template_body=SAMPLE_TEMPLATE, | 116 'stack_name', template_body=SAMPLE_TEMPLATE, |
| 115 parameters=[('KeyName', 'myKeyName')]) | 117 parameters=[('KeyName', 'myKeyName')]) |
| 116 | 118 |
| 119 def test_create_stack_fail_error(self): |
| 120 self.set_http_response(status_code=400, reason='Bad Request', |
| 121 body='{"RequestId": "abc", "Error": {"Code": 1, "Message": "Invalid
arg."}}') |
| 122 try: |
| 123 api_response = self.service_connection.create_stack( |
| 124 'stack_name', template_body=SAMPLE_TEMPLATE, |
| 125 parameters=[('KeyName', 'myKeyName')]) |
| 126 except BotoServerError, e: |
| 127 self.assertEqual('abc', e.request_id) |
| 128 self.assertEqual(1, e.error_code) |
| 129 self.assertEqual('Invalid arg.', e.message) |
| 117 | 130 |
| 118 class TestCloudFormationUpdateStack(CloudFormationConnectionBase): | 131 class TestCloudFormationUpdateStack(CloudFormationConnectionBase): |
| 119 def default_body(self): | 132 def default_body(self): |
| 120 return json.dumps( | 133 return json.dumps( |
| 121 {u'UpdateStackResponse': | 134 {u'UpdateStackResponse': |
| 122 {u'UpdateStackResult': {u'StackId': self.stack_id}, | 135 {u'UpdateStackResult': {u'StackId': self.stack_id}, |
| 123 u'ResponseMetadata': {u'RequestId': u'1'}}}) | 136 u'ResponseMetadata': {u'RequestId': u'1'}}}) |
| 124 | 137 |
| 125 def test_update_stack_all_args(self): | 138 def test_update_stack_all_args(self): |
| 126 self.set_http_response(status_code=200) | 139 self.set_http_response(status_code=200) |
| (...skipping 435 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 562 <ParameterKey>InstanceType</ParameterKey> | 575 <ParameterKey>InstanceType</ParameterKey> |
| 563 <Description>Type of instance to launch</Description> | 576 <Description>Type of instance to launch</Description> |
| 564 <DefaultValue>m1.small</DefaultValue> | 577 <DefaultValue>m1.small</DefaultValue> |
| 565 </member> | 578 </member> |
| 566 <member> | 579 <member> |
| 567 <NoEcho>false</NoEcho> | 580 <NoEcho>false</NoEcho> |
| 568 <ParameterKey>KeyName</ParameterKey> | 581 <ParameterKey>KeyName</ParameterKey> |
| 569 <Description>EC2 KeyPair</Description> | 582 <Description>EC2 KeyPair</Description> |
| 570 </member> | 583 </member> |
| 571 </Parameters> | 584 </Parameters> |
| 585 <CapabilitiesReason>Reason</CapabilitiesReason> |
| 586 <Capabilities> |
| 587 <member>CAPABILITY_IAM</member> |
| 588 </Capabilities> |
| 572 </ValidateTemplateResult> | 589 </ValidateTemplateResult> |
| 573 <ResponseMetadata> | 590 <ResponseMetadata> |
| 574 <RequestId>0be7b6e8-e4a0-11e0-a5bd-9f8d5a7dbc91</RequestId> | 591 <RequestId>0be7b6e8-e4a0-11e0-a5bd-9f8d5a7dbc91</RequestId> |
| 575 </ResponseMetadata> | 592 </ResponseMetadata> |
| 576 </ValidateTemplateResponse> | 593 </ValidateTemplateResponse> |
| 577 """ | 594 """ |
| 578 | 595 |
| 579 def test_validate_template(self): | 596 def test_validate_template(self): |
| 580 self.set_http_response(status_code=200) | 597 self.set_http_response(status_code=200) |
| 581 template = self.service_connection.validate_template(template_body=SAMPL
E_TEMPLATE, | 598 template = self.service_connection.validate_template(template_body=SAMPL
E_TEMPLATE, |
| 582 template_url='http://u
rl') | 599 template_url='http://u
rl') |
| 583 self.assertEqual(template.description, 'My Description.') | 600 self.assertEqual(template.description, 'My Description.') |
| 584 self.assertEqual(len(template.template_parameters), 2) | 601 self.assertEqual(len(template.template_parameters), 2) |
| 585 param1, param2 = template.template_parameters | 602 param1, param2 = template.template_parameters |
| 586 self.assertEqual(param1.default_value, 'm1.small') | 603 self.assertEqual(param1.default_value, 'm1.small') |
| 587 self.assertEqual(param1.description, 'Type of instance to launch') | 604 self.assertEqual(param1.description, 'Type of instance to launch') |
| 588 self.assertEqual(param1.no_echo, True) | 605 self.assertEqual(param1.no_echo, True) |
| 589 self.assertEqual(param1.parameter_key, 'InstanceType') | 606 self.assertEqual(param1.parameter_key, 'InstanceType') |
| 590 | 607 |
| 591 self.assertEqual(param2.default_value, None) | 608 self.assertEqual(param2.default_value, None) |
| 592 self.assertEqual(param2.description, 'EC2 KeyPair') | 609 self.assertEqual(param2.description, 'EC2 KeyPair') |
| 593 self.assertEqual(param2.no_echo, True) | 610 self.assertEqual(param2.no_echo, True) |
| 594 self.assertEqual(param2.parameter_key, 'KeyName') | 611 self.assertEqual(param2.parameter_key, 'KeyName') |
| 595 | 612 |
| 613 self.assertEqual(template.capabilities_reason, 'Reason') |
| 614 |
| 615 self.assertEqual(len(template.capabilities), 1) |
| 616 self.assertEqual(template.capabilities[0].value, 'CAPABILITY_IAM') |
| 617 |
| 596 self.assert_request_parameters({ | 618 self.assert_request_parameters({ |
| 597 'Action': 'ValidateTemplate', | 619 'Action': 'ValidateTemplate', |
| 598 'TemplateBody': SAMPLE_TEMPLATE, | 620 'TemplateBody': SAMPLE_TEMPLATE, |
| 599 'TemplateURL': 'http://url', | 621 'TemplateURL': 'http://url', |
| 600 'Version': '2010-05-15', | 622 'Version': '2010-05-15', |
| 601 }) | 623 }) |
| 602 | 624 |
| 603 | 625 |
| 604 class TestCloudFormationCancelUpdateStack(CloudFormationConnectionBase): | 626 class TestCloudFormationCancelUpdateStack(CloudFormationConnectionBase): |
| 605 def default_body(self): | 627 def default_body(self): |
| 606 return """<CancelUpdateStackResult/>""" | 628 return """<CancelUpdateStackResult/>""" |
| 607 | 629 |
| 608 def test_cancel_update_stack(self): | 630 def test_cancel_update_stack(self): |
| 609 self.set_http_response(status_code=200) | 631 self.set_http_response(status_code=200) |
| 610 api_response = self.service_connection.cancel_update_stack('stack_name') | 632 api_response = self.service_connection.cancel_update_stack('stack_name') |
| 611 self.assertEqual(api_response, True) | 633 self.assertEqual(api_response, True) |
| 612 self.assert_request_parameters({ | 634 self.assert_request_parameters({ |
| 613 'Action': 'CancelUpdateStack', | 635 'Action': 'CancelUpdateStack', |
| 614 'StackName': 'stack_name', | 636 'StackName': 'stack_name', |
| 615 'Version': '2010-05-15', | 637 'Version': '2010-05-15', |
| 616 }) | 638 }) |
| 617 | 639 |
| 618 | 640 |
| 641 class TestCloudFormationEstimateTemplateCost(CloudFormationConnectionBase): |
| 642 def default_body(self): |
| 643 return """ |
| 644 { |
| 645 "EstimateTemplateCostResponse": { |
| 646 "EstimateTemplateCostResult": { |
| 647 "Url": "http://calculator.s3.amazonaws.com/calc5.html?ke
y=cf-2e351785-e821-450c-9d58-625e1e1ebfb6" |
| 648 } |
| 649 } |
| 650 } |
| 651 """ |
| 652 |
| 653 def test_estimate_template_cost(self): |
| 654 self.set_http_response(status_code=200) |
| 655 api_response = self.service_connection.estimate_template_cost( |
| 656 template_body='{}') |
| 657 self.assertEqual(api_response, |
| 658 'http://calculator.s3.amazonaws.com/calc5.html?key=cf-2e351785-e821-
450c-9d58-625e1e1ebfb6') |
| 659 self.assert_request_parameters({ |
| 660 'Action': 'EstimateTemplateCost', |
| 661 'ContentType': 'JSON', |
| 662 'TemplateBody': '{}', |
| 663 'Version': '2010-05-15', |
| 664 }) |
| 665 |
| 666 |
| 667 class TestCloudFormationGetStackPolicy(CloudFormationConnectionBase): |
| 668 def default_body(self): |
| 669 return """ |
| 670 { |
| 671 "GetStackPolicyResponse": { |
| 672 "GetStackPolicyResult": { |
| 673 "StackPolicyBody": "{...}" |
| 674 } |
| 675 } |
| 676 } |
| 677 """ |
| 678 |
| 679 def test_get_stack_policy(self): |
| 680 self.set_http_response(status_code=200) |
| 681 api_response = self.service_connection.get_stack_policy('stack-id') |
| 682 self.assertEqual(api_response, '{...}') |
| 683 self.assert_request_parameters({ |
| 684 'Action': 'GetStackPolicy', |
| 685 'ContentType': 'JSON', |
| 686 'StackName': 'stack-id', |
| 687 'Version': '2010-05-15', |
| 688 }) |
| 689 |
| 690 |
| 691 class TestCloudFormationSetStackPolicy(CloudFormationConnectionBase): |
| 692 def default_body(self): |
| 693 return """ |
| 694 { |
| 695 "SetStackPolicyResponse": { |
| 696 "SetStackPolicyResult": { |
| 697 "Some": "content" |
| 698 } |
| 699 } |
| 700 } |
| 701 """ |
| 702 |
| 703 def test_set_stack_policy(self): |
| 704 self.set_http_response(status_code=200) |
| 705 api_response = self.service_connection.set_stack_policy('stack-id', |
| 706 stack_policy_body='{}') |
| 707 self.assertEqual(api_response['Some'], 'content') |
| 708 self.assert_request_parameters({ |
| 709 'Action': 'SetStackPolicy', |
| 710 'ContentType': 'JSON', |
| 711 'StackName': 'stack-id', |
| 712 'StackPolicyBody': '{}', |
| 713 'Version': '2010-05-15', |
| 714 }) |
| 715 |
| 716 |
| 619 if __name__ == '__main__': | 717 if __name__ == '__main__': |
| 620 unittest.main() | 718 unittest.main() |
| OLD | NEW |