def integration_test_connect_A(self): py_vmomi_service = pyVmomiService(SmartConnect, Disconnect) cred = TestCredentials() si = py_vmomi_service.connect(cred.host, cred.username, cred.password, cred.port) synchronous_task_waiter = SynchronousTaskWaiter() mapper = VnicToNetworkMapper(DvPortGroupNameGenerator()) dv_port_group_creator = DvPortGroupCreator(py_vmomi_service, synchronous_task_waiter) port_group_name_generator = DvPortGroupNameGenerator() virtual_machine_port_group_configurer = VirtualMachinePortGroupConfigurer( py_vmomi_service, synchronous_task_waiter, mapper, VNicService(), port_group_name_generator) mapping = VmNetworkMapping() mapping.vlan_id = [vim.NumericRange(start=65, end=65)] mapping.dv_port_name = DvPortGroupNameGenerator( ).generate_port_group_name(65, 'Trunk') mapping.dv_switch_name = 'dvSwitch' mapping.dv_switch_path = 'QualiSB' mapping.vlan_spec = vim.dvs.VmwareDistributedVirtualSwitch.TrunkVlanSpec( ) connector = VirtualSwitchToMachineConnector( dv_port_group_creator, virtual_machine_port_group_configurer) vm = py_vmomi_service.find_vm_by_name(si, 'QualiSB/Raz', '2') # Act connector.connect_by_mapping(si, vm, [mapping], None, []) pass
def test_name(self): # arrange gen = DvPortGroupNameGenerator() # act name = gen.generate_port_group_name('some_dv_switch', 'id', 'Trunk') # assert self.assertEqual('QS_some_dv_switch_VLAN_id_Trunk', name)
def integration_test_connect_B(self): py_vmomi_service = pyVmomiService(SmartConnect, Disconnect) cred = TestCredentials() si = py_vmomi_service.connect(cred.host, cred.username, cred.password, cred.port) vm_uuid = py_vmomi_service.find_vm_by_name(si, 'QualiSB/Boris', 'Boris2-win7').config.uuid mapping = VmNetworkMapping() mapping.vlan_id = '114' # mapping.dv_port_name = 65 mapping.dv_switch_name = 'dvSwitch' mapping.dv_switch_path = 'QualiSB' mapping.vlan_spec = 'Trunk' vlan_spec = VlanSpecFactory() range_fac = VLanIdRangeParser() synchronous_task_waiter = SynchronousTaskWaiter() name_gen = DvPortGroupNameGenerator() mapper = VnicToNetworkMapper(name_gen) dv_port_group_creator = DvPortGroupCreator(py_vmomi_service, synchronous_task_waiter) virtual_machine_port_group_configurer = VirtualMachinePortGroupConfigurer( py_vmomi_service, synchronous_task_waiter, mapper, VNicService()) connector = VirtualSwitchToMachineConnector( dv_port_group_creator, virtual_machine_port_group_configurer) command = VirtualSwitchConnectCommand(py_vmomi_service, connector, name_gen, vlan_spec, range_fac, Mock()) command.connect_to_networks(si, vm_uuid, [mapping], 'QualiSB/anetwork', [])
def setUp(self): self.portgroup_name = DvPortGroupNameGenerator() self.connector = Mock() self.disconnector = Mock() self.model_parser = Mock() self.vc_data_model = Mock() self.vc_data_model.reserved_networks = 'restricted_network1,restricted_network2' self.vc_data_model.default_dvswitch = 'dvSwitch' self.vc_data_model.default_network = 'Anetwork' self.vc_data_model.default_datacenter = 'datacenter' self.vc_data_model.holding_network = 'Holding Network' self.si = Mock() self.ConnectionCommandOrchestrator = ConnectionCommandOrchestrator( self.connector, self.disconnector, self.model_parser)
def setUp(self): self._si = None self.virtual_machine_path = 'SergiiT' self.virtual_machine_name = 'JustTestNeedToBeRemoved' self.vm_uuid = "422254d5-5226-946e-26fb-60c21898b731" self.vcenter_name = "QualiSB" self.dv_switch_path = 'QualiSB' self.network_path = 'QualiSB' self.dv_switch_name = 'dvSwitch-SergiiT' self.dv_port_group_name = 'aa-dvPortGroup3B' self.network = Mock() self.network.key = "network-key" self.network.name = "QS_123" self.network.config.distributedVirtualSwitch.uuid = "422254d5-5226-946e-26fb-60c21898b73f" self.py_vmomi_service = Mock() self.network2 = Mock() self.network2.key = "network2-key" self.network2.name = "QS_network2" self.network3 = Mock() self.network3.key = "network3-key" self.network3.name = "QS_network3" self.vm = Mock() self.vm.config.hardware = Mock() self.vnic = Mock(spec=vim.vm.device.VirtualEthernetCard) self.vnic.macAddress = True self.vnic.deviceInfo = Mock() self.vm.config.hardware.device = [self.vnic] self.py_vmomi_service.find_by_uuid = lambda a, b, c: self.vm self.py_vmomi_service.find_network_by_name = Mock( return_value=self.network) self.synchronous_task_waiter = Mock() self.synchronous_task_waiter.wait_for_task = Mock( return_value="TASK OK") self.si = Mock() mapping = {'vnic 1': (Mock(spec=vim.Network), None)} self.vnic_service = Mock() self.vnic_to_network_mapper = Mock() self.vnic_to_network_mapper.map_request_to_vnics = Mock( return_value=mapping) self.vnics = {'vnic 1': self.vnic} self.port_group_name_generator = DvPortGroupNameGenerator() self.vnic_service.map_vnics = Mock(return_value=self.vnics) self.configurer = VirtualMachinePortGroupConfigurer( self.py_vmomi_service, self.synchronous_task_waiter, self.vnic_to_network_mapper, self.vnic_service, self.port_group_name_generator)
def test_(self): vnics = { 'net 1': Mock(spec=vim.vm.device.VirtualEthernetCard), 'net 2': Mock(spec=vim.vm.device.VirtualEthernetCard) } network2 = Mock(spec=vim.Network) network2.name = 'aa' network1 = Mock(spec=vim.Network) network1.name = 'bb' request1 = ConnectRequest('net 2', 'aa') request2 = ConnectRequest(None, 'ab') requests = [request1, request2] mapper = VnicToNetworkMapper(DvPortGroupNameGenerator()) mappig = mapper.map_request_to_vnics(requests, vnics, [], network1, []) self.assertTrue(mappig[request1.vnic_name], request1.network) self.assertTrue(mappig['net 1'], request2.network)
def setUp(self): self.portgroup_name = DvPortGroupNameGenerator() self.connector = Mock() self.disconnector = Mock() self.model_parser = Mock() self.vc_data_model = Mock() self.vc_data_model.reserved_networks = 'restricted_network1,restricted_network2' self.vc_data_model.default_dvswitch = 'dvSwitch' self.vc_data_model.default_network = 'Anetwork' self.vc_data_model.default_datacenter = 'datacenter' self.vc_data_model.holding_network = 'Holding Network' self.si = Mock() self.ConnectionCommandOrchestrator = ConnectionCommandOrchestrator(self.connector, self.disconnector, self.model_parser)
class TestCommandOrchestrator(TestCase): def setUp(self): self.portgroup_name = DvPortGroupNameGenerator() self.connector = Mock() self.disconnector = Mock() self.model_parser = Mock() self.vc_data_model = Mock() self.vc_data_model.reserved_networks = 'restricted_network1,restricted_network2' self.vc_data_model.default_dvswitch = 'dvSwitch' self.vc_data_model.default_network = 'Anetwork' self.vc_data_model.default_datacenter = 'datacenter' self.vc_data_model.holding_network = 'Holding Network' self.vc_data_model.default_port_group_location = '/' self.si = Mock() self.ConnectionCommandOrchestrator = ConnectionCommandOrchestrator(self.connector, self.disconnector, self.model_parser) def test_connect_bulk1(self): """ Simple connect vm to vlan 'Access' mode """ request, expected = self._get_test1_params() results = self.ConnectionCommandOrchestrator.connect_bulk(si=self.si, logger=Mock(), vcenter_data_model=self.vc_data_model, request=request) self._assert_as_expected(results, expected) def test_connect_bulk2(self): """ Simple disconnect """ request, expected = self._get_test2_params() results = self.ConnectionCommandOrchestrator.connect_bulk(si=self.si, logger=Mock(), vcenter_data_model=self.vc_data_model, request=request) self._assert_as_expected(results, expected) def test_connect_bulk3(self): """ Complex connect multiple vms and multiple vlan types """ request, expected = self._get_test3_params() results = self.ConnectionCommandOrchestrator.connect_bulk(si=self.si, logger=Mock(), vcenter_data_model=self.vc_data_model, request=request) self._assert_as_expected(results, expected) def test_connect_bulk4(self): """ Simple disconnect """ request, expected = self._get_test4_params() self.assertRaises(ValueError, self.ConnectionCommandOrchestrator.connect_bulk, self.si, Mock(), self.vc_data_model, request) def test_connect_bulk5(self): """ Disconnect returns error """ request, expected = self._get_test5_params() res = self.ConnectionCommandOrchestrator.connect_bulk(si=self.si, logger=Mock(), vcenter_data_model=self.vc_data_model, request=request) self._assert_as_expected(res, expected) def test_connect_bulk6(self): """ Connect returns error """ request, expected = self._get_test6_params() res = self.ConnectionCommandOrchestrator.connect_bulk(si=self.si, logger=Mock(), vcenter_data_model=self.vc_data_model, request=request) self._assert_as_expected(res, expected) def _assert_as_expected(self, res, exp): for r in res: for e in exp: found = e.actionId == r.actionId if found: for prop in [prop for prop in dir(r) if not prop.startswith('__')]: self.assertEqual(getattr(r, prop), getattr(e, prop)) break def _set_connect_to_networks_by_request(self, request): a = DeployDataHolder(request['driverRequest']) res = [] for action in a.actions: vm_uuid = self.ConnectionCommandOrchestrator._get_vm_uuid(action) vnic_name = self.ConnectionCommandOrchestrator._validate_vnic_name( self.ConnectionCommandOrchestrator._get_vnic_name(action)) vlan_id = action.connectionParams.vlanId mode = action.connectionParams.mode interface = self._get_interface_name(action) r = ConnectionResult(mac_address=interface, vnic_name=vnic_name, requested_vnic=vnic_name, vm_uuid=vm_uuid, network_name=self.portgroup_name.generate_port_group_name(vlan_id, mode), network_key='aa') res.append(r) self.connector.connect_to_networks = Mock(return_value=res) def _set_disconnect_from_networks(self, request): a = DeployDataHolder(request['driverRequest']) res = [] for action in a.actions: vnic_name = self.ConnectionCommandOrchestrator._validate_vnic_name( self.ConnectionCommandOrchestrator._get_vnic_name(action)) interface = self._get_interface_name(action) r = VNicDeviceMapper(vnic=Mock(), requested_vnic=vnic_name, network=Mock(), connect=False, mac=interface) res.append(r) self.disconnector.disconnect_from_networks = Mock(return_value=res) def _get_interface_name(self, action): interface_attributes = [attr.attributeValue for attr in action.connectorAttributes if attr.attributeName == 'Interface'] interface = 'aa' if not interface_attributes else interface_attributes[0] return interface def _get_connect_excepted_results(self, request, error_msg=None): a = DeployDataHolder(request['driverRequest']) res = [] for action in a.actions: r = ActionResult() r.actionId = action.actionId r.type = action.type r.errorMessage = error_msg r.infoMessage = 'VLAN successfully set' if not error_msg else None r.success = True if not error_msg else False r.updatedInterface = self._get_interface_name(action) res.append(r) return res def _get_disconnect_excepted_results(self, request, error_msg=None): a = DeployDataHolder(request['driverRequest']) res = [] for action in a.actions: r = ActionResult() r.actionId = action.actionId r.type = action.type r.errorMessage = error_msg r.infoMessage = 'VLAN Successfully removed' if not error_msg else None r.success = True if not error_msg else False r.updatedInterface = self._get_interface_name(action) res.append(r) return res def _get_test1_params(self): request = { 'driverRequest': { 'actions': [ { "connectorAttributes": [], "connectionParams": { "vlanId": "2", "mode": "Access", "vlanServiceAttributes": [ { "attributeName": "Access Mode", "attributeValue": "Access", "type": "vlanServiceAttribute" } ], "type": "setVlanParameter" }, "actionId": "ee8a3dc8-eb4b-4141-92ad-58bbd8430cad_376046bb-1ef0-4ecc-bb52-c0c7a9c75b1c", "actionTarget": { "fullName": "VM Deployment1_9602ad34", "fullAddress": "N/A/NA", "type": "actionTarget" }, "customActionAttributes": [ { "attributeName": "VM_UUID", "attributeValue": "42220ae6-2fa8-b4cd-14e4-16fbad2798f6", "type": "customAttribute" } ], "type": "setVlan" } ] } } self._set_connect_to_networks_by_request(request) expected = self._get_connect_excepted_results(request) return jsonpickle.encode(request), expected def _get_test2_params(self): request = { "driverRequest": { "actions": [ { "connectionId": "d5369772-66de-4003-85cc-e94a57c20f1e", "connectionParams": { "vlanId": "654", "mode": "Access", "vlanServiceAttributes": [ { "attributeName": "Access Mode", "attributeValue": "Access", "type": "vlanServiceAttribute" }, { "attributeName": "VLAN ID", "attributeValue": "654", "type": "vlanServiceAttribute" }, { "attributeName": "Virtual Network", "attributeValue": "654", "type": "vlanServiceAttribute" } ], "type": "setVlanParameter" }, "connectorAttributes": [ { "attributeName": "Interface", "attributeValue": "00:50:56:a2:5f:07", "type": "connectorAttribute" } ], "actionId": "d5369772-66de-4003-85cc-e94a57c20f1e_30fb5d10-1ce9-4e9f-b41e-ce0d271fd7ab", "actionTarget": { "fullName": "Temp4_a443da02", "fullAddress": "192.168.65.33", "type": "actionTarget" }, "customActionAttributes": [ { "attributeName": "VM_UUID", "attributeValue": "422279bf-5899-789f-ca44-2c62075b3d2d", "type": "customAttribute" } ], "type": "removeVlan" } ] } } self._set_disconnect_from_networks(request) expected = self._get_disconnect_excepted_results(request) return jsonpickle.encode(request), expected def _get_test3_params(self): request = { 'driverRequest': { 'actions': [ { "connectorAttributes": [], "connectionParams": { "vlanId": "2", "mode": "Access", "vlanServiceAttributes": [ { "attributeName": "Access Mode", "attributeValue": "Access", "type": "vlanServiceAttribute" } ], "type": "setVlanParameter" }, "actionId": "1", "actionTarget": { "fullName": "VM Deployment1_9602ad34", "fullAddress": "N/A/NA", "type": "actionTarget" }, "customActionAttributes": [ { "attributeName": "VM_UUID", "attributeValue": "42220ae6-2fa8-b4cd-14e4-16fbad2798f6", "type": "customAttribute" } ], "type": "setVlan" }, { "connectorAttributes": [], "connectionParams": { "vlanId": "2-10", "mode": "Access", "vlanServiceAttributes": [ { "attributeName": "Access Mode", "attributeValue": "Trunk", "type": "vlanServiceAttribute" } ], "type": "setVlanParameter" }, "actionId": "2", "actionTarget": { "fullName": "VM Deployment1_9602ad34", "fullAddress": "N/A/NA", "type": "actionTarget" }, "customActionAttributes": [ { "attributeName": "VM_UUID", "attributeValue": "42220ae6-2fa8-b4cd-14e4-16fbad2798f6", "type": "customAttribute" } ], "type": "setVlan" }, { "connectorAttributes": [], "connectionParams": { "vlanId": "2", "mode": "Access", "vlanServiceAttributes": [ { "attributeName": "Access Mode", "attributeValue": "Access", "type": "vlanServiceAttribute" } ], "type": "setVlanParameter" }, "actionId": "3", "actionTarget": { "fullName": "VM Deployment1_9602ad34", "fullAddress": "N/A/NA", "type": "actionTarget" }, "customActionAttributes": [ { "attributeName": "VM_UUID", "attributeValue": "42220ae6-2fa8-b4cd-14e4-16fbad2798f6", "type": "customAttribute" }, { "attributeName": "Vnic Name", "attributeValue": "1", "type": "customAttribute" } ], "type": "setVlan" }, { "connectorAttributes": [], "connectionParams": { "vlanId": "2", "mode": "Access", "vlanServiceAttributes": [ { "attributeName": "Access Mode", "attributeValue": "Access", "type": "vlanServiceAttribute" } ], "type": "setVlanParameter" }, "actionId": "4", "actionTarget": { "fullName": "VM Deployment1_9602ad34", "fullAddress": "N/A/NA", "type": "actionTarget" }, "customActionAttributes": [ { "attributeName": "VM_UUID", "attributeValue": "1234", "type": "customAttribute" }, { "attributeName": "Vnic Name", "attributeValue": "1", "type": "customAttribute" } ], "type": "setVlan" } ] } } self._set_connect_to_networks_by_request(request) expected = self._get_connect_excepted_results(request) return jsonpickle.encode(request), expected def _get_test4_params(self): request = { "driverRequest": { "actions": [ { "connectionId": "d5369772-66de-4003-85cc-e94a57c20f1e", "connectionParams": { "vlanId": "654", "mode": "Access", "vlanServiceAttributes": [ { "attributeName": "Access Mode", "attributeValue": "Access", "type": "vlanServiceAttribute" }, { "attributeName": "VLAN ID", "attributeValue": "654", "type": "vlanServiceAttribute" }, { "attributeName": "Virtual Network", "attributeValue": "654", "type": "vlanServiceAttribute" } ], "type": "setVlanParameter" }, "connectorAttributes": [ { "attributeName": "Interface", "attributeValue": "00:50:56:a2:5f:07", "type": "connectorAttribute" } ], "actionId": "d5369772-66de-4003-85cc-e94a57c20f1e_30fb5d10-1ce9-4e9f-b41e-ce0d271fd7ab", "actionTarget": { "fullName": "Temp4_a443da02", "fullAddress": "192.168.65.33", "type": "actionTarget" }, "customActionAttributes": [ ], "type": "removeVlan" } ] } } self._set_disconnect_from_networks(request) expected = self._get_disconnect_excepted_results(request, 'VM_UUID is missing on action attributes') return jsonpickle.encode(request), expected def _get_test5_params(self): request = { "driverRequest": { "actions": [ { "connectionId": "d5369772-66de-4003-85cc-e94a57c20f1e", "connectionParams": { "vlanId": "654", "mode": "Access", "vlanServiceAttributes": [ { "attributeName": "Access Mode", "attributeValue": "Access", "type": "vlanServiceAttribute" }, { "attributeName": "VLAN ID", "attributeValue": "654", "type": "vlanServiceAttribute" }, { "attributeName": "Virtual Network", "attributeValue": "654", "type": "vlanServiceAttribute" } ], "type": "setVlanParameter" }, "connectorAttributes": [ { "attributeName": "Interface", "attributeValue": "00:50:56:a2:5f:07", "type": "connectorAttribute" } ], "actionId": "d5369772-66de-4003-85cc-e94a57c20f1e_30fb5d10-1ce9-4e9f-b41e-ce0d271fd7ab", "actionTarget": { "fullName": "Temp4_a443da02", "fullAddress": "192.168.65.33", "type": "actionTarget" }, "customActionAttributes": [ { "attributeName": "VM_UUID", "attributeValue": "42220ae6-2fa8-b4cd-14e4-16fbad2798f6", "type": "customAttribute" } ], "type": "removeVlan" } ] } } self.disconnector.disconnect_from_networks = Mock() self.disconnector.disconnect_from_networks.side_effect = ValueError('vnic not found') expected = self._get_disconnect_excepted_results(request, 'vnic not found') return jsonpickle.encode(request), expected def _get_test6_params(self): request = { 'driverRequest': { 'actions': [ { "connectorAttributes": [], "connectionParams": { "vlanId": "2", "mode": "Access", "vlanServiceAttributes": [ { "attributeName": "Access Mode", "attributeValue": "Access", "type": "vlanServiceAttribute" } ], "type": "setVlanParameter" }, "actionId": "ee8a3dc8-eb4b-4141-92ad-58bbd8430cad_376046bb-1ef0-4ecc-bb52-c0c7a9c75b1c", "actionTarget": { "fullName": "VM Deployment1_9602ad34", "fullAddress": "N/A/NA", "type": "actionTarget" }, "customActionAttributes": [ { "attributeName": "VM_UUID", "attributeValue": "42220ae6-2fa8-b4cd-14e4-16fbad2798f6", "type": "customAttribute" } ], "type": "setVlan" } ] } } self.connector.connect_to_networks = Mock() self.connector.connect_to_networks.side_effect = ValueError('vnic not found') expected = self._get_connect_excepted_results(request, 'vnic not found') # here we don't don't get vnic input input for e in expected: e.updatedInterface = None return jsonpickle.encode(request), expected
def __init__(self): """ Initialize the driver session, this function is called everytime a new instance of the driver is created in here the driver is going to be bootstrapped :param context: models.QualiDriverModels.InitCommandContext """ self.cs_helper = CloudshellDriverHelper() synchronous_task_waiter = SynchronousTaskWaiter() pv_service = pyVmomiService(connect=SmartConnect, disconnect=Disconnect, task_waiter=synchronous_task_waiter) self.resource_model_parser = ResourceModelParser() port_group_name_generator = DvPortGroupNameGenerator() vnic_to_network_mapper = VnicToNetworkMapper( quali_name_generator=port_group_name_generator) resource_remover = CloudshellResourceRemover() ovf_service = OvfImageDeployerService(self.resource_model_parser) self.vm_loader = VMLoader(pv_service) vm_deployer = VirtualMachineDeployer( pv_service=pv_service, name_generator=generate_unique_name, ovf_service=ovf_service, cs_helper=self.cs_helper, resource_model_parser=ResourceModelParser()) dv_port_group_creator = DvPortGroupCreator( pyvmomi_service=pv_service, synchronous_task_waiter=synchronous_task_waiter) virtual_machine_port_group_configurer = \ VirtualMachinePortGroupConfigurer(pyvmomi_service=pv_service, synchronous_task_waiter=synchronous_task_waiter, vnic_to_network_mapper=vnic_to_network_mapper, vnic_service=VNicService(), name_gen=port_group_name_generator) virtual_switch_to_machine_connector = VirtualSwitchToMachineConnector( dv_port_group_creator, virtual_machine_port_group_configurer) # Command Wrapper self.command_wrapper = CommandWrapper( pv_service=pv_service, cloud_shell_helper=self.cs_helper, resource_model_parser=self.resource_model_parser, context_based_logger_factory=ContextBasedLoggerFactory()) # Deploy Command self.deploy_command = DeployCommand(deployer=vm_deployer) # Virtual Switch Revoke self.virtual_switch_disconnect_command = \ VirtualSwitchToMachineDisconnectCommand( pyvmomi_service=pv_service, port_group_configurer=virtual_machine_port_group_configurer, resource_model_parser=self.resource_model_parser) # Virtual Switch Connect virtual_switch_connect_command = \ VirtualSwitchConnectCommand( pv_service=pv_service, virtual_switch_to_machine_connector=virtual_switch_to_machine_connector, dv_port_group_name_generator=DvPortGroupNameGenerator(), vlan_spec_factory=VlanSpecFactory(), vlan_id_range_parser=VLanIdRangeParser()) self.connection_orchestrator = ConnectionCommandOrchestrator( connector=virtual_switch_connect_command, disconnector=self.virtual_switch_disconnect_command, resource_model_parser=self.resource_model_parser) # Destroy VM Command self.destroy_virtual_machine_command = \ DestroyVirtualMachineCommand(pv_service=pv_service, resource_remover=resource_remover, disconnector=self.virtual_switch_disconnect_command) # Power Command self.vm_power_management_command = \ VirtualMachinePowerManagementCommand(pyvmomi_service=pv_service, synchronous_task_waiter=synchronous_task_waiter) ip_manager = VMIPManager() # Refresh IP command self.refresh_ip_command = RefreshIpCommand( pyvmomi_service=pv_service, resource_model_parser=ResourceModelParser(), ip_manager=ip_manager) # Save Snapshot self.snapshot_saver = SaveSnapshotCommand( pyvmomi_service=pv_service, task_waiter=synchronous_task_waiter) # Snapshot Restorer self.snapshot_restorer = SnapshotRestoreCommand( pyvmomi_service=pv_service, task_waiter=synchronous_task_waiter) self.snapshots_retriever = RetrieveSnapshotsCommand( pyvmomi_service=pv_service)
class TestCommandOrchestrator(TestCase): def setUp(self): self.portgroup_name = DvPortGroupNameGenerator() self.connector = Mock() self.disconnector = Mock() self.model_parser = Mock() self.vc_data_model = Mock() self.vc_data_model.reserved_networks = 'restricted_network1,restricted_network2' self.vc_data_model.default_dvswitch = 'dvSwitch' self.vc_data_model.default_network = 'Anetwork' self.vc_data_model.default_datacenter = 'datacenter' self.vc_data_model.holding_network = 'Holding Network' self.si = Mock() self.ConnectionCommandOrchestrator = ConnectionCommandOrchestrator( self.connector, self.disconnector, self.model_parser) def test_connect_bulk_dvswitch_is_None(self): """ tests the error by missing dvswitch """ vc_data_model = Mock() vc_data_model.reserved_networks = 'restricted_network1,restricted_network2' vc_data_model.default_dvswitch = None vc_data_model.default_network = 'Anetwork' vc_data_model.default_datacenter = 'datacenter' vc_data_model.holding_network = 'Holding Network' request, expected = self._get_missing_dv_switch_params() results = self.ConnectionCommandOrchestrator.connect_bulk( si=self.si, logger=Mock(), vcenter_data_model=vc_data_model, request=request) self._assert_as_expected(results, expected) def test_connect_bulk1(self): """ Simple connect vm to vlan 'Access' mode """ request, expected = self._get_test1_params() results = self.ConnectionCommandOrchestrator.connect_bulk( si=self.si, logger=Mock(), vcenter_data_model=self.vc_data_model, request=request) self._assert_as_expected(results, expected) def test_connect_bulk2(self): """ Simple disconnect """ request, expected = self._get_test2_params() results = self.ConnectionCommandOrchestrator.connect_bulk( si=self.si, logger=Mock(), vcenter_data_model=self.vc_data_model, request=request) self._assert_as_expected(results, expected) def test_connect_bulk3(self): """ Complex connect multiple vms and multiple vlan types """ request, expected = self._get_test3_params() results = self.ConnectionCommandOrchestrator.connect_bulk( si=self.si, logger=Mock(), vcenter_data_model=self.vc_data_model, request=request) self._assert_as_expected(results, expected) def test_connect_bulk4(self): """ Simple disconnect """ request, expected = self._get_test4_params() self.assertRaises(ValueError, self.ConnectionCommandOrchestrator.connect_bulk, self.si, Mock(), self.vc_data_model, request) def test_connect_bulk5(self): """ Disconnect returns error """ request, expected = self._get_test5_params() res = self.ConnectionCommandOrchestrator.connect_bulk( si=self.si, logger=Mock(), vcenter_data_model=self.vc_data_model, request=request) self._assert_as_expected(res, expected) def test_connect_bulk6(self): """ Connect returns error """ request, expected = self._get_test6_params() res = self.ConnectionCommandOrchestrator.connect_bulk( si=self.si, logger=Mock(), vcenter_data_model=self.vc_data_model, request=request) self._assert_as_expected(res, expected) def test_connect_bulk7(self): """ Simple connect vm to vlan 'Access' mode """ request, expected = self._get_test7_params() results = self.ConnectionCommandOrchestrator.connect_bulk( si=self.si, logger=Mock(), vcenter_data_model=self.vc_data_model, request=request) self._assert_as_expected(results, expected) def test_connect_bulk8(self): """ Simple connect vm to vlan 'Access' mode """ request, expected = self._get_test8_params() results = self.ConnectionCommandOrchestrator.connect_bulk( si=self.si, logger=Mock(), vcenter_data_model=self.vc_data_model, request=request) self._assert_as_expected(results, expected) def test_connect_bulk9(self): """ Simple disconnect """ request, expected = self._get_test9_params() results = self.ConnectionCommandOrchestrator.connect_bulk( si=self.si, logger=Mock(), vcenter_data_model=self.vc_data_model, request=request) self._assert_as_expected(results, expected) def _assert_as_expected(self, res, exp): for r in res: for e in exp: found = e.actionId == r.actionId if found: for prop in [ prop for prop in dir(r) if not prop.startswith('__') ]: self.assertEqual(getattr(r, prop), getattr(e, prop)) break def _set_connect_to_networks_by_request(self, request): a = DeployDataHolder(request['driverRequest']) res = [] for action in a.actions: vm_uuid = self.ConnectionCommandOrchestrator._get_vm_uuid(action) vnic_name = self.ConnectionCommandOrchestrator._validate_vnic_name( self.ConnectionCommandOrchestrator._get_vnic_name(action)) if vnic_name: vnic_name = vnic_name.strip().split(',') else: vnic_name = [None] for vnic in vnic_name: vlan_id = action.connectionParams.vlanId mode = action.connectionParams.mode interface = self._get_interface_name(action) name = 'Network adapter {0}'.format( vnic) if vnic and vnic.isdigit() else vnic r = ConnectionResult( mac_address=interface, vnic_name=name, requested_vnic=name, vm_uuid=vm_uuid, network_name=self.portgroup_name.generate_port_group_name( 'dvSwitch', vlan_id, mode), network_key='aa') res.append(r) self.connector.connect_to_networks = Mock(return_value=res) def _set_disconnect_from_networks(self, request): a = DeployDataHolder(request['driverRequest']) res = [] for action in a.actions: vnic_name = self.ConnectionCommandOrchestrator._validate_vnic_name( self.ConnectionCommandOrchestrator._get_vnic_name(action)) interface = self._get_interface_name(action) r = VNicDeviceMapper(vnic=Mock(), requested_vnic=vnic_name, network=Mock(), connect=False, mac=interface) res.append(r) self.disconnector.disconnect_from_networks = Mock(return_value=res) def _get_interface_name_for_result(self, action): interface_attributes = [ attr.attributeValue for attr in action.connectorAttributes if attr.attributeName == 'Interface' ] vnics = [ attr.attributeValue for attr in action.connectorAttributes if attr.attributeName == 'Requested Source vNIC Name' ] if vnics: vnics = vnics[0].strip().split(',') interface = 'aa' for vnic in vnics[1:]: interface = '{0},{1}'.format( interface, 'aa' if not interface_attributes else interface_attributes[0]) return interface def _get_interface_name(self, action): interface_attributes = [ attr.attributeValue for attr in action.connectorAttributes if attr.attributeName == 'Interface' ] interface = 'aa' if not interface_attributes else interface_attributes[ 0] return interface def _get_connect_excepted_results(self, request, error_msg=None, ignore_interface=False): a = DeployDataHolder(request['driverRequest']) res = [] for action in a.actions: r = ActionResult() r.actionId = action.actionId r.type = action.type r.errorMessage = error_msg r.infoMessage = 'VLAN successfully set' if not error_msg else None r.success = True if not error_msg else False r.updatedInterface = None if ignore_interface else self._get_interface_name_for_result( action) res.append(r) return res def _get_disconnect_excepted_results(self, request, error_msg=None): a = DeployDataHolder(request['driverRequest']) res = [] for action in a.actions: r = ActionResult() r.actionId = action.actionId r.type = action.type r.errorMessage = error_msg r.infoMessage = 'VLAN Successfully removed' if not error_msg else None r.success = True if not error_msg else False r.updatedInterface = self._get_interface_name(action) res.append(r) return res def _get_missing_dv_switch_params(self): request = { 'driverRequest': { 'actions': [{ "connectorAttributes": [], "connectionParams": { "vlanId": "2", "mode": "Access", "vlanServiceAttributes": [{ "attributeName": "Access Mode", "attributeValue": "Access", "type": "vlanServiceAttribute" }], "type": "setVlanParameter" }, "actionId": "ee8a3dc8-eb4b-4141-92ad-58bbd8430cad_376046bb-1ef0-4ecc-bb52-c0c7a9c75b1c", "actionTarget": { "fullName": "VM Deployment1_9602ad34", "fullAddress": "N/A/NA", "type": "actionTarget" }, "customActionAttributes": [{ "attributeName": "VM_UUID", "attributeValue": "42220ae6-2fa8-b4cd-14e4-16fbad2798f6", "type": "customAttribute" }], "type": "setVlan" }] } } self._set_connect_to_networks_by_request(request) expected = self._get_connect_excepted_results( request, error_msg= 'Please set the attribute "Default DvSwitch" in order to execute any connectivity changes', ignore_interface=True) return jsonpickle.encode(request), expected def _get_test1_params(self): request = { 'driverRequest': { 'actions': [{ "connectorAttributes": [], "connectionParams": { "vlanId": "2", "mode": "Access", "vlanServiceAttributes": [{ "attributeName": "Access Mode", "attributeValue": "Access", "type": "vlanServiceAttribute" }], "type": "setVlanParameter" }, "actionId": "ee8a3dc8-eb4b-4141-92ad-58bbd8430cad_376046bb-1ef0-4ecc-bb52-c0c7a9c75b1c", "actionTarget": { "fullName": "VM Deployment1_9602ad34", "fullAddress": "N/A/NA", "type": "actionTarget" }, "customActionAttributes": [{ "attributeName": "VM_UUID", "attributeValue": "42220ae6-2fa8-b4cd-14e4-16fbad2798f6", "type": "customAttribute" }], "type": "setVlan" }] } } self._set_connect_to_networks_by_request(request) expected = self._get_connect_excepted_results(request) return jsonpickle.encode(request), expected def _get_test2_params(self): request = { "driverRequest": { "actions": [{ "connectionId": "d5369772-66de-4003-85cc-e94a57c20f1e", "connectionParams": { "vlanId": "654", "mode": "Access", "vlanServiceAttributes": [{ "attributeName": "Access Mode", "attributeValue": "Access", "type": "vlanServiceAttribute" }, { "attributeName": "VLAN ID", "attributeValue": "654", "type": "vlanServiceAttribute" }, { "attributeName": "Virtual Network", "attributeValue": "654", "type": "vlanServiceAttribute" }], "type": "setVlanParameter" }, "connectorAttributes": [{ "attributeName": "Interface", "attributeValue": "00:50:56:a2:5f:07", "type": "connectorAttribute" }], "actionId": "d5369772-66de-4003-85cc-e94a57c20f1e_30fb5d10-1ce9-4e9f-b41e-ce0d271fd7ab", "actionTarget": { "fullName": "Temp4_a443da02", "fullAddress": "192.168.65.33", "type": "actionTarget" }, "customActionAttributes": [{ "attributeName": "VM_UUID", "attributeValue": "422279bf-5899-789f-ca44-2c62075b3d2d", "type": "customAttribute" }], "type": "removeVlan" }] } } self._set_disconnect_from_networks(request) expected = self._get_disconnect_excepted_results(request) return jsonpickle.encode(request), expected def _get_test3_params(self): request = { 'driverRequest': { 'actions': [{ "connectorAttributes": [], "connectionParams": { "vlanId": "2", "mode": "Access", "vlanServiceAttributes": [{ "attributeName": "Access Mode", "attributeValue": "Access", "type": "vlanServiceAttribute" }], "type": "setVlanParameter" }, "actionId": "1", "actionTarget": { "fullName": "VM Deployment1_9602ad34", "fullAddress": "N/A/NA", "type": "actionTarget" }, "customActionAttributes": [{ "attributeName": "VM_UUID", "attributeValue": "42220ae6-2fa8-b4cd-14e4-16fbad2798f6", "type": "customAttribute" }], "type": "setVlan" }, { "connectorAttributes": [], "connectionParams": { "vlanId": "2-10", "mode": "Access", "vlanServiceAttributes": [{ "attributeName": "Access Mode", "attributeValue": "Trunk", "type": "vlanServiceAttribute" }], "type": "setVlanParameter" }, "actionId": "2", "actionTarget": { "fullName": "VM Deployment1_9602ad34", "fullAddress": "N/A/NA", "type": "actionTarget" }, "customActionAttributes": [{ "attributeName": "VM_UUID", "attributeValue": "42220ae6-2fa8-b4cd-14e4-16fbad2798f6", "type": "customAttribute" }], "type": "setVlan" }, { "connectorAttributes": [], "connectionParams": { "vlanId": "2", "mode": "Access", "vlanServiceAttributes": [{ "attributeName": "Access Mode", "attributeValue": "Access", "type": "vlanServiceAttribute" }], "type": "setVlanParameter" }, "actionId": "3", "actionTarget": { "fullName": "VM Deployment1_9602ad34", "fullAddress": "N/A/NA", "type": "actionTarget" }, "customActionAttributes": [{ "attributeName": "VM_UUID", "attributeValue": "42220ae6-2fa8-b4cd-14e4-16fbad2798f6", "type": "customAttribute" }, { "attributeName": "Vnic Name", "attributeValue": "1", "type": "customAttribute" }], "type": "setVlan" }, { "connectorAttributes": [], "connectionParams": { "vlanId": "2", "mode": "Access", "vlanServiceAttributes": [{ "attributeName": "Access Mode", "attributeValue": "Access", "type": "vlanServiceAttribute" }], "type": "setVlanParameter" }, "actionId": "4", "actionTarget": { "fullName": "VM Deployment1_9602ad34", "fullAddress": "N/A/NA", "type": "actionTarget" }, "customActionAttributes": [{ "attributeName": "VM_UUID", "attributeValue": "1234", "type": "customAttribute" }, { "attributeName": "Vnic Name", "attributeValue": "1", "type": "customAttribute" }], "type": "setVlan" }] } } self._set_connect_to_networks_by_request(request) expected = self._get_connect_excepted_results(request) return jsonpickle.encode(request), expected def _get_test4_params(self): request = { "driverRequest": { "actions": [{ "connectionId": "d5369772-66de-4003-85cc-e94a57c20f1e", "connectionParams": { "vlanId": "654", "mode": "Access", "vlanServiceAttributes": [{ "attributeName": "Access Mode", "attributeValue": "Access", "type": "vlanServiceAttribute" }, { "attributeName": "VLAN ID", "attributeValue": "654", "type": "vlanServiceAttribute" }, { "attributeName": "Virtual Network", "attributeValue": "654", "type": "vlanServiceAttribute" }], "type": "setVlanParameter" }, "connectorAttributes": [{ "attributeName": "Interface", "attributeValue": "00:50:56:a2:5f:07", "type": "connectorAttribute" }], "actionId": "d5369772-66de-4003-85cc-e94a57c20f1e_30fb5d10-1ce9-4e9f-b41e-ce0d271fd7ab", "actionTarget": { "fullName": "Temp4_a443da02", "fullAddress": "192.168.65.33", "type": "actionTarget" }, "customActionAttributes": [], "type": "removeVlan" }] } } self._set_disconnect_from_networks(request) expected = self._get_disconnect_excepted_results( request, 'VM_UUID is missing on action attributes') return jsonpickle.encode(request), expected def _get_test5_params(self): request = { "driverRequest": { "actions": [{ "connectionId": "d5369772-66de-4003-85cc-e94a57c20f1e", "connectionParams": { "vlanId": "654", "mode": "Access", "vlanServiceAttributes": [{ "attributeName": "Access Mode", "attributeValue": "Access", "type": "vlanServiceAttribute" }, { "attributeName": "VLAN ID", "attributeValue": "654", "type": "vlanServiceAttribute" }, { "attributeName": "Virtual Network", "attributeValue": "654", "type": "vlanServiceAttribute" }], "type": "setVlanParameter" }, "connectorAttributes": [{ "attributeName": "Interface", "attributeValue": "00:50:56:a2:5f:07", "type": "connectorAttribute" }], "actionId": "d5369772-66de-4003-85cc-e94a57c20f1e_30fb5d10-1ce9-4e9f-b41e-ce0d271fd7ab", "actionTarget": { "fullName": "Temp4_a443da02", "fullAddress": "192.168.65.33", "type": "actionTarget" }, "customActionAttributes": [{ "attributeName": "VM_UUID", "attributeValue": "42220ae6-2fa8-b4cd-14e4-16fbad2798f6", "type": "customAttribute" }], "type": "removeVlan" }] } } self.disconnector.disconnect_from_networks = Mock() self.disconnector.disconnect_from_networks.side_effect = ValueError( 'vnic not found') expected = self._get_disconnect_excepted_results( request, 'vnic not found') return jsonpickle.encode(request), expected def _get_test6_params(self): request = { 'driverRequest': { 'actions': [{ "connectorAttributes": [], "connectionParams": { "vlanId": "2", "mode": "Access", "vlanServiceAttributes": [{ "attributeName": "Access Mode", "attributeValue": "Access", "type": "vlanServiceAttribute" }], "type": "setVlanParameter" }, "actionId": "ee8a3dc8-eb4b-4141-92ad-58bbd8430cad_376046bb-1ef0-4ecc-bb52-c0c7a9c75b1c", "actionTarget": { "fullName": "VM Deployment1_9602ad34", "fullAddress": "N/A/NA", "type": "actionTarget" }, "customActionAttributes": [{ "attributeName": "VM_UUID", "attributeValue": "42220ae6-2fa8-b4cd-14e4-16fbad2798f6", "type": "customAttribute" }], "type": "setVlan" }] } } self.connector.connect_to_networks = Mock() self.connector.connect_to_networks.side_effect = ValueError( 'vnic not found') expected = self._get_connect_excepted_results(request, 'vnic not found') # here we don't don't get vnic input input for e in expected: e.updatedInterface = None return jsonpickle.encode(request), expected def _get_test7_params(self): request = { "driverRequest": { "actions": [{ "connectionId": "6bd00213-6cfa-435a-abf5-6c839dae0f44", "connectionParams": { "vlanId": "2", "mode": "Access", "vlanServiceAttributes": [{ "attributeName": "QnQ", "attributeValue": "False", "type": "vlanServiceAttribute" }, { "attributeName": "CTag", "attributeValue": "", "type": "vlanServiceAttribute" }, { "attributeName": "Allocation Ranges", "attributeValue": "2-4094", "type": "vlanServiceAttribute" }, { "attributeName": "Isolation Level", "attributeValue": "Exclusive", "type": "vlanServiceAttribute" }, { "attributeName": "Access Mode", "attributeValue": "Access", "type": "vlanServiceAttribute" }, { "attributeName": "VLAN ID", "attributeValue": "", "type": "vlanServiceAttribute" }, { "attributeName": "Pool Name", "attributeValue": "", "type": "vlanServiceAttribute" }, { "attributeName": "Virtual Network", "attributeValue": "2", "type": "vlanServiceAttribute" }], "type": "setVlanParameter" }, "connectorAttributes": [{ "attributeName": "Requested Source vNIC Name", "attributeValue": "1,2", "type": "connectorAttribute" }], "actionId": "6bd00213-6cfa-435a-abf5-6c839dae0f44_c6b69177-d251-4dba-ba88-d7c5bb259505", "actionTarget": { "fullName": "temp_b648f80f", "fullAddress": "NA", "type": "actionTarget" }, "customActionAttributes": [{ "attributeName": "auto_power_off", "attributeValue": "auto_power_off", "type": "customAttribute" }, { "attributeName": "ip_regex", "attributeValue": "ip_regex", "type": "customAttribute" }, { "attributeName": "wait_for_ip", "attributeValue": "wait_for_ip", "type": "customAttribute" }, { "attributeName": "autoload", "attributeValue": "autoload", "type": "customAttribute" }, { "attributeName": "auto_delete", "attributeValue": "auto_delete", "type": "customAttribute" }, { "attributeName": "auto_power_on", "attributeValue": "auto_power_on", "type": "customAttribute" }, { "attributeName": "refresh_ip_timeout", "attributeValue": "refresh_ip_timeout", "type": "customAttribute" }, { "attributeName": "VM_UUID", "attributeValue": "422203f6-eadd-9f88-5dc8-00c17f49fa21", "type": "customAttribute" }, { "attributeName": "Vnic Name", "attributeValue": "1,2", "type": "customAttribute" }], "type": "setVlan" }] } } self._set_connect_to_networks_by_request(request) expected = self._get_connect_excepted_results(request) return jsonpickle.encode(request), expected def _get_test8_params(self): request = { "driverRequest": { "actions": [{ "connectionId": "6bd00213-6cfa-435a-abf5-6c839dae0f44", "connectionParams": { "vlanId": "2", "mode": "Access", "vlanServiceAttributes": [{ "attributeName": "QnQ", "attributeValue": "False", "type": "vlanServiceAttribute" }, { "attributeName": "CTag", "attributeValue": "", "type": "vlanServiceAttribute" }, { "attributeName": "Allocation Ranges", "attributeValue": "2-4094", "type": "vlanServiceAttribute" }, { "attributeName": "Isolation Level", "attributeValue": "Exclusive", "type": "vlanServiceAttribute" }, { "attributeName": "Access Mode", "attributeValue": "Access", "type": "vlanServiceAttribute" }, { "attributeName": "VLAN ID", "attributeValue": "", "type": "vlanServiceAttribute" }, { "attributeName": "Pool Name", "attributeValue": "", "type": "vlanServiceAttribute" }, { "attributeName": "Virtual Network", "attributeValue": "2", "type": "vlanServiceAttribute" }], "type": "setVlanParameter" }, "connectorAttributes": [{ "attributeName": "Requested Source vNIC Name", "attributeValue": "1,2", "type": "connectorAttribute" }], "actionId": "6bd00213-6cfa-435a-abf5-6c839dae0f44_c6b69177-d251-4dba-ba88-d7c5bb259505", "actionTarget": { "fullName": "temp_b648f80f", "fullAddress": "NA", "type": "actionTarget" }, "customActionAttributes": [{ "attributeName": "auto_power_off", "attributeValue": "auto_power_off", "type": "customAttribute" }, { "attributeName": "ip_regex", "attributeValue": "ip_regex", "type": "customAttribute" }, { "attributeName": "wait_for_ip", "attributeValue": "wait_for_ip", "type": "customAttribute" }, { "attributeName": "autoload", "attributeValue": "autoload", "type": "customAttribute" }, { "attributeName": "auto_delete", "attributeValue": "auto_delete", "type": "customAttribute" }, { "attributeName": "auto_power_on", "attributeValue": "auto_power_on", "type": "customAttribute" }, { "attributeName": "refresh_ip_timeout", "attributeValue": "refresh_ip_timeout", "type": "customAttribute" }, { "attributeName": "VM_UUID", "attributeValue": "422203f6-eadd-9f88-5dc8-00c17f49fa21", "type": "customAttribute" }, { "attributeName": "Vnic Name", "attributeValue": "Network Adapter 1,Network Adapter 2", "type": "customAttribute" }], "type": "setVlan" }] } } self._set_connect_to_networks_by_request(request) expected = self._get_connect_excepted_results(request) return jsonpickle.encode(request), expected def _get_test9_params(self): request = { "driverRequest": { "actions": [{ "connectionId": "4dd285fa-d271-4023-b7d1-881553a8f59f", "connectionParams": { "vlanId": "2", "mode": "Access", "vlanServiceAttributes": [{ "attributeName": "QnQ", "attributeValue": "False", "type": "vlanServiceAttribute" }, { "attributeName": "CTag", "attributeValue": "", "type": "vlanServiceAttribute" }, { "attributeName": "Allocation Ranges", "attributeValue": "2-4094", "type": "vlanServiceAttribute" }, { "attributeName": "Isolation Level", "attributeValue": "Exclusive", "type": "vlanServiceAttribute" }, { "attributeName": "Access Mode", "attributeValue": "Access", "type": "vlanServiceAttribute" }, { "attributeName": "VLAN ID", "attributeValue": "", "type": "vlanServiceAttribute" }, { "attributeName": "Pool Name", "attributeValue": "", "type": "vlanServiceAttribute" }, { "attributeName": "Virtual Network", "attributeValue": "2", "type": "vlanServiceAttribute" }], "type": "setVlanParameter" }, "connectorAttributes": [{ "attributeName": "Interface", "attributeValue": "00:50:56:a2:37:3b", "type": "connectorAttribute" }], "actionId": "4dd285fa-d271-4023-b7d1-881553a8f59f_c6b69177-d251-4dba-ba88-d7c5bb259505", "actionTarget": { "fullName": "temp_b648f80f", "fullAddress": "NA", "type": "actionTarget" }, "customActionAttributes": [{ "attributeName": "auto_power_off", "attributeValue": "auto_power_off", "type": "customAttribute" }, { "attributeName": "ip_regex", "attributeValue": "ip_regex", "type": "customAttribute" }, { "attributeName": "wait_for_ip", "attributeValue": "wait_for_ip", "type": "customAttribute" }, { "attributeName": "autoload", "attributeValue": "autoload", "type": "customAttribute" }, { "attributeName": "auto_delete", "attributeValue": "auto_delete", "type": "customAttribute" }, { "attributeName": "auto_power_on", "attributeValue": "auto_power_on", "type": "customAttribute" }, { "attributeName": "refresh_ip_timeout", "attributeValue": "refresh_ip_timeout", "type": "customAttribute" }, { "attributeName": "VM_UUID", "attributeValue": "422203f6-eadd-9f88-5dc8-00c17f49fa21", "type": "customAttribute" }], "type": "removeVlan" }] } } self._set_disconnect_from_networks(request) expected = self._get_disconnect_excepted_results(request) return jsonpickle.encode(request), expected
class TestCommandOrchestrator(TestCase): def setUp(self): self.portgroup_name = DvPortGroupNameGenerator() self.connector = Mock() self.disconnector = Mock() self.model_parser = Mock() self.vc_data_model = Mock() self.vc_data_model.reserved_networks = 'restricted_network1,restricted_network2' self.vc_data_model.default_dvswitch = 'dvSwitch' self.vc_data_model.default_network = 'Anetwork' self.vc_data_model.default_datacenter = 'datacenter' self.vc_data_model.holding_network = 'Holding Network' self.si = Mock() self.ConnectionCommandOrchestrator = ConnectionCommandOrchestrator(self.connector, self.disconnector, self.model_parser) def test_connect_bulk_dvswitch_is_None(self): """ tests the error by missing dvswitch """ vc_data_model = Mock() vc_data_model.reserved_networks = 'restricted_network1,restricted_network2' vc_data_model.default_dvswitch = None vc_data_model.default_network = 'Anetwork' vc_data_model.default_datacenter = 'datacenter' vc_data_model.holding_network = 'Holding Network' request, expected = self._get_missing_dv_switch_params() results = self.ConnectionCommandOrchestrator.connect_bulk(si=self.si, logger=Mock(), vcenter_data_model=vc_data_model, request=request) self._assert_as_expected(results, expected) def test_connect_bulk1(self): """ Simple connect vm to vlan 'Access' mode """ request, expected = self._get_test1_params() results = self.ConnectionCommandOrchestrator.connect_bulk(si=self.si, logger=Mock(), vcenter_data_model=self.vc_data_model, request=request) self._assert_as_expected(results, expected) def test_connect_bulk2(self): """ Simple disconnect """ request, expected = self._get_test2_params() results = self.ConnectionCommandOrchestrator.connect_bulk(si=self.si, logger=Mock(), vcenter_data_model=self.vc_data_model, request=request) self._assert_as_expected(results, expected) def test_connect_bulk3(self): """ Complex connect multiple vms and multiple vlan types """ request, expected = self._get_test3_params() results = self.ConnectionCommandOrchestrator.connect_bulk(si=self.si, logger=Mock(), vcenter_data_model=self.vc_data_model, request=request) self._assert_as_expected(results, expected) def test_connect_bulk4(self): """ Simple disconnect """ request, expected = self._get_test4_params() self.assertRaises(ValueError, self.ConnectionCommandOrchestrator.connect_bulk, self.si, Mock(), self.vc_data_model, request) def test_connect_bulk5(self): """ Disconnect returns error """ request, expected = self._get_test5_params() res = self.ConnectionCommandOrchestrator.connect_bulk(si=self.si, logger=Mock(), vcenter_data_model=self.vc_data_model, request=request) self._assert_as_expected(res, expected) def test_connect_bulk6(self): """ Connect returns error """ request, expected = self._get_test6_params() res = self.ConnectionCommandOrchestrator.connect_bulk(si=self.si, logger=Mock(), vcenter_data_model=self.vc_data_model, request=request) self._assert_as_expected(res, expected) def test_connect_bulk7(self): """ Simple connect vm to vlan 'Access' mode """ request, expected = self._get_test7_params() results = self.ConnectionCommandOrchestrator.connect_bulk(si=self.si, logger=Mock(), vcenter_data_model=self.vc_data_model, request=request) self._assert_as_expected(results, expected) def test_connect_bulk8(self): """ Simple connect vm to vlan 'Access' mode """ request, expected = self._get_test8_params() results = self.ConnectionCommandOrchestrator.connect_bulk(si=self.si, logger=Mock(), vcenter_data_model=self.vc_data_model, request=request) self._assert_as_expected(results, expected) def test_connect_bulk9(self): """ Simple disconnect """ request, expected = self._get_test9_params() results = self.ConnectionCommandOrchestrator.connect_bulk(si=self.si, logger=Mock(), vcenter_data_model=self.vc_data_model, request=request) self._assert_as_expected(results, expected) def _assert_as_expected(self, res, exp): for r in res: for e in exp: found = e.actionId == r.actionId if found: for prop in [prop for prop in dir(r) if not prop.startswith('__')]: self.assertEqual(getattr(r, prop), getattr(e, prop)) break def _set_connect_to_networks_by_request(self, request): a = DeployDataHolder(request['driverRequest']) res = [] for action in a.actions: vm_uuid = self.ConnectionCommandOrchestrator._get_vm_uuid(action) vnic_name = self.ConnectionCommandOrchestrator._validate_vnic_name( self.ConnectionCommandOrchestrator._get_vnic_name(action)) if vnic_name: vnic_name = vnic_name.strip().split(',') else: vnic_name = [None] for vnic in vnic_name: vlan_id = action.connectionParams.vlanId mode = action.connectionParams.mode interface = self._get_interface_name(action) name = 'Network adapter {0}'.format(vnic) if vnic and vnic.isdigit() else vnic r = ConnectionResult(mac_address=interface, vnic_name=name, requested_vnic=name, vm_uuid=vm_uuid, network_name=self.portgroup_name.generate_port_group_name('dvSwitch', vlan_id, mode), network_key='aa') res.append(r) self.connector.connect_to_networks = Mock(return_value=res) def _set_disconnect_from_networks(self, request): a = DeployDataHolder(request['driverRequest']) res = [] for action in a.actions: vnic_name = self.ConnectionCommandOrchestrator._validate_vnic_name( self.ConnectionCommandOrchestrator._get_vnic_name(action)) interface = self._get_interface_name(action) r = VNicDeviceMapper(vnic=Mock(), requested_vnic=vnic_name, network=Mock(), connect=False, mac=interface) res.append(r) self.disconnector.disconnect_from_networks = Mock(return_value=res) def _get_interface_name_for_result(self, action): interface_attributes = [attr.attributeValue for attr in action.connectorAttributes if attr.attributeName == 'Interface'] vnics = [attr.attributeValue for attr in action.connectorAttributes if attr.attributeName == 'Requested Source vNIC Name'] if vnics: vnics = vnics[0].strip().split(',') interface = 'aa' for vnic in vnics[1:]: interface = '{0},{1}'.format(interface, 'aa' if not interface_attributes else interface_attributes[0]) return interface def _get_interface_name(self, action): interface_attributes = [attr.attributeValue for attr in action.connectorAttributes if attr.attributeName == 'Interface'] interface = 'aa' if not interface_attributes else interface_attributes[0] return interface def _get_connect_excepted_results(self, request, error_msg=None, ignore_interface=False): a = DeployDataHolder(request['driverRequest']) res = [] for action in a.actions: r = ActionResult() r.actionId = action.actionId r.type = action.type r.errorMessage = error_msg r.infoMessage = 'VLAN successfully set' if not error_msg else None r.success = True if not error_msg else False r.updatedInterface = None if ignore_interface else self._get_interface_name_for_result(action) res.append(r) return res def _get_disconnect_excepted_results(self, request, error_msg=None): a = DeployDataHolder(request['driverRequest']) res = [] for action in a.actions: r = ActionResult() r.actionId = action.actionId r.type = action.type r.errorMessage = error_msg r.infoMessage = 'VLAN Successfully removed' if not error_msg else None r.success = True if not error_msg else False r.updatedInterface = self._get_interface_name(action) res.append(r) return res def _get_missing_dv_switch_params(self): request = { 'driverRequest': { 'actions': [ { "connectorAttributes": [], "connectionParams": { "vlanId": "2", "mode": "Access", "vlanServiceAttributes": [ { "attributeName": "Access Mode", "attributeValue": "Access", "type": "vlanServiceAttribute" } ], "type": "setVlanParameter" }, "actionId": "ee8a3dc8-eb4b-4141-92ad-58bbd8430cad_376046bb-1ef0-4ecc-bb52-c0c7a9c75b1c", "actionTarget": { "fullName": "VM Deployment1_9602ad34", "fullAddress": "N/A/NA", "type": "actionTarget" }, "customActionAttributes": [ { "attributeName": "VM_UUID", "attributeValue": "42220ae6-2fa8-b4cd-14e4-16fbad2798f6", "type": "customAttribute" } ], "type": "setVlan" } ] } } self._set_connect_to_networks_by_request(request) expected = self._get_connect_excepted_results(request, error_msg='Please set the attribute "Default DvSwitch" in order to execute any connectivity changes', ignore_interface=True) return jsonpickle.encode(request), expected def _get_test1_params(self): request = { 'driverRequest': { 'actions': [ { "connectorAttributes": [], "connectionParams": { "vlanId": "2", "mode": "Access", "vlanServiceAttributes": [ { "attributeName": "Access Mode", "attributeValue": "Access", "type": "vlanServiceAttribute" } ], "type": "setVlanParameter" }, "actionId": "ee8a3dc8-eb4b-4141-92ad-58bbd8430cad_376046bb-1ef0-4ecc-bb52-c0c7a9c75b1c", "actionTarget": { "fullName": "VM Deployment1_9602ad34", "fullAddress": "N/A/NA", "type": "actionTarget" }, "customActionAttributes": [ { "attributeName": "VM_UUID", "attributeValue": "42220ae6-2fa8-b4cd-14e4-16fbad2798f6", "type": "customAttribute" } ], "type": "setVlan" } ] } } self._set_connect_to_networks_by_request(request) expected = self._get_connect_excepted_results(request) return jsonpickle.encode(request), expected def _get_test2_params(self): request = { "driverRequest": { "actions": [ { "connectionId": "d5369772-66de-4003-85cc-e94a57c20f1e", "connectionParams": { "vlanId": "654", "mode": "Access", "vlanServiceAttributes": [ { "attributeName": "Access Mode", "attributeValue": "Access", "type": "vlanServiceAttribute" }, { "attributeName": "VLAN ID", "attributeValue": "654", "type": "vlanServiceAttribute" }, { "attributeName": "Virtual Network", "attributeValue": "654", "type": "vlanServiceAttribute" } ], "type": "setVlanParameter" }, "connectorAttributes": [ { "attributeName": "Interface", "attributeValue": "00:50:56:a2:5f:07", "type": "connectorAttribute" } ], "actionId": "d5369772-66de-4003-85cc-e94a57c20f1e_30fb5d10-1ce9-4e9f-b41e-ce0d271fd7ab", "actionTarget": { "fullName": "Temp4_a443da02", "fullAddress": "192.168.65.33", "type": "actionTarget" }, "customActionAttributes": [ { "attributeName": "VM_UUID", "attributeValue": "422279bf-5899-789f-ca44-2c62075b3d2d", "type": "customAttribute" } ], "type": "removeVlan" } ] } } self._set_disconnect_from_networks(request) expected = self._get_disconnect_excepted_results(request) return jsonpickle.encode(request), expected def _get_test3_params(self): request = { 'driverRequest': { 'actions': [ { "connectorAttributes": [], "connectionParams": { "vlanId": "2", "mode": "Access", "vlanServiceAttributes": [ { "attributeName": "Access Mode", "attributeValue": "Access", "type": "vlanServiceAttribute" } ], "type": "setVlanParameter" }, "actionId": "1", "actionTarget": { "fullName": "VM Deployment1_9602ad34", "fullAddress": "N/A/NA", "type": "actionTarget" }, "customActionAttributes": [ { "attributeName": "VM_UUID", "attributeValue": "42220ae6-2fa8-b4cd-14e4-16fbad2798f6", "type": "customAttribute" } ], "type": "setVlan" }, { "connectorAttributes": [], "connectionParams": { "vlanId": "2-10", "mode": "Access", "vlanServiceAttributes": [ { "attributeName": "Access Mode", "attributeValue": "Trunk", "type": "vlanServiceAttribute" } ], "type": "setVlanParameter" }, "actionId": "2", "actionTarget": { "fullName": "VM Deployment1_9602ad34", "fullAddress": "N/A/NA", "type": "actionTarget" }, "customActionAttributes": [ { "attributeName": "VM_UUID", "attributeValue": "42220ae6-2fa8-b4cd-14e4-16fbad2798f6", "type": "customAttribute" } ], "type": "setVlan" }, { "connectorAttributes": [], "connectionParams": { "vlanId": "2", "mode": "Access", "vlanServiceAttributes": [ { "attributeName": "Access Mode", "attributeValue": "Access", "type": "vlanServiceAttribute" } ], "type": "setVlanParameter" }, "actionId": "3", "actionTarget": { "fullName": "VM Deployment1_9602ad34", "fullAddress": "N/A/NA", "type": "actionTarget" }, "customActionAttributes": [ { "attributeName": "VM_UUID", "attributeValue": "42220ae6-2fa8-b4cd-14e4-16fbad2798f6", "type": "customAttribute" }, { "attributeName": "Vnic Name", "attributeValue": "1", "type": "customAttribute" } ], "type": "setVlan" }, { "connectorAttributes": [], "connectionParams": { "vlanId": "2", "mode": "Access", "vlanServiceAttributes": [ { "attributeName": "Access Mode", "attributeValue": "Access", "type": "vlanServiceAttribute" } ], "type": "setVlanParameter" }, "actionId": "4", "actionTarget": { "fullName": "VM Deployment1_9602ad34", "fullAddress": "N/A/NA", "type": "actionTarget" }, "customActionAttributes": [ { "attributeName": "VM_UUID", "attributeValue": "1234", "type": "customAttribute" }, { "attributeName": "Vnic Name", "attributeValue": "1", "type": "customAttribute" } ], "type": "setVlan" } ] } } self._set_connect_to_networks_by_request(request) expected = self._get_connect_excepted_results(request) return jsonpickle.encode(request), expected def _get_test4_params(self): request = { "driverRequest": { "actions": [ { "connectionId": "d5369772-66de-4003-85cc-e94a57c20f1e", "connectionParams": { "vlanId": "654", "mode": "Access", "vlanServiceAttributes": [ { "attributeName": "Access Mode", "attributeValue": "Access", "type": "vlanServiceAttribute" }, { "attributeName": "VLAN ID", "attributeValue": "654", "type": "vlanServiceAttribute" }, { "attributeName": "Virtual Network", "attributeValue": "654", "type": "vlanServiceAttribute" } ], "type": "setVlanParameter" }, "connectorAttributes": [ { "attributeName": "Interface", "attributeValue": "00:50:56:a2:5f:07", "type": "connectorAttribute" } ], "actionId": "d5369772-66de-4003-85cc-e94a57c20f1e_30fb5d10-1ce9-4e9f-b41e-ce0d271fd7ab", "actionTarget": { "fullName": "Temp4_a443da02", "fullAddress": "192.168.65.33", "type": "actionTarget" }, "customActionAttributes": [ ], "type": "removeVlan" } ] } } self._set_disconnect_from_networks(request) expected = self._get_disconnect_excepted_results(request, 'VM_UUID is missing on action attributes') return jsonpickle.encode(request), expected def _get_test5_params(self): request = { "driverRequest": { "actions": [ { "connectionId": "d5369772-66de-4003-85cc-e94a57c20f1e", "connectionParams": { "vlanId": "654", "mode": "Access", "vlanServiceAttributes": [ { "attributeName": "Access Mode", "attributeValue": "Access", "type": "vlanServiceAttribute" }, { "attributeName": "VLAN ID", "attributeValue": "654", "type": "vlanServiceAttribute" }, { "attributeName": "Virtual Network", "attributeValue": "654", "type": "vlanServiceAttribute" } ], "type": "setVlanParameter" }, "connectorAttributes": [ { "attributeName": "Interface", "attributeValue": "00:50:56:a2:5f:07", "type": "connectorAttribute" } ], "actionId": "d5369772-66de-4003-85cc-e94a57c20f1e_30fb5d10-1ce9-4e9f-b41e-ce0d271fd7ab", "actionTarget": { "fullName": "Temp4_a443da02", "fullAddress": "192.168.65.33", "type": "actionTarget" }, "customActionAttributes": [ { "attributeName": "VM_UUID", "attributeValue": "42220ae6-2fa8-b4cd-14e4-16fbad2798f6", "type": "customAttribute" } ], "type": "removeVlan" } ] } } self.disconnector.disconnect_from_networks = Mock() self.disconnector.disconnect_from_networks.side_effect = ValueError('vnic not found') expected = self._get_disconnect_excepted_results(request, 'vnic not found') return jsonpickle.encode(request), expected def _get_test6_params(self): request = { 'driverRequest': { 'actions': [ { "connectorAttributes": [], "connectionParams": { "vlanId": "2", "mode": "Access", "vlanServiceAttributes": [ { "attributeName": "Access Mode", "attributeValue": "Access", "type": "vlanServiceAttribute" } ], "type": "setVlanParameter" }, "actionId": "ee8a3dc8-eb4b-4141-92ad-58bbd8430cad_376046bb-1ef0-4ecc-bb52-c0c7a9c75b1c", "actionTarget": { "fullName": "VM Deployment1_9602ad34", "fullAddress": "N/A/NA", "type": "actionTarget" }, "customActionAttributes": [ { "attributeName": "VM_UUID", "attributeValue": "42220ae6-2fa8-b4cd-14e4-16fbad2798f6", "type": "customAttribute" } ], "type": "setVlan" } ] } } self.connector.connect_to_networks = Mock() self.connector.connect_to_networks.side_effect = ValueError('vnic not found') expected = self._get_connect_excepted_results(request, 'vnic not found') # here we don't don't get vnic input input for e in expected: e.updatedInterface = None return jsonpickle.encode(request), expected def _get_test7_params(self): request = {"driverRequest": {"actions": [{"connectionId": "6bd00213-6cfa-435a-abf5-6c839dae0f44", "connectionParams": {"vlanId": "2", "mode": "Access", "vlanServiceAttributes": [ {"attributeName": "QnQ", "attributeValue": "False", "type": "vlanServiceAttribute"}, {"attributeName": "CTag", "attributeValue": "", "type": "vlanServiceAttribute"}, {"attributeName": "Allocation Ranges", "attributeValue": "2-4094", "type": "vlanServiceAttribute"}, {"attributeName": "Isolation Level", "attributeValue": "Exclusive", "type": "vlanServiceAttribute"}, {"attributeName": "Access Mode", "attributeValue": "Access", "type": "vlanServiceAttribute"}, {"attributeName": "VLAN ID", "attributeValue": "", "type": "vlanServiceAttribute"}, {"attributeName": "Pool Name", "attributeValue": "", "type": "vlanServiceAttribute"}, {"attributeName": "Virtual Network", "attributeValue": "2", "type": "vlanServiceAttribute"}], "type": "setVlanParameter"}, "connectorAttributes": [ {"attributeName": "Requested Source vNIC Name", "attributeValue": "1,2", "type": "connectorAttribute"}], "actionId": "6bd00213-6cfa-435a-abf5-6c839dae0f44_c6b69177-d251-4dba-ba88-d7c5bb259505", "actionTarget": {"fullName": "temp_b648f80f", "fullAddress": "NA", "type": "actionTarget"}, "customActionAttributes": [ {"attributeName": "auto_power_off", "attributeValue": "auto_power_off", "type": "customAttribute"}, {"attributeName": "ip_regex", "attributeValue": "ip_regex", "type": "customAttribute"}, {"attributeName": "wait_for_ip", "attributeValue": "wait_for_ip", "type": "customAttribute"}, {"attributeName": "autoload", "attributeValue": "autoload", "type": "customAttribute"}, {"attributeName": "auto_delete", "attributeValue": "auto_delete", "type": "customAttribute"}, {"attributeName": "auto_power_on", "attributeValue": "auto_power_on", "type": "customAttribute"}, {"attributeName": "refresh_ip_timeout", "attributeValue": "refresh_ip_timeout", "type": "customAttribute"}, {"attributeName": "VM_UUID", "attributeValue": "422203f6-eadd-9f88-5dc8-00c17f49fa21", "type": "customAttribute"}, {"attributeName": "Vnic Name", "attributeValue": "1,2", "type": "customAttribute"}], "type": "setVlan"}]}} self._set_connect_to_networks_by_request(request) expected = self._get_connect_excepted_results(request) return jsonpickle.encode(request), expected def _get_test8_params(self): request = {"driverRequest": {"actions": [{"connectionId": "6bd00213-6cfa-435a-abf5-6c839dae0f44", "connectionParams": {"vlanId": "2", "mode": "Access", "vlanServiceAttributes": [ {"attributeName": "QnQ", "attributeValue": "False", "type": "vlanServiceAttribute"}, {"attributeName": "CTag", "attributeValue": "", "type": "vlanServiceAttribute"}, { "attributeName": "Allocation Ranges", "attributeValue": "2-4094", "type": "vlanServiceAttribute"}, { "attributeName": "Isolation Level", "attributeValue": "Exclusive", "type": "vlanServiceAttribute"}, { "attributeName": "Access Mode", "attributeValue": "Access", "type": "vlanServiceAttribute"}, { "attributeName": "VLAN ID", "attributeValue": "", "type": "vlanServiceAttribute"}, { "attributeName": "Pool Name", "attributeValue": "", "type": "vlanServiceAttribute"}, { "attributeName": "Virtual Network", "attributeValue": "2", "type": "vlanServiceAttribute"}], "type": "setVlanParameter"}, "connectorAttributes": [ {"attributeName": "Requested Source vNIC Name", "attributeValue": "1,2", "type": "connectorAttribute"}], "actionId": "6bd00213-6cfa-435a-abf5-6c839dae0f44_c6b69177-d251-4dba-ba88-d7c5bb259505", "actionTarget": {"fullName": "temp_b648f80f", "fullAddress": "NA", "type": "actionTarget"}, "customActionAttributes": [ {"attributeName": "auto_power_off", "attributeValue": "auto_power_off", "type": "customAttribute"}, {"attributeName": "ip_regex", "attributeValue": "ip_regex", "type": "customAttribute"}, {"attributeName": "wait_for_ip", "attributeValue": "wait_for_ip", "type": "customAttribute"}, {"attributeName": "autoload", "attributeValue": "autoload", "type": "customAttribute"}, {"attributeName": "auto_delete", "attributeValue": "auto_delete", "type": "customAttribute"}, {"attributeName": "auto_power_on", "attributeValue": "auto_power_on", "type": "customAttribute"}, {"attributeName": "refresh_ip_timeout", "attributeValue": "refresh_ip_timeout", "type": "customAttribute"}, {"attributeName": "VM_UUID", "attributeValue": "422203f6-eadd-9f88-5dc8-00c17f49fa21", "type": "customAttribute"}, {"attributeName": "Vnic Name", "attributeValue": "Network Adapter 1,Network Adapter 2", "type": "customAttribute"}], "type": "setVlan"}]}} self._set_connect_to_networks_by_request(request) expected = self._get_connect_excepted_results(request) return jsonpickle.encode(request), expected def _get_test9_params(self): request = {"driverRequest":{"actions":[{"connectionId":"4dd285fa-d271-4023-b7d1-881553a8f59f","connectionParams":{"vlanId":"2","mode":"Access","vlanServiceAttributes":[{"attributeName":"QnQ","attributeValue":"False","type":"vlanServiceAttribute"},{"attributeName":"CTag","attributeValue":"","type":"vlanServiceAttribute"},{"attributeName":"Allocation Ranges","attributeValue":"2-4094","type":"vlanServiceAttribute"},{"attributeName":"Isolation Level","attributeValue":"Exclusive","type":"vlanServiceAttribute"},{"attributeName":"Access Mode","attributeValue":"Access","type":"vlanServiceAttribute"},{"attributeName":"VLAN ID","attributeValue":"","type":"vlanServiceAttribute"},{"attributeName":"Pool Name","attributeValue":"","type":"vlanServiceAttribute"},{"attributeName":"Virtual Network","attributeValue":"2","type":"vlanServiceAttribute"}],"type":"setVlanParameter"},"connectorAttributes":[{"attributeName":"Interface","attributeValue":"00:50:56:a2:37:3b","type":"connectorAttribute"}],"actionId":"4dd285fa-d271-4023-b7d1-881553a8f59f_c6b69177-d251-4dba-ba88-d7c5bb259505","actionTarget":{"fullName":"temp_b648f80f","fullAddress":"NA","type":"actionTarget"},"customActionAttributes":[{"attributeName":"auto_power_off","attributeValue":"auto_power_off","type":"customAttribute"},{"attributeName":"ip_regex","attributeValue":"ip_regex","type":"customAttribute"},{"attributeName":"wait_for_ip","attributeValue":"wait_for_ip","type":"customAttribute"},{"attributeName":"autoload","attributeValue":"autoload","type":"customAttribute"},{"attributeName":"auto_delete","attributeValue":"auto_delete","type":"customAttribute"},{"attributeName":"auto_power_on","attributeValue":"auto_power_on","type":"customAttribute"},{"attributeName":"refresh_ip_timeout","attributeValue":"refresh_ip_timeout","type":"customAttribute"},{"attributeName":"VM_UUID","attributeValue":"422203f6-eadd-9f88-5dc8-00c17f49fa21","type":"customAttribute"}],"type":"removeVlan"}]}} self._set_disconnect_from_networks(request) expected = self._get_disconnect_excepted_results(request) return jsonpickle.encode(request), expected