Add some unit test plumbing
* Add a base test class derived from oslotest * Remove py33 support from tox * Cleanup pep8 errors Also, Jenkins requires a test to exist to pass, so this commit also contains a fix for Glance client endpoint rotation and an associated test case. Change-Id: Id9cfbe6cf54c2d5b078cbcb6dcee690915afa7ba
This commit is contained in:
@@ -57,4 +57,3 @@ class ComputeDiscoverer(object):
|
||||
|
||||
# TODO(belliott) parse response, handle errors
|
||||
return []
|
||||
|
||||
|
||||
@@ -32,8 +32,13 @@ CONF.register_opts(opts, group='glance')
|
||||
class Client(object):
|
||||
"""Glance client wrapper that handles auth and config."""
|
||||
|
||||
def __init__(self):
|
||||
self.authclient = auth.Client()
|
||||
def __init__(self, auth_client=None):
|
||||
|
||||
if auth_client:
|
||||
self.authclient = auth_client
|
||||
else:
|
||||
self.authclient = auth.Client()
|
||||
|
||||
self.authclient.auth()
|
||||
|
||||
self.endpoints = self._endpoints()
|
||||
@@ -46,7 +51,7 @@ class Client(object):
|
||||
def _client(self):
|
||||
# pick a glance endpoint via round-robin
|
||||
endpoint = self.endpoints[self.next_]
|
||||
self.next_ += 1
|
||||
self.next_ = (self.next_ + 1) % len(self.endpoints)
|
||||
|
||||
api_version = 2
|
||||
token = self.authclient.token
|
||||
|
||||
28
cachemonkey/tests/base.py
Normal file
28
cachemonkey/tests/base.py
Normal file
@@ -0,0 +1,28 @@
|
||||
# Copyright 2014 Rackspace Hosting
|
||||
# All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
from oslo.config import cfg
|
||||
from oslotest import base
|
||||
|
||||
CONF = cfg.CONF
|
||||
|
||||
|
||||
class BaseTestCase(base.BaseTestCase):
|
||||
|
||||
def flags(self, **kw):
|
||||
"""Override flag variables for a test."""
|
||||
group = kw.pop('group', None)
|
||||
for k, v in kw.iteritems():
|
||||
CONF.set_override(k, v, group)
|
||||
53
cachemonkey/tests/test_glance.py
Normal file
53
cachemonkey/tests/test_glance.py
Normal file
@@ -0,0 +1,53 @@
|
||||
# Copyright 2014 Rackspace Hosting
|
||||
# All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import mock
|
||||
|
||||
from cachemonkey import glance
|
||||
from cachemonkey.tests import base
|
||||
|
||||
|
||||
class GlanceTestCase(base.BaseTestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(GlanceTestCase, self).setUp()
|
||||
|
||||
self.endpoints = ['http://1.2.3.4:1234', 'http://2.3.4.5:2345']
|
||||
self.flags(endpoints=self.endpoints, group='glance')
|
||||
|
||||
class FakeAuthClient(object):
|
||||
token = 'token'
|
||||
|
||||
def auth(self):
|
||||
return
|
||||
|
||||
fake_auth_client = FakeAuthClient()
|
||||
self.client = glance.Client(auth_client=fake_auth_client)
|
||||
|
||||
@mock.patch('glanceclient.Client')
|
||||
def test_endpoint_round_robin(self, mock_glanceclient):
|
||||
api_version = 2
|
||||
|
||||
for i in range(len(self.endpoints)):
|
||||
self.client.dosomething()
|
||||
|
||||
call = mock.call(api_version, self.endpoints[i], token='token')
|
||||
self.assertEqual(call, mock_glanceclient.call_args)
|
||||
|
||||
# call once more to confirm first endpoint is used again
|
||||
self.client.dosomething()
|
||||
|
||||
call = mock.call(api_version, self.endpoints[0], token='token')
|
||||
self.assertEqual(call, mock_glanceclient.call_args)
|
||||
@@ -1,6 +1,4 @@
|
||||
coverage>=3.6
|
||||
hacking>=0.8.0,<0.9
|
||||
oslotest
|
||||
pylint==0.25.2
|
||||
testrepository>=0.0.17
|
||||
testscenarios>=0.4
|
||||
testtools>=0.9.32,<0.9.35
|
||||
|
||||
Reference in New Issue
Block a user