|
| 1 | +#!/usr/bin/env python |
| 2 | +# encoding: utf-8 |
| 3 | + |
| 4 | +import json |
| 5 | + |
| 6 | +import mock |
| 7 | + |
| 8 | +from ec2stack.helpers import read_file, generate_signature |
| 9 | +from . import Ec2StackAppTestCase |
| 10 | + |
| 11 | + |
| 12 | +class ServiceOfferingsTestCase(Ec2StackAppTestCase): |
| 13 | + |
| 14 | + def test_get_service_offering(self): |
| 15 | + data = self.get_example_data() |
| 16 | + data['Action'] = 'RunInstances' |
| 17 | + data['ImageId'] = 'a32d70ee-95e4-11e3-b2e4-d19c9d3e5e1d' |
| 18 | + data['InstanceType'] = 'Small Instance' |
| 19 | + data['MinCount'] = '0' |
| 20 | + data['MaxCount'] = '0' |
| 21 | + data['SecurityGroupId.1'] = 'example-security-group-id' |
| 22 | + data['SecurityGroup.1'] = 'example-security-group-name' |
| 23 | + data['KeyName'] = 'example-ssh-key-name' |
| 24 | + data['UserData'] = 'example-user-data' |
| 25 | + data['Signature'] = generate_signature(data, 'POST', 'localhost', '/') |
| 26 | + |
| 27 | + get = mock.Mock() |
| 28 | + get.return_value.text = read_file( |
| 29 | + 'tests/data/valid_run_instance.json' |
| 30 | + ) |
| 31 | + get.return_value.status_code = 200 |
| 32 | + |
| 33 | + get_service_offering = mock.Mock() |
| 34 | + get_service_offering.return_value = json.loads(read_file( |
| 35 | + 'tests/data/service_offerings_search.json' |
| 36 | + )) |
| 37 | + |
| 38 | + get_zone = mock.Mock() |
| 39 | + get_zone.return_value = json.loads(read_file( |
| 40 | + 'tests/data/zones_search.json' |
| 41 | + )) |
| 42 | + |
| 43 | + with mock.patch('requests.get', get): |
| 44 | + with mock.patch( |
| 45 | + 'ec2stack.providers.cloudstack.describe_items_request', |
| 46 | + get_service_offering |
| 47 | + ): |
| 48 | + with mock.patch( |
| 49 | + 'ec2stack.providers.cloudstack.zones.get_zone', |
| 50 | + get_zone |
| 51 | + ): |
| 52 | + response = self.post( |
| 53 | + '/', |
| 54 | + data=data |
| 55 | + ) |
| 56 | + |
| 57 | + self.assert_ok(response) |
| 58 | + assert 'RunInstancesResponse' in response.data |
| 59 | + |
| 60 | + def test_get_service_offering_invalid_name(self): |
| 61 | + data = self.get_example_data() |
| 62 | + data['Action'] = 'RunInstances' |
| 63 | + data['ImageId'] = 'a32d70ee-95e4-11e3-b2e4-d19c9d3e5e1d' |
| 64 | + data['InstanceType'] = 'invalid-instance-type' |
| 65 | + data['MinCount'] = '0' |
| 66 | + data['MaxCount'] = '0' |
| 67 | + data['SecurityGroupId.1'] = 'example-security-group-id' |
| 68 | + data['SecurityGroup.1'] = 'example-security-group-name' |
| 69 | + data['KeyName'] = 'example-ssh-key-name' |
| 70 | + data['UserData'] = 'example-user-data' |
| 71 | + data['Signature'] = generate_signature(data, 'POST', 'localhost', '/') |
| 72 | + |
| 73 | + get = mock.Mock() |
| 74 | + get.return_value.text = read_file( |
| 75 | + 'tests/data/valid_run_instance.json' |
| 76 | + ) |
| 77 | + get.return_value.status_code = 200 |
| 78 | + |
| 79 | + get_service_offering = mock.Mock() |
| 80 | + get_service_offering.return_value = json.loads(read_file( |
| 81 | + 'tests/data/service_offerings_search.json' |
| 82 | + )) |
| 83 | + |
| 84 | + get_zone = mock.Mock() |
| 85 | + get_zone.return_value = json.loads(read_file( |
| 86 | + 'tests/data/zones_search.json' |
| 87 | + )) |
| 88 | + |
| 89 | + with mock.patch('requests.get', get): |
| 90 | + with mock.patch( |
| 91 | + 'ec2stack.providers.cloudstack.describe_items_request', |
| 92 | + get_service_offering |
| 93 | + ): |
| 94 | + with mock.patch( |
| 95 | + 'ec2stack.providers.cloudstack.zones.get_zone', |
| 96 | + get_zone |
| 97 | + ): |
| 98 | + response = self.post( |
| 99 | + '/', |
| 100 | + data=data |
| 101 | + ) |
| 102 | + |
| 103 | + self.assert_bad_request(response) |
| 104 | + assert 'InvalidServiceOffering.NotFound' in response.data |
0 commit comments