Refactor driver.py testcase

Refactor unit test case for driver.py
specially remove
vm.CONF = mock.Mock()
vm.CONF.host = "fake-mini"
vm.CONF.dpm.physical_storage_adapter_mappings = "mapping"
because it is overriding other testcase in other module.

Also remove test case for vm.py and host.py because when
we are trying to write test case for other module then
few variables are getting overrided. Anyways we need to change
the testcase using zhmcclient_mock framework then that time
any how we have to delete these test cases. So we are deleting
now to not get conflict in other module.

Change-Id: I4d9dfcf73bda61670eca41e3db92dcfeb5a1c44a
Signed-off-by: Prabhat Ranjan <pranjank@in.ibm.com>
This commit is contained in:
Prabhat Ranjan 2017-03-09 13:22:21 +05:30
parent 2b09b80a3b
commit 4e9526f42b
5 changed files with 5 additions and 826 deletions

View File

@ -1,58 +0,0 @@
# Copyright 2016 IBM Corp. All Rights Reserved.
#
# 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.
def getFakeCPCconf():
conf = CONF()
return conf
def getFakeInstance():
props = {
'hostname': 'DummyPartition',
'uuid': '6511ee0f-0d64-4392-b9e0-cdbea10a17c3'
}
instance = PartitionInstance(props)
return instance
class PartitionInstance(object):
hostname = None
def __init__(self, properties):
self.properties = properties
global hostname
hostname = properties['hostname']
def save(self):
return
@property
def uuid(self):
return self.properties['uuid']
class DPM(object):
cpc_object_id = "1232132"
max_processors = 10
max_memory = 200
max_partitions = 10
physical_storage_adapter_mappings = [
"439da232-b18d-11e6-9c12-42f2e9ef1641:0"]
class CONF(object):
host = "S12subset"
dpm = DPM()

View File

