From 01ce62fc7bfb52f34026ded7d9da2dfa0eb00d05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Magimel?= Date: Thu, 21 Aug 2014 01:26:02 +0200 Subject: [PATCH] Add tests for the client Change-Id: Icbca6ffbd792515e6e0d4b499aeaa4be0ed5bc6a --- cloudkittyclient/tests/base.py | 27 +++++++ cloudkittyclient/tests/common/__init__.py | 0 cloudkittyclient/tests/common/test_auth.py | 94 ++++++++++++++++++++++ cloudkittyclient/tests/test_client.py | 45 +++++++++++ cloudkittyclient/tests/test_fake.py | 6 -- 5 files changed, 166 insertions(+), 6 deletions(-) create mode 100644 cloudkittyclient/tests/base.py create mode 100644 cloudkittyclient/tests/common/__init__.py create mode 100644 cloudkittyclient/tests/common/test_auth.py create mode 100644 cloudkittyclient/tests/test_client.py delete mode 100644 cloudkittyclient/tests/test_fake.py diff --git a/cloudkittyclient/tests/base.py b/cloudkittyclient/tests/base.py new file mode 100644 index 0000000..588b5b3 --- /dev/null +++ b/cloudkittyclient/tests/base.py @@ -0,0 +1,27 @@ +# Copyright 2010-2011 OpenStack Foundation +# +# 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 fixtures + +from cloudkittyclient.openstack.common import test + + +class TestCase(test.BaseTestCase): + """Test case base class for all unit tests.""" + + def setUp(self): + """Run before each test method to initialize test environment.""" + + super(TestCase, self).setUp() + self.log_fixture = self.useFixture(fixtures.FakeLogger()) diff --git a/cloudkittyclient/tests/common/__init__.py b/cloudkittyclient/tests/common/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/cloudkittyclient/tests/common/test_auth.py b/cloudkittyclient/tests/common/test_auth.py new file mode 100644 index 0000000..37aaae9 --- /dev/null +++ b/cloudkittyclient/tests/common/test_auth.py @@ -0,0 +1,94 @@ +# -*- coding: utf-8 -*- +# Copyright 2014 Objectif Libre +# +# 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. + +""" +Tests for the Keystone auth plugin +`cloudkittyclient.common.KeystoneAuthPluginTest`. +""" + +from keystoneclient.v2_0 import client as ksclient +import mock + +from cloudkittyclient.common import auth +from cloudkittyclient.openstack.common.apiclient import client +from cloudkittyclient.openstack.common.apiclient import exceptions +from cloudkittyclient.tests import base + + +@mock.patch.object(ksclient, 'Client') +class KeystoneAuthPluginTest(base.TestCase): + def setUp(self): + super(KeystoneAuthPluginTest, self).setUp() + plugin = auth.KeystoneAuthPlugin( + username="fake-username", + password="fake-password", + tenant_name="fake-tenant-name", + auth_url="http://auth:5000", + endpoint="http://cloudkitty:8888") + self.cs = client.HTTPClient(auth_plugin=plugin) + + def test_authenticate(self, mock_ksclient): + self.cs.authenticate() + mock_ksclient.assert_called_with( + username="fake-username", + password="fake-password", + tenant_name="fake-tenant-name", + auth_url="http://auth:5000") + + def test_token_and_endpoint(self, mock_ksclient): + self.cs.authenticate() + (token, endpoint) = self.cs.auth_plugin.token_and_endpoint( + "fake-endpoint-type", "fake-service-type") + self.assertIsInstance(token, mock.MagicMock) + self.assertEqual("http://cloudkitty:8888", endpoint) + + def test_token_and_endpoint_before_auth(self, mock_ksclient): + (token, endpoint) = self.cs.auth_plugin.token_and_endpoint( + "fake-endpoint-type", "fake-service-type") + self.assertIsNone(token, None) + self.assertIsNone(endpoint, None) + + def test_sufficient_options_missing_tenant_name(self, mock_ksclient): + plugin = auth.KeystoneAuthPlugin( + username="fake-username", + password="fake-password", + auth_url="http://auth:5000", + endpoint="http://cloudkitty:8888") + cs = client.HTTPClient(auth_plugin=plugin) + self.assertRaises(exceptions.AuthPluginOptionsMissing, + cs.authenticate) + + +@mock.patch.object(ksclient, 'Client') +class KeystoneAuthPluginTokenTest(base.TestCase): + def test_token_and_endpoint(self, mock_ksclient): + plugin = auth.KeystoneAuthPlugin( + token="fake-token", + endpoint="http://cloudkitty:8888") + cs = client.HTTPClient(auth_plugin=plugin) + + cs.authenticate() + (token, endpoint) = cs.auth_plugin.token_and_endpoint( + "fake-endpoint-type", "fake-service-type") + self.assertEqual('fake-token', token) + self.assertEqual('http://cloudkitty:8888', endpoint) + + def test_sufficient_options_missing_endpoint(self, mock_ksclient): + plugin = auth.KeystoneAuthPlugin( + token="fake-token") + cs = client.HTTPClient(auth_plugin=plugin) + + self.assertRaises(exceptions.AuthPluginOptionsMissing, + cs.authenticate) diff --git a/cloudkittyclient/tests/test_client.py b/cloudkittyclient/tests/test_client.py new file mode 100644 index 0000000..9d74c44 --- /dev/null +++ b/cloudkittyclient/tests/test_client.py @@ -0,0 +1,45 @@ +# -*- coding: utf-8 -*- +# Copyright 2014 Objectif Libre +# +# 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. +# +# @author: François Magimel (linkid) + +""" +Tests for the main Client interface `cloudkittyclient.client`. +""" + +import mock +import six + +from cloudkittyclient import client +from cloudkittyclient.common import auth +from cloudkittyclient.tests import base +from cloudkittyclient.v1 import client as v1client + + +VERSIONS = { + '1': v1client, +} + + +class ClientTest(base.TestCase): + def test_client_unsupported_version(self): + self.assertRaises(ImportError, client.Client, + '111.11', **{}) + + def test_client(self): + for (version, instance) in six.iteritems(VERSIONS): + with mock.patch.object(auth, 'KeystoneAuthPlugin'): + c = client.Client(version, **{}) + self.assertIsInstance(c, instance.Client) diff --git a/cloudkittyclient/tests/test_fake.py b/cloudkittyclient/tests/test_fake.py deleted file mode 100644 index e4646c8..0000000 --- a/cloudkittyclient/tests/test_fake.py +++ /dev/null @@ -1,6 +0,0 @@ -import testtools - - -class FakeTest(testtools.TestCase): - def test_foo(self): - pass