The zhmcclient PyPI package provides unit testing support for its users via its zhmcclient_mock
Python package. That package allows users of zhmcclient to easily define a faked HMC that is populated with resources as needed by the test case.
The faked HMC environment is set up by creating an instance of the zhmcclient_mock.FakedSession
class instead of the zhmcclient.Session
class:
import zhmcclient
import zhmcclient_mock
session = zhmcclient_mock.FakedSession('fake-host', 'fake-hmc', '2.13.1',
'1.8')
client = zhmcclient.Client(session)
cpcs = client.cpcs.list()
. . .
Other than using a different session class, the code operates against the same zhmcclient API as before. For example, you can see in the example above that the client object is set up from the same zhmcclient.Client
class as before, and that the CPCs can be listed through the API of the client object as before.
The difference is that the faked session object contains a faked HMC and does not communicate at all with an actual HMC.
The faked HMC of the faked session object can be accessed via the ~zhmcclient_mock.FakedSession.hmc
attribute of the faked session object in order to populate it with resources, for example to build up an initial resource environment for a test case.
The following example of a unit test case shows how an initial set of resources that is defined as a dictionary and loaded into the faked HMC using the ~zhmcclient_mock.FakedHmc.add_resources
method:
import unittest
import zhmcclient
import zhmcclient_mock
class MyTests(unittest.TestCase):
def setUp(self):
self.session = zhmcclient_mock.FakedSession(
'fake-host', 'fake-hmc', '2.13.1', '1.8')
self.session.hmc.add_resources({
'cpcs': [
{
'properties': {
'name': 'cpc_1',
'description': 'CPC #1',
},
'adapters': [
{
'properties': {
'name': 'osa_1',
'description': 'OSA #1',
'adapter-family': 'osa',
},
'ports': [
{
'properties': {
'name': 'osa_1_1',
'description': 'OSA #1 Port #1',
},
},
]
},
]
},
]
})
self.client = zhmcclient.Client(self.session)
def test_list(self):
cpcs = self.client.cpcs.list()
self.assertEqual(len(cpcs), 1)
self.assertEqual(cpcs[0].name, 'cpc_1')
In this example, the test_list()
method tests the CPC list method of the zhmcclient package, but the same approach is used for testing code that uses the zhmcclient package.
As an alternative to bulk-loading resources via the input dictionary, it is also possible to add resources one by one using the add()
methods of the faked resource manager classes, as shown in the following example:
class MyTests(unittest.TestCase):
def setUp(self):
self.session = zhmcclient_mock.FakedSession(
'fake-host', 'fake-hmc', '2.13.1', '1.8')
cpc1 = self.session.hmc.cpcs.add({
'name': 'cpc_1',
'description': 'CPC #1',
})
adapter1 = cpc1.adapters.add({
'name': 'osa_1',
'description': 'OSA #1',
'adapter-family': 'osa',
})
port1 = adapter1.ports.add({
'name': 'osa_1_1',
'description': 'OSA #1 Port #1',
})
self.client = zhmcclient.Client(self.session)
As you can see, the resources need to be added from top to bottom in the resource tree, starting at the ~zhmcclient_mock.FakedSession.hmc
attribute of the faked session object.
Section Faked HMC
describes all faked resource and manager classes that you can use to add resources that way.
Section Faked session
describes the faked session class.
zhmcclient_mock._session
zhmcclient_mock.FakedSession
zhmcclient_mock.HmcDefinitionYamlError
zhmcclient_mock.HmcDefinitionSchemaError
zhmcclient_mock._hmc
zhmcclient_mock.InputError
zhmcclient_mock.FakedHmc
zhmcclient_mock.FakedActivationProfileManager
zhmcclient_mock.FakedActivationProfile
zhmcclient_mock.FakedAdapterManager
zhmcclient_mock.FakedAdapter
zhmcclient_mock.FakedCapacityGroupManager
zhmcclient_mock.FakedCapacityGroup
zhmcclient_mock.FakedConsoleManager
zhmcclient_mock.FakedConsole
zhmcclient_mock.FakedCpcManager
zhmcclient_mock.FakedCpc
zhmcclient_mock.FakedHbaManager
zhmcclient_mock.FakedHba
zhmcclient_mock.FakedLdapServerDefinitionManager
zhmcclient_mock.FakedLdapServerDefinition
zhmcclient_mock.FakedLparManager
zhmcclient_mock.FakedLpar
zhmcclient_mock.FakedNicManager
zhmcclient_mock.FakedNic
zhmcclient_mock.FakedPartitionManager
zhmcclient_mock.FakedPartition
zhmcclient_mock.FakedPasswordRuleManager
zhmcclient_mock.FakedPasswordRule
zhmcclient_mock.FakedPortManager
zhmcclient_mock.FakedPort
zhmcclient_mock.FakedTaskManager
zhmcclient_mock.FakedTask
zhmcclient_mock.FakedUnmanagedCpcManager
zhmcclient_mock.FakedUnmanagedCpc
zhmcclient_mock.FakedUserManager
zhmcclient_mock.FakedUser
zhmcclient_mock.FakedUserPatternManager
zhmcclient_mock.FakedUserPattern
zhmcclient_mock.FakedUserRoleManager
zhmcclient_mock.FakedUserRole
zhmcclient_mock.FakedVirtualFunctionManager
zhmcclient_mock.FakedVirtualFunction
zhmcclient_mock.FakedVirtualSwitchManager
zhmcclient_mock.FakedVirtualSwitch
zhmcclient_mock.FakedMetricsContextManager
zhmcclient_mock.FakedMetricsContext
zhmcclient_mock.FakedMetricGroupDefinition
zhmcclient_mock.FakedMetricObjectValues
zhmcclient_mock.FakedBaseManager
zhmcclient_mock.FakedBaseResource
zhmcclient_mock._urihandler
zhmcclient_mock.LparActivateHandler
zhmcclient_mock.LparDeactivateHandler
zhmcclient_mock.LparLoadHandler