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
This commit is contained in:
Nate Potter 2016-11-18 16:07:44 -08:00
parent d9e1231cd3
commit 8144b0c98b
3 changed files with 103 additions and 7 deletions

View File

@ -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"
}
]

View File

@ -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)

View File

@ -1,7 +0,0 @@
from unittest import TestCase
class TestUnits(TestCase):
def test_units(self):
assert 5 * 5 == 25