def _volume_delete(self, uuid): pdisk = PersistentDisk(self.slConfigHolder) wait_time = 30 time_stop = time.time() + wait_time while 0 != int(pdisk.getValue('count', uuid)): if time.time() >= time_stop: self._print_detail( 'Disk %s is still in use after waiting for %s sec.' % (uuid, wait_time)) time.sleep(3) return pdisk.deleteVolume(uuid)
def testCleanQuarantine(self): now = datetime.now() past = str(now - timedelta(days=1)) way_past = str(now - timedelta(days=3)) mock = Mock( return_value=[ {"quarantine": past, "uuid": "past"}, {"quarantine": way_past, "uuid": "way_past"}, {"quarantine": str(now), "uuid": "now"}, ] ) config = ConfigHolder() config.set("endpoint", "something") pd = PersistentDisk(config) pd.quarantinePeriod = "2d" pd.describeVolumes = mock pd.deleteVolume = Mock() pd._setPDiskUserCredentials = Mock() pd.cleanQuarantine() self.assertEqual(("way_past",), pd.deleteVolume.call_args[0]) self.assertEqual(1, pd.deleteVolume.call_count)
def _get_volatile_disk_ids_to_delete(self, vm_info): """Return list of volatile disks uuids for the provided VM. vm_info : <ElementTree> object """ # Collect disk sources starting with 'pdisk'. sources = [] for disk in vm_info.findall('TEMPLATE/DISK'): if disk.find('SOURCE') is not None: disk_source = disk.find('SOURCE').text if disk_source.startswith('pdisk'): sources.append(disk_source) if not sources: return [] pdisk = PersistentDisk(self.slConfigHolder.copy()) # Collect uuids of the disks sources only if they have special (volatile) tag in PDisk. # TOOD: This can be parallelized. uuids = [] for source in sources: source = source.replace('/', ':') uuid = source.split(':')[-1] tag = pdisk.getValue('tag', uuid) if tag.startswith(self.VOLATILE_DISK_PREFIX): uuids.append(uuid) return uuids
def testGetMountVmIds(self): pd = PersistentDisk() pd.endpoint = "https://example.com:8445/pwd" pd._raiseOnErrors = pd._setPDiskUserCredentials = pd._initPDiskConnection = Mock() pd._getJson = Mock(return_value=("", '[{"vmId":"1234", "device":"static"}]')) assert ["1234"] == pd.getMountVmIds("foo") pd._getJson.assert_called_once_with(*("https://example.com:8445/pwd/disks/foo/mounts",), **{}) pd._getJson = Mock( return_value=("", '[{"vmId":"1234", "device":"static"}, {"vmId":"4567", "device":"static"}]') ) assert ["1234", "4567"] == pd.getMountVmIds("bar") pd._getJson.assert_called_with(*("https://example.com:8445/pwd/disks/bar/mounts",), **{})
def _volume_exists(self, uuid): pd = PersistentDisk(self.slConfigHolder) return pd.volumeExists(uuid)
def _volume_create(self, size, tag): pdisk = PersistentDisk(self.slConfigHolder) public = False return pdisk.createVolume(str(size), tag, public)
def testParseQuarantinePeriod(self): config = ConfigHolder() config.set("endpoint", "something") pd = PersistentDisk(config) pd.quarantinePeriod = None self.assertRaises(ValidationException, pd._getQuarantinePeriod) pd.quarantinePeriod = "15x" self.assertRaises(ValidationException, pd._getQuarantinePeriod) pd.quarantinePeriod = "xym" self.assertRaises(ValidationException, pd._getQuarantinePeriod) pd.quarantinePeriod = "15" self.assertEqual(pd._getQuarantinePeriod(), 15) pd.quarantinePeriod = "15m" self.assertEqual(pd._getQuarantinePeriod(), 15) pd.quarantinePeriod = "15h" self.assertEqual(pd._getQuarantinePeriod(), 15 * 60) pd.quarantinePeriod = "15d" self.assertEqual(pd._getQuarantinePeriod(), 15 * 60 * 24)