Revision f32e8021 snf-cyclades-app/synnefo/api/tests/ports.py
b/snf-cyclades-app/synnefo/api/tests/ports.py | ||
---|---|---|
31 | 31 |
# interpreted as representing official policies, either expressed |
32 | 32 |
# or implied, of GRNET S.A.i |
33 | 33 |
|
34 |
from snf_django.utils.testing import BaseAPITest |
|
34 |
from django.conf import settings |
|
35 |
from snf_django.utils.testing import BaseAPITest, override_settings |
|
35 | 36 |
from django.utils import simplejson as json |
36 | 37 |
from synnefo.cyclades_settings import cyclades_services |
37 | 38 |
from synnefo.lib.services import get_service_path |
... | ... | |
58 | 59 |
self.assertEqual(response.status_code, 404) |
59 | 60 |
|
60 | 61 |
def test_get_port(self): |
61 |
nic = dbmf.NetworkInterfaceFactory.create()
|
|
62 |
nic = dbmf.NetworkInterfaceFactory() |
|
62 | 63 |
url = join_urls(PORTS_URL, str(nic.id)) |
63 | 64 |
response = self.get(url, user=nic.network.userid) |
64 | 65 |
self.assertEqual(response.status_code, 200) |
65 | 66 |
|
66 | 67 |
@patch("synnefo.db.models.get_rapi_client") |
67 | 68 |
def test_delete_port(self, mrapi): |
68 |
nic = dbmf.NetworkInterfaceFactory.create(device_owner='vm')
|
|
69 |
nic = dbmf.NetworkInterfaceFactory(device_owner='vm') |
|
69 | 70 |
url = join_urls(PORTS_URL, str(nic.id)) |
70 | 71 |
mrapi().ModifyInstance.return_value = 42 |
71 |
response = self.delete(url, user=nic.network.userid) |
|
72 |
with override_settings(settings, GANETI_USE_HOTPLUG=True): |
|
73 |
response = self.delete(url, user=nic.network.userid) |
|
72 | 74 |
self.assertEqual(response.status_code, 204) |
75 |
with override_settings(settings, GANETI_USE_HOTPLUG=False): |
|
76 |
response = self.delete(url, user=nic.network.userid) |
|
77 |
self.assertEqual(response.status_code, 400) |
|
73 | 78 |
|
74 | 79 |
def test_delete_port_from_nonvm(self): |
75 |
nic = dbmf.NetworkInterfaceFactory.create(device_owner='router')
|
|
80 |
nic = dbmf.NetworkInterfaceFactory(device_owner='router') |
|
76 | 81 |
url = join_urls(PORTS_URL, str(nic.id)) |
77 | 82 |
response = self.delete(url, user=nic.network.userid) |
78 | 83 |
self.assertEqual(response.status_code, 400) |
... | ... | |
83 | 88 |
self.assertItemNotFound(response) |
84 | 89 |
|
85 | 90 |
def test_update_port_name(self): |
86 |
nic = dbmf.NetworkInterfaceFactory.create(device_owner='vm')
|
|
91 |
nic = dbmf.NetworkInterfaceFactory(device_owner='vm') |
|
87 | 92 |
url = join_urls(PORTS_URL, str(nic.id)) |
88 | 93 |
request = {'port': {"name": "test-name"}} |
89 | 94 |
response = self.put(url, params=json.dumps(request), |
... | ... | |
93 | 98 |
self.assertEqual(res['port']['name'], "test-name") |
94 | 99 |
|
95 | 100 |
def test_update_port_sg_unfound(self): |
96 |
sg1 = dbmf.SecurityGroupFactory.create()
|
|
97 |
nic = dbmf.NetworkInterfaceFactory.create(device_owner='vm')
|
|
101 |
sg1 = dbmf.SecurityGroupFactory() |
|
102 |
nic = dbmf.NetworkInterfaceFactory(device_owner='vm') |
|
98 | 103 |
nic.security_groups.add(sg1) |
99 | 104 |
nic.save() |
100 | 105 |
url = join_urls(PORTS_URL, str(nic.id)) |
... | ... | |
104 | 109 |
self.assertEqual(response.status_code, 404) |
105 | 110 |
|
106 | 111 |
def test_update_port_sg(self): |
107 |
sg1 = dbmf.SecurityGroupFactory.create()
|
|
108 |
sg2 = dbmf.SecurityGroupFactory.create()
|
|
109 |
sg3 = dbmf.SecurityGroupFactory.create()
|
|
110 |
nic = dbmf.NetworkInterfaceFactory.create(device_owner='vm')
|
|
112 |
sg1 = dbmf.SecurityGroupFactory() |
|
113 |
sg2 = dbmf.SecurityGroupFactory() |
|
114 |
sg3 = dbmf.SecurityGroupFactory() |
|
115 |
nic = dbmf.NetworkInterfaceFactory(device_owner='vm') |
|
111 | 116 |
nic.security_groups.add(sg1) |
112 | 117 |
nic.save() |
113 | 118 |
url = join_urls(PORTS_URL, str(nic.id)) |
... | ... | |
131 | 136 |
|
132 | 137 |
@patch("synnefo.db.models.get_rapi_client") |
133 | 138 |
def test_create_port_private_net(self, mrapi): |
134 |
net = dbmf.NetworkFactory.create(public=False)
|
|
135 |
dbmf.IPv4SubnetFactory.create(network=net)
|
|
136 |
dbmf.IPv6SubnetFactory.create(network=net)
|
|
137 |
sg1 = dbmf.SecurityGroupFactory.create()
|
|
138 |
sg2 = dbmf.SecurityGroupFactory.create()
|
|
139 |
vm = dbmf.VirtualMachineFactory.create(userid=net.userid)
|
|
139 |
net = dbmf.NetworkFactory(public=False) |
|
140 |
dbmf.IPv4SubnetFactory(network=net) |
|
141 |
dbmf.IPv6SubnetFactory(network=net) |
|
142 |
sg1 = dbmf.SecurityGroupFactory() |
|
143 |
sg2 = dbmf.SecurityGroupFactory() |
|
144 |
vm = dbmf.VirtualMachineFactory(userid=net.userid) |
|
140 | 145 |
request = { |
141 | 146 |
"port": { |
142 | 147 |
"name": "port1", |
... | ... | |
146 | 151 |
} |
147 | 152 |
} |
148 | 153 |
mrapi().ModifyInstance.return_value = 42 |
149 |
response = self.post(PORTS_URL, params=json.dumps(request), |
|
150 |
user=net.userid) |
|
154 |
with override_settings(settings, GANETI_USE_HOTPLUG=False): |
|
155 |
response = self.post(PORTS_URL, params=json.dumps(request), |
|
156 |
user=net.userid) |
|
157 |
self.assertEqual(response.status_code, 400) |
|
158 |
with override_settings(settings, GANETI_USE_HOTPLUG=True): |
|
159 |
response = self.post(PORTS_URL, params=json.dumps(request), |
|
160 |
user=net.userid) |
|
151 | 161 |
self.assertEqual(response.status_code, 201) |
152 | 162 |
|
153 | 163 |
def test_create_port_public_net_no_ip(self): |
154 |
net = dbmf.NetworkFactory.create(public=True)
|
|
155 |
vm = dbmf.VirtualMachineFactory.create(userid=net.userid)
|
|
164 |
net = dbmf.NetworkFactory(public=True) |
|
165 |
vm = dbmf.VirtualMachineFactory(userid=net.userid) |
|
156 | 166 |
request = { |
157 | 167 |
"port": { |
158 | 168 |
"name": "port1", |
... | ... | |
165 | 175 |
self.assertEqual(response.status_code, 400) |
166 | 176 |
|
167 | 177 |
def test_create_port_public_net_wrong_ip(self): |
168 |
net = dbmf.NetworkFactory.create(public=True)
|
|
169 |
vm = dbmf.VirtualMachineFactory.create(userid=net.userid)
|
|
178 |
net = dbmf.NetworkFactory(public=True) |
|
179 |
vm = dbmf.VirtualMachineFactory(userid=net.userid) |
|
170 | 180 |
request = { |
171 | 181 |
"port": { |
172 | 182 |
"name": "port1", |
... | ... | |
180 | 190 |
self.assertEqual(response.status_code, 404) |
181 | 191 |
|
182 | 192 |
def test_create_port_public_net_conflict(self): |
183 |
net = dbmf.NetworkFactory.create(public=True)
|
|
193 |
net = dbmf.NetworkFactory(public=True) |
|
184 | 194 |
fip = dbmf.FloatingIPFactory(nic=None, userid=net.userid) |
185 |
vm = dbmf.VirtualMachineFactory.create(userid=net.userid)
|
|
195 |
vm = dbmf.VirtualMachineFactory(userid=net.userid) |
|
186 | 196 |
request = { |
187 | 197 |
"port": { |
188 | 198 |
"name": "port1", |
... | ... | |
196 | 206 |
self.assertEqual(response.status_code, 409) |
197 | 207 |
|
198 | 208 |
def test_create_port_public_net_taken_ip(self): |
199 |
net = dbmf.NetworkFactory.create(public=True)
|
|
209 |
net = dbmf.NetworkFactory(public=True) |
|
200 | 210 |
fip = dbmf.FloatingIPFactory(network=net, userid=net.userid) |
201 |
vm = dbmf.VirtualMachineFactory.create(userid=net.userid)
|
|
211 |
vm = dbmf.VirtualMachineFactory(userid=net.userid) |
|
202 | 212 |
request = { |
203 | 213 |
"port": { |
204 | 214 |
"name": "port1", |
... | ... | |
209 | 219 |
} |
210 | 220 |
response = self.post(PORTS_URL, params=json.dumps(request), |
211 | 221 |
user=net.userid) |
212 |
self.assertEqual(response.status_code, 400)
|
|
222 |
self.assertEqual(response.status_code, 409)
|
|
213 | 223 |
|
214 | 224 |
@patch("synnefo.db.models.get_rapi_client") |
215 |
def test_create_port_public_net(self, mrapi):
|
|
216 |
net = dbmf.NetworkFactory.create(public=True)
|
|
217 |
fip = dbmf.FloatingIPFactory(network=net, nic=None, userid=net.userid)
|
|
218 |
vm = dbmf.VirtualMachineFactory.create(userid=net.userid)
|
|
225 |
def test_create_port_with_floating_ip(self, mrapi):
|
|
226 |
vm = dbmf.VirtualMachineFactory()
|
|
227 |
fip = dbmf.FloatingIPFactory(network__public=True, nic=None,
|
|
228 |
userid=vm.userid)
|
|
219 | 229 |
request = { |
220 | 230 |
"port": { |
221 | 231 |
"name": "port1", |
222 |
"network_id": str(net.id),
|
|
232 |
"network_id": str(fip.network_id),
|
|
223 | 233 |
"device_id": str(vm.id), |
224 | 234 |
"fixed_ips": [{"ip_address": fip.address}] |
225 | 235 |
} |
226 | 236 |
} |
227 | 237 |
mrapi().ModifyInstance.return_value = 42 |
228 |
response = self.post(PORTS_URL, params=json.dumps(request), |
|
229 |
user=net.userid) |
|
238 |
with override_settings(settings, GANETI_USE_HOTPLUG=True): |
|
239 |
response = self.post(PORTS_URL, params=json.dumps(request), |
|
240 |
user=vm.userid) |
|
230 | 241 |
self.assertEqual(response.status_code, 201) |
231 | 242 |
|
232 | 243 |
def test_add_nic_to_deleted_network(self): |
Also available in: Unified diff