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_server_detail(self): """Test if a server details are returned.""" db_vm = self.vm2 user = self.vm2.userid net = mfactory.NetworkFactory() nic = mfactory.NetworkInterfaceFactory(machine=self.vm2, network=net, ipv6="::babe") 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'], nic.ipv4) self.assertEqual(api_nic['ipv6'], nic.ipv6) self.assertEqual(api_nic['id'], 'nic-%s-%s' % (db_vm.id, nic.index)) api_address = server["addresses"] self.assertEqual(api_address[str(net.id)], [{ "version": 4, "addr": nic.ipv4, "OS-EXT-IPS:type": "fixed" }, { "version": 6, "addr": nic.ipv6, "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_get_metadata(self): vm = self.vm create_meta = lambda: mfactory.VirtualMachineMetadataFactory(vm=vm) metadata = [create_meta(), create_meta(), create_meta()] response = self.myget('servers/%d/metadata' % vm.id, vm.userid) self.assertTrue(response.status_code in [200, 203]) api_metadata = json.loads(response.content)['metadata'] self.assertEqual(len(api_metadata), len(metadata) + 1) for db_m in metadata: self.assertEqual(api_metadata[db_m.meta_key], db_m.meta_value) request = {'metadata': {'foo': 'bar'}, metadata[0].meta_key: 'bar2'} response = self.mypost('servers/%d/metadata' % vm.id, vm.userid, json.dumps(request), 'json') metadata2 = VirtualMachineMetadata.objects.filter(vm=vm) response = self.myget('servers/%d/metadata' % vm.id, vm.userid) self.assertTrue(response.status_code in [200, 203]) api_metadata2 = json.loads(response.content)['metadata'] self.assertTrue('foo' in api_metadata2.keys()) self.assertTrue(api_metadata2[metadata[0].meta_key], 'bar2') self.assertEqual(len(api_metadata2), len(metadata2)) for db_m in metadata2: self.assertEqual(api_metadata2[db_m.meta_key], db_m.meta_value) # Create new meta request = {'meta': {'foo2': 'bar2'}} response = self.myput('servers/%d/metadata/foo2' % vm.id, vm.userid, json.dumps(request), 'json') # Get the new meta response = self.myget('servers/%d/metadata/foo2' % vm.id, vm.userid) meta = json.loads(response.content)['meta'] self.assertEqual(meta['foo2'], 'bar2') # Delete the new meta response = self.mydelete('servers/%d/metadata/foo2' % vm.id, vm.userid) self.assertEqual(response.status_code, 204) # Try to get the deleted meta: should raise 404 response = self.myget('servers/%d/metadata/foo2' % vm.id, vm.userid) self.assertEqual(response.status_code, 404)
def setUp(self): self.vm = mfactory.VirtualMachineFactory() self.metadata = mfactory.VirtualMachineMetadataFactory(vm=self.vm) super(ServerMetadataAPITest, self).setUp()