def test_timestamp_cleanup(self): def fake_get_timestamp(ds_browser, ds_path): self.assertEqual('fake-ds-browser', ds_browser) self.assertEqual('[fake-ds] fake-path', str(ds_path)) if not self.exists: return ts = '%s%s' % (imagecache.TIMESTAMP_PREFIX, timeutils.strtime(at=self._time, fmt=imagecache.TIMESTAMP_FORMAT)) return ts with contextlib.nested( mock.patch.object(self._imagecache, '_get_timestamp', fake_get_timestamp), mock.patch.object(ds_util, 'file_delete') ) as (_get_timestamp, _file_delete): self.exists = False self._imagecache.timestamp_cleanup( 'fake-dc-ref', 'fake-ds-browser', ds_obj.DatastorePath('fake-ds', 'fake-path')) self.assertEqual(0, _file_delete.call_count) self.exists = True self._imagecache.timestamp_cleanup( 'fake-dc-ref', 'fake-ds-browser', ds_obj.DatastorePath('fake-ds', 'fake-path')) expected_ds_path = ds_obj.DatastorePath( 'fake-ds', 'fake-path', self._file_name) _file_delete.assert_called_once_with(self._session, expected_ds_path, 'fake-dc-ref')
def test_join(self): p = datastore.DatastorePath('ds_name', 'a') ds_path = p.join('b') self.assertEqual('[ds_name] a/b', str(ds_path)) p = datastore.DatastorePath('ds_name', 'a') ds_path = p.join() bad_args = [[None], ['', None], ['a', None], ['a', None, 'b']] for arg in bad_args: self.assertRaises(ValueError, p.join, *arg)
def test_ds_path_no_subdir(self): args = [('dsname', ['', 'x.vmdk']), ('dsname', ['x.vmdk'])] canonical_p = datastore.DatastorePath('dsname', 'x.vmdk') self.assertEqual('[dsname] x.vmdk', str(canonical_p)) self.assertEqual('', canonical_p.dirname) self.assertEqual('x.vmdk', canonical_p.basename) self.assertEqual('x.vmdk', canonical_p.rel_path) for t in args: p = datastore.DatastorePath(t[0], *t[1]) self.assertEqual(str(canonical_p), str(p))
def test_ds_path_ds_only(self): args = [('dsname', []), ('dsname', ['']), ('dsname', ['', ''])] canonical_p = datastore.DatastorePath('dsname') self.assertEqual('[dsname]', str(canonical_p)) self.assertEqual('', canonical_p.rel_path) self.assertEqual('', canonical_p.basename) self.assertEqual('', canonical_p.dirname) for t in args: p = datastore.DatastorePath(t[0], *t[1]) self.assertEqual(str(canonical_p), str(p)) self.assertEqual(canonical_p.rel_path, p.rel_path)
def test_ds_path_equivalence(self): args = [('dsname', ['a/b/c/', 'x.vmdk']), ('dsname', ['a/', 'b/c/', 'x.vmdk']), ('dsname', ['a', 'b', 'c', 'x.vmdk']), ('dsname', ['a/b/c', 'x.vmdk'])] canonical_p = datastore.DatastorePath('dsname', 'a/b/c', 'x.vmdk') for t in args: p = datastore.DatastorePath(t[0], *t[1]) self.assertEqual(str(canonical_p), str(p)) self.assertEqual(canonical_p.datastore, p.datastore) self.assertEqual(canonical_p.rel_path, p.rel_path) self.assertEqual(str(canonical_p.parent), str(p.parent))
def test_ds_path(self): p = datastore.DatastorePath('dsname', 'a/b/c', 'file.iso') self.assertEqual('[dsname] a/b/c/file.iso', str(p)) self.assertEqual('a/b/c/file.iso', p.rel_path) self.assertEqual('a/b/c', p.parent.rel_path) self.assertEqual('[dsname] a/b/c', str(p.parent)) self.assertEqual('dsname', p.datastore) self.assertEqual('file.iso', p.basename) self.assertEqual('a/b/c', p.dirname)
def test_ds_path_non_equivalence(self): args = [ # leading slash ('dsname', ['/a', 'b', 'c', 'x.vmdk']), ('dsname', ['/a/b/c/', 'x.vmdk']), ('dsname', ['a/b/c', '/x.vmdk']), # leading space ('dsname', ['a/b/c/', ' x.vmdk']), ('dsname', ['a/', ' b/c/', 'x.vmdk']), ('dsname', [' a', 'b', 'c', 'x.vmdk']), # trailing space ('dsname', ['/a/b/c/', 'x.vmdk ']), ('dsname', ['a/b/c/ ', 'x.vmdk']) ] canonical_p = datastore.DatastorePath('dsname', 'a/b/c', 'x.vmdk') for t in args: p = datastore.DatastorePath(t[0], *t[1]) self.assertNotEqual(str(canonical_p), str(p))
def test_age_cached_images(self): def fake_get_ds_browser(ds_ref): return 'fake-ds-browser' def fake_get_timestamp(ds_browser, ds_path): self._get_timestamp_called += 1 path = str(ds_path) if path == '[fake-ds] fake-path/fake-image-1': # No time stamp exists return if path == '[fake-ds] fake-path/fake-image-2': # Timestamp that will be valid => no deletion return 'ts-2012-11-22-10-00-00' if path == '[fake-ds] fake-path/fake-image-3': # Timestamp that will be invalid => deletion return 'ts-2012-11-20-12-00-00' self.fail() def fake_mkdir(session, ts_path, dc_ref): self.assertEqual( '[fake-ds] fake-path/fake-image-1/ts-2012-11-22-12-00-00', str(ts_path)) def fake_file_delete(session, ds_path, dc_ref): self.assertEqual('[fake-ds] fake-path/fake-image-3', str(ds_path)) def fake_timestamp_cleanup(dc_ref, ds_browser, ds_path): self.assertEqual('[fake-ds] fake-path/fake-image-4', str(ds_path)) with contextlib.nested( mock.patch.object(self._imagecache, '_get_ds_browser', fake_get_ds_browser), mock.patch.object(self._imagecache, '_get_timestamp', fake_get_timestamp), mock.patch.object(ds_util, 'mkdir', fake_mkdir), mock.patch.object(ds_util, 'file_delete', fake_file_delete), mock.patch.object(self._imagecache, 'timestamp_cleanup', fake_timestamp_cleanup), ) as (_get_ds_browser, _get_timestamp, _mkdir, _file_delete, _timestamp_cleanup): timeutils.set_time_override(override_time=self._time) datastore = ds_obj.Datastore(name='ds', ref='fake-ds-ref') dc_info = vmops.DcInfo(ref='dc_ref', name='name', vmFolder='vmFolder') self._get_timestamp_called = 0 self._imagecache.originals = set(['fake-image-1', 'fake-image-2', 'fake-image-3', 'fake-image-4']) self._imagecache.used_images = set(['fake-image-4']) self._imagecache._age_cached_images( 'fake-context', datastore, dc_info, ds_obj.DatastorePath('fake-ds', 'fake-path')) self.assertEqual(3, self._get_timestamp_called)
def test_get_timestamp(self): def fake_get_sub_folders(session, ds_browser, ds_path): self.assertEqual('fake-ds-browser', ds_browser) self.assertEqual('[fake-ds] fake-path', str(ds_path)) if self.exists: files = set() files.add(self._file_name) return files with mock.patch.object(ds_util, 'get_sub_folders', fake_get_sub_folders): self.exists = True ts = self._imagecache._get_timestamp( 'fake-ds-browser', ds_obj.DatastorePath('fake-ds', 'fake-path')) self.assertEqual(self._file_name, ts) self.exists = False ts = self._imagecache._get_timestamp( 'fake-ds-browser', ds_obj.DatastorePath('fake-ds', 'fake-path')) self.assertIsNone(ts)
def test_mkdir(self): def fake_call_method(module, method, *args, **kwargs): self.assertEqual('MakeDirectory', method) name = kwargs.get('name') self.assertEqual('[ds] fake/path', name) datacenter = kwargs.get('datacenter') self.assertEqual('fake-dc-ref', datacenter) createParentDirectories = kwargs.get('createParentDirectories') self.assertTrue(createParentDirectories) with mock.patch.object(self.session, '_call_method', fake_call_method): ds_path = ds_obj.DatastorePath('ds', 'fake/path') ds_util.mkdir(self.session, ds_path, 'fake-dc-ref')
def test_file_move(self): def fake_call_method(module, method, *args, **kwargs): self.assertEqual('MoveDatastoreFile_Task', method) sourceName = kwargs.get('sourceName') self.assertEqual('[ds] tmp/src', sourceName) destinationName = kwargs.get('destinationName') self.assertEqual('[ds] base/dst', destinationName) sourceDatacenter = kwargs.get('sourceDatacenter') self.assertEqual('fake-dc-ref', sourceDatacenter) destinationDatacenter = kwargs.get('destinationDatacenter') self.assertEqual('fake-dc-ref', destinationDatacenter) return 'fake_move_task' with contextlib.nested( mock.patch.object(self.session, '_wait_for_task'), mock.patch.object(self.session, '_call_method', fake_call_method)) as (_wait_for_task, _call_method): src_ds_path = ds_obj.DatastorePath('ds', 'tmp/src') dst_ds_path = ds_obj.DatastorePath('ds', 'base/dst') ds_util.file_move(self.session, 'fake-dc-ref', src_ds_path, dst_ds_path) _wait_for_task.assert_has_calls([mock.call('fake_move_task')])
def test_file_copy(self): def fake_call_method(module, method, *args, **kwargs): self.assertEqual('CopyDatastoreFile_Task', method) src_name = kwargs.get('sourceName') self.assertEqual('[ds] fake/path/src_file', src_name) src_dc_ref = kwargs.get('sourceDatacenter') self.assertEqual('fake-src-dc-ref', src_dc_ref) dst_name = kwargs.get('destinationName') self.assertEqual('[ds] fake/path/dst_file', dst_name) dst_dc_ref = kwargs.get('destinationDatacenter') self.assertEqual('fake-dst-dc-ref', dst_dc_ref) return 'fake_copy_task' with contextlib.nested( mock.patch.object(self.session, '_wait_for_task'), mock.patch.object(self.session, '_call_method', fake_call_method)) as (_wait_for_task, _call_method): src_ds_path = ds_obj.DatastorePath('ds', 'fake/path', 'src_file') dst_ds_path = ds_obj.DatastorePath('ds', 'fake/path', 'dst_file') ds_util.file_copy(self.session, str(src_ds_path), 'fake-src-dc-ref', str(dst_ds_path), 'fake-dst-dc-ref') _wait_for_task.assert_has_calls([mock.call('fake_copy_task')])
def test_file_delete(self): def fake_call_method(module, method, *args, **kwargs): self.assertEqual('DeleteDatastoreFile_Task', method) name = kwargs.get('name') self.assertEqual('[ds] fake/path', name) datacenter = kwargs.get('datacenter') self.assertEqual('fake-dc-ref', datacenter) return 'fake_delete_task' with contextlib.nested( mock.patch.object(self.session, '_wait_for_task'), mock.patch.object(self.session, '_call_method', fake_call_method)) as (_wait_for_task, _call_method): ds_path = ds_obj.DatastorePath('ds', 'fake/path') ds_util.file_delete(self.session, ds_path, 'fake-dc-ref') _wait_for_task.assert_has_calls([mock.call('fake_delete_task')])
def test_file_exists(self): def fake_call_method(module, method, *args, **kwargs): if method == 'SearchDatastore_Task': ds_browser = args[0] self.assertEqual('fake-browser', ds_browser) datastorePath = kwargs.get('datastorePath') self.assertEqual('[ds] fake/path', datastorePath) return 'fake_exists_task' # Should never get here self.fail() def fake_wait_for_task(task_ref): if task_ref == 'fake_exists_task': result_file = fake.DataObject() result_file.path = 'fake-file' result = fake.DataObject() result.file = [result_file] result.path = '[ds] fake/path' task_info = fake.DataObject() task_info.result = result return task_info # Should never get here self.fail() with contextlib.nested( mock.patch.object(self.session, '_call_method', fake_call_method), mock.patch.object(self.session, '_wait_for_task', fake_wait_for_task)): ds_path = ds_obj.DatastorePath('ds', 'fake/path') file_exists = ds_util.file_exists(self.session, 'fake-browser', ds_path, 'fake-file') self.assertTrue(file_exists)
def test_file_exists_fails(self): def fake_call_method(module, method, *args, **kwargs): if method == 'SearchDatastore_Task': return 'fake_exists_task' # Should never get here self.fail() def fake_wait_for_task(task_ref): if task_ref == 'fake_exists_task': raise vexc.FileNotFoundException() # Should never get here self.fail() with contextlib.nested( mock.patch.object(self.session, '_call_method', fake_call_method), mock.patch.object(self.session, '_wait_for_task', fake_wait_for_task)): ds_path = ds_obj.DatastorePath('ds', 'fake/path') file_exists = ds_util.file_exists(self.session, 'fake-browser', ds_path, 'fake-file') self.assertFalse(file_exists)
def test_equal(self): a = datastore.DatastorePath('ds_name', 'a') b = datastore.DatastorePath('ds_name', 'a') self.assertEqual(a, b)