From 62a27905ddc2dc9041353764351e42d6a780a0d2 Mon Sep 17 00:00:00 2001 From: Hunt Xu Date: Thu, 22 Mar 2018 20:52:49 +0800 Subject: [PATCH] add missing unit tests for methods of function controller Some methods of the function API controller are not well covered by the unit tests. Change-Id: I0afec56aff461dccabf09548a308cde7bf28e09f Story: 2001595 --- .../unit/api/controllers/v1/test_function.py | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/qinling/tests/unit/api/controllers/v1/test_function.py b/qinling/tests/unit/api/controllers/v1/test_function.py index 30e2defe..7557f601 100644 --- a/qinling/tests/unit/api/controllers/v1/test_function.py +++ b/qinling/tests/unit/api/controllers/v1/test_function.py @@ -201,3 +201,75 @@ class TestFunctionController(base.APITest): ) self.assertEqual(403, resp.status_int) + + @mock.patch('qinling.rpc.EngineClient.scaleup_function') + def test_scale_up(self, scaleup_function_mock): + db_func = self.create_function( + runtime_id=self.runtime_id, prefix=TEST_CASE_NAME + ) + + body = {'count': 1} + resp = self.app.post( + '/v1/functions/%s/scale_up' % db_func.id, + params=json.dumps(body), + content_type='application/json' + ) + + self.assertEqual(202, resp.status_int) + scaleup_function_mock.assert_called_once_with( + db_func.id, runtime_id=self.runtime_id, count=1) + + @mock.patch('qinling.utils.etcd_util.get_workers') + @mock.patch('qinling.rpc.EngineClient.scaledown_function') + def test_scale_down(self, scaledown_function_mock, get_workers_mock): + db_func = self.create_function( + runtime_id=self.runtime_id, prefix=TEST_CASE_NAME + ) + get_workers_mock.return_value = [mock.Mock(), mock.Mock()] + + body = {'count': 1} + resp = self.app.post( + '/v1/functions/%s/scale_down' % db_func.id, + params=json.dumps(body), + content_type='application/json' + ) + + self.assertEqual(202, resp.status_int) + scaledown_function_mock.assert_called_once_with(db_func.id, count=1) + + @mock.patch('qinling.utils.etcd_util.get_workers') + @mock.patch('qinling.rpc.EngineClient.scaledown_function') + def test_scale_down_no_need( + self, scaledown_function_mock, get_workers_mock + ): + db_func = self.create_function( + runtime_id=self.runtime_id, prefix=TEST_CASE_NAME + ) + get_workers_mock.return_value = [mock.Mock()] + + body = {'count': 1} + resp = self.app.post( + '/v1/functions/%s/scale_down' % db_func.id, + params=json.dumps(body), + content_type='application/json' + ) + + self.assertEqual(202, resp.status_int) + scaledown_function_mock.assert_not_called() + + @mock.patch('qinling.utils.etcd_util.delete_function') + @mock.patch('qinling.rpc.EngineClient.delete_function') + def test_detach( + self, engine_delete_function_mock, etcd_delete_function_mock + ): + db_func = self.create_function( + runtime_id=self.runtime_id, prefix=TEST_CASE_NAME + ) + + resp = self.app.post( + '/v1/functions/%s/detach' % db_func.id + ) + + self.assertEqual(202, resp.status_int) + engine_delete_function_mock.assert_called_once_with(db_func.id) + etcd_delete_function_mock.assert_called_once_with(db_func.id)