def test_floating_ip_holdings(self): mfactory.IPv4AddressFactory(userid="user1", floating_ip=True) mfactory.IPv4AddressFactory(userid="user1", floating_ip=True) mfactory.IPv4AddressFactory(userid="user2", floating_ip=True) mfactory.IPv4AddressFactory(userid="user3", floating_ip=True) holdings = util.get_db_holdings() self.assertEqual(holdings["user1"]["user1"]["cyclades.floating_ip"], 2) self.assertEqual(holdings["user2"]["user2"]["cyclades.floating_ip"], 1) self.assertEqual(holdings["user3"]["user3"]["cyclades.floating_ip"], 1)
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_server_port_forwarding(self): vm = mfactory.VirtualMachineFactory() # test None if the server has no public IP ports = { 22: ("foo", 61000), 80: lambda ip, id, fqdn, user: ("bar", 61001) } with override_settings(settings, CYCLADES_PORT_FORWARDING=ports): response = self.myget("servers/%d" % vm.id, vm.userid) server = json.loads(response.content)['server'] self.assertEqual(server["SNF:port_forwarding"], {}) # Add with public IP mfactory.IPv4AddressFactory(nic__machine=vm, network__public=True) with override_settings(settings, CYCLADES_PORT_FORWARDING=ports): response = self.myget("servers/%d" % vm.id, vm.userid) server = json.loads(response.content)['server'] self.assertEqual( server["SNF:port_forwarding"], { "22": { "host": "foo", "port": "61000" }, "80": { "host": "bar", "port": "61001" } }) def _port_from_ip(ip, base): fields = ip.split('.', 4) return (base + 256 * int(fields[2]) + int(fields[3])) ports = { 22: lambda ip, id, fqdn, user: ip and ("gate", _port_from_ip(ip, 10000)) or None } vm = mfactory.VirtualMachineFactory() with override_settings(settings, CYCLADES_PORT_FORWARDING=ports): response = self.myget("servers/%d" % vm.id, vm.userid) server = json.loads(response.content)['server'] self.assertEqual(server["SNF:port_forwarding"], {}) mfactory.IPv4AddressFactory(nic__machine=vm, network__public=True, address="192.168.2.2") with override_settings(settings, CYCLADES_PORT_FORWARDING=ports): response = self.myget("servers/%d" % vm.id, vm.userid) server = json.loads(response.content)['server'] self.assertEqual(server["SNF:port_forwarding"], {"22": { "host": "gate", "port": "10514" }})
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 setUp(self): settings.SKIP_SSH_VALIDATION = True settings.HELPDESK_ENABLED = True self.client = AuthClient() # init models vm1u1 = mfactory.VirtualMachineFactory(userid=USER1, name="user1 vm", pk=1001) vm1u2 = mfactory.VirtualMachineFactory(userid=USER2, name="user2 vm1", pk=1002) vm2u2 = mfactory.VirtualMachineFactory(userid=USER2, name="user2 vm2", pk=1003) nic1 = mfactory.NetworkInterfaceFactory(machine=vm1u2, userid=vm1u2.userid, network__public=False, network__userid=USER1) ip2 = mfactory.IPv4AddressFactory(nic__machine=vm1u1, userid=vm1u1.userid, network__public=True, network__userid=None, address="195.251.222.211") mfactory.IPAddressLogFactory(address=ip2.address, server_id=vm1u1.id, network_id=ip2.network.id, active=True)
def get_ip_or_404_helper(self, get_model_or_404): for ip_version in ['ipv4', 'ipv6']: model = getattr(self, ip_version, None) returned_model = get_model_or_404(model.id) self.assertEqual(model, returned_model) with self.assertRaises(AdminHttp404): get_model_or_404(gibberish(like='number')) returned_model = get_model_or_404(model.address) self.assertEqual(model, returned_model) with self.assertRaises(AdminHttp404): get_model_or_404(gibberish()) self.ipv4_2 = mf.IPv4AddressFactory(address="1.2.3.4") self.ipv6_2 = mf.IPv6AddressFactory(address="::1") for ip_version in ['ipv4', 'ipv6']: model = getattr(self, ip_version, None) returned_model = get_model_or_404(model.id) self.assertEqual(model, returned_model) with self.assertRaises(AdminHttp404): returned_model = get_model_or_404(model.address)
def test_sharing(self): ip = mf.IPv4AddressFactory(userid="user1", floating_ip=True) action = { "reassign": { "shared_to_project": True, "project": "project1" } } response = self.post(URL + "/%s/action" % ip.id, "user1", json.dumps(action)) self.assertSuccess(response) response = self.delete(URL + "/%s" % ip.id, "user2", _projects=["user2", "project1"]) # 409 instead of 404 self.assertEqual(response.status_code, 409) action['reassign']['shared_to_project'] = False response = self.post(URL + "/%s/action" % ip.id, "user2", json.dumps(action)) self.assertItemNotFound(response) action['reassign']['shared_to_project'] = False response = self.post(URL + "/%s/action" % ip.id, "user1", json.dumps(action)) self.assertSuccess(response) response = self.delete(URL + "/%s" % ip.id, "user2", _projects=["user2", "project1"]) self.assertItemNotFound(response)
def test_release(self): ip = mf.IPv4AddressFactory(userid="user1", floating_ip=True, nic=None) with mocked_quotaholder(): response = self.delete(URL + "/%s" % ip.id, ip.userid) self.assertSuccess(response) ips_after = floating_ips.filter(id=ip.id) self.assertEqual(len(ips_after), 0)
def test_create_server_with_port(self, mrapi): # Test invalid networks request = deepcopy(self.request) request["server"]["networks"] = {"foo": "lala"} with override_settings(settings, **self.network_settings): response = self.mypost("servers", "dummy_user", json.dumps(request), 'json') self.assertBadRequest(response) request["server"]["networks"] = ['1', '2'] with override_settings(settings, **self.network_settings): response = self.mypost("servers", "dummy_user", json.dumps(request), 'json') self.assertBadRequest(response) mrapi().CreateInstance.return_value = 42 ip = mfactory.IPv4AddressFactory(nic__machine=None) port1 = ip.nic request = deepcopy(self.request) request["server"]["networks"] = [{"port": port1.id}] with override_settings(settings, **self.network_settings): with mocked_quotaholder(): response = self.mypost("servers", port1.userid, json.dumps(request), 'json') self.assertEqual(response.status_code, 202) vm_id = json.loads(response.content)["server"]["id"] port1 = NetworkInterface.objects.get(id=port1.id) self.assertEqual(port1.machine_id, vm_id) # 409 if already used with override_settings(settings, **self.network_settings): with mocked_quotaholder(): response = self.mypost("servers", port1.userid, json.dumps(request), 'json') self.assertConflict(response) # Test permissions ip = mfactory.IPv4AddressFactory(userid="user1", nic__userid="user1") port2 = ip.nic request["server"]["networks"] = [{"port": port2.id}] with override_settings(settings, **self.network_settings): with mocked_quotaholder(): response = self.mypost("servers", "user2", json.dumps(request), 'json') self.assertEqual(response.status_code, 404)
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_get_ip(self): ip = mf.IPv4AddressFactory(userid="user1", floating_ip=True) with mocked_quotaholder(): response = self.get(URL + "/%s" % ip.id, "user1") self.assertSuccess(response) api_ip = json.loads(response.content)["floatingip"] self.assertEqual(api_ip, {"instance_id": str(ip.nic.machine_id), "floating_ip_address": ip.address, "fixed_ip_address": None, "id": str(ip.id), "port_id": str(ip.nic.id), "deleted": False, "floating_network_id": str(ip.network_id), "tenant_id": ip.userid, "user_id": ip.userid})
def setUp(self): """Common setUp for all AdminTestCases. This setUp method will create and approve a project as well as add several Cyclades model instances using Cyclades' model_factory. """ ProjectAPITest.setUp(self) # Create a simple project. h_owner = {"HTTP_X_AUTH_TOKEN": self.user1.auth_token} app1 = { "name": "test.pr", "description": u"δεσκρίπτιον", "end_date": "2113-5-5T20:20:20Z", "join_policy": "auto", "max_members": 5, "resources": { u"σέρβις1.ρίσορς11": { "project_capacity": 1024, "member_capacity": 512 } } } status, body = self.create(app1, h_owner) # Ensure that the project application has been created. self.assertEqual(status, 201) project_id = body["id"] app_id = body["application"] # Approve the application. with transaction.atomic(): self.project = approve_application(app_id, project_id) self.assertIsNotNone(self.project) # Alias owner user with a generic name self.user = self.user1 # Create cyclades models. self.vm = mf.VirtualMachineFactory() self.volume = mf.VolumeFactory() self.network = mf.NetworkFactory() self.ipv4 = mf.IPv4AddressFactory(address="1.2.3.4") self.ipv6 = mf.IPv6AddressFactory(address="::1")
def test_server_detail(self): """Test if a server details are returned.""" db_vm = self.vm2 user = self.vm2.userid ip4 = mfactory.IPv4AddressFactory(nic__machine=self.vm2) nic = ip4.nic net = ip4.network ip6 = mfactory.IPv6AddressFactory(nic=nic, network=net) nic.mac = "aa:00:11:22:33:44" nic.save() db_vm_meta = mfactory.VirtualMachineMetadataFactory(vm=db_vm) response = self.myget('servers/%d' % db_vm.id, user) server = json.loads(response.content)['server'] self.assertEqual(server['flavor']["id"], db_vm.flavor.id) self.assertEqual(server['hostId'], db_vm.hostid) self.assertEqual(server['id'], db_vm.id) self.assertEqual(server['image']["id"], db_vm.imageid) self.assertEqual(server['name'], db_vm.name) self.assertEqual(server['status'], get_rsapi_state(db_vm)) api_nic = server['attachments'][0] self.assertEqual(api_nic['network_id'], str(net.id)) self.assertEqual(api_nic['mac_address'], nic.mac) self.assertEqual(api_nic['firewallProfile'], nic.firewall_profile) self.assertEqual(api_nic['ipv4'], ip4.address) self.assertEqual(api_nic['ipv6'], ip6.address) self.assertEqual(api_nic['OS-EXT-IPS:type'], "fixed") self.assertEqual(api_nic['id'], nic.id) api_address = server["addresses"] self.assertEqual(api_address[str(net.id)], [{ "version": 4, "addr": ip4.address, "OS-EXT-IPS:type": "fixed" }, { "version": 6, "addr": ip6.address, "OS-EXT-IPS:type": "fixed" }]) metadata = server['metadata'] self.assertEqual(len(metadata), 1) self.assertEqual(metadata[db_vm_meta.meta_key], db_vm_meta.meta_value) self.assertSuccess(response)
def test_delete_network_with_floating_ips(self): ip = mf.IPv4AddressFactory(userid="user1", floating_ip=True, network=self.pool, nic=None) # Mark the network as non-pubic to not get 403 network = ip.network network.public = False network.save() # Cannot remove network with floating IPs with mocked_quotaholder(): response = self.delete(NETWORKS_URL + "/%s" % self.pool.id, self.pool.userid) self.assertConflict(response) # But we can with only deleted Floating Ips ip.deleted = True ip.save() with mocked_quotaholder(): response = self.delete(NETWORKS_URL + "/%s" % self.pool.id, self.pool.userid) self.assertSuccess(response)
def test_unsynced_nics(self, mrapi): network1 = mfactory.NetworkWithSubnetFactory( subnet__cidr="10.0.0.0/24", subnet__gateway="10.0.0.2") network2 = mfactory.NetworkWithSubnetFactory( subnet__cidr="192.168.2.0/24", subnet__gateway="192.168.2.2") vm1 = mfactory.VirtualMachineFactory(backend=self.backend, deleted=False, operstate="STOPPED") subnet = network1.subnets.get(ipversion=4) ip = mfactory.IPv4AddressFactory(nic__machine=vm1, network=network1, subnet=subnet, address="10.0.0.3") nic = ip.nic mrapi().GetInstances.return_value =\ [{"name": vm1.backend_vm_id, "beparams": {"maxmem": 2048, "minmem": 2048, "vcpus": 4}, "oper_state": True, "mtime": time(), "disk.sizes": [], "disk.names": [], "disk.uuids": [], "nic.names": [nic.backend_uuid], "nic.ips": ["192.168.2.5"], "nic.macs": ["aa:00:bb:cc:dd:ee"], "nic.networks.names": [network2.backend_id], "tags": []}] with mocked_quotaholder(): self.reconciler.reconcile() vm1 = VirtualMachine.objects.get(id=vm1.id) self.assertEqual(vm1.operstate, "STARTED") nic = vm1.nics.all()[0] self.assertEqual(nic.network, network2) self.assertEqual(nic.ipv4_address, "192.168.2.5") self.assertEqual(nic.mac, "aa:00:bb:cc:dd:ee")
def test_create_network_info(self, mrapi): mrapi().CreateInstance.return_value = 12 # User requested private networks s1 = mfactory.IPv4SubnetFactory(network__userid="test") s2 = mfactory.IPv6SubnetFactory(network__userid="test") # and a public IPv6 request = deepcopy(self.request) request["server"]["networks"] = [{ "uuid": s1.network_id }, { "uuid": s2.network_id }] with override_settings(settings, **self.network_settings): with mocked_quotaholder(): response = self.mypost('servers', "test", json.dumps(request), 'json') self.assertEqual(response.status_code, 202) name, args, kwargs = mrapi().CreateInstance.mock_calls[0] self.assertEqual(len(kwargs["nics"]), 3) self.assertEqual(kwargs["nics"][0]["network"], self.net6.backend_id) self.assertEqual(kwargs["nics"][1]["network"], s1.network.backend_id) self.assertEqual(kwargs["nics"][2]["network"], s2.network.backend_id) # but fail if others user network s3 = mfactory.IPv6SubnetFactory(network__userid="test_other") request = deepcopy(self.request) request["server"]["networks"] = [{"uuid": s3.network_id}] response = self.mypost('servers', "test", json.dumps(request), 'json') self.assertEqual(response.status_code, 404) # User requested public networks # but no floating IP.. s1 = mfactory.IPv4SubnetFactory(network__public=True) request = deepcopy(self.request) request["server"]["networks"] = [{"uuid": s1.network_id}] response = self.mypost('servers', "test", json.dumps(request), 'json') self.assertEqual(response.status_code, 409) # Add one floating IP fp1 = mfactory.IPv4AddressFactory(userid="test", subnet=s1, network=s1.network, floating_ip=True, nic=None) self.assertEqual(fp1.nic, None) request = deepcopy(self.request) request["server"]["networks"] = [{ "uuid": s1.network_id, "fixed_ip": fp1.address }] with mocked_quotaholder(): with override_settings(settings, **self.network_settings): response = self.mypost('servers', "test", json.dumps(request), 'json') self.assertEqual(response.status_code, 202) server_id = json.loads(response.content)["server"]["id"] fp1 = IPAddress.objects.get(id=fp1.id) self.assertEqual(fp1.nic.machine_id, server_id) # check used floating IP response = self.mypost('servers', "test", json.dumps(request), 'json') self.assertEqual(response.status_code, 409) # Add more floating IP. but check auto-reserve fp2 = mfactory.IPv4AddressFactory(userid="test", subnet=s1, network=s1.network, floating_ip=True, nic=None) self.assertEqual(fp2.nic, None) request = deepcopy(self.request) request["server"]["networks"] = [{"uuid": s1.network_id}] with mocked_quotaholder(): with override_settings(settings, **self.network_settings): response = self.mypost('servers', "test", json.dumps(request), 'json') self.assertEqual(response.status_code, 202) server_id = json.loads(response.content)["server"]["id"] fp2 = IPAddress.objects.get(id=fp2.id) self.assertEqual(fp2.nic.machine_id, server_id) name, args, kwargs = mrapi().CreateInstance.mock_calls[-1] self.assertEqual(len(kwargs["nics"]), 2) self.assertEqual(kwargs["nics"][0]["network"], self.net6.backend_id) self.assertEqual(kwargs["nics"][1]["network"], fp2.network.backend_id)
def test_wrong_user(self): ip = mf.IPv4AddressFactory(userid="user1", floating_ip=True) response = self.delete(URL + "/%s" % ip.id, "user2") self.assertItemNotFound(response)
def test_update(self): ip = mf.IPv4AddressFactory(userid="user1", floating_ip=True, nic=None) with mocked_quotaholder(): response = self.put(URL + "/%s" % ip.id, ip.userid) self.assertEqual(response.status_code, 501)
def test_delete_network_with_floating_ips(self): test_net = dbmf.NetworkFactory() dbmf.IPv4AddressFactory(network=test_net, floating_ip=True, nic=None) url = join_urls(NETWORKS_URL, str(test_net.id)) response = self.delete(url, user=test_net.userid) self.assertEqual(response.status_code, 409)