def check_attach_ethernet_card(self, settings): _ctx = self._gen_relation_ctx() conn_mock = Mock() smart_connect = MagicMock(return_value=conn_mock) with patch("vsphere_plugin_common.SmartConnectNoSSL", smart_connect): with patch("vsphere_plugin_common.Disconnect", Mock()): # use unexisted network _ctx.source.instance.runtime_properties.update(settings) network = None with patch( "vsphere_plugin_common.VsphereClient._get_obj_by_name", MagicMock(return_value=network)): with self.assertRaises(NonRecoverableError) as e: devices.attach_ethernet_card(ctx=_ctx) self.assertEqual(e.exception.message, "Network Cloudify could not be found") # without vm-id / distributed _ctx.source.instance.runtime_properties[ 'switch_distributed'] = True network = Mock() network.obj = network network.config.distributedVirtualSwitch.uuid = "aa-bb-vv" network.key = "121" with patch( "vsphere_plugin_common.VsphereClient._get_obj_by_name", MagicMock(return_value=network)): with self.assertRaises(NonRecoverableError) as e: devices.attach_ethernet_card(ctx=_ctx) self.assertEqual(e.exception.message, "VM is not defined") # without vm-id / simple network _ctx.source.instance.runtime_properties[ 'switch_distributed'] = False network = vim.Network("Cloudify") network.obj = network with patch( "vsphere_plugin_common.VsphereClient._get_obj_by_name", MagicMock(return_value=network)): with self.assertRaises(NonRecoverableError) as e: devices.attach_ethernet_card(ctx=_ctx) self.assertEqual(e.exception.message, "VM is not defined") # issues with add device _ctx.target.instance.runtime_properties[ 'vsphere_server_id'] = "vm-101" network = vim.Network("Cloudify") network.obj = network vm = self._get_vm() with patch( "vsphere_plugin_common.VsphereClient._get_obj_by_id", MagicMock(return_value=vm)): with patch( "vsphere_plugin_common.VsphereClient._get_obj_by_name", MagicMock(return_value=network)): with self.assertRaises(NonRecoverableError) as e: devices.attach_ethernet_card(ctx=_ctx) self.assertEqual( e.exception.message, "Have not found key for new added device")
def test_create_ippool(self): client = vsphere_plugin_common.NetworkClient() # no such datacenter client._get_obj_by_name = Mock(return_value=None) with self.assertRaises(NonRecoverableError): client.create_ippool("datacenter", {}, []) # create distribute datacenter = Mock() network = Mock() network.obj = vim.Network(Mock()) results = [network, datacenter] def _get_obj_by_name(_type, _name): return results.pop() client._get_obj_by_name = _get_obj_by_name network_instance = Mock() network_instance.runtime_properties = { "network_name": "some", "switch_distributed": True } client.si = Mock() client.si.content.ipPoolManager.CreateIpPool = Mock(return_value=124) self.assertEqual( client.create_ippool( "datacenter", { "name": "ippool-check", "subnet": "192.0.2.0", "netmask": "255.255.255.0", "gateway": "192.0.2.254", "range": "192.0.2.1#12" }, [network_instance]), 124) # legacy network client._get_obj_by_name = Mock(return_value=datacenter) client._collect_properties = Mock( return_value=[{ "obj": vim.Network("network_id"), "name": "some" }]) network_instance = Mock() network_instance.runtime_properties = { "network_name": "some", "switch_distributed": False } client.si = Mock() client.si.content.ipPoolManager.CreateIpPool = Mock(return_value=124) self.assertEqual( client.create_ippool( "datacenter", { "name": "ippool-check", "subnet": "192.0.2.0", "netmask": "255.255.255.0", "gateway": "192.0.2.254", "range": "192.0.2.1#12" }, [network_instance]), 124) client._collect_properties.assert_called_once_with(vim.Network, path_set=['name'])
def check_attach_ethernet_card(self, settings): _ctx = self._gen_relation_ctx() conn_mock = Mock() smart_connect = MagicMock(return_value=conn_mock) with patch("vsphere_plugin_common.SmartConnectNoSSL", smart_connect): with patch("vsphere_plugin_common.Disconnect", Mock()): # use unexisted network _ctx.source.instance.runtime_properties.update(settings) network = None with patch( "vsphere_plugin_common.VsphereClient._get_obj_by_name", MagicMock(return_value=network)): with self.assertRaises(NonRecoverableError) as e: devices.attach_ethernet_card(ctx=_ctx) self.assertEqual(e.exception.message, "Network Cloudify could not be found") # without vm-id / distributed _ctx.source.instance.runtime_properties[ 'switch_distributed'] = True network = Mock() network.obj = network network.config.distributedVirtualSwitch.uuid = "aa-bb-vv" network.key = "121" with patch( "vsphere_plugin_common.VsphereClient._get_obj_by_name", MagicMock(return_value=network)): with self.assertRaises(NonRecoverableError) as e: devices.attach_ethernet_card(ctx=_ctx) self.assertEqual(e.exception.message, "VM is not defined") # without vm-id / simple network _ctx.source.instance.runtime_properties[ 'switch_distributed'] = False network = vim.Network("Cloudify") network.obj = network with patch( "vsphere_plugin_common.VsphereClient._get_obj_by_name", MagicMock(return_value=network)): with self.assertRaises(NonRecoverableError) as e: devices.attach_ethernet_card(ctx=_ctx) self.assertEqual(e.exception.message, "VM is not defined") # issues with add device _ctx.target.instance.runtime_properties[ 'vsphere_server_id'] = "vm-101" network = vim.Network("Cloudify") network.obj = network vm = self._get_vm() with patch( "vsphere_plugin_common.VsphereClient._get_obj_by_id", MagicMock(return_value=vm)): with patch( "vsphere_plugin_common.VsphereClient._get_obj_by_name", MagicMock(return_value=network)): with self.assertRaises(NonRecoverableError) as e: devices.attach_ethernet_card(ctx=_ctx) self.assertEqual( e.exception.message, "Have not found key for new added device") args, kwargs = vm.obj.ReconfigVM_Task.call_args self.assertEqual(args, ()) self.assertEqual(kwargs.keys(), ['spec']) new_adapter = str(type(kwargs['spec'].deviceChange[0].device)) if settings.get('adapter_type'): self.assertTrue(settings['adapter_type'].lower() in new_adapter.lower()) else: self.assertEqual( new_adapter, "<class 'pyVmomi.VmomiSupport.vim.vm.device." "VirtualVmxnet3'>")