]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
VMware: Refactor initialize_connection unit tests
authorVipin Balachandran <vbala@vmware.com>
Wed, 11 Feb 2015 12:46:16 +0000 (18:16 +0530)
committerVipin Balachandran <vbala@vmware.com>
Mon, 23 Feb 2015 14:10:33 +0000 (19:40 +0530)
This patch refactors initialize_connection unit tests
to use mock.

Partial-Bug: #1261097
Change-Id: I9a1a50526d038c0a9c17d0015f00b4bf27d68131

cinder/tests/test_vmware_vmdk.py

index 278ce412e22a86f7894694b37f2852ba41a76a13..18db984db1a4ad20adbf4fceaa5d1d9f9851bac0 100644 (file)
@@ -305,31 +305,6 @@ class VMwareEsxVmdkDriverTestCase(test.TestCase):
         m.UnsetStubs()
         m.VerifyAll()
 
-    def test_init_conn_with_instance_and_backing(self):
-        """Test initialize_connection with instance and backing."""
-        m = self.mox
-        m.StubOutWithMock(self._driver.__class__, 'volumeops')
-        self._driver.volumeops = self._volumeops
-        m.StubOutWithMock(self._volumeops, 'get_backing')
-        volume = FakeObject()
-        volume['name'] = 'volume_name'
-        volume['id'] = 'volume_id'
-        volume['size'] = 1
-        connector = {'instance': 'my_instance'}
-        backing = FakeMor('VirtualMachine', 'my_back')
-        self._volumeops.get_backing(volume['name']).AndReturn(backing)
-        m.StubOutWithMock(self._volumeops, 'get_host')
-        host = FakeMor('HostSystem', 'my_host')
-        self._volumeops.get_host(mox.IgnoreArg()).AndReturn(host)
-
-        m.ReplayAll()
-        conn_info = self._driver.initialize_connection(volume, connector)
-        self.assertEqual(conn_info['driver_volume_type'], 'vmdk')
-        self.assertEqual(conn_info['data']['volume'], 'my_back')
-        self.assertEqual(conn_info['data']['volume_id'], 'volume_id')
-        m.UnsetStubs()
-        m.VerifyAll()
-
     def test_get_volume_group_folder(self):
         """Test _get_volume_group_folder."""
         m = self.mox
@@ -459,71 +434,6 @@ class VMwareEsxVmdkDriverTestCase(test.TestCase):
                           vmdk.VMwareEsxVmdkDriver._get_disk_type,
                           volume)
 
-    def test_init_conn_with_instance_no_backing(self):
-        """Test initialize_connection with instance and without backing."""
-        m = self.mox
-        m.StubOutWithMock(self._driver.__class__, 'volumeops')
-        self._driver.volumeops = self._volumeops
-        m.StubOutWithMock(self._volumeops, 'get_backing')
-        volume = FakeObject()
-        volume['name'] = 'volume_name'
-        volume['id'] = 'volume_id'
-        volume['size'] = 1
-        volume['volume_type_id'] = None
-        connector = {'instance': 'my_instance'}
-        self._volumeops.get_backing(volume['name'])
-        m.StubOutWithMock(self._volumeops, 'get_host')
-        host = FakeMor('HostSystem', 'my_host')
-        self._volumeops.get_host(mox.IgnoreArg()).AndReturn(host)
-        m.StubOutWithMock(self._volumeops, 'get_dss_rp')
-        resource_pool = FakeMor('ResourcePool', 'my_rp')
-        datastores = [FakeMor('Datastore', 'my_ds')]
-        self._volumeops.get_dss_rp(host).AndReturn((datastores, resource_pool))
-        m.StubOutWithMock(self._driver, '_get_folder_ds_summary')
-        folder = FakeMor('Folder', 'my_fol')
-        summary = FakeDatastoreSummary(1, 1)
-        self._driver._get_folder_ds_summary(volume, resource_pool,
-                                            datastores).AndReturn((folder,
-                                                                   summary))
-        backing = FakeMor('VirtualMachine', 'my_back')
-        m.StubOutWithMock(self._volumeops, 'create_backing')
-        self._volumeops.create_backing(volume['name'],
-                                       volume['size'] * units.Mi,
-                                       mox.IgnoreArg(), folder,
-                                       resource_pool, host,
-                                       mox.IgnoreArg(),
-                                       mox.IgnoreArg(),
-                                       mox.IgnoreArg()).AndReturn(backing)
-
-        m.ReplayAll()
-        conn_info = self._driver.initialize_connection(volume, connector)
-        self.assertEqual(conn_info['driver_volume_type'], 'vmdk')
-        self.assertEqual(conn_info['data']['volume'], 'my_back')
-        self.assertEqual(conn_info['data']['volume_id'], 'volume_id')
-        m.UnsetStubs()
-        m.VerifyAll()
-
-    def test_init_conn_without_instance(self):
-        """Test initialize_connection without instance and a backing."""
-        m = self.mox
-        m.StubOutWithMock(self._driver.__class__, 'volumeops')
-        self._driver.volumeops = self._volumeops
-        m.StubOutWithMock(self._volumeops, 'get_backing')
-        backing = FakeMor('VirtualMachine', 'my_back')
-        volume = FakeObject()
-        volume['name'] = 'volume_name'
-        volume['id'] = 'volume_id'
-        connector = {}
-        self._volumeops.get_backing(volume['name']).AndReturn(backing)
-
-        m.ReplayAll()
-        conn_info = self._driver.initialize_connection(volume, connector)
-        self.assertEqual(conn_info['driver_volume_type'], 'vmdk')
-        self.assertEqual(conn_info['data']['volume'], 'my_back')
-        self.assertEqual(conn_info['data']['volume_id'], 'volume_id')
-        m.UnsetStubs()
-        m.VerifyAll()
-
     def test_create_snapshot_without_backing(self):
         """Test vmdk.create_snapshot without backing."""
         m = self.mox
