Skip to content
Permalink
Browse files
add some run instance unit tests.... more requireD
  • Loading branch information
imduffy15 committed Mar 6, 2014
1 parent 3fe1906 commit 6b14fc0987e0b903fbe84d14e16ce4ad1cb1ca96
Show file tree
Hide file tree
Showing 15 changed files with 98 additions and 59 deletions.
@@ -27,9 +27,9 @@ def _get_action(action):
actions = {
'AttachVolume': volumes.attach_volume,
'AuthorizeSecurityGroupEgress':
security_groups.authenticate_security_group_egress,
security_groups.authenticate_security_group_egress,
'AuthorizeSecurityGroupIngress':
security_groups.authenticate_security_group_ingress,
security_groups.authenticate_security_group_ingress,
'CreateKeyPair': keypairs.create_keypair,
'CreateSecurityGroup': security_groups.create_security_group,
'CreateVolume': volumes.create_volume,
@@ -51,9 +51,9 @@ def _get_action(action):
'RegisterSecretKey': register_secret_key,
'RemoveSecretKey': remove_secret_key,
'RevokeSecurityGroupEgress':
security_groups.revoke_security_group_egress,
security_groups.revoke_security_group_egress,
'RevokeSecurityGroupIngress':
security_groups.revoke_security_group_ingress,
security_groups.revoke_security_group_ingress,
'RunInstances': instances.run_instance,
'StartInstances': instances.start_instance,
'StopInstances': instances.stop_instance,
@@ -7,7 +7,6 @@


class Ec2stackError(Exception):

def __init__(self, code, error, message):
self.code = code
self.error = error
@@ -2,10 +2,10 @@
# encoding: utf-8

import argparse
import requests

from xml.dom.minidom import parseString as xmlparse

import requests


def _generate_args(description):
parser = argparse.ArgumentParser(
@@ -15,7 +15,6 @@ class Ec2StackTestCase(TestCase):


class Ec2StackAppTestCase(FlaskTestCaseMixin, Ec2StackTestCase):

def _create_app(self):
return create_app(settings=settings)

@@ -8,7 +8,6 @@


class ControllerTestCase(Ec2StackAppTestCase):

def test_invalid_action(self):
data = self.get_example_data()
data['Action'] = 'InvalidAction'
@@ -10,7 +10,6 @@


class DiskOfferingsTestCase(Ec2StackAppTestCase):

def test_get_disk_offering(self):
data = self.get_example_data()
data['Action'] = 'CreateVolume'
@@ -8,7 +8,6 @@


class ImagesTestCase(Ec2StackAppTestCase):

def test_describe_image(self):
data = self.get_example_data()
data['Action'] = 'DescribeImages'
@@ -10,7 +10,6 @@


class InstancesTestCase(Ec2StackAppTestCase):

def test_describe_instance_attribute(self):
data = self.get_example_data()
data['Action'] = 'DescribeInstanceAttribute'
@@ -270,45 +269,94 @@ def test_terminate_instance(self):
self.assert_ok(response)
assert 'TerminateInstancesResponse' in response.data

def test_run_instance(self):
data = self.get_example_data()
data['Action'] = 'RunInstances'
data['ImageId'] = 'a32d70ee-95e4-11e3-b2e4-d19c9d3e5e1d'
data['MinCount'] = '0'
data['MaxCount'] = '0'
data['SecurityGroupId.1'] = 'example-security-group-id'
data['SecurityGroup.1'] = 'example-security-group-name'
data['KeyName'] = 'example-ssh-key-name'
data['UserData'] = 'example-user-data'
data['Signature'] = generate_signature(data, 'POST', 'localhost')

get = mock.Mock()
get.return_value.text = read_file(
'tests/data/valid_run_instance.json'
)
get.return_value.status_code = 200

get_service_offering = mock.Mock()
get_service_offering.return_value = json.loads(read_file(
'tests/data/service_offering_search.json'
))

get_zone = mock.Mock()
get_zone.return_value = json.loads(read_file(
'tests/data/zones_search.json'
))

with mock.patch('requests.get', get):
with mock.patch(
'ec2stack.providers.cloudstack.service_offerings.get_service_offering',
get_service_offering
):
with mock.patch(
'ec2stack.providers.cloudstack.zones.get_zone',
get_zone
):
response = self.post(
'/',
data=data
)

self.assert_ok(response)
assert 'RunInstancesResponse' in response.data

# TODO revist this at a later point
# def test_run_instance(self):
# data = self.get_example_data()
# data['Action'] = 'RunInstances'
# data['ImageId'] = 'image-id'
# data['Placement.AvailabilityZone'] = 'Sandbox-simulator'
# data['InstanceType'] = 'Small Instance'
# data['Signature'] = generate_signature(data, 'POST', 'localhost')
#
# get = mock.Mock()
# get.return_value.text = read_file(
# 'tests/data/valid_run_instance.json'
# )
# get.return_value.status_code = 200
#
# get_service_offering = mock.Mock()
# get_service_offering.return_value = json.loads(read_file(
# 'tests/data/service_offering_search.json'
# ))
#
# get_zone = mock.Mock()
# get_zone.return_value = json.loads(read_file(
# 'tests/data/zones_search.json'
# ))
#
# with mock.patch('requests.get', get):
# with mock.patch(
# 'ec2stack.providers.cloudstack.service_offerings.get_service_offering',
# get_service_offering
# ):
# with mock.patch(
# 'ec2stack.providers.cloudstack.zones.get_zone',
# get_zone
# ):
# response = self.post(
# '/',
# data=data
# )
#
# self.assert_ok(response)
# assert 'RunInstancesResponse' in response.data
def test_run_instance_with_zone_and_type_(self):
data = self.get_example_data()
data['Action'] = 'RunInstances'
data['InstanceType'] = 'micro'
data['Placement.AvailabilityZone'] = 'example-zone'
data['ImageId'] = 'a32d70ee-95e4-11e3-b2e4-d19c9d3e5e1d'
data['MinCount'] = '0'
data['MaxCount'] = '0'
data['SecurityGroupId.1'] = 'example-security-group-id'
data['SecurityGroup.1'] = 'example-security-group-name'
data['KeyName'] = 'example-ssh-key-name'
data['UserData'] = 'example-user-data'
data['Signature'] = generate_signature(data, 'POST', 'localhost')

get = mock.Mock()
get.return_value.text = read_file(
'tests/data/valid_run_instance.json'
)
get.return_value.status_code = 200

get_service_offering = mock.Mock()
get_service_offering.return_value = json.loads(read_file(
'tests/data/service_offering_search.json'
))

get_zone = mock.Mock()
get_zone.return_value = json.loads(read_file(
'tests/data/zones_search.json'
))

with mock.patch('requests.get', get):
with mock.patch(
'ec2stack.providers.cloudstack.service_offerings.get_service_offering',
get_service_offering
):
with mock.patch(
'ec2stack.providers.cloudstack.zones.get_zone',
get_zone
):
response = self.post(
'/',
data=data
)

self.assert_ok(response)
assert 'RunInstancesResponse' in response.data
@@ -10,7 +10,6 @@


class KeyPairTestCase(Ec2StackAppTestCase):

def test_create_keypair(self):
data = self.get_example_data()
data['Action'] = 'CreateKeyPair'
@@ -8,7 +8,6 @@


class PasswordTestCase(Ec2StackAppTestCase):

def test_get_password_data(self):
data = self.get_example_data()
data['Action'] = 'GetPasswordData'
@@ -10,7 +10,6 @@


class SecurityGroupTestCase(Ec2StackAppTestCase):

def test_authorize_security_group_ingress_by_name(self):
data = self.get_example_data()
data['Action'] = 'AuthorizeSecurityGroupIngress'
@@ -7,6 +7,8 @@
CLOUDSTACK_CUSTOM_DISK_OFFERING = 'Custom'
CLOUDSTACK_DEFAULT_ZONE = 'CH-GV2'

INSTANCE_TYPE_MAP = {'m1.small': 'micro'}

DEBUG = False
TESTING = True

@@ -9,7 +9,6 @@


class FlaskTestCaseMixin(object):

@staticmethod
def _html_data(kwargs):
if not kwargs.get('content_type'):
@@ -10,7 +10,6 @@


class VolumeTestCase(Ec2StackAppTestCase):

def test_attach_volume(self):
data = self.get_example_data()
data['Action'] = 'AttachVolume'
@@ -10,7 +10,6 @@


class ZonesTestCase(Ec2StackAppTestCase):

def test_describe_zone(self):
data = self.get_example_data()
data['Action'] = 'DescribeAvailabilityZones'

0 comments on commit 6b14fc0

Please sign in to comment.