1
From 04abab869d28e007d9b869122740f70cd839daf7 Mon Sep 17 00:00:00 2001
2
From: Rushi Agrawal <rushi.agr@gmail.com>
3
Date: Mon, 8 Sep 2014 00:28:57 +0530
4
Subject: [PATCH] Mock glance client object in version unit tests
6
This patch changes the glance client version unit tests
7
to mock the Client object completely. Previously the
8
tests were ensuring the right version of the client was
9
returned, but that required too much knowledge of glance's
10
implementation and ended up breaking the tests when glance
11
changed the implementation details of the Client class.
13
The new code tests if cinder is calling the integration
14
point correctly for the version, rather than if glance
15
is correctly returning the right client; that should be
16
a glance test, not a cinder test.
19
Change-Id: I979175e6c3b5f98076bde6d36ca52fb8b03009b8
21
cinder/tests/image/test_glance.py | 47 +++++++++++----------------------------
22
1 file changed, 13 insertions(+), 34 deletions(-)
24
diff --git a/cinder/tests/image/test_glance.py b/cinder/tests/image/test_glance.py
25
index c2421dd..b252ac9 100644
26
--- a/cinder/tests/image/test_glance.py
27
+++ b/cinder/tests/image/test_glance.py
31
import glanceclient.exc
32
-from glanceclient.v2 import client as glance_client_v2
34
from oslo.config import cfg
36
from cinder import context
37
@@ -588,45 +588,24 @@ def __init__(self, metadata):
39
class TestGlanceClientVersion(test.TestCase):
40
"""Tests the version of the glance client generated."""
42
- super(TestGlanceClientVersion, self).setUp()
44
- def fake_get_model(self):
47
- self.stubs.Set(glance_client_v2.Client, '_get_image_model',
51
- self.stubs.Set(glance_client_v2.Client, '_get_member_model',
53
- except AttributeError:
54
- # method requires stubbing only with newer glanceclients.
57
- def test_glance_version_by_flag(self):
58
+ @mock.patch('cinder.image.glance.glanceclient.Client')
59
+ def test_glance_version_by_flag(self, _mockglanceclient):
60
"""Test glance version set by flag is honoured."""
61
- client_wrapper_v1 = glance.GlanceClientWrapper('fake', 'fake_host',
63
- self.assertEqual(client_wrapper_v1.client.__module__,
64
- 'glanceclient.v1.client')
65
+ glance.GlanceClientWrapper('fake', 'fake_host', 9292)
66
+ self.assertEqual('1', _mockglanceclient.call_args[0][0])
67
self.flags(glance_api_version=2)
68
- client_wrapper_v2 = glance.GlanceClientWrapper('fake', 'fake_host',
70
- self.assertEqual(client_wrapper_v2.client.__module__,
71
- 'glanceclient.v2.client')
72
+ glance.GlanceClientWrapper('fake', 'fake_host', 9292)
73
+ self.assertEqual('2', _mockglanceclient.call_args[0][0])
76
- def test_glance_version_by_arg(self):
77
+ @mock.patch('cinder.image.glance.glanceclient.Client')
78
+ def test_glance_version_by_arg(self, _mockglanceclient):
79
"""Test glance version set by arg to GlanceClientWrapper"""
80
- client_wrapper_v1 = glance.GlanceClientWrapper('fake', 'fake_host',
82
- self.assertEqual(client_wrapper_v1.client.__module__,
83
- 'glanceclient.v1.client')
84
- client_wrapper_v2 = glance.GlanceClientWrapper('fake', 'fake_host',
86
- self.assertEqual(client_wrapper_v2.client.__module__,
87
- 'glanceclient.v2.client')
88
+ glance.GlanceClientWrapper('fake', 'fake_host', 9292, version=1)
89
+ self.assertEqual('1', _mockglanceclient.call_args[0][0])
90
+ glance.GlanceClientWrapper('fake', 'fake_host', 9292, version=2)
91
+ self.assertEqual('2', _mockglanceclient.call_args[0][0])
94
def _create_failing_glance_client(info):