def check_role_with_parametrization(clients, user, cluster_bundle: Bundle, provider_bundle: Bundle): """Check that update of role with parametrization leads to correct permissions update""" cluster, provider = cluster_bundle.cluster_create('clusteraster'), provider_bundle.provider_create('provideraider') role_name = "Role with parametrization" role = clients.admin.role_create( name=role_name, display_name=role_name, child=_form_children(clients.admin, BusinessRoles.EditClusterConfigurations), ) policy = clients.admin.policy_create(name="User policy", role=role, objects=[cluster], user=[user]) with new_client_instance(*TEST_USER_CREDENTIALS, clients.user.url) as user_client: user_cluster, user_provider = as_user_objects(user_client, cluster, provider) is_allowed(user_cluster, BusinessRoles.EditClusterConfigurations) is_denied(user_provider, BusinessRoles.EditProviderConfigurations) role.update(child=_form_children(clients.admin, BusinessRoles.EditProviderConfigurations)) with new_client_instance(*TEST_USER_CREDENTIALS, clients.user.url) as user_client: user_cluster, user_provider = as_user_objects(user_client, cluster, provider) is_denied(user_cluster, BusinessRoles.EditClusterConfigurations) is_denied(user_provider, BusinessRoles.EditProviderConfigurations) policy.update(object=[{'type': 'provider', 'id': provider.id}]) with new_client_instance(*TEST_USER_CREDENTIALS, clients.user.url) as user_client: user_cluster, user_provider = as_user_objects(user_client, cluster, provider) is_denied(user_cluster, BusinessRoles.EditClusterConfigurations) is_allowed(user_provider, BusinessRoles.EditProviderConfigurations) policy.delete() role.delete()
def test_get_cluster_list(self, cluster_bundle: Bundle): """Test get cluster list""" actual, expected = [], [] # Create list of clusters and fill expected list for name in utils.random_string_list(): cluster_bundle.cluster_create(name) expected.append(name) for cluster in cluster_bundle.cluster_list(): actual.append(cluster.name) with allure.step("Check cluster list"): assert actual == expected
def assert_provider_host_states(bundle: Bundle, statemap: dict): for pname, pv in statemap.items(): pstate = bundle.provider(name=pname).state pstate_expected = pv['state'] expect( pstate == pstate_expected, f"Provider \"{pname}\" is \"{pstate}\" while expected \"{pstate_expected}\"" ) for hname, hstate_expected in pv['hosts'].items(): hstate = bundle.provider(name=pname).host(fqdn=hname).state expect(hstate == hstate_expected, f'Host state is {hstate} while expected {hstate_expected}') assert_expectations()
def test_change_service_state_by_name(cluster_bundle: Bundle): expected_state = copy.deepcopy(INITIAL_CLUSTERS_STATE) assert_cluster_service_states(cluster_bundle, expected_state) with allure.step('Run action: set first service'): cluster_bundle.cluster(name='first').action( name='set_first_service').run().try_wait() expected_state['first']['services']['First'] = 'bimba!' assert_cluster_service_states(cluster_bundle, expected_state) with allure.step('Run action: set second service'): cluster_bundle.cluster(name='third').action( name='set_second_service').run().try_wait() expected_state['third']['services']['Second'] = 'state2' assert_cluster_service_states(cluster_bundle, expected_state)
def assert_cluster_service_states(bundle: Bundle, statemap: dict): for cname, clv in statemap.items(): cstate = bundle.cluster(name=cname).state cstate_expected = clv['state'] expect( cstate == cstate_expected, f"Cluster \"{cname}\" is \"{cstate}\" while expected \"{cstate_expected}\"" ) for sname, sstate_expected in clv['services'].items(): sstate = bundle.cluster(name=cname).service(name=sname).state expect(sstate == sstate_expected, (f"Cluster \"{cname}\" service \"{sname}\" state is " f"\"{sstate}\" while expected \"{sstate_expected}\"")) assert_expectations()
def test_creating_cluster_with_name_and_desc(self, cluster_bundle: Bundle): """Test create cluster with name and desc""" name, description = utils.random_string_list(2) cluster = cluster_bundle.cluster_create(name=name, description=description) with allure.step("Check created cluster"): assert cluster.name == name assert cluster.description == description
def test_verify_that_supported_type_is(self, cluster_bundle: Bundle, datatype, name): """Some mysterious type checking test""" with allure.step("Check stack config"): for item in cluster_bundle.cluster_prototype().config: if item["name"] == name: assert item["type"] == datatype
def _create_cluster_with_all_services(self, cluster_bundle: Bundle, hosts: Tuple[Host, Host, Host]) -> Cluster: """ Create cluster with three services Add three hosts on it Set components on hosts Run some actions """ with allure.step('Create cluster and add services'): cluster = cluster_bundle.cluster_create(name='With all services') cluster.config_set_diff({'very_important_flag': 1.6}) cheese_service = cluster.service_add(name=self.CHEESE_SERVICE) sauce_service = cluster.service_add(name=self.SAUCE_SERVICE) bread_service = cluster.service_add(name=self.BREAD_SERVICE) components = { self.MILK_COMPONENT: cheese_service.component(name=self.MILK_COMPONENT), self.TOMATO_COMPONENT: sauce_service.component(name=self.TOMATO_COMPONENT), self.LEMON_COMPONENT: sauce_service.component(name=self.LEMON_COMPONENT), self.SPICE_COMPONENT: sauce_service.component(name=self.SPICE_COMPONENT), } with allure.step('Add hosts'): for host in hosts: cluster.host_add(host) with allure.step('Run actions on the cluster, all components and services'): self._run_actions_on_components(cluster, sauce_service, components, hosts) _wait_for_tasks(service.action().run() for service in (cheese_service, sauce_service, bread_service)) cluster.action(name='make_sandwich').run().wait() return cluster
def test_change_provider_state(host_bundle: Bundle): expected_state = copy.deepcopy(INITIAL_HOST_STATE) assert_provider_host_states(host_bundle, expected_state) with allure.step('Run second host action: set state'): second = host_bundle.provider(name='second') second.action(name='set_state').run().try_wait() expected_state['second']['state'] = 'pstatex' assert_provider_host_states(host_bundle, expected_state)
def assert_provider_config(bundle: Bundle, statemap: dict): for pname, plv in statemap.items(): actual_cnf = bundle.provider(name=pname).config() expected_cnf = plv['config'] for k, v in expected_cnf.items(): expect( v == actual_cnf[k], 'Provider {} config "{}" is "{}" while expected "{}"'.format( pname, k, str(actual_cnf[k]), str(v))) for hname, host_expected_cnf in plv['hosts'].items(): host_actual_cnf = bundle.provider(name=pname).host( fqdn=hname).config() for k, v in host_expected_cnf.items(): expect( v == host_actual_cnf[k], 'Provider {} host {} config {} is {} while expected {}'. format(pname, hname, k, str(host_actual_cnf[k]), str(v))) assert_expectations()
def test_cluster_config(cluster_bundle: Bundle, keys_clusters): expected_state = copy.deepcopy(INITIAL_CLUSTERS_CONFIG) assert_cluster_config(cluster_bundle, expected_state) with allure.step('Check cluster keys'): for key, cname in keys_clusters: cluster = cluster_bundle.cluster(name=cname) cluster.action(name='cluster_' + key).run().try_wait() expected_state[cname]["config"][key] = NEW_VALUES[key] assert_cluster_config(cluster_bundle, expected_state)
def test_service_config(cluster_bundle: Bundle, keys_clusters_services): expected_state = copy.deepcopy(INITIAL_CLUSTERS_CONFIG) assert_cluster_config(cluster_bundle, expected_state) with allure.step('Check service keys'): for key, cname, sname in keys_clusters_services: cluster = cluster_bundle.cluster(name=cname) service = cluster.service(name=sname) service.action(name='service_' + key).run().try_wait() expected_state[cname]["services"][sname][key] = NEW_VALUES[key] assert_cluster_config(cluster_bundle, expected_state)
def test_provider_config(provider_bundle: Bundle): expected_state = copy.deepcopy(INITIAL_PROVIDERS_CONFIG) assert_provider_config(provider_bundle, expected_state) with allure.step('Check provider config'): for key, pname in sparse_matrix(KEYS, PROVIDERS): provider = provider_bundle.provider(name=pname) provider.action(name='provider_' + key).run().try_wait() expected_state[pname]["config"][key] = NEW_VALUES[key] assert_provider_config(provider_bundle, expected_state)
def test_change_service_state_by_name_from_another_service( cluster_bundle: Bundle): expected_state = copy.deepcopy(INITIAL_CLUSTERS_STATE) assert_cluster_service_states(cluster_bundle, expected_state) with allure.step('Run action: set first service'): second = cluster_bundle.cluster(name='first').service(name='Second') result = second.action(name='set_first_service').run().wait() with allure.step('Check job state'): assert result == "failed", "Job expected to be failed" assert_cluster_service_states(cluster_bundle, expected_state)
def assert_cluster_config(bundle: Bundle, statemap: dict): for cname, clv in statemap.items(): actual_cnf = bundle.cluster(name=cname).config() expected_cnf = clv['config'] for k, v in expected_cnf.items(): expect( v == actual_cnf[k], 'Cluster {} config "{}" is "{}" while expected "{}"'.format( cname, k, str(actual_cnf[k]), str(v))) for sname, service_expected_cnf in clv['services'].items(): service_actual_cnf = bundle.cluster(name=cname).service( name=sname).config() for k, v in service_expected_cnf.items(): expect( v == service_actual_cnf[k], 'Cluster {} service {} config {} is {} while expected {}'. format(cname, sname, k, str(service_actual_cnf[k]), str(v))) assert_expectations()
def test_change_host_from_provider_state(host_bundle: Bundle): expected_state = copy.deepcopy(INITIAL_HOST_STATE) assert_provider_host_states(host_bundle, expected_state) with allure.step('Run second host action: set host state'): second = host_bundle.provider(name='second') second.action(name='set_host_state').run(config={ "fqdn": "second_host2" }).try_wait() expected_state['second']['hosts']['second_host2'] = 'stateq' assert_provider_host_states(host_bundle, expected_state)
def test_change_host_state(host_bundle: Bundle): expected_state = copy.deepcopy(INITIAL_HOST_STATE) assert_provider_host_states(host_bundle, expected_state) first = host_bundle.provider(name='first') second = host_bundle.provider(name='second') with allure.step('Run first host action: set host'): first.host(fqdn='first_host1').action(name='set_host').run().try_wait() expected_state['first']['hosts']['first_host1'] = 'statez' assert_provider_host_states(host_bundle, expected_state) with allure.step('Run second host action: set host'): second.host(fqdn='second_host1').action( name='set_host').run().try_wait() expected_state['second']['hosts']['second_host1'] = 'statez' assert_provider_host_states(host_bundle, expected_state) with allure.step('Change second host'): second.host(fqdn='second_host3').action( name='set_host').run().try_wait() expected_state['second']['hosts']['second_host3'] = 'statez' assert_provider_host_states(host_bundle, expected_state)
def test_change_cluster_state(cluster_bundle: Bundle): expected_state = copy.deepcopy(INITIAL_CLUSTERS_STATE) assert_cluster_service_states(cluster_bundle, expected_state) first = cluster_bundle.cluster(name='first') second = cluster_bundle.cluster(name='second') third = cluster_bundle.cluster(name='third') with allure.step('Run second cluster action: set cluster'): second.action(name='set_cluster').run().try_wait() with allure.step('Check cluster state'): expected_state['second']['state'] = 'statey' assert_cluster_service_states(cluster_bundle, expected_state) with allure.step('Run first service action: set cluster'): first.service(name='First').action(name='set_cluster').run().try_wait() expected_state['first']['state'] = 'statey' assert_cluster_service_states(cluster_bundle, expected_state) with allure.step('Run second service action: set cluster'): third.service(name='Second').action( name='set_cluster').run().try_wait() expected_state['third']['state'] = 'statey' assert_cluster_service_states(cluster_bundle, expected_state)
def from_action(cls, action: Action, expected_status: str = "Undefined"): """Create instance from Action obj""" proto = Prototype(api=action._api, prototype_id=action.prototype_id) bundle = Bundle(api=action._api, bundle_id=proto.bundle_id) return cls( action_name=action.name, expected_status=expected_status, parent_name=_make_parent_name(proto), parent_type=proto.type, bundle_info=_get_bundle_id(bundle), called_from=os.getenv("PYTEST_CURRENT_TEST", "Undefined"), )
def create_complex_provider( self, provider_bundle: Bundle, template: str = 'complex-host', amount_of_hosts: int = 18 ) -> Tuple[Provider, Task]: """ Create provider, bunch of hosts via action (provide template if you want to use it more than 1 time). :returns: Create provider and hosts create tasks """ provider = provider_bundle.provider_create(name=f'Complex Provider {random_string(6)}') provider.config_set_diff({'very_important_flag': 54.4}) task = provider.action(name='create_hosts').run(config={'count': amount_of_hosts, 'template': template}) return provider, task
def test_host_config(provider_bundle: Bundle): expected_state = copy.deepcopy(INITIAL_PROVIDERS_CONFIG) assert_provider_config(provider_bundle, expected_state) with allure.step('Check host config'): for key, pname, host_idx in sparse_matrix(KEYS, PROVIDERS, [0, 1]): provider = provider_bundle.provider(name=pname) fqdn = list( INITIAL_PROVIDERS_CONFIG[pname]['hosts'].keys())[host_idx] host = provider.host(fqdn=fqdn) host.action(name='host_' + key).run().try_wait() expected_state[pname]["hosts"][fqdn][key] = NEW_VALUES[key] assert_provider_config(provider_bundle, expected_state)
def from_action(cls, action: Action): """Create instance from Action obj""" proto = Prototype(api=action._api, prototype_id=action.prototype_id) actions = [action["name"] for action in proto.actions] # pylint: disable=not-an-iterable bundle = Bundle(api=action._api, bundle_id=proto.bundle_id) return cls( actions=actions, parent_name=_make_parent_name(proto), parent_type=proto.type, bundle_info=_get_bundle_id(bundle), )
def _create_cluster_with_hosts( self, cluster_bundle: Bundle, hosts: Tuple[Host, ...], service_name: str = SAUCE_SERVICE ) -> Cluster: """ Create cluster with given amount of hosts. Cluster is not configured (can't run actions on it). Cluster has 1 service added. """ cluster = cluster_bundle.cluster_create(name='Cluster with hosts') cluster.service_add(name=service_name) for host in hosts: cluster.host_add(host) return cluster
def test_another_service_from_service_by_name(cluster_bundle: Bundle, keys_clusters_services): expected_state = copy.deepcopy(INITIAL_CLUSTERS_CONFIG) assert_cluster_config(cluster_bundle, expected_state) with allure.step('Check another service from service by name'): for key, cname, sname in keys_clusters_services: if sname == "Second": continue second = cluster_bundle.cluster(name=cname).service(name='Second') result = second.action(name='service_name_' + sname + '_' + key).run().wait() assert result == "failed", "Job expected to be failed" assert_cluster_config(cluster_bundle, expected_state)
def _create_cluster_with_config_history(self, bundle: Bundle) -> Cluster: """Create cluster with one service and config history""" def get_random_config_map() -> dict: return { 'a_lot_of_text': {'simple_string': random_string(25), 'file_pass': random_string(16)}, 'from_doc': { 'memory_size': random.randint(2, 64), 'person': { 'name': random_string(13), 'age': str(random.randint(14, 80)), 'custom_field': random_string(12), }, }, 'country_codes': [ {'country': random_string(12), 'code': int(random.randint(1, 200))} for _ in range(4) ], } def get_component_random_config_map() -> dict: return {'illicium': random.random()} config_change_iterations = 100 cluster = bundle.cluster_create(name='Config history') cluster.config_set_diff({'very_important_flag': 1.6}) with allure.step(f"Change cluster's config {config_change_iterations} times"): for _ in range(config_change_iterations): cluster.config_set_diff(get_random_config_map()) with allure.step(f"Add service and change its config {config_change_iterations} times"): service = cluster.service_add(name=self.SAUCE_SERVICE) for _ in range(config_change_iterations): service.config_set_diff(get_random_config_map()) with allure.step(f"Change component's config {config_change_iterations} times"): component = service.component() for _ in range(config_change_iterations): component.config_set_diff(get_component_random_config_map()) return cluster
def second_p(bundle: Bundle): """Second provider""" return bundle.provider(name="second_p")
def third_p(bundle: Bundle): """Third provider""" return bundle.provider(name="third_p")
def forth_p(bundle: Bundle): """Forth provider""" return bundle.provider(name="forth_p")
def provider(provider_bundle: Bundle) -> Provider: """Create provider""" return provider_bundle.provider_create(name=utils.random_string())
def cluster(cluster_bundle: Bundle) -> Cluster: """Create cluster""" return cluster_bundle.cluster_create(name=utils.random_string())