From 20eeff46e42bcd0c5c1e288defed527ae6f7aa00 Mon Sep 17 00:00:00 2001 From: Kevin Meinhardt Date: Wed, 18 Dec 2024 22:41:52 +0100 Subject: [PATCH] Fix broken checks --- src/olympia/amo/monitors.py | 1 + src/olympia/amo/tests/test_monitor.py | 15 +++++++++++++++ src/olympia/core/apps.py | 4 +++- src/olympia/core/tests/test_apps.py | 18 ++++++++++++++++++ 4 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/olympia/amo/monitors.py b/src/olympia/amo/monitors.py index 82fe51ca0f0c..4cf7eb0f8fd3 100644 --- a/src/olympia/amo/monitors.py +++ b/src/olympia/amo/monitors.py @@ -134,6 +134,7 @@ def elastic(): status = 'ES is red' elastic_results = health except Exception: + status = 'Failed to connect to Elasticsearch' elastic_results = {'exception': traceback.format_exc()} return status, elastic_results diff --git a/src/olympia/amo/tests/test_monitor.py b/src/olympia/amo/tests/test_monitor.py index 510d55185bfd..f7c85a78ded3 100644 --- a/src/olympia/amo/tests/test_monitor.py +++ b/src/olympia/amo/tests/test_monitor.py @@ -49,6 +49,21 @@ def test_elastic(self): status, elastic_result = monitors.elastic() assert status == '' + @patch('olympia.amo.monitors.get_es', side_effect=Exception('Connection error')) + def test_elastic_connection_error(self, _): + status, elastic_result = monitors.elastic() + assert status == 'Failed to connect to Elasticsearch' + assert 'Connection error' in elastic_result['exception'] + + def test_elastic_status_red(self): + mock_es = MagicMock() + mock_es.cluster.health.return_value = {'status': 'red'} + with patch('olympia.amo.monitors.get_es', return_value=mock_es): + status, elastic_result = monitors.elastic() + assert status == 'ES is red' + assert elastic_result == {'status': 'red'} + + @patch('os.path.exists') @patch('os.access') def test_path(self, mock_exists, mock_access): diff --git a/src/olympia/core/apps.py b/src/olympia/core/apps.py index 2a32723f5214..879b30868248 100644 --- a/src/olympia/core/apps.py +++ b/src/olympia/core/apps.py @@ -3,6 +3,7 @@ import subprocess import warnings from io import StringIO +from pwd import getpwnam from django.apps import AppConfig from django.conf import settings @@ -48,7 +49,8 @@ def host_check(app_configs, **kwargs): # set the expected uid to 9500, otherwise we expect the uid # passed to the environment to be the expected uid. expected_uid = 9500 if settings.HOST_UID is None else int(settings.HOST_UID) - actual_uid = os.getuid() + # Get the actual uid from the olympia user + actual_uid = getpwnam('olympia').pw_uid if actual_uid != expected_uid: return [ diff --git a/src/olympia/core/tests/test_apps.py b/src/olympia/core/tests/test_apps.py index 8264c125e7e4..bb4c8b6d1ffe 100644 --- a/src/olympia/core/tests/test_apps.py +++ b/src/olympia/core/tests/test_apps.py @@ -3,6 +3,7 @@ from django.core.management import call_command from django.core.management.base import SystemCheckError from django.test import TestCase +from django.test.utils import override_settings from olympia.core.utils import REQUIRED_VERSION_KEYS @@ -67,3 +68,20 @@ def test_missing_version_keys_check(self): f'{broken_key} is missing from version.json', ): call_command('check') + + @override_settings(HOST_UID=None) + @mock.patch('olympia.core.apps.getpwnam') + def test_illegal_override_uid_check(self, mock_getpwnam): + """ + In production, or when HOST_UID is not set, we expect to not override + the default uid of 9500 for the olympia user. + """ + mock_getpwnam.return_value.pw_uid = 1000 + with self.assertRaisesMessage( + SystemCheckError, + 'Expected user uid to be 9500', + ): + call_command('check') + + with override_settings(HOST_UID=1000): + call_command('check')