Skip to content

Commit

Permalink
Add deplyVM tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brogand93 committed Jun 5, 2014
1 parent 20c32c5 commit 390026b
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 6 deletions.
5 changes: 2 additions & 3 deletions gstack/controllers/instances.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,10 @@ def getinstance(projectid, authorization, zone, instance):
@authentication.required
def addinstance(authorization, projectid, zone):
data = json.loads(request.data)
print data
args = {}
args['name'] = data['name']
args['serviceoffering'] = data['machineType'].rsplit('/', 1)[1]
print args['serviceoffering']
args['template'] = data['disks'][0]['initializeParams']['sourceImage'].rsplit('/', 1)[1]
args['zone'] = zone

Expand Down Expand Up @@ -321,8 +321,7 @@ def addinstance(authorization, projectid, zone):
else:
populated_response = operations.create_response(
projectid=projectid,
operationid=deployment_result[
'deployvirtualmachineresponse']['jobid'],
operationid=deployment_result['deployvirtualmachineresponse']['jobid'],
authorization=authorization
)

Expand Down
2 changes: 1 addition & 1 deletion gstack/controllers/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def _create_instance_response(async_result, projectid, authorization):
'getinstance',
projectid=projectid,
zone=async_result['jobresult']['virtualmachine']['zonename'],
instance=async_result['jobresult']['virtualmachine']['displayname']))
instance=async_result['jobresult']['virtualmachine']['name']))
_add_sshkey_metadata(
authorization=authorization,
publickey=publickey_storage[projectid],
Expand Down
2 changes: 1 addition & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def _auth_example_user(self):

with mock.patch('requests.get', get):
self.get('/oauth2/auth?scope=example&redirect_uri=http://127.0.0.1:9999&response_type=code&client_id=ExampleAPIKey&access_type=offline')
response = self.post('/oauth2/token', data=data)
response = self.post_html('/oauth2/token', data=data)

GStackAppTestCase.access_token = json.loads(response.data)['access_token']

Expand Down
6 changes: 6 additions & 0 deletions tests/data/valid_async_deploy_vm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"deployvirtualmachineresponse": {
"id": "d0140ed4-988e-495c-b46f-19b9637ee969",
"jobid": "955dc9dd-f900-4fb4-80ef-b96ca3fc2687"
}
}
85 changes: 85 additions & 0 deletions tests/instances_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import mock
import json
from gstack import publickey_storage

from gstack.helpers import read_file
from . import GStackAppTestCase
Expand Down Expand Up @@ -117,3 +118,87 @@ def test_get_instance_instance_not_found(self):

# self.assert_ok(response)

def test_add_instance(self):
data = {
'kind': 'compte#instance',
'machineType': 'https://localhost:5000/compte/v1/projects/brogand93@darrenbrogan.ie/zones/ch-gva-2/machineTypes/machinetypename',
'description': '',
'tags': {
'items': []
},
'disks': [{
'deviceName': 'machinetypename',
'initializeParams': {
'diskName': 'machinetypename',
'sourceImage': 'https://localhost:5000/compte/v1/projects/brogand93@darrenbrogan.ie/global/images/imagename'
},
'autoDelete': False,
'boot': True,
'mode': 'READ_WRITE',
'type': 'PERSISTENT'
}],
'metadata': {
'items': [],
'kind': 'compte#metadata'
},
'networkInterfaces': [{
'accessConfigs': [{
'type': 'ONE_TO_ONE_NAT',
'name': 'External NAT'
}],
'network': 'https://localhost:5000/compte/v1/projects/brogand93@darrenbrogan.ie/global/networks/networkname'
}],
'name': 'foobar'
}

publickey_storage['admin'] = 'testkey'

data = json.dumps(data)

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

get_templates = mock.Mock()
get_templates.return_value = json.loads(read_file('tests/data/valid_describe_images.json'))

get_zones = mock.Mock()
get_zones.return_value = json.loads(read_file('tests/data/valid_describe_zone.json'))

get_service_offerings = mock.Mock()
get_service_offerings.return_value = json.loads(read_file('tests/data/valid_describe_service_offering.json'))

get_networks = mock.Mock()
get_networks.return_value = json.loads(read_file('tests/data/valid_describe_security_group.json'))

get_async_result = mock.Mock()
get_async_result.return_value = json.loads(read_file('tests/data/valid_run_instance.json'))

with mock.patch('requests.get', get):
with mock.patch(
'gstack.controllers.images._get_templates',
get_templates
):
with mock.patch(
'gstack.controllers.zones._get_zones',
get_zones
):
with mock.patch(
'gstack.controllers.machine_type._get_machinetypes',
get_service_offerings
):
with mock.patch(
'gstack.controllers.networks._get_networks',
get_networks
):
with mock.patch(
'gstack.controllers.operations._get_async_result',
get_async_result
):
headers = {
'authorization': 'Bearer ' + str(GStackAppTestCase.access_token),
}

response = self.post_json('/compute/v1/projects/admin/zones/zonename/instances', data=data, headers=headers)

self.assert_ok(response)
12 changes: 11 additions & 1 deletion tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,28 @@ class FlaskTestCaseMixin(object):
def _html_data(kwargs):
if not kwargs.get('content_type'):
kwargs['content_type'] = 'application/x-www-form-urlencoded'
return kwargs

@staticmethod
def _json_data(kwargs):
if not kwargs.get('content_type'):
kwargs['content_type'] = 'application/json'
return kwargs

@staticmethod
def _request(method, *args, **kwargs):
return method(*args, **kwargs)

def post(self, *args, **kwargs):
def post_html(self, *args, **kwargs):
return (
self._request(self.client.post, *args, **self._html_data(kwargs))
)

def post_json(self, *args, **kwargs):
return (
self._request(self.client.post, *args, **self._json_data(kwargs))
)

def get(self, *args, **kwargs):
return self._request(self.client.get, *args, **kwargs)

Expand Down

0 comments on commit 390026b

Please sign in to comment.