From 04abab869d28e007d9b869122740f70cd839daf7 Mon Sep 17 00:00:00 2001
From: Rushi Agrawal <rushi.agr@gmail.com>
Date: Mon, 8 Sep 2014 00:28:57 +0530
Subject: [PATCH] Mock glance client object in version unit tests

This patch changes the glance client version unit tests
to mock the Client object completely.  Previously the
tests were ensuring the right version of the client was
returned, but that required too much knowledge of glance's
implementation and ended up breaking the tests when glance
changed the implementation details of the Client class.

The new code tests if cinder is calling the integration
point correctly for the version, rather than if glance
is correctly returning the right client; that should be
a glance test, not a cinder test.

Closes-Bug: 1366596
Change-Id: I979175e6c3b5f98076bde6d36ca52fb8b03009b8
---
 cinder/tests/image/test_glance.py | 47 +++++++++----------------------
 1 file changed, 13 insertions(+), 34 deletions(-)

diff --git a/cinder/tests/image/test_glance.py b/cinder/tests/image/test_glance.py
index c2421dddd..b252ac987 100644
--- a/cinder/tests/image/test_glance.py
+++ b/cinder/tests/image/test_glance.py
@@ -17,7 +17,7 @@
 import datetime
 
 import glanceclient.exc
-from glanceclient.v2 import client as glance_client_v2
+import mock
 from oslo.config import cfg
 
 from cinder import context
@@ -588,45 +588,24 @@ class TestGlanceImageService(test.TestCase):
 
 class TestGlanceClientVersion(test.TestCase):
     """Tests the version of the glance client generated."""
-    def setUp(self):
-        super(TestGlanceClientVersion, self).setUp()
-
-        def fake_get_model(self):
-            return
-
-        self.stubs.Set(glance_client_v2.Client, '_get_image_model',
-                       fake_get_model)
-
-        try:
-            self.stubs.Set(glance_client_v2.Client, '_get_member_model',
-                           fake_get_model)
-        except AttributeError:
-            # method requires stubbing only with newer glanceclients.
-            pass
 
-    def test_glance_version_by_flag(self):
+    @mock.patch('cinder.image.glance.glanceclient.Client')
+    def test_glance_version_by_flag(self, _mockglanceclient):
         """Test glance version set by flag is honoured."""
-        client_wrapper_v1 = glance.GlanceClientWrapper('fake', 'fake_host',
-                                                       9292)
-        self.assertEqual(client_wrapper_v1.client.__module__,
-                         'glanceclient.v1.client')
+        glance.GlanceClientWrapper('fake', 'fake_host', 9292)
+        self.assertEqual('1', _mockglanceclient.call_args[0][0])
         self.flags(glance_api_version=2)
-        client_wrapper_v2 = glance.GlanceClientWrapper('fake', 'fake_host',
-                                                       9292)
-        self.assertEqual(client_wrapper_v2.client.__module__,
-                         'glanceclient.v2.client')
+        glance.GlanceClientWrapper('fake', 'fake_host', 9292)
+        self.assertEqual('2', _mockglanceclient.call_args[0][0])
         CONF.reset()
 
-    def test_glance_version_by_arg(self):
+    @mock.patch('cinder.image.glance.glanceclient.Client')
+    def test_glance_version_by_arg(self, _mockglanceclient):
         """Test glance version set by arg to GlanceClientWrapper"""
-        client_wrapper_v1 = glance.GlanceClientWrapper('fake', 'fake_host',
-                                                       9292, version=1)
-        self.assertEqual(client_wrapper_v1.client.__module__,
-                         'glanceclient.v1.client')
-        client_wrapper_v2 = glance.GlanceClientWrapper('fake', 'fake_host',
-                                                       9292, version=2)
-        self.assertEqual(client_wrapper_v2.client.__module__,
-                         'glanceclient.v2.client')
+        glance.GlanceClientWrapper('fake', 'fake_host', 9292, version=1)
+        self.assertEqual('1', _mockglanceclient.call_args[0][0])
+        glance.GlanceClientWrapper('fake', 'fake_host', 9292, version=2)
+        self.assertEqual('2', _mockglanceclient.call_args[0][0])
 
 
 def _create_failing_glance_client(info):
-- 
2.45.2