def test_changed_nic(self, client): ip = mfactory.IPv4AddressFactory(subnet__cidr="10.0.0.0/24", address="10.0.0.2") network = ip.network subnet = ip.subnet vm = ip.nic.machine pool = subnet.get_ip_pools()[0] pool.reserve("10.0.0.2") pool.save() msg = self.create_msg(instance_nics=[{'network': network.backend_id, 'ip': '10.0.0.3', 'mac': 'aa:bb:cc:00:11:22', 'name': ip.nic.backend_uuid}], instance=vm.backend_vm_id) update_db(client, msg) self.assertTrue(client.basic_ack.called) db_vm = VirtualMachine.objects.get(id=vm.id) nics = db_vm.nics.all() self.assertEqual(len(nics), 1) self.assertEqual(nics[0].index, 0) self.assertEqual(nics[0].ipv4_address, '10.0.0.3') self.assertEqual(nics[0].mac, 'aa:bb:cc:00:11:22') pool = subnet.get_ip_pools()[0] self.assertTrue(pool.is_available('10.0.0.2')) self.assertFalse(pool.is_available('10.0.0.3')) pool.save()
def test_reboot(self, client): vm = mfactory.VirtualMachineFactory() msg = self.create_msg(operation='OP_INSTANCE_REBOOT', instance=vm.backend_vm_id) update_db(client, msg) client.basic_ack.assert_called_once() db_vm = VirtualMachine.objects.get(id=vm.id) self.assertEqual(db_vm.operstate, 'STARTED')
def test_create(self, client): vm = mfactory.VirtualMachineFactory() msg = self.create_msg(operation='OP_INSTANCE_CREATE', instance=vm.backend_vm_id) update_db(client, msg) self.assertTrue(client.basic_ack.called) db_vm = VirtualMachine.objects.get(id=vm.id) self.assertEqual(db_vm.operstate, 'STARTED')
def test_start(self, client): vm = mfactory.VirtualMachineFactory() msg = self.create_msg(operation='OP_INSTANCE_STARTUP', instance=vm.backend_vm_id) with mocked_quotaholder(): update_db(client, msg) self.assertTrue(client.basic_ack.called) db_vm = VirtualMachine.objects.get(id=vm.id) self.assertEqual(db_vm.operstate, 'STARTED')
def test_stop(self, client): vm = mfactory.VirtualMachineFactory() msg = self.create_msg(operation='OP_INSTANCE_SHUTDOWN', instance=vm.backend_vm_id) with mocked_quotaholder(): update_db(client, msg) self.assertTrue(client.basic_ack.called) db_vm = VirtualMachine.objects.get(id=vm.id) self.assertEqual(db_vm.operstate, 'STOPPED')
def test_remove(self, client): vm = mfactory.VirtualMachineFactory(flavor__cpu=1, flavor__ram=128) mfactory.VolumeFactory(userid=vm.userid, machine=vm, size=1) mfactory.VolumeFactory(userid=vm.userid, machine=vm, size=3) # Also create a NIC ip = mfactory.IPv4AddressFactory(nic__machine=vm) nic = ip.nic with transaction.atomic(): nic.network.get_ip_pools()[0].reserve(nic.ipv4_address) msg = self.create_msg(operation='OP_INSTANCE_REMOVE', instance=vm.backend_vm_id) with mocked_quotaholder() as m: update_db(client, msg) self.assertTrue(client.basic_ack.called) db_vm = VirtualMachine.objects.get(id=vm.id) self.assertEqual(db_vm.operstate, 'DESTROYED') self.assertTrue(db_vm.deleted) # Check that nics are deleted self.assertFalse(db_vm.nics.all()) with transaction.atomic(): self.assertTrue(nic.network.get_ip_pools()[0].is_available(ip.address)) # Check that volumes are deleted self.assertFalse(db_vm.volumes.filter(deleted=False)) # Check quotas name, args, kwargs = m.mock_calls[0] for (userid, res), value in args[1].items(): if res == 'cyclades.disk': self.assertEqual(value, -4 << 30) elif res == 'cyclades.cpu': self.assertEqual(value, -1) elif res == 'cyclades.ram': self.assertEqual(value, -128 << 20) vm2 = mfactory.VirtualMachineFactory() fp1 = mfactory.IPv4AddressFactory(nic__machine=vm2, floating_ip=True, network__floating_ip_pool=True) network = fp1.network nic1 = mfactory.NetworkInterfaceFactory(machine=vm2) fp1.nic = nic1 fp1.save() with transaction.atomic(): pool = network.get_ip_pools()[0] pool.reserve(fp1.address) pool.save() msg = self.create_msg(operation='OP_INSTANCE_REMOVE', instance=vm2.backend_vm_id) with mocked_quotaholder(): update_db(client, msg) self.assertEqual(2, client.basic_ack.call_count) db_vm = VirtualMachine.objects.get(id=vm.id) self.assertEqual(db_vm.operstate, 'DESTROYED') self.assertTrue(db_vm.deleted) self.assertEqual(IPAddress.objects.get(id=fp1.id).nic, None) with transaction.atomic(): pool = network.get_ip_pools()[0] # Test that floating ips are not released self.assertFalse(pool.is_available(fp1.address))
def test_create_error(self, client): """Test that error create sets vm to ERROR state""" vm = mfactory.VirtualMachineFactory() msg = self.create_msg(operation='OP_INSTANCE_CREATE', instance=vm.backend_vm_id, status='error') update_db(client, msg) self.assertTrue(client.basic_ack.called) db_vm = VirtualMachine.objects.get(id=vm.id) self.assertEqual(db_vm.operstate, 'ERROR')
def test_other_error(self, client): """Test that other error messages do no affect the VM""" vm = mfactory.VirtualMachineFactory() msg = self.create_msg(operation='OP_INSTANCE_STARTUP', instance=vm.backend_vm_id, status='error') update_db(client, msg) self.assertTrue(client.basic_ack.called) db_vm = VirtualMachine.objects.get(id=vm.id) self.assertEqual(db_vm.operstate, vm.operstate) self.assertEqual(db_vm.backendtime, vm.backendtime)
def test_no_nics(self, client): vm = mfactory.VirtualMachineFactory(operstate='ERROR') mfactory.NetworkInterfaceFactory(machine=vm) mfactory.NetworkInterfaceFactory(machine=vm) mfactory.NetworkInterfaceFactory(machine=vm) self.assertEqual(len(vm.nics.all()), 3) msg = self.create_msg(nics=[], instance=vm.backend_vm_id) update_db(client, msg) client.basic_ack.assert_called_once() db_vm = VirtualMachine.objects.get(id=vm.id) self.assertEqual(len(db_vm.nics.all()), 0)
def test_no_nics(self, client): vm = mfactory.VirtualMachineFactory(operstate='ERROR') mfactory.NetworkInterfaceFactory(machine=vm, state="ACTIVE") mfactory.NetworkInterfaceFactory(machine=vm, state="ACTIVE") mfactory.NetworkInterfaceFactory(machine=vm, state="ACTIVE") self.assertEqual(len(vm.nics.all()), 3) msg = self.create_msg(instance_nics=[], instance=vm.backend_vm_id) update_db(client, msg) self.assertTrue(client.basic_ack.called) db_vm = VirtualMachine.objects.get(id=vm.id) self.assertEqual(len(db_vm.nics.all()), 0)
def test_remove(self, client): vm = mfactory.VirtualMachineFactory(flavor__cpu=1, flavor__ram=128) mfactory.VolumeFactory(userid=vm.userid, machine=vm, size=1) mfactory.VolumeFactory(userid=vm.userid, machine=vm, size=3) # Also create a NIC ip = mfactory.IPv4AddressFactory(nic__machine=vm) nic = ip.nic nic.network.get_ip_pools()[0].reserve(nic.ipv4_address) msg = self.create_msg(operation='OP_INSTANCE_REMOVE', instance=vm.backend_vm_id) with mocked_quotaholder() as m: update_db(client, msg) self.assertTrue(client.basic_ack.called) db_vm = VirtualMachine.objects.get(id=vm.id) self.assertEqual(db_vm.operstate, 'DESTROYED') self.assertTrue(db_vm.deleted) # Check that nics are deleted self.assertFalse(db_vm.nics.all()) self.assertTrue(nic.network.get_ip_pools()[0].is_available(ip.address)) # Check that volumes are deleted self.assertFalse(db_vm.volumes.filter(deleted=False)) # Check quotas name, args, kwargs = m.mock_calls[0] for (userid, res), value in args[1].items(): if res == 'cyclades.disk': self.assertEqual(value, -4 << 30) elif res == 'cyclades.cpu': self.assertEqual(value, -1) elif res == 'cyclades.ram': self.assertEqual(value, -128 << 20) vm2 = mfactory.VirtualMachineFactory() fp1 = mfactory.IPv4AddressFactory(nic__machine=vm2, floating_ip=True, network__floating_ip_pool=True) network = fp1.network nic1 = mfactory.NetworkInterfaceFactory(machine=vm2) fp1.nic = nic1 fp1.save() pool = network.get_ip_pools()[0] pool.reserve(fp1.address) pool.save() msg = self.create_msg(operation='OP_INSTANCE_REMOVE', instance=vm2.backend_vm_id) with mocked_quotaholder(): update_db(client, msg) client.basic_ack.assert_called_once() db_vm = VirtualMachine.objects.get(id=vm.id) self.assertEqual(db_vm.operstate, 'DESTROYED') self.assertTrue(db_vm.deleted) self.assertEqual(IPAddress.objects.get(id=fp1.id).nic, None) pool = network.get_ip_pools()[0] # Test that floating ips are not released self.assertFalse(pool.is_available(fp1.address))
def test_shutdown_rescued_vm(self, client): rescue_image = mfactory.RescueImageFactory( location='test.iso', location_type=RescueImage.FILETYPE_FILE) vm = mfactory.VirtualMachineFactory(rescue=True, rescue_image=rescue_image) msg = self.create_msg(operation='OP_INSTANCE_SHUTDOWN', instance=vm.backend_vm_id) with mocked_quotaholder(): update_db(client, msg) self.assertTrue(client.basic_ack.called) db_vm = VirtualMachine.objects.get(id=vm.id) self.assertEqual(db_vm.operstate, 'STOPPED') self.assertTrue(db_vm.rescue) self.assertEqual(db_vm.rescue_image, rescue_image)
def test_remove(self, client): vm = mfactory.VirtualMachineFactory() # Also create a NIC mfactory.NetworkInterfaceFactory(machine=vm) msg = self.create_msg(operation='OP_INSTANCE_REMOVE', instance=vm.backend_vm_id) with mocked_quotaholder(): update_db(client, msg) client.basic_ack.assert_called_once() db_vm = VirtualMachine.objects.get(id=vm.id) self.assertEqual(db_vm.operstate, 'DESTROYED') self.assertTrue(db_vm.deleted) # Check that nics are deleted self.assertFalse(db_vm.nics.all())
def test_start_rescued_vm(self, client): rescue_image = mfactory.RescueImageFactory( location='test.iso', location_type=RescueImage.FILETYPE_FILE) vm = mfactory.VirtualMachineFactory(rescue=True, rescue_image=rescue_image) msg = self.create_msg(operation='OP_INSTANCE_STARTUP', instance=vm.backend_vm_id) with mocked_quotaholder(): update_db(client, msg) self.assertTrue(client.basic_ack.called) db_vm = VirtualMachine.objects.get(id=vm.id) # VM should start normally and the rescue flag # should remain true self.assertEqual(db_vm.operstate, "STARTED") self.assertTrue(db_vm.rescue) self.assertEqual(db_vm.rescue_image, rescue_image)
def test_old_msg(self, client): from time import sleep from datetime import datetime old_time = time() sleep(0.01) new_time = datetime.fromtimestamp(time()) vm = mfactory.VirtualMachineFactory(backendtime=new_time) vm.operstate = 'STOPPED' vm.save() msg = self.create_msg(operation='OP_INSTANCE_STARTUP', event_time=split_time(old_time), instance=vm.backend_vm_id) update_db(client, msg) self.assertTrue(client.basic_ack.called) db_vm = VirtualMachine.objects.get(id=vm.id) self.assertEquals(db_vm.operstate, "STOPPED") self.assertEquals(db_vm.backendtime, new_time)
def test_unrescue(self, client): vm = mfactory.VirtualMachineFactory(operstate="STOPPED", rescue=True) status = "success" msg = self.create_msg(operation='OP_INSTANCE_SET_PARAMS', instance=vm.backend_vm_id, instance_hvparams={ 'cdrom_image_path': '', 'boot_order': 'disk'}, status=status) client.reset_mock() with mocked_quotaholder(): update_db(client, msg) self.assertTrue(client.basic_ack.called) db_vm = VirtualMachine.objects.get(id=vm.id) self.assertEqual(db_vm.operstate, vm.operstate) self.assertEqual(db_vm.operstate, "STOPPED") self.assertFalse(db_vm.rescue) self.assertTrue(db_vm.rescue_image is None)
def test_empty_nic(self, client): vm = mfactory.VirtualMachineFactory(operstate='ERROR') for public in [True, False]: net = mfactory.NetworkFactory(public=public) msg = self.create_msg(nics=[{'network': net.backend_id}], instance=vm.backend_vm_id) update_db(client, msg) client.basic_ack.assert_called_once() db_vm = VirtualMachine.objects.get(id=vm.id) nics = db_vm.nics.all() self.assertEqual(len(nics), 1) self.assertEqual(nics[0].index, 0) self.assertEqual(nics[0].ipv4, '') self.assertEqual(nics[0].ipv6, '') self.assertEqual(nics[0].mac, '') if public: self.assertEqual(nics[0].firewall_profile, settings.DEFAULT_FIREWALL_PROFILE) else: self.assertEqual(nics[0].firewall_profile, '')
def test_remove(self, client): vm = mfactory.VirtualMachineFactory() # Also create a NIC ip = mfactory.IPv4AddressFactory(nic__machine=vm) nic = ip.nic nic.network.get_ip_pools()[0].reserve(nic.ipv4_address) msg = self.create_msg(operation='OP_INSTANCE_REMOVE', instance=vm.backend_vm_id) with mocked_quotaholder(): update_db(client, msg) self.assertTrue(client.basic_ack.called) db_vm = VirtualMachine.objects.get(id=vm.id) self.assertEqual(db_vm.operstate, 'DESTROYED') self.assertTrue(db_vm.deleted) # Check that nics are deleted self.assertFalse(db_vm.nics.all()) self.assertTrue(nic.network.get_ip_pools()[0].is_available(ip.address)) vm2 = mfactory.VirtualMachineFactory() fp1 = mfactory.IPv4AddressFactory(nic__machine=vm2, floating_ip=True, network__floating_ip_pool=True) network = fp1.network nic1 = mfactory.NetworkInterfaceFactory(machine=vm2) fp1.nic = nic1 fp1.save() pool = network.get_ip_pools()[0] pool.reserve(fp1.address) pool.save() msg = self.create_msg(operation='OP_INSTANCE_REMOVE', instance=vm2.backend_vm_id) with mocked_quotaholder(): update_db(client, msg) client.basic_ack.assert_called_once() db_vm = VirtualMachine.objects.get(id=vm.id) self.assertEqual(db_vm.operstate, 'DESTROYED') self.assertTrue(db_vm.deleted) self.assertEqual(IPAddress.objects.get(id=fp1.id).nic, None) pool = network.get_ip_pools()[0] # Test that floating ips are not released self.assertFalse(pool.is_available(fp1.address))
def test_error_snapshot(self, pithos_backend, client): vm = mfactory.VirtualMachineFactory() disks = [ (0, {"snapshot_info": json.dumps({"snapshot_id": "test_snapshot_id"})}) ] msg = self.create_msg(operation='OP_INSTANCE_SNAPSHOT', instance=vm.backend_vm_id, job_fields={'disks': disks}, status="running") update_db(client, msg) self.assertEqual(pithos_backend().update_object_status.mock_calls, []) msg = self.create_msg(operation='OP_INSTANCE_SNAPSHOT', instance=vm.backend_vm_id, job_fields={'disks': disks}, event_time=split_time(time()), status="error") update_db(client, msg) pithos_backend().update_object_status\ .assert_called_once_with("test_snapshot_id", state=-1) pithos_backend.reset_mock() msg = self.create_msg(operation='OP_INSTANCE_SNAPSHOT', instance=vm.backend_vm_id, job_fields={'disks': disks}, event_time=split_time(time()), status="success") update_db(client, msg) pithos_backend().update_object_status\ .assert_called_once_with("test_snapshot_id", state=1)
def test_full_nic(self, client): vm = mfactory.VirtualMachineFactory(operstate='ERROR') net = mfactory.NetworkFactory(subnet='10.0.0.0/24') pool = net.get_pool() self.assertTrue(pool.is_available('10.0.0.22')) pool.save() msg = self.create_msg(nics=[{'network': net.backend_id, 'ip': '10.0.0.22', 'mac': 'aa:bb:cc:00:11:22'}], instance=vm.backend_vm_id) update_db(client, msg) client.basic_ack.assert_called_once() db_vm = VirtualMachine.objects.get(id=vm.id) nics = db_vm.nics.all() self.assertEqual(len(nics), 1) self.assertEqual(nics[0].index, 0) self.assertEqual(nics[0].ipv4, '10.0.0.22') self.assertEqual(nics[0].ipv6, '') self.assertEqual(nics[0].mac, 'aa:bb:cc:00:11:22') pool = net.get_pool() self.assertFalse(pool.is_available('10.0.0.22')) pool.save()
def test_empty_nic(self, client): vm = mfactory.VirtualMachineFactory(operstate='ERROR') for public in [True, False]: net = mfactory.NetworkFactory(public=public) msg = self.create_msg(nics=[{ 'network': net.backend_id }], instance=vm.backend_vm_id) update_db(client, msg) client.basic_ack.assert_called_once() db_vm = VirtualMachine.objects.get(id=vm.id) nics = db_vm.nics.all() self.assertEqual(len(nics), 1) self.assertEqual(nics[0].index, 0) self.assertEqual(nics[0].ipv4, '') self.assertEqual(nics[0].ipv6, '') self.assertEqual(nics[0].mac, '') if public: self.assertEqual(nics[0].firewall_profile, settings.DEFAULT_FIREWALL_PROFILE) else: self.assertEqual(nics[0].firewall_profile, '')
def test_rescue_with_unknown_image(self, client): vm = mfactory.VirtualMachineFactory() rescue_image = mfactory.RescueImageFactory( location='known.iso', location_type=RescueImage.FILETYPE_FILE) vm.operstate = "STOPPED" status = "success" vm.save() msg = self.create_msg(operation='OP_INSTANCE_SET_PARAMS', instance=vm.backend_vm_id, instance_hvparams={ 'cdrom_image_path': os.path.join( settings.RESCUE_IMAGE_PATH, 'none.iso'), 'boot_order': 'cdrom,disk'}, status=status) client.reset_mock() with mocked_quotaholder(): update_db(client, msg) self.assertTrue(client.basic_ack.called) db_vm = VirtualMachine.objects.get(id=vm.id) self.assertEqual(db_vm.operstate, vm.operstate) self.assertEqual(db_vm.operstate, "STOPPED") self.assertTrue(db_vm.rescue) self.assertTrue(db_vm.rescue_image is None)
def test_full_nic(self, client): vm = mfactory.VirtualMachineFactory(operstate='ERROR') net = mfactory.NetworkFactory(subnet='10.0.0.0/24') pool = net.get_pool() self.assertTrue(pool.is_available('10.0.0.22')) pool.save() msg = self.create_msg(nics=[{ 'network': net.backend_id, 'ip': '10.0.0.22', 'mac': 'aa:bb:cc:00:11:22' }], instance=vm.backend_vm_id) update_db(client, msg) client.basic_ack.assert_called_once() db_vm = VirtualMachine.objects.get(id=vm.id) nics = db_vm.nics.all() self.assertEqual(len(nics), 1) self.assertEqual(nics[0].index, 0) self.assertEqual(nics[0].ipv4, '10.0.0.22') self.assertEqual(nics[0].ipv6, '') self.assertEqual(nics[0].mac, 'aa:bb:cc:00:11:22') pool = net.get_pool() self.assertFalse(pool.is_available('10.0.0.22')) pool.save()
def test_remove_error(self, rapi, client): vm = mfactory.VirtualMachineFactory() # Also create a NIC msg = self.create_msg(operation='OP_INSTANCE_REMOVE', status="error", instance=vm.backend_vm_id) rapi().GetInstance.return_value = {} update_db(client, msg) db_vm = VirtualMachine.objects.get(id=vm.id) self.assertFalse(db_vm.deleted) rapi().GetInstance.side_effect = GanetiApiError(msg="msg", code=503) update_db(client, msg) db_vm = VirtualMachine.objects.get(id=vm.id) self.assertFalse(db_vm.deleted) rapi().GetInstance.side_effect = GanetiApiError(msg="msg", code=404) with mocked_quotaholder(): update_db(client, msg) db_vm = VirtualMachine.objects.get(id=vm.id) self.assertTrue(db_vm.deleted)
def test_missing_instance(self, client): msg = self.create_msg(operation='OP_INSTANCE_STARTUP', instance='foo') update_db(client, msg) self.assertTrue(client.basic_ack.called)
def test_unhandled_exception(self, client): update_db(client, {}) client.basic_reject.assert_called_once()
def test_missing_attribute(self, client): update_db(client, json.dumps({'body': {}})) self.assertTrue(client.basic_reject.called)
def test_missing_attribute(self, client): update_db(client, json.dumps({'body': {}})) client.basic_nack.assert_called_once()
def test_wrong_type(self, client): msg = self.create_msg(type="WRONG_TYPE") update_db(client, msg) client.basic_ack.assert_called_once()
def test_resize_msg(self, client): vm = mfactory.VirtualMachineFactory() # Test empty beparams for status in ["success", "error"]: msg = self.create_msg(operation='OP_INSTANCE_SET_PARAMS', instance=vm.backend_vm_id, job_fields={"beparams": {}}, status=status) client.reset_mock() with mocked_quotaholder(): update_db(client, msg) self.assertTrue(client.basic_ack.called) db_vm = VirtualMachine.objects.get(id=vm.id) self.assertEqual(db_vm.operstate, vm.operstate) # Test intermediate states vm.operstate = "STOPPED" vm.save() for status in ["queued", "waiting", "running"]: beparams = {"vcpus": 4, "minmem": 2048, "maxmem": 2048} msg = self.create_msg(operation='OP_INSTANCE_SET_PARAMS', instance=vm.backend_vm_id, job_fields={"beparams": beparams}, status=status) client.reset_mock() update_db(client, msg) self.assertTrue(client.basic_ack.called) db_vm = VirtualMachine.objects.get(id=vm.id) self.assertEqual(db_vm.operstate, "STOPPED") # Test operstate after error msg = self.create_msg(operation='OP_INSTANCE_SET_PARAMS', instance=vm.backend_vm_id, beparams={"vcpus": 4}, status="error") client.reset_mock() with mocked_quotaholder(): update_db(client, msg) self.assertTrue(client.basic_ack.called) db_vm = VirtualMachine.objects.get(id=vm.id) self.assertEqual(db_vm.operstate, "STOPPED") # Test success f1 = mfactory.FlavorFactory(cpu=4, ram=1024, disk_template="drbd", disk=1024) vm.flavor = f1 vm.save() f2 = mfactory.FlavorFactory(cpu=8, ram=2048, disk_template="drbd", disk=1024) beparams = {"vcpus": 8, "minmem": 2048, "maxmem": 2048} msg = self.create_msg(operation='OP_INSTANCE_SET_PARAMS', instance=vm.backend_vm_id, job_fields={"beparams": beparams}, status="success") client.reset_mock() with mocked_quotaholder(): update_db(client, msg) self.assertTrue(client.basic_ack.called) db_vm = VirtualMachine.objects.get(id=vm.id) self.assertEqual(db_vm.operstate, "STOPPED") self.assertEqual(db_vm.flavor, f2) beparams = {"vcpus": 100, "minmem": 2048, "maxmem": 2048} msg = self.create_msg(operation='OP_INSTANCE_SET_PARAMS', instance=vm.backend_vm_id, job_fields={"beparams": beparams}, status="success") client.reset_mock() with mocked_quotaholder(): update_db(client, msg) self.assertTrue(client.basic_reject.called)
def test_not_json(self, client): update_db(client, {'body': ''}) self.assertEqual(1, client.basic_nack.call_count)
def test_unhandled_exception(self, client): update_db(client, None) self.assertEqual(1, client.basic_reject.call_count)
def test_resize_msg(self, client): vm = mfactory.VirtualMachineFactory() # Test empty beparams for status in ["success", "error"]: msg = self.create_msg(operation='OP_INSTANCE_SET_PARAMS', instance=vm.backend_vm_id, job_fields={"beparams": {}}, status=status) client.reset_mock() with mocked_quotaholder(): update_db(client, msg) self.assertTrue(client.basic_ack.called) db_vm = VirtualMachine.objects.get(id=vm.id) self.assertEqual(db_vm.operstate, vm.operstate) # Test intermediate states vm.operstate = "STOPPED" vm.save() for status in ["queued", "waiting", "running"]: beparams = {"vcpus": 4, "minmem": 2048, "maxmem": 2048} msg = self.create_msg(operation='OP_INSTANCE_SET_PARAMS', instance=vm.backend_vm_id, job_fields={"beparams": beparams}, status=status) client.reset_mock() update_db(client, msg) self.assertTrue(client.basic_ack.called) db_vm = VirtualMachine.objects.get(id=vm.id) self.assertEqual(db_vm.operstate, "STOPPED") # Test operstate after error msg = self.create_msg(operation='OP_INSTANCE_SET_PARAMS', instance=vm.backend_vm_id, beparams={"vcpus": 4}, status="error") client.reset_mock() with mocked_quotaholder(): update_db(client, msg) self.assertTrue(client.basic_ack.called) db_vm = VirtualMachine.objects.get(id=vm.id) self.assertEqual(db_vm.operstate, "STOPPED") # Test success f1 = mfactory.FlavorFactory(cpu=4, ram=1024, volume_type__disk_template="drbd", disk=1024) vm.flavor = f1 vm.save() f2 = mfactory.FlavorFactory(cpu=8, ram=2048, volume_type__disk_template="drbd", disk=1024) beparams = {"vcpus": 8, "minmem": 2048, "maxmem": 2048} msg = self.create_msg(operation='OP_INSTANCE_SET_PARAMS', instance=vm.backend_vm_id, job_fields={"beparams": beparams}, status="success") client.reset_mock() with mocked_quotaholder(): update_db(client, msg) self.assertTrue(client.basic_ack.called) db_vm = VirtualMachine.objects.get(id=vm.id) self.assertEqual(db_vm.operstate, "STOPPED") self.assertEqual(db_vm.flavor, f2) beparams = {"vcpus": 100, "minmem": 2048, "maxmem": 2048} msg = self.create_msg(operation='OP_INSTANCE_SET_PARAMS', instance=vm.backend_vm_id, job_fields={"beparams": beparams}, status="success") client.reset_mock() with mocked_quotaholder(): update_db(client, msg) self.assertTrue(client.basic_reject.called)
def test_wrong_type(self, client): msg = self.create_msg(type="WRONG_TYPE") update_db(client, msg) self.assertTrue(client.basic_nack.called)
def test_malformed_json(self, client): update_db(client, {'body': '{}'}) self.assertEqual(1, client.basic_nack.call_count)