def test_nested_dicts(self):
     assert (merge_dicts({
         "a": {
             "a": 1,
             "b": 1
         },
         "b": {
             "a": 1,
             "b": 1
         }
     }, {
         "a": {
             "a": 2
         },
         "b": {
             "b": 2
         }
     }) == {
         "a": {
             "a": 2,
             "b": 1
         },
         "b": {
             "a": 1,
             "b": 2
         }
     })
 def test_dict_keys_are_merged(self):
     assert (merge_dicts({"a": {
         "a": 1
     }}, {"a": {
         "b": 2
     }}) == {
         "a": {
             "a": 1,
             "b": 2
         }
     })
def paas_manifest(environment, app, vars_file, var, out_file):
    """Generate a PaaS manifest file from a Jinja2 template"""

    variables = load_variables(environment, vars_file, {
        'environment': environment,
        'app': app.replace('_', '-')
    })

    template_file = f"paas/{app}.j2"
    template_content = load_file(template_file)

    variables = merge_dicts(variables, variables[app])

    try:
        variables = merge_dicts(
            variables, get_variables_from_command_line_or_environment(var))
    except KeyError as e:
        sys.exit(
            f"""Error: Command line variable "--var '{e.args[0]}'" was not set by the flag"""
            """ and was not found in environment variables. Please check your environment is correctly configured."""
        )

    try:
        manifest_content = template_string(template_content,
                                           variables,
                                           templates_path='paas/')
    except UndefinedError as e:
        # the UndefinedError.message is usually something like "'VAR' is undefined"
        sys.exit(
            f"""Error: The template '{template_file}' thinks that the variable {e.message}."""
            """ Please check you have included all of the var files and command line vars that you need."""
        )

    if out_file is not None:
        with open(out_file, 'w') as f:
            f.write(manifest_content)
        os.chmod(out_file, 0o600)
    else:
        print(manifest_content)
Ejemplo n.º 4
0
def paas_manifest(environment, app, vars_file, out_file):
    """Generate a PaaS manifest file from a Jinja2 template"""

    variables = load_variables(environment, vars_file, {
        'environment': environment,
        'app': app.replace('_', '-')
    })

    template_content = load_file('paas/{}.j2'.format(app))

    variables = merge_dicts(variables, variables[app])

    manifest_content = template_string(template_content, variables, templates_path='paas/')

    if out_file is not None:
        with open(out_file, 'w') as f:
            f.write(manifest_content)
        os.chmod(out_file, 0o600)
    else:
        print(manifest_content)
Ejemplo n.º 5
0
 def test_replacing_keys_with_dicts(self):
     assert merge_dicts({"a": 2}, {"a": {"a": 1}}) == {"a": {"a": 1}}
Ejemplo n.º 6
0
 def test_no_overwriting_dict_keys(self):
     with pytest.raises(ValueError):
         assert merge_dicts({"a": {"a": 1}}, {"a": 2}) == {"a": 2}
Ejemplo n.º 7
0
 def test_overwriting_list_key(self):
     assert merge_dicts({"a": [1]}, {"a": [2]}) == {"a": [2]}
Ejemplo n.º 8
0
 def test_overwriting_key(self):
     assert merge_dicts({"a": 1}, {"a": 2}) == {"a": 2}
Ejemplo n.º 9
0
 def test_simple_dicts(self):
     assert merge_dicts({"a": 1}, {"b": 2}) == {"a": 1, "b": 2}