Fixed timeout logic when attaching/detaching volumes.
[snaps.git] / snaps / openstack / utils / tests / nova_utils_tests.py
index 77dc5dd..9383088 100644 (file)
@@ -33,6 +33,7 @@ from snaps.openstack.tests import openstack_tests
 from snaps.openstack.tests.os_source_file_test import OSComponentTestCase
 from snaps.openstack.utils import (
     nova_utils, neutron_utils, glance_utils, cinder_utils)
+from snaps.openstack.utils.nova_utils import NovaException
 
 __author__ = 'spisarski'
 
@@ -440,7 +441,8 @@ class NovaUtilsInstanceVolumeTests(OSComponentTestCase):
 
     def test_add_remove_volume(self):
         """
-        Tests the nova_utils.create_server() method
+        Tests the nova_utils.attach_volume() and detach_volume functions with
+        a timeout value
         :return:
         """
 
@@ -449,23 +451,27 @@ class NovaUtilsInstanceVolumeTests(OSComponentTestCase):
 
         # Attach volume to VM
         neutron = neutron_utils.neutron_client(self.os_creds)
-        nova_utils.attach_volume(
+        self.assertIsNotNone(nova_utils.attach_volume(
             self.nova, neutron, self.instance_creator.get_vm_inst(),
-            self.volume_creator.get_volume(), 120)
+            self.volume_creator.get_volume()))
 
-        vol_attach = cinder_utils.get_volume_by_id(
-            self.cinder, self.volume_creator.get_volume().id)
-        vm_attach = nova_utils.get_server_object_by_id(
-            self.nova, neutron, self.instance_creator.get_vm_inst().id)
+        vol_attach = None
+        attached = False
+        start_time = time.time()
+        while time.time() < start_time + 30:
+            vol_attach = cinder_utils.get_volume_by_id(
+                self.cinder, self.volume_creator.get_volume().id)
 
-        # Detach volume to VM
-        nova_utils.detach_volume(
-            self.nova, neutron, self.instance_creator.get_vm_inst(),
-            self.volume_creator.get_volume(), 120)
+            if len(vol_attach.attachments) > 0:
+                attached = True
+                break
 
-        vol_detach = cinder_utils.get_volume_by_id(
-            self.cinder, self.volume_creator.get_volume().id)
-        vm_detach = nova_utils.get_server_object_by_id(
+            time.sleep(3)
+
+        self.assertTrue(attached)
+        self.assertIsNotNone(vol_attach)
+
+        vm_attach = nova_utils.get_server_object_by_id(
             self.nova, neutron, self.instance_creator.get_vm_inst().id)
 
         # Validate Attachment
@@ -475,8 +481,84 @@ class NovaUtilsInstanceVolumeTests(OSComponentTestCase):
         self.assertEqual(vm_attach.volume_ids[0]['id'],
                          vol_attach.attachments[0]['volume_id'])
 
+        # Detach volume to VM
+        self.assertIsNotNone(nova_utils.detach_volume(
+            self.nova, neutron, self.instance_creator.get_vm_inst(),
+            self.volume_creator.get_volume()))
+
+        vol_detach = cinder_utils.get_volume_by_id(
+            self.cinder, self.volume_creator.get_volume().id)
+        vm_detach = nova_utils.get_server_object_by_id(
+            self.nova, neutron, self.instance_creator.get_vm_inst().id)
+
         # Validate Detachment
         self.assertIsNotNone(vol_detach)
         self.assertEqual(self.volume_creator.get_volume().id, vol_detach.id)
+
+        if len(vol_detach.attachments) > 0:
+            vol_detach = cinder_utils.get_volume_by_id(
+                self.cinder, self.volume_creator.get_volume().id)
+
         self.assertEqual(0, len(vol_detach.attachments))
         self.assertEqual(0, len(vm_detach.volume_ids))
+
+    def test_attach_volume_nowait(self):
+        """
+        Tests the nova_utils.attach_volume() with a timeout value that is too
+        small to have the volume attachment data to be included on the VmInst
+        object that was supposed to be returned
+        """
+
+        self.assertIsNotNone(self.volume_creator.get_volume())
+        self.assertEqual(0, len(self.volume_creator.get_volume().attachments))
+
+        # Attach volume to VM
+        neutron = neutron_utils.neutron_client(self.os_creds)
+        with self.assertRaises(NovaException):
+            nova_utils.attach_volume(
+                self.nova, neutron, self.instance_creator.get_vm_inst(),
+                self.volume_creator.get_volume(), 0)
+
+    def test_detach_volume_nowait(self):
+        """
+        Tests the nova_utils.detach_volume() with a timeout value that is too
+        small to have the volume attachment data to be included on the VmInst
+        object that was supposed to be returned
+        """
+
+        self.assertIsNotNone(self.volume_creator.get_volume())
+        self.assertEqual(0, len(self.volume_creator.get_volume().attachments))
+
+        # Attach volume to VM
+        neutron = neutron_utils.neutron_client(self.os_creds)
+        nova_utils.attach_volume(
+            self.nova, neutron, self.instance_creator.get_vm_inst(),
+            self.volume_creator.get_volume())
+
+        # Check VmInst for attachment
+        latest_vm = nova_utils.get_server_object_by_id(
+            self.nova, neutron, self.instance_creator.get_vm_inst().id)
+        self.assertEqual(1, len(latest_vm.volume_ids))
+
+        # Check Volume for attachment
+        vol_attach = None
+        attached = False
+        start_time = time.time()
+        while time.time() < start_time + 30:
+            vol_attach = cinder_utils.get_volume_by_id(
+                self.cinder, self.volume_creator.get_volume().id)
+
+            if len(vol_attach.attachments) > 0:
+                attached = True
+                break
+
+            time.sleep(3)
+
+        self.assertTrue(attached)
+        self.assertIsNotNone(vol_attach)
+
+        # Detach volume
+        with self.assertRaises(NovaException):
+            nova_utils.detach_volume(
+                self.nova, neutron, self.instance_creator.get_vm_inst(),
+                self.volume_creator.get_volume(), 0)