def test_reassign_vm_backends(self, mrapi): volume = mfactory.VolumeFactory() vm = volume.machine original_project = vm.project another_project = "another_project" with mocked_quotaholder(): servers.reassign(vm, another_project, False) self.assertEqual(vm.project, another_project) self.assertEqual(vm.shared_to_project, False) vol = vm.volumes.get(id=volume.id) self.assertNotEqual(vol.project, another_project) backend = vm.backend backend.public = False backend.save() with mocked_quotaholder(): self.assertRaises(faults.BadRequest, servers.reassign, vm, original_project, False) self.assertEqual(vm.project, another_project) self.assertEqual(vm.shared_to_project, False) vol = vm.volumes.get(id=volume.id) self.assertNotEqual(vol.project, another_project) mfactory.ProjectBackendFactory(project=original_project, backend=backend) with mocked_quotaholder(): servers.reassign(vm, original_project, False) self.assertEqual(vm.project, original_project) self.assertEqual(vm.shared_to_project, False) vol = vm.volumes.get(id=volume.id) self.assertEqual(vol.project, original_project)
def test_reassign_vm_backends(self, mrapi): volume = mfactory.VolumeFactory() vm = volume.machine original_project = vm.project another_project = "another_project" with mocked_quotaholder(): vm = servers.reassign(vm.id, another_project, False, credentials=self.credentials) self.assertEqual(vm.project, another_project) self.assertEqual(vm.shared_to_project, False) vol = vm.volumes.get(id=volume.id) self.assertNotEqual(vol.project, another_project) backend = vm.backend backend.public = False backend.save() with mocked_quotaholder(): self.assertRaises(faults.Forbidden, servers.reassign, vm.id, original_project, False, credentials=self.credentials) self.assertEqual(vm.project, another_project) self.assertEqual(vm.shared_to_project, False) vol = vm.volumes.get(id=volume.id) self.assertNotEqual(vol.project, another_project) mfactory.ProjectBackendFactory(project=original_project, backend=backend) with mocked_quotaholder(): vm = servers.reassign(vm.id, original_project, False, credentials=self.credentials) self.assertEqual(vm.project, original_project) self.assertEqual(vm.shared_to_project, False) vol = vm.volumes.get(id=volume.id) self.assertEqual(vol.project, original_project)
def test_create(self, mrapi): flavor = mfactory.FlavorFactory() kwargs = { "userid": "test", "name": "test_vm", "password": "******", "flavor": flavor, "image_id": "safs", "networks": [], "metadata": { "foo": "bar" }, "personality": [], } # no backend! mfactory.BackendFactory(offline=True) self.assertRaises(faults.ServiceUnavailable, servers.create, **kwargs) self.assertEqual(models.VirtualMachine.objects.count(), 0) mfactory.IPv4SubnetFactory(network__public=True) mfactory.IPv6SubnetFactory(network__public=True) backend = mfactory.BackendFactory() # error in nics req = deepcopy(kwargs) req["networks"] = [{"uuid": 42}] self.assertRaises(faults.ItemNotFound, servers.create, **req) self.assertEqual(models.VirtualMachine.objects.count(), 0) # error in enqueue. check the vm is deleted and resources released mrapi().CreateInstance.side_effect = Exception("ganeti is down") with mocked_quotaholder(): servers.create(**kwargs) vm = models.VirtualMachine.objects.get() self.assertFalse(vm.deleted) self.assertEqual(vm.operstate, "ERROR") for nic in vm.nics.all(): self.assertEqual(nic.state, "ERROR") # test ext settings: req = deepcopy(kwargs) ext_flavor = mfactory.FlavorFactory( volume_type__disk_template="ext_archipelago", disk=1) req["flavor"] = ext_flavor mrapi().CreateInstance.return_value = 42 backend.disk_templates = ["ext"] backend.save() osettings = { "GANETI_DISK_PROVIDER_KWARGS": { "archipelago": { "foo": "mpaz", "lala": "lolo" } } } with mocked_quotaholder(): with override_settings(settings, **osettings): vm = servers.create(**req) name, args, kwargs = mrapi().CreateInstance.mock_calls[-1] self.assertEqual( kwargs["disks"][0], { "provider": "archipelago", "origin": "test_mapfile", "origin_size": 1000, "name": vm.volumes.all()[0].backend_volume_uuid, "foo": "mpaz", "lala": "lolo", "size": 1024 })
def test_create(self, mrapi): flavor = mfactory.FlavorFactory() kwargs = { "userid": "test", "name": "test_vm", "password": "******", "flavor": flavor, "image_id": "safs", "networks": [], "metadata": {"foo": "bar"}, "personality": [], } # no backend! mfactory.BackendFactory(offline=True) self.assertRaises(faults.ServiceUnavailable, servers.create, **kwargs) self.assertEqual(models.VirtualMachine.objects.count(), 0) mfactory.IPv4SubnetFactory(network__public=True) mfactory.IPv6SubnetFactory(network__public=True) backend = mfactory.BackendFactory() # error in nics req = deepcopy(kwargs) req["networks"] = [{"uuid": 42}] self.assertRaises(faults.ItemNotFound, servers.create, **req) self.assertEqual(models.VirtualMachine.objects.count(), 0) # error in enqueue. check the vm is deleted and resources released mrapi().CreateInstance.side_effect = Exception("ganeti is down") with mocked_quotaholder(): servers.create(**kwargs) vm = models.VirtualMachine.objects.get() self.assertFalse(vm.deleted) self.assertEqual(vm.operstate, "ERROR") for nic in vm.nics.all(): self.assertEqual(nic.state, "ERROR") # test ext settings: req = deepcopy(kwargs) ext_flavor = mfactory.FlavorFactory( volume_type__disk_template="ext_archipelago", disk=1) req["flavor"] = ext_flavor mrapi().CreateInstance.return_value = 42 backend.disk_templates = ["ext"] backend.save() osettings = { "GANETI_DISK_PROVIDER_KWARGS": { "archipelago": { "foo": "mpaz", "lala": "lolo" } } } with mocked_quotaholder(): with override_settings(settings, **osettings): with patch( 'synnefo.logic.backend_allocator.update_backends_disk_templates' ) as update_disk_templates_mock: # Check that between the `get_available_backends` call # and the `update_backend_disk_templates` call # the backend doesn't change. update_disk_templates_mock.return_value = [backend] vm = servers.create(**req) update_disk_templates_mock.assert_called_once_with([backend], ext_flavor) name, args, kwargs = mrapi().CreateInstance.mock_calls[-1] self.assertEqual(kwargs["disks"][0], {"provider": "archipelago", "origin": "test_mapfile", "origin_size": 1000, "name": vm.volumes.all()[0].backend_volume_uuid, "foo": "mpaz", "lala": "lolo", "size": 1024})
def test_create(self, mrapi): flavor = mfactory.FlavorFactory() kwargs = { "credentials": self.credentials, "name": "test_vm", "password": "******", "flavor": flavor, "image_id": "safs", "networks": [], "metadata": { "foo": "bar" }, "personality": [], } # no backend! mfactory.BackendFactory(offline=True) self.assertRaises(faults.ServiceUnavailable, servers.create, **kwargs) self.assertEqual(models.VirtualMachine.objects.count(), 0) mfactory.IPv4SubnetFactory(network__public=True) mfactory.IPv6SubnetFactory(network__public=True) backend = mfactory.BackendFactory() # error in nics req = deepcopy(kwargs) req["networks"] = [{"uuid": 42}] self.assertRaises(faults.ItemNotFound, servers.create, **req) self.assertEqual(models.VirtualMachine.objects.count(), 0) # error in enqueue. check the vm is deleted and resources released mrapi().CreateInstance.side_effect = Exception("ganeti is down") with mocked_quotaholder(): servers.create(**kwargs) vm = models.VirtualMachine.objects.get() self.assertFalse(vm.deleted) self.assertEqual(vm.operstate, "ERROR") for nic in vm.nics.all(): self.assertEqual(nic.state, "ERROR") # test ext settings: req = deepcopy(kwargs) vlmt = mfactory.VolumeTypeFactory(disk_template='ext_archipelago') # Generate 4 specs. 2 prefixed with GNT_EXTP_VOLTYPESPEC_PREFIX # and 2 with an other prefix that should be omitted volume_type_specs = [ mfactory.VolumeTypeSpecsFactory(volume_type=vlmt, key='%sbar' % GNT_EXTP_VOLTYPESPEC_PREFIX), mfactory.VolumeTypeSpecsFactory(volume_type=vlmt, key='%sfoo' % GNT_EXTP_VOLTYPESPEC_PREFIX), mfactory.VolumeTypeSpecsFactory(volume_type=vlmt, key='other-prefx-baz'), mfactory.VolumeTypeSpecsFactory(volume_type=vlmt, key='another-prefix-biz'), ] gnt_prefixed_specs = filter( lambda s: s.key.startswith(GNT_EXTP_VOLTYPESPEC_PREFIX), volume_type_specs) ext_flavor = mfactory.FlavorFactory(volume_type=vlmt, disk=1) req["flavor"] = ext_flavor mrapi().CreateInstance.return_value = 42 backend.disk_templates = ["ext"] backend.save() osettings = { "GANETI_DISK_PROVIDER_KWARGS": { "archipelago": { "foo": "mpaz", "lala": "lolo" } } } with mocked_quotaholder(): with override_settings(settings, **osettings): with patch( 'synnefo.logic.backend_allocator.update_backends_disk_templates' # noqa E265 ) as update_disk_templates_mock: # Check that between the `get_available_backends` call # and the `update_backend_disk_templates` call # the backend doesn't change. update_disk_templates_mock.return_value = [backend] vm = servers.create(**req) update_disk_templates_mock.assert_called_once_with([backend]) name, args, kwargs = mrapi().CreateInstance.mock_calls[-1] disk_kwargs = { "provider": "archipelago", "origin": "test_mapfile", "origin_size": 1000, "name": vm.volumes.all()[0].backend_volume_uuid, "foo": "mpaz", "lala": "lolo", "size": 1024 } disk_kwargs.update({ spec.key[len(GNT_EXTP_VOLTYPESPEC_PREFIX):]: spec.value for spec in gnt_prefixed_specs }) self.assertEqual(kwargs["disks"][0], disk_kwargs)
def test_create(self, mrapi): flavor = mfactory.FlavorFactory() kwargs = { "credentials": self.credentials, "name": "test_vm", "password": "******", "flavor": flavor, "image_id": "safs", "networks": [], "metadata": {"foo": "bar"}, "personality": [], } # no backend! mfactory.BackendFactory(offline=True) self.assertRaises(faults.ServiceUnavailable, servers.create, **kwargs) self.assertEqual(models.VirtualMachine.objects.count(), 0) mfactory.IPv4SubnetFactory(network__public=True) mfactory.IPv6SubnetFactory(network__public=True) backend = mfactory.BackendFactory() # error in nics req = deepcopy(kwargs) req["networks"] = [{"uuid": 42}] self.assertRaises(faults.ItemNotFound, servers.create, **req) self.assertEqual(models.VirtualMachine.objects.count(), 0) # error in enqueue. check the vm is deleted and resources released mrapi().CreateInstance.side_effect = Exception("ganeti is down") with mocked_quotaholder(): servers.create(**kwargs) vm = models.VirtualMachine.objects.get() self.assertFalse(vm.deleted) self.assertEqual(vm.operstate, "ERROR") for nic in vm.nics.all(): self.assertEqual(nic.state, "ERROR") # test ext settings: req = deepcopy(kwargs) vlmt = mfactory.VolumeTypeFactory(disk_template='ext_archipelago') # Generate 4 specs. 2 prefixed with GNT_EXTP_VOLTYPESPEC_PREFIX # and 2 with an other prefix that should be omitted volume_type_specs = [ mfactory.VolumeTypeSpecsFactory( volume_type=vlmt, key='%sbar' % GNT_EXTP_VOLTYPESPEC_PREFIX), mfactory.VolumeTypeSpecsFactory( volume_type=vlmt, key='%sfoo' % GNT_EXTP_VOLTYPESPEC_PREFIX), mfactory.VolumeTypeSpecsFactory( volume_type=vlmt, key='other-prefx-baz'), mfactory.VolumeTypeSpecsFactory( volume_type=vlmt, key='another-prefix-biz'), ] gnt_prefixed_specs = filter(lambda s: s.key.startswith( GNT_EXTP_VOLTYPESPEC_PREFIX), volume_type_specs) ext_flavor = mfactory.FlavorFactory( volume_type=vlmt, disk=1) req["flavor"] = ext_flavor mrapi().CreateInstance.return_value = 42 backend.disk_templates = ["ext"] backend.save() osettings = { "GANETI_DISK_PROVIDER_KWARGS": { "archipelago": { "foo": "mpaz", "lala": "lolo" } } } with mocked_quotaholder(): with override_settings(settings, **osettings): with patch( 'synnefo.logic.backend_allocator.update_backends_disk_templates' # noqa E265 ) as update_disk_templates_mock: # Check that between the `get_available_backends` call # and the `update_backend_disk_templates` call # the backend doesn't change. update_disk_templates_mock.return_value = [backend] vm = servers.create(**req) update_disk_templates_mock.assert_called_once_with([backend]) name, args, kwargs = mrapi().CreateInstance.mock_calls[-1] disk_kwargs = {"provider": "archipelago", "origin": "test_mapfile", "origin_size": 1000, "name": vm.volumes.all()[0].backend_volume_uuid, "foo": "mpaz", "lala": "lolo", "size": 1024} disk_kwargs.update({spec.key[len(GNT_EXTP_VOLTYPESPEC_PREFIX):]: spec.value for spec in gnt_prefixed_specs}) self.assertEqual(kwargs["disks"][0], disk_kwargs)