Skip to content

Commit 4297e57

Browse files
jk0Dean Troyer
authored andcommitted
First pass at adding compute unit tests.
Change-Id: Icf3340d457f75eec89bb0e5c9b4b953c3b81020f
1 parent 1bb59c5 commit 4297e57

5 files changed

Lines changed: 91 additions & 21 deletions

File tree

tests/compute/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Copyright 2013 OpenStack, LLC.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
# not use this file except in compliance with the License. You may obtain
5+
# a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.
14+
#

tests/compute/test_compute.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Copyright 2013 OpenStack, LLC.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
# not use this file except in compliance with the License. You may obtain
5+
# a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.
14+
#
15+
16+
import mock
17+
18+
from openstackclient.common import clientmanager
19+
from openstackclient.compute import client as compute_client
20+
from tests import utils
21+
22+
23+
class FakeClient(object):
24+
def __init__(self, endpoint=None, **kwargs):
25+
self.client = mock.MagicMock()
26+
self.servers = mock.MagicMock()
27+
28+
self.client.auth_url = kwargs['auth_url']
29+
30+
31+
class TestCompute(utils.TestCase):
32+
def setUp(self):
33+
super(TestCompute, self).setUp()
34+
35+
self.auth_token = "foobar"
36+
self.auth_url = "http://0.0.0.0"
37+
38+
api_version = {"compute": "2"}
39+
40+
compute_client.API_VERSIONS = {
41+
"2": "tests.compute.test_compute.FakeClient"
42+
}
43+
44+
self.cm = clientmanager.ClientManager(token=self.auth_token,
45+
url=self.auth_url,
46+
auth_url=self.auth_url,
47+
api_version=api_version)
48+
49+
def test_make_client(self):
50+
test_servers = [
51+
["id 1", "name 1", "status 1", "networks 1"],
52+
["id 2", "name 2", "status 2", "networks 2"]
53+
]
54+
55+
self.cm.compute.servers.list.return_value = test_servers
56+
57+
self.assertEqual(self.cm.compute.servers.list(), test_servers)
58+
self.assertEqual(self.cm.compute.client.auth_token, self.auth_token)
59+
self.assertEqual(self.cm.compute.client.auth_url, self.auth_url)
Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,22 @@
1414
#
1515

1616
from openstackclient.common import clientmanager
17-
18-
19-
def factory(inst):
20-
return object()
17+
from tests import utils
2118

2219

2320
class Container(object):
21+
attr = clientmanager.ClientCache(lambda x: object())
2422

25-
attr = clientmanager.ClientCache(factory)
23+
def __init__(self):
24+
pass
2625

27-
def init_token(self):
28-
return
2926

27+
class TestClientManager(utils.TestCase):
28+
def setUp(self):
29+
super(TestClientManager, self).setUp()
3030

31-
def test_singleton():
32-
# Verify that the ClientCache descriptor only
33-
# invokes the factory one time and always
34-
# returns the same value after that.
35-
c = Container()
36-
assert c.attr is c.attr
31+
def test_singleton(self):
32+
# NOTE(dtroyer): Verify that the ClientCache descriptor only invokes
33+
# the factory one time and always returns the same value after that.
34+
c = Container()
35+
self.assertEqual(c.attr, c.attr)

tests/test_shell.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
# under the License.
1414
#
1515

16+
import fixtures
1617
import os
1718
import mock
1819

19-
import fixtures
2020
from openstackclient import shell as os_shell
2121
from tests import utils
2222

@@ -47,8 +47,7 @@ def make_shell():
4747
return _shell
4848

4949

50-
class ShellTest(utils.TestCase):
51-
50+
class TestShell(utils.TestCase):
5251
FAKE_ENV = {
5352
'OS_AUTH_URL': DEFAULT_AUTH_URL,
5453
'OS_TENANT_ID': DEFAULT_TENANT_ID,
@@ -60,7 +59,7 @@ class ShellTest(utils.TestCase):
6059

6160
def setUp(self):
6261
""" Patch os.environ to avoid required auth info"""
63-
super(ShellTest, self).setUp()
62+
super(TestShell, self).setUp()
6463
for var in self.FAKE_ENV:
6564
self.useFixture(
6665
fixtures.EnvironmentVariable(
@@ -85,7 +84,7 @@ def setUp(self):
8584
def tearDown(self):
8685
#self.auth_patch.stop()
8786
self.cmd_patch.stop()
88-
super(ShellTest, self).tearDown()
87+
super(TestShell, self).tearDown()
8988

9089
def test_shell_args(self):
9190
sh = make_shell()

tests/utils.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,13 @@
2222
class TestCase(testtools.TestCase):
2323
def setUp(self):
2424
super(TestCase, self).setUp()
25+
2526
if (os.environ.get("OS_STDOUT_NOCAPTURE") == "True" and
2627
os.environ.get("OS_STDOUT_NOCAPTURE") == "1"):
2728
stdout = self.useFixture(fixtures.StringStream("stdout")).stream
2829
self.useFixture(fixtures.MonkeyPatch("sys.stdout", stdout))
30+
2931
if (os.environ.get("OS_STDERR_NOCAPTURE") == "True" and
3032
os.environ.get("OS_STDERR_NOCAPTURE") == "1"):
3133
stderr = self.useFixture(fixtures.StringStream("stderr")).stream
3234
self.useFixture(fixtures.MonkeyPatch("sys.stderr", stderr))
33-
34-
def tearDown(self):
35-
super(TestCase, self).tearDown()

0 commit comments

Comments
 (0)