From 8144b0c98ba900198ebf3cf1a9ff11f17093232f Mon Sep 17 00:00:00 2001 From: Nate Potter Date: Fri, 18 Nov 2016 16:07:44 -0800 Subject: [PATCH] Add initial redfish unit testing This commit is the start of filling in unit testing for the redfish controller. It sets up tests for sending requests and filtering chassis, as well as setting up the fakes file for mocking responses and objects. Change-Id: I65da37a175c2d8cca7b53d1e2f45eb53243ef2ed Partial-bug: #1643095 --- valence/tests/unit/fakes.py | 46 ++++++++++++++++++++++++ valence/tests/unit/test_redfish.py | 57 ++++++++++++++++++++++++++++++ valence/tests/unit/test_units.py | 7 ---- 3 files changed, 103 insertions(+), 7 deletions(-) create mode 100644 valence/tests/unit/fakes.py create mode 100644 valence/tests/unit/test_redfish.py delete mode 100644 valence/tests/unit/test_units.py diff --git a/valence/tests/unit/fakes.py b/valence/tests/unit/fakes.py new file mode 100644 index 0000000..8c6ea4b --- /dev/null +++ b/valence/tests/unit/fakes.py @@ -0,0 +1,46 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import json + + +def mock_request_get(json_data, status_code): + + class MockResponse(object): + def __init__(self, json_data, status_code): + self.json_data = json_data + self.status_code = status_code + + def json(self): + return json.dumps(self.json_data) + + return MockResponse(json_data, status_code) + + +def fake_chassis_list(): + return [ + { + "Id": "1", + "ChassisType": "Pod", + "Name": "Pod 1" + }, + { + "Id": "2", + "ChassisType": "Rack", + "Name": "Rack 1" + }, + { + "Id": "3", + "ChassisType": "Rack", + "Name": "Rack 2" + } + ] diff --git a/valence/tests/unit/test_redfish.py b/valence/tests/unit/test_redfish.py new file mode 100644 index 0000000..f8d2cd5 --- /dev/null +++ b/valence/tests/unit/test_redfish.py @@ -0,0 +1,57 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import mock +from unittest import TestCase + +from valence import config as cfg +from valence.redfish import redfish +from valence.tests.unit import fakes + + +class TestRedfish(TestCase): + + def test_get_rfs_url_no_service_ext(self): + expected = cfg.podm_url + "/redfish/v1/Systems/1" + result = redfish.get_rfs_url("Systems/1") + self.assertEqual(expected, result) + + def test_get_rfs_url_with_service_ext(self): + expected = cfg.podm_url + "/redfish/v1/Systems/1" + result = redfish.get_rfs_url("/redfish/v1/Systems/1") + self.assertEqual(expected, result) + + @mock.patch('requests.request') + def test_send_request(self, mock_request): + mock_request.return_value = "fake_node" + result = redfish.send_request("Nodes/1") + self.assertEqual("fake_node", result) + + @mock.patch('valence.redfish.redfish.send_request') + def test_filter_chassis_rack(self, mock_request): + fake_chassis_list = fakes.fake_chassis_list() + first_request = fakes.mock_request_get(fake_chassis_list[0], "200") + second_request = fakes.mock_request_get(fake_chassis_list[1], "200") + third_request = fakes.mock_request_get(fake_chassis_list[2], "200") + mock_request.side_effect = [first_request, + second_request, + third_request] + chassis = ('{"Members":' + '[{"@odata.id": "1"},' + '{"@odata.id": "2"},' + '{"@odata.id": "3"}]}') + expected = {'Members': [ + {u'@odata.id': u'2'}, + {u'@odata.id': u'3'} + ], 'Members@odata.count': 2} + result = redfish.filter_chassis(chassis, "Rack") + self.assertEqual(expected, result) diff --git a/valence/tests/unit/test_units.py b/valence/tests/unit/test_units.py deleted file mode 100644 index 573fb68..0000000 --- a/valence/tests/unit/test_units.py +++ /dev/null @@ -1,7 +0,0 @@ -from unittest import TestCase - - -class TestUnits(TestCase): - - def test_units(self): - assert 5 * 5 == 25