class TestCreateUser(): @classmethod def setup_class(self): self.api_utils = RestUtils() def test_create_message_with_all_parameters(self): body = create_default_forum_message_body() response = self.api_utils.create_message_forum(body=body) assert_true(response.ok) assert_equals(response.content, 'message created') def test_create_message_with_none_parameters(self): body = create_default_forum_message_body() body[THEME] = None response = self.api_utils.create_message_forum(body=body) assert_equals(response.status_code, 400) response_body = assert_json_format(response) assert_equals(response_body[MESSAGES], 'Theme not valid') def test_create_message_without_some_parameter(self): for key in FORUM_KEYS: body = create_default_forum_message_body() body = delete_keys_from_dict(dict_del=body, key=key) response = self.api_utils.create_message_forum(body=body) assert_equals(response.status_code, 400) response_body = assert_json_format(response) assert_equals(response_body[MESSAGES], 'some parameter is not correct') def test_create_message_with_incorrect_json_format(self): body = 'Not JSON format' response = self.api_utils.create_message_forum(body=body) assert_equals(response.status_code, 400) response_body = assert_json_format(response) assert_equals(response_body[MESSAGES], 'some parameter is not correct') def test_create_message_with_incorrect_theme(self): theme_list = ['QA', 'security', '', 'AUTOMATION', '"testing"'] for theme in theme_list: body = create_default_forum_message_body() body[THEME] = theme response = self.api_utils.create_message_forum(body=body) assert_equals(response.status_code, 400) response_body = assert_json_format(response) assert_equals(response_body[MESSAGES], 'Theme not valid')
def wait_for_task_finished(vdc_id, task_id, seconds=WAIT_FOR_OPERATION, status_to_be_finished=None, headers=None): rest_utils = RestUtils() correct_status = False for count in range(seconds): response = rest_utils.retrieve_task(headers=headers, vdc_id=vdc_id, task_id=task_id) response_body = response_body_to_dict(response, headers[ACCEPT_HEADER], with_attributes=True, xml_root_element_name=TASK) status = response_body[TASK_STATUS] print 'Waiting for task status. TIME: {}\n STATUS: {}'.format(count, status) if status == status_to_be_finished: correct_status = True break elif status != TASK_STATUS_VALUE_RUNNING: break time.sleep(1) return correct_status
def wait_for_task_finished(vdc_id, task_id, seconds=WAIT_FOR_OPERATION, status_to_be_finished=None, headers=None): rest_utils = RestUtils() correct_status = False for count in range(seconds): response = rest_utils.retrieve_task(headers=headers, vdc_id=vdc_id, task_id=task_id) response_body = response_body_to_dict(response, headers[ACCEPT_HEADER], with_attributes=True, xml_root_element_name=TASK) status = response_body[TASK_STATUS] print '[TASK] Waiting for task status. CHECK: {} - STATUS: {}'.format(count, status) if status == status_to_be_finished: correct_status = True break elif status != TASK_STATUS_VALUE_RUNNING: break time.sleep(5) if correct_status is False: print "[TASK] Response body:", response_body return correct_status
__author__ = 'arobres' # -*- coding: utf-8 -*- from lettuce import step, world, before, after from commons.authentication import get_token from commons.rest_utils import RestUtils from commons.product_body import default_product, create_default_metadata_or_attributes_list, create_product_release from commons.utils import dict_to_xml, set_default_headers, xml_to_dict from commons.constants import CONTENT_TYPE, PRODUCT_NAME, ACCEPT_HEADER, AUTH_TOKEN_HEADER, CONTENT_TYPE_JSON, LONG_ID, \ VERSION, PRODUCT_RELEASE from nose.tools import assert_equals, assert_true api_utils = RestUtils() @before.each_feature def setup_feature(feature): world.token_id, world.tenant_id = get_token() @before.each_scenario def setup_scenario(scenario): world.headers = set_default_headers(world.token_id, world.tenant_id) api_utils.delete_all_testing_products(world.headers) world.attributes = None world.metadatas = None @step(u'Given a created product with name "([^"]*)"') def given_a_created_product_with_name_group1(step, product_id):
def setup_class(self): self.api_utils = RestUtils()
class TestCreateUser(): @classmethod def setup_class(self): self.api_utils = RestUtils() def create_body(self, name=None, username=None, pwd=None, role=None, email=None): body = {} if name is not None: body['name'] = name if username is not None: body['username'] = username if pwd is not None: body['password'] = pwd if role is not None: body['role'] = role if email is not None: body['email'] = email return body def test_create_user_with_all_parameters(self): name = 'toni' username = '******' pwd = 'easypwd' role = 'QA' email = '*****@*****.**' response = self.api_utils.create_user(name=name, username=username, pwd=pwd, role=role, email=email) assert_true(response.ok) assert_equals(response.content, 'user created') def test_create_user_with_none_values(self): name = 'toni' username = '******' pwd = 'easypwd' role = 'QA' email = '*****@*****.**' response = self.api_utils.create_user(name=name, username=username, pwd=pwd, role=role) assert_true(response.ok) assert_equals(response.content, 'user created') def test_create_user_without_some_parameter(self): name = 'toni' username = '******' pwd = 'easypwd' role = 'QA' email = '*****@*****.**' body = self.create_body(name, username, pwd, role) response = self.api_utils.create_user(body=body) assert_equals(response.status_code, 400) response_body = response.json() assert_equals(response_body['message'], 'some parameter is not correct') body = self.create_body(username, pwd, role, email) response = self.api_utils.create_user(body=body) assert_equals(response.status_code, 400) response_body = response.json() assert_equals(response_body['message'], 'some parameter is not correct') def test_create_user_invalid_json_format(self): body = 'hello, is not a JSON' response = self.api_utils.create_user(body=body) assert_equals(response.status_code, 400) response_body = response.json() assert_equals(response_body['message'], 'some parameter is not correct') def test_not_existant_role(self): name = 'toni' username = '******' pwd = 'easypwd' role = 'tester' email = '*****@*****.**' response = self.api_utils.create_user(name=name, username=username, pwd=pwd, role=role, email=email) assert_equals(response.status_code, 400) response_body = response.json() assert_equals(response_body['message'], 'Role not valid')
# # For those usages not covered by the Apache version 2.0 License please # contact with [email protected] __author__ = 'jfernandez' from lettuce import world, before, after from commons.terrain_steps import setup_feature, setup_scenario, setup_outline, tear_down from commons.provisioning_steps import ProvisioningSteps from commons.rest_utils import RestUtils from commons.configuration import CONFIG_VM_HOSTNAME from commons.fabric_utils import execute_chef_client, execute_puppet_agent, remove_chef_client_cert_file, \ execute_chef_client_stop, execute_puppet_agent_stop, remove_puppet_agent_cert_file provisioning_steps = ProvisioningSteps() rest_utils = RestUtils() @before.each_feature def before_each_feature(feature): """ Hook: Will be executed before each feature. Configures global vars and gets token from keystone. Launch agents (puppet and chef) in the target VM """ setup_feature(feature) execute_chef_client() execute_puppet_agent() @before.each_scenario def before_each_scenario(scenario):
class ProvisioningSteps(): api_utils = RestUtils() def __init__(self): None @staticmethod def and_a_vm_with_this_parameters(step): """ Given step. Configure a VM parameters. Load data from config file if value is "CONFIG_FILE" Set values from hash to world: world.vm_hostname world.vm_ip world.vm_fqn world.ostype :param step: Lettuce step data :return: Values in lettuce world. """ if "hostname" in step.hashes[0]: hostname = step.hashes[0]['hostname'] world.vm_hostname = CONFIG_VM_HOSTNAME if CONFIG_FILE == hostname else hostname if "ip" in step.hashes[0]: ip = step.hashes[0]['ip'] world.vm_ip = CONFIG_VM_IP if CONFIG_FILE == ip else ip if "fqn" in step.hashes[0]: fqn = step.hashes[0]['fqn'] world.vm_fqn = CONFIG_VM_FQN if CONFIG_FILE == fqn else fqn if "ostype" in step.hashes[0]: world.ostype = step.hashes[0]['ostype'] def i_install_the_product_in_the_vm(self, step): """ Install a product in a virtual machine. :param step: Lettuce step data :return: The response is set into world.response """ if world.instance_attributes is None: body_model = simple_installation_body(product_name=world.product_name, product_version=world.product_version, hostname=world.vm_hostname, ip=world.vm_ip, fqn=world.vm_fqn, ostype=world.vm_ostype) else: body_model = installation_body_with_attributes(product_name=world.product_name, product_version=world.product_version, hostname=world.vm_hostname, ip=world.vm_ip, fqn=world.vm_fqn, ostype=world.vm_ostype, attributes=world.instance_attributes) body = body_model_to_body_request(body_model, world.headers[CONTENT_TYPE], body_model_root_element=PRODUCT_INSTANCE) world.response = self.api_utils.install_product(headers=world.headers, vdc_id=world.tenant_id, body=body) @staticmethod def task_is_created(step): """ Checks if the task is created with the correct info :param step: Lettuce step :return: In world.task_id, the task id created """ assert_true(world.response.ok, 'RESPONSE: {}'.format(world.response.content)) response_headers = world.response.headers assert_in(response_headers[CONTENT_TYPE], world.headers[ACCEPT_HEADER], 'RESPONSE HEADERS: {}'.format(world.response.headers)) response_body = response_body_to_dict(world.response, world.headers[ACCEPT_HEADER], with_attributes=True, xml_root_element_name=TASK) assert_equals(response_body[TASK_STATUS], TASK_STATUS_VALUE_RUNNING) assert_in(world.product_name, response_body[DESCRIPTION]) assert_in(world.vm_hostname, response_body[DESCRIPTION]) assert_equals(world.tenant_id, response_body[TASK_VDC]) m = re.search('/task/(.*)$', response_body[TASK_HREF]) world.task_id = m.group(1) def the_product_is_instantiated(self, step): """ Checks if the product is instantiated with the correct data (values from world) :param step: Lettuce step data :return: world.instance_status with the installation status """ world.instance_id = generate_product_instance_id(world.vm_fqn, world.product_name, world.product_version) response = self.api_utils.retrieve_product_instance(headers=world.headers, vdc_id=world.tenant_id, product_instance_id=world.instance_id) assert_true(response.ok, 'RESPONSE: {}'.format(response)) response_body = response_body_to_dict(response, world.headers[ACCEPT_HEADER], xml_root_element_name=PRODUCT_INSTANCE) assert_equals(response_body[PRODUCT_INSTANCE_NAME], world.instance_id) assert_true(response_body[PRODUCT_INSTANCE_STATUS] != "") assert_equals(response_body[PRODUCT_INSTANCE_VM][PRODUCT_INSTANCE_VM_FQN], world.vm_fqn) assert_equals(response_body[PRODUCT_INSTANCE_VM][PRODUCT_INSTANCE_VM_HOSTNAME], world.vm_hostname) ip_aux = "" if world.vm_ip is None else world.vm_ip assert_equals(response_body[PRODUCT_INSTANCE_VM][PRODUCT_INSTANCE_VM_IP], ip_aux) assert_equals(response_body[PRODUCT][VERSION], world.product_version) assert_equals(response_body[PRODUCT][PRODUCT_NAME], world.product_name) # If the instance has been created with attributes, check it. if world.instance_attributes is not None: # Check if attributes have got type. # Else, add plain type before check it (default type) for attribute in world.instance_attributes: if ATTRIBUTE_TYPE not in attribute: attribute.update({ATTRIBUTE_TYPE: ATTRIBUTE_TYPE_PLAIN}) world.instance_attributes = world.instance_attributes[0] \ if len(world.instance_attributes) == 1 else world.instance_attributes assert_equals(response_body[PRODUCT_INSTANCE_ATTRIBUTES], world.instance_attributes) world.instance_status = response_body[PRODUCT_INSTANCE_STATUS] def the_product_installation_status_is(self, step, status): """ Checks the product instalation status. :param step: Lettuce data :param status: Expected status to check :return: """ if world.instance_status is None: world.instance_id = "{}_{}_{}".format(world.vm_fqn, world.product_name, world.product_version) response = self.api_utils.retrieve_product_instance(headers=world.headers, vdc_id=world.tenant_id, product_instance_id=world.instance_id) assert_true(response.ok, 'RESPONSE: {}'.format(response)) response_body = response_body_to_dict(response, world.headers[ACCEPT_HEADER], xml_root_element_name=PRODUCT_INSTANCE) world.instance_status = response_body[PRODUCT_INSTANCE_STATUS] assert_equals(world.instance_status, status) def i_uninstall_a_installed_product_and_release(self, step): """ Uninstal a product. Use data from world world.vm_fqn, world.product_name, world.product_version, world.tenant_id :return: Response in world.response and instance_id in world.instance_id """ world.instance_id = generate_product_instance_id(world.vm_fqn, world.product_name, world.product_version) world.response = self.api_utils.uninstall_product_by_product_instance_id(headers=world.headers, vdc_id=world.tenant_id, product_instance_id=world.instance_id) @staticmethod def the_task_has_finished_with_status_group1(step, status): """ Wait for task's result and check that this one is the expected status :param step: Lettuce steps :param status: Expected status :return: """ finished = wait_for_task_finished(vdc_id=world.tenant_id, task_id=world.task_id, status_to_be_finished=status, headers=world.headers) assert_true(finished, 'Task is not in the correct status. Expected: {}'.format(status))
class ProductSteps (): api_utils = RestUtils() def __init__(self): None def a_created_product_with_name(self, step, product_name, metadatas=None): """ Creates new product in SDC if not exists, using default values. - Product parameters: * Metadata: Default values if metadatas is None * Attributes: if exist in world.attributes - list - Assertions: * Product response is OK :param step: Lettuce step :param product_name: The name of the product :param metadatas: Metadata list to be used in the new product :return: None """ response = self.api_utils.retrieve_product(headers=world.headers, product_id=product_name) if response.ok: print "WARNING: Product %s already exists. It will not be created." % product_name else: metadata_list = DEFAULT_METADATA[METADATA] if metadatas is None else metadatas if world.attributes is None: body_model = default_product(name=product_name, metadata=metadata_list) else: body_model = product_with_all_parameters(name=product_name, description='QA Test', metadata=metadata_list, attributes=world.attributes) body = body_model_to_body_request(body_model, world.headers[CONTENT_TYPE], body_model_root_element=PRODUCT) response = self.api_utils.add_new_product(headers=world.headers, body=body) assert_true(response.ok, response.content) def a_created_release(self, step, product_name, product_version): """ Creates new release for the product if not exists, using default values. - Assertions: * Release response is OK :param step: Lettuce step :param product_name: The name of the product :param product_version: The version for the product release :return: None """ response = self.api_utils.retrieve_product_release_information(headers=world.headers, product_id=product_name, version=product_version) if response.ok: print "WARNING: Version %s for the product %s already exists. It will not be created." % \ (product_version, product_name) else: body_model = create_product_release(version=product_version) body = body_model_to_body_request(body_model, world.headers[CONTENT_TYPE], body_model_root_element=PRODUCT_RELEASE) response = self.api_utils.add_product_release(headers=world.headers, body=body, product_id=product_name) assert_true(response.ok, response.content) def a_created_product_with_name_and_release(self, step, product_name, product_version): """ Creates new product in SDC with a release if not exists, using default values. - Product parameters: * Metadata: Default values * Attributes: if exists in world.attributes - list - Assertions: * Product response is OK * Release response is OK :param step: Lettuce step :param product_name: The name of the product :param product_version: The version for the product release :return: None """ # Create product self.a_created_product_with_name(step, product_name) # Create release for that product self.a_created_release(step, product_name, product_version) def a_created_product_with_name_and_release_with_metadatas(self, step, product_name, product_version, metadatas): """ Creates new product with metadatas in SDC with a release if not exists, using default values. - Product parameters: * Attributes: if exists in world.attributes - list - Assertions: * Product response is OK * Release response is OK :param step: Lettuce step :param product_name: The name of the product :param product_version: The version for the product release :param metadatas: The metadatas to be used for the new product :return: None """ # Create product self.a_created_product_with_name(step, product_name, metadatas=metadatas) # Create release for that product self.a_created_release(step, product_name, product_version) def a_created_product_with_name_and_release_list(self, step, product_name, product_version_list): """ Creates new product in SDC with a release if not exists, using default values. - Product parameters: * Metadata: Default values * Attributes: if exists in world.attributes - list - Assertions: * Product response is OK * Release response is OK :param step: Lettuce step :param product_name: The name of the product :param product_version_list: The list of versions for the product to be created - list :return: None """ # Create product self.a_created_product_with_name(step, product_name) for product_version in product_version_list: # Create release for that product self.a_created_release(step, product_name, product_version)
class TestCreateUser(): @classmethod def setup_class(self): self.api_utils = RestUtils() def create_body(self, theme=None, subject=None, message=None): body = {THEME: theme, SUBJECT: subject, MESSAGES: message} return body def create_default_body(self): theme = 'testing' subject = 'QA movie' message = 'New movie about QA people!' return self.create_body(theme=theme, subject=subject, message=message) def test_create_message_with_all_parameters(self): body = self.create_default_body() response = self.api_utils.create_message_forum(body=body) assert_true(response.ok) assert_equals(response.content, 'message created') def test_create_message_with_none_parameters(self): body = self.create_default_body() body[THEME] = None response = self.api_utils.create_message_forum(body=body) assert_equals(response.status_code, 400) response_body = assert_json_format(response) assert_equals(response_body[MESSAGES], 'Theme not valid') def test_create_message_without_some_parameter(self): for key in FORUM_KEYS: body = self.create_default_body() body = delete_keys_from_dict(dict_del=body, key=key) response = self.api_utils.create_message_forum(body=body) assert_equals(response.status_code, 400) response_body = assert_json_format(response) assert_equals(response_body[MESSAGES], 'some parameter is not correct') def test_create_message_with_incorrect_json_format(self): body = 'Not JSON format' response = self.api_utils.create_message_forum(body=body) assert_equals(response.status_code, 400) response_body = assert_json_format(response) assert_equals(response_body[MESSAGES], 'some parameter is not correct') def test_create_message_with_incorrect_theme(self): theme_list = ['QA', 'Security', '', 'AUTOMATION', '"testing"'] for theme in theme_list: body = self.create_default_body() body[THEME] = theme response = self.api_utils.create_message_forum(body=body) assert_equals(response.status_code, 400) response_body = assert_json_format(response) assert_equals(response_body[MESSAGES], 'Theme not valid')
class TestCreateUser(): @classmethod def setup_class(self): self.api_utils = RestUtils() self.api_utils.reset_mock() def test_create_user_with_all_parameters(self): response = self.api_utils.create_user(body=create_default_user()) assert_true(response.ok) assert_equals(response.content, 'user created') def test_create_user_with_none_values(self): name = None username = '******' pwd = 'easypwd' role = 'QA' email = '*****@*****.**' request_body = create_user_body(name=name, username=username, pwd=pwd, role=role, email=email) response = self.api_utils.create_user(body=request_body) assert_true(response.ok) assert_equals(response.content, 'user created') def test_create_user_without_some_parameter(self): request_body = create_default_user() request_body = delete_keys_from_dict(request_body, NAME) response = self.api_utils.create_user(body=request_body) assert_equals(response.status_code, 400) response_body = response.json() assert_equals(response_body['message'], 'some parameter is not correct') request_body = create_default_user() request_body = delete_keys_from_dict(request_body, USERNAME) response = self.api_utils.create_user(body=request_body) assert_equals(response.status_code, 400) response_body = response.json() assert_equals(response_body['message'], 'some parameter is not correct') def test_create_user_invalid_json_format(self): body = 'hello, is not a JSON' response = self.api_utils.create_user(body=body) assert_equals(response.status_code, 400) response_body = response.json() assert_equals(response_body['message'], 'some parameter is not correct') def test_not_existent_role(self): name = 'toni' username = '******' pwd = 'easypwd' role = 'tester' email = '*****@*****.**' request_body = create_user_body(name=name, username=username, pwd=pwd, role=role, email=email) response = self.api_utils.create_user(body=request_body) assert_equals(response.status_code, 400) response_body = response.json() assert_equals(response_body['message'], 'Role not valid') def test_existent_user(self): response = self.api_utils.create_user(body=create_default_user()) assert_equals(response.status_code, 409) response_body = response.json() assert_equals(response_body['message'], 'User exist!')
def setup_class(self): self.api_utils = RestUtils() self.api_utils.reset_mock()