Exemple #1
0
    def test_kubernetes_service_failure(self):
        """
        Cause an Exception in kubernetes services
        """
        app_id = self.create_app()

        # scheduler.get_service exception
        with mock.patch('scheduler.KubeHTTPClient.get_service') as mock_kube:
            mock_kube.side_effect = KubeException('Boom!')
            domain = 'foo.com'
            url = '/v2/apps/{}/domains'.format(app_id)
            response = self.client.post(url, {'domain': domain})
            self.assertEqual(response.status_code, 503, response.data)

        # scheduler.update_service exception
        with mock.patch(
                'scheduler.KubeHTTPClient.update_service') as mock_kube:
            domain = 'foo.com'
            url = '/v2/apps/{}/domains'.format(app_id)
            response = self.client.post(url, {'domain': domain})
            self.assertEqual(response.status_code, 201, response.data)

            mock_kube.side_effect = KubeException('Boom!')
            url = '/v2/apps/{}/domains/{}'.format(app_id, domain)
            response = self.client.delete(url)
            self.assertEqual(response.status_code, 503, response.data)
Exemple #2
0
    def test_list_pods_failure(self, mock_requests):
        """
        Listing all available pods exceptions
        """

        app_id = self.create_app()

        with mock.patch('scheduler.resources.pod.Pod.get') as kube_pod:
            with mock.patch('scheduler.resources.pod.Pod.get') as kube_pods:
                kube_pod.side_effect = KubeException('boom!')
                kube_pods.side_effect = KubeException('boom!')
                url = "/v2/apps/{app_id}/pods".format(**locals())
                response = self.client.get(url)
                self.assertEqual(response.status_code, 503, response.data)
Exemple #3
0
    def test_list_pods_failure(self, mock_requests):
        """
        Listing all available pods exceptions
        """

        url = '/v2/apps'
        response = self.client.post(url)
        self.assertEqual(response.status_code, 201, response.data)
        app_id = response.data['id']

        with mock.patch('scheduler.KubeHTTPClient.get_pod') as kube_pod:
            with mock.patch('scheduler.KubeHTTPClient.get_pods') as kube_pods:
                kube_pod.side_effect = KubeException('boom!')
                kube_pods.side_effect = KubeException('boom!')
                url = "/v2/apps/{app_id}/pods".format(**locals())
                response = self.client.get(url)
                self.assertEqual(response.status_code, 503, response.data)
Exemple #4
0
 def test_app_create_failure_kubernetes_create(self, mock_requests):
     """
     Create an app but have scheduler.svc.create fail with an exception
     """
     with mock.patch('scheduler.resources.service.Service.create') as mock_kube:
         mock_kube.side_effect = KubeException('Boom!')
         response = self.client.post('/v2/apps')
         self.assertEqual(response.status_code, 503, response.data)
Exemple #5
0
 def test_app_create_failure_kubernetes_create(self, mock_requests):
     """
     Create an app but have scheduler.create_service fail with an exception
     """
     with mock.patch('scheduler.KubeHTTPClient.create_service') as mock_kube:
         mock_kube.side_effect = KubeException('Boom!')
         response = self.client.post('/v2/apps', {'id': 'test-kube'})
         self.assertEqual(response.status_code, 503, response.data)
Exemple #6
0
    def test_scale_errors(self, mock_requests):
        url = '/v2/apps'
        response = self.client.post(url)
        self.assertEqual(response.status_code, 201, response.data)
        app_id = response.data['id']

        # should start with zero
        url = "/v2/apps/{app_id}/pods".format(**locals())
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200, response.data)
        self.assertEqual(len(response.data['results']), 0)

        # post a new build
        url = "/v2/apps/{app_id}/builds".format(**locals())
        body = {
            'image': 'autotest/example',
            'sha': 'a'*40,
            'procfile': json.dumps({
                'web': 'node server.js',
                'worker': 'node worker.js'
            })
        }
        response = self.client.post(url, body)
        self.assertEqual(response.status_code, 201, response.data)

        # scale to a negative number
        url = "/v2/apps/{app_id}/scale".format(**locals())
        body = {'web': -1}
        response = self.client.post(url, body)
        self.assertEqual(response.status_code, 400, response.data)

        # scale to something other than a number
        url = "/v2/apps/{app_id}/scale".format(**locals())
        body = {'web': 'one'}
        response = self.client.post(url, body)
        self.assertEqual(response.status_code, 400, response.data)

        # scale to something other than a number
        url = "/v2/apps/{app_id}/scale".format(**locals())
        body = {'web': [1]}
        response = self.client.post(url, body)
        self.assertEqual(response.status_code, 400, response.data)

        # scale up to an integer as a sanity check
        url = "/v2/apps/{app_id}/scale".format(**locals())
        body = {'web': 1}
        response = self.client.post(url, body)
        self.assertEqual(response.status_code, 204, response.data)

        with mock.patch('scheduler.KubeHTTPClient.scale') as mock_kube:
            mock_kube.side_effect = KubeException('Boom!')
            url = "/v2/apps/{app_id}/scale".format(**locals())
            response = self.client.post(url, {'web': 10})
            self.assertEqual(response.status_code, 503, response.data)
