def test_0010_config_invalid_keys(config): """Test that config files with invalid/extra keys don't pass validation.""" bad_key_config1 = testutils.yaml_to_dict(env.ACTIVE_CONFIG_FILEPATH) del bad_key_config1['amqp'] bad_key_config1['extra_section'] = True bad_key_config2 = testutils.yaml_to_dict(env.ACTIVE_CONFIG_FILEPATH) del bad_key_config2['vcs'][0]['username'] bad_key_config2['vcs'][0]['extra_property'] = 'a' bad_key_config3 = testutils.yaml_to_dict(env.ACTIVE_CONFIG_FILEPATH) del bad_key_config3['broker']['templates'][0]['mem'] del bad_key_config3['broker']['templates'][0]['name'] bad_key_config3['broker']['templates'][0]['extra_property'] = 0 configs = [ bad_key_config1, bad_key_config2, bad_key_config3 ] for config in configs: testutils.dict_to_yaml_file(config, env.ACTIVE_CONFIG_FILEPATH) try: get_validated_config(env.ACTIVE_CONFIG_FILEPATH) assert False, f"{env.ACTIVE_CONFIG_FILEPATH} passed validation " \ f"when it should not have" except KeyError: pass
def test_0020_config_invalid_value_types(config): """Test that configs with invalid value types don't pass validation.""" bad_values_config1 = testutils.yaml_to_dict(env.ACTIVE_CONFIG_FILEPATH) bad_values_config1['vcd'] = True bad_values_config1['vcs'] = 'a' bad_values_config2 = testutils.yaml_to_dict(env.ACTIVE_CONFIG_FILEPATH) bad_values_config2['vcd']['username'] = True bad_values_config2['vcd']['api_version'] = 123 bad_values_config2['vcd']['port'] = 'a' bad_values_config3 = testutils.yaml_to_dict(env.ACTIVE_CONFIG_FILEPATH) bad_values_config3['vcs'][0]['username'] = True bad_values_config3['vcs'][0]['password'] = 123 bad_values_config3['vcs'][0]['verify'] = 'a' bad_values_config4 = testutils.yaml_to_dict(env.ACTIVE_CONFIG_FILEPATH) bad_values_config4['broker']['templates'][0]['cpu'] = 'a' bad_values_config4['broker']['templates'][0]['name'] = 123 configs = [ bad_values_config1, bad_values_config2, bad_values_config3, bad_values_config4 ] for config in configs: testutils.dict_to_yaml_file(config, env.ACTIVE_CONFIG_FILEPATH) try: get_validated_config(env.ACTIVE_CONFIG_FILEPATH) assert False, f"{env.ACTIVE_CONFIG_FILEPATH} passed validation " \ f"when it should not have" except ValueError: pass
def test_0110_install_cleanup_true(config, blank_cust_scripts, unregister_cse): """Tests that installation deletes temp vapps when 'cleanup' is True. command: cse install --config cse_test_config.yaml expected: temp vapps are deleted """ # set cleanup to true in config file for template_config in config['broker']['templates']: template_config['cleanup'] = True testutils.dict_to_yaml_file(config, env.ACTIVE_CONFIG_FILEPATH) result = env.CLI_RUNNER.invoke( cli, ['install', '--config', env.ACTIVE_CONFIG_FILEPATH], catch_exceptions=False) assert result.exit_code == 0 # check that cse was registered correctly env.check_cse_registration(config['amqp']['routing_key'], config['amqp']['exchange']) for template_config in config['broker']['templates']: # check that vapp templates exists assert env.catalog_item_exists(template_config['catalog_item']), \ 'vApp template does not exist when it should.' # check that temp vapps do not exist (cleanup: true) assert not env.vapp_exists(template_config['temp_vapp']), \ 'Temp vapp exists when it should not (cleanup: True).'
def test_0040_config_missing_keys(config): """Test that config files with missing keys don't pass validation.""" bad_key_config1 = testutils.yaml_to_dict(env.ACTIVE_CONFIG_FILEPATH) del bad_key_config1['mqtt'] bad_key_config2 = testutils.yaml_to_dict(env.ACTIVE_CONFIG_FILEPATH) del bad_key_config2['vcs'][0]['username'] configs = [ bad_key_config1, bad_key_config2 ] for config in configs: testutils.dict_to_yaml_file(config, env.ACTIVE_CONFIG_FILEPATH) PYTEST_LOGGER.debug(f"Validating config: {config}") try: get_validated_config(env.ACTIVE_CONFIG_FILEPATH, skip_config_decryption=True) PYTEST_LOGGER.debug("Validation succeeded when it " "should not have") assert False, f"{env.ACTIVE_CONFIG_FILEPATH} passed validation " \ f"when it should not have" except KeyError as e: PYTEST_LOGGER.debug("Validation failed as expected due " f"to invalid keys: {e}")
def test_0020_config_invalid_value_types(self): # tests for when config file has incorrect value types invalid_values_config1 = yaml_to_dict(ACTIVE_CONFIG_FILEPATH) invalid_values_config1['vcd'] = True invalid_values_config1['vcs'] = 'a' invalid_values_config2 = yaml_to_dict(ACTIVE_CONFIG_FILEPATH) invalid_values_config2['vcd']['username'] = True invalid_values_config2['vcd']['api_version'] = 123 invalid_values_config2['vcd']['port'] = 'a' invalid_values_config3 = yaml_to_dict(ACTIVE_CONFIG_FILEPATH) invalid_values_config3['vcs'][0]['username'] = True invalid_values_config3['vcs'][0]['password'] = 123 invalid_values_config3['vcs'][0]['verify'] = 'a' invalid_values_config4 = yaml_to_dict(ACTIVE_CONFIG_FILEPATH) invalid_values_config4['broker']['templates'][0]['cpu'] = 'a' invalid_values_config4['broker']['templates'][0]['name'] = 123 configs = [ invalid_values_config1, invalid_values_config2, invalid_values_config3, invalid_values_config4 ] for config_dict in configs: dict_to_yaml_file(config_dict, ACTIVE_CONFIG_FILEPATH) try: get_validated_config(ACTIVE_CONFIG_FILEPATH) print(f"{ACTIVE_CONFIG_FILEPATH} passed validation when " f"it should not have") assert False except ValueError: pass
def test_0010_config_invalid_keys(self): """Tests that config file with invalid/extra keys or invalid value types do not pass config validation. """ # 3 tests for when config file has missing or extra keys invalid_keys_config1 = yaml_to_dict(ACTIVE_CONFIG_FILEPATH) del invalid_keys_config1['amqp'] invalid_keys_config1['extra_section'] = True invalid_keys_config2 = yaml_to_dict(ACTIVE_CONFIG_FILEPATH) del invalid_keys_config2['vcs'][0]['username'] invalid_keys_config2['vcs'][0]['extra_property'] = 'a' invalid_keys_config3 = yaml_to_dict(ACTIVE_CONFIG_FILEPATH) del invalid_keys_config3['broker']['templates'][0]['mem'] del invalid_keys_config3['broker']['templates'][0]['name'] invalid_keys_config3['broker']['templates'][0]['extra_property'] = 0 configs = [ invalid_keys_config1, invalid_keys_config2, invalid_keys_config3 ] for config_dict in configs: dict_to_yaml_file(config_dict, ACTIVE_CONFIG_FILEPATH) try: get_validated_config(ACTIVE_CONFIG_FILEPATH) print(f"{ACTIVE_CONFIG_FILEPATH} passed validation when " f"it should not have") assert False except KeyError: pass
def setUp(self): """Runs before each test method. Tasks: - Create a new active config and file from base config file. - Reset VCD AMQP settings. - Unregister CSE from VCD. - Blank out customization scripts. """ self._config = yaml_to_dict(BASE_CONFIG_FILEPATH) dict_to_yaml_file(self._config, ACTIVE_CONFIG_FILEPATH) os.chmod(ACTIVE_CONFIG_FILEPATH, 0o600) configure_vcd_amqp(self._client, self._default_amqp_settings['AmqpExchange'], self._default_amqp_settings['AmqpHost'], self._default_amqp_settings['AmqpPort'], self._default_amqp_settings['AmqpPrefix'], self._default_amqp_settings['AmqpSslAcceptAll'], self._default_amqp_settings['AmqpUseSSL'], self._default_amqp_settings['AmqpVHost'], self._amqp_username, self._amqp_password, quiet=True) try: self._api_extension.delete_extension(CSE_NAME, CSE_NAMESPACE) except MissingRecordException: pass restore_customizaton_scripts()
def test_0050_config_invalid_value_types(config): """Test that configs with invalid value types don't pass validation.""" bad_values_config1 = testutils.yaml_to_dict(env.ACTIVE_CONFIG_FILEPATH) bad_values_config1['vcd'] = True bad_values_config1['vcs'] = 'a' bad_values_config2 = testutils.yaml_to_dict(env.ACTIVE_CONFIG_FILEPATH) bad_values_config2['vcd']['username'] = True bad_values_config2['vcd']['port'] = 'a' bad_values_config3 = testutils.yaml_to_dict(env.ACTIVE_CONFIG_FILEPATH) bad_values_config3['vcs'][0]['username'] = True bad_values_config3['vcs'][0]['password'] = 123 bad_values_config3['vcs'][0]['verify'] = 'a' bad_values_config4 = testutils.yaml_to_dict(env.ACTIVE_CONFIG_FILEPATH) bad_values_config4['broker']['remote_template_cookbook_url'] = 1 configs = [ bad_values_config1, bad_values_config2, bad_values_config3, bad_values_config4 ] for config in configs: testutils.dict_to_yaml_file(config, env.ACTIVE_CONFIG_FILEPATH) PYTEST_LOGGER.debug(f"Validating config: {config}") try: get_validated_config(env.ACTIVE_CONFIG_FILEPATH, skip_config_decryption=True) assert False, f"{env.ACTIVE_CONFIG_FILEPATH} passed validation " \ f"when it should not have" except TypeError as e: PYTEST_LOGGER.debug("Validation failed as expected due " f"to invalid value: {e}")
def test_0060_install_temp_vapp_already_exists(config, blank_cust_scripts, unregister_cse): """Test installation when temp vapp already exists. Tests that installation: - skips cse registration (answering no to prompt), - captures temp vapp as template correctly, - does not delete temp_vapp when config file 'cleanup' property is false. command: cse install --config cse_test_config.yaml --template photon-v2 required files: cse_test_config.yaml expected: cse not registered, photon-v2 template exists, temp-vapp exists """ template_config = None # set cleanup to false for this test for i, template_dict in enumerate(config['broker']['templates']): config['broker']['templates'][i]['cleanup'] = False if template_dict['name'] == env.PHOTON_TEMPLATE_NAME: template_config = template_dict break assert template_config is not None, \ 'Target template not found in config file' testutils.dict_to_yaml_file(config, env.ACTIVE_CONFIG_FILEPATH) res = env.CLI_RUNNER.invoke(cli, ['install', '--config', env.ACTIVE_CONFIG_FILEPATH, '--template', env.PHOTON_TEMPLATE_NAME], input='N\nN', catch_exceptions=False) assert res.exit_code == 0 # check that cse was not registered assert not env.is_cse_registered(), \ 'CSE is registered as an extension when it should not be.' # check that vapp template exists in catalog assert env.catalog_item_exists(template_config['catalog_item']), \ 'vApp template does not exist when it should.' # check that temp vapp exists (cleanup: false) assert env.vapp_exists(template_config['temp_vapp']), \ 'vApp does not exist when it should (cleanup: false).'
def setup_active_config(): """Set up the active config file from BASE_CONFIG_FILEPATH. 'test' section is removed if it exists in base config, active config is created at ACTIVE_CONFIG_FILEPATH. :returns: config dict without 'test' key :rtype: dict """ config = testutils.yaml_to_dict(BASE_CONFIG_FILEPATH) if 'test' in config: del config['test'] testutils.dict_to_yaml_file(config, ACTIVE_CONFIG_FILEPATH) os.chmod(ACTIVE_CONFIG_FILEPATH, 0o600) return config
def test_0040_config_missing_keys(config): """Test that config files with missing keys don't pass validation.""" bad_key_config1 = testutils.yaml_to_dict(env.ACTIVE_CONFIG_FILEPATH) del bad_key_config1['amqp'] bad_key_config2 = testutils.yaml_to_dict(env.ACTIVE_CONFIG_FILEPATH) del bad_key_config2['vcs'][0]['username'] configs = [bad_key_config1, bad_key_config2] for config in configs: testutils.dict_to_yaml_file(config, env.ACTIVE_CONFIG_FILEPATH) try: get_validated_config(env.ACTIVE_CONFIG_FILEPATH) assert False, f"{env.ACTIVE_CONFIG_FILEPATH} passed validation " \ f"when it should not have" except KeyError: pass
def test_0090_install_temp_vapp_already_exists(config, blank_cust_scripts, unregister_cse): """Test installation when temp vapp already exists. Tests that installation: - captures temp vapp as template correctly, - does not delete temp_vapp when config file 'cleanup' property is false. command: cse install --config cse_test_config.yaml --template photon-v2 required files: cse_test_config.yaml expected: cse not registered, photon-v2 template exists, temp-vapp exists """ # set cleanup to false for this test for i, template_dict in enumerate(config['broker']['templates']): config['broker']['templates'][i]['cleanup'] = False template_config = testutils.get_default_template_config(config) testutils.dict_to_yaml_file(config, env.ACTIVE_CONFIG_FILEPATH) result = env.CLI_RUNNER.invoke(cli, [ 'install', '--config', env.ACTIVE_CONFIG_FILEPATH, '--template', template_config['name'] ], catch_exceptions=False) assert result.exit_code == 0 # check that cse was registered correctly env.check_cse_registration(config['amqp']['routing_key'], config['amqp']['exchange']) # check that vapp template exists in catalog assert env.catalog_item_exists(template_config['catalog_item']), \ 'vApp template does not exist when it should.' # check that temp vapp exists (cleanup: false) assert env.vapp_exists(template_config['temp_vapp']), \ 'vApp does not exist when it should (cleanup: false).'
def test_0060_install_temp_vapp_already_exists(self): """Tests installation when temp vapp already exists. Tests that installation skips amqp configuration (when answering no to prompt), skips cse registration (when answering no to prompt), captures temp vapp as template correctly, does not delete temp_vapp when config file 'cleanup' property is false. command: cse install --config cse_test_config.yaml --template photon-v2 required files: cse_test_config.yaml expected: cse not registered, amqp not configured, photon-v2 template exists, temp-vapp exists """ template_config = None for i, template_dict in enumerate(self._config['broker']['templates']): # set cleanup to false for this test self._config['broker']['templates'][i]['cleanup'] = False if template_dict['name'] == PHOTON_TEMPLATE_NAME: template_config = template_dict break if template_config is None: print('Target template not found in config file') assert False dict_to_yaml_file(self._config, ACTIVE_CONFIG_FILEPATH) result = self._runner.invoke(cli, [ 'install', '--config', ACTIVE_CONFIG_FILEPATH, '--template', PHOTON_TEMPLATE_NAME ], input='N\nN', catch_exceptions=False) assert result.exit_code == 0 # reloads required due to inability to wait for catalog deletion. # vdc can't find vapp unless we reload self._org.reload() self._vdc.reload() # check that amqp was not configured assert diff_amqp_settings(self._amqp_service, self._config['amqp']) # check that cse was not registered try: self._api_extension.get_extension(CSE_NAME, namespace=CSE_NAMESPACE) print('CSE is registered as an extension when it should not be.') assert False except MissingRecordException: pass # check that vapp template exists in catalog try: self._org.get_catalog_item(self._config['broker']['catalog'], template_config['catalog_item']) except EntityNotFoundException: print('vApp template does not exist when it should') assert False # check that temp vapp exists (cleanup: false) try: self._vdc.get_vapp(template_config['temp_vapp']) except EntityNotFoundException: print('vApp does not exist when it should (cleanup: false)') assert False