Skip to content

Commit

Permalink
Container continues to run silently when cos-alerter fails (#70)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
IbraAoad committed Mar 8, 2024
1 parent e857cb2 commit 55114fa
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 13 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/pull-request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
17 changes: 12 additions & 5 deletions cos_alerter/alerter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
]
Expand Down
6 changes: 2 additions & 4 deletions rockcraft.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
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:
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:
Expand Down
2 changes: 1 addition & 1 deletion snap/snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
13 changes: 13 additions & 0 deletions tests/test_alerter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright 2023 Canonical Ltd.
# See LICENSE file for licensing details.

import os
import textwrap
import threading
import unittest.mock
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 55114fa

Please sign in to comment.