Exemple #7
0
    def test_app_delete_failure_kubernetes_destroy(self, mock_requests):
        """
        Create an app and then delete but have scheduler.ns.delete
        fail with an exception
        """
        # create
        app_id = self.create_app()

        with mock.patch('scheduler.resources.namespace.Namespace.delete') as mock_kube:
            # delete
            mock_kube.side_effect = KubeException('Boom!')
            response = self.client.delete('/v2/apps/{}'.format(app_id))
            self.assertEqual(response.status_code, 503, response.data)
Exemple #8
0
    def test_build_deploy_kube_failure(self, mock_requests):
        """
        Cause an Exception in scheduler.deploy
        """
        app_id = self.create_app()

        with mock.patch('scheduler.KubeHTTPClient.deploy') as mock_deploy:
            mock_deploy.side_effect = KubeException('Boom!')

            url = "/v2/apps/{app_id}/builds".format(**locals())
            body = {'image': 'autotest/example'}
            response = self.client.post(url, body)
            self.assertEqual(response.status_code, 400, response.data)
    def test_kubernetes_service_failure(self, mock_requests):
        """
        Cause an Exception in kubernetes services
        """
        app_id = self.create_app()

        # scheduler.svc.update exception
        with mock.patch('scheduler.resources.service.Service.update') as mock_kube:
            mock_kube.side_effect = KubeException('Boom!')
            addresses = ["2.3.4.5"]
            url = '/v2/apps/{}/allowlist'.format(app_id)
            response = self.client.post(url, {'addresses': addresses})
            self.assertEqual(response.status_code, 201, response.data)
Exemple #10
0
    def test_app_delete_failure_kubernetes_destroy(self, mock_requests):
        """
        Create an app and then delete but have scheduler.delete_namespace
        fail with an exception
        """
        # create
        response = self.client.post('/v2/apps', {'id': 'test'})
        self.assertEqual(response.status_code, 201, response.data)

        with mock.patch('scheduler.KubeHTTPClient.delete_namespace') as mock_kube:
            # delete
            mock_kube.side_effect = KubeException('Boom!')
            response = self.client.delete('/v2/apps/test')
            self.assertEqual(response.status_code, 503, response.data)
Exemple #11
0
    def test_build_deploy_kube_failure(self, mock_requests):
        """
        Cause an Exception in scheduler.deploy
        """
        body = {'id': 'test'}
        self.client.post('/v2/apps', body)

        with mock.patch('scheduler.KubeHTTPClient.deploy') as mock_deploy:
            mock_deploy.side_effect = KubeException('Boom!')

            url = "/v2/apps/test/builds"
            body = {'image': 'autotest/example'}
            response = self.client.post(url, body)
            self.assertEqual(response.status_code, 400, response.data)
Exemple #12
0
    def test_run_failure(self, mock_requests):
        """Raise a KubeException via scheduler.run"""
        app_id = self.create_app()

        # create build
        body = {'image': 'autotest/example', 'stack': 'container'}
        url = '/v2/apps/{app_id}/builds'.format(**locals())
        response = self.client.post(url, body)
        self.assertEqual(response.status_code, 201, response.data)

        with mock.patch('scheduler.KubeHTTPClient.run') as kube_run:
            kube_run.side_effect = KubeException('boom!')
            # run command
            url = '/v2/apps/{}/run'.format(app_id)
            body = {'command': 'ls -al'}
            response = self.client.post(url, body)
            self.assertEqual(response.status_code, 503, response.data)
