Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix broken checks #22954

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/olympia/amo/monitors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions src/olympia/amo/tests/test_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 3 additions & 1 deletion src/olympia/core/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 [
Expand Down
18 changes: 18 additions & 0 deletions src/olympia/core/tests/test_apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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')
Loading