コード例 #1
0
    def test__it_creates_the_default_config_file(self):
        # Setup
        with open('templates/alertmanager-config-default.yml') as am_yaml:
            expected_config = yaml.safe_load(am_yaml)

        # Exercise
        config = domain.build_alertmanager_config("", "")

        # Assert
        assert yaml.safe_load(config.yaml_dump()) == expected_config
コード例 #2
0
    def test__it_loads_a_base64_encoded_yaml_from_alertmanager_config(self):
        # Setup
        expected_config = {
            str(uuid4()): str(uuid4()),
            str(uuid4()): str(uuid4()),
            str(uuid4()): str(uuid4()),
        }
        base64_config_yaml = \
            b64encode(bytes(yaml.dump(expected_config), 'utf-8'))

        # Exercise
        config = domain.build_alertmanager_config(base64_config_yaml, "")

        # Assert
        assert yaml.safe_load(config.yaml_dump()) == expected_config
コード例 #3
0
    def test__it_loads_a_base64_encoded_yaml_from_alertmanager_secrets(self):
        # Setup
        with open('templates/alertmanager-config-default.yml') as am_yaml:
            expected_config = yaml.safe_load(am_yaml)
        secrets = {
            str(uuid4()): str(uuid4()),
            str(uuid4()): str(uuid4()),
            str(uuid4()): str(uuid4()),
        }
        expected_config.update(secrets)

        base64_secrets_yaml = \
            b64encode(bytes(yaml.dump(expected_config), 'utf-8'))

        # Exercise
        config = domain.build_alertmanager_config("", base64_secrets_yaml)

        # Assert
        assert yaml.safe_load(config.yaml_dump()) == expected_config
コード例 #4
0
    def test__secrets_always_take_precedence(self):
        # Setup
        common_key = str(uuid4())
        common_key_value_in_config = str(uuid4())
        common_key_value_in_secrets = str(uuid4())

        config = {
            str(uuid4()): str(uuid4()),
            str(uuid4()): str(uuid4()),
            str(uuid4()): str(uuid4()),
            common_key: common_key_value_in_config,
        }
        secrets = {
            str(uuid4()): str(uuid4()),
            str(uuid4()): str(uuid4()),
            str(uuid4()): str(uuid4()),
            common_key: common_key_value_in_secrets,
        }

        # Build a new dict without the common key
        expected_config = {
            k: v
            for k, v in dict(config, **secrets).items() if k != common_key
        }
        # Then add back the common key with the secrets value
        expected_config[common_key] = common_key_value_in_secrets

        base64_config_yaml = \
            b64encode(bytes(yaml.dump(config), 'utf-8'))

        base64_secrets_yaml = \
            b64encode(bytes(yaml.dump(secrets), 'utf-8'))

        # Exercise
        config = domain.build_alertmanager_config(base64_config_yaml,
                                                  base64_secrets_yaml)

        # Assert
        assert yaml.safe_load(config.yaml_dump()) == expected_config
コード例 #5
0
def set_juju_pod_spec(fw_adapter):
    if not fw_adapter.am_i_leader():
        logging.debug("Unit is not a leader, skip pod spec configuration")
        return

    charm_config = fw_adapter.get_config()

    logging.debug("Building AlertManager config file")
    alertmanager_config = build_alertmanager_config(
        base64_config_yaml=charm_config["alertmanager-config"],
        base64_secrets_yaml=charm_config["alertmanager-secrets"])

    logging.debug("Building Juju pod spec")
    juju_pod_spec = build_juju_pod_spec(
        app_name=fw_adapter.get_app_name(),
        charm_config=charm_config,
        image_meta=fw_adapter.get_image_meta('alertmanager-image'),
        alertmanager_config=alertmanager_config)

    logging.debug("Configuring pod")
    fw_adapter.set_pod_spec(juju_pod_spec.to_dict())
    fw_adapter.set_unit_status(MaintenanceStatus("Configuring pod"))