Skip to content
This repository was archived by the owner on May 6, 2020. It is now read-only.
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
6 changes: 4 additions & 2 deletions rootfs/api/models/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,10 @@ def get_port(self):
# application has registry auth - $PORT is required
if (creds is not None) or (settings.REGISTRY_LOCATION != 'on-cluster'):
if envs.get('PORT', None) is None:
self.app.log('Private registry detected but no $PORT defined. Defaulting to $PORT 5000', logging.WARNING) # noqa
return 5000
raise DeisException(
'PORT needs to be set in the config '
'when using a private registry'
)

# User provided PORT
return int(envs.get('PORT'))
Expand Down
35 changes: 28 additions & 7 deletions rootfs/api/tests/test_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@

from django.contrib.auth.models import User
from django.core.cache import cache
from django.conf import settings
from django.test.utils import override_settings
from unittest import mock
from rest_framework.authtoken.models import Token

from api.models import App, Release
from scheduler import KubeHTTPException
from api.exceptions import DeisException
from api.tests import adapter, mock_port, DeisTransactionTestCase
import requests_mock

Expand Down Expand Up @@ -380,20 +381,19 @@ def test_release_get_port(self, mock_requests):

# TODO(bacongobbler): test dockerfile ports

@override_settings(REGISTRY_LOCATION="off-cluster")
def test_release_external_registry(self, mock_requests):
"""
Test that get_port always returns the proper value.
"""
app_id = "test"
body = {'id': app_id}
response = self.client.post('/v2/apps', body,)
self.assertEqual(response.status_code, 201, response.data)
app_id = self.create_app()

# set the required port for external registries
body = {'values': json.dumps({'PORT': '3000'})}
config_response = self.client.post('/v2/apps/test/config', body)
config_response = self.client.post('/v2/apps/{}/config'.format(app_id), body)
self.assertEqual(config_response.status_code, 201, config_response.data)

app = App.objects.get(id=app_id)
settings.REGISTRY_LOCATION = "off-cluster"
url = '/v2/apps/{app_id}/builds'.format(**locals())
body = {'image': 'test/autotest/example'}
response = self.client.post(url, body)
Expand All @@ -403,3 +403,24 @@ def test_release_external_registry(self, mock_requests):
self.assertEqual(release.get_port(), 3000)

self.assertEqual(release.image, 'test/autotest/example')

@override_settings(REGISTRY_LOCATION="off-cluster")
def test_release_external_registry_no_port(self, mock_requests):
"""
Test that an exception is raised when registry if off-cluster but
no port is provided.
"""
app_id = self.create_app()

app = App.objects.get(id=app_id)
url = '/v2/apps/{app_id}/builds'.format(**locals())
body = {'image': 'test/autotest/example'}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 400, response.data)

with self.assertRaises(
DeisException,
msg='PORT needs to be set in the config when using a private registry'
):
release = app.release_set.latest()
release.get_port()
16 changes: 12 additions & 4 deletions rootfs/scheduler/tests/test_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""
from django.core.cache import cache
from django.test import TestCase
from django.conf import settings
from django.test.utils import override_settings

from scheduler import mock
import base64
Expand Down Expand Up @@ -143,15 +143,21 @@ def test_get_private_registry_config(self):
self.assertEqual(secret_name, "private-registry")
self.assertEqual(secret_create, True)

settings.REGISTRY_LOCATION = "ecr"
@override_settings(REGISTRY_LOCATION="ecr")
def test_get_private_registry_config_ecr(self):
registry = {}
image = "test.com/test/test"
docker_config, secret_name, secret_create = self.scheduler._get_private_registry_config(registry, image) # noqa
self.assertEqual(docker_config, None)
self.assertEqual(secret_name, "private-registry-ecr")
self.assertEqual(secret_create, False)

settings.REGISTRY_LOCATION = "off-cluster"
@override_settings(REGISTRY_LOCATION="off-cluster")
def test_get_private_registry_config_off_cluster(self):
registry = {}
auth = bytes('{}:{}'.format("test", "test"), 'UTF-8')
encAuth = base64.b64encode(auth).decode(encoding='UTF-8')
image = "test.com/test/test"
docker_config, secret_name, secret_create = self.scheduler._get_private_registry_config(registry, image) # noqa
dockerConfig = json.loads(docker_config)
expected = {"https://index.docker.io/v1/": {
Expand All @@ -161,7 +167,9 @@ def test_get_private_registry_config(self):
self.assertEqual(secret_name, "private-registry-off-cluster")
self.assertEqual(secret_create, True)

settings.REGISTRY_LOCATION = "ecra"
@override_settings(REGISTRY_LOCATION="ecra")
def test_get_private_registry_config_bad_registry_location(self):
registry = {}
image = "test.com/test/test"
docker_config, secret_name, secret_create = self.scheduler._get_private_registry_config(registry, image) # noqa
self.assertEqual(docker_config, None)
Expand Down