def test_remove_tenants(self): vms = [(str(uuid.uuid4()), 'vm1')] privileges = self.get_privileges() default_datastore, default_privileges = self.get_default_datastore_and_privileges() error_info, tenant1 = self.auth_mgr.create_tenant('tenant1', 'Some tenant', default_datastore, default_privileges, vms, privileges) self.assertEqual(error_info, None) self.assertTrue(uuid.UUID(tenant1.id)) vms = [(str(uuid.uuid4()), 'vm2'), (str(uuid.uuid4()), 'vm3')] privileges = [] error_info, tenant2 = self.auth_mgr.create_tenant('tenant2', 'Some tenant', default_datastore, default_privileges, vms, privileges) self.assertEqual(error_info, None) self.assertTrue(uuid.UUID(tenant2.id)) tenant2.set_datastore_access_privileges(self.auth_mgr.conn, privileges) error_info = self.auth_mgr.remove_tenant(tenant2.id, False) self.assertEqual(error_info, None) # Check tenants table error_info, tenants_row = auth.get_row_from_tenants_table(self.auth_mgr.conn, tenant2.id) self.assertEqual(error_info, None) self.assertEqual(tenants_row, None) # check vms table error_info, vms_row = auth.get_row_from_vms_table(self.auth_mgr.conn, tenant2.id) self.assertEqual(error_info, None) self.assertEqual(vms_row, []) # check privileges table error_info, privileges_row = auth.get_row_from_privileges_table(self.auth_mgr.conn, tenant2.id) self.assertEqual(error_info, None) self.assertEqual(privileges_row, [])
def test_set_default_datastore_and_privileges(self): vm1_uuid = str(uuid.uuid4()) vms = [(vm1_uuid, 'vm1')] privileges = self.get_privileges() default_datastore, default_privileges = self.get_default_datastore_and_privileges() error_info, tenant1 = self.auth_mgr.create_tenant('tenant1', 'Some tenant', default_datastore, default_privileges, vms, privileges) self.assertEqual(error_info, None) self.assertTrue(uuid.UUID(tenant1.id)) default_datastore = 'new_default_ds' default_privileges = {'datastore': default_datastore, 'global_visibility': 0, 'create_volume': 1, 'delete_volume': 1, 'mount_volume': 1, 'max_volume_size': 0, 'usage_quota': 0} error_info = tenant1.set_default_datastore_and_privileges(self.auth_mgr.conn, default_datastore, default_privileges) self.assertEqual(error_info, None) # Check tenants table error_info, tenants_row = auth.get_row_from_tenants_table(self.auth_mgr.conn, tenant1.id) self.assertEqual(error_info, None) expected_output = 'new_default_ds' actual_output = tenants_row[auth_data_const.COL_DEFAULT_DATASTORE] self.assertEqual(actual_output, expected_output) #check privileges table error_info, privileges_row = auth.get_row_from_privileges_table(self.auth_mgr.conn, tenant1.id) self.assertEqual(error_info, None) self.assertEqual(len(privileges_row), 2) expected_default_privileges = [tenant1.id, default_privileges[auth_data_const.COL_DATASTORE], default_privileges[auth_data_const.COL_GLOBAL_VISIBILITY], default_privileges[auth_data_const.COL_CREATE_VOLUME], default_privileges[auth_data_const.COL_DELETE_VOLUME], default_privileges[auth_data_const.COL_MOUNT_VOLUME], default_privileges[auth_data_const.COL_MAX_VOLUME_SIZE], default_privileges[auth_data_const.COL_USAGE_QUOTA] ] actual_default_privileges = [privileges_row[1][auth_data_const.COL_TENANT_ID], privileges_row[1][auth_data_const.COL_DATASTORE], privileges_row[1][auth_data_const.COL_GLOBAL_VISIBILITY], privileges_row[1][auth_data_const.COL_CREATE_VOLUME], privileges_row[1][auth_data_const.COL_DELETE_VOLUME], privileges_row[1][auth_data_const.COL_MOUNT_VOLUME], privileges_row[1][auth_data_const.COL_MAX_VOLUME_SIZE], privileges_row[1][auth_data_const.COL_USAGE_QUOTA] ] self.assertEqual(actual_default_privileges, expected_default_privileges)
def test_set_description(self): vm1_uuid = str(uuid.uuid4()) vms = [(vm1_uuid, 'vm1')] privileges = self.get_privileges() default_datastore, default_privileges = self.get_default_datastore_and_privileges() error_info, tenant1 = self.auth_mgr.create_tenant('tenant1', 'Some tenant', default_datastore, default_privileges, vms, privileges) self.assertEqual(error_info, None) self.assertTrue(uuid.UUID(tenant1.id)) error_info = tenant1.set_description(self.auth_mgr.conn, 'new description') self.assertEqual(error_info, None) error_info, tenants_row = auth.get_row_from_tenants_table(self.auth_mgr.conn, tenant1.id) self.assertEqual(error_info, None) expected_output = 'new description' actual_output = tenants_row[auth_data_const.COL_DESCRIPTION] self.assertEqual(actual_output, expected_output)
def test_set_description(self): vms = [(self.vm1_uuid, self.vm1_name)] privileges = self.get_privileges() error_info, tenant1 = self.auth_mgr.create_tenant(name=self.tenant_name, description='Some tenant', vms=vms, privileges=privileges) self.assertEqual(error_info, None) self.assertTrue(uuid.UUID(tenant1.id)) error_info = tenant1.set_description(self.auth_mgr.conn, 'new description') self.assertEqual(error_info, None) error_info, tenants_row = auth.get_row_from_tenants_table(self.auth_mgr.conn, tenant1.id) self.assertEqual(error_info, None) expected_output = 'new description' actual_output = tenants_row[auth_data_const.COL_DESCRIPTION] self.assertEqual(actual_output, expected_output)
def test_remove_tenants(self): vms = [(self.vm1_uuid, self.vm1_name)] privileges = self.get_privileges() default_datastore = self.get_default_datastore() default_datastore_url = self.get_datastore_url(default_datastore) error_info, tenant1 = self.auth_mgr.create_tenant(name=self.tenant_name, description='Some tenant', vms=vms, privileges=privileges) self.assertEqual(error_info, None) self.assertTrue(uuid.UUID(tenant1.id)) vms = [(self.vm2_uuid, self.vm2_name), (self.vm3_uuid, self.vm3_name)] privileges = [] error_info, tenant2 = self.auth_mgr.create_tenant(name=self.tenant_2_name, description='Some tenant', vms=vms, privileges=privileges) self.assertEqual(error_info, None) self.assertTrue(uuid.UUID(tenant2.id)) tenant2.set_datastore_access_privileges(self.auth_mgr.conn, privileges) error_info = self.auth_mgr.remove_tenant(tenant2.id, False) self.assertEqual(error_info, None) # Check tenants table error_info, tenants_row = auth.get_row_from_tenants_table(self.auth_mgr.conn, tenant2.id) self.assertEqual(error_info, None) self.assertEqual(tenants_row, None) # check vms table error_info, vms_row = auth.get_row_from_vms_table(self.auth_mgr.conn, tenant2.id) self.assertEqual(error_info, None) self.assertEqual(vms_row, []) # check privileges table error_info, privileges_row = auth.get_row_from_privileges_table(self.auth_mgr.conn, tenant2.id) self.assertEqual(error_info, None) self.assertEqual(privileges_row, [])
def test_set_name(self): vms = [(self.vm1_uuid, self.vm1_name)] privileges = self.get_privileges() default_datastore = self.get_default_datastore() default_datastore_url = self.get_datastore_url(default_datastore) error_info, tenant1 = self.auth_mgr.create_tenant(name=self.tenant_name, description='Some tenant', vms=vms, privileges=privileges) self.assertEqual(error_info, None) self.assertTrue(uuid.UUID(tenant1.id)) error_info = tenant1.set_name(self.auth_mgr.conn, self.tenant_name, self.tenant_2_name) self.assertEqual(error_info, None) error_info, tenants_row = auth.get_row_from_tenants_table(self.auth_mgr.conn, tenant1.id) self.assertEqual(error_info, None) expected_output = self.tenant_2_name actual_output = tenants_row[auth_data_const.COL_NAME] self.assertEqual(actual_output, expected_output)
def get_default_datastore(self, conn): """ Get default_datastore for this tenant Return value: error_msg: return None on success or error info on failure datastore: return default_datastore name on success or None on failure """ error_msg, result = auth.get_row_from_tenants_table(conn, self.id) if error_msg: logging.error("Error %s when getting default datastore for tenant_id %s", error_msg, self.id) return str(error_msg), None else: datastore_url = result[auth_data_const.COL_DEFAULT_DATASTORE_URL] logging.debug("get_default_datastore: datastore_url=%s", datastore_url) if not datastore_url: # datastore_url read from DB is empty return None, None else: datastore = vmdk_utils.get_datastore_name(datastore_url) return None, datastore
def test_set_default_datastore(self): vm1_uuid = str(uuid.uuid4()) vms = [(vm1_uuid)] privileges = self.get_privileges() default_datastore = self.get_default_datastore() default_datastore_url = self.get_datastore_url(default_datastore) error_info, tenant1 = self.auth_mgr.create_tenant(name='tenant1', description='Some tenant', vms=vms, privileges=privileges) self.assertEqual(error_info, None) self.assertTrue(uuid.UUID(tenant1.id)) default_datastore = 'new_default_ds' default_datastore_url = self.get_datastore_url(default_datastore) error_info = tenant1.set_default_datastore(self.auth_mgr.conn, default_datastore_url) self.assertEqual(error_info, None) # Check tenants table error_info, tenants_row = auth.get_row_from_tenants_table(self.auth_mgr.conn, tenant1.id) self.assertEqual(error_info, None) expected_output = 'new_default_ds_url' actual_output = tenants_row[auth_data_const.COL_DEFAULT_DATASTORE_URL] self.assertEqual(actual_output, expected_output)
def test_create_tenant(self): """ Test create_tenant() API """ vm1_uuid = str(uuid.uuid4()) vms = [(vm1_uuid, 'vm1')] #privileges = [] privileges = self.get_privileges() default_datastore, default_privileges = self.get_default_datastore_and_privileges() error_info, tenant1 = self.auth_mgr.create_tenant('tenant1', 'Some tenant', default_datastore, default_privileges, vms, privileges) self.assertEqual(error_info, None) self.assertTrue(uuid.UUID(tenant1.id)) # Check tenants table error_info, tenants_row = auth.get_row_from_tenants_table(self.auth_mgr.conn, tenant1.id) self.assertEqual(error_info, None) expected_output = [tenant1.id, 'tenant1', 'Some tenant', 'default_ds'] actual_output = [tenants_row[auth_data_const.COL_ID], tenants_row[auth_data_const.COL_NAME], tenants_row[auth_data_const.COL_DESCRIPTION], tenants_row[auth_data_const.COL_DEFAULT_DATASTORE] ] self.assertEqual(actual_output, expected_output) # check vms table error_info, vms_row = auth.get_row_from_vms_table(self.auth_mgr.conn, tenant1.id) self.assertEqual(error_info, None) expected_output = [vm1_uuid, 'vm1', tenant1.id] self.assertEqual(len(vms_row), 1) actual_output = [vms_row[0][auth_data_const.COL_VM_ID], vms_row[0][auth_data_const.COL_VM_NAME], vms_row[0][auth_data_const.COL_TENANT_ID] ] self.assertEqual(actual_output, expected_output) # check privileges table error_info, privileges_row = auth.get_row_from_privileges_table(self.auth_mgr.conn, tenant1.id) self.assertEqual(error_info, None) self.assertEqual(len(privileges_row), 2) expected_privileges = [tenant1.id, privileges[0][auth_data_const.COL_DATASTORE], privileges[0][auth_data_const.COL_GLOBAL_VISIBILITY], privileges[0][auth_data_const.COL_CREATE_VOLUME], privileges[0][auth_data_const.COL_DELETE_VOLUME], privileges[0][auth_data_const.COL_MOUNT_VOLUME], privileges[0][auth_data_const.COL_MAX_VOLUME_SIZE], privileges[0][auth_data_const.COL_USAGE_QUOTA] ] expected_default_privileges = [tenant1.id, default_privileges[auth_data_const.COL_DATASTORE], default_privileges[auth_data_const.COL_GLOBAL_VISIBILITY], default_privileges[auth_data_const.COL_CREATE_VOLUME], default_privileges[auth_data_const.COL_DELETE_VOLUME], default_privileges[auth_data_const.COL_MOUNT_VOLUME], default_privileges[auth_data_const.COL_MAX_VOLUME_SIZE], default_privileges[auth_data_const.COL_USAGE_QUOTA] ] expected_output = [expected_privileges, expected_default_privileges ] actual_privileges = [privileges_row[0][auth_data_const.COL_TENANT_ID], privileges_row[0][auth_data_const.COL_DATASTORE], privileges_row[0][auth_data_const.COL_GLOBAL_VISIBILITY], privileges_row[0][auth_data_const.COL_CREATE_VOLUME], privileges_row[0][auth_data_const.COL_DELETE_VOLUME], privileges_row[0][auth_data_const.COL_MOUNT_VOLUME], privileges_row[0][auth_data_const.COL_MAX_VOLUME_SIZE], privileges_row[0][auth_data_const.COL_USAGE_QUOTA] ] actual_default_privileges = [privileges_row[1][auth_data_const.COL_TENANT_ID], privileges_row[1][auth_data_const.COL_DATASTORE], privileges_row[1][auth_data_const.COL_GLOBAL_VISIBILITY], privileges_row[1][auth_data_const.COL_CREATE_VOLUME], privileges_row[1][auth_data_const.COL_DELETE_VOLUME], privileges_row[1][auth_data_const.COL_MOUNT_VOLUME], privileges_row[1][auth_data_const.COL_MAX_VOLUME_SIZE], privileges_row[1][auth_data_const.COL_USAGE_QUOTA] ] actual_output = [actual_privileges, actual_default_privileges] self.assertEqual(actual_output, expected_output)
def test_create_tenant(self): """ Test create_tenant() API """ vm1_uuid = str(uuid.uuid4()) vms = [(vm1_uuid)] privileges = self.get_privileges() default_datastore = self.get_default_datastore() default_datastore_url = self.get_datastore_url(default_datastore) error_info, tenant1 = self.auth_mgr.create_tenant(name='tenant1', description='Some tenant', vms=vms, privileges=privileges) self.assertEqual(error_info, None) self.assertTrue(uuid.UUID(tenant1.id)) # Check tenants table error_info, tenants_row = auth.get_row_from_tenants_table(self.auth_mgr.conn, tenant1.id) self.assertEqual(error_info, None) expected_output = [tenant1.id, 'tenant1', 'Some tenant'] actual_output = [tenants_row[auth_data_const.COL_ID], tenants_row[auth_data_const.COL_NAME], tenants_row[auth_data_const.COL_DESCRIPTION] ] self.assertEqual(actual_output, expected_output) # check vms table error_info, vms_row = auth.get_row_from_vms_table(self.auth_mgr.conn, tenant1.id) self.assertEqual(error_info, None) expected_output = [vm1_uuid, tenant1.id] self.assertEqual(len(vms_row), 1) actual_output = [vms_row[0][auth_data_const.COL_VM_ID], vms_row[0][auth_data_const.COL_TENANT_ID] ] self.assertEqual(actual_output, expected_output) # check privileges table error_info, privileges_row = auth.get_row_from_privileges_table(self.auth_mgr.conn, tenant1.id) self.assertEqual(error_info, None) self.assertEqual(len(privileges_row), 1) expected_privileges = [tenant1.id, privileges[0][auth_data_const.COL_DATASTORE_URL], privileges[0][auth_data_const.COL_ALLOW_CREATE], privileges[0][auth_data_const.COL_MAX_VOLUME_SIZE], privileges[0][auth_data_const.COL_USAGE_QUOTA] ] expected_output = [expected_privileges ] actual_privileges = [privileges_row[0][auth_data_const.COL_TENANT_ID], privileges_row[0][auth_data_const.COL_DATASTORE_URL], privileges_row[0][auth_data_const.COL_ALLOW_CREATE], privileges_row[0][auth_data_const.COL_MAX_VOLUME_SIZE], privileges_row[0][auth_data_const.COL_USAGE_QUOTA] ] actual_output = [actual_privileges] self.assertEqual(actual_output, expected_output)
def test_create_tenant(self): """ Test create_tenant() API """ vms = [(self.vm1_uuid, self.vm1_name)] privileges = self.get_privileges() default_datastore = self.get_default_datastore() default_datastore_url = self.get_datastore_url(default_datastore) error_info, tenant1 = self.auth_mgr.create_tenant(name=self.tenant_name, description='Some tenant', vms=vms, privileges=privileges) self.assertEqual(error_info, None) self.assertTrue(uuid.UUID(tenant1.id)) # Check tenants table error_info, tenants_row = auth.get_row_from_tenants_table(self.auth_mgr.conn, tenant1.id) self.assertEqual(error_info, None) expected_output = [tenant1.id, self.tenant_name, 'Some tenant'] actual_output = [tenants_row[auth_data_const.COL_ID], tenants_row[auth_data_const.COL_NAME], tenants_row[auth_data_const.COL_DESCRIPTION] ] self.assertEqual(actual_output, expected_output) # check vms table error_info, vms_row = auth.get_row_from_vms_table(self.auth_mgr.conn, tenant1.id) self.assertEqual(error_info, None) expected_output = [self.vm1_uuid, tenant1.id] self.assertEqual(len(vms_row), 1) actual_output = [vms_row[0][auth_data_const.COL_VM_ID], vms_row[0][auth_data_const.COL_TENANT_ID] ] self.assertEqual(actual_output, expected_output) # check privileges table error_info, privileges_row = auth.get_row_from_privileges_table(self.auth_mgr.conn, tenant1.id) self.assertEqual(error_info, None) self.assertEqual(len(privileges_row), 1) expected_privileges = [tenant1.id, privileges[0][auth_data_const.COL_DATASTORE_URL], privileges[0][auth_data_const.COL_ALLOW_CREATE], privileges[0][auth_data_const.COL_MAX_VOLUME_SIZE], privileges[0][auth_data_const.COL_USAGE_QUOTA] ] expected_output = [expected_privileges ] actual_privileges = [privileges_row[0][auth_data_const.COL_TENANT_ID], privileges_row[0][auth_data_const.COL_DATASTORE_URL], privileges_row[0][auth_data_const.COL_ALLOW_CREATE], privileges_row[0][auth_data_const.COL_MAX_VOLUME_SIZE], privileges_row[0][auth_data_const.COL_USAGE_QUOTA] ] actual_output = [actual_privileges] self.assertEqual(actual_output, expected_output)