def _init_test_vars(config, logger=NULL_LOGGER): """Initialize all the environment variables that are used for test. :param dict config: """ global TEMPLATE_DEFINITIONS, TEARDOWN_INSTALLATION, TEARDOWN_CLUSTERS, \ TEST_ALL_TEMPLATES, TEST_ORG, TEST_VDC, TEST_NETWORK, \ USERNAME_TO_CLUSTER_NAME, SHOULD_INSTALL_PREREQUISITES, \ IS_CSE_SERVER_RUNNING, TEST_CLUSTER_UPGRADES USERNAME_TO_CLUSTER_NAME = { SYS_ADMIN_NAME: SYS_ADMIN_TEST_CLUSTER_NAME, CLUSTER_ADMIN_NAME: CLUSTER_ADMIN_TEST_CLUSTER_NAME, CLUSTER_AUTHOR_NAME: CLUSTER_AUTHOR_TEST_CLUSTER_NAME } test_config = config['test'] if test_config is not None: TEARDOWN_INSTALLATION = test_config.get('teardown_installation', True) TEARDOWN_CLUSTERS = test_config.get('teardown_clusters', True) TEST_ALL_TEMPLATES = test_config.get('test_all_templates', False) TEST_ORG = test_config.get('org', 'test-org') TEST_VDC = test_config.get('vdc', 'test-vdc') TEST_NETWORK = test_config.get('network', 'test-network') rtm = RemoteTemplateManager( config['broker']['remote_template_cookbook_url'], legacy_mode=config['service']['legacy_mode']) template_cookbook = rtm.get_filtered_remote_template_cookbook() TEMPLATE_DEFINITIONS = template_cookbook['templates'] if not TEST_ALL_TEMPLATES: specified_templates_str = test_config.get('test_templates', "") specified_templates = specified_templates_str.split(",") specified_templates_def = [] for template in specified_templates: tokens = template.split(":") # ToDo: log missing/bad specified templates if len(tokens) == 2: template_name = tokens[0] template_revision = tokens[1] for template_def in TEMPLATE_DEFINITIONS: if (template_name, int(template_revision)) == ( template_def['name'], int(template_def['revision'])): # noqa: E501 specified_templates_def.append(template_def) break TEMPLATE_DEFINITIONS = specified_templates_def SHOULD_INSTALL_PREREQUISITES = \ test_config.get('should_install_prerequisites', True) IS_CSE_SERVER_RUNNING = test_config.get('is_cse_server_running', False) TEST_CLUSTER_UPGRADES = \ test_config.get('test_cluster_upgrades', False) if TEST_CLUSTER_UPGRADES: _populate_template_upgrade_paths(config, logger=NULL_LOGGER)
def _populate_template_upgrade_paths(config, logger=NULL_LOGGER): global TEMPLATE_UPGRADE_PATH_LIST rtm = RemoteTemplateManager( config['broker']['remote_template_cookbook_url'], legacy_mode=config['service']['legacy_mode']) # The following function call will filter out the templates based on # cse version as well. template_cookbook = rtm.get_filtered_remote_template_cookbook() logger.debug(f"Template cookbook: {template_cookbook}") all_template_definitions = template_cookbook['templates'] # filter out photon templates all_ubuntu_templates = \ [template_definition for template_definition in all_template_definitions if 'ubuntu' in template_definition['name']] # noqa: E501 logger.debug(f"All template definitions: {all_ubuntu_templates}") RT_KEYS = get_template_descriptor_keys(rtm.cookbook_version) template_name_to_desc = {} for t in all_ubuntu_templates: template_name_to_desc[t['name']] = t max_ubuntu_template = all_ubuntu_templates[0] max_ubuntu_template_version = semantic_version.Version( max_ubuntu_template[RT_KEYS.KUBERNETES_VERSION]) # noqa: E501 for t in all_ubuntu_templates[1:]: if semantic_version.Version( t[RT_KEYS.KUBERNETES_VERSION] ) > max_ubuntu_template_version: # noqa: E501 max_ubuntu_template = t max_ubuntu_template_version = semantic_version.Version( t[RT_KEYS.KUBERNETES_VERSION]) # noqa: E501 paths_arr = [[max_ubuntu_template]] # this list will contain all the bottom to top upgrade paths. TEMPLATE_UPGRADE_PATH_LIST = [] while len(paths_arr) > 0: path = paths_arr.pop() prev_template = path[0] for t_name in prev_template.get('upgrade_from', []): if t_name not in template_name_to_desc: # Add existing path to the final arr TEMPLATE_UPGRADE_PATH_LIST.append(path) else: t_desc = template_name_to_desc.get(t_name) if t_name != prev_template['name']: new_path = [t_desc] + path paths_arr.append(new_path) logger.debug(f"Collected upgrade path list: {TEMPLATE_UPGRADE_PATH_LIST}")
def _validate_broker_config( broker_dict, legacy_mode=False, msg_update_callback=NullPrinter(), logger_debug=NULL_LOGGER ): """Ensure that 'broker' section of config is correct. Checks that 'broker' section of config has correct keys and value types. Also checks that 'default_broker' property is a valid template. :param dict broker_dict: 'broker' section of config file as a dict. :param utils.ConsoleMessagePrinter msg_update_callback: Callback object. :raises KeyError: if @broker_dict has missing properties. :raises TypeError: if the value type for a @broker_dict property is incorrect. :raises ValueError: if 'ip_allocation_mode' is not 'dhcp' or 'pool'. Or if remote_template_cookbook_url is invalid. """ check_keys_and_value_types( broker_dict, SAMPLE_BROKER_CONFIG['broker'], location="config file 'broker' section", msg_update_callback=msg_update_callback ) valid_ip_allocation_modes = [ 'dhcp', 'pool' ] if broker_dict['ip_allocation_mode'] not in valid_ip_allocation_modes: raise ValueError( "IP allocation mode is " f"'{broker_dict['ip_allocation_mode']}' when it " "should be either 'dhcp' or 'pool'" ) rtm = RemoteTemplateManager( remote_template_cookbook_url=broker_dict['remote_template_cookbook_url'], # noqa: E501 legacy_mode=legacy_mode, logger=logger_debug ) remote_template_cookbook = rtm.get_filtered_remote_template_cookbook() if not remote_template_cookbook: raise Exception("Remote template cookbook is invalid.")
def _init_test_vars(config, logger=NULL_LOGGER): """Initialize all the environment variables that are used for test. :param dict test_config: test section of config.yaml """ global TEMPLATE_DEFINITIONS, TEARDOWN_INSTALLATION, TEARDOWN_CLUSTERS, \ TEST_ALL_TEMPLATES, TEST_ORG, TEST_VDC, TEST_NETWORK test_config = config['test'] if test_config is not None: TEARDOWN_INSTALLATION = test_config.get('teardown_installation', True) TEARDOWN_CLUSTERS = test_config.get('teardown_clusters', True) TEST_ALL_TEMPLATES = test_config.get('test_all_templates', False) TEST_ORG = test_config.get('org', 'test-org') TEST_VDC = test_config.get('vdc', 'test-vdc') TEST_NETWORK = test_config.get('network', 'test-network') rtm = RemoteTemplateManager( config['broker']['remote_template_cookbook_url'], legacy_mode=config['service']['legacy_mode']) template_cookbook = rtm.get_filtered_remote_template_cookbook() TEMPLATE_DEFINITIONS = template_cookbook['templates'] if not TEST_ALL_TEMPLATES: specified_templates_str = test_config.get('test_templates', "") specified_templates = specified_templates_str.split(",") specified_templates_def = [] for template in specified_templates: tokens = template.split(":") # ToDo: log missing/bad specified templates if len(tokens) == 2: template_name = tokens[0] template_revision = tokens[1] for template_def in TEMPLATE_DEFINITIONS: if (template_name, int(template_revision)) == ( template_def['name'], int(template_def['revision'])): # noqa: E501 specified_templates_def.append(template_def) break TEMPLATE_DEFINITIONS = specified_templates_def
def init_environment(config_filepath=BASE_CONFIG_FILEPATH): """Set up module variables according to config dict. :param str config_filepath: """ global AMQP_USERNAME, AMQP_PASSWORD, CLIENT, ORG_HREF, VDC_HREF, \ CATALOG_NAME, TEARDOWN_INSTALLATION, TEARDOWN_CLUSTERS, \ TEMPLATE_DEFINITIONS, TEST_ALL_TEMPLATES, SYS_ADMIN_LOGIN_CMD, \ ORG_ADMIN_LOGIN_CMD, K8_AUTHOR_LOGIN_CMD, USERNAME_TO_LOGIN_CMD, \ USERNAME_TO_CLUSTER_NAME, TEST_ORG_HREF, TEST_VDC_HREF, \ VCD_API_VERSION_TO_USE, TEMPLATE_COOKBOOK_VERSION config = testutils.yaml_to_dict(config_filepath) rtm = \ RemoteTemplateManager(config['broker']['remote_template_cookbook_url'], legacy_mode=config['service']['legacy_mode']) template_cookbook = rtm.get_filtered_remote_template_cookbook() TEMPLATE_COOKBOOK_VERSION = rtm.cookbook_version TEMPLATE_DEFINITIONS = template_cookbook['templates'] rtm.download_all_template_scripts(force_overwrite=True) CLIENT = Client(config['vcd']['host'], api_version=config['vcd']['api_version'], verify_ssl_certs=config['vcd']['verify']) credentials = BasicLoginCredentials(config['vcd']['username'], shared_constants.SYSTEM_ORG_NAME, config['vcd']['password']) CLIENT.set_credentials(credentials) VCD_API_VERSION_TO_USE = config['vcd']['api_version'] CATALOG_NAME = config['broker']['catalog'] AMQP_USERNAME = config['amqp']['username'] AMQP_PASSWORD = config['amqp']['password'] SYS_ADMIN_LOGIN_CMD = f"login {config['vcd']['host']} system " \ f"{config['vcd']['username']} " \ f"-iwp {config['vcd']['password']} " \ f"-V {VCD_API_VERSION_TO_USE}" ORG_ADMIN_LOGIN_CMD = f"login {config['vcd']['host']} " \ f"{TEST_ORG}" \ f" {ORG_ADMIN_NAME} -iwp {ORG_ADMIN_PASSWORD} " \ f"-V {VCD_API_VERSION_TO_USE}" K8_AUTHOR_LOGIN_CMD = f"login {config['vcd']['host']} " \ f"{TEST_ORG} " \ f"{K8_AUTHOR_NAME} -iwp {K8_AUTHOR_PASSWORD}" \ f" -V {VCD_API_VERSION_TO_USE}" USERNAME_TO_LOGIN_CMD = { 'sys_admin': SYS_ADMIN_LOGIN_CMD, 'org_admin': ORG_ADMIN_LOGIN_CMD, 'k8_author': K8_AUTHOR_LOGIN_CMD } USERNAME_TO_CLUSTER_NAME = { 'sys_admin': SYS_ADMIN_TEST_CLUSTER_NAME, 'org_admin': ORG_ADMIN_TEST_CLUSTER_NAME, 'k8_author': K8_AUTHOR_TEST_CLUSTER_NAME } # hrefs for Org and VDC that hosts the catalog org = pyvcloud_utils.get_org(CLIENT, org_name=config['broker']['org']) vdc = pyvcloud_utils.get_vdc(CLIENT, vdc_name=config['broker']['vdc'], org=org) ORG_HREF = org.href VDC_HREF = vdc.href # hrefs for Org and VDC that tests cluster operations test_org = pyvcloud_utils.get_org(CLIENT, org_name=TEST_ORG) test_vdc = pyvcloud_utils.get_vdc(CLIENT, vdc_name=TEST_VDC, org=test_org) TEST_ORG_HREF = test_org.href TEST_VDC_HREF = test_vdc.href create_k8_author_role(config['vcd'])
def init_rde_environment(config_filepath=BASE_CONFIG_FILEPATH, logger=NULL_LOGGER): # noqa: E501 """Set up module variables according to config dict. :param str config_filepath: """ global CLIENT, ORG_HREF, VDC_HREF, \ CATALOG_NAME, TEARDOWN_INSTALLATION, TEARDOWN_CLUSTERS, \ TEMPLATE_DEFINITIONS, TEST_ALL_TEMPLATES, SYS_ADMIN_LOGIN_CMD, \ CLUSTER_ADMIN_LOGIN_CMD, CLUSTER_AUTHOR_LOGIN_CMD, \ USERNAME_TO_LOGIN_CMD, USERNAME_TO_CLUSTER_NAME, TEST_ORG_HREF, \ TEST_VDC_HREF, VCD_API_VERSION_TO_USE logger.debug("Setting RDE environement") config = testutils.yaml_to_dict(config_filepath) logger.debug(f"Config file used: {config}") # download all remote template scripts rtm = RemoteTemplateManager( config['broker']['remote_template_cookbook_url'], legacy_mode=config['service']['legacy_mode']) template_cookbook = rtm.get_filtered_remote_template_cookbook() TEMPLATE_DEFINITIONS = template_cookbook['templates'] rtm.download_all_template_scripts(force_overwrite=True) sysadmin_client = Client(config['vcd']['host'], verify_ssl_certs=config['vcd']['verify']) sysadmin_client.set_credentials( BasicLoginCredentials(config['vcd']['username'], shared_constants.SYSTEM_ORG_NAME, config['vcd']['password'])) vcd_supported_api_versions = \ set(sysadmin_client.get_supported_versions_list()) cse_supported_api_versions = set( shared_constants.SUPPORTED_VCD_API_VERSIONS) # noqa: E501 common_supported_api_versions = \ list(cse_supported_api_versions.intersection(vcd_supported_api_versions)) # noqa: E501 common_supported_api_versions.sort() max_api_version = get_max_api_version(common_supported_api_versions) CLIENT = Client(config['vcd']['host'], api_version=max_api_version, verify_ssl_certs=config['vcd']['verify']) credentials = BasicLoginCredentials(config['vcd']['username'], shared_constants.SYSTEM_ORG_NAME, config['vcd']['password']) CLIENT.set_credentials(credentials) VCD_API_VERSION_TO_USE = max_api_version logger.debug(f"Using VCD api version: {VCD_API_VERSION_TO_USE}") CATALOG_NAME = config['broker']['catalog'] SYS_ADMIN_LOGIN_CMD = f"login {config['vcd']['host']} system " \ f"{config['vcd']['username']} " \ f"-iwp {config['vcd']['password']} " \ f"-V {VCD_API_VERSION_TO_USE}" CLUSTER_ADMIN_LOGIN_CMD = f"login {config['vcd']['host']} " \ f"{TEST_ORG}" \ f" {CLUSTER_ADMIN_NAME} " \ f"-iwp {CLUSTER_ADMIN_PASSWORD} " \ f"-V {VCD_API_VERSION_TO_USE}" CLUSTER_AUTHOR_LOGIN_CMD = f"login {config['vcd']['host']} " \ f"{TEST_ORG}" \ f" {CLUSTER_AUTHOR_NAME} " \ f"-iwp {CLUSTER_AUTHOR_PASSWORD} " \ f"-V {VCD_API_VERSION_TO_USE}" USERNAME_TO_LOGIN_CMD = { SYS_ADMIN_NAME: SYS_ADMIN_LOGIN_CMD, CLUSTER_ADMIN_NAME: CLUSTER_ADMIN_LOGIN_CMD, CLUSTER_AUTHOR_NAME: CLUSTER_AUTHOR_LOGIN_CMD } # hrefs for Org and VDC that hosts the catalog org = pyvcloud_utils.get_org(CLIENT, org_name=config['broker']['org']) vdc = pyvcloud_utils.get_vdc(CLIENT, vdc_name=config['broker']['vdc'], org=org) ORG_HREF = org.href VDC_HREF = vdc.href logger.debug(f"Using template org {org.get_name()} with href {ORG_HREF}") logger.debug(f"Using template vdc {vdc.name} with href {VDC_HREF}") # hrefs for Org and VDC that tests cluster operations test_org = pyvcloud_utils.get_org(CLIENT, org_name=TEST_ORG) test_vdc = pyvcloud_utils.get_vdc(CLIENT, vdc_name=TEST_VDC, org=test_org) TEST_ORG_HREF = test_org.href TEST_VDC_HREF = test_vdc.href logger.debug(f"Using test org {test_org.get_name()} " f"with href {TEST_ORG_HREF}") logger.debug(f"Using test vdc {test_vdc.name} with href {TEST_VDC_HREF}") create_cluster_admin_role(config['vcd'], logger=logger) create_cluster_author_role(config['vcd'], logger=logger)