@@ -1921,37 +1831,83 @@ class VMwareVcVmdkDriverTestCase(VMwareEsxVmdkDriverTestCase):
         self._test_create_backing_by_copying(volumeops, create_backing,
                                              extend_virtual_disk)
 
-    def test_init_conn_with_instance_and_backing(self):
-        """Test initialize_connection with instance and backing."""
-        m = self.mox
-        m.StubOutWithMock(self._driver.__class__, 'volumeops')
-        self._driver.volumeops = self._volumeops
-        m.StubOutWithMock(self._volumeops, 'get_backing')
-        volume = FakeObject()
-        volume['name'] = 'volume_name'
-        volume['id'] = 'volume_id'
-        volume['size'] = 1
-        connector = {'instance': 'my_instance'}
-        backing = FakeMor('VirtualMachine', 'my_back')
-        self._volumeops.get_backing(volume['name']).AndReturn(backing)
-        m.StubOutWithMock(self._volumeops, 'get_host')
-        host = FakeMor('HostSystem', 'my_host')
-        self._volumeops.get_host(mox.IgnoreArg()).AndReturn(host)
-        datastore = FakeMor('Datastore', 'my_ds')
-        resource_pool = FakeMor('ResourcePool', 'my_rp')
-        m.StubOutWithMock(self._volumeops, 'get_dss_rp')
-        self._volumeops.get_dss_rp(host).AndReturn(([datastore],
-                                                    resource_pool))
-        m.StubOutWithMock(self._volumeops, 'get_datastore')
-        self._volumeops.get_datastore(backing).AndReturn(datastore)
+    @mock.patch.object(VMDK_DRIVER, 'volumeops')
+    @mock.patch.object(VMDK_DRIVER, '_relocate_backing')
+    def test_initialize_connection_with_instance_and_backing(
+            self, relocate_backing, vops):
 
-        m.ReplayAll()
+        instance = mock.sentinel.instance
+        connector = {'instance': instance}
+
+        backing = mock.Mock(value=mock.sentinel.backing_value)
+        vops.get_backing.return_value = backing
+
+        host = mock.sentinel.host
+        vops.get_host.return_value = host
+
+        volume = {'name': 'vol-1', 'id': 1}
         conn_info = self._driver.initialize_connection(volume, connector)
