def test_tenant_vm(self): """ Test AdminCLI command for tenant vm management """ # create tenant1 without adding any vms and privilege error_info, tenant = auth_api._tenant_create( name=self.tenant1_name, description="Test tenant1", vm_list=[], privileges=[]) self.assertEqual(None, error_info) error_info, vms = auth_api._tenant_vm_ls(self.tenant1_name) self.assertEqual(None, error_info) headers = vmdkops_admin.tenant_vm_ls_headers() rows = vmdkops_admin.generate_tenant_vm_ls_rows(vms) self.assertEqual(len(headers), TENANT_VM_LS_EXPECTED_COLUMN_COUNT) expected_output = [] actual_output = rows self.assertEqual(expected_output, actual_output) # tenant vm add to add two VMs to the tenant error_info = auth_api._tenant_vm_add( name=self.tenant1_name, vm_list=[self.vm1_name, self.vm2_name]) self.assertEqual(None, error_info) error_info, vms = auth_api._tenant_vm_ls(self.tenant1_name) self.assertEqual(None, error_info) # There are 2 columns for each row, the name of the columns are # "Uuid", "Name" # Sample output of a row: # [u'564d2b7d-187c-eaaf-60bc-e015b5cdc3eb', 'test_vm1'] rows = vmdkops_admin.generate_tenant_vm_ls_rows(vms) # Two vms are associated with this tenant self.assertEqual(len(rows), 2) expected_output = [self.vm1_name, self.vm2_name] actual_output = [rows[0][1], rows[1][1]] self.assertEqual(expected_output, actual_output) # tenant vm rm to remove one VM from the tenant error_info = auth_api._tenant_vm_rm( name=self.tenant1_name, vm_list=[self.vm2_name]) self.assertEqual(None, error_info) error_info, vms = auth_api._tenant_vm_ls(self.tenant1_name) self.assertEqual(None, error_info) rows = vmdkops_admin.generate_tenant_vm_ls_rows(vms) # tenant should only have one VM now self.assertEqual(len(rows), 1) expected_output = [self.vm1_name] actual_output = [rows[0][1]] self.assertEqual(expected_output, actual_output)
def tenant_vm_ls(args): """ Handle tenant vm ls command """ error_info, vms = auth_api._tenant_vm_ls(args.name) if error_info: return operation_fail(error_info.msg) header = tenant_vm_ls_headers() rows = generate_tenant_vm_ls_rows(vms) print(cli_table.create(header, rows))
def cleanup_tenant(name): error_info, vms = auth_api._tenant_vm_ls(name) if error_info: return error_info; # remove associated vms, if any if vms: vm_names = [vm_name for (_, vm_name) in vms] auth_api._tenant_vm_rm(name=name, vm_list=vm_names) # remove the tenant return auth_api._tenant_rm(name=name, remove_volumes=True)
def tenant_vm_ls(args): """ Handle tenant vm ls command """ # Handling _DEFAULT tenant case separately to print info message # instead of printing empty list if (args.name == auth_data_const.DEFAULT_TENANT): print("{0} tenant contains all VMs which were not added to other tenants".format(auth_data_const.DEFAULT_TENANT)) return error_info, vms = auth_api._tenant_vm_ls(args.name) if error_info: return operation_fail(error_info.msg) header = tenant_vm_ls_headers() rows = generate_tenant_vm_ls_rows(vms) print(cli_table.create(header, rows))
def test_tenant_vm(self): """ Test AdminCLI command for tenant vm management """ logging.debug("test_tenant_vm") # create tenant1 without adding any vms and privilege error_info, tenant = auth_api._tenant_create( name=self.tenant1_name, default_datastore=auth_data_const.VM_DS, description="Test tenant1", vm_list=[], privileges=[]) self.assertEqual(None, error_info) error_info, vms = auth_api._tenant_vm_ls(self.tenant1_name) self.assertEqual(None, error_info) headers = vmdkops_admin.tenant_vm_ls_headers() rows = vmdkops_admin.generate_tenant_vm_ls_rows(vms) self.assertEqual(len(headers), TENANT_VM_LS_EXPECTED_COLUMN_COUNT) expected_output = [] actual_output = rows self.assertEqual(expected_output, actual_output) # Trying to create tenant with duplicate vm names error_info, tenant_dup = auth_api._tenant_create( name="tenant_add_dup_vms", default_datastore=auth_data_const.VM_DS, description="Tenant with duplicate VMs", vm_list=[self.vm1_name, self.vm1_name], privileges=[]) self.assertEqual(error_code.ErrorCode.VM_DUPLICATE, error_info.code) # tenant vm add to add two VMs to the tenant error_info = auth_api._tenant_vm_add( name=self.tenant1_name, vm_list=[self.vm1_name, self.vm2_name]) self.assertEqual(None, error_info) error_info, vms = auth_api._tenant_vm_ls(self.tenant1_name) self.assertEqual(None, error_info) # create tenant2 with vm1 a part of it. Should fail as VM can be a part # of just one tenant error_info, tenant2 = auth_api._tenant_create( name="Test_tenant2", default_datastore=auth_data_const.VM_DS, description="Test_tenant2", vm_list=[self.vm1_name], privileges=[]) self.assertEqual(error_code.ErrorCode.VM_IN_ANOTHER_TENANT, error_info.code) # create tenant3 and then try to add vm1 to it which is a part of # another tenant. Should fail as VM can be a part of just one tenant error_info, tenant3 = auth_api._tenant_create( name="Test_tenant3", default_datastore=auth_data_const.VM_DS, description="Test_tenant3", vm_list=[], privileges=[]) self.assertEqual(None, error_info) error_info = auth_api._tenant_vm_add(name=tenant3.name, vm_list=[self.vm1_name]) self.assertEqual(error_code.ErrorCode.VM_IN_ANOTHER_TENANT, error_info.code) # Replace should fail since vm1 is already a part of tenant1 error_info = auth_api._tenant_vm_replace(name=tenant3.name, vm_list=[self.vm1_name]) self.assertEqual(error_code.ErrorCode.VM_IN_ANOTHER_TENANT, error_info.code) # remove the tenant3 error_info = test_utils.cleanup_tenant(name=tenant3.name) self.assertEqual(None, error_info) # There are 2 columns for each row, the name of the columns are # "Uuid", "Name" # Sample output of a row: # [u'564d2b7d-187c-eaaf-60bc-e015b5cdc3eb', 'test_vm1'] rows = vmdkops_admin.generate_tenant_vm_ls_rows(vms) # Two vms are associated with this tenant self.assertEqual(len(rows), 2) expected_output = [self.vm1_name, self.vm2_name] actual_output = [rows[0][1], rows[1][1]] self.assertEqual(expected_output, actual_output) # tenant vm rm to remove one VM from the tenant error_info = auth_api._tenant_vm_rm(name=self.tenant1_name, vm_list=[self.vm2_name]) self.assertEqual(None, error_info) error_info, vms = auth_api._tenant_vm_ls(self.tenant1_name) self.assertEqual(None, error_info) rows = vmdkops_admin.generate_tenant_vm_ls_rows(vms) # tenant should only have one VM now self.assertEqual(len(rows), 1) expected_output = [self.vm1_name] actual_output = [rows[0][1]] self.assertEqual(expected_output, actual_output)
def test_tenant_vm(self): """ Test AdminCLI command for tenant vm management """ logging.debug("test_tenant_vm") # create tenant1 without adding any vms and privilege error_info, tenant = auth_api._tenant_create( name=self.tenant1_name, default_datastore=auth_data_const.VM_DS, description="Test tenant1", vm_list=[], privileges=[]) self.assertEqual(None, error_info) error_info, vms = auth_api._tenant_vm_ls(self.tenant1_name) self.assertEqual(None, error_info) headers = vmdkops_admin.tenant_vm_ls_headers() rows = vmdkops_admin.generate_tenant_vm_ls_rows(vms) self.assertEqual(len(headers), TENANT_VM_LS_EXPECTED_COLUMN_COUNT) expected_output = [] actual_output = rows self.assertEqual(expected_output, actual_output) # Trying to create tenant with duplicate vm names error_info, tenant_dup = auth_api._tenant_create( name="tenant_add_dup_vms", default_datastore=auth_data_const.VM_DS, description="Tenant with duplicate VMs", vm_list=[self.vm1_name, self.vm1_name], privileges=[]) self.assertEqual(error_code.ErrorCode.VM_DUPLICATE, error_info.code) # tenant vm add to add two VMs to the tenant error_info = auth_api._tenant_vm_add( name=self.tenant1_name, vm_list=[self.vm1_name, self.vm2_name]) self.assertEqual(None, error_info) error_info, vms = auth_api._tenant_vm_ls(self.tenant1_name) self.assertEqual(None, error_info) # create tenant2 with vm1 a part of it. Should fail as VM can be a part # of just one tenant error_info, tenant2 = auth_api._tenant_create( name="Test_tenant2", default_datastore=auth_data_const.VM_DS, description="Test_tenant2", vm_list=[self.vm1_name], privileges=[]) self.assertEqual(error_code.ErrorCode.VM_IN_ANOTHER_TENANT, error_info.code) # create tenant3 and then try to add vm1 to it which is a part of # another tenant. Should fail as VM can be a part of just one tenant error_info, tenant3 = auth_api._tenant_create( name="Test_tenant3", default_datastore=auth_data_const.VM_DS, description="Test_tenant3", vm_list=[], privileges=[]) self.assertEqual(None, error_info) error_info = auth_api._tenant_vm_add( name=tenant3.name, vm_list=[self.vm1_name]) self.assertEqual(error_code.ErrorCode.VM_IN_ANOTHER_TENANT, error_info.code) # Replace should fail since vm1 is already a part of tenant1 error_info = auth_api._tenant_vm_replace( name=tenant3.name, vm_list=[self.vm1_name]) self.assertEqual(error_code.ErrorCode.VM_IN_ANOTHER_TENANT, error_info.code) # remove the tenant3 error_info = test_utils.cleanup_tenant(name=tenant3.name) self.assertEqual(None, error_info) # There are 2 columns for each row, the name of the columns are # "Uuid", "Name" # Sample output of a row: # [u'564d2b7d-187c-eaaf-60bc-e015b5cdc3eb', 'test_vm1'] rows = vmdkops_admin.generate_tenant_vm_ls_rows(vms) # Two vms are associated with this tenant self.assertEqual(len(rows), 2) expected_output = [self.vm1_name, self.vm2_name] actual_output = [rows[0][1], rows[1][1]] self.assertEqual(expected_output, actual_output) # tenant vm rm to remove one VM from the tenant error_info = auth_api._tenant_vm_rm( name=self.tenant1_name, vm_list=[self.vm2_name]) self.assertEqual(None, error_info) error_info, vms = auth_api._tenant_vm_ls(self.tenant1_name) self.assertEqual(None, error_info) rows = vmdkops_admin.generate_tenant_vm_ls_rows(vms) # tenant should only have one VM now self.assertEqual(len(rows), 1) expected_output = [self.vm1_name] actual_output = [rows[0][1]] self.assertEqual(expected_output, actual_output)