From 55114fa94769ae6ead61cf2462a2d3c4c82faea6 Mon Sep 17 00:00:00 2001 From: Ibrahim Awwad Date: Fri, 8 Mar 2024 16:55:44 +0200 Subject: [PATCH] Container continues to run silently when cos-alerter fails (#70) * absent config handling * remove python packages * adding to change log * adding unit test * linting * fix unit test * Remove py-pkg python check in CI * bump snap version --- .github/workflows/pull-request.yaml | 2 -- CHANGELOG.md | 4 ++++ cos_alerter/alerter.py | 17 ++++++++++++----- pyproject.toml | 2 +- rockcraft.yaml | 6 ++---- snap/snapcraft.yaml | 2 +- tests/test_alerter.py | 13 +++++++++++++ 7 files changed, 33 insertions(+), 13 deletions(-) diff --git a/.github/workflows/pull-request.yaml b/.github/workflows/pull-request.yaml index bcc7fa0..0093a1b 100644 --- a/.github/workflows/pull-request.yaml +++ b/.github/workflows/pull-request.yaml @@ -16,9 +16,7 @@ jobs: run: | pkg_version=$(grep -P -o 'version = "\d+\.\d+\.\d+"' pyproject.toml | grep -P -o '\d+\.\d+\.\d+') rock_version=$(grep -P -o 'version: "\d+\.\d+\.\d+"' rockcraft.yaml | grep -P -o '\d+\.\d+\.\d+') - rock_pkg_version=$(grep -P -o 'cos-alerter==\d+\.\d+\.\d+' rockcraft.yaml | grep -P -o '\d+\.\d+\.\d+') [ "${pkg_version}" == "${rock_version}" ] - [ "${rock_version}" == "${rock_pkg_version}" ] changelog-check: name: Changelog Check runs-on: ubuntu-latest diff --git a/CHANGELOG.md b/CHANGELOG.md index 4773939..288e880 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [0.8.0] - 2024-03-07 + +- Fixes container silently running by exiting with non-zero status when configuration file is missing. (#70). + ## [0.7.0] - 2024-02-26 - Client state is now retained on a graceful shutdown (#66). diff --git a/cos_alerter/alerter.py b/cos_alerter/alerter.py index 4b90b22..661a97e 100644 --- a/cos_alerter/alerter.py +++ b/cos_alerter/alerter.py @@ -46,16 +46,23 @@ def _validate_hashes(self, clients): def reload(self): """Reload config values from the disk.""" yaml = YAML(typ="rt") + + # Load default configuration with open( os.path.join(os.path.dirname(os.path.realpath(__file__)), "config-defaults.yaml") ) as f: self.data = yaml.load(f) - with open(self.path, "r") as f: - try: + + # Load user configuration + try: + with open(self.path, "r") as f: user_data = yaml.load(f) - except DuplicateKeyError: - logger.critical("Duplicate client IDs found in COS Alerter config. Exiting...") - sys.exit(1) + except FileNotFoundError: + logger.critical("Config file not found. Exiting...") + sys.exit(1) + except DuplicateKeyError: + logger.critical("Duplicate client IDs found in COS Alerter config. Exiting...") + sys.exit(1) # Validate that keys are valid SHA-512 hashes if user_data and user_data.get("watch", {}).get("clients"): diff --git a/pyproject.toml b/pyproject.toml index 02d0591..996831d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "cos-alerter" -version = "0.7.0" +version = "0.8.0" authors = [ { name="Dylan Stephano-Shachter", email="dylan.stephano-shachter@canonical.com" } ] diff --git a/rockcraft.yaml b/rockcraft.yaml index f0a3682..274f491 100644 --- a/rockcraft.yaml +++ b/rockcraft.yaml @@ -1,8 +1,8 @@ name: cos-alerter summary: A liveness checker for self-monitoring. description: Receive regular pings from the cos stack and alert when they stop. -version: "0.7.0" # NOTE: Make sure this matches `cos-alerter` below -base: ubuntu:22.04 +version: "0.8.0" +base: ubuntu@22.04 license: Apache-2.0 platforms: amd64: @@ -10,8 +10,6 @@ parts: cos-alerter: plugin: python source: . - python-packages: - - cos-alerter==0.7.0 # NOTE: Make sure this matches `version` above stage-packages: - python3-venv services: diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index d1c8c24..b8570a6 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -1,5 +1,5 @@ name: cos-alerter -version: '0.7.0' +version: '0.8.0' summary: A watchdog alerting on alertmanager notification failures. license: Apache-2.0 contact: simon.aronsson@canonical.com diff --git a/tests/test_alerter.py b/tests/test_alerter.py index e446672..8cb29e7 100644 --- a/tests/test_alerter.py +++ b/tests/test_alerter.py @@ -1,6 +1,7 @@ # Copyright 2023 Canonical Ltd. # See LICENSE file for licensing details. +import os import textwrap import threading import unittest.mock @@ -29,6 +30,18 @@ def test_config_default_empty_file(fake_fs): assert config["watch"]["down_interval"] == 300 +def test_file_not_found_error(fake_fs): + with unittest.mock.patch("cos_alerter.alerter.logger") as mock_logger: + os.unlink("/etc/cos-alerter.yaml") + try: + config.reload() + except SystemExit as exc: + assert exc.code == 1 + mock_logger.critical.assert_called_once_with("Config file not found. Exiting...") + else: + assert False + + def test_duplicate_key_error(fake_fs): duplicate_config = """ watch: