Skip to content

Commit

Permalink
Fix broken checks
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinMind committed Dec 18, 2024
1 parent 75b17fd commit 62a4562
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 17 deletions.
25 changes: 9 additions & 16 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ EOF
ARG DOCKER_TARGET
ENV DOCKER_TARGET=${DOCKER_TARGET}

# Add our custom mime types (required for for ts/json/md files)
COPY docker/etc/mime.types /etc/mime.types

# Define production dependencies as a single layer
# let's the rest of the stages inherit prod dependencies
# and makes copying the /deps dir to the final layer easy.
Expand All @@ -141,6 +144,8 @@ RUN \
${HOME}/scripts/install_deps.py prod
EOF

FROM base AS development

FROM base AS locales
ARG LOCALE_DIR=${HOME}/locale
# Compile locales
Expand Down Expand Up @@ -176,29 +181,17 @@ echo "from olympia.lib.settings_base import *" > settings_local.py
DJANGO_SETTINGS_MODULE="settings_local" make -f Makefile-docker update_assets
EOF

FROM base AS sources



# Add our custom mime types (required for for ts/json/md files)
COPY docker/etc/mime.types /etc/mime.types
FROM base AS production
# Copy the rest of the source files from the host
COPY --chown=olympia:olympia . ${HOME}
# Copy compiled locales from builder
COPY --from=locales --chown=olympia:olympia ${HOME}/locale ${HOME}/locale
# Copy assets from assets
COPY --from=assets --chown=olympia:olympia ${HOME}/site-static ${HOME}/site-static
COPY --from=assets --chown=olympia:olympia ${HOME}/static-build ${HOME}/static-build
# Copy build info from info
COPY --from=info ${BUILD_INFO} ${BUILD_INFO}

# Set shell back to sh until we can prove we can use bash at runtime
SHELL ["/bin/sh", "-c"]

FROM sources AS development

FROM sources AS production

# Copy compiled locales from builder
COPY --from=locales --chown=olympia:olympia ${HOME}/locale ${HOME}/locale
# Copy dependencies from `pip_production`
COPY --from=pip_production --chown=olympia:olympia /deps /deps


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
14 changes: 14 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,20 @@ 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')

0 comments on commit 62a4562

Please sign in to comment.