root / snf-cyclades-app / synnefo / api / tests / ports.py @ 5db2001a
History | View | Annotate | Download (5.6 kB)
1 | 5db2001a | Marios Kogias | # Copyright 2011-2013 GRNET S.A. All rights reserved.
|
---|---|---|---|
2 | 5db2001a | Marios Kogias | #
|
3 | 5db2001a | Marios Kogias | # Redistribution and use in source and binary forms, with or
|
4 | 5db2001a | Marios Kogias | # without modification, are permitted provided that the following
|
5 | 5db2001a | Marios Kogias | # conditions are met:
|
6 | 5db2001a | Marios Kogias | #
|
7 | 5db2001a | Marios Kogias | # 1. Redistributions of source code must retain the above
|
8 | 5db2001a | Marios Kogias | # copyright notice, this list of conditions and the following
|
9 | 5db2001a | Marios Kogias | # disclaimer.
|
10 | 5db2001a | Marios Kogias | #
|
11 | 5db2001a | Marios Kogias | # 2. Redistributions in binary form must reproduce the above
|
12 | 5db2001a | Marios Kogias | # copyright notice, this list of conditions and the following
|
13 | 5db2001a | Marios Kogias | # disclaimer in the documentation and/or other materials
|
14 | 5db2001a | Marios Kogias | # provided with the distribution.
|
15 | 5db2001a | Marios Kogias | #
|
16 | 5db2001a | Marios Kogias | # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 | 5db2001a | Marios Kogias | # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 | 5db2001a | Marios Kogias | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 | 5db2001a | Marios Kogias | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 | 5db2001a | Marios Kogias | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 | 5db2001a | Marios Kogias | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 | 5db2001a | Marios Kogias | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 | 5db2001a | Marios Kogias | # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 | 5db2001a | Marios Kogias | # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 | 5db2001a | Marios Kogias | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 | 5db2001a | Marios Kogias | # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 | 5db2001a | Marios Kogias | # POSSIBILITY OF SUCH DAMAGE.
|
28 | 5db2001a | Marios Kogias | #
|
29 | 5db2001a | Marios Kogias | # The views and conclusions contained in the software and
|
30 | 5db2001a | Marios Kogias | # documentation are those of the authors and should not be
|
31 | 5db2001a | Marios Kogias | # interpreted as representing official policies, either expressed
|
32 | 5db2001a | Marios Kogias | # or implied, of GRNET S.A.i
|
33 | 5db2001a | Marios Kogias | |
34 | 5db2001a | Marios Kogias | from snf_django.utils.testing import (BaseAPITest, override_settings) |
35 | 593851e0 | Buildbot | from snf_django.utils.testing import BaseAPITest |
36 | 593851e0 | Buildbot | from django.utils import simplejson as json |
37 | 593851e0 | Buildbot | from synnefo.cyclades_settings import cyclades_services |
38 | 593851e0 | Buildbot | from synnefo.lib.services import get_service_path |
39 | 593851e0 | Buildbot | from synnefo.lib import join_urls |
40 | 593851e0 | Buildbot | import synnefo.db.models_factory as dbmf |
41 | 593851e0 | Buildbot | |
42 | 593851e0 | Buildbot | COMPUTE_URL = get_service_path(cyclades_services, 'compute',
|
43 | 593851e0 | Buildbot | version='v2.0')
|
44 | 593851e0 | Buildbot | PORTS_URL = join_urls(COMPUTE_URL, "ports/")
|
45 | 593851e0 | Buildbot | |
46 | 593851e0 | Buildbot | |
47 | 593851e0 | Buildbot | class PortTest(BaseAPITest): |
48 | 593851e0 | Buildbot | def test_get_ports(self): |
49 | 593851e0 | Buildbot | url = join_urls(PORTS_URL) |
50 | 593851e0 | Buildbot | response = self.get(url)
|
51 | 593851e0 | Buildbot | self.assertEqual(response.status_code, 200) |
52 | 593851e0 | Buildbot | ports = json.loads(response.content) |
53 | 593851e0 | Buildbot | self.assertEqual(ports, {"ports": []}) |
54 | 593851e0 | Buildbot | |
55 | 593851e0 | Buildbot | def test_get_port_unfound(self): |
56 | 593851e0 | Buildbot | url = join_urls(PORTS_URL, "123")
|
57 | 593851e0 | Buildbot | response = self.get(url)
|
58 | 593851e0 | Buildbot | self.assertEqual(response.status_code, 404) |
59 | 593851e0 | Buildbot | |
60 | 593851e0 | Buildbot | def test_get_port(self): |
61 | 593851e0 | Buildbot | nic = dbmf.NetworkInterfaceFactory.create() |
62 | 593851e0 | Buildbot | url = join_urls(PORTS_URL, str(nic.id))
|
63 | 593851e0 | Buildbot | response = self.get(url, user=nic.network.userid)
|
64 | 593851e0 | Buildbot | self.assertEqual(response.status_code, 200) |
65 | 593851e0 | Buildbot | |
66 | 593851e0 | Buildbot | def test_delete_port(self): |
67 | 593851e0 | Buildbot | nic = dbmf.NetworkInterfaceFactory.create(device_owner='vm')
|
68 | 593851e0 | Buildbot | url = join_urls(PORTS_URL, str(nic.id))
|
69 | 593851e0 | Buildbot | response = self.delete(url, user=nic.network.userid)
|
70 | 593851e0 | Buildbot | self.assertEqual(response.status_code, 204) |
71 | 593851e0 | Buildbot | |
72 | 593851e0 | Buildbot | def test_update_port_name(self): |
73 | 593851e0 | Buildbot | nic = dbmf.NetworkInterfaceFactory.create(device_owner='vm')
|
74 | 593851e0 | Buildbot | url = join_urls(PORTS_URL, str(nic.id))
|
75 | 593851e0 | Buildbot | request = {'port': {"name": "test-name"}} |
76 | 593851e0 | Buildbot | response = self.put(url, params=json.dumps(request),
|
77 | 593851e0 | Buildbot | user=nic.network.userid) |
78 | 593851e0 | Buildbot | self.assertEqual(response.status_code, 200) |
79 | 593851e0 | Buildbot | res = json.loads(response.content) |
80 | 593851e0 | Buildbot | self.assertEqual(res['port']['name'], "test-name") |
81 | 593851e0 | Buildbot | |
82 | 593851e0 | Buildbot | def test_update_port_sg_unfound(self): |
83 | 593851e0 | Buildbot | sg1 = dbmf.SecurityGroupFactory.create() |
84 | 5db2001a | Marios Kogias | nic = dbmf.NetworkInterfaceFactory.create(device_owner='vm')
|
85 | 593851e0 | Buildbot | nic.security_groups.add(sg1) |
86 | 593851e0 | Buildbot | nic.save() |
87 | 593851e0 | Buildbot | url = join_urls(PORTS_URL, str(nic.id))
|
88 | 593851e0 | Buildbot | request = {'port': {"security_groups": ["123"]}} |
89 | 593851e0 | Buildbot | response = self.put(url, params=json.dumps(request),
|
90 | 593851e0 | Buildbot | user=nic.network.userid) |
91 | 593851e0 | Buildbot | self.assertEqual(response.status_code, 404) |
92 | 593851e0 | Buildbot | |
93 | 593851e0 | Buildbot | def test_update_port_sg(self): |
94 | 593851e0 | Buildbot | sg1 = dbmf.SecurityGroupFactory.create() |
95 | 593851e0 | Buildbot | sg2 = dbmf.SecurityGroupFactory.create() |
96 | 593851e0 | Buildbot | sg3 = dbmf.SecurityGroupFactory.create() |
97 | 593851e0 | Buildbot | nic = dbmf.NetworkInterfaceFactory.create(device_owner='vm')
|
98 | 593851e0 | Buildbot | nic.security_groups.add(sg1) |
99 | 593851e0 | Buildbot | nic.save() |
100 | 593851e0 | Buildbot | url = join_urls(PORTS_URL, str(nic.id))
|
101 | 593851e0 | Buildbot | request = {'port': {"security_groups": [str(sg2.id), str(sg3.id)]}} |
102 | 593851e0 | Buildbot | response = self.put(url, params=json.dumps(request),
|
103 | 593851e0 | Buildbot | user=nic.network.userid) |
104 | 593851e0 | Buildbot | res = json.loads(response.content) |
105 | 593851e0 | Buildbot | self.assertEqual(res['port']['security_groups'], |
106 | 593851e0 | Buildbot | [str(sg2.id), str(sg3.id)]) |
107 | 593851e0 | Buildbot | |
108 | 593851e0 | Buildbot | def test_create_port_no_network(self): |
109 | 593851e0 | Buildbot | request = { |
110 | 593851e0 | Buildbot | "port": {
|
111 | 593851e0 | Buildbot | "device_id": "123", |
112 | 593851e0 | Buildbot | "name": "port1", |
113 | 593851e0 | Buildbot | "network_id": "123" |
114 | 593851e0 | Buildbot | } |
115 | 593851e0 | Buildbot | } |
116 | 593851e0 | Buildbot | response = self.post(PORTS_URL, params=json.dumps(request))
|
117 | 593851e0 | Buildbot | self.assertEqual(response.status_code, 404) |
118 | 593851e0 | Buildbot | |
119 | 593851e0 | Buildbot | def test_create_port(self): |
120 | 593851e0 | Buildbot | net = dbmf.NetworkFactory.create() |
121 | 593851e0 | Buildbot | subnet1 = dbmf.IPv4SubnetFactory.create(network=net) |
122 | 593851e0 | Buildbot | subnet2 = dbmf.IPv6SubnetFactory.create(network=net) |
123 | 593851e0 | Buildbot | sg1 = dbmf.SecurityGroupFactory.create() |
124 | 593851e0 | Buildbot | sg2 = dbmf.SecurityGroupFactory.create() |
125 | 593851e0 | Buildbot | vm = dbmf.VirtualMachineFactory.create(userid=net.userid) |
126 | 593851e0 | Buildbot | request = { |
127 | 593851e0 | Buildbot | "port": {
|
128 | 593851e0 | Buildbot | "name": "port1", |
129 | 593851e0 | Buildbot | "network_id": str(net.id), |
130 | 593851e0 | Buildbot | "device_id": str(vm.id), |
131 | 593851e0 | Buildbot | "security_groups": [str(sg1.id), str(sg2.id)] |
132 | 593851e0 | Buildbot | } |
133 | 593851e0 | Buildbot | } |
134 | 593851e0 | Buildbot | response = self.post(PORTS_URL, params=json.dumps(request),
|
135 | 593851e0 | Buildbot | user=net.userid) |
136 | 593851e0 | Buildbot | self.assertEqual(response.status_code, 201) |