-        self.assertEqual(conn_info['driver_volume_type'], 'vmdk')
-        self.assertEqual(conn_info['data']['volume'], 'my_back')
-        self.assertEqual(conn_info['data']['volume_id'], 'volume_id')
-        m.UnsetStubs()
-        m.VerifyAll()
+
+        relocate_backing.assert_called_once_with(volume, backing, host)
+
+        self.assertEqual('vmdk', conn_info['driver_volume_type'])
+        self.assertEqual(backing.value, conn_info['data']['volume'])
+        self.assertEqual(volume['id'],
+                         conn_info['data']['volume_id'])
+
+    @mock.patch.object(VMDK_DRIVER, 'volumeops')
+    @mock.patch.object(VMDK_DRIVER, '_relocate_backing')
+    @mock.patch.object(VMDK_DRIVER, '_create_backing')
+    def test_initialize_connection_with_instance_and_no_backing(
+            self, create_backing, relocate_backing, vops):
+
+        instance = mock.sentinel.instance
+        connector = {'instance': instance}
+
+        vops.get_backing.return_value = None
+
+        host = mock.sentinel.host
+        vops.get_host.return_value = host
+
+        backing = mock.Mock(value=mock.sentinel.backing_value)
+        create_backing.return_value = backing
+
+        volume = {'name': 'vol-1', 'id': 1}
+        conn_info = self._driver.initialize_connection(volume, connector)
+
+        create_backing.assert_called_once_with(volume, host)
+        self.assertFalse(relocate_backing.called)
+
+        self.assertEqual('vmdk', conn_info['driver_volume_type'])
+        self.assertEqual(backing.value, conn_info['data']['volume'])
+        self.assertEqual(volume['id'],
+                         conn_info['data']['volume_id'])
+
+    @mock.patch.object(VMDK_DRIVER, 'volumeops')
+    @mock.patch.object(VMDK_DRIVER, '_relocate_backing')
+    @mock.patch.object(VMDK_DRIVER, '_create_backing_in_inventory')
+    def test_initialize_connection_with_no_instance_and_no_backing(
+            self, create_backing_in_inv, relocate_backing, vops):
+
+        vops.get_backing.return_value = None
+
+        host = mock.sentinel.host
+        vops.get_host.return_value = host
+
+        backing = mock.Mock(value=mock.sentinel.backing_value)
+        create_backing_in_inv.return_value = backing
+
+        connector = {}
+        volume = {'name': 'vol-1', 'id': 1}
+        conn_info = self._driver.initialize_connection(volume, connector)
+
+        create_backing_in_inv.assert_called_once_with(volume)
+        self.assertFalse(relocate_backing.called)
+
+        self.assertEqual('vmdk', conn_info['driver_volume_type'])
+        self.assertEqual(backing.value, conn_info['data']['volume'])
+        self.assertEqual(volume['id'],
+                         conn_info['data']['volume_id'])
 
     def test_get_volume_group_folder(self):
         """Test _get_volume_group_folder."""
@@ -1970,50 +1926,6 @@ class VMwareVcVmdkDriverTestCase(VMwareEsxVmdkDriverTestCase):
         m.UnsetStubs()
         m.VerifyAll()
 
-    def test_init_conn_with_instance_and_backing_and_relocation(self):
-        """Test initialize_connection with backing being relocated."""
-        m = self.mox
-        m.StubOutWithMock(self._driver.__class__, 'volumeops')
-        self._driver.volumeops = self._volumeops
-        m.StubOutWithMock(self._volumeops, 'get_backing')
-        volume = FakeObject()
-        volume['name'] = 'volume_name'
-        volume['id'] = 'volume_id'
-        volume['size'] = 1
-        connector = {'instance': 'my_instance'}
-        backing = FakeMor('VirtualMachine', 'my_back')
-        self._volumeops.get_backing(volume['name']).AndReturn(backing)
-        m.StubOutWithMock(self._volumeops, 'get_host')
-        host = FakeMor('HostSystem', 'my_host')
-        self._volumeops.get_host(mox.IgnoreArg()).AndReturn(host)
-        datastore1 = FakeMor('Datastore', 'my_ds_1')
-        datastore2 = FakeMor('Datastore', 'my_ds_2')
-        resource_pool = FakeMor('ResourcePool', 'my_rp')
-        m.StubOutWithMock(self._volumeops, 'get_dss_rp')
-        self._volumeops.get_dss_rp(host).AndReturn(([datastore1],
-                                                    resource_pool))
-        m.StubOutWithMock(self._volumeops, 'get_datastore')
-        self._volumeops.get_datastore(backing).AndReturn(datastore2)
-        m.StubOutWithMock(self._driver, '_get_folder_ds_summary')
-        folder = FakeMor('Folder', 'my_fol')
-        summary = FakeDatastoreSummary(1, 1, datastore1)
-        self._driver._get_folder_ds_summary(volume, resource_pool,
-                                            [datastore1]).AndReturn((folder,
-                                                                     summary))
-        m.StubOutWithMock(self._volumeops, 'relocate_backing')
-        self._volumeops.relocate_backing(backing, datastore1,
-                                         resource_pool, host)
-        m.StubOutWithMock(self._volumeops, 'move_backing_to_folder')
-        self._volumeops.move_backing_to_folder(backing, folder)
-
-        m.ReplayAll()
-        conn_info = self._driver.initialize_connection(volume, connector)
-        self.assertEqual(conn_info['driver_volume_type'], 'vmdk')
-        self.assertEqual(conn_info['data']['volume'], 'my_back')
-        self.assertEqual(conn_info['data']['volume_id'], 'volume_id')
-        m.UnsetStubs()
-        m.VerifyAll()
-
     @mock.patch.object(VMDK_DRIVER, '_extend_vmdk_virtual_disk')
     @mock.patch.object(VMDK_DRIVER, 'volumeops')
     def test_clone_backing_linked(self, volume_ops, _extend_vmdk_virtual_disk):