1111from django .core .cache import cache
1212from django .conf import settings
1313from django .test import override_settings
14- from rest_framework .test import APITransactionTestCase
1514from unittest import mock
1615from rest_framework .authtoken .models import Token
1716
1817from api .models import Build
1918from registry .dockerclient import RegistryException
2019from scheduler import KubeException
2120
22- from api .tests import adapter
23- from api .tests import mock_port
21+ from api .tests import adapter , mock_port , DeisTransactionTestCase
2422import requests_mock
2523
2624
2725@override_settings (DEIS_KUBERNETES_DEPLOYMENTS = '1' )
2826@requests_mock .Mocker (real_http = True , adapter = adapter )
2927@mock .patch ('api.models.release.publish_release' , lambda * args : None )
3028@mock .patch ('api.models.release.docker_get_port' , mock_port )
31- class BuildTest (APITransactionTestCase ):
29+ class BuildTest (DeisTransactionTestCase ):
3230
3331 """Tests build notification from build system"""
3432
@@ -47,10 +45,7 @@ def test_build(self, mock_requests):
4745 """
4846 Test that a null build is created and that users can post new builds
4947 """
50- url = '/v2/apps'
51- response = self .client .post (url )
52- self .assertEqual (response .status_code , 201 , response .data )
53- app_id = response .data ['id' ]
48+ app_id = self .create_app ()
5449 # check to see that no initial build was created
5550 url = "/v2/apps/{app_id}/builds" .format (** locals ())
5651 response = self .client .get (url )
@@ -109,10 +104,7 @@ def test_response_data(self, mock_requests):
109104 self .assertDictContainsSubset (expected , response .data )
110105
111106 def test_build_default_containers (self , mock_requests ):
112- url = '/v2/apps'
113- response = self .client .post (url )
114- self .assertEqual (response .status_code , 201 , response .data )
115- app_id = response .data ['id' ]
107+ app_id = self .create_app ()
116108 # post an image as a build
117109 url = "/v2/apps/{app_id}/builds" .format (** locals ())
118110 body = {'image' : 'autotest/example' }
@@ -133,10 +125,7 @@ def test_build_default_containers(self, mock_requests):
133125 self .assertRegex (container ['name' ], app_id + '-v2-cmd-[a-z0-9]{5}' )
134126
135127 # start with a new app
136- url = '/v2/apps'
137- response = self .client .post (url )
138- self .assertEqual (response .status_code , 201 , response .data )
139- app_id = response .data ['id' ]
128+ app_id = self .create_app ()
140129 # post a new build with procfile
141130 url = "/v2/apps/{app_id}/builds" .format (** locals ())
142131 body = {
@@ -161,10 +150,7 @@ def test_build_default_containers(self, mock_requests):
161150 self .assertRegex (container ['name' ], app_id + '-v2-cmd-[a-z0-9]{5}' )
162151
163152 # start with a new app
164- url = '/v2/apps'
165- response = self .client .post (url )
166- self .assertEqual (response .status_code , 201 , response .data )
167- app_id = response .data ['id' ]
153+ app_id = self .create_app ()
168154
169155 # post a new build with procfile
170156 url = "/v2/apps/{app_id}/builds" .format (** locals ())
@@ -193,10 +179,7 @@ def test_build_default_containers(self, mock_requests):
193179 self .assertRegex (container ['name' ], app_id + '-v2-cmd-[a-z0-9]{5}' )
194180
195181 # start with a new app
196- url = '/v2/apps'
197- response = self .client .post (url )
198- self .assertEqual (response .status_code , 201 , response .data )
199- app_id = response .data ['id' ]
182+ app_id = self .create_app ()
200183 # post a new build with procfile
201184
202185 url = "/v2/apps/{app_id}/builds" .format (** locals ())
@@ -226,10 +209,7 @@ def test_build_default_containers(self, mock_requests):
226209
227210 def test_build_str (self , mock_requests ):
228211 """Test the text representation of a build."""
229- url = '/v2/apps'
230- response = self .client .post (url )
231- self .assertEqual (response .status_code , 201 , response .data )
232- app_id = response .data ['id' ]
212+ app_id = self .create_app ()
233213 # post a new build
234214 url = "/v2/apps/{app_id}/builds" .format (** locals ())
235215 body = {'image' : 'autotest/example' }
@@ -248,10 +228,7 @@ def test_admin_can_create_builds_on_other_apps(self, mock_requests):
248228 token = Token .objects .get (user = user ).key
249229 self .client .credentials (HTTP_AUTHORIZATION = 'Token ' + token )
250230
251- url = '/v2/apps'
252- response = self .client .post (url )
253- self .assertEqual (response .status_code , 201 , response .data )
254- app_id = response .data ['id' ]
231+ app_id = self .create_app ()
255232
256233 # post a new build as admin
257234 self .client .credentials (HTTP_AUTHORIZATION = 'Token ' + self .token )
@@ -289,10 +266,7 @@ def test_new_build_does_not_scale_up_automatically(self, mock_requests):
289266 After the first initial deploy, if the containers are scaled down to zero,
290267 they should stay that way on a new release.
291268 """
292- url = '/v2/apps'
293- response = self .client .post (url )
294- self .assertEqual (response .status_code , 201 , response .data )
295- app_id = response .data ['id' ]
269+ app_id = self .create_app ()
296270
297271 # post a new build
298272 url = "/v2/apps/{app_id}/builds" .format (** locals ())
0 commit comments