@ -1,437 +0,0 @@
# Copyright 2016 IBM Corp. All Rights Reserved.
#
# 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.
"""Fake zhmcclient"""
from nova_dpm.tests.unit.virt.dpm import fakeutils
CONF = fakeutils.getFakeCPCconf()
# We have considered 3 fake partition for unit test in one CPC.
# DummyPartition1, DummyPartition2, DummyPartition3
# Data for Fake partition1
INSTANCE_NAME1 = "6511ee0f-0d64-4392-b9e0-cdbea10a17c4"
PARTITION_NAME1 = ("OpenStack-"
+ CONF.host + "-6511ee0f-0d64-4392-b9e0-cdbea10a17c4")
PARTITION_URI1 = "/api/partitions/00000000-aaba-bbbb-cccc-abcdabcdabcd"
PARTITION_CP_PROCESSOR1 = 1
PARTITION_INITIAL_MEMORY1 = 512
# Data for Fake partition2
INSTANCE_NAME2 = "6511ee0f-0d64-4392-b9e0-cdbea10a17c5"
PARTITION_NAME2 = ("OpenStack-"
+ CONF.host + "-6511ee0f-0d64-4392-b9e0-cdbea10a17c5")
PARTITION_URI2 = "/api/partitions/00000000-aaba-bcbb-cccc-abcdabcdabcd"
PARTITION_CP_PROCESSOR2 = 2
PARTITION_INITIAL_MEMORY2 = 1024
# Data for Fake partition3
INSTANCE_NAME3 = "6511ee0f-0d64-4392-b9e0-cdbea10a17c6"
PARTITION_NAME3 = ("OpenStack-"
+ CONF.host + "-6511ee0f-0d64-4392-b9e0-cdbea10a17c6")
PARTITION_URI3 = "/api/partitions/00000000-aaba-bbbb-cdcc-abcdabcdabcd"
PARTITION_CP_PROCESSOR3 = 1
PARTITION_INITIAL_MEMORY3 = 512
# In our scenario we have considered 3 partition.
# Here idea is the partition which will use maximum cp-processor
# we will consider that maximum cp-processor as used processor.
# We will use this Maximum cp-processor to test used cp-processor.
# For further details please see testcase
# nova_dpm.
# tests.unit.virt.dpm.test_host.HostTestCase.test_get_proc_used
# So in our above scenario partition2 ("DummyPartition2")
# is using maximum cp-processor
MAX_CP_PROCESSOR = PARTITION_CP_PROCESSOR2
# For used memory we are using fake partition1, partition2 and partition3
# For more info please check function getFakeInstanceList()
# in fakeutils.py
# So used memory will be
# PARTITION_INITIAL_MEMORY1
# + PARTITION_INITIAL_MEMORY2
# + PARTITION_INITIAL_MEMORY2
USED_MEMORY = (PARTITION_INITIAL_MEMORY1
+ PARTITION_INITIAL_MEMORY2
+ PARTITION_INITIAL_MEMORY3)
def getCpcmgr(ipaddress, username, password):
session = Session(ipaddress, username, password)
client = Client(session)
cpcmgr = CpcManager(client)
return cpcmgr
def getCpcmgrForClient(client):
cpcmgr = CpcManager(client)
return cpcmgr
def getdummyCpcmgr():
zhmclientcpcmgr = getzhmclientCpcmgr("0.0.0.0",
"dummyuser", "dummypassword")
return zhmclientcpcmgr
def getzhmclientCpcmgr(ipaddress, username, password):
session = Session(ipaddress, username, password)
client = Client(session)
cpcmgr = CpcManager(client)
return cpcmgr
def getFakeCPC(cpcmanager=None):
cpc_props = dict()
cpc_props['object-uri'] = "/api/cpc/dummy"
cpc_props['name'] = "fakecpc"
cpc_props['storage-customer'] = 100
cpc_props['processor-count-pending-ifl'] = 6
cpc_props['processor-count-ifl'] = 12
cpc_props['storage-customer-available'] = 500
if not cpcmanager:
cpcmanager = getdummyCpcmgr()
cpc = Cpc(cpcmanager, cpc_props['object-uri'], cpc_props)
return cpc
def getFakeCPCconf():
conf = {'cpcsubset_name': "S12subset",
'cpc_object_id': "1232132",
'max_processors': 10,
'max_memory_mb': 200,
'max_partitions': 10
}
return conf
def getFakeCPCwithProp(cpcmanager, cpc_props):
cpc = Cpc(cpcmanager, cpc_props['object-uri'], cpc_props)
return cpc
def getFakePartition():
partition_props = dict()
partition_props['name'] = (
"OpenStack-" + CONF.host + "-6511ee0f-0d64-4392-b9e0-cdbea10a17c3")
partition_props['description'] = "OpenStack CPCSubset=foo"
partition_props['object-uri'] = "/api/partitions/" \
"00000000-aaaa-bbbb-cccc-abcdabcdabcd"
partition_props['initial-memory'] = 0
partition_props['ifl-processors'] = 0
partition_props['boot-os-specific-parameters'] = "foo"
partition_props['state'] = "Active"
partition = Partition(getdummyCpcmgr(), partition_props['object-uri'],
partition_props)
return partition
def get_fake_partition(properties):
partition = Partition(
getdummyCpcmgr(),
properties['object-uri'], properties)
return partition
def get_fake_partition_list():
partition_list = []
properties1 = {
'name': PARTITION_NAME1,
'object-uri': PARTITION_URI1,
'initial-memory': PARTITION_INITIAL_MEMORY1,
'ifl-processors': PARTITION_CP_PROCESSOR1
}
properties2 = {
'name': PARTITION_NAME2,
'object-uri': PARTITION_URI2,
'initial-memory': PARTITION_INITIAL_MEMORY2,
'ifl-processors': PARTITION_CP_PROCESSOR2
}
properties3 = {
'name': PARTITION_NAME3,
'object-uri': PARTITION_URI3,
'initial-memory': PARTITION_INITIAL_MEMORY3,
'ifl-processors': PARTITION_CP_PROCESSOR3
}
partition_list.append(get_fake_partition(properties1))
partition_list.append(get_fake_partition(properties2))
partition_list.append(get_fake_partition(properties3))
partition_list.append(getFakePartition())
return partition_list
def getFakeNicManager():
nics = NicManager(getFakePartition())
return nics
def getFakeNic(properties=None):
if not properties:
properties = {}
properties['element-uri'] = "/api/partitions/" \
"00000000-aaaa-bbbb-cccc-abcdabcdabcd" \
"/nics/00000000-nics-bbbb-cccc-abcdabcdabcd"
nic = Nic(getFakeNicManager(), properties['element-uri'],
properties)
return nic
def getFakeHbaManager():
hbas = HbaManager(getFakePartition())
return hbas
def getFakeHba(properties=None):
if not properties:
properties = {}
properties['element-uri'] = "/api/partitions/" \
"00000000-aaaa-bbbb-cccc-abcdabcdabcd" \
"/hbas/00000000-nics-bbbb-cccc-abcdabcdabcd"
hba = Hba(getFakeHbaManager(), properties['element-uri'],
properties)
return hba
def getFakeAdapterManager():
adapters = AdapterManager(getFakeCPC())
return adapters
def getFakeAdapter(properties=None):
if not properties:
properties = {}
properties['object-uri'] = "/api/adapters/" \
"9b926334-8e01-11e5-b1a4-9abe94228ee1"
properties['type'] = "fcp"
adapter = Adapter(getFakeAdapterManager(), properties['object-uri'],
properties)
return adapter
class BaseResource(object):
def __init__(self, manager, object_uri, properties):
self.manager = manager
self.object_uri = object_uri
self.properties = properties
def get_property(self, name):
return self.properties[name]
class BaseManager(object):
def __init__(self, resource):
self.resource = resource
class CpcManager(BaseManager):
"""fake cpcmanager"""
def __init__(self, client):
# This function should not go into the docs.
# Parameters:
# client (:class:`~zhmcclient.Client`):
# Client object for the HMC to be used.
super(CpcManager, self).__init__(Cpc)
self._session = client.session
def list(self, full_properties=False):
cpc_list = []
cpc_list.append(getFakeCPC(getdummyCpcmgr()))
return cpc_list
def find(self, **kwargs):
return getFakeCPC(getdummyCpcmgr())
class Cpc(BaseResource):
def __init__(self, manager, uri, properties):
super(Cpc, self).__init__(manager, uri, properties)
@property
def dpm_enabled(self):
return True
def pull_full_properties(self):
self._pull_full_properties = True
@property
def partitions(self):
return PartitionManager(self)
@property
def adapters(self):
return getFakeAdapterManager()
class PartitionManager(BaseManager):
"""fake cpcmanager"""
def __init__(self, cpc):
# This function should not go into the docs.
# Parameters:
# cpc (:class:`~zhmcclient.Cpc`):
# CPC defining the scope for this manager.
super(PartitionManager, self).__init__(cpc)
def list(self, full_properties=False):
part_list = get_fake_partition_list()
part_list.append(getFakePartition())
return part_list
def find(self, **kwargs):
return getFakePartition()
class Partition(BaseResource):
def __init__(self, manager, uri, properties):
super(Partition, self).__init__(manager, uri, properties)
def pull_full_properties(self):
self._pull_full_properties = True
class NicManager(BaseManager):
"""fake cpcmanager"""
def __init__(self, partition):
# This function should not go into the docs.
# Parameters:
# partition (:class:`~zhmcclient.Partition`):
# Partition defining the scope for this manager.
super(NicManager, self).__init__(partition)
def list(self, full_properties=False):
nic_list = []
nic_list.append(getFakeNic())
return nic_list
def create(self, properties):
return getFakeNic(properties)
def find(self, **kwargs):
return getFakeNic()
class Nic(BaseResource):
def __init__(self, manager, uri, properties):
super(Nic, self).__init__(manager, uri, properties)
def pull_full_properties(self):
self._pull_full_properties = True
class HbaManager(BaseManager):
"""fake cpcmanager"""
def __init__(self, partition):
# This function should not go into the docs.
# Parameters:
# partition (:class:`~zhmcclient.Partition`):
# Partition defining the scope for this manager.
super(HbaManager, self).__init__(partition)
def list(self, full_properties=False):
hba_list = []
hba_list.append(getFakeHba())
return hba_list
def create(self, properties):
return getFakeHba(properties)
def find(self, **kwargs):
return getFakeHba()
class Hba(BaseResource):
def __init__(self, manager, uri, properties):
super(Hba, self).__init__(manager, uri, properties)
def pull_full_properties(self):
self._pull_full_properties = True
class AdapterManager(BaseManager):
"""fake cpcmanager"""
def __init__(self, partition):
# This function should not go into the docs.
# Parameters:
# partition (:class:`~zhmcclient.Partition`):
# Partition defining the scope for this manager.
super(AdapterManager, self).__init__(partition)
def list(self, full_properties=False):
adapter_list = []
adapter_list.append(getFakeAdapter())
return adapter_list
def create(self, properties):
return getFakeAdapter(properties)
def find(self, **kwargs):
return getFakeAdapter()
class Adapter(BaseResource):
def __init__(self, manager, uri, properties):
super(Adapter, self).__init__(manager, uri, properties)
def pull_full_properties(self):
self._pull_full_properties = True
class Session(object):
"""fake Session"""
def __init__(self, host, userid=None, password=None):
self._host = host
self._userid = userid
self._password = password
return None
@property
def userid(self):
return self._userid
class Client(object):
"""fake client"""
def __init__(self, session):
self._session = session
self._cpcs = CpcManager(self)
self._api_version = None
@property
def cpcs(self):
cpcmgr = getCpcmgr("0.0.0.0", "dummyuser", "dummypassword")
return cpcmgr
@property
def session(self):
return self._session

