From 4297e5781b03bad1c67cfdaa6cfdb73f6d1385de Mon Sep 17 00:00:00 2001
From: Josh Kearney <josh@jk0.org>
Date: Thu, 24 Jan 2013 12:33:17 -0600
Subject: [PATCH] First pass at adding compute unit tests.

Change-Id: Icf3340d457f75eec89bb0e5c9b4b953c3b81020f
---
 tests/compute/__init__.py                     | 14 +++++
 tests/compute/test_compute.py                 | 59 +++++++++++++++++++
 ...r_clientcache.py => test_clientmanager.py} | 27 ++++-----
 tests/test_shell.py                           |  9 ++-
 tests/utils.py                                |  5 +-
 5 files changed, 92 insertions(+), 22 deletions(-)
 create mode 100644 tests/compute/__init__.py
 create mode 100644 tests/compute/test_compute.py
 rename tests/{test_clientmanager_clientcache.py => test_clientmanager.py} (59%)

diff --git a/tests/compute/__init__.py b/tests/compute/__init__.py
new file mode 100644
index 0000000000..ebf59b327e
--- /dev/null
+++ b/tests/compute/__init__.py
@@ -0,0 +1,14 @@
+#   Copyright 2013 OpenStack, LLC.
+#
+#   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.
+#
diff --git a/tests/compute/test_compute.py b/tests/compute/test_compute.py
new file mode 100644
index 0000000000..bbac7123af
--- /dev/null
+++ b/tests/compute/test_compute.py
@@ -0,0 +1,59 @@
+#   Copyright 2013 OpenStack, LLC.
+#
+#   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 openstackclient.common import clientmanager
+from openstackclient.compute import client as compute_client
+from tests import utils
+
+
+class FakeClient(object):
+    def __init__(self, endpoint=None, **kwargs):
+        self.client = mock.MagicMock()
+        self.servers = mock.MagicMock()
+
+        self.client.auth_url = kwargs['auth_url']
+
+
+class TestCompute(utils.TestCase):
+    def setUp(self):
+        super(TestCompute, self).setUp()
+
+        self.auth_token = "foobar"
+        self.auth_url = "http://0.0.0.0"
+
+        api_version = {"compute": "2"}
+
+        compute_client.API_VERSIONS = {
+            "2": "tests.compute.test_compute.FakeClient"
+        }
+
+        self.cm = clientmanager.ClientManager(token=self.auth_token,
+                                              url=self.auth_url,
+                                              auth_url=self.auth_url,
+                                              api_version=api_version)
+
+    def test_make_client(self):
+        test_servers = [
+            ["id 1", "name 1", "status 1", "networks 1"],
+            ["id 2", "name 2", "status 2", "networks 2"]
+        ]
+
+        self.cm.compute.servers.list.return_value = test_servers
+
+        self.assertEqual(self.cm.compute.servers.list(), test_servers)
+        self.assertEqual(self.cm.compute.client.auth_token, self.auth_token)
+        self.assertEqual(self.cm.compute.client.auth_url, self.auth_url)
diff --git a/tests/test_clientmanager_clientcache.py b/tests/test_clientmanager.py
similarity index 59%
rename from tests/test_clientmanager_clientcache.py
rename to tests/test_clientmanager.py
index 8ebf14be42..fe99c599f1 100644
--- a/tests/test_clientmanager_clientcache.py
+++ b/tests/test_clientmanager.py
@@ -14,23 +14,22 @@
 #
 
 from openstackclient.common import clientmanager
-
-
-def factory(inst):
-    return object()
+from tests import utils
 
 
 class Container(object):
+    attr = clientmanager.ClientCache(lambda x: object())
 
-    attr = clientmanager.ClientCache(factory)
-
-    def init_token(self):
-        return
+    def __init__(self):
+        pass
 
 
-def test_singleton():
-    # Verify that the ClientCache descriptor only
-    # invokes the factory one time and always
-    # returns the same value after that.
-    c = Container()
-    assert c.attr is c.attr
+class TestClientManager(utils.TestCase):
+    def setUp(self):
+        super(TestClientManager, self).setUp()
+
+    def test_singleton(self):
+        # NOTE(dtroyer): Verify that the ClientCache descriptor only invokes
+        # the factory one time and always returns the same value after that.
+        c = Container()
+        self.assertEqual(c.attr, c.attr)
diff --git a/tests/test_shell.py b/tests/test_shell.py
index cf4fc9244f..87a7795a9b 100644
--- a/tests/test_shell.py
+++ b/tests/test_shell.py
@@ -13,10 +13,10 @@
 #   under the License.
 #
 
+import fixtures
 import os
 import mock
 
-import fixtures
 from openstackclient import shell as os_shell
 from tests import utils
 
@@ -47,8 +47,7 @@ def make_shell():
     return _shell
 
 
-class ShellTest(utils.TestCase):
-
+class TestShell(utils.TestCase):
     FAKE_ENV = {
         'OS_AUTH_URL': DEFAULT_AUTH_URL,
         'OS_TENANT_ID': DEFAULT_TENANT_ID,
@@ -60,7 +59,7 @@ class ShellTest(utils.TestCase):
 
     def setUp(self):
         """ Patch os.environ to avoid required auth info"""
-        super(ShellTest, self).setUp()
+        super(TestShell, self).setUp()
         for var in self.FAKE_ENV:
             self.useFixture(
                 fixtures.EnvironmentVariable(
@@ -85,7 +84,7 @@ class ShellTest(utils.TestCase):
     def tearDown(self):
         #self.auth_patch.stop()
         self.cmd_patch.stop()
-        super(ShellTest, self).tearDown()
+        super(TestShell, self).tearDown()
 
     def test_shell_args(self):
         sh = make_shell()
diff --git a/tests/utils.py b/tests/utils.py
index 5c4b50c623..75515fad56 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -22,14 +22,13 @@ import testtools
 class TestCase(testtools.TestCase):
     def setUp(self):
         super(TestCase, self).setUp()
+
         if (os.environ.get("OS_STDOUT_NOCAPTURE") == "True" and
                 os.environ.get("OS_STDOUT_NOCAPTURE") == "1"):
             stdout = self.useFixture(fixtures.StringStream("stdout")).stream
             self.useFixture(fixtures.MonkeyPatch("sys.stdout", stdout))
+
         if (os.environ.get("OS_STDERR_NOCAPTURE") == "True" and
                 os.environ.get("OS_STDERR_NOCAPTURE") == "1"):
             stderr = self.useFixture(fixtures.StringStream("stderr")).stream
             self.useFixture(fixtures.MonkeyPatch("sys.stderr", stderr))
-
-    def tearDown(self):
-        super(TestCase, self).tearDown()