Skip to content

Commit b8aa206

Browse files
author
Matthew Fisher
authored
fix(api): check if release.build is NoneType (#1078)
1 parent 22f96eb commit b8aa206

2 files changed

Lines changed: 36 additions & 3 deletions

File tree

rootfs/api/models/app.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
from api.models import UuidAuditedModel, AlreadyExists, DeisException, ServiceUnavailable
2424

2525
from api.utils import generate_app_name, async_run
26-
from api.models.release import Release
2726
from api.models.config import Config
2827
from api.models.domain import Domain
28+
from api.models.release import Release
2929
from api.models.tls import TLS
3030
from api.models.appsettings import AppSettings
3131

@@ -786,6 +786,8 @@ def _build_env_vars(self, release):
786786
Build a dict of env vars, setting default vars based on app type
787787
and then combining with the user set ones
788788
"""
789+
if release.build is None:
790+
raise DeisException('No build associated with this release to run this command')
789791

790792
# mix in default environment information deis may require
791793
default_env = {
@@ -803,8 +805,7 @@ def _build_env_vars(self, release):
803805

804806
# fetch application port and inject into ENV vars as needed
805807
port = release.get_port()
806-
if port:
807-
default_env['PORT'] = port
808+
default_env['PORT'] = port
808809

809810
# merge envs on top of default to make envs win
810811
default_env.update(release.config.values)

rootfs/api/tests/test_app.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from api.models import App
2020
from scheduler import KubeException
2121

22+
from api.exceptions import DeisException
2223
from api.tests import adapter, mock_port, DeisTestCase
2324
import requests_mock
2425

@@ -572,6 +573,37 @@ def test_get_private_registry_config_bad_registry_location(self, mock_requests):
572573
self.assertEqual(name, None)
573574
self.assertEqual(create, None)
574575

576+
def test_build_env_vars(self, mock_requests):
577+
app = App.objects.create(owner=self.user)
578+
# Make sure an exception is raised when calling without a build available
579+
with self.assertRaises(DeisException):
580+
app._build_env_vars(app.release_set.latest())
581+
data = {'image': 'autotest/example'}
582+
url = "/v2/apps/{app.id}/builds".format(**locals())
583+
response = self.client.post(url, data)
584+
self.assertEqual(response.status_code, 201, response.data)
585+
self.assertEqual(
586+
app._build_env_vars(app.release_set.latest()),
587+
{
588+
'DEIS_APP': app.id,
589+
'WORKFLOW_RELEASE': 'v2',
590+
'PORT': 5000
591+
})
592+
data['sha'] = 'abc1234'
593+
response = self.client.post(url, data)
594+
self.assertEqual(response.status_code, 201, response.data)
595+
self.assertEqual(
596+
app._build_env_vars(app.release_set.latest()),
597+
{
598+
'DEIS_APP': app.id,
599+
'WORKFLOW_RELEASE': 'v3',
600+
'PORT': 5000,
601+
'SLUG_URL': 'autotest/example',
602+
'BUILDER_STORAGE': None,
603+
'DEIS_MINIO_SERVICE_HOST': '127.0.0.1',
604+
'DEIS_MINIO_SERVICE_PORT': 80
605+
})
606+
575607

576608
FAKE_LOG_DATA = bytes("""
577609
2013-08-15 12:41:25 [33454] [INFO] Starting gunicorn 17.5

0 commit comments

Comments
 (0)