github.com/blystad/deis@v0.11.0/controller/api/tests/test_build.py (about)

     1  """
     2  Unit tests for the Deis api app.
     3  
     4  Run the tests with "./manage.py test api"
     5  """
     6  
     7  from __future__ import unicode_literals
     8  
     9  import json
    10  import mock
    11  import requests
    12  
    13  from django.test import TransactionTestCase
    14  from django.test.utils import override_settings
    15  
    16  from api.models import Build
    17  
    18  
    19  def mock_import_repository_task(*args, **kwargs):
    20      resp = requests.Response()
    21      resp.status_code = 200
    22      resp._content_consumed = True
    23      return resp
    24  
    25  
    26  @override_settings(CELERY_ALWAYS_EAGER=True)
    27  class BuildTest(TransactionTestCase):
    28  
    29      """Tests build notification from build system"""
    30  
    31      fixtures = ['tests.json']
    32  
    33      def setUp(self):
    34          self.assertTrue(
    35              self.client.login(username='autotest', password='password'))
    36          body = {'id': 'autotest', 'domain': 'autotest.local', 'type': 'mock',
    37                  'hosts': 'host1,host2', 'auth': 'base64string', 'options': {}}
    38          response = self.client.post('/api/clusters', json.dumps(body),
    39                                      content_type='application/json')
    40          self.assertEqual(response.status_code, 201)
    41  
    42      @mock.patch('requests.post', mock_import_repository_task)
    43      def test_build(self):
    44          """
    45          Test that a null build is created and that users can post new builds
    46          """
    47          url = '/api/apps'
    48          body = {'cluster': 'autotest'}
    49          response = self.client.post(url, json.dumps(body), content_type='application/json')
    50          self.assertEqual(response.status_code, 201)
    51          app_id = response.data['id']
    52          # check to see that an initial build was created
    53          url = "/api/apps/{app_id}/builds".format(**locals())
    54          response = self.client.get(url)
    55          self.assertEqual(response.status_code, 200)
    56          self.assertEqual(response.data['count'], 1)
    57          # post a new build
    58          body = {'image': 'autotest/example'}
    59          response = self.client.post(url, json.dumps(body), content_type='application/json')
    60          self.assertEqual(response.status_code, 201)
    61          build_id = response.data['uuid']
    62          build1 = response.data
    63          self.assertEqual(response.data['image'], body['image'])
    64          # read the build
    65          url = "/api/apps/{app_id}/builds/{build_id}".format(**locals())
    66          response = self.client.get(url)
    67          self.assertEqual(response.status_code, 200)
    68          build2 = response.data
    69          self.assertEqual(build1, build2)
    70          # post a new build
    71          url = "/api/apps/{app_id}/builds".format(**locals())
    72          body = {'image': 'autotest/example'}
    73          response = self.client.post(url, json.dumps(body), content_type='application/json')
    74          self.assertEqual(response.status_code, 201)
    75          self.assertIn('x-deis-release', response._headers)
    76          build3 = response.data
    77          self.assertEqual(response.data['image'], body['image'])
    78          self.assertNotEqual(build2['uuid'], build3['uuid'])
    79          # disallow put/patch/delete
    80          self.assertEqual(self.client.put(url).status_code, 405)
    81          self.assertEqual(self.client.patch(url).status_code, 405)
    82          self.assertEqual(self.client.delete(url).status_code, 405)
    83  
    84      @mock.patch('requests.post', mock_import_repository_task)
    85      def test_build_default_containers(self):
    86          url = '/api/apps'
    87          body = {'cluster': 'autotest'}
    88          response = self.client.post(url, json.dumps(body), content_type='application/json')
    89          self.assertEqual(response.status_code, 201)
    90          app_id = response.data['id']
    91          # post an image as a build
    92          url = "/api/apps/{app_id}/builds".format(**locals())
    93          body = {'image': 'autotest/example'}
    94          response = self.client.post(url, json.dumps(body), content_type='application/json')
    95          self.assertEqual(response.status_code, 201)
    96          url = "/api/apps/{app_id}/containers/cmd".format(**locals())
    97          response = self.client.get(url)
    98          self.assertEqual(response.status_code, 200)
    99          self.assertEqual(len(response.data['results']), 1)
   100          container = response.data['results'][0]
   101          self.assertEqual(container['type'], 'cmd')
   102          self.assertEqual(container['num'], 1)
   103          # start with a new app
   104          url = '/api/apps'
   105          body = {'cluster': 'autotest'}
   106          response = self.client.post(url, json.dumps(body), content_type='application/json')
   107          self.assertEqual(response.status_code, 201)
   108          app_id = response.data['id']
   109          # post a new build with procfile
   110          url = "/api/apps/{app_id}/builds".format(**locals())
   111          body = {'image': 'autotest/example',
   112                  'sha': 'a'*40,
   113                  'dockerfile': "FROM scratch"}
   114          response = self.client.post(url, json.dumps(body), content_type='application/json')
   115          self.assertEqual(response.status_code, 201)
   116          url = "/api/apps/{app_id}/containers/cmd".format(**locals())
   117          response = self.client.get(url)
   118          self.assertEqual(response.status_code, 200)
   119          self.assertEqual(len(response.data['results']), 1)
   120          container = response.data['results'][0]
   121          self.assertEqual(container['type'], 'cmd')
   122          self.assertEqual(container['num'], 1)
   123          # start with a new app
   124          url = '/api/apps'
   125          body = {'cluster': 'autotest'}
   126          response = self.client.post(url, json.dumps(body), content_type='application/json')
   127          self.assertEqual(response.status_code, 201)
   128          app_id = response.data['id']
   129          # post a new build with procfile
   130          url = "/api/apps/{app_id}/builds".format(**locals())
   131          body = {'image': 'autotest/example',
   132                  'sha': 'a'*40,
   133                  'dockerfile': "FROM scratch",
   134                  'procfile': {'worker': 'node worker.js'}}
   135          response = self.client.post(url, json.dumps(body), content_type='application/json')
   136          self.assertEqual(response.status_code, 201)
   137          url = "/api/apps/{app_id}/containers/cmd".format(**locals())
   138          response = self.client.get(url)
   139          self.assertEqual(response.status_code, 200)
   140          self.assertEqual(len(response.data['results']), 1)
   141          container = response.data['results'][0]
   142          self.assertEqual(container['type'], 'cmd')
   143          self.assertEqual(container['num'], 1)
   144          # start with a new app
   145          url = '/api/apps'
   146          body = {'cluster': 'autotest'}
   147          response = self.client.post(url, json.dumps(body), content_type='application/json')
   148          self.assertEqual(response.status_code, 201)
   149          app_id = response.data['id']
   150          # post a new build with procfile
   151          url = "/api/apps/{app_id}/builds".format(**locals())
   152          body = {'image': 'autotest/example',
   153                  'sha': 'a'*40,
   154                  'procfile': json.dumps({'web': 'node server.js',
   155                                          'worker': 'node worker.js'})}
   156          response = self.client.post(url, json.dumps(body), content_type='application/json')
   157          self.assertEqual(response.status_code, 201)
   158          url = "/api/apps/{app_id}/containers/web".format(**locals())
   159          response = self.client.get(url)
   160          self.assertEqual(response.status_code, 200)
   161          self.assertEqual(len(response.data['results']), 1)
   162          container = response.data['results'][0]
   163          self.assertEqual(container['type'], 'web')
   164          self.assertEqual(container['num'], 1)
   165  
   166      @mock.patch('requests.post', mock_import_repository_task)
   167      def test_build_str(self):
   168          """Test the text representation of a build."""
   169          url = '/api/apps'
   170          body = {'cluster': 'autotest'}
   171          response = self.client.post(url, json.dumps(body), content_type='application/json')
   172          self.assertEqual(response.status_code, 201)
   173          app_id = response.data['id']
   174          # post a new build
   175          url = "/api/apps/{app_id}/builds".format(**locals())
   176          body = {'image': 'autotest/example'}
   177          response = self.client.post(url, json.dumps(body), content_type='application/json')
   178          self.assertEqual(response.status_code, 201)
   179          build = Build.objects.get(uuid=response.data['uuid'])
   180          self.assertEqual(str(build), "{}-{}".format(
   181                           response.data['app'], response.data['uuid'][:7]))