View File

@ -62,21 +62,18 @@ class DPMdriverInitHostTestCase(TestCase):
self.flags(group="dpm", max_memory=512)
self.dpmdriver.init_host(None)
@mock.patch.object(driver.LOG, 'debug')
def test_get_available_resource(self, mock_warning):
def test_get_available_resource(self):
host_properties = self.dpmdriver.get_available_resource(None)
self.assertEqual('cpc_2', host_properties['cpc_name'])
@mock.patch.object(driver.LOG, 'debug')
def test_invalid_mem_config(self, mock_warning):
def test_invalid_mem_config(self):
self.flags(group="dpm", max_memory=2000)
self.assertRaises(exception.ValidationError,
self.dpmdriver.init_host,
None)
@mock.patch.object(driver.LOG, 'debug')
def test_invalid_proc_config(self, mock_warning):
def test_invalid_proc_config(self):
self.flags(group="dpm", max_processors=50)
self.assertRaises(exception.ValidationError,
@ -211,9 +208,8 @@ class DPMDriverInstanceTestCase(TestCase):
cpc = self.client.cpcs.find(**{"object-id": "2"})
self.dpmdriver._cpc = cpc
vm.CONF = mock.Mock()
vm.CONF.host = "fake-mini"
vm.CONF.dpm.physical_storage_adapter_mappings = "mapping"
self.flags(host="fake-mini")
self.flags(group="dpm", physical_storage_adapter_mappings="mapping")
mock_instance = mock.Mock()
mock_instance.uuid = "1"

View File

@ -1,119 +0,0 @@
# Copyright 2016 IBM Corp. All Rights Reserved.
#
# 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
import mock
from nova.test import TestCase
from nova_dpm.tests.unit.virt.dpm import fakeutils
from nova_dpm.tests.unit.virt.dpm import fakezhmcclient
from nova_dpm.virt.dpm import host
from nova_dpm.virt.dpm import vm
"""
cpcsubset unit testcase
"""
host.CONF = fakeutils.getFakeCPCconf()
def fakeHost():
session = fakezhmcclient.Session("hostip", "dummyhost", "dummyhost")
client = fakezhmcclient.Client(session)
cpcmanager = fakezhmcclient.getCpcmgrForClient(client)
cpc = fakezhmcclient.getFakeCPC(cpcmanager)
host1 = host.Host(cpc, client)
return host1
def fakeProperties():
props = {}
props = {"memory_mb": 100,
"vcpus": 10,
'vcpus_used': 5,
"local_gb": 1024,
"memory_mb_used": 1024,
"local_gb_used": 0,
"cpu_info": None,
"hypervisor_type": "PRSM",
"hypervisor_version": "1",
"numa_topology": "",
"hypervisor_hostname": "FakepropsHostname",
"cpc_name": "FakepropsCpcName",
"disk_available_least": 1024,
# relevant value
'supported_instances':
[("S390", "DPM", "hvm")]}
return props
class HostTestCase(TestCase):
def setUp(self):
super(HostTestCase, self).setUp()
self._session = fakezhmcclient.Session(
"dummy", "dummy", "dummy")
self._client = fakezhmcclient.Client(self._session)
self._cpcmanager = fakezhmcclient.getCpcmgrForClient(self._client)
self._cpc = fakezhmcclient.getFakeCPC(self._cpcmanager)
self.host_obj = host.Host(self._cpc, self._client)
@mock.patch.object(host.LOG, 'debug')
def test_host(self, mock_warning):
host.Host(self._cpc, self._client)
expected_arg = "Host initializing done"
assertlogs = False
for call in mock_warning.call_args_list:
if (len(call) > 0):
if (len(call[0]) > 0 and call[0][0] == expected_arg):
assertlogs = True
self.assertTrue(assertlogs)
@mock.patch.object(host.LOG, 'debug')
def test_host_properties(self, mock_warning):
client = fakezhmcclient.Client(self._session)
cpcmanager = fakezhmcclient.getCpcmgrForClient(client)
cpc = fakezhmcclient.getFakeCPC(cpcmanager)
host1 = host.Host(cpc, client)
host_properties = host1.properties
self.assertEqual(host_properties['hypervisor_hostname'],
'S12subset')
self.assertEqual(host_properties['cpc_name'], 'fakecpc')
cpu_info = host_properties['cpu_info']
cpu_info_dict = json.loads(cpu_info)
self.assertEqual(cpu_info_dict['arch'], 's390x')
self.assertEqual(cpu_info_dict['vendor'], 'IBM')
@mock.patch.object(
vm,
'cpcsubset_partition_list',
return_value=fakezhmcclient.get_fake_partition_list())
def test_get_proc_used(self, mock_partitions_list):
proc_used = self.host_obj._get_proc_used()
self.assertEqual(proc_used, fakezhmcclient.MAX_CP_PROCESSOR)
@mock.patch.object(
vm,
'cpcsubset_partition_list',
return_value=fakezhmcclient.get_fake_partition_list())
def test_mem_used(self, mock_partitions_list):
memory_used = self.host_obj._get_mem_used()
self.assertEqual(memory_used, fakezhmcclient.USED_MEMORY)

View File

@ -1,203 +0,0 @@
# Copyright 2016 IBM Corp. All Rights Reserved.
#
# 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 nova.compute import manager as compute_manager
from nova import exception
from nova.test import TestCase
from nova_dpm.tests.unit.virt.dpm import fakeutils
from nova_dpm.tests.unit.virt.dpm import fakezhmcclient
from nova_dpm.virt.dpm import exceptions
from nova_dpm.virt.dpm import vm
"""
vm unit testcase
"""
vm.CONF = fakeutils.getFakeCPCconf()
def getMockInstance():
session = fakezhmcclient.Session("hostip", "dummyhost", "dummyhost")
client = fakezhmcclient.Client(session)
cpc = fakezhmcclient.getFakeCPC()
inst = vm.PartitionInstance(fakeutils.getFakeInstance(), cpc, client)
inst.partition = fakezhmcclient.getFakePartition()
return inst
class VmFunctionTestCase(TestCase):
def setUp(self):
super(VmFunctionTestCase, self).setUp()
self.valid_name = (
'OpenStack-'
+ vm.CONF.host + '-6511ee0f-0d64-4392-b9e0-cdbea10a17c3')
self.invalid_name = 'OpenStack-Instance-6511ee0f'
self.cpc = fakezhmcclient.getFakeCPC()
@mock.patch.object(vm.PartitionInstance, 'get_partition',
return_value=None)
def test_partition_destroy_partition_none(self, mock_get_part):
mock_nova_inst = mock.Mock()
mock_nova_inst.hostname = 'foo'
mock_nova_inst.uuid = 'foo-id'
inst = vm.PartitionInstance(
mock_nova_inst, mock.Mock(), mock.Mock())
self.assertRaises(exception.InstanceNotFound,
inst.destroy)
def test_set_boot_os_specific_parameters(self):
mock_part = mock.Mock()
with mock.patch.object(vm.PartitionInstance, 'get_partition',
return_value=mock_part):
inst = vm.PartitionInstance(None, None, None)
inst.set_boot_os_specific_parameters("foo")
mock_part.update_properties.assert_called_with(
{"boot-os-specific-parameters": "foo"})
def test_set_boot_os_specific_parameters_exceeded(self):
mock_part = mock.Mock()
data = "a" * 257
with mock.patch.object(vm.PartitionInstance, 'get_partition',
return_value=mock_part):
inst = vm.PartitionInstance(None, None, None)
self.assertRaises(
exceptions.BootOsSpecificParametersPropertyExceededError,
inst.set_boot_os_specific_parameters, data)
class InstancePropertiesTestCase(TestCase):
def setUp(self):
super(InstancePropertiesTestCase, self).setUp()
self.mock_nova_inst = mock.Mock()
self.mock_nova_inst.uuid = 'foo-id'
@mock.patch.object(vm.PartitionInstance, 'get_partition')
def test_partition_name(self, mock_get_part):
inst = vm.PartitionInstance(
self.mock_nova_inst, mock.Mock())
self.assertEqual("OpenStack-" + vm.CONF.host + "-foo-id",
inst.partition_name)
@mock.patch.object(vm.PartitionInstance, 'get_partition')
def test_partition_description(self, mock_get_part):
inst = vm.PartitionInstance(
self.mock_nova_inst, mock.Mock())
self.assertEqual("OpenStack CPCSubset=" + vm.CONF.host,
inst.partition_description)
@mock.patch.object(vm.PartitionInstance, 'get_partition')
def test_properties(self, mock_get_part):
mock_flavor = mock.Mock()
mock_flavor.vcpus = 5
mock_flavor.memory_mb = 2000
inst = vm.PartitionInstance(
self.mock_nova_inst, mock.Mock(), flavor=mock_flavor)
props = inst.properties()
self.assertEqual('OpenStack-'
+ vm.CONF.host + '-foo-id', props['name'])
self.assertEqual('OpenStack CPCSubset=' + vm.CONF.host,
props['description'])
self.assertEqual(5, props['ifl-processors'])
self.assertEqual(2000, props['initial-memory'])
self.assertEqual(2000, props['maximum-memory'])
class VmNicTestCase(TestCase):
def setUp(self):
super(VmNicTestCase, self).setUp()
vm.zhmcclient = fakezhmcclient
self.inst = getMockInstance()
self.inst.partition.nics = fakezhmcclient.getFakeNicManager()
self.vif1 = {
'id': 1234, 'type': 'dpm_vswitch', 'address': '12:34:56:78:9A:BC',
'details': {'object_id': '00000000-aaaa-bbbb-cccc-abcdabcdabcd'}}
@mock.patch.object(vm.LOG, 'debug')
def test_attach_nic(self, mock_debug):
ret_val = mock.MagicMock()
ret_val.get_property.return_value = "0001"
# Required to satisfy dict[..] operations on mocks
ret_val .__getitem__.side_effect = dict.__getitem__
with mock.patch.object(fakezhmcclient.NicManager, 'create',
return_value=ret_val) as mock_create:
nic_interface = self.inst.attach_nic(self.vif1)
self.assertEqual(ret_val, nic_interface)
self.assertTrue(mock_create.called)
call_arg_dict = mock_create.mock_calls[0][1][0]
# Name
self.assertTrue(call_arg_dict['name'].startswith('OpenStack'))
self.assertIn(str(1234), call_arg_dict['name'])
# Description
self.assertTrue(call_arg_dict['description'].startswith('OpenStack'))
self.assertIn('mac=12:34:56:78:9A:BC', call_arg_dict['description'])
self.assertIn('CPCSubset=' + vm.CONF.host,
call_arg_dict['description'])
# virtual-switch-uri
self.assertEqual(
'/api/virtual-switches/00000000-aaaa-bbbb-cccc-abcdabcdabcd',
call_arg_dict['virtual-switch-uri'])
class VmHBATestCase(TestCase):
def setUp(self):
super(VmHBATestCase, self).setUp()
vm.zhmcclient = fakezhmcclient
self.inst = getMockInstance()
self.inst.partition.hbas = fakezhmcclient.getFakeHbaManager()
@mock.patch.object(vm.LOG, 'debug')
@mock.patch.object(compute_manager.ComputeManager, '_prep_block_device',
return_value="blockdeviceinfo")
def test_build_resources(self, mock_prep_block_dev, mock_debug):
context = None
novainstance = fakeutils.getFakeInstance()
block_device_mapping = None
resources = self.inst._build_resources(
context, novainstance, block_device_mapping)
self.assertEqual(resources['block_device_info'],
"blockdeviceinfo")
@mock.patch.object(vm.LOG, 'debug')
def test_attach_hba(self, mock_debug):
self.inst.attach_hbas()
class InstancePartitionLifecycleTestCase(TestCase):
@mock.patch.object(vm.PartitionInstance, "_loop_status_update")
def test_launch(self, mock_loop_stat_upd):
mock_part = mock.Mock()
mock_inst = mock.Mock()
with mock.patch.object(vm.PartitionInstance, "get_partition",
return_value=mock_part):
part_inst = vm.PartitionInstance(mock_inst, None, None)
part_inst._boot_os_specific_parameters = "foo"
part_inst.launch()
self.assertEqual("building", mock_inst.vm_state)
self.assertEqual("spawning", mock_inst.task_state)
mock_inst.save.assert_called_once()
mock_part.start.assert_called_once_with(True)