Skip to content
Snippets Groups Projects
Commit 11811d59 authored by Bastien Abadie's avatar Bastien Abadie
Browse files

Merge branch 'update-checks' into 'master'

Update system checks

Closes #890

See merge request !1533
parents 116effe1 89f372eb
No related branches found
No related tags found
1 merge request!1533Update system checks
import os.path
import subprocess
import yaml
from django.core.checks import Error, Warning, register
......@@ -44,56 +43,18 @@ def local_imageserver_check(*args, **kwargs):
"""
from django.conf import settings
from arkindex.images.models import ImageServer
errors = []
local_id = settings.LOCAL_IMAGESERVER_ID
try:
ImageServer.objects.get(id=local_id)
except ImageServer.DoesNotExist:
errors.append(Error(
'Local ImageServer with ID {} does not exist'.format(local_id),
hint='settings.LOCAL_IMAGESERVER_ID = {}'.format(local_id),
id='arkindex.E004',
))
return errors
@register()
def docker_images_check(*args, **kwargs):
"""
Check that the Arkindex backend and ML images exist
"""
from django.conf import settings
errors = []
if settings.PONOS_RECIPE is None:
# In a Ponos task
return []
images = (
(settings.ARKINDEX_TASKS_IMAGE, 'ARKINDEX_TASKS_IMAGE'),
)
for image_tag, setting_name in images:
try:
subprocess.run(
['docker', 'image', 'inspect', image_tag],
check=True,
# Silent output
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
except subprocess.CalledProcessError:
errors.append(Error(
'Docker image with tag "{}" was not found.'.format(image_tag),
hint='settings.{} = "{}"'.format(setting_name, image_tag),
id='arkindex.E006',
))
except FileNotFoundError:
# Docker is not available, ignore check
pass
return [Warning(
f'Local ImageServer with ID {local_id} does not exist',
hint=f'settings.LOCAL_IMAGESERVER_ID = {local_id}',
id='arkindex.W009',
)]
return errors
return []
@register()
......@@ -176,14 +137,15 @@ def s3_check(*args, **kwargs):
'AWS_SECRET_KEY': 'AWS secret key',
'AWS_THUMBNAIL_BUCKET': 'S3 thumbnails bucket name',
'AWS_STAGING_BUCKET': 'S3 staging bucket name',
'AWS_EXPORT_BUCKET': 'S3 export bucket name',
}
errors = []
for name, display_name in aws_settings.items():
value = getattr(settings, name, None)
if not value:
errors.append(Error(
'{} is missing; all S3-related features will fail.'.format(display_name),
hint='settings.{} = {}'.format(name, repr(value)),
f'{display_name} is missing; all S3-related features will fail.',
hint=f'settings.{name} = {value!r}',
id='arkindex.E011',
))
......@@ -198,6 +160,6 @@ def public_hostname_check(*args, **kwargs):
return [Warning(
'The public_hostname setting is missing; absolute URLs may not be correctly generated.',
hint='settings.PUBLIC_HOSTNAME',
id='arkindex.W007',
id='arkindex.W008',
)]
return []
import subprocess
from pathlib import Path
from subprocess import CalledProcessError
from unittest.mock import call, patch
from unittest.mock import patch
from django.conf import settings
from django.core.checks import Error, Warning
......@@ -47,10 +45,10 @@ class ChecksTestCase(TestCase):
with self.settings(LOCAL_IMAGESERVER_ID=42):
self.assertListEqual(local_imageserver_check(), [
Error(
Warning(
'Local ImageServer with ID 42 does not exist',
hint='settings.LOCAL_IMAGESERVER_ID = 42',
id='arkindex.E004',
id='arkindex.W009',
),
])
......@@ -61,55 +59,6 @@ class ChecksTestCase(TestCase):
srv.delete()
@patch('arkindex.project.checks.subprocess.run')
@override_settings(
ARKINDEX_TASKS_IMAGE='nuh',
)
def test_docker_images_check(self, run_mock):
from arkindex.project.checks import docker_images_check
expected_calls = [
call(
['docker', 'image', 'inspect', 'nuh'],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
check=True,
),
]
run_mock.side_effect = CalledProcessError(1, '')
self.assertListEqual(docker_images_check(), [
Error(
'Docker image with tag "nuh" was not found.',
hint='settings.ARKINDEX_TASKS_IMAGE = "nuh"',
id='arkindex.E006',
)
])
self.assertEqual(run_mock.call_count, 1)
self.assertEqual(run_mock.call_args_list, expected_calls)
@patch('arkindex.project.checks.subprocess.run')
def test_docker_images_check_missing_client(self, run_mock):
"""
Test the Docker images check does not show errors if the Docker client is missing
"""
from arkindex.project.checks import docker_images_check
run_mock.side_effect = FileNotFoundError
with self.settings(ARKINDEX_APP_IMAGE='nope', ARKINDEX_TASKS_IMAGE='nuh'):
self.assertListEqual(docker_images_check(), [])
self.assertEqual(run_mock.call_count, 1)
self.assertEqual(run_mock.call_args_list, [
call(
['docker', 'image', 'inspect', 'nuh'],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
check=True,
),
])
@override_settings()
@patch('arkindex.project.checks.parse_recipe')
def test_ponos_recipe_check(self, parse_mock):
......@@ -180,6 +129,7 @@ class ChecksTestCase(TestCase):
del settings.AWS_SECRET_KEY
del settings.AWS_THUMBNAIL_BUCKET
del settings.AWS_STAGING_BUCKET
del settings.AWS_EXPORT_BUCKET
self.assertCountEqual(s3_check(), [
Error(
'AWS access key ID is missing; all S3-related features will fail.',
......@@ -201,12 +151,18 @@ class ChecksTestCase(TestCase):
hint='settings.AWS_STAGING_BUCKET = None',
id='arkindex.E011',
),
Error(
'S3 export bucket name is missing; all S3-related features will fail.',
hint='settings.AWS_EXPORT_BUCKET = None',
id='arkindex.E011',
),
])
settings.AWS_ACCESS_KEY = 'key'
settings.AWS_SECRET_KEY = 's3kr3t'
settings.AWS_THUMBNAIL_BUCKET = 'Thumbs.db'
settings.AWS_STAGING_BUCKET = 'buckette'
settings.AWS_EXPORT_BUCKET = 'devnull'
self.assertListEqual(s3_check(), [])
@override_settings()
......@@ -218,7 +174,7 @@ class ChecksTestCase(TestCase):
Warning(
'The public_hostname setting is missing; absolute URLs may not be correctly generated.',
hint='settings.PUBLIC_HOSTNAME',
id='arkindex.W007',
id='arkindex.W008',
)
])
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment