def test_vm_renamed(controller, database, esxi_api_client, vcenter_api_client, vnc_api_client, vrouter_api_client, vm_created_update, vm_renamed_update, vm_properties_renamed, vn_model_1): # Virtual Networks are already created for us and after synchronization, # their models are stored in our database database.save(vn_model_1) # The port has no vlan id set in vcenter vcenter_api_client.get_vlan_id.return_value = None # A new update containing VmCreatedEvent arrives and is being handled by the controller controller.handle_update(vm_created_update) # A user renames the VM in vSphere and VmRenamedEvent arrives esxi_api_client.read_vm_properties.return_value = vm_properties_renamed controller.handle_update(vm_renamed_update) # Check if VM Model has been saved properly: # - in VNC: assert vnc_api_client.update_vmi.call_count == 2 vnc_vm = vnc_api_client.update_vm.call_args[0][0] assert_vnc_vm_state(vnc_vm, uuid='vmware-vm-uuid-1', name='vmware-vm-uuid-1', display_name='VM1-renamed') # - in Database: vm_model = database.get_vm_model_by_uuid('vmware-vm-uuid-1') assert_vm_model_state(vm_model, uuid='vmware-vm-uuid-1', name='VM1-renamed') # Check if VMI Model has been saved properly: # - in VNC assert vnc_api_client.update_vmi.call_count == 2 # - in Database vmi_model = database.get_all_vmi_models()[0] # Check if VMI Model's Instance IP has been created in VNC: vnc_api_client.create_and_read_instance_ip.assert_called_once() # Check if VMI's vRouter Port has been added: vrouter_api_client.add_port.called_with(vmi_model) assert vrouter_api_client.add_port.call_count == 2 # Check if VLAN ID has been set using VLAN Override vcenter_port = vcenter_api_client.set_vlan_id.call_args[0][0] assert vcenter_port.port_key == '10' assert vcenter_port.vlan_id == 0 # Check inner VMI model state assert_vmi_model_state( vmi_model, mac_address='mac-address', ip_address='192.168.100.5', vlan_id=0, display_name='vmi-DPG1-VM1-renamed', vn_model=vn_model_1, vm_model=vm_model )
def test_sync_no_uuid_vm(vm_service, database, esxi_api_client, vnc_api_client, vmware_vm_1, vmware_vm_no_uuid): esxi_api_client.get_all_vms.return_value = [vmware_vm_1, vmware_vm_no_uuid] vm_service.get_vms_from_vmware() vm_model = database.get_vm_model_by_uuid('vmware-vm-uuid-1') assert vm_model.devices == vmware_vm_1.config.hardware.device assert_vm_model_state( vm_model=vm_model, uuid='vmware-vm-uuid-1', name='VM1', ) vnc_api_client.update_vm.assert_called_once()
def test_update_new_vm(vm_service, database, vnc_api_client, vmware_vm_1): vm_service.update(vmware_vm_1) vm_model = database.get_vm_model_by_uuid('vmware-vm-uuid-1') assert_vm_model_state( vm_model=vm_model, uuid='vmware-vm-uuid-1', name='VM1', has_ports={'mac-address': 'dvportgroup-1'}, tools_running=True, is_powered_on=True, ) vnc_api_client.update_vm.assert_called_once()
def test_vm_running_tools_update(controller, database, vm_created_update, vmware_tools_not_running_update, vmware_tools_running_update, vn_model_1): # Virtual Networks are already created for us and after synchronization, # their models are stored in our database database.save(vn_model_1) # A new update containing VmCreatedEvent arrives and is being handled by the controller controller.handle_update(vm_created_update) vm_model = database.get_vm_model_by_uuid('vmware-vm-uuid-1') # Assumption that VM tools running assert_vm_model_state(vm_model, tools_running=True) # Then VM disable vmware tools event is being handled controller.handle_update(vmware_tools_not_running_update) # Check that VM vmware tools is not running assert_vm_model_state(vm_model, tools_running=False) # Then VM enable vmware tools event is being handled controller.handle_update(vmware_tools_running_update) # Check that VM vmware tools is running assert_vm_model_state(vm_model, tools_running=True)
def test_vm_power_state_update(controller, database, vrouter_api_client, vm_created_update, vm_power_on_state_update, vm_power_off_state_update, vn_model_1): # Virtual Networks are already created for us and after synchronization, # their models are stored in our database database.save(vn_model_1) # A new update containing VmCreatedEvent arrives and is being handled by the controller controller.handle_update(vm_created_update) vm_model = database.get_vm_model_by_uuid('vmware-vm-uuid-1') # Assumption that VM is in powerOn state assert_vm_model_state(vm_model, is_powered_on=True) # Then VM power state change is being handled controller.handle_update(vm_power_off_state_update) # Check that VM is in powerOff state assert_vm_model_state(vm_model, is_powered_on=False) vmi_models = database.get_vmi_models_by_vm_uuid('vmware-vm-uuid-1') assert len(vmi_models) == 1 vmi_model = vmi_models[0] # Check that vRouter Port was disabled vrouter_api_client.disable_port.assert_called_once_with(vmi_model.uuid) controller.handle_update(vm_power_on_state_update) # Check that VM is in powerOn state assert_vm_model_state(vm_model, is_powered_on=True) # Check that vRouter Port was enabled vrouter_api_client.enable_port.assert_called_with(vmi_model.uuid)
def test_set_vlan_id_on_power_on(controller, database, vcenter_api_client, esxi_api_client, vrouter_api_client, vm_registered_update, vm_power_on_state_update, vn_model_1, vm_properties_1_off): # Virtual Networks are already created for us and after synchronization, # their models are stored in our database database.save(vn_model_1) # CVM sets VLAN ID in vCenter only when current VLAN conflicts with VLAN of another VM database.is_vlan_available = Mock() database.is_vlan_available.return_value = False vcenter_api_client.set_vlan_id.return_value = 'success', None # VM is powered off when the VM Registered event comes esxi_api_client.read_vm_properties.return_value = vm_properties_1_off # A new update containing VmCreatedEvent arrives and is being handled by the controller controller.handle_update(vm_registered_update) vm_model = database.get_vm_model_by_uuid('vmware-vm-uuid-1') # Assumption that VM is in powerOn state assert_vm_model_state(vm_model, is_powered_on=False) # Then VM power state change is being handled controller.handle_update(vm_power_on_state_update) # Check that VM is in powerOn state assert_vm_model_state(vm_model, is_powered_on=True) vmi_models = database.get_vmi_models_by_vm_uuid('vmware-vm-uuid-1') assert len(vmi_models) == 1 vmi_model = vmi_models[0] vcenter_api_client.set_vlan_id.assert_called_once_with( vmi_model.vcenter_port) # Check that vRouter Port was disabled vrouter_api_client.enable_port.assert_called_once_with(vmi_model.uuid)
def test_vm_created(controller, database, vcenter_api_client, vnc_api_client, vrouter_api_client, vlan_id_pool, vm_created_update, vnc_vn_1, vn_model_1): # Virtual Networks are already created for us and after synchronization, # their models are stored in our database database.save(vn_model_1) # Some vlan ids should be already reserved vcenter_api_client.get_vlan_id.return_value = None reserve_vlan_ids(vlan_id_pool, [0, 1]) # A new update containing VmCreatedEvent arrives and is being handled by the controller controller.handle_update(vm_created_update) # Check if VM Model has been saved properly: # - in VNC: vnc_api_client.update_vm.assert_called_once() vnc_vm = vnc_api_client.update_vm.call_args[0][0] assert_vnc_vm_state(vnc_vm, uuid='vmware-vm-uuid-1', name='vmware-vm-uuid-1', owner='project-uuid') # - in Database: vm_model = database.get_vm_model_by_uuid('vmware-vm-uuid-1') assert_vm_model_state(vm_model, uuid='vmware-vm-uuid-1', name='VM1') # Check if VMI Model has been saved properly: # - in VNC vnc_api_client.update_vmi.assert_called_once() vnc_vmi = vnc_api_client.update_vmi.call_args[0][0] assert_vnc_vmi_state(vnc_vmi, mac_address='mac-address', vnc_vm_uuid=vnc_vm.uuid, vnc_vn_uuid=vnc_vn_1.uuid) # - in Database vmi_model = database.get_all_vmi_models()[0] # Check if VMI Model's Instance IP has been created in VNC: vnc_api_client.create_and_read_instance_ip.assert_called_once() # Check if VMI's vRouter Port has been added: vrouter_api_client.read_port.assert_called_once() vrouter_api_client.delete_port.assert_not_called() vrouter_api_client.add_port.assert_called_once_with(vmi_model) vrouter_api_client.disable_port.assert_not_called() # Check if VLAN ID has been set using VLAN Override vcenter_port = vcenter_api_client.set_vlan_id.call_args[0][0] assert vcenter_port.port_key == '10' assert vcenter_port.vlan_id == 2 # Check inner VMI model state assert_vmi_model_state(vmi_model, mac_address='mac-address', ip_address='192.168.100.5', vlan_id=2, display_name='vmi-DPG1-VM1', vn_model=vn_model_1, vm_model=vm_model)
def test_vm_reconfigured(controller, database, vcenter_api_client, vnc_api_client, vrouter_api_client, vm_created_update, vm_reconfigured_update, vmware_vm_1, vn_model_1, vnc_vn_2, vn_model_2, vlan_id_pool): # Virtual Networks are already created for us and after synchronization, # their models are stored in our database database.save(vn_model_1) database.save(vn_model_2) # Some vlan ids should be already reserved vcenter_api_client.get_vlan_id.return_value = None reserve_vlan_ids(vlan_id_pool, [0, 1, 2, 3]) # A new update containing VmCreatedEvent arrives and is being handled by the controller controller.handle_update(vm_created_update) # After a portgroup is changed, the port key is also changed vmware_vm_1.config.hardware.device[ 0].backing.port.portgroupKey = 'dvportgroup-2' vmware_vm_1.config.hardware.device[0].backing.port.portKey = '11' # Then VmReconfiguredEvent is being handled controller.handle_update(vm_reconfigured_update) # Check if VM Model has been saved properly in Database: vm_model = database.get_vm_model_by_uuid('vmware-vm-uuid-1') assert_vm_model_state(vm_model, has_ports={'mac-address': 'dvportgroup-2'}) # Check that VM was not updated in VNC except VM create event vnc_api_client.update_vm.assert_called_once() # Check if VMI Model has been saved properly: # - in Database vmi_models = database.get_vmi_models_by_vm_uuid('vmware-vm-uuid-1') assert len(vmi_models) == 1 vmi_model = vmi_models[0] # - in VNC assert vnc_api_client.update_vmi.call_count == 2 vnc_vmi = vnc_api_client.update_vmi.call_args[0][0] assert vnc_vmi.get_virtual_network_refs()[0]['uuid'] == vnc_vn_2.uuid # Check if VMI Model's Instance IP has been updated in VNC: assert vnc_api_client.create_and_read_instance_ip.call_count == 2 new_instance_ip = vmi_model.vnc_instance_ip assert vnc_api_client.create_and_read_instance_ip.call_args[0][ 0] == vmi_model assert vnc_vn_2.uuid in [ ref['uuid'] for ref in new_instance_ip.get_virtual_network_refs() ] # Check if VMI's vRouter Port has been updated: vrouter_api_client.delete_port.assert_called_once_with(vmi_model.uuid) assert vrouter_api_client.add_port.call_count == 2 assert vrouter_api_client.add_port.call_args[0][0] == vmi_model # Check if VLAN ID has been set using VLAN Override assert vcenter_api_client.set_vlan_id.call_count == 2 vcenter_port = vcenter_api_client.set_vlan_id.call_args[0][0] assert vcenter_port.port_key == '11' assert vcenter_port.vlan_id == 5 # Check inner VMI model state assert_vmi_model_state(vmi_model, mac_address='mac-address', ip_address='192.168.100.5', vlan_id=5, display_name='vmi-DPG2-VM1', vn_model=vn_model_2)