Exemple #13
0
    def test_scale_errors(self, mock_requests):
        app_id = self.create_app()

        # should start with zero
        url = "/v2/apps/{app_id}/pods".format(**locals())
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200, response.data)
        self.assertEqual(len(response.data['results']), 0)

        # post a new build
        url = "/v2/apps/{app_id}/builds".format(**locals())
        body = {
            'image': 'autotest/example',
            'sha': 'a' * 40,
            'procfile': {
                'web': 'node server.js',
                'worker': 'node worker.js'
            }
        }
        response = self.client.post(url, body)
        self.assertEqual(response.status_code, 201, response.data)

        # scale to a negative number
        url = "/v2/apps/{app_id}/scale".format(**locals())
        body = {'web': -1}
        response = self.client.post(url, body)
        self.assertEqual(response.status_code, 400, response.data)
        self.assertEqual(
            response.data, {
                "detail":
                "Invalid scaling format: "
                "[ErrorDetail(string='Must be greater "
                "than or equal to zero', code='invalid')]"
            })

        # scale to something other than a number
        url = "/v2/apps/{app_id}/scale".format(**locals())
        body = {'web': 'one'}
        response = self.client.post(url, body)
        self.assertEqual(response.status_code, 400, response.data)

        # scale to something other than a number
        url = "/v2/apps/{app_id}/scale".format(**locals())
        body = {'web': [1]}
        response = self.client.post(url, body)
        self.assertEqual(response.status_code, 400, response.data)

        # scale with a non-existent proc type
        url = "/v2/apps/{app_id}/scale".format(**locals())
        body = {'foo': 1}
        response = self.client.post(url, body)
        self.assertEqual(response.status_code, 404, response.data)

        # scale up to an integer as a sanity check
        url = "/v2/apps/{app_id}/scale".format(**locals())
        body = {'web': 1}
        response = self.client.post(url, body)
        self.assertEqual(response.status_code, 204, response.data)

        with mock.patch('scheduler.KubeHTTPClient.scale') as mock_kube:
            mock_kube.side_effect = KubeException('Boom!')
            url = "/v2/apps/{app_id}/scale".format(**locals())
            response = self.client.post(url, {'web': 10})
            self.assertEqual(response.status_code, 503, response.data)
Exemple #14
0
    def test_scale_errors(self, mock_requests):
        app_id = self.create_app()

        # should start with zero
        url = "/v2/apps/{app_id}/pods".format(**locals())
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200, response.data)
        self.assertEqual(len(response.data['results']), 0)

        # post a new build
        url = "/v2/apps/{app_id}/builds".format(**locals())
        body = {
            'image': 'autotest/example',
            'stack': 'heroku-18',
            'sha': 'a' * 40,
            'procfile': {
                'web': 'node server.js',
                'worker': 'node worker.js'
            }
        }
        response = self.client.post(url, body)
        self.assertEqual(response.status_code, 201, response.data)

        # scale to a negative number
        url = "/v2/apps/{app_id}/scale".format(**locals())
        body = {'web': -1}
        response = self.client.post(url, body)
        self.assertEqual(response.status_code, 204, response.data)
        app = App.objects.get(id=app_id)
        self.assertEqual(app.structure["web"], 1)

        # scale to something other than a number
        url = "/v2/apps/{app_id}/scale".format(**locals())
        body = {'web': 'one'}
        response = self.client.post(url, body)
        app = App.objects.get(id=app_id)
        self.assertEqual(response.status_code, 204, response.data)
        self.assertEqual(app.structure["web"], 1)

        # scale to something other than a number
        url = "/v2/apps/{app_id}/scale".format(**locals())
        body = {'web': [1]}
        response = self.client.post(url, body)
        self.assertEqual(response.status_code, 204, response.data)
        app = App.objects.get(id=app_id)
        self.assertEqual(app.structure["web"], 1)

        # scale with a non-existent proc type
        url = "/v2/apps/{app_id}/scale".format(**locals())
        body = {'foo': 1}
        response = self.client.post(url, body)
        self.assertEqual(response.status_code, 204, response.data)
        app = App.objects.get(id=app_id)
        self.assertEqual("foo" in app.structure, False)

        # scale up to an integer as a sanity check
        url = "/v2/apps/{app_id}/scale".format(**locals())
        body = {'web': 1}
        response = self.client.post(url, body)
        self.assertEqual(response.status_code, 204, response.data)
        app = App.objects.get(id=app_id)
        self.assertEqual(app.structure["web"], 1)

        with mock.patch('scheduler.KubeHTTPClient.scale') as mock_kube:
            mock_kube.side_effect = KubeException('Boom!')
            url = "/v2/apps/{app_id}/scale".format(**locals())
            response = self.client.post(url, {'web': 10})
            self.assertEqual(response.status_code, 204, response.data)
            app = App.objects.get(id=app_id)
            self.assertEqual(app.structure["web"], 1)