def test_06_download_detached_volume(self): """Download a Volume unattached to an VM """ # Validate the following # 1. able to download the volume when its not attached to instance self.debug("Extract detached Volume ID: %s" % self.volume.id) self.virtual_machine.attach_volume(self.apiClient, self.volume) self.virtual_machine.detach_volume(self.apiClient, self.volume) self.attached = False cmd = extractVolume.extractVolumeCmd() cmd.id = self.volume.id cmd.mode = "HTTP_DOWNLOAD" cmd.zoneid = self.services["zoneid"] extract_vol = self.apiClient.extractVolume(cmd) #Attempt to download the volume and save contents locally try: formatted_url = urllib.unquote_plus(extract_vol.url) self.debug("Attempting to download volume at url %s" % formatted_url) response = urllib.urlopen(formatted_url) self.debug("response from volume url %s" % response.getcode()) fd, path = tempfile.mkstemp() self.debug("Saving volume %s to path %s" %(self.volume.id, path)) os.close(fd) with open(path, 'wb') as fd: fd.write(response.read()) self.debug("Saved volume successfully") except Exception: self.fail( "Extract Volume Failed with invalid URL %s (vol id: %s)" \ % (extract_vol.url, self.volume.id) )
def test_03_download_attached_volume(self): """Download a Volume attached to a VM """ # Validate the following # 1. download volume will fail with proper error message # "Failed - Invalid state of the volume with ID: # It should be either detached or the VM should be in stopped state self.debug("Extract attached Volume ID: %s" % self.volume.id) self.virtual_machine.attach_volume(self.apiClient, self.volume) self.attached = True cmd = extractVolume.extractVolumeCmd() cmd.id = self.volume.id cmd.mode = "HTTP_DOWNLOAD" cmd.zoneid = self.services["zoneid"] # A proper exception should be raised; # downloading attach VM is not allowed with self.assertRaises(Exception): self.apiClient.extractVolume(cmd)
def test_06_download_detached_volume(self): """Download a Volume unattached to an VM Validate the following 1. able to download the volume when its not attached to instance """ self.debug("Extract detached Volume ID: %s" % self.volume.id) self.virtual_machine.attach_volume(self.apiClient, self.volume) # Sleep to ensure the current state will reflected in other calls time.sleep(self.services["sleep"]) self.virtual_machine.detach_volume(self.apiClient, self.volume) self.attached = False # Sleep to ensure the current state will reflected in other calls time.sleep(self.services["sleep"]) cmd = extractVolume.extractVolumeCmd() cmd.id = self.volume.id cmd.mode = "HTTP_DOWNLOAD" cmd.zoneid = self.services["zoneid"] extract_vol = self.apiClient.extractVolume(cmd) # Attempt to download the volume and save contents locally try: formatted_url = urllib.parse.unquote_plus(extract_vol.url) self.debug("Attempting to download volume at url %s" % formatted_url) response = urllib.request.urlopen(formatted_url) self.debug("response from volume url %s" % response.getcode()) fd, path = tempfile.mkstemp() self.debug("Saving volume %s to path %s" % (self.volume.id, path)) os.close(fd) with open(path, 'wb') as fd: fd.write(response.read()) self.debug("Saved volume successfully") except Exception as e: self.fail( "Extract Volume Failed (URL: %s , vol id: %s) due to %s" % (extract_vol.url, self.volume.id, e))
def test_05_extract_root_volume_and_destroy_vm(self): """Create VM, extract root volume, then destroy vm and volume Steps: # 1. create vm without data disk, resource count increases. # 2. stop vm # 3. extract root volume # 4. expunge vm, root volume in Expunged state. resource count decreased with size of root disk. # 5. destroy volume (expunge = false), Exception happened. resource count no changes # 6. destroy volume (expunge = true). volume is not found. resource count no changes. """ # Create vm try: virtual_machine_4 = VirtualMachine.create( self.apiclient, self.services["virtual_machine"], accountid=self.account.name, domainid=self.account.domainid, serviceofferingid=self.service_offering.id, templateid=self.template.id, zoneid=self.zone.id) self.cleanup.append(virtual_machine_4) except Exception as e: self.fail("Exception while deploying virtual machine: %s" % e) self.expectedCount = self.expectedCount + self.templatesize self.volumeTotal = self.volumeTotal + 1 self.verify_resource_count_primary_storage(self.expectedCount, self.volumeTotal) # Get id of root disk root_volumes_list = Volume.list(self.apiclient, virtualmachineid=virtual_machine_4.id, type='ROOT', listall=True) status = validateList(root_volumes_list) self.assertEqual(status[0], PASS, "ROOT Volume List Validation Failed") root_volume_id = root_volumes_list[0].id # Stop vm virtual_machine_4.stop(self.apiclient) # extract root volume cmd = extractVolume.extractVolumeCmd() cmd.id = root_volume_id cmd.mode = "HTTP_DOWNLOAD" cmd.zoneid = self.zone.id self.apiclient.extractVolume(cmd) # Destroy VM (expunge=True) virtual_machine_4.delete(self.apiclient, expunge=True) self.cleanup.remove(virtual_machine_4) self.expectedCount = self.expectedCount - self.templatesize self.volumeTotal = self.volumeTotal - 1 self.verify_resource_count_primary_storage(self.expectedCount, self.volumeTotal) # check root disk state root_volumes_list = Volume.list(self.apiclient, id=root_volume_id, listall=True) status = validateList(root_volumes_list) self.assertEqual(status[0], PASS, "ROOT Volume List Validation Failed") root_volume = root_volumes_list[0] self.assertEqual(root_volume['state'], 'Expunged', "ROOT volume should be Destroy after restorevm") # delete root disk cmd = deleteVolume.deleteVolumeCmd() cmd.id = root_volume.id self.apiclient.deleteVolume(cmd) self.verify_resource_count_primary_storage(self.expectedCount, self.volumeTotal)