def change_user_password(username, password, tenant_name, newpassword): """ Change a user can change its password """ bt = BaseTest() user = bt.get_user_by_name(username) if user == None: return 3 headers = {'Content-Type': 'application/json', 'Accept': 'application/json'} endpoint = bt.get_service_endpoint('keystone') endpoint = endpoint + '/users/' + user['id'] bt_user = BaseTest(username, password, tenant_name) post_body = { 'user': { 'id': user['id'], 'password': newpassword, } } response, content = bt_user.put(endpoint, headers=headers, body=json.dumps(post_body)) if(response['status'] != '200'): return 1 return 0
def test_nova_launch_instance_multiimg(username, password, tenant_name, images_name, flavor_id, timeout): """ test a user to create multiple server with different images file """ images_id = list() bt = BaseTest() user_bt = BaseTest(username, password, tenant_name) for image_name in images_name: image = bt.get_image_by_name(image_name) if (image == None): return 3 images_id.append(image['id']) images_dict = dict(zip(images_name, images_id)) for image_name in images_dict: server_name = 'jeos_' + image_name resp, server = user_bt.servers_client.create_server( server_name, images_dict[image_name], flavor_id) ret = user_bt._wait_for_server_status(server['id'], 'ACTIVE', timeout) if (ret != 0): return 1 user_bt.clean_servers('jeos_', True) return 0
def test_delete_user_nologin(username, password, tenant_name): """ test a user deleted can not login anymore """ bt = BaseTest() try: bt_user = BaseTest(username, password, tenant_name) except exceptions.AuthenticationFailure: return 0 return 1
def test_nocleanup_nova_instance_admin_user(username, password, tenant_name, server_name): """ test a uer to teminiate a server created by administrator should fail """ bt = BaseTest() server = bt.get_server_by_name(server_name) user_bt = BaseTest(username, password, tenant_name) try: res, meta = user_bt.servers_client.delete_server(server['id']) except exceptions.NotFound: return 0 return 1
def test_user_disabled_nologin(username, password, tenant_name): """ test a disabled user can not to login anymore """ bt = BaseTest() user = bt.get_user_by_name(username) if (user == None): return 3 try: bt_user = BaseTest(username, password, tenant_name) except exceptions.AuthenticationFailure: return 0 return 1
def test_tenant_reenable(tenant_name): """ test enable a disabled tenant """ bt = BaseTest() tenant = bt.get_tenant_by_name(tenant_name) if (tenant == None): return 3 tenant_id = tenant['id'] endpoint = bt.get_service_endpoint('keystone') endpoint = endpoint + '/tenants/' + tenant_id body = { "tenant": { "enabled": True, }, } headers = { 'Content-Type': 'application/json', 'Accept': 'application/json' } response, content = bt.post(endpoint, headers=headers, body=json.dumps(body)) if (response['status'] == '200'): return 0 return 1
def test_create_user_by_name(username, password, tenant_name): """ test administrator to create a user whose name is the giving name""" bt = BaseTest() endpoint = bt.get_service_endpoint('keystone') endpoint = endpoint + '/users' headers = { 'Content-Type': 'application/json', 'Accept': 'application/json' } tenant = bt.get_tenant_by_name(tenant_name) user = bt.get_user_by_name(username) if tenant == None or user != None: return 3 body = { 'user': { 'name': username, 'password': password, 'tenantId': tenant['id'], 'email': '*****@*****.**', 'enabled': True } } response, content = bt.post(endpoint, headers=headers, body=json.dumps(body)) if (response['status'] == '200'): return 0 return 1
def test_user_reenable(username): """ test enable a user who was disabled """ bt = BaseTest() user = bt.get_user_by_name(username) if (user == None): return 3 user_id = user['id'] endpoint = bt.get_service_endpoint('keystone') endpoint = endpoint + '/users/' + user_id + '/enabled' headers = { 'Content-Type': 'application/json', 'Accept': 'application/json' } body = { 'user': { 'enabled': True, }, } response, content = bt.put(endpoint, headers=headers, body=json.dumps(body)) if (response['status'] != '200'): print "Reenable user failed!" return 1 return 0
def test_create_tenant_by_name(name): """ test administrator to create a tenant whose name is the giving name """ bt = BaseTest() endpoint = bt.get_service_endpoint('keystone') endpoint = endpoint + '/tenants' headers = { 'Content-Type': 'application/json', 'Accept': 'application/json' } body = { "tenant": { "name": name, "description": "test create tenant", "enabled": "true", }, } response, content = bt.post(endpoint, headers=headers, body=json.dumps(body)) if (response['status'] == '200'): return 0 return 1
def test_nova_launch_instance_multiple(username, password, tenant_name, image_name, flavor_id, number): """ test a user to create multiple servers with a same image """ bt_user = BaseTest(username, password, tenant_name) image = bt_user.get_image_by_name(image_name) if image == None: return 3 image_id = image['id'] server_ids = list() for i in range(0, number): server_name = 'test_' + str(i) resp, server = bt_user.servers_client.create_server(server_name, image_id, flavor_id) server_ids.append(server['id']) ret = 0 for server_id in server_ids: ret += bt_user._wait_for_server_status(server_id, 'ACTIVE', 180) if(ret > 0): return 1 bt_user.clean_servers('test_', True) return 0
def test_disable_user(username): """ test administrator to disable a ueser by giving user name """ bt = BaseTest() user = bt.get_user_by_name(username) if (user == None): return 3 user_id = user['id'] endpoint = bt.get_service_endpoint('keystone') endpoint = endpoint + '/users/' + user_id + '/enabled' headers = { 'Content-Type': 'application/json', 'Accept': 'application/json' } body = { 'user': { 'enabled': False, }, } response, content = bt.put(endpoint, headers=headers, body=json.dumps(body)) if (response['status'] != '200'): return 1 return 0
def test_nova_launch_instance_cycle(username, password, tenant_name, image_name, flavor_id, cycle_number, timeout): """ test a user to teminate a creating server for times in a specific time period """ user_bt = BaseTest(username, password, tenant_name) image = user_bt.get_image_by_name(image_name) if image == None: return 3 image_id = image['id'] server_name = 'test_cycle' for i in range(0, cycle_number): resp, server = user_bt.servers_client.create_server(server_name, image_id, flavor_id) resp, body = user_bt.servers_client.delete_server(server['id']) start = int(time.time()) while(server != None): time.sleep(5) timed_out = int(time.time()) - start >= timeout if timed_out: return 1 server = user_bt.get_server_by_name(server_name) return 0
def test_nova_change_quota_by_tenantname(tenant_name, key, value): """ test to change quotas of a tenant with a key and value """ support_keys = ('metadata_items', 'injected_file_content_bytes', 'injected_files', 'gigabytes', 'ram', 'floating_ips', 'security_group_rules', 'instances', 'volumes', 'cores', 'id', 'security_groups') if key not in support_keys: return 2 headers = {'Content-Type': 'application/json', 'Accept': 'application/json'} bt = BaseTest() endpoint = bt.get_service_endpoint('nova') tenant = bt.get_tenant_by_name(tenant_name) body = { 'quota_set': { key: value } } endpoint = endpoint + '/os-quota-sets/' + tenant['id'] #response, content = bt.get(endpoint) response, content = bt.put(endpoint, headers, body=json.dumps(body)) if response['status'] == '200': return 0 return 1
def test_login_by_username(username, password, tenant_name): """ test a user whose name is giving name to login first time """ bt_user = BaseTest(username, password, tenant_name) resp, body = bt_user.servers_client.list_servers() if resp['status'] == '200': return 0 return 1
def test_user_project_disabled_noaccess(username, password, tenant_name): """ test a user can not login anymore when its project is disabled """ try: bt_user = BaseTest(username, password, tenant_name) except exceptions.AuthenticationFailure: return 0 return 1
def test_nocleanup_nova_instance_admin_user(username, password, tenant_name, server_name): """ test a uer to teminiate a server created by administrator should fail """ bt = BaseTest() server = bt.get_server_by_name(server_name) res, meta = bt.servers_client.delete_server(server['id']) if res['status'] == '204': return 0 return 1
def test_user_chpasswd_login(username, password, tenant_name, newpassword): """ test change a users password and the user can login with new password """ ret = change_user_password(username, password, tenant_name, newpassword) if(ret != 0): return ret bt = BaseTest() endpoint = bt.get_service_endpoint('nova') endpoint = endpoint + '/servers' bt_user = BaseTest(username, newpassword, tenant_name) response, content = bt_user.get(endpoint) # restore the user's password change_user_password(username, newpassword, tenant_name, password) if response['status'] == '200': return 0 return 1
def test_nova_launch_instance_admin(server_name, image_name, flavor_id): """ test a administrato to create server by uploading image file and tiny flavor """ bt_admin = BaseTest() image = bt_admin.get_image_by_name(image_name) if image == None: return 3 image_id = image['id'] resp, server = bt_admin.servers_client.create_server( server_name, image_id, flavor_id) return bt_admin._wait_for_server_status(server['id'], 'ACTIVE', 180)
def test_nova_launch_instance_user(username, password, tenant_name, server_name, image_name, flavor_id): """ test a user to create instance by uploading image file and tiny flavor """ user_bt = BaseTest(username, password, tenant_name) image = user_bt.get_image_by_name(image_name) if image == None: return 3 image_id = image['id'] resp, server = user_bt.servers_client.create_server( server_name, image_id, flavor_id) return user_bt._wait_for_server_status(server['id'], 'ACTIVE', 180)
def test_cleanup_nova_instance_user(username, password, tenant_name, server_name): """ test a user to delete a server """ user_bt = BaseTest(username, password, tenant_name) server = user_bt.get_server_by_name(server_name) if server == None: return 3 response, content = user_bt.clean_servers(server_name) if response['status'] == '204': return 0 return 1
def upload_glance_image(file, disk_format='raw', container_format='bare', image_name='jeos1', timeout=60): """ Upload glance image file """ baseTest = BaseTest() token = baseTest.token endpoint = baseTest.get_service_endpoint('glance') endpoint = endpoint.rsplit('/', 1)[0] glance = Client('1', endpoint, token) start = int(time.time()) image = glance.images.create(name=image_name, disk_format=disk_format, container_format=container_format, is_public=True) image.update(data=open(file, 'rb')) return baseTest._wait_for_image_status(image.id, 'ACTIVE', 60)
def test_delete_user(username): """ test administrator to delete a user whose name is the giving name""" bt = BaseTest() user = bt.get_user_by_name(username) if (user == None): return 3 user_id = user['id'] endpoint = bt.get_service_endpoint('keystone') endpoint = endpoint + '/users/' + user_id response, content = bt.delete(endpoint) if (response['status'] == '200'): return 0 return 1
def test_delete_tenant_by_name(name): """ test administrator to delete a tenant whose name is the giving name""" bt = BaseTest() tenant = bt.get_tenant_by_name(name) if (tenant == None): print 'the tenant named %s is not existed!, skipped' % name return 3 tenant_id = tenant['id'] endpoint = bt.get_service_endpoint('keystone') endpoint = endpoint + '/tenants/' + tenant_id response, content = bt.delete(endpoint) if (response['status'] == '204'): return 0 return 1
def upload_glance_remote_image(file_path, disk_format='raw', container_format='bare', image_name='jeos_remote', timeout=60): """ Upload remote glance image file """ baseTest = BaseTest() token = baseTest.token endpoint = baseTest.get_service_endpoint('glance') endpoint = endpoint.rsplit('/', 1)[0] glance = Client('1', endpoint, token) meta = { 'name': image_name, 'is_public': True, 'disk_format': disk_format, 'container_format': container_format, 'copy_from': file_path, } start = int(time.time()) results = glance.images.create(**meta) image_id = results.id ret = baseTest._wait_for_image_status(image_id, 'ACTIVE', timeout) image_file = "/var/lib/glance/images/" + image_id # Ensure the image has been realy stroed in the glance location if ret == 0 and os.path.isfile(image_file): # clean the created image baseTest.clean_images(image_name) return 0 return 1
def delete_image(name, regmode): bt = BaseTest() bt.clean_images(name, regmode)
def test_cleanup_nova_instance_admin(server_name): """ test a administrator to teminate the server whose name is giving name """ bt = BaseTest() ret = bt.clean_servers(server_name)