def test_glance_ids_tenant(self): add_args = [] if 'OS_TENANT_ID' in os.environ: add_args = ['--owner', os.environ['OS_TENANT_ID']] elif 'OS_TENANT_NAME' in os.environ: tenant_name = os.environ['OS_TENANT_NAME'] ret = utils.run(['keystone', 'tenant-get', tenant_name], out=True, err=True) status, _, out, err = ret if status: self.assertTrue(len(out) > 0) _, block, _, _ = openstack_out.parse_block(out) self.assertTrue(len(block) > 0) for prop, val in block: if prop == 'id': add_args = ['--owner', val] break self.fail('TENANT "%s" not found by keystone...' % tenant_name) else: print 'OUT:', out print 'ERR:', err self.fail('Unable to run keystone tenant-get %s' % tenant_name) self.assertTrue(len(add_args) == 2) l1 = glance.glance_ids(None, *add_args) self.assertTrue(set() < l1) # We can see more images if we are administrator and don't filter on tenant l2 = glance.glance_ids(names=None) self.assertTrue(set() < l1 <= l2)
def test_glance_ids_uuid(self): glance.glance_delete_all(utils.test_name(), quiet=True) ret = glance.glance_import(os.devnull, name=utils.test_name(), diskformat='raw') self.assertTrue(ret) img_uuids_name = list(glance.glance_ids(utils.test_name())) self.assertEqual(len(img_uuids_name), 1) img_uuid = img_uuids_name[0] img_uuids_uuid = list(glance.glance_ids(img_uuid)) self.assertEqual(img_uuids_uuid, img_uuids_name) glance.glance_delete_all(utils.test_name(), quiet=True)
def get_glance_images(): '''Get info about images already in glance Cache those informations to speedup subsequent calls ''' global _GLANCE_IMAGES if _GLANCE_IMAGES is None: _GLANCE_IMAGES = {} add_args = [] tenant_msg = 'Using %s environment variable to filter image list' if 'OS_TENANT_ID' in os.environ: vprint(tenant_msg % 'OS_TENANT_ID') add_args = ['--owner', os.environ['OS_TENANT_ID']] elif 'OS_TENANT_NAME' in os.environ: vprint(tenant_msg % 'OS_TENANT_NAME') cmd = ['keystone', 'tenant-get', os.environ['OS_TENANT_NAME']] status, _, out, _ = utils.run(cmd, out=True) if status: _, block, _, _ = openstack_out.parse_block(out) for prop, val in block: if prop == 'id': add_args = ['--owner', val] break for imgid in glance.glance_ids(None, *add_args): img = glance.glance_show(imgid) if img: vmmap = openstack_out.map_block(img) if 'mpid' in vmmap: vprint( ("Found 'mpid' property (%(mpid)s) already set on " + "image: %(id)s (%(name)s)") % vmmap) _GLANCE_IMAGES[vmmap['mpid']] = vmmap _GLANCE_IMAGES[vmmap['checksum']] = vmmap _GLANCE_IMAGES[vmmap['name']] = vmmap return _GLANCE_IMAGES
def test_glance_ids_list_single(self): self.assertEqual(set(), glance.glance_ids([None])) self.assertEqual(set(), glance.glance_ids([True])) self.assertEqual(set(), glance.glance_ids([False])) self.assertEqual(set(), glance.glance_ids([''])) self.assertEqual(set(), glance.glance_ids([u''])) self.assertEqual(set(), glance.glance_ids([[]]))
def test_glance_rename_multi(self): # Rename multiple images with same name... self.common_start() self.assertTrue(glance.glance_exists(self._IMG_NAME)) IMG_ID = glance.glance_import_id(os.devnull, name=self._IMG_NAME, diskformat='raw') self.assertTrue(IMG_ID) self.assertTrue(glance.glance_exists(IMG_ID)) self.assertTrue(glance.glance_exists(self._IMG_NAME)) self.assertTrue(len(glance.glance_ids(self._IMG_NAME)) == 2) self.assertTrue(glance.glance_rename(self._IMG_NAME, self._IMG_NAME + '_rename_multi')) # One has been renamed, the other stayed as-is... self.assertTrue(glance.glance_exists(self._IMG_NAME + '_rename_multi')) self.assertTrue(glance.glance_exists(self._IMG_NAME)) self.assertTrue(glance.glance_exists(self._IMG_ID)) self.assertTrue(glance.glance_exists(IMG_ID)) # Get it back to its previous name, so that it is cleaned up self.assertTrue(glance.glance_rename(self._IMG_NAME + '_rename_multi', self._IMG_NAME))
def test_glance_rename_multi(self): # Rename multiple images with same name... self.common_start() self.assertTrue(glance.glance_exists(self._IMG_NAME)) IMG_ID = glance.glance_import_id(os.devnull, name=self._IMG_NAME, diskformat='raw') self.assertTrue(IMG_ID) self.assertTrue(glance.glance_exists(IMG_ID)) self.assertTrue(glance.glance_exists(self._IMG_NAME)) self.assertTrue(len(glance.glance_ids(self._IMG_NAME)) == 2) self.assertTrue( glance.glance_rename(self._IMG_NAME, self._IMG_NAME + '_rename_multi')) # One has been renamed, the other stayed as-is... self.assertTrue(glance.glance_exists(self._IMG_NAME + '_rename_multi')) self.assertTrue(glance.glance_exists(self._IMG_NAME)) self.assertTrue(glance.glance_exists(self._IMG_ID)) self.assertTrue(glance.glance_exists(IMG_ID)) # Get it back to its previous name, so that it is cleaned up self.assertTrue( glance.glance_rename(self._IMG_NAME + '_rename_multi', self._IMG_NAME))
def get_glance_images(): '''Get info about images already in glance Cache those informations to speedup subsequent calls ''' global _GLANCE_IMAGES if _GLANCE_IMAGES is None: _GLANCE_IMAGES = {} add_args = [] tenant_msg = 'Using %s environment variable to filter image list' if 'OS_TENANT_ID' in os.environ: vprint(tenant_msg % 'OS_TENANT_ID') add_args = ['--owner', os.environ['OS_TENANT_ID']] elif 'OS_TENANT_NAME' in os.environ: vprint(tenant_msg % 'OS_TENANT_NAME') cmd = ['keystone', 'tenant-get', os.environ['OS_TENANT_NAME']] status, _, out, err = utils.run(cmd, out=True, err=True) if status: _, block, _, _ = openstack_out.parse_block(out) for prop, val in block: if prop == 'id': add_args = ['--owner', val] break else: vprint('Failed to run : %s' % ' '.join(cmd)) vprint('OUT: ' + str(out)) vprint('ERR: ' + str(err)) for imgid in glance.glance_ids(None, *add_args): img = glance.glance_show(imgid) if img: vmmap = openstack_out.map_block(img) if 'mpid' in vmmap: vprint(("Found 'mpid' property (%(mpid)s) already set on " + "image: %(id)s (%(name)s)") % vmmap) _GLANCE_IMAGES[vmmap['mpid']] = vmmap _GLANCE_IMAGES[vmmap['checksum']] = vmmap _GLANCE_IMAGES[vmmap['name']] = vmmap return _GLANCE_IMAGES
def test_glance_ids_list(self): self.assertEqual(set(), glance.glance_ids([None, True, False])) self.assertEqual(set(), glance.glance_ids(['', u'']))
def test_glance_ids_single(self): self.assertEqual(set(), glance.glance_ids(True)) self.assertEqual(set(), glance.glance_ids(False)) self.assertEqual(set(), glance.glance_ids('')) self.assertEqual(set(), glance.glance_ids(u'')) self.assertEqual(set(), glance.glance_ids([]))
def test_glance_ids_unfiltered(self): l1 = glance.glance_ids(None) self.assertTrue(set() <= l1) l2 = glance.glance_ids() self.assertTrue(set() <= l2) self.assertEqual(l1, l2)
def test_glance_ids(self): self.assertEqual(set(), glance.glance_ids('XXX')) with utils.environ('OS_PASSWORD', 'not_the_one'): self.assertEqual(set(), glance.glance_ids('XXX'))