root / snf-cyclades-app / synnefo / api / tests / ports.py @ 593851e0
History | View | Annotate | Download (4.1 kB)
1 |
from snf_django.utils.testing import BaseAPITest |
---|---|
2 |
from django.utils import simplejson as json |
3 |
from synnefo.cyclades_settings import cyclades_services |
4 |
from synnefo.lib.services import get_service_path |
5 |
from synnefo.lib import join_urls |
6 |
import json |
7 |
import synnefo.db.models_factory as dbmf |
8 |
|
9 |
COMPUTE_URL = get_service_path(cyclades_services, 'compute',
|
10 |
version='v2.0')
|
11 |
PORTS_URL = join_urls(COMPUTE_URL, "ports/")
|
12 |
|
13 |
|
14 |
class PortTest(BaseAPITest): |
15 |
def test_get_ports(self): |
16 |
url = join_urls(PORTS_URL) |
17 |
response = self.get(url)
|
18 |
self.assertEqual(response.status_code, 200) |
19 |
ports = json.loads(response.content) |
20 |
self.assertEqual(ports, {"ports": []}) |
21 |
|
22 |
def test_get_port_unfound(self): |
23 |
url = join_urls(PORTS_URL, "123")
|
24 |
response = self.get(url)
|
25 |
self.assertEqual(response.status_code, 404) |
26 |
|
27 |
def test_get_port(self): |
28 |
nic = dbmf.NetworkInterfaceFactory.create() |
29 |
url = join_urls(PORTS_URL, str(nic.id))
|
30 |
response = self.get(url, user=nic.network.userid)
|
31 |
self.assertEqual(response.status_code, 200) |
32 |
|
33 |
def test_delete_port(self): |
34 |
nic = dbmf.NetworkInterfaceFactory.create(device_owner='vm')
|
35 |
url = join_urls(PORTS_URL, str(nic.id))
|
36 |
response = self.delete(url, user=nic.network.userid)
|
37 |
self.assertEqual(response.status_code, 204) |
38 |
|
39 |
def test_update_port_name(self): |
40 |
nic = dbmf.NetworkInterfaceFactory.create(device_owner='vm')
|
41 |
url = join_urls(PORTS_URL, str(nic.id))
|
42 |
request = {'port': {"name": "test-name"}} |
43 |
response = self.put(url, params=json.dumps(request),
|
44 |
user=nic.network.userid) |
45 |
self.assertEqual(response.status_code, 200) |
46 |
res = json.loads(response.content) |
47 |
self.assertEqual(res['port']['name'], "test-name") |
48 |
|
49 |
def test_update_port_sg_unfound(self): |
50 |
sg1 = dbmf.SecurityGroupFactory.create() |
51 |
nic =dbmf.NetworkInterfaceFactory.create(device_owner='vm')
|
52 |
nic.security_groups.add(sg1) |
53 |
nic.save() |
54 |
url = join_urls(PORTS_URL, str(nic.id))
|
55 |
request = {'port': {"security_groups": ["123"]}} |
56 |
response = self.put(url, params=json.dumps(request),
|
57 |
user=nic.network.userid) |
58 |
self.assertEqual(response.status_code, 404) |
59 |
|
60 |
def test_update_port_sg(self): |
61 |
sg1 = dbmf.SecurityGroupFactory.create() |
62 |
sg2 = dbmf.SecurityGroupFactory.create() |
63 |
sg3 = dbmf.SecurityGroupFactory.create() |
64 |
nic = dbmf.NetworkInterfaceFactory.create(device_owner='vm')
|
65 |
nic.security_groups.add(sg1) |
66 |
nic.save() |
67 |
url = join_urls(PORTS_URL, str(nic.id))
|
68 |
request = {'port': {"security_groups": [str(sg2.id), str(sg3.id)]}} |
69 |
response = self.put(url, params=json.dumps(request),
|
70 |
user=nic.network.userid) |
71 |
res = json.loads(response.content) |
72 |
self.assertEqual(res['port']['security_groups'], |
73 |
[str(sg2.id), str(sg3.id)]) |
74 |
|
75 |
|
76 |
def test_create_port_no_network(self): |
77 |
request = { |
78 |
"port": {
|
79 |
"device_id": "123", |
80 |
"name": "port1", |
81 |
"network_id": "123" |
82 |
} |
83 |
} |
84 |
response = self.post(PORTS_URL, params=json.dumps(request))
|
85 |
self.assertEqual(response.status_code, 404) |
86 |
|
87 |
def test_create_port(self): |
88 |
net = dbmf.NetworkFactory.create() |
89 |
subnet1 = dbmf.IPv4SubnetFactory.create(network=net) |
90 |
subnet2 = dbmf.IPv6SubnetFactory.create(network=net) |
91 |
sg1 = dbmf.SecurityGroupFactory.create() |
92 |
sg2 = dbmf.SecurityGroupFactory.create() |
93 |
vm = dbmf.VirtualMachineFactory.create(userid=net.userid) |
94 |
request = { |
95 |
"port": {
|
96 |
"name": "port1", |
97 |
"network_id": str(net.id), |
98 |
"device_id": str(vm.id), |
99 |
"security_groups": [str(sg1.id), str(sg2.id)] |
100 |
} |
101 |
} |
102 |
response = self.post(PORTS_URL, params=json.dumps(request),
|
103 |
user=net.userid) |
104 |
self.assertEqual(response.status_code, 201) |