summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVictor Morales <victor.morales@intel.com>2017-09-26 16:00:54 -0700
committerVictor Morales <victor.morales@intel.com>2017-09-26 17:32:53 -0700
commit244e346e9deefaa856a2a89fd1334a50d4f85b89 (patch)
treeedd3a593368bfd14752778ca3e967fcbc5975955
parent9d02be9a1a42f0035186e2de69e4fd8ecc7845af (diff)
Improve get_mock_session method
The previous implementation of get_mock_session method didn't accept other responses for different actions. This change makes it more flexible Change-Id: I68ceda07b1011cce4b81e22cf429393dfcbf73b1 Signed-off-by: Victor Morales <victor.morales@intel.com> Issue-Id: MULTICLOUD-83
-rw-r--r--newton/newton/requests/tests/test_base.py38
-rw-r--r--newton/newton/requests/tests/test_server.py90
-rw-r--r--newton/newton/requests/views/server.py1
3 files changed, 49 insertions, 80 deletions
diff --git a/newton/newton/requests/tests/test_base.py b/newton/newton/requests/tests/test_base.py
index fa72672d..3e6b59c3 100644
--- a/newton/newton/requests/tests/test_base.py
+++ b/newton/newton/requests/tests/test_base.py
@@ -18,28 +18,6 @@ import unittest
from django.test import Client
-MOCK_TOKEN_ID = "1a62b3971d774404a504c5d9a3e506e3"
-
-MOCK_VIM_INFO = {
- "createTime": "2017-04-01 02:22:27",
- "domain": "Default",
- "name": "TiS_R4",
- "password": "admin",
- "tenant": "admin",
- "type": "openstack",
- "url": "http://128.224.180.14:5000/v3",
- "userName": "admin",
- "vendor": "WindRiver",
- "version": "newton",
- "vimId": "windriver-hudson-dc_RegionOne",
- 'cloud_owner': 'windriver-hudson-dc',
- 'cloud_region_id': 'RegionOne',
- 'cloud_extra_info': '',
- 'cloud_epa_caps': '{"huge_page":"true","cpu_pinning":"true",\
- "cpu_thread_policy":"true","numa_aware":"true","sriov":"true",\
- "dpdk_vswitch":"true","rdt":"false","numa_locality_pci":"true"}',
- 'insecure': 'True',
-}
class MockResponse(object):
status_code = status.HTTP_200_OK
@@ -49,15 +27,17 @@ class MockResponse(object):
pass
-def get_mock_session(http_actions, response):
- mock_session_specs = http_actions
+def get_mock_session(http_actions, response_dict={}):
mock_session = mock.Mock(
- name='mock_session',spec=mock_session_specs)
- mock_response_obj = mock.Mock(spec=MockResponse)
- mock_response_obj.status_code = status.HTTP_200_OK
- mock_response_obj.content = response
- mock_response_obj.json.return_value = response
+ name='mock_session',spec=http_actions)
for action in http_actions:
+ mock_response_obj = mock.Mock(spec=MockResponse)
+ mock_response_obj.content = response_dict.get(
+ action).get("content")
+ mock_response_obj.json.return_value = response_dict.get(
+ action).get("content")
+ mock_response_obj.status_code = response_dict.get(
+ action).get("status_code", status.HTTP_200_OK)
if action == "get":
mock_session.get.return_value = mock_response_obj
if action == "post":
diff --git a/newton/newton/requests/tests/test_server.py b/newton/newton/requests/tests/test_server.py
index c0640b4b..c3e9e0f6 100644
--- a/newton/newton/requests/tests/test_server.py
+++ b/newton/newton/requests/tests/test_server.py
@@ -16,8 +16,8 @@ import json
import mock
from rest_framework import status
-import six
+from newton.requests.tests import mock_info
from newton.requests.tests import test_base
from newton.requests.views.util import VimDriverUtils
@@ -95,7 +95,7 @@ class TestNetwork(test_base.TestRequest):
response = self.client.get((
"/api/multicloud-newton/v0/windriver-hudson-dc_RegionOne/"
"" + tenant_id + "/servers"),
- {}, HTTP_X_AUTH_TOKEN=test_base.MOCK_TOKEN_ID)
+ {}, HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID)
self.assertEquals(status.HTTP_500_INTERNAL_SERVER_ERROR,
response.status_code)
@@ -105,29 +105,32 @@ class TestNetwork(test_base.TestRequest):
@mock.patch.object(VimDriverUtils, 'get_vim_info')
def test_get_list_servers(self, mock_get_vim_info,
mock_get_session):
- mock_get_vim_info.return_value = test_base.MOCK_VIM_INFO
+ mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO
mock_get_session.side_effect = [
test_base.get_mock_session(
- ["get"], MOCK_GET_SERVERS_RESPONSE),
+ ["get"],
+ {"get": { "content": MOCK_GET_SERVERS_RESPONSE }}),
test_base.get_mock_session(
- ["get"], MOCK_GET_PORTS_RESPONSE),
+ ["get"],
+ {"get": { "content":MOCK_GET_PORTS_RESPONSE}}),
test_base.get_mock_session(
- ["get"], None)
+ ["get"],
+ {"get": {"content": None}}),
]
tenant_id = "fcca3cc49d5e42caae15459e27103efc"
response = self.client.get((
"/api/multicloud-newton/v0/windriver-hudson-dc_RegionOne/"
"" + tenant_id + "/servers"),
- {}, HTTP_X_AUTH_TOKEN=test_base.MOCK_TOKEN_ID)
+ {}, HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID)
self.assertEquals(status.HTTP_200_OK, response.status_code)
content = response.json()
self.assertEquals(
- test_base.MOCK_VIM_INFO["name"], content["vimName"])
+ mock_info.MOCK_VIM_INFO["name"], content["vimName"])
self.assertEquals(tenant_id, content["tenantId"])
self.assertEquals(
- test_base.MOCK_VIM_INFO["vimId"], content["vimId"])
+ mock_info.MOCK_VIM_INFO["vimId"], content["vimId"])
self.assertEquals(len(MOCK_GET_SERVERS_RESPONSE["servers"]),
len(content["servers"]))
@@ -135,12 +138,12 @@ class TestNetwork(test_base.TestRequest):
@mock.patch.object(VimDriverUtils, 'get_vim_info')
def test_one_server_info(self, mock_get_vim_info,
mock_get_session):
- mock_get_vim_info.return_value = test_base.MOCK_VIM_INFO
+ mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO
mock_get_session.side_effect = [
test_base.get_mock_session(
- ["get"], MOCK_GET_SERVER_RESPONSE.copy()),
+ ["get"], {"get": {"content": MOCK_GET_SERVER_RESPONSE.copy()}}),
test_base.get_mock_session(
- ["get"], MOCK_GET_PORTS_RESPONSE.copy()),
+ ["get"], {"get": {"content": MOCK_GET_PORTS_RESPONSE.copy()}}),
]
tenant_id = "fcca3cc49d5e42caae15459e27103efc"
server_id = "f5dc173b-6804-445a-a6d8-c705dad5b5eb"
@@ -148,28 +151,29 @@ class TestNetwork(test_base.TestRequest):
response = self.client.get((
"/api/multicloud-newton/v0/windriver-hudson-dc_RegionOne/"
"" + tenant_id + "/servers/" + server_id),
- {}, HTTP_X_AUTH_TOKEN=test_base.MOCK_TOKEN_ID)
+ {}, HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID)
self.assertEquals(status.HTTP_200_OK, response.status_code)
content = response.json()
self.assertEquals(
- test_base.MOCK_VIM_INFO["name"], content["vimName"])
+ mock_info.MOCK_VIM_INFO["name"], content["vimName"])
self.assertEquals(tenant_id, content["tenantId"])
self.assertEquals(
- test_base.MOCK_VIM_INFO["vimId"], content["vimId"])
+ mock_info.MOCK_VIM_INFO["vimId"], content["vimId"])
@mock.patch.object(VimDriverUtils, 'get_session')
@mock.patch.object(VimDriverUtils, 'get_vim_info')
def test_create_existing_server(self, mock_get_vim_info,
mock_get_session):
- mock_get_vim_info.return_value = test_base.MOCK_VIM_INFO
+ mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO
mock_get_session.side_effect = [
test_base.get_mock_session(
- ["get"], MOCK_GET_SERVERS_RESPONSE),
+ ["get"],
+ {"get": {"content":MOCK_GET_SERVERS_RESPONSE}}),
test_base.get_mock_session(
- ["get"], None),
+ ["get"], {"get": {"content":None}}),
test_base.get_mock_session(
- ["get"], None),
+ ["get"], {"get": {"content": None}}),
]
tenant_id = "fcca3cc49d5e42caae15459e27103efc"
@@ -180,7 +184,7 @@ class TestNetwork(test_base.TestRequest):
"" + tenant_id + "/servers/" + server_id),
data=json.dumps(TEST_CREATE_SERVER),
content_type="application/json",
- HTTP_X_AUTH_TOKEN=test_base.MOCK_TOKEN_ID)
+ HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID)
context = response.json()
self.assertEquals(status.HTTP_200_OK,response.status_code)
@@ -196,33 +200,16 @@ class TestNetwork(test_base.TestRequest):
self.assertEquals(0, context["returnCode"])
@mock.patch.object(VimDriverUtils, 'get_session')
- def test_create_server_sucessfuly(self, mock_get_session):
+ def test_create_server_successfully(self, mock_get_session):
VimDriverUtils.get_vim_info = mock.Mock(
- return_value=test_base.MOCK_VIM_INFO)
+ return_value=mock_info.MOCK_VIM_INFO)
- def side_effect(items):
- def func():
- for item in items:
- yield item
- yield test_base.get_mock_session(
- ["post"], None)
-
- generator = func()
-
- def effect(*args, **kwargs):
- return six.next(generator)
-
- return effect
-
- effects = [
- test_base.get_mock_session(
- ["get"], {"servers":[]}),
+ mock_get_session.side_effect = [
test_base.get_mock_session(
- ["post"], MOCK_POST_SERVER_RESPONSE.copy()),
+ ["get"], {"get": {"content": {"servers":[]}}}),
test_base.get_mock_session(
- ["get"], MOCK_POST_SERVER_CREATED_THREAD_RESPONSE.copy()),
+ ["post"], {"post": {"content": MOCK_POST_SERVER_RESPONSE.copy()}}),
]
- mock_get_session.side_effect = side_effect(effects)
tenant_id = "fcca3cc49d5e42caae15459e27103efc"
server_id = "f5dc173b-6804-445a-a6d8-c705dad5b5eb"
@@ -231,12 +218,12 @@ class TestNetwork(test_base.TestRequest):
"" + tenant_id + "/servers/" + server_id),
data=json.dumps(TEST_CREATE_SERVER),
content_type="application/json",
- HTTP_X_AUTH_TOKEN=test_base.MOCK_TOKEN_ID)
+ HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID)
context = response.json()
self.assertEquals(status.HTTP_200_OK, response.status_code)
self.assertEquals(
- test_base.MOCK_VIM_INFO["vimId"], context["vimId"])
+ mock_info.MOCK_VIM_INFO["vimId"], context["vimId"])
self.assertEquals(tenant_id, context["tenantId"])
# self.assertEquals(len(TEST_CREATE_SERVER["volumeArray"]),
# len(context['volumeArray']))
@@ -245,7 +232,7 @@ class TestNetwork(test_base.TestRequest):
self.assertEquals(len(TEST_CREATE_SERVER["nicArray"]),
len(context["nicArray"]))
self.assertEquals(
- test_base.MOCK_VIM_INFO["name"], context["vimName"])
+ mock_info.MOCK_VIM_INFO["name"], context["vimName"])
self.assertIsNotNone(TEST_CREATE_SERVER["boot"])
self.assertEquals(TEST_CREATE_SERVER["boot"]["volumeId"],
context["boot"]["volumeId"])
@@ -260,16 +247,17 @@ class TestNetwork(test_base.TestRequest):
@mock.patch.object(VimDriverUtils, 'get_session')
@mock.patch.object(VimDriverUtils, 'get_vim_info')
- def test_delete_existing_serever(self, mock_get_vim_info,
+ def test_delete_existing_server(self, mock_get_vim_info,
mock_get_session):
- mock_get_vim_info.return_value = test_base.MOCK_VIM_INFO
+ mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO
mock_get_session.side_effect = [
test_base.get_mock_session(
- ["delete"], None),
+ ["delete"], {"delete": {"content": None}}),
test_base.get_mock_session(
- ["get"], MOCK_GET_SERVER_RESPONSE.copy()),
+ ["get"],
+ {"get": {"content": MOCK_GET_SERVER_RESPONSE.copy()}}),
test_base.get_mock_session(
- ["get"], None),
+ ["get"], {"get": {"content": None}}),
]
tenant_id = "fcca3cc49d5e42caae15459e27103efc"
@@ -280,6 +268,6 @@ class TestNetwork(test_base.TestRequest):
"" + tenant_id + "/servers/" + server_id),
data=json.dumps(TEST_CREATE_SERVER),
content_type="application/json",
- HTTP_X_AUTH_TOKEN=test_base.MOCK_TOKEN_ID)
+ HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID)
self.assertEquals(status.HTTP_200_OK, response.status_code) \ No newline at end of file
diff --git a/newton/newton/requests/views/server.py b/newton/newton/requests/views/server.py
index 97008768..d6384eb7 100644
--- a/newton/newton/requests/views/server.py
+++ b/newton/newton/requests/views/server.py
@@ -31,6 +31,7 @@ logger = logging.getLogger(__name__)
running_threads = {}
running_thread_lock = threading.Lock()
+
#assume volume is attached on server creation
class ServerVolumeAttachThread (threading.Thread):
service = {'service_type': 'compute',