diff options
author | Bin Yang <bin.yang@windriver.com> | 2017-10-23 11:23:25 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2017-10-23 11:23:25 +0000 |
commit | 7898e7a0c1f2ef642d43b9c0b803ed3d3f3c9cd0 (patch) | |
tree | 49920ebc35bf306225f36baa2c4ef96064672d00 | |
parent | d0fcc2312d1f54bed254f186a0baef522e420f63 (diff) | |
parent | 2264c0f8d0644770c747c99e23fd0af064a05b03 (diff) |
Merge "Add Unit Test Abstract Base in test_base"
-rw-r--r-- | newton/newton/requests/tests/test_base.py | 180 | ||||
-rw-r--r-- | newton/newton/requests/tests/test_image.py | 207 |
2 files changed, 227 insertions, 160 deletions
diff --git a/newton/newton/requests/tests/test_base.py b/newton/newton/requests/tests/test_base.py index 06f610cb..2ba05cb0 100644 --- a/newton/newton/requests/tests/test_base.py +++ b/newton/newton/requests/tests/test_base.py @@ -16,8 +16,12 @@ import mock from rest_framework import status import unittest +from abc import ABCMeta from django.test import Client +from newton.requests.tests import mock_info +from newton.requests.views.util import VimDriverUtils + class MockResponse(object): status_code = status.HTTP_200_OK @@ -53,3 +57,179 @@ class TestRequest(unittest.TestCase): def setUp(self): self.client = Client() + + +class AbstractTestResource(object): + __metaclass__ = ABCMeta + + def __init__(self): + self.openstack_version = "" + self.region = "" + self.resource_name = "" + + self.MOCK_GET_RESOURCES_RESPONSE = {} + self.MOCK_GET_RESOURCE_RESPONSE = {} + + self.MOCK_POST_RESOURCE_REQUEST = {} + self.MOCK_POST_RESOURCE_REQUEST_EXISTING = {} + + self.MOCK_POST_RESOURCE_RESPONSE = {} + + self.assert_keys = "" + self.assert_key = "" + + self.HTTP_not_found = status.HTTP_404_NOT_FOUND + + @mock.patch.object(VimDriverUtils, 'get_session') + @mock.patch.object(VimDriverUtils, 'get_vim_info') + def test_get_resources_list(self, mock_get_vim_info, mock_get_session): + mock_get_session.side_effect = [ + get_mock_session( + ["get"], {"get": {"content": self.MOCK_GET_RESOURCES_RESPONSE}}), + ] + + mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO + + url = "/api/multicloud-{}/v0/{}/fcca3cc49d5e42caae15459e27103efc/{}".format( + self.openstack_version, self.region, self.resource_name + ) + + response = self.client.get(url, {}, HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID) + + context = response.json() + self.assertEquals(status.HTTP_200_OK, response.status_code) + self.assertIsNotNone(context[self.assert_keys]) + self.assertEqual( + self.MOCK_GET_RESOURCES_RESPONSE[self.assert_keys], context[self.assert_keys]) + + @mock.patch.object(VimDriverUtils, 'get_session') + @mock.patch.object(VimDriverUtils, 'get_vim_info') + def test_get_resource_info(self, mock_get_vim_info, mock_get_session): + mock_get_session.side_effect = [ + get_mock_session( + ["get"], {"get": {"content": self.MOCK_GET_RESOURCE_RESPONSE}}), + ] + + mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO + + url = "/api/multicloud-{}/v0/{}/fcca3cc49d5e42caae15459e27103efc/{}".format( + self.openstack_version, self.region, self.resource_name + ) + + response = self.client.get(url + "/uuid_1", {}, HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID) + + context = response.json() + self.assertEquals(status.HTTP_200_OK, response.status_code) + self.assertEquals( + self.MOCK_GET_RESOURCE_RESPONSE[self.assert_key], context[self.assert_key]) + + @mock.patch.object(VimDriverUtils, 'get_session') + @mock.patch.object(VimDriverUtils, 'get_vim_info') + def test_get_resource_not_found(self, mock_get_vim_info, mock_get_session): + mock_get_session.side_effect = [ + get_mock_session( + ["get"], {"get": {"content": {}, + "status_code": 404}}), + ] + + url = "/api/multicloud-{}/v0/{}/fcca3cc49d5e42caae15459e27103efc/{}".format( + self.openstack_version, self.region, self.resource_name + ) + + mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO + + response = self.client.get(url + "/uuid_3", {}, HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID) + + self.assertEquals(self.HTTP_not_found, response.status_code) + + @mock.patch.object(VimDriverUtils, 'get_session') + @mock.patch.object(VimDriverUtils, 'get_vim_info') + def test_post_resource(self, mock_get_vim_info, mock_get_session): + mock_get_session.side_effect = [ + get_mock_session( + ["get"], {"get": {"content": self.MOCK_GET_RESOURCE_RESPONSE}}), + get_mock_session( + ["post"], {"post": {"content": self.MOCK_POST_RESOURCE_RESPONSE, + "status_code": 202}}), + ] + + url = "/api/multicloud-{}/v0/{}/fcca3cc49d5e42caae15459e27103efc/{}".format( + self.openstack_version, self.region, self.resource_name + ) + + mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO + + response = self.client.post(url, self.MOCK_POST_RESOURCE_REQUEST, HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID) + + context = response.json() + self.assertEquals(status.HTTP_202_ACCEPTED, response.status_code) + self.assertIsNotNone(context['id']) + self.assertEqual(1, context['returnCode']) + + @mock.patch.object(VimDriverUtils, 'get_session') + @mock.patch.object(VimDriverUtils, 'get_vim_info') + def test_post_resource_existing(self, mock_get_vim_info, mock_get_session): + mock_get_session.side_effect = [ + get_mock_session( + ["get"], {"get": {"content": self.MOCK_GET_RESOURCES_RESPONSE}}), + get_mock_session( + ["post"], {"post": {"content": self.MOCK_POST_RESOURCE_RESPONSE, + "status_code": 201}}), + ] + + url = "/api/multicloud-{}/v0/{}/fcca3cc49d5e42caae15459e27103efc/{}".format( + self.openstack_version, self.region, self.resource_name + ) + + mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO + + response = self.client.post(url, self.MOCK_POST_RESOURCE_REQUEST_EXISTING, HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID) + + context = response.json() + self.assertEquals(status.HTTP_200_OK, response.status_code) + self.assertIsNotNone(context['returnCode']) + self.assertEqual(0, context['returnCode']) + + @mock.patch.object(VimDriverUtils, 'get_session') + @mock.patch.object(VimDriverUtils, 'get_vim_info') + def test_post_resource_empty(self, mock_get_vim_info, mock_get_session): + mock_get_session.side_effect = [ + get_mock_session( + ["get"], {"get": {"content": self.MOCK_GET_RESOURCE_RESPONSE}}), + get_mock_session( + ["post"], {"post": {"content": self.MOCK_POST_RESOURCE_RESPONSE, + "status_code": 202}}), + ] + + url = "/api/multicloud-{}/v0/{}/fcca3cc49d5e42caae15459e27103efc/{}".format( + self.openstack_version, self.region, self.resource_name + ) + + mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO + + response = self.client.post(url, {}, HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID) + + context = response.json() + self.assertIn('error', context) + self.assertEquals(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code) + + @mock.patch.object(VimDriverUtils, 'get_session') + @mock.patch.object(VimDriverUtils, 'get_vim_info') + def test_delete_resource(self, mock_get_vim_info, mock_get_session): + + mock_get_session.side_effect = [ + get_mock_session( + ["delete"], {"delete": {"content": {}, + "status_code": 204}}) + ] + + url = "/api/multicloud-{}/v0/{}/fcca3cc49d5e42caae15459e27103efc/{}".format( + self.openstack_version, self.region, self.resource_name + ) + + mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO + + response = self.client.delete(url + "/uuid_1", HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID) + + self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code) + self.assertIsNone(response.data) diff --git a/newton/newton/requests/tests/test_image.py b/newton/newton/requests/tests/test_image.py index 467e4710..1e747630 100644 --- a/newton/newton/requests/tests/test_image.py +++ b/newton/newton/requests/tests/test_image.py @@ -21,108 +21,55 @@ from rest_framework import status from newton.requests.tests import mock_info from newton.requests.tests import test_base +from newton.requests.tests.test_base import AbstractTestResource from newton.requests.views.image import imageThread from newton.requests.views.util import VimDriverUtils -MOCK_GET_IMAGES_RESPONSE = { - "images": [ - {"id": "uuid_1", "name": "image_1"}, - {"id": "uuid_2", "name": "image_2"} - ] -} +class TestImageNewton(unittest.TestCase, AbstractTestResource): -MOCK_GET_IMAGE_RESPONSE = { - "image": { - "id": "uuid_1", - "name": "image_1" - } -} - -MOCK_POST_IMAGE_REQUEST = { - "id": "uuid_3", - "name": "image_3", - "imagePath": "test.com/image_3" -} - -MOCK_POST_IMAGE_REQUEST_EXISTING = { - "id": "uuid_1", - "name": "image_1", - "imagePath": "test.com/image_1" -} - -MOCK_POST_IMAGE_RESPONSE = { - "id": "uuid_3", - "name": "image_3" -} - - -class TestImage(unittest.TestCase): def setUp(self): self.client = Client() - def tearDown(self): - pass - - @mock.patch.object(VimDriverUtils, 'get_session') - @mock.patch.object(VimDriverUtils, 'get_vim_info') - def test_get_images(self, mock_get_vim_info, mock_get_session): - - mock_get_session.side_effect = [ - test_base.get_mock_session( - ["get"], {"get": {"content": MOCK_GET_IMAGES_RESPONSE}}), - ] - - mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO - response = self.client.get( - "/api/multicloud-newton/v0/windriver-hudson-dc_RegionOne/fcca3cc49d5e42caae15459e27103efc/images", - {}, HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID) - - context = response.json() - self.assertEquals(status.HTTP_200_OK, response.status_code) - self.assertIsNotNone(context['images']) - self.assertEqual(MOCK_GET_IMAGES_RESPONSE["images"], context["images"]) - - @mock.patch.object(VimDriverUtils, 'get_session') - @mock.patch.object(VimDriverUtils, 'get_vim_info') - def test_get_image(self, mock_get_vim_info, mock_get_session): - - mock_get_session.side_effect = [ - test_base.get_mock_session( - ["get"], {"get": {"content": MOCK_GET_IMAGE_RESPONSE}}), - ] - - mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO - - response = self.client.get( - "/api/multicloud-newton/v0/windriver-hudson-dc_RegionOne/fcca3cc49d5e42caae15459e27103efc" - "/images/uuid_1", - {}, HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID) - context = response.json() - - self.assertEquals(status.HTTP_200_OK, response.status_code) - self.assertEqual(MOCK_GET_IMAGE_RESPONSE["image"], context["image"]) - - @mock.patch.object(VimDriverUtils, 'get_session') - @mock.patch.object(VimDriverUtils, 'get_vim_info') - def test_get_image_not_found(self, mock_get_vim_info, mock_get_session): - - mock_get_session.side_effect = [ - test_base.get_mock_session( - ["get"], {"get": {"content": {}, - "status_code": 404}}) - ] - - mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO - - response = self.client.get( - "/api/multicloud-newton/v0/windriver-hudson-dc_RegionOne/fcca3cc49d5e42caae15459e27103efc" - "/images/uuid_3", - {}, HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID) - context = response.json() - - self.assertEquals(status.HTTP_404_NOT_FOUND, response.status_code) - self.assertIsNone(context.get("image")) + self.openstack_version = "newton" + self.region = "windriver-hudson-dc_RegionOne" + self.resource_name = "images" + + self.MOCK_GET_RESOURCES_RESPONSE = { + "images": [ + {"id": "uuid_1", "name": "image_1"}, + {"id": "uuid_2", "name": "image_2"} + ] + } + + self.MOCK_GET_RESOURCE_RESPONSE = { + "image": { + "id": "uuid_1", + "name": "image_1" + } + } + + self.MOCK_POST_RESOURCE_REQUEST = { + "id": "uuid_3", + "name": "image_3", + "imagePath": "test.com/image_3" + } + + self.MOCK_POST_RESOURCE_REQUEST_EXISTING = { + "id": "uuid_1", + "name": "image_1", + "imagePath": "test.com/image_1" + } + + self.MOCK_POST_RESOURCE_RESPONSE = { + "id": "uuid_3", + "name": "image_3" + } + + self.assert_keys = "images" + self.assert_key = "image" + + self.HTTP_not_found = status.HTTP_404_NOT_FOUND @mock.patch.object(imageThread, 'run') @mock.patch.object(urllib, 'request') @@ -132,9 +79,9 @@ class TestImage(unittest.TestCase): mock_get_session.side_effect = [ test_base.get_mock_session( - ["get"], {"get": {"content": MOCK_GET_IMAGES_RESPONSE}}), + ["get"], {"get": {"content": self.MOCK_GET_RESOURCES_RESPONSE}}), test_base.get_mock_session( - ["post"], {"post": {"content": MOCK_POST_IMAGE_RESPONSE, + ["post"], {"post": {"content": self.MOCK_POST_RESOURCE_RESPONSE, "status_code": 201}}), ] @@ -144,73 +91,13 @@ class TestImage(unittest.TestCase): response = self.client.post( "/api/multicloud-newton/v0/windriver-hudson-dc_RegionOne/fcca3cc49d5e42caae15459e27103efc/images", - MOCK_POST_IMAGE_REQUEST, HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID) + self.MOCK_POST_RESOURCE_REQUEST, HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID) context = response.json() self.assertEquals(status.HTTP_201_CREATED, response.status_code) self.assertIsNotNone(context['id']) self.assertEqual(1, context['returnCode']) - @mock.patch.object(VimDriverUtils, 'get_session') - @mock.patch.object(VimDriverUtils, 'get_vim_info') - def test_post_image_existing(self, mock_get_vim_info, mock_get_session): - - mock_get_session.side_effect = [ - test_base.get_mock_session( - ["get"], {"get": {"content": MOCK_GET_IMAGES_RESPONSE}}), - test_base.get_mock_session( - ["post"], {"post": {"content": MOCK_POST_IMAGE_RESPONSE, - "status_code": 201}}), - ] - - mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO - - response = self.client.post( - "/api/multicloud-newton/v0/windriver-hudson-dc_RegionOne/fcca3cc49d5e42caae15459e27103efc/images", - MOCK_POST_IMAGE_REQUEST_EXISTING, HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID) - - context = response.json() - self.assertEquals(status.HTTP_200_OK, response.status_code) - self.assertIsNotNone(context['returnCode']) - self.assertEqual(0, context['returnCode']) - - @mock.patch.object(VimDriverUtils, 'get_session') - @mock.patch.object(VimDriverUtils, 'get_vim_info') - def test_post_image_empty(self, mock_get_vim_info, mock_get_session): - - mock_get_session.side_effect = [ - test_base.get_mock_session( - ["get"], {"get": {"content": MOCK_GET_IMAGES_RESPONSE}}), - test_base.get_mock_session( - ["post"], {"post": {"content": MOCK_POST_IMAGE_RESPONSE, - "status_code": 201}}), - ] - - mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO - - response = self.client.post( - "/api/multicloud-newton/v0/windriver-hudson-dc_RegionOne/fcca3cc49d5e42caae15459e27103efc/images", - {}, HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID) - - context = response.json() - self.assertIn('error', context) - self.assertEquals(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code) - - @mock.patch.object(VimDriverUtils, 'get_session') - @mock.patch.object(VimDriverUtils, 'get_vim_info') - def test_delete_image(self, mock_get_vim_info, mock_get_session): - - mock_get_session.side_effect = [ - test_base.get_mock_session( - ["delete"], {"delete": {"content": {}, - "status_code": 204}}) - ] - - mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO - - response = self.client.delete( - "/api/multicloud-newton/v0/windriver-hudson-dc_RegionOne/fcca3cc49d5e42caae15459e27103efc" - "/images/uuid_1", HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID) - - self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code) - self.assertIsNone(response.data) + # Overridden method from test base to not make it run for current test case. + def test_post_resource(self): + pass |