Skip to content

Commit

Permalink
Merge pull request #78 from rackerlabs/lb_api_tests
Browse files Browse the repository at this point in the history
Initial load balancer api tests
  • Loading branch information
kivattik committed Sep 19, 2014
2 parents 218f52a + f2cb947 commit a883d65
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 34 deletions.
64 changes: 64 additions & 0 deletions mimic/test/fixtures.py
@@ -0,0 +1,64 @@
"""
Define fixtures to provide common functionality for Mimic testing
"""
from mimic.test.helpers import request
from mimic.core import MimicCore
from mimic.resource import MimicRoot
from twisted.internet.task import Clock
import json
import treq


class APIMockHelper(object):
"""
Provides common functionality for mimic tests
"""

def __init__(self, test_case, apis):
"""
Initialize a mimic core and the specified :obj:`mimic.imimic.IAPIMock`s
:param apis: A list of :obj:`mimic.imimic.IAPIMock` objects to be initialized
"""
self.core = MimicCore(Clock(), apis)
self.root = MimicRoot(self.core).app.resource()
# Pass in arbitrary username and password
response = request(
self, self.root, "POST", "/identity/v2.0/tokens",
json.dumps({
"auth": {
"passwordCredentials": {
"username": "test1",
"password": "test1password",
},
}
})
)
auth_response = test_case.successResultOf(response)
self.service_catalog_json = test_case.successResultOf(
treq.json_content(auth_response))
self.uri = self.nth_endpoint_public(0)

def nth_endpoint_public(self, n):
"""
Return the publicURL for the ``n``th endpoint.
:param int n: The index of the endpoint in the first catalog entry to return
"""
return (
self.service_catalog_json
['access']['serviceCatalog'][0]['endpoints'][n]['publicURL']
)

def get_service_endpoint(self, service_name, region=''):
"""
Return the publicURL for the given service and region. Note that if there are multiple
endpoints for a given region, the first will be returned, and if no region is specified,
the first endpoint will be returned.
:param unicode service_name: The name of the service for which to get an endpoint as
listed in the service catalog
:param unicode region: The service catalog region of the desired endpoint
"""
for service in self.service_catalog_json['access']['serviceCatalog']:
if service['name'] == service_name:
for item in service['endpoints']:
if (item['region'] == region) or (region == ''):
return item['publicURL']
98 changes: 98 additions & 0 deletions mimic/test/test_loadbalancer.py
@@ -1,7 +1,15 @@
"""
Unit tests for the
"""

import json
import treq

from twisted.trial.unittest import SynchronousTestCase
from mimic.canned_responses.loadbalancer import load_balancer_example
from mimic.test.fixtures import APIMockHelper
from mimic.rest.loadbalancer_api import LoadBalancerApi
from mimic.test.helpers import request


class ResponseGenerationTests(SynchronousTestCase):
Expand Down Expand Up @@ -65,3 +73,93 @@ def test_canned_loadbalancer(self):
"contentCaching": {"enabled": False}}

self.assertEqual(actual, lb_example)


class LoadbalancerAPITests(SynchronousTestCase):
"""
Tests for the Loadbalancer plugin API
"""

def setUp(self):
"""
Create a :obj:`MimicCore` with :obj:`LoadBalancerApi` as the only plugin
"""
fixture = APIMockHelper(self, [LoadBalancerApi()])
self.root = fixture.root
self.uri = fixture.uri

def _create_loadbalancer(self, name):
"""
Helper methond to create a load balancer and return the lb_id
"""
create_lb = request(
self, self.root, "POST", self.uri + '/loadbalancers',
json.dumps({
"loadBalancer": {
"name": name,
"protocol": "HTTP",
"virtualIps": [{"type": "PUBLIC"}]
}
})
)
create_lb_response = self.successResultOf(create_lb)
create_lb_response_body = self.successResultOf(treq.json_content(create_lb_response))
return create_lb_response_body['loadBalancer']['id']

def test_add_load_balancer(self):
"""
Test to verify :func:`add_load_balancer` on ``POST /v1.0/<tenant_id>/loadbalancers``
"""
lb_name = 'mimic_lb'
create_lb = request(
self, self.root, "POST", self.uri + '/loadbalancers',
json.dumps({
"loadBalancer": {
"name": lb_name,
"protocol": "HTTP",
"virtualIps": [{"type": "PUBLIC"}]
}
})
)
create_lb_response = self.successResultOf(create_lb)
create_lb_response_body = self.successResultOf(treq.json_content(create_lb_response))
self.assertEqual(create_lb_response.code, 202)
self.assertEqual(create_lb_response_body['loadBalancer']['name'], lb_name)

def test_list_loadbalancers(self):
"""
Test to verify :func:`list_load_balancers` with on ``GET /v1.0/<tenant_id>/loadbalancers``
Create two load balancers, then list them and verify the ids
"""
test1_id = self._create_loadbalancer('test1')
test2_id = self._create_loadbalancer('test2')
list_lb = request(self, self.root, "GET", self.uri + '/loadbalancers')
list_lb_response = self.successResultOf(list_lb)
list_lb_response_body = self.successResultOf(treq.json_content(list_lb_response))
self.assertEqual(list_lb_response.code, 200)
self.assertEqual(len(list_lb_response_body['loadBalancers']), 2)
self.assertTrue(list_lb_response_body['loadBalancers'][0]['id'] in [test1_id, test2_id])
self.assertTrue(list_lb_response_body['loadBalancers'][1]['id'] in [test1_id, test2_id])
self.assertTrue(list_lb_response_body['loadBalancers'][0]['id'] !=
list_lb_response_body['loadBalancers'][1]['id'])

def test_delete_loadbalancer(self):
"""
Test to verify :func:`delete_load_balancer` with on
``DELETE /v1.0/<tenant_id>/loadbalancers/<lb_id>``
Create two load balancers, then list them and verify the ids
"""
# These will fail if the servers weren't created
test1_id = self._create_loadbalancer('test1')
test2_id = self._create_loadbalancer('test2')
delete_lb = request(self, self.root, 'DELETE', self.uri + '/loadbalancers/' + str(test1_id))
del_lb_response = self.successResultOf(delete_lb)
# This response code does not match the Rackspace documentation which specifies a 200 response
# See comment: http://bit.ly/1AVHs3v
self.assertEqual(del_lb_response.code, 202)
# List lb to make sure the correct lb is gone and the other remains
list_lb = request(self, self.root, "GET", self.uri + '/loadbalancers')
list_lb_response = self.successResultOf(list_lb)
list_lb_response_body = self.successResultOf(treq.json_content(list_lb_response))
self.assertTrue(len(list_lb_response_body['loadBalancers']), 1)
self.assertTrue(list_lb_response_body['loadBalancers'][0]['id'] == test2_id)
43 changes: 9 additions & 34 deletions mimic/test/test_nova.py
@@ -1,17 +1,14 @@


import itertools
import json
import treq

from twisted.trial.unittest import SynchronousTestCase
from twisted.internet.task import Clock

from mimic.canned_responses.nova import server_template
from mimic.core import MimicCore
from mimic.resource import MimicRoot
from mimic.test.helpers import json_request, request
from mimic.rest.nova_api import NovaApi
from mimic.test.fixtures import APIMockHelper


class ResponseGenerationTests(SynchronousTestCase):
Expand Down Expand Up @@ -134,25 +131,11 @@ def setUp(self):
Create a :obj:`MimicCore` with :obj:`NovaApi` as the only plugin,
and create a server
"""
self.core = MimicCore(Clock(), [NovaApi(["ORD", "MIMIC"])])
self.root = MimicRoot(self.core).app.resource()
self.response = request(
self, self.root, "POST", "/identity/v2.0/tokens",
json.dumps({
"auth": {
"passwordCredentials": {
"username": "test1",
"password": "test1password",
},
}
})
)
self.auth_response = self.successResultOf(self.response)
self.service_catalog_json = self.successResultOf(
treq.json_content(self.auth_response))
self.uri = self.nth_endpoint_public(0)
fixture = APIMockHelper(self, [NovaApi(["ORD", "MIMIC"])])
self.root = fixture.root
self.uri = fixture.uri
self.server_name = 'test_server'
self.create_server = request(
create_server = request(
self, self.root, "POST", self.uri + '/servers',
json.dumps({
"server": {
Expand All @@ -161,19 +144,11 @@ def setUp(self):
"flavorRef": "test-flavor"
}
}))
self.create_server_response = self.successResultOf(self.create_server)
self.create_server_response_body = self.successResultOf(
self.create_server_response = self.successResultOf(create_server)
create_server_response_body = self.successResultOf(
treq.json_content(self.create_server_response))
self.server_id = self.create_server_response_body['server']['id']

def nth_endpoint_public(self, n):
"""
Return the publicURL for the ``n``th endpoint.
"""
return (
self.service_catalog_json
['access']['serviceCatalog'][0]['endpoints'][n]['publicURL']
)
self.server_id = create_server_response_body['server']['id']
self.nth_endpoint_public = fixture.nth_endpoint_public

def test_create_server(self):
"""
Expand Down

0 comments on commit a883d65

Please sign in to comment.