Commit cb3ee157 authored by Georgios D. Tsoukalas's avatar Georgios D. Tsoukalas
Browse files

cyclades: fix URLs in compute/networks tests

parent 501dd79b
......@@ -37,10 +37,35 @@ from mock import patch
from snf_django.utils.testing import BaseAPITest, mocked_quotaholder
from synnefo.db.models import Network, NetworkInterface
from synnefo.db import models_factory as mfactory
from synnefo.cyclades_settings import cyclades_services
from synnefo.lib.services import get_service_path
from synnefo.lib import join_urls
class ComputeAPITest(BaseAPITest):
def setUp(self, *args, **kwargs):
super(ComputeAPITest, self).setUp(*args, **kwargs)
self.compute_path = get_service_path(cyclades_services, 'compute',
version='v2.0')
def myget(self, path, *args, **kwargs):
path = join_urls(self.compute_path, path)
return self.get(path, *args, **kwargs)
def myput(self, path, *args, **kwargs):
path = join_urls(self.compute_path, path)
return self.put(path, *args, **kwargs)
def mypost(self, path, *args, **kwargs):
path = join_urls(self.compute_path, path)
return self.post(path, *args, **kwargs)
def mydelete(self, path, *args, **kwargs):
path = join_urls(self.compute_path, path)
return self.delete(path, *args, **kwargs)
@patch('synnefo.logic.rapi_pool.GanetiRapiClient')
class NetworkAPITest(BaseAPITest):
class NetworkAPITest(ComputeAPITest):
def setUp(self):
self.mac_prefixes = mfactory.MacPrefixPoolTableFactory()
self.bridges = mfactory.BridgePoolTableFactory(base="link")
......@@ -53,6 +78,7 @@ class NetworkAPITest(BaseAPITest):
machine=self.vm1)
self.net2 = mfactory.NetworkFactory(userid=self.user)
self.nic3 = mfactory.NetworkInterfaceFactory(network=self.net2)
super(NetworkAPITest, self).setUp()
def assertNetworksEqual(self, db_net, api_net, detail=False):
self.assertEqual(str(db_net.id), api_net["id"])
......@@ -75,8 +101,8 @@ class NetworkAPITest(BaseAPITest):
'network': {'name': 'foo', "type": "MAC_FILTERED"}
}
with mocked_quotaholder():
response = self.post('/api/v1.1/networks/', 'user1',
json.dumps(request), 'json')
response = self.mypost('networks/', 'user1',
json.dumps(request), 'json')
self.assertEqual(response.status_code, 202)
db_networks = Network.objects.filter(userid='user1')
self.assertEqual(len(db_networks), 1)
......@@ -91,8 +117,8 @@ class NetworkAPITest(BaseAPITest):
request = {
'network': {'name': 'foo', 'type': 'LoLo'}
}
response = self.post('/api/v1.1/networks/', 'user1',
json.dumps(request), 'json')
response = self.mypost('networks/', 'user1',
json.dumps(request), 'json')
self.assertBadRequest(response)
self.assertEqual(len(Network.objects.filter(userid='user1')), 0)
......@@ -103,8 +129,8 @@ class NetworkAPITest(BaseAPITest):
'cidr': '10.0.0.0/8', "type":
"MAC_FILTERED"}
}
response = self.post('/api/v1.1/networks/', 'user1',
json.dumps(request), 'json')
response = self.mypost('networks/', 'user1',
json.dumps(request), 'json')
self.assertFault(response, 413, "overLimit")
def test_invalid_data_3(self, mrapi):
......@@ -114,8 +140,8 @@ class NetworkAPITest(BaseAPITest):
"public": "True",
"type": "MAC_FILTERED"}
}
response = self.post('/api/v1.1/networks/', 'user1',
json.dumps(request), 'json')
response = self.mypost('networks/', 'user1',
json.dumps(request), 'json')
self.assertFault(response, 403, "forbidden")
def test_invalid_data_4(self, mrapi):
......@@ -123,8 +149,8 @@ class NetworkAPITest(BaseAPITest):
request = {
'network': {'name': 'foo', 'type': 'CUSTOM'}
}
response = self.post('/api/v1.1/networks/', 'user1',
json.dumps(request), 'json')
response = self.mypost('networks/', 'user1',
json.dumps(request), 'json')
self.assertFault(response, 403, "forbidden")
def test_invalid_subnet(self, mrapi):
......@@ -134,8 +160,8 @@ class NetworkAPITest(BaseAPITest):
'cidr': '10.0.0.10/27',
"type": "MAC_FILTERED"}
}
response = self.post('/api/v1.1/networks/', 'user1',
json.dumps(request), 'json')
response = self.mypost('networks/', 'user1',
json.dumps(request), 'json')
self.assertBadRequest(response)
def test_invalid_gateway_1(self, mrapi):
......@@ -144,8 +170,8 @@ class NetworkAPITest(BaseAPITest):
'cidr': '10.0.0.0/28',
'gateway': '10.0.0.0.300'}
}
response = self.post('/api/v1.1/networks/', 'user1',
json.dumps(request), 'json')
response = self.mypost('networks/', 'user1',
json.dumps(request), 'json')
self.assertBadRequest(response)
def test_invalid_gateway_2(self, mrapi):
......@@ -154,8 +180,8 @@ class NetworkAPITest(BaseAPITest):
'cidr': '10.0.0.0/28',
'gateway': '10.2.0.1'}
}
response = self.post('/api/v1.1/networks/', 'user1',
json.dumps(request), 'json')
response = self.mypost('networks/', 'user1',
json.dumps(request), 'json')
self.assertBadRequest(response)
def test_invalid_network6(self, mrapi):
......@@ -165,8 +191,8 @@ class NetworkAPITest(BaseAPITest):
'subnet6': '10.0.0.0/28',
'gateway': '10.2.0.1'}
}
response = self.post('/api/v1.1/networks/', 'user1',
json.dumps(request), 'json')
response = self.mypost('networks/', 'user1',
json.dumps(request), 'json')
self.assertBadRequest(response)
def test_invalid_gateway6(self, mrapi):
......@@ -176,8 +202,8 @@ class NetworkAPITest(BaseAPITest):
'subnet6': '2001:0db8:0123:4567:89ab:cdef:1234:5678',
'gateway': '10.2.0.1'}
}
response = self.post('/api/v1.1/networks/', 'user1',
json.dumps(request), 'json')
response = self.mypost('networks/', 'user1',
json.dumps(request), 'json')
self.assertBadRequest(response)
def test_list_networks(self, mrapi):
......@@ -185,7 +211,7 @@ class NetworkAPITest(BaseAPITest):
# Create a deleted network
mfactory.NetworkFactory(userid=self.user, deleted=True)
response = self.get('/api/v1.1/networks/', self.user)
response = self.myget('networks/', self.user)
self.assertSuccess(response)
db_nets = Network.objects.filter(userid=self.user, deleted=False)
......@@ -201,7 +227,7 @@ class NetworkAPITest(BaseAPITest):
# Create a deleted network
mfactory.NetworkFactory(userid=self.user, deleted=True)
response = self.get('/api/v1.1/networks/detail', self.user)
response = self.myget('networks/detail', self.user)
self.assertSuccess(response)
db_nets = Network.objects.filter(userid=self.user, deleted=False)
......@@ -218,58 +244,54 @@ class NetworkAPITest(BaseAPITest):
machine = mfactory.VirtualMachineFactory(userid=net.userid)
mfactory.NetworkInterfaceFactory(network=net, machine=machine,
state="BUILDING")
response = self.get('/api/v1.1/networks/%d' % net.id,
net.userid)
response = self.myget('networks/%d' % net.id, net.userid)
self.assertSuccess(response)
api_net = json.loads(response.content)["network"]
self.assertEqual(len(api_net["attachments"]), 0)
def test_network_details_1(self, mrapi):
"""Test that expected details for a network are returned"""
response = self.get('/api/v1.1/networks/%d' % self.net1.id,
self.net1.userid)
response = self.myget('networks/%d' % self.net1.id, self.net1.userid)
self.assertSuccess(response)
api_net = json.loads(response.content)["network"]
self.assertNetworksEqual(self.net1, api_net, detail=True)
def test_invalid_network(self, mrapi):
"""Test details for non-existing network."""
response = self.get('/api/v1.1/networks/%d' % 42,
self.net1.userid)
response = self.myget('networks/%d' % 42, self.net1.userid)
self.assertItemNotFound(response)
def test_rename_network(self, mrapi):
request = {'network': {'name': "new_name"}}
response = self.put('/api/v1.1/networks/%d' % self.net2.id,
self.net2.userid, json.dumps(request), 'json')
response = self.myput('networks/%d' % self.net2.id,
self.net2.userid, json.dumps(request), 'json')
self.assertEqual(response.status_code, 204)
self.assertEqual(Network.objects.get(id=self.net2.id).name, "new_name")
# Check invalid
request = {'name': "new_name"}
response = self.put('/api/v1.1/networks/%d' % self.net2.id,
self.net2.userid, json.dumps(request), 'json')
response = self.myput('networks/%d' % self.net2.id,
self.net2.userid, json.dumps(request), 'json')
self.assertBadRequest(response)
def test_rename_deleted_network(self, mrapi):
net = mfactory.NetworkFactory(deleted=True)
request = {'network': {'name': "new_name"}}
response = self.put('/api/v1.1/networks/%d' % net.id,
net.userid, json.dumps(request), 'json')
response = self.myput('networks/%d' % net.id,
net.userid, json.dumps(request), 'json')
self.assertBadRequest(response)
def test_rename_public_network(self, mrapi):
net = mfactory.NetworkFactory(public=True)
request = {'network': {'name': "new_name"}}
response = self.put('/api/v1.1/networks/%d' % net.id,
self.net2.userid, json.dumps(request), 'json')
response = self.myput('networks/%d' % net.id,
self.net2.userid, json.dumps(request), 'json')
self.assertFault(response, 403, 'forbidden')
def test_delete_network(self, mrapi):
net = mfactory.NetworkFactory(deleted=False, state='ACTIVE',
link="link-10")
with mocked_quotaholder():
response = self.delete('/api/v1.1/networks/%d' % net.id,
net.userid)
response = self.mydelete('networks/%d' % net.id, net.userid)
self.assertEqual(response.status_code, 204)
net = Network.objects.get(id=net.id, userid=net.userid)
self.assertEqual(net.action, 'DESTROY')
......@@ -277,21 +299,18 @@ class NetworkAPITest(BaseAPITest):
def test_delete_public_network(self, mrapi):
net = mfactory.NetworkFactory(public=True)
response = self.delete('/api/v1.1/networks/%d' % net.id,
self.net2.userid)
response = self.mydelete('networks/%d' % net.id, self.net2.userid)
self.assertFault(response, 403, 'forbidden')
self.assertFalse(mrapi.called)
def test_delete_deleted_network(self, mrapi):
net = mfactory.NetworkFactory(deleted=True)
response = self.delete('/api/v1.1/networks/%d' % net.id,
net.userid)
response = self.mydelete('networks/%d' % net.id, net.userid)
self.assertBadRequest(response)
def test_delete_network_in_use(self, mrapi):
net = self.net1
response = self.delete('/api/v1.1/networks/%d' % net.id,
net.userid)
response = self.mydelete('networks/%d' % net.id, net.userid)
self.assertFault(response, 421, 'networkInUse')
self.assertFalse(mrapi.called)
......@@ -300,8 +319,8 @@ class NetworkAPITest(BaseAPITest):
vm = mfactory.VirtualMachineFactory(name='yo', userid=user)
net = mfactory.NetworkFactory(state='ACTIVE', userid=user)
request = {'add': {'serverRef': vm.id}}
response = self.post('/api/v1.1/networks/%d/action' % net.id,
net.userid, json.dumps(request), 'json')
response = self.mypost('networks/%d/action' % net.id,
net.userid, json.dumps(request), 'json')
self.assertEqual(response.status_code, 202)
def test_add_nic_to_deleted_network(self, mrapi):
......@@ -310,8 +329,8 @@ class NetworkAPITest(BaseAPITest):
net = mfactory.NetworkFactory(state='ACTIVE', userid=user,
deleted=True)
request = {'add': {'serverRef': vm.id}}
response = self.post('/api/v1.1/networks/%d/action' % net.id,
net.userid, json.dumps(request), 'json')
response = self.mypost('networks/%d/action' % net.id,
net.userid, json.dumps(request), 'json')
self.assertBadRequest(response)
self.assertFalse(mrapi.called)
......@@ -320,8 +339,8 @@ class NetworkAPITest(BaseAPITest):
vm = mfactory.VirtualMachineFactory(name='yo', userid=user)
net = mfactory.NetworkFactory(state='ACTIVE', userid=user, public=True)
request = {'add': {'serverRef': vm.id}}
response = self.post('/api/v1.1/networks/%d/action' % net.id,
net.userid, json.dumps(request), 'json')
response = self.mypost('networks/%d/action' % net.id,
net.userid, json.dumps(request), 'json')
self.assertFault(response, 403, 'forbidden')
self.assertFalse(mrapi.called)
......@@ -330,8 +349,8 @@ class NetworkAPITest(BaseAPITest):
vm = mfactory.VirtualMachineFactory(name='yo', userid=user)
net = mfactory.NetworkFactory(state='ACTIVE', userid=user)
request = {'add': {'serveRef': vm.id}}
response = self.post('/api/v1.1/networks/%d/action' % net.id,
net.userid, json.dumps(request), 'json')
response = self.mypost('networks/%d/action' % net.id,
net.userid, json.dumps(request), 'json')
self.assertBadRequest(response)
self.assertFalse(mrapi.called)
......@@ -340,8 +359,8 @@ class NetworkAPITest(BaseAPITest):
vm = mfactory.VirtualMachineFactory(name='yo', userid=user)
net = mfactory.NetworkFactory(state='ACTIVE', userid=user)
request = {'add': {'serveRef': [vm.id, 22]}}
response = self.post('/api/v1.1/networks/%d/action' % net.id,
net.userid, json.dumps(request), 'json')
response = self.mypost('networks/%d/action' % net.id,
net.userid, json.dumps(request), 'json')
self.assertBadRequest(response)
self.assertFalse(mrapi.called)
......@@ -352,8 +371,8 @@ class NetworkAPITest(BaseAPITest):
net = mfactory.NetworkFactory(state='PENDING', subnet='10.0.0.0/31',
userid=user)
request = {'add': {'serveRef': vm.id}}
response = self.post('/api/v1.1/networks/%d/action' % net.id,
net.userid, json.dumps(request), 'json')
response = self.mypost('networks/%d/action' % net.id,
net.userid, json.dumps(request), 'json')
# Test that returns BuildInProgress
self.assertEqual(response.status_code, 409)
self.assertFalse(mrapi.called)
......@@ -371,8 +390,8 @@ class NetworkAPITest(BaseAPITest):
pool = net.get_pool()
self.assertTrue(pool.empty())
request = {'add': {'serverRef': vm.id}}
response = self.post('/api/v1.1/networks/%d/action' % net.id,
net.userid, json.dumps(request), 'json')
response = self.mypost('networks/%d/action' % net.id,
net.userid, json.dumps(request), 'json')
# Test that returns OverLimit
self.assertEqual(response.status_code, 413)
self.assertFalse(mrapi.called)
......@@ -383,13 +402,13 @@ class NetworkAPITest(BaseAPITest):
net = mfactory.NetworkFactory(state='ACTIVE', userid=user)
nic = mfactory.NetworkInterfaceFactory(machine=vm, network=net)
request = {'remove': {'attachment': 'nic-%s-%s' % (vm.id, nic.index)}}
response = self.post('/api/v1.1/networks/%d/action' % net.id,
net.userid, json.dumps(request), 'json')
response = self.mypost('networks/%d/action' % net.id,
net.userid, json.dumps(request), 'json')
self.assertEqual(response.status_code, 202)
self.assertTrue(NetworkInterface.objects.get(id=nic.id).dirty)
# Remove dirty nic
response = self.post('/api/v1.1/networks/%d/action' % net.id,
net.userid, json.dumps(request), 'json')
response = self.mypost('networks/%d/action' % net.id,
net.userid, json.dumps(request), 'json')
self.assertFault(response, 409, 'buildInProgress')
def test_remove_nic_malformed(self, mrapi):
......@@ -400,8 +419,8 @@ class NetworkAPITest(BaseAPITest):
request = {'remove':
{'att234achment': 'nic-%s-%s' % (vm.id, nic.index)}
}
response = self.post('/api/v1.1/networks/%d/action' % net.id,
net.userid, json.dumps(request), 'json')
response = self.mypost('networks/%d/action' % net.id,
net.userid, json.dumps(request), 'json')
self.assertBadRequest(response)
def test_remove_nic_malformed_2(self, mrapi):
......@@ -411,6 +430,6 @@ class NetworkAPITest(BaseAPITest):
request = {'remove':
{'attachment': 'nic-%s' % vm.id}
}
response = self.post('/api/v1.1/networks/%d/action' % net.id,
net.userid, json.dumps(request), 'json')
response = self.mypost('networks/%d/action' % net.id,
net.userid, json.dumps(request), 'json')
self.assertBadRequest(response)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment