Cleanup unittest file loading

Followup patch to update how test files are loaded accross all project:
- don't specify 'r' when opening a file as it is default mode
- use json.load(file) instead of json.loads(string)
- in some cases use file path inline instead of defining new var
- use loops to load several related files to avoid repetitive code

Change-Id: Ib4a66a4d97e7025849e3f84fc489fa1fa98af327
This commit is contained in:
Aija Jaunteva 2018-07-10 16:32:25 +03:00
parent e5400176cd
commit 33c4635ff3
12 changed files with 104 additions and 107 deletions

View File

@ -25,8 +25,8 @@ class ManagerTestCase(base.TestCase):
def setUp(self):
super(ManagerTestCase, self).setUp()
self.conn = mock.Mock()
with open('sushy/tests/unit/json_samples/manager.json', 'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
with open('sushy/tests/unit/json_samples/manager.json') as f:
self.conn.get.return_value.json.return_value = json.load(f)
self.manager = manager.Manager(self.conn, '/redfish/v1/Managers/BMC',
redfish_version='1.0.2')
@ -213,8 +213,8 @@ class ManagerCollectionTestCase(base.TestCase):
super(ManagerCollectionTestCase, self).setUp()
self.conn = mock.Mock()
with open('sushy/tests/unit/json_samples/'
'manager_collection.json', 'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
'manager_collection.json') as f:
self.conn.get.return_value.json.return_value = json.load(f)
self.managers = manager.ManagerCollection(
self.conn, '/redfish/v1/Managers', redfish_version='1.0.2')

View File

@ -27,8 +27,8 @@ class SessionTestCase(base.TestCase):
super(SessionTestCase, self).setUp()
self.conn = mock.Mock()
self.auth = mock.Mock()
with open('sushy/tests/unit/json_samples/session.json', 'r') as f:
sample_json = json.loads(f.read())
with open('sushy/tests/unit/json_samples/session.json') as f:
sample_json = json.load(f)
self.conn.get.return_value.json.return_value = sample_json
self.auth._session_key = 'fake_x_auth_token'
self.auth._session_uri = sample_json['@odata.id']
@ -66,9 +66,9 @@ class SessionCollectionTestCase(base.TestCase):
def setUp(self):
super(SessionCollectionTestCase, self).setUp()
self.conn = mock.Mock()
js_f = 'sushy/tests/unit/json_samples/session_collection.json'
with open(js_f, 'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
with open('sushy/tests/unit/json_samples/'
'session_collection.json') as f:
self.conn.get.return_value.json.return_value = json.load(f)
self.sess_col = session.SessionCollection(
self.conn, '/redfish/v1/SessionService/Sessions',

View File

@ -28,9 +28,8 @@ class SessionServiceTestCase(base.TestCase):
def setUp(self):
super(SessionServiceTestCase, self).setUp()
self.conn = mock.Mock()
js_f = 'sushy/tests/unit/json_samples/session_service.json'
with open(js_f, 'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
with open('sushy/tests/unit/json_samples/session_service.json') as f:
self.conn.get.return_value.json.return_value = json.load(f)
self.sess_serv_inst = sessionservice.SessionService(
self.conn, '/redfish/v1/SessionService',
@ -78,8 +77,8 @@ class SessionServiceTestCase(base.TestCase):
def test_create_session(self):
with open('sushy/tests/unit/json_samples/'
'session_creation_headers.json', 'r') as f:
self.conn.post.return_value.headers = json.loads(f.read())
'session_creation_headers.json') as f:
self.conn.post.return_value.headers = json.load(f)
session_key, session_uri = (
self.sess_serv_inst.create_session('foo', 'secret'))
@ -91,8 +90,8 @@ class SessionServiceTestCase(base.TestCase):
def test_create_session_unknown_path(self):
del self.sess_serv_inst.json['Sessions']
with open('sushy/tests/unit/json_samples/'
'session_creation_headers.json', 'r') as f:
self.conn.post.return_value.headers = json.loads(f.read())
'session_creation_headers.json') as f:
self.conn.post.return_value.headers = json.load(f)
session_key, session_uri = (
self.sess_serv_inst.create_session('foo', 'secret'))
@ -109,8 +108,8 @@ class SessionServiceTestCase(base.TestCase):
def test_create_session_missing_x_auth_token(self):
with open('sushy/tests/unit/json_samples/'
'session_creation_headers.json', 'r') as f:
self.conn.post.return_value.headers = json.loads(f.read())
'session_creation_headers.json') as f:
self.conn.post.return_value.headers = json.load(f)
self.conn.post.return_value.headers.pop('X-Auth-Token')
self.assertRaisesRegex(
@ -120,8 +119,8 @@ class SessionServiceTestCase(base.TestCase):
@mock.patch.object(sessionservice, 'LOG', autospec=True)
def test_create_session_missing_location(self, mock_LOG):
with open('sushy/tests/unit/json_samples/'
'session_creation_headers.json', 'r') as f:
self.conn.post.return_value.headers = json.loads(f.read())
'session_creation_headers.json') as f:
self.conn.post.return_value.headers = json.load(f)
self.conn.post.return_value.headers.pop('Location')
self.sess_serv_inst.create_session('foo', 'bar')
@ -130,8 +129,8 @@ class SessionServiceTestCase(base.TestCase):
def _setUp_sessions(self):
self.conn.get.return_value.json.reset_mock()
successive_return_values = []
with open('sushy/tests/unit/json_samples/session.json', 'r') as f:
successive_return_values.append(json.loads(f.read()))
with open('sushy/tests/unit/json_samples/session.json') as f:
successive_return_values.append(json.load(f))
self.conn.get.return_value.json.side_effect = successive_return_values
def test_sessions(self):
@ -162,8 +161,8 @@ class SessionServiceTestCase(base.TestCase):
self.conn.get.return_value.json.side_effect = None
# On refreshing the sess_serv_inst instance...
with open('sushy/tests/unit/json_samples/session.json', 'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
with open('sushy/tests/unit/json_samples/session.json') as f:
self.conn.get.return_value.json.return_value = json.load(f)
self.sess_serv_inst.refresh(force=True)
# | WHEN & THEN |

View File

@ -23,9 +23,8 @@ class VolumeTestCase(base.TestCase):
def setUp(self):
super(VolumeTestCase, self).setUp()
self.conn = mock.Mock()
volume_file = 'sushy/tests/unit/json_samples/volume.json'
with open(volume_file, 'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
with open('sushy/tests/unit/json_samples/volume.json') as f:
self.conn.get.return_value.json.return_value = json.load(f)
self.stor_volume = volume.Volume(
self.conn, '/redfish/v1/Systems/437XR1138R2/Storage/1/Volumes/1',
@ -45,8 +44,8 @@ class VolumeCollectionTestCase(base.TestCase):
super(VolumeCollectionTestCase, self).setUp()
self.conn = mock.Mock()
with open('sushy/tests/unit/json_samples/'
'volume_collection.json', 'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
'volume_collection.json') as f:
self.conn.get.return_value.json.return_value = json.load(f)
self.stor_vol_col = volume.VolumeCollection(
self.conn, '/redfish/v1/Systems/437XR1138R2/Storage/1/Volumes',
redfish_version='1.0.2')
@ -91,12 +90,12 @@ class VolumeCollectionTestCase(base.TestCase):
self.conn.get.return_value.json.reset_mock()
successive_return_values = []
with open('sushy/tests/unit/json_samples/volume.json', 'r') as f:
successive_return_values.append(json.loads(f.read()))
with open('sushy/tests/unit/json_samples/volume2.json', 'r') as f:
successive_return_values.append(json.loads(f.read()))
with open('sushy/tests/unit/json_samples/volume3.json', 'r') as f:
successive_return_values.append(json.loads(f.read()))
file_names = ['sushy/tests/unit/json_samples/volume.json',
'sushy/tests/unit/json_samples/volume2.json',
'sushy/tests/unit/json_samples/volume3.json']
for file_name in file_names:
with open(file_name) as f:
successive_return_values.append(json.load(f))
self.conn.get.return_value.json.side_effect = successive_return_values
self.assertEqual(1073741824000, self.stor_vol_col.max_size_bytes)
@ -112,12 +111,12 @@ class VolumeCollectionTestCase(base.TestCase):
self.conn.get.return_value.json.reset_mock()
successive_return_values = []
with open('sushy/tests/unit/json_samples/volume.json', 'r') as f:
successive_return_values.append(json.loads(f.read()))
with open('sushy/tests/unit/json_samples/volume2.json', 'r') as f:
successive_return_values.append(json.loads(f.read()))
with open('sushy/tests/unit/json_samples/volume3.json', 'r') as f:
successive_return_values.append(json.loads(f.read()))
file_names = ['sushy/tests/unit/json_samples/volume.json',
'sushy/tests/unit/json_samples/volume2.json',
'sushy/tests/unit/json_samples/volume3.json']
for file_name in file_names:
with open(file_name) as f:
successive_return_values.append(json.load(f))
self.conn.get.return_value.json.side_effect = successive_return_values
self.assertEqual(1073741824000, self.stor_vol_col.max_size_bytes)

View File

@ -26,11 +26,10 @@ class BiosTestCase(base.TestCase):
def setUp(self):
super(BiosTestCase, self).setUp()
self.conn = mock.Mock()
with open('sushy/tests/unit/json_samples/bios.json', 'r') as f:
bios_json = json.loads(f.read())
with open('sushy/tests/unit/json_samples/bios_settings.json',
'r') as f:
bios_settings_json = json.loads(f.read())
with open('sushy/tests/unit/json_samples/bios.json') as f:
bios_json = json.load(f)
with open('sushy/tests/unit/json_samples/bios_settings.json') as f:
bios_settings_json = json.load(f)
self.conn.get.return_value.json.side_effect = [
bios_json,

View File

@ -25,9 +25,9 @@ class EthernetInterfaceTestCase(base.TestCase):
def setUp(self):
super(EthernetInterfaceTestCase, self).setUp()
self.conn = mock.Mock()
eth_file = 'sushy/tests/unit/json_samples/ethernet_interfaces.json'
with open(eth_file, 'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
with open('sushy/tests/unit/json_samples/'
'ethernet_interfaces.json') as f:
self.conn.get.return_value.json.return_value = json.load(f)
eth_path = ("/redfish/v1/Systems/437XR1138R2/EthernetInterfaces/"
"12446A3B0411")
@ -54,8 +54,8 @@ class EthernetInterfaceCollectionTestCase(base.TestCase):
super(EthernetInterfaceCollectionTestCase, self).setUp()
self.conn = mock.Mock()
with open('sushy/tests/unit/json_samples/'
'ethernet_interfaces_collection.json', 'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
'ethernet_interfaces_collection.json') as f:
self.conn.get.return_value.json.return_value = json.load(f)
self.sys_eth_col = ethernet_interface.EthernetInterfaceCollection(
self.conn, '/redfish/v1/Systems/437XR1138R2/EthernetInterfaces',
redfish_version='1.0.2')
@ -96,9 +96,9 @@ class EthernetInterfaceCollectionTestCase(base.TestCase):
def test_summary(self):
self.assertIsNone(self.sys_eth_col._summary)
self.conn.get.return_value.json.reset_mock()
path = 'sushy/tests/unit/json_samples/ethernet_interfaces.json'
with open(path, 'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
with open('sushy/tests/unit/json_samples/'
'ethernet_interfaces.json') as f:
self.conn.get.return_value.json.return_value = json.load(f)
expected_summary = {
'12:44:6A:3B:04:11':
sys_map.HEALTH_STATE_VALUE_MAP_REV.get(

View File

@ -26,8 +26,8 @@ class ProcessorTestCase(base.TestCase):
def setUp(self):
super(ProcessorTestCase, self).setUp()
self.conn = mock.Mock()
with open('sushy/tests/unit/json_samples/processor.json', 'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
with open('sushy/tests/unit/json_samples/processor.json') as f:
self.conn.get.return_value.json.return_value = json.load(f)
self.sys_processor = processor.Processor(
self.conn, '/redfish/v1/Systems/437XR1138R2/Processors/CPU1',
@ -74,8 +74,8 @@ class ProcessorCollectionTestCase(base.TestCase):
super(ProcessorCollectionTestCase, self).setUp()
self.conn = mock.Mock()
with open('sushy/tests/unit/json_samples/'
'processor_collection.json', 'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
'processor_collection.json') as f:
self.conn.get.return_value.json.return_value = json.load(f)
self.sys_processor_col = processor.ProcessorCollection(
self.conn, '/redfish/v1/Systems/437XR1138R2/Processors',
redfish_version='1.0.2')
@ -115,10 +115,11 @@ class ProcessorCollectionTestCase(base.TestCase):
def _setUp_processor_summary(self):
self.conn.get.return_value.json.reset_mock()
successive_return_values = []
with open('sushy/tests/unit/json_samples/processor.json', 'r') as f:
successive_return_values.append(json.loads(f.read()))
with open('sushy/tests/unit/json_samples/processor2.json', 'r') as f:
successive_return_values.append(json.loads(f.read()))
file_names = ['sushy/tests/unit/json_samples/processor.json',
'sushy/tests/unit/json_samples/processor2.json']
for file_name in file_names:
with open(file_name) as f:
successive_return_values.append(json.load(f))
self.conn.get.return_value.json.side_effect = successive_return_values
@ -155,8 +156,8 @@ class ProcessorCollectionTestCase(base.TestCase):
self.conn.get.return_value.json.side_effect = None
# On refreshing the sys_processor_col instance...
with open('sushy/tests/unit/json_samples/'
'processor_collection.json', 'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
'processor_collection.json') as f:
self.conn.get.return_value.json.return_value = json.load(f)
self.sys_processor_col.refresh(force=True)
# | WHEN & THEN |

View File

@ -33,8 +33,8 @@ class SystemTestCase(base.TestCase):
def setUp(self):
super(SystemTestCase, self).setUp()
self.conn = mock.Mock()
with open('sushy/tests/unit/json_samples/system.json', 'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
with open('sushy/tests/unit/json_samples/system.json') as f:
self.conn.get.return_value.json.return_value = json.load(f)
self.sys_inst = system.System(
self.conn, '/redfish/v1/Systems/437XR1138R2',
@ -271,9 +271,9 @@ class SystemTestCase(base.TestCase):
self.assertIsNone(self.sys_inst._processors)
# | GIVEN |
self.conn.get.return_value.json.reset_mock()
with open('sushy/tests/unit/json_samples/processor_collection.json',
'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
with open('sushy/tests/unit/json_samples/'
'processor_collection.json') as f:
self.conn.get.return_value.json.return_value = json.load(f)
# | WHEN |
actual_processors = self.sys_inst.processors
# | THEN |
@ -291,16 +291,16 @@ class SystemTestCase(base.TestCase):
def test_processors_on_refresh(self):
# | GIVEN |
with open('sushy/tests/unit/json_samples/processor_collection.json',
'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
with open('sushy/tests/unit/json_samples/'
'processor_collection.json') as f:
self.conn.get.return_value.json.return_value = json.load(f)
# | WHEN & THEN |
self.assertIsInstance(self.sys_inst.processors,
processor.ProcessorCollection)
# On refreshing the system instance...
with open('sushy/tests/unit/json_samples/system.json', 'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
with open('sushy/tests/unit/json_samples/system.json') as f:
self.conn.get.return_value.json.return_value = json.load(f)
self.sys_inst.invalidate()
self.sys_inst.refresh(force=False)
@ -310,9 +310,9 @@ class SystemTestCase(base.TestCase):
self.assertTrue(self.sys_inst._processors._is_stale)
# | GIVEN |
with open('sushy/tests/unit/json_samples/processor_collection.json',
'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
with open('sushy/tests/unit/json_samples/'
'processor_collection.json') as f:
self.conn.get.return_value.json.return_value = json.load(f)
# | WHEN & THEN |
self.assertIsInstance(self.sys_inst.processors,
processor.ProcessorCollection)
@ -320,18 +320,19 @@ class SystemTestCase(base.TestCase):
def _setUp_processor_summary(self):
self.conn.get.return_value.json.reset_mock()
with open('sushy/tests/unit/json_samples/processor_collection.json',
'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
with open('sushy/tests/unit/json_samples/'
'processor_collection.json') as f:
self.conn.get.return_value.json.return_value = json.load(f)
# fetch processors for the first time
self.sys_inst.processors
successive_return_values = []
with open('sushy/tests/unit/json_samples/processor.json', 'r') as f:
successive_return_values.append(json.loads(f.read()))
with open('sushy/tests/unit/json_samples/processor2.json', 'r') as f:
successive_return_values.append(json.loads(f.read()))
file_names = ['sushy/tests/unit/json_samples/processor.json',
'sushy/tests/unit/json_samples/processor2.json']
for file_name in file_names:
with open(file_name) as f:
successive_return_values.append(json.load(f))
self.conn.get.return_value.json.side_effect = successive_return_values
@ -360,13 +361,12 @@ class SystemTestCase(base.TestCase):
self.conn.get.return_value.json.reset_mock()
eth_coll_return_value = None
eth_return_value = None
path = ('sushy/tests/unit/json_samples/'
'ethernet_interfaces_collection.json')
with open(path, 'r') as f:
eth_coll_return_value = json.loads(f.read())
with open('sushy/tests/unit/json_samples/ethernet_interfaces.json',
'r') as f:
eth_return_value = (json.loads(f.read()))
with open('sushy/tests/unit/json_samples/'
'ethernet_interfaces_collection.json') as f:
eth_coll_return_value = json.load(f)
with open('sushy/tests/unit/json_samples/'
'ethernet_interfaces.json') as f:
eth_return_value = json.load(f)
self.conn.get.return_value.json.side_effect = [eth_coll_return_value,
eth_return_value]
@ -384,8 +384,8 @@ class SystemTestCase(base.TestCase):
def test_bios(self):
self.conn.get.return_value.json.reset_mock()
bios_return_value = None
with open('sushy/tests/unit/json_samples/bios.json', 'r') as f:
bios_return_value = json.loads(f.read())
with open('sushy/tests/unit/json_samples/bios.json') as f:
bios_return_value = json.load(f)
self.conn.get.return_value.json.side_effect = [bios_return_value]
self.assertIsNone(self.sys_inst._bios)
@ -400,8 +400,8 @@ class SystemCollectionTestCase(base.TestCase):
super(SystemCollectionTestCase, self).setUp()
self.conn = mock.Mock()
with open('sushy/tests/unit/json_samples/'
'system_collection.json', 'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
'system_collection.json') as f:
self.conn.get.return_value.json.return_value = json.load(f)
self.sys_col = system.SystemCollection(
self.conn, '/redfish/v1/Systems', redfish_version='1.0.2')

View File

@ -24,9 +24,8 @@ class SettingsFieldTestCase(base.TestCase):
def setUp(self):
super(SettingsFieldTestCase, self).setUp()
with open('sushy/tests/unit/json_samples/settings.json',
'r') as f:
self.json = json.loads(f.read())
with open('sushy/tests/unit/json_samples/settings.json') as f:
self.json = json.load(f)
self.settings = settings.SettingsField()

View File

@ -221,7 +221,7 @@ class ConnectorOpTestCase(base.TestCase):
def test_known_http_error(self):
self.request.return_value.status_code = http_client.BAD_REQUEST
with open('sushy/tests/unit/json_samples/error.json', 'r') as f:
with open('sushy/tests/unit/json_samples/error.json') as f:
self.request.return_value.json.return_value = json.load(f)
with self.assertRaisesRegex(exceptions.BadRequestError,

View File

@ -40,8 +40,8 @@ class MainTestCase(base.TestCase):
self.sess_serv.create_session.return_value = (None, None)
mock_session_service.return_value = self.sess_serv
mock_connector.return_value = self.conn
with open('sushy/tests/unit/json_samples/root.json', 'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
with open('sushy/tests/unit/json_samples/root.json') as f:
self.conn.get.return_value.json.return_value = json.load(f)
self.root = main.Sushy('http://foo.bar:1234',
verify=True, auth=mock_auth)
mock_connector.assert_called_once_with(
@ -68,9 +68,9 @@ class MainTestCase(base.TestCase):
@mock.patch.object(connector, 'Connector', autospec=True)
def test_custom_connector(self, mock_Sushy_Connector):
connector_mock = mock.MagicMock()
with open('sushy/tests/unit/json_samples/root.json', 'r') as f:
with open('sushy/tests/unit/json_samples/root.json') as f:
connector_mock.get.return_value.json.return_value = (
json.loads(f.read()))
json.load(f))
main.Sushy('http://foo.bar:1234', 'foo', 'bar',
connector=connector_mock)
self.assertTrue(connector_mock.post.called)
@ -126,8 +126,8 @@ class BareMinimumMainTestCase(base.TestCase):
super(BareMinimumMainTestCase, self).setUp()
self.conn = mock.MagicMock()
with open('sushy/tests/unit/json_samples/'
'bare_minimum_root.json', 'r') as f:
self.conn.get.return_value.json.return_value = json.loads(f.read())
'bare_minimum_root.json') as f:
self.conn.get.return_value.json.return_value = json.load(f)
self.root = main.Sushy('http://foo.bar:1234', verify=True,
auth=mock.MagicMock(), connector=self.conn)

View File

@ -46,8 +46,8 @@ class UtilsTestCase(base.TestCase):
def setUp(self):
super(UtilsTestCase, self).setUp()
self.conn = mock.MagicMock()
with open('sushy/tests/unit/json_samples/system.json', 'r') as f:
system_json = json.loads(f.read())
with open('sushy/tests/unit/json_samples/system.json') as f:
system_json = json.load(f)
self.conn.get.return_value.json.return_value = system_json
self.sys_inst = system.System(self.conn,