diff --git a/cachemonkey/discover.py b/cachemonkey/discover.py index b656135..d6c10d5 100644 --- a/cachemonkey/discover.py +++ b/cachemonkey/discover.py @@ -57,4 +57,3 @@ class ComputeDiscoverer(object): # TODO(belliott) parse response, handle errors return [] - diff --git a/cachemonkey/glance.py b/cachemonkey/glance.py index 3519f96..662b645 100644 --- a/cachemonkey/glance.py +++ b/cachemonkey/glance.py @@ -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 diff --git a/cachemonkey/tests/base.py b/cachemonkey/tests/base.py new file mode 100644 index 0000000..d39a29a --- /dev/null +++ b/cachemonkey/tests/base.py @@ -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) diff --git a/cachemonkey/tests/test_glance.py b/cachemonkey/tests/test_glance.py new file mode 100644 index 0000000..9ad5531 --- /dev/null +++ b/cachemonkey/tests/test_glance.py @@ -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) diff --git a/test-requirements.txt b/test-requirements.txt index 48bfc95..b141e03 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -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 diff --git a/tox.ini b/tox.ini index 5033095..758db95 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = py26,py27,py33,pep8 +envlist = py26,py27,pep8 minversion = 1.6 skipsdist = True