summaryrefslogtreecommitdiffstats
path: root/app/test/fetch/api_fetch/test_api_fetch_project_hosts.py
diff options
context:
space:
mode:
Diffstat (limited to 'app/test/fetch/api_fetch/test_api_fetch_project_hosts.py')
-rw-r--r--app/test/fetch/api_fetch/test_api_fetch_project_hosts.py43
1 files changed, 35 insertions, 8 deletions
diff --git a/app/test/fetch/api_fetch/test_api_fetch_project_hosts.py b/app/test/fetch/api_fetch/test_api_fetch_project_hosts.py
index da3df17..784079e 100644
--- a/app/test/fetch/api_fetch/test_api_fetch_project_hosts.py
+++ b/app/test/fetch/api_fetch/test_api_fetch_project_hosts.py
@@ -7,6 +7,7 @@
# which accompanies this distribution, and is available at #
# http://www.apache.org/licenses/LICENSE-2.0 #
###############################################################################
+import copy
from unittest.mock import MagicMock
from discover.fetchers.api.api_fetch_project_hosts import ApiFetchProjectHosts
from test.fetch.test_fetch import TestFetch
@@ -36,23 +37,28 @@ class TestApiFetchProjectHosts(TestFetch):
"type in host_type")
def test_add_host_type_with_existent_host_type(self):
+ fetch_host_os_details = self.fetcher.fetch_host_os_details
+ self.fetcher.fetch_host_os_details = MagicMock()
# add nonexistent host type to host type
HOST_DOC["host_type"] = [NONEXISTENT_TYPE]
# try to add existing host type
self.fetcher.add_host_type(HOST_DOC, NONEXISTENT_TYPE, HOST_ZONE)
- self.assertEqual(len(HOST_DOC['host_type']), 1, "Add duplicate host type")
+ self.assertEqual(len(HOST_DOC['host_type']), 1,
+ "Add duplicate host type")
+ self.fetcher.fetch_host_os_details = fetch_host_os_details
def test_add_compute_host_type(self):
- HOST_DOC['host_type'] = []
+ doc = copy.deepcopy(HOST_DOC)
+ doc['host_type'] = []
# clear zone
- HOST_DOC['zone'] = None
+ doc['zone'] = None
# add compute host type
- self.fetcher.add_host_type(HOST_DOC, COMPUTE_TYPE, HOST_ZONE)
+ self.fetcher.add_host_type(doc, COMPUTE_TYPE, HOST_ZONE)
# for compute host type, zone information will be added
- self.assertEqual(HOST_DOC['zone'], HOST_ZONE, "Can't update zone " +
- "name for compute node")
- self.assertEqual(HOST_DOC['parent_id'], HOST_ZONE, "Can't update parent_id " +
- "for compute node")
+ self.assertEqual(doc['zone'], HOST_ZONE,
+ "Can't update zone name for compute node")
+ self.assertEqual(doc['parent_id'], HOST_ZONE,
+ "Can't update parent_id for compute node")
def test_fetch_compute_node_ip_address(self):
# mock ip address information fetched from DB
@@ -78,16 +84,24 @@ class TestApiFetchProjectHosts(TestFetch):
def test_get_host_details(self):
# test node have nova-conductor attribute, controller type will be added
+ fetch_host_os_details = self.fetcher.fetch_host_os_details
+ self.fetcher.fetch_host_os_details = MagicMock()
result = self.fetcher.get_host_details(AVAILABILITY_ZONE, HOST_NAME)
self.assertIn("Controller", result['host_type'], "Can't put controller type " +
"in the compute node host_type")
+ self.fetcher.fetch_host_os_details = fetch_host_os_details
def test_get_hosts_from_az(self):
+ fetch_host_os_details = self.fetcher.fetch_host_os_details
+ self.fetcher.fetch_host_os_details = MagicMock()
result = self.fetcher.get_hosts_from_az(AVAILABILITY_ZONE)
self.assertNotEqual(result, [], "Can't get hosts information from "
"availability zone")
+ self.fetcher.fetch_host_os_details = fetch_host_os_details
def test_get_for_region(self):
+ fetch_host_os_details = self.fetcher.fetch_host_os_details
+ self.fetcher.fetch_host_os_details = MagicMock()
# mock region url for nova node
self.fetcher.get_region_url = MagicMock(return_value=REGION_URL)
# mock the response from OpenStack Api
@@ -96,6 +110,7 @@ class TestApiFetchProjectHosts(TestFetch):
result = self.fetcher.get_for_region(self.region, TOKEN)
self.assertNotEqual(result, [], "Can't get hosts information for region")
+ self.fetcher.fetch_host_os_details = fetch_host_os_details
def test_get_for_region_without_token(self):
self.fetcher.get_region_url = MagicMock(return_value=REGION_URL)
@@ -112,6 +127,8 @@ class TestApiFetchProjectHosts(TestFetch):
self.assertEqual(result, [], "Can't get [] when the response is wrong")
def test_get_for_region_with_error_hypervisors_response(self):
+ fetch_host_os_details = self.fetcher.fetch_host_os_details
+ self.fetcher.fetch_host_os_details = MagicMock()
self.fetcher.get_region_url = MagicMock(return_value=REGION_URL)
# mock error hypervisors response from OpenStack Api
side_effect = [AVAILABILITY_ZONE_RESPONSE, HYPERVISORS_ERROR_RESPONSE]
@@ -120,6 +137,7 @@ class TestApiFetchProjectHosts(TestFetch):
result = self.fetcher.get_for_region(self.region, TOKEN)
self.assertNotEqual(result, [], "Can't get hosts information when " +
"the hypervisors response is wrong")
+ self.fetcher.fetch_host_os_details = fetch_host_os_details
def test_get(self):
original_method = self.fetcher.get_for_region
@@ -140,6 +158,15 @@ class TestApiFetchProjectHosts(TestFetch):
result = self.fetcher.get(PROJECT_NAME)
self.assertEqual(result, [], "Can't get [] when the token is invalid")
+ def test_fetch_host_os_details(self):
+ original_method = self.fetcher.run
+ self.fetcher.run = MagicMock(return_value=OS_DETAILS_INPUT)
+ doc = {'host': 'host1'}
+ self.fetcher.fetch_host_os_details(doc)
+ self.assertEqual(doc.get('OS', {}), OS_DETAILS)
+ self.fetcher.run = original_method
+
+
def tearDown(self):
super().tearDown()
ApiFetchProjectHosts.v2_auth_pwd = self._v2_auth_pwd