summaryrefslogtreecommitdiffstats
path: root/newton
diff options
context:
space:
mode:
Diffstat (limited to 'newton')
-rw-r--r--newton/newton/requests/tests/test_image.py137
-rw-r--r--newton/newton/requests/tests/test_network.py127
2 files changed, 81 insertions, 183 deletions
diff --git a/newton/newton/requests/tests/test_image.py b/newton/newton/requests/tests/test_image.py
index 4b6bfb13..467e4710 100644
--- a/newton/newton/requests/tests/test_image.py
+++ b/newton/newton/requests/tests/test_image.py
@@ -20,6 +20,7 @@ from django.test import Client
from rest_framework import status
from newton.requests.tests import mock_info
+from newton.requests.tests import test_base
from newton.requests.views.image import imageThread
from newton.requests.views.util import VimDriverUtils
@@ -56,14 +57,6 @@ MOCK_POST_IMAGE_RESPONSE = {
}
-class MockResponse(object):
- status_code = 200
- content = ''
-
- def json(self):
- pass
-
-
class TestImage(unittest.TestCase):
def setUp(self):
self.client = Client()
@@ -74,17 +67,12 @@ class TestImage(unittest.TestCase):
@mock.patch.object(VimDriverUtils, 'get_vim_info')
def test_get_images(self, mock_get_vim_info, mock_get_session):
- mock_session_specs = ["get"]
- mock_session = mock.Mock(name='mock_session', spec=mock_session_specs)
-
- mock_get_images_response_obj = mock.Mock(spec=MockResponse)
- mock_get_images_response_obj.status_code = 200
- mock_get_images_response_obj.content = MOCK_GET_IMAGES_RESPONSE
- mock_get_images_response_obj.json.return_value = MOCK_GET_IMAGES_RESPONSE
- mock_session.get.return_value = mock_get_images_response_obj
+ 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
- mock_get_session.return_value = mock_session
response = self.client.get(
"/api/multicloud-newton/v0/windriver-hudson-dc_RegionOne/fcca3cc49d5e42caae15459e27103efc/images",
@@ -99,17 +87,12 @@ class TestImage(unittest.TestCase):
@mock.patch.object(VimDriverUtils, 'get_vim_info')
def test_get_image(self, mock_get_vim_info, mock_get_session):
- mock_session_specs = ["get"]
- mock_session = mock.Mock(name='mock_session', spec=mock_session_specs)
-
- mock_get_image_response_obj = mock.Mock(spec=MockResponse)
- mock_get_image_response_obj.status_code = 200
- mock_get_image_response_obj.content = MOCK_GET_IMAGE_RESPONSE
- mock_get_image_response_obj.json.return_value = MOCK_GET_IMAGE_RESPONSE
- mock_session.get.return_value = mock_get_image_response_obj
+ 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
- mock_get_session.return_value = mock_session
response = self.client.get(
"/api/multicloud-newton/v0/windriver-hudson-dc_RegionOne/fcca3cc49d5e42caae15459e27103efc"
@@ -124,20 +107,13 @@ class TestImage(unittest.TestCase):
@mock.patch.object(VimDriverUtils, 'get_vim_info')
def test_get_image_not_found(self, mock_get_vim_info, mock_get_session):
- mock_session_specs = ["get"]
- mock_session = mock.Mock(name='mock_session', spec=mock_session_specs)
-
- mock_get_image_response_obj = mock.Mock(spec=MockResponse)
- mock_get_image_response_obj.status_code = 404
- mock_get_image_response_obj.content = {}
- mock_get_image_response_obj.json.return_value = {}
- mock_session.get.return_value = mock_get_image_response_obj
+ 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
- mock_get_session.return_value = mock_session
-
- mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO
- mock_get_session.return_value = mock_session
response = self.client.get(
"/api/multicloud-newton/v0/windriver-hudson-dc_RegionOne/fcca3cc49d5e42caae15459e27103efc"
@@ -153,24 +129,17 @@ class TestImage(unittest.TestCase):
@mock.patch.object(VimDriverUtils, 'get_session')
@mock.patch.object(VimDriverUtils, 'get_vim_info')
def test_post_image(self, mock_get_vim_info, mock_get_session, mock_request, mock_run):
- mock_session_specs = ["get", "post"]
- mock_session = mock.Mock(name='mock_session', spec=mock_session_specs)
-
- mock_get_images_response_obj = mock.Mock(spec=MockResponse)
- mock_get_images_response_obj.status_code = 200
- mock_get_images_response_obj.content = MOCK_GET_IMAGES_RESPONSE
- mock_get_images_response_obj.json.return_value = MOCK_GET_IMAGES_RESPONSE
- mock_post_image_response_obj = mock.Mock(spec=MockResponse)
- mock_post_image_response_obj.status_code = 201
- mock_post_image_response_obj.content = MOCK_POST_IMAGE_RESPONSE
- mock_post_image_response_obj.json.return_value = MOCK_POST_IMAGE_RESPONSE
-
- mock_session.get.return_value = mock_get_images_response_obj
- mock_session.post.return_value = mock_post_image_response_obj
+ 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
- mock_get_session.return_value = mock_session
+
mock_request.urlopen.return_value = "image"
response = self.client.post(
@@ -185,27 +154,16 @@ class TestImage(unittest.TestCase):
@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_session_specs = ["get", "post"]
- mock_session = mock.Mock(name='mock_session', spec=mock_session_specs)
-
- mock_get_images_response_obj = mock.Mock(spec=MockResponse)
- mock_get_images_response_obj.status_code = 200
- mock_get_images_response_obj.content = MOCK_GET_IMAGES_RESPONSE
- mock_get_images_response_obj.json.return_value = MOCK_GET_IMAGES_RESPONSE
-
- mock_post_image_response_obj = mock.Mock(spec=MockResponse)
- mock_post_image_response_obj.status_code = 201
- mock_post_image_response_obj.content = MOCK_POST_IMAGE_RESPONSE
- mock_post_image_response_obj.json.return_value = MOCK_POST_IMAGE_RESPONSE
-
- mock_session.get.return_value = mock_get_images_response_obj
- mock_session.post.return_value = mock_post_image_response_obj
- mock_session.get.return_value = mock_get_images_response_obj
- mock_session.post.return_value = mock_post_image_response_obj
+ 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
- mock_get_session.return_value = mock_session
response = self.client.post(
"/api/multicloud-newton/v0/windriver-hudson-dc_RegionOne/fcca3cc49d5e42caae15459e27103efc/images",
@@ -219,27 +177,16 @@ class TestImage(unittest.TestCase):
@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_session_specs = ["get", "post"]
- mock_session = mock.Mock(name='mock_session', spec=mock_session_specs)
- mock_get_images_response_obj = mock.Mock(spec=MockResponse)
- mock_get_images_response_obj.status_code = 200
- mock_get_images_response_obj.content = MOCK_GET_IMAGES_RESPONSE
- mock_get_images_response_obj.json.return_value = MOCK_GET_IMAGES_RESPONSE
-
- mock_post_image_response_obj = mock.Mock(spec=MockResponse)
- mock_post_image_response_obj.status_code = 201
- mock_post_image_response_obj.content = MOCK_POST_IMAGE_RESPONSE
- mock_post_image_response_obj.json.return_value = MOCK_POST_IMAGE_RESPONSE
-
- mock_session.get.return_value = mock_get_images_response_obj
- mock_session.post.return_value = mock_post_image_response_obj
-
- mock_session.get.return_value = mock_get_images_response_obj
- mock_session.post.return_value = mock_post_image_response_obj
+ 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
- mock_get_session.return_value = mock_session
response = self.client.post(
"/api/multicloud-newton/v0/windriver-hudson-dc_RegionOne/fcca3cc49d5e42caae15459e27103efc/images",
@@ -252,16 +199,14 @@ class TestImage(unittest.TestCase):
@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_session_specs = ["delete"]
- mock_session = mock.Mock(name='mock_session', spec=mock_session_specs)
-
- mock_delete_image_response_obj = mock.Mock(spec=MockResponse)
- mock_delete_image_response_obj.status_code = 204
- mock_session.delete.return_value = mock_delete_image_response_obj
+ 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
- mock_get_session.return_value = mock_session
response = self.client.delete(
"/api/multicloud-newton/v0/windriver-hudson-dc_RegionOne/fcca3cc49d5e42caae15459e27103efc"
diff --git a/newton/newton/requests/tests/test_network.py b/newton/newton/requests/tests/test_network.py
index 3eb8b0e6..448c098b 100644
--- a/newton/newton/requests/tests/test_network.py
+++ b/newton/newton/requests/tests/test_network.py
@@ -20,6 +20,7 @@ from rest_framework import status
from newton.requests.views.util import VimDriverUtils
from newton.requests.tests import mock_info
+from newton.requests.tests import test_base
MOCK_GET_NETWORKS_RESPONSE = {
@@ -51,14 +52,6 @@ MOCK_POST_NETWORK_RESPONSE = {
}
-class MockResponse(object):
- status_code = 200
- content = ''
-
- def json(self):
- pass
-
-
class TestNetwork(unittest.TestCase):
def setUp(self):
self.client = Client()
@@ -70,16 +63,12 @@ class TestNetwork(unittest.TestCase):
@mock.patch.object(VimDriverUtils, 'get_vim_info')
def test_get_networks(self, mock_get_vim_info, mock_get_session):
- mock_session_specs = ["get"]
- mock_session = mock.Mock(name='mock_session', spec=mock_session_specs)
- mock_get_networks_response_obj = mock.Mock(spec=MockResponse)
- mock_get_networks_response_obj.status_code = 200
- mock_get_networks_response_obj.content = MOCK_GET_NETWORKS_RESPONSE
- mock_get_networks_response_obj.json.return_value = MOCK_GET_NETWORKS_RESPONSE
- mock_session.get.return_value = mock_get_networks_response_obj
+ mock_get_session.side_effect = [
+ test_base.get_mock_session(
+ ["get"], {"get": {"content": MOCK_GET_NETWORKS_RESPONSE}}),
+ ]
mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO
- mock_get_session.return_value = mock_session
response = self.client.get(
"/api/multicloud-newton/v0/windriver-hudson-dc_RegionOne/fcca3cc49d5e42caae15459e27103efc/networks",
@@ -94,16 +83,12 @@ class TestNetwork(unittest.TestCase):
@mock.patch.object(VimDriverUtils, 'get_vim_info')
def test_get_network(self, mock_get_vim_info, mock_get_session):
- mock_session_specs = ["get"]
- mock_session = mock.Mock(name='mock_session', spec=mock_session_specs)
- mock_get_network_response_obj = mock.Mock(spec=MockResponse)
- mock_get_network_response_obj.status_code = 200
- mock_get_network_response_obj.content = MOCK_GET_NETWORK_RESPONSE
- mock_get_network_response_obj.json.return_value = MOCK_GET_NETWORK_RESPONSE
- mock_session.get.return_value = mock_get_network_response_obj
+ mock_get_session.side_effect = [
+ test_base.get_mock_session(
+ ["get"], {"get": {"content": MOCK_GET_NETWORK_RESPONSE}}),
+ ]
mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO
- mock_get_session.return_value = mock_session
response = self.client.get(
"/api/multicloud-newton/v0/windriver-hudson-dc_RegionOne/fcca3cc49d5e42caae15459e27103efc"
@@ -118,16 +103,13 @@ class TestNetwork(unittest.TestCase):
@mock.patch.object(VimDriverUtils, 'get_vim_info')
def test_get_network_not_found(self, mock_get_vim_info, mock_get_session):
- mock_session_specs = ["get"]
- mock_session = mock.Mock(name='mock_session', spec=mock_session_specs)
- mock_get_network_response_obj = mock.Mock(spec=MockResponse)
- mock_get_network_response_obj.status_code = 404
- mock_get_network_response_obj.context = {}
- mock_get_network_response_obj.json.return_value = {}
- mock_session.get.return_value = mock_get_network_response_obj
+ 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
- mock_get_session.return_value = mock_session
response = self.client.get(
"/api/multicloud-newton/v0/windriver-hudson-dc_RegionOne/fcca3cc49d5e42caae15459e27103efc"
@@ -142,24 +124,15 @@ class TestNetwork(unittest.TestCase):
@mock.patch.object(VimDriverUtils, 'get_vim_info')
def test_post(self, mock_get_vim_info, mock_get_session):
- mock_session_specs = ["get", "post"]
- mock_session = mock.Mock(name='mock_session', spec=mock_session_specs)
-
- mock_get_networks_response_obj = mock.Mock(spec=MockResponse)
- mock_get_networks_response_obj.status_code = 200
- mock_get_networks_response_obj.content = MOCK_GET_NETWORKS_RESPONSE
- mock_get_networks_response_obj.json.return_value = MOCK_GET_NETWORKS_RESPONSE
-
- mock_post_network_response_obj = mock.Mock(spec=MockResponse)
- mock_post_network_response_obj.status_code = 202
- mock_post_network_response_obj.content = MOCK_POST_NETWORK_RESPONSE
- mock_post_network_response_obj.json.return_value = MOCK_POST_NETWORK_RESPONSE
-
- mock_session.get.return_value = mock_get_networks_response_obj
- mock_session.post.return_value = mock_post_network_response_obj
+ mock_get_session.side_effect = [
+ test_base.get_mock_session(
+ ["get"], {"get": {"content": MOCK_GET_NETWORKS_RESPONSE}}),
+ test_base.get_mock_session(
+ ["post"], {"post": {"content": MOCK_POST_NETWORK_RESPONSE,
+ "status_code": 202}}),
+ ]
mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO
- mock_get_session.return_value = mock_session
response = self.client.post(
"/api/multicloud-newton/v0/windriver-hudson-dc_RegionOne/fcca3cc49d5e42caae15459e27103efc/networks",
@@ -173,24 +146,16 @@ class TestNetwork(unittest.TestCase):
@mock.patch.object(VimDriverUtils, 'get_session')
@mock.patch.object(VimDriverUtils, 'get_vim_info')
def test_post_existing(self, mock_get_vim_info, mock_get_session):
- mock_session_specs = ["get", "post"]
- mock_session = mock.Mock(name='mock_session', spec=mock_session_specs)
-
- mock_get_networks_response_obj = mock.Mock(spec=MockResponse)
- mock_get_networks_response_obj.status_code = 200
- mock_get_networks_response_obj.content = MOCK_GET_NETWORKS_RESPONSE
- mock_get_networks_response_obj.json.return_value = MOCK_GET_NETWORKS_RESPONSE
- mock_post_network_response_obj = mock.Mock(spec=MockResponse)
- mock_post_network_response_obj.status_code = 202
- mock_post_network_response_obj.content = MOCK_POST_NETWORK_RESPONSE
- mock_post_network_response_obj.json.return_value = MOCK_POST_NETWORK_RESPONSE
-
- mock_session.get.return_value = mock_get_networks_response_obj
- mock_session.post.return_value = mock_post_network_response_obj
+ mock_get_session.side_effect = [
+ test_base.get_mock_session(
+ ["get"], {"get": {"content": MOCK_GET_NETWORKS_RESPONSE}}),
+ test_base.get_mock_session(
+ ["post"], {"post": {"content": MOCK_POST_NETWORK_RESPONSE,
+ "status_code": 202}}),
+ ]
mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO
- mock_get_session.return_value = mock_session
response = self.client.post(
"/api/multicloud-newton/v0/windriver-hudson-dc_RegionOne/fcca3cc49d5e42caae15459e27103efc/networks",
@@ -204,24 +169,16 @@ class TestNetwork(unittest.TestCase):
@mock.patch.object(VimDriverUtils, 'get_session')
@mock.patch.object(VimDriverUtils, 'get_vim_info')
def test_post_empty_body(self, mock_get_vim_info, mock_get_session):
- mock_session_specs = ["get", "post"]
- mock_session = mock.Mock(name='mock_session', spec=mock_session_specs)
-
- mock_get_networks_response_obj = mock.Mock(spec=MockResponse)
- mock_get_networks_response_obj.status_code = 200
- mock_get_networks_response_obj.content = MOCK_GET_NETWORKS_RESPONSE
- mock_get_networks_response_obj.json.return_value = MOCK_GET_NETWORKS_RESPONSE
-
- mock_post_network_response_obj = mock.Mock(spec=MockResponse)
- mock_post_network_response_obj.status_code = 202
- mock_post_network_response_obj.content = MOCK_POST_NETWORK_RESPONSE
- mock_post_network_response_obj.json.return_value = MOCK_POST_NETWORK_RESPONSE
- mock_session.get.return_value = mock_get_networks_response_obj
- mock_session.post.return_value = mock_post_network_response_obj
+ mock_get_session.side_effect = [
+ test_base.get_mock_session(
+ ["get"], {"get": {"content": MOCK_GET_NETWORKS_RESPONSE}}),
+ test_base.get_mock_session(
+ ["post"], {"post": {"content": MOCK_POST_NETWORK_RESPONSE,
+ "status_code": 202}}),
+ ]
mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO
- mock_get_session.return_value = mock_session
response = self.client.post(
"/api/multicloud-newton/v0/windriver-hudson-dc_RegionOne/fcca3cc49d5e42caae15459e27103efc/networks",
@@ -234,17 +191,13 @@ class TestNetwork(unittest.TestCase):
@mock.patch.object(VimDriverUtils, 'get_session')
@mock.patch.object(VimDriverUtils, 'get_vim_info')
def test_delete(self, mock_get_vim_info, mock_get_session):
-
- mock_session_specs = ["delete"]
- mock_session = mock.Mock(name='mock_session', spec=mock_session_specs)
-
- mock_delete_network_response_obj = mock.Mock(spec=MockResponse)
- mock_delete_network_response_obj.status_code = 204
-
- mock_session.delete.return_value = mock_delete_network_response_obj
+ 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
- mock_get_session.return_value = mock_session
response = self.client.delete(
"/api/multicloud-newton/v0/windriver-hudson-dc_RegionOne/fcca3cc49d5e42caae15459e27103efc"