github.com/blystad/deis@v0.11.0/controller/api/tests/test_cluster.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  
    11  from django.test import TestCase
    12  from django.test.utils import override_settings
    13  
    14  
    15  @override_settings(CELERY_ALWAYS_EAGER=True)
    16  class ClusterTest(TestCase):
    17  
    18      """Tests cluster management"""
    19  
    20      fixtures = ['tests.json']
    21  
    22      def setUp(self):
    23          self.assertTrue(
    24              self.client.login(username='autotest', password='password'))
    25  
    26      def test_cluster(self):
    27          """
    28          Test that an administrator can create, read, update and delete a cluster
    29          """
    30          url = '/api/clusters'
    31          options = {'key': 'val'}
    32          body = {'id': 'autotest', 'domain': 'autotest.local', 'type': 'mock',
    33                  'hosts': 'host1;host2', 'auth': 'base64string', 'options': options}
    34          response = self.client.post(url, json.dumps(body), content_type='application/json')
    35          self.assertEqual(response.status_code, 400)
    36          body['hosts'] = 'host1,host2'
    37          response = self.client.post(url, json.dumps(body), content_type='application/json')
    38          self.assertEqual(response.status_code, 201)
    39          cluster_id = response.data['id']  # noqa
    40          self.assertIn('owner', response.data)
    41          self.assertIn('id', response.data)
    42          self.assertIn('domain', response.data)
    43          self.assertIn('hosts', response.data)
    44          self.assertIn('auth', response.data)
    45          self.assertIn('options', response.data)
    46          self.assertEqual(response.data['hosts'], 'host1,host2')
    47          self.assertEqual(json.loads(response.data['options']), {'key': 'val'})
    48          response = self.client.get('/api/clusters')
    49          self.assertEqual(response.status_code, 200)
    50          self.assertEqual(len(response.data['results']), 1)
    51          # ensure we can delete the cluster with an app
    52          # see https://github.com/deis/deis/issues/927
    53          url = '/api/apps'
    54          body = {'cluster': 'autotest'}
    55          response = self.client.post(url, json.dumps(body), content_type='application/json')
    56          self.assertEqual(response.status_code, 201)
    57          url = '/api/clusters/{cluster_id}'.format(**locals())
    58          response = self.client.get(url)
    59          self.assertEqual(response.status_code, 200)
    60          # regression test for https://github.com/deis/deis/issues/1552
    61          body = {'hosts': 'host2 host3'}
    62          response = self.client.patch(url, json.dumps(body), content_type='application/json')
    63          self.assertEqual(response.status_code, 400)
    64          body = {'hosts': 'host2;host3'}
    65          response = self.client.patch(url, json.dumps(body), content_type='application/json')
    66          self.assertEqual(response.status_code, 400)
    67          # update cluster hosts
    68          new_hosts, new_options = 'host2.domain,host3.sub.domain,127.0.0.1', {'key': 'val2'}
    69          body = {'hosts': new_hosts, 'options': new_options}
    70          response = self.client.patch(url, json.dumps(body), content_type='application/json')
    71          self.assertEqual(response.status_code, 200)
    72          self.assertEqual(response.data['hosts'], new_hosts)
    73          self.assertEqual(json.loads(response.data['options']), new_options)
    74          response = self.client.delete(url)
    75          self.assertEqual(response.status_code, 204)
    76  
    77      def test_cluster_perms_denied(self):
    78          """
    79          Test that a user cannot make changes to a cluster
    80          """
    81          url = '/api/clusters'
    82          options = {'key': 'val'}
    83          self.client.login(username='autotest2', password='password')
    84          body = {'id': 'autotest2', 'domain': 'autotest.local', 'type': 'mock',
    85                  'hosts': 'host1,host2', 'auth': 'base64string', 'options': options}
    86          response = self.client.post(url, json.dumps(body), content_type='application/json')
    87          self.assertEqual(response.status_code, 403)
    88  
    89      def test_cluster_errors(self):
    90          """
    91          Tests bad inputs to clusters:create and clusters:update.
    92          """
    93          url = '/api/clusters'
    94          options = {'key': 'val'}
    95          body = {'id': 'autotest', 'domain': 'http://bad.domain', 'type': 'mock',
    96                  'hosts': 'host1,host2', 'auth': 'base64string', 'options': options}
    97          response = self.client.post(url, json.dumps(body), content_type='application/json')
    98          self.assertEqual(response.status_code, 400)
    99          body['domain'] = 'autotest.local'
   100          response = self.client.post(url, json.dumps(body), content_type='application/json')
   101          self.assertEqual(response.status_code, 201)
   102          cluster_id = response.data['id']  # noqa
   103          url = '/api/clusters/{cluster_id}'.format(**locals())
   104          body = {'domain': 'http://bad.domain'}
   105          response = self.client.patch(url, json.dumps(body), content_type='application/json')
   106          self.assertEqual(response.status_code, 400)