def test_configuration(click_runner, click_dir, cloud): input_values = [ "test", cloud, "unittest-nl", "y", # Use commercetools?" "ct-unittest", "n", # Use sentry? "n", ] result = click_runner.invoke(bootstrap, ["config"], input="\n".join(input_values)) assert not result.exception file_path = os.path.join(click_dir, "main.yml") assert os.path.exists(file_path) config = parse_config_from_file(file_path) validate_config(config) assert config.general_config.environment == "test" assert not config.general_config.sentry assert config.sites[0].identifier == "unittest-nl" assert config.sites[0].commercetools.project_key == "ct-unittest" if cloud == "aws": assert not config.general_config.azure assert not config.general_config.terraform_config.azure_remote_state assert config.general_config.terraform_config.aws_remote_state elif cloud == "azure": assert config.general_config.azure assert config.general_config.terraform_config.azure_remote_state assert not config.general_config.terraform_config.aws_remote_state
def test_validate_azure_service_plans(parsed_azure_config: types.MachConfig): config = parsed_azure_config config.components[0].azure.service_plan = "premium" with pytest.raises(ValidationError) as e: validate.validate_config(config) assert str(e.value) == ( "Component api-extensions requires service plan premium which is not defined in the " "Azure configuration.") config.general_config.azure.service_plans["premium"] = types.ServicePlan( kind="Linux", tier="PremiumV2", size="P2v2", )
def test_validate_aws_default_endpoint(config: types.MachConfig): """It must be possible for a component to use the default API Gateway endpoint.""" config.components[0].endpoints = { "main": "public", } config = parse.parse_config(config) with pytest.raises(ValidationError) as e: validate.validate_config(config) assert str(e.value) == "Missing required endpoints public" config.components[0].endpoints = { "main": "default", } config = parse.parse_config(config) validate.validate_config(config)
def parse_and_validate( file: str, output_path: str = None, *, ignore_version=True, vars={}, vars_encrypted=False, ) -> MachConfig: """Parse and validate configuration.""" config = parse_config_from_file(file, vars=vars, vars_encrypted=vars_encrypted) config.file = file click.echo(f"Parsed {file} into config") validate_config(config, ignore_version=ignore_version) if output_path: full_output_path = Path(f"{output_path}/{splitext(basename(file))[0]}") full_output_path.mkdir(exist_ok=True, parents=True) config.output_path = str(full_output_path) return config
def test_validate_stores(parsed_config: types.MachConfig): """Tests if the stores used in the store variables match the defined commercetools stores.""" config = parsed_config site = config.sites[0] site.components[0].store_variables = { "main-store": { "FOO": "BAR", } } # It should fail because we refer a store that hasnt been defined yet with pytest.raises(ValidationError): validate.validate_config(config) site.commercetools = types.CommercetoolsSettings( project_key="ct-unit-test", client_id="a96e59be-24da-4f41-a6cf-d61d7b6e1766", client_secret="98c32de8-1a6c-45a9-a718-d3cce5201799", scopes="manage_project:ct-unit-test", project_settings=types.CommercetoolsProjectSettings( languages=["nl-NL"], countries=["NL"], currencies=["EUR"], ), stores=[ types.CommercetoolsStore( name={ "en-GB": "Default store", }, key="main-store", ), ], ) validate.validate_config(config)
def test_validate_component_endpoint(config: types.MachConfig): """An endpoint defined on a component must exist for all sites that use that component.""" config.components[0].endpoints = { "main": "public", } config = parse.parse_config(config) with pytest.raises(ValidationError): validate.validate_config(config) config.sites[0].endpoints = [ types.Endpoint( key="public", url="api.mach-example.com", ), types.Endpoint( key="services", url="services.mach-example.com", ), ] validate.validate_config(config) new_site = types.Site( identifier="unittest-nl", components=[], aws=types.SiteAWSSettings( account_id=1234567890, region="eu-central-1", ), ) config.sites.append(new_site) validate.validate_config(config) new_site.components.append(types.Component(name="api-extensions", )) config = parse.parse_config(config) with pytest.raises(ValidationError): validate.validate_config(config)