Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / api / tests.py @ bb54871d

History | View | Annotate | Download (38.4 kB)

1 adee02b8 Giorgos Verigakis
# Copyright 2011 GRNET S.A. All rights reserved.
2 c25cc9ec Vangelis Koukis
#
3 adee02b8 Giorgos Verigakis
# Redistribution and use in source and binary forms, with or
4 adee02b8 Giorgos Verigakis
# without modification, are permitted provided that the following
5 adee02b8 Giorgos Verigakis
# conditions are met:
6 c25cc9ec Vangelis Koukis
#
7 adee02b8 Giorgos Verigakis
#   1. Redistributions of source code must retain the above
8 adee02b8 Giorgos Verigakis
#      copyright notice, this list of conditions and the following
9 adee02b8 Giorgos Verigakis
#      disclaimer.
10 c25cc9ec Vangelis Koukis
#
11 adee02b8 Giorgos Verigakis
#   2. Redistributions in binary form must reproduce the above
12 adee02b8 Giorgos Verigakis
#      copyright notice, this list of conditions and the following
13 adee02b8 Giorgos Verigakis
#      disclaimer in the documentation and/or other materials
14 adee02b8 Giorgos Verigakis
#      provided with the distribution.
15 c25cc9ec Vangelis Koukis
#
16 adee02b8 Giorgos Verigakis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 adee02b8 Giorgos Verigakis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 adee02b8 Giorgos Verigakis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 adee02b8 Giorgos Verigakis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 adee02b8 Giorgos Verigakis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 adee02b8 Giorgos Verigakis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 adee02b8 Giorgos Verigakis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 adee02b8 Giorgos Verigakis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 adee02b8 Giorgos Verigakis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 adee02b8 Giorgos Verigakis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 adee02b8 Giorgos Verigakis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 adee02b8 Giorgos Verigakis
# POSSIBILITY OF SUCH DAMAGE.
28 c25cc9ec Vangelis Koukis
#
29 adee02b8 Giorgos Verigakis
# The views and conclusions contained in the software and
30 adee02b8 Giorgos Verigakis
# documentation are those of the authors and should not be
31 adee02b8 Giorgos Verigakis
# interpreted as representing official policies, either expressed
32 adee02b8 Giorgos Verigakis
# or implied, of GRNET S.A.
33 894f6b7e Dimitris Moraitis
34 53b1eea6 Giorgos Verigakis
from __future__ import with_statement
35 53b1eea6 Giorgos Verigakis
36 53b1eea6 Giorgos Verigakis
from collections import defaultdict
37 8b01f7f3 Giorgos Verigakis
from email.utils import parsedate
38 ba6ded2a Giorgos Verigakis
from random import choice, randint, sample
39 8b01f7f3 Giorgos Verigakis
from time import mktime
40 8b01f7f3 Giorgos Verigakis
41 ccd0d474 Giorgos Verigakis
from django.conf import settings
42 a237869d Faidon Liambotis
from django.utils import simplejson as json
43 894f6b7e Dimitris Moraitis
from django.test import TestCase
44 894f6b7e Dimitris Moraitis
from django.test.client import Client
45 883de713 Giorgos Verigakis
46 968b3e04 Giorgos Verigakis
from synnefo.db.models import *
47 40777cc8 Giorgos Verigakis
from synnefo.logic.utils import get_rsapi_state
48 40777cc8 Giorgos Verigakis
49 40777cc8 Giorgos Verigakis
50 40777cc8 Giorgos Verigakis
class AaiClient(Client):
51 40777cc8 Giorgos Verigakis
    def request(self, **request):
52 244c552b Giorgos Verigakis
        request['HTTP_X_AUTH_TOKEN'] = '0000'
53 40777cc8 Giorgos Verigakis
        return super(AaiClient, self).request(**request)
54 894f6b7e Dimitris Moraitis
55 883de713 Giorgos Verigakis
56 894f6b7e Dimitris Moraitis
class APITestCase(TestCase):
57 53481544 Vangelis Koukis
    fixtures = ['users', 'api_test_data']
58 d2e5f1d1 Markos Gogoulos
    test_server_id = 1001
59 d2e5f1d1 Markos Gogoulos
    test_image_id = 1
60 d2e5f1d1 Markos Gogoulos
    test_flavor_id = 1
61 a8acbbac Markos Gogoulos
    test_group_id = 1
62 d2e5f1d1 Markos Gogoulos
    test_wrong_server_id = 99999999
63 d2e5f1d1 Markos Gogoulos
    test_wrong_image_id = 99999999
64 d2e5f1d1 Markos Gogoulos
    test_wrong_flavor_id = 99999999
65 a8acbbac Markos Gogoulos
    test_wrong_group_id = 99999999
66 d2e5f1d1 Markos Gogoulos
    #make the testing with these id's
67 d2e5f1d1 Markos Gogoulos
68 894f6b7e Dimitris Moraitis
    def setUp(self):
69 40777cc8 Giorgos Verigakis
        self.client = AaiClient()
70 ccd0d474 Giorgos Verigakis
        settings.MAX_VMS_PER_USER = 5
71 c25cc9ec Vangelis Koukis
72 e646ebe5 Dimitris Moraitis
    def test_api_version(self):
73 883de713 Giorgos Verigakis
        """Check API version."""
74 aa197ee4 Vangelis Koukis
75 883de713 Giorgos Verigakis
        response = self.client.get('/api/v1.1/')
76 894f6b7e Dimitris Moraitis
        self.assertEqual(response.status_code, 200)
77 54c30633 Markos Gogoulos
        api_version = json.loads(response.content)['version']
78 883de713 Giorgos Verigakis
        self.assertEqual(api_version['id'], 'v1.1')
79 54c30633 Markos Gogoulos
        self.assertEqual(api_version['status'], 'CURRENT')
80 54c30633 Markos Gogoulos
81 e646ebe5 Dimitris Moraitis
    def test_server_list(self):
82 883de713 Giorgos Verigakis
        """Test if the expected list of servers is returned."""
83 aa197ee4 Vangelis Koukis
84 883de713 Giorgos Verigakis
        response = self.client.get('/api/v1.1/servers')
85 883de713 Giorgos Verigakis
        vms_from_api = json.loads(response.content)['servers']['values']
86 668c1361 Markos Gogoulos
        vms_from_db = VirtualMachine.objects.filter(deleted=False)
87 6781c0d0 Dimitris Moraitis
        self.assertEqual(len(vms_from_api), len(vms_from_db))
88 e646ebe5 Dimitris Moraitis
        self.assertTrue(response.status_code in [200, 203])
89 54c30633 Markos Gogoulos
        for vm_from_api in vms_from_api:
90 54c30633 Markos Gogoulos
            vm_from_db = VirtualMachine.objects.get(id=vm_from_api['id'])
91 54c30633 Markos Gogoulos
            self.assertEqual(vm_from_api['id'], vm_from_db.id)
92 54c30633 Markos Gogoulos
            self.assertEqual(vm_from_api['name'], vm_from_db.name)
93 54c30633 Markos Gogoulos
94 e646ebe5 Dimitris Moraitis
    def test_server_details(self):
95 883de713 Giorgos Verigakis
        """Test if the expected server is returned."""
96 aa197ee4 Vangelis Koukis
97 883de713 Giorgos Verigakis
        response = self.client.get('/api/v1.1/servers/%d' % self.test_server_id)
98 668c1361 Markos Gogoulos
        vm_from_api = json.loads(response.content)['server']
99 d2e5f1d1 Markos Gogoulos
        vm_from_db = VirtualMachine.objects.get(id=self.test_server_id)
100 b335768e Markos Gogoulos
        self.assertEqual(vm_from_api['flavorRef'], vm_from_db.flavor.id)
101 668c1361 Markos Gogoulos
        self.assertEqual(vm_from_api['hostId'], vm_from_db.hostid)
102 668c1361 Markos Gogoulos
        self.assertEqual(vm_from_api['id'], vm_from_db.id)
103 936afb7b Giorgos Verigakis
        self.assertEqual(vm_from_api['imageRef'], vm_from_db.imageid)
104 668c1361 Markos Gogoulos
        self.assertEqual(vm_from_api['name'], vm_from_db.name)
105 40777cc8 Giorgos Verigakis
        self.assertEqual(vm_from_api['status'], get_rsapi_state(vm_from_db))
106 883de713 Giorgos Verigakis
        self.assertTrue(response.status_code in [200, 203])
107 668c1361 Markos Gogoulos
108 e646ebe5 Dimitris Moraitis
    def test_servers_details(self):
109 883de713 Giorgos Verigakis
        """Test if the servers details are returned."""
110 aa197ee4 Vangelis Koukis
111 883de713 Giorgos Verigakis
        response = self.client.get('/api/v1.1/servers/detail')
112 ba15eb15 Vangelis Koukis
113 ba15eb15 Vangelis Koukis
        # Make sure both DB and API responses are sorted by id,
114 ba15eb15 Vangelis Koukis
        # to allow for 1-1 comparisons
115 ba15eb15 Vangelis Koukis
        vms_from_db = VirtualMachine.objects.filter(deleted=False).order_by('id')
116 ba15eb15 Vangelis Koukis
        vms_from_api = json.loads(response.content)['servers']['values']
117 ba15eb15 Vangelis Koukis
        vms_from_api = sorted(vms_from_api, key=lambda vm: vm['id'])
118 ba15eb15 Vangelis Koukis
        self.assertEqual(len(vms_from_db), len(vms_from_api))
119 ba15eb15 Vangelis Koukis
120 54c30633 Markos Gogoulos
        id_list = [vm.id for vm in vms_from_db]
121 685ab2b6 Markos Gogoulos
        number = 0
122 685ab2b6 Markos Gogoulos
        for vm_id in id_list:
123 ba15eb15 Vangelis Koukis
            vm_from_api = vms_from_api[number]
124 685ab2b6 Markos Gogoulos
            vm_from_db = VirtualMachine.objects.get(id=vm_id)
125 b335768e Markos Gogoulos
            self.assertEqual(vm_from_api['flavorRef'], vm_from_db.flavor.id)
126 685ab2b6 Markos Gogoulos
            self.assertEqual(vm_from_api['hostId'], vm_from_db.hostid)
127 685ab2b6 Markos Gogoulos
            self.assertEqual(vm_from_api['id'], vm_from_db.id)
128 936afb7b Giorgos Verigakis
            self.assertEqual(vm_from_api['imageRef'], vm_from_db.imageid)
129 685ab2b6 Markos Gogoulos
            self.assertEqual(vm_from_api['name'], vm_from_db.name)
130 40777cc8 Giorgos Verigakis
            self.assertEqual(vm_from_api['status'], get_rsapi_state(vm_from_db))
131 685ab2b6 Markos Gogoulos
            number += 1
132 54c30633 Markos Gogoulos
        for vm_from_api in vms_from_api:
133 54c30633 Markos Gogoulos
            vm_from_db = VirtualMachine.objects.get(id=vm_from_api['id'])
134 b335768e Markos Gogoulos
            self.assertEqual(vm_from_api['flavorRef'], vm_from_db.flavor.id)
135 54c30633 Markos Gogoulos
            self.assertEqual(vm_from_api['hostId'], vm_from_db.hostid)
136 54c30633 Markos Gogoulos
            self.assertEqual(vm_from_api['id'], vm_from_db.id)
137 936afb7b Giorgos Verigakis
            self.assertEqual(vm_from_api['imageRef'], vm_from_db.imageid)
138 54c30633 Markos Gogoulos
            self.assertEqual(vm_from_api['name'], vm_from_db.name)
139 40777cc8 Giorgos Verigakis
            self.assertEqual(vm_from_api['status'], get_rsapi_state(vm_from_db))
140 c25cc9ec Vangelis Koukis
        self.assertTrue(response.status_code in [200, 203])
141 685ab2b6 Markos Gogoulos
142 e646ebe5 Dimitris Moraitis
    def test_wrong_server(self):
143 883de713 Giorgos Verigakis
        """Test 404 response if server does not exist."""
144 aa197ee4 Vangelis Koukis
145 883de713 Giorgos Verigakis
        response = self.client.get('/api/v1.1/servers/%d' % self.test_wrong_server_id)
146 685ab2b6 Markos Gogoulos
        self.assertEqual(response.status_code, 404)
147 685ab2b6 Markos Gogoulos
148 e646ebe5 Dimitris Moraitis
    def test_create_server_empty(self):
149 883de713 Giorgos Verigakis
        """Test if the create server call returns a 400 badRequest if
150 883de713 Giorgos Verigakis
           no attributes are specified."""
151 aa197ee4 Vangelis Koukis
152 883de713 Giorgos Verigakis
        response = self.client.post('/api/v1.1/servers', {})
153 7de0c985 Dimitris Moraitis
        self.assertEqual(response.status_code, 400)
154 54c30633 Markos Gogoulos
155 e646ebe5 Dimitris Moraitis
    def test_create_server(self):
156 883de713 Giorgos Verigakis
        """Test if the create server call returns the expected response
157 883de713 Giorgos Verigakis
           if a valid request has been speficied."""
158 aa197ee4 Vangelis Koukis
159 7de0c985 Dimitris Moraitis
        request = {
160 285a1fa8 Dimitris Moraitis
                    "server": {
161 e646ebe5 Dimitris Moraitis
                        "name": "new-server-test",
162 244c552b Giorgos Verigakis
                        "userid": "test",
163 e646ebe5 Dimitris Moraitis
                        "imageRef": 1,
164 e646ebe5 Dimitris Moraitis
                        "flavorRef": 1,
165 e646ebe5 Dimitris Moraitis
                        "metadata": {
166 285a1fa8 Dimitris Moraitis
                            "My Server Name": "Apache1"
167 285a1fa8 Dimitris Moraitis
                        },
168 e646ebe5 Dimitris Moraitis
                        "personality": []
169 285a1fa8 Dimitris Moraitis
                    }
170 7de0c985 Dimitris Moraitis
        }
171 883de713 Giorgos Verigakis
        response = self.client.post('/api/v1.1/servers', json.dumps(request),
172 685ab2b6 Markos Gogoulos
                                    content_type='application/json')
173 285a1fa8 Dimitris Moraitis
        self.assertEqual(response.status_code, 202)
174 e646ebe5 Dimitris Moraitis
        #TODO: check response.content
175 e646ebe5 Dimitris Moraitis
        #TODO: check create server with wrong options (eg non existing flavor)
176 668c1361 Markos Gogoulos
177 e646ebe5 Dimitris Moraitis
    def test_server_polling(self):
178 883de713 Giorgos Verigakis
        """Test if the server polling works as expected."""
179 aa197ee4 Vangelis Koukis
180 883de713 Giorgos Verigakis
        response = self.client.get('/api/v1.1/servers/detail')
181 883de713 Giorgos Verigakis
        vms_from_api_initial = json.loads(response.content)['servers']['values']
182 8b01f7f3 Giorgos Verigakis
        ts = mktime(parsedate(response['Date']))
183 8b01f7f3 Giorgos Verigakis
        since = datetime.datetime.fromtimestamp(ts).isoformat() + 'Z'
184 8b01f7f3 Giorgos Verigakis
        response = self.client.get('/api/v1.1/servers/detail?changes-since=%s' % since)
185 34244492 Markos Gogoulos
        self.assertEqual(len(response.content), 0)
186 34244492 Markos Gogoulos
187 34244492 Markos Gogoulos
        #now create a machine. Then check if it is on the list
188 34244492 Markos Gogoulos
        request = {
189 34244492 Markos Gogoulos
                    "server": {
190 e646ebe5 Dimitris Moraitis
                        "name": "new-server-test",
191 e646ebe5 Dimitris Moraitis
                        "imageRef": 1,
192 e646ebe5 Dimitris Moraitis
                        "flavorRef": 1,
193 e646ebe5 Dimitris Moraitis
                        "metadata": {
194 34244492 Markos Gogoulos
                            "My Server Name": "Apache1"
195 34244492 Markos Gogoulos
                        },
196 e646ebe5 Dimitris Moraitis
                        "personality": []
197 34244492 Markos Gogoulos
                    }
198 34244492 Markos Gogoulos
        }
199 aa197ee4 Vangelis Koukis
200 883de713 Giorgos Verigakis
        path = '/api/v1.1/servers'
201 883de713 Giorgos Verigakis
        response = self.client.post(path, json.dumps(request), content_type='application/json')
202 34244492 Markos Gogoulos
        self.assertEqual(response.status_code, 202)
203 34244492 Markos Gogoulos
204 8b01f7f3 Giorgos Verigakis
        response = self.client.get('/api/v1.1/servers/detail?changes-since=%s' % since)
205 7f63ff66 Giorgos Verigakis
        self.assertEqual(response.status_code, 200)
206 009d3bf2 Giorgos Verigakis
        vms_from_api_after = json.loads(response.content)['servers']['values']
207 e646ebe5 Dimitris Moraitis
        #make sure the newly created server is included on the updated list
208 34244492 Markos Gogoulos
        self.assertEqual(len(vms_from_api_after), 1)
209 34244492 Markos Gogoulos
210 e646ebe5 Dimitris Moraitis
    def test_reboot_server(self):
211 883de713 Giorgos Verigakis
        """Test if the specified server is rebooted."""
212 883de713 Giorgos Verigakis
        request = {'reboot': {'type': 'HARD'}}
213 883de713 Giorgos Verigakis
        path = '/api/v1.1/servers/%d/action' % self.test_server_id
214 883de713 Giorgos Verigakis
        response = self.client.post(path, json.dumps(request), content_type='application/json')
215 685ab2b6 Markos Gogoulos
        self.assertEqual(response.status_code, 202)
216 f90c3d8c Vangelis Koukis
        # server id that does not exist
217 883de713 Giorgos Verigakis
        path = '/api/v1.1/servers/%d/action' % self.test_wrong_server_id
218 883de713 Giorgos Verigakis
        response = self.client.post(path, json.dumps(request), content_type='application/json')
219 685ab2b6 Markos Gogoulos
        self.assertEqual(response.status_code, 404)
220 668c1361 Markos Gogoulos
221 e646ebe5 Dimitris Moraitis
    def test_shutdown_server(self):
222 883de713 Giorgos Verigakis
        """Test if the specified server is shutdown."""
223 aa197ee4 Vangelis Koukis
224 883de713 Giorgos Verigakis
        request = {'shutdown': {}}
225 883de713 Giorgos Verigakis
        path = '/api/v1.1/servers/%d/action' % self.test_server_id
226 883de713 Giorgos Verigakis
        response = self.client.post(path, json.dumps(request), content_type='application/json')
227 685ab2b6 Markos Gogoulos
        self.assertEqual(response.status_code, 202)
228 f90c3d8c Vangelis Koukis
        # server id that does not exist
229 883de713 Giorgos Verigakis
        path = '/api/v1.1/servers/%d/action' % self.test_wrong_server_id
230 883de713 Giorgos Verigakis
        response = self.client.post(path, json.dumps(request), content_type='application/json')
231 685ab2b6 Markos Gogoulos
        self.assertEqual(response.status_code, 404)
232 668c1361 Markos Gogoulos
233 e646ebe5 Dimitris Moraitis
    def test_start_server(self):
234 883de713 Giorgos Verigakis
        """Test if the specified server is started."""
235 aa197ee4 Vangelis Koukis
236 883de713 Giorgos Verigakis
        request = {'start': {}}
237 883de713 Giorgos Verigakis
        path = '/api/v1.1/servers/%d/action' % self.test_server_id
238 883de713 Giorgos Verigakis
        response = self.client.post(path, json.dumps(request), content_type='application/json')
239 685ab2b6 Markos Gogoulos
        self.assertEqual(response.status_code, 202)
240 f90c3d8c Vangelis Koukis
        # server id that does not exist
241 883de713 Giorgos Verigakis
        path = '/api/v1.1/servers/%d/action' % self.test_wrong_server_id
242 883de713 Giorgos Verigakis
        response = self.client.post(path, json.dumps(request), content_type='application/json')
243 685ab2b6 Markos Gogoulos
        self.assertEqual(response.status_code, 404)
244 668c1361 Markos Gogoulos
245 e646ebe5 Dimitris Moraitis
    def test_delete_server(self):
246 883de713 Giorgos Verigakis
        """Test if the specified server is deleted."""
247 883de713 Giorgos Verigakis
        response = self.client.delete('/api/v1.1/servers/%d' % self.test_server_id)
248 883de713 Giorgos Verigakis
        self.assertEqual(response.status_code, 204)
249 f90c3d8c Vangelis Koukis
        # server id that does not exist
250 883de713 Giorgos Verigakis
        response = self.client.delete('/api/v1.1/servers/%d' % self.test_wrong_server_id)
251 685ab2b6 Markos Gogoulos
        self.assertEqual(response.status_code, 404)
252 668c1361 Markos Gogoulos
253 e646ebe5 Dimitris Moraitis
    def test_flavor_list(self):
254 883de713 Giorgos Verigakis
        """Test if the expected list of flavors is returned by."""
255 aa197ee4 Vangelis Koukis
256 883de713 Giorgos Verigakis
        response = self.client.get('/api/v1.1/flavors')
257 883de713 Giorgos Verigakis
        flavors_from_api = json.loads(response.content)['flavors']['values']
258 668c1361 Markos Gogoulos
        flavors_from_db = Flavor.objects.all()
259 668c1361 Markos Gogoulos
        self.assertEqual(len(flavors_from_api), len(flavors_from_db))
260 e646ebe5 Dimitris Moraitis
        self.assertTrue(response.status_code in [200, 203])
261 54c30633 Markos Gogoulos
        for flavor_from_api in flavors_from_api:
262 54c30633 Markos Gogoulos
            flavor_from_db = Flavor.objects.get(id=flavor_from_api['id'])
263 54c30633 Markos Gogoulos
            self.assertEqual(flavor_from_api['id'], flavor_from_db.id)
264 54c30633 Markos Gogoulos
            self.assertEqual(flavor_from_api['name'], flavor_from_db.name)
265 668c1361 Markos Gogoulos
266 e646ebe5 Dimitris Moraitis
    def test_flavors_details(self):
267 883de713 Giorgos Verigakis
        """Test if the flavors details are returned."""
268 aa197ee4 Vangelis Koukis
269 883de713 Giorgos Verigakis
        response = self.client.get('/api/v1.1/flavors/detail')
270 f3af8b40 Dimitris Moraitis
        flavors_from_db = Flavor.objects.all()
271 883de713 Giorgos Verigakis
        flavors_from_api = json.loads(response.content)['flavors']['values']
272 e646ebe5 Dimitris Moraitis
273 f3af8b40 Dimitris Moraitis
        # Assert that all flavors in the db appear inthe API call result
274 f3af8b40 Dimitris Moraitis
        for i in range(0, len(flavors_from_db)):
275 f3af8b40 Dimitris Moraitis
            flavor_from_api = flavors_from_api[i]
276 f3af8b40 Dimitris Moraitis
            flavor_from_db = Flavor.objects.get(id=flavors_from_db[i].id)
277 685ab2b6 Markos Gogoulos
            self.assertEqual(flavor_from_api['cpu'], flavor_from_db.cpu)
278 685ab2b6 Markos Gogoulos
            self.assertEqual(flavor_from_api['id'], flavor_from_db.id)
279 685ab2b6 Markos Gogoulos
            self.assertEqual(flavor_from_api['disk'], flavor_from_db.disk)
280 685ab2b6 Markos Gogoulos
            self.assertEqual(flavor_from_api['name'], flavor_from_db.name)
281 685ab2b6 Markos Gogoulos
            self.assertEqual(flavor_from_api['ram'], flavor_from_db.ram)
282 f3af8b40 Dimitris Moraitis
283 e646ebe5 Dimitris Moraitis
        # Assert that all flavors returned by the API also exist in the db
284 54c30633 Markos Gogoulos
        for flavor_from_api in flavors_from_api:
285 54c30633 Markos Gogoulos
            flavor_from_db = Flavor.objects.get(id=flavor_from_api['id'])
286 54c30633 Markos Gogoulos
            self.assertEqual(flavor_from_api['cpu'], flavor_from_db.cpu)
287 54c30633 Markos Gogoulos
            self.assertEqual(flavor_from_api['id'], flavor_from_db.id)
288 54c30633 Markos Gogoulos
            self.assertEqual(flavor_from_api['disk'], flavor_from_db.disk)
289 54c30633 Markos Gogoulos
            self.assertEqual(flavor_from_api['name'], flavor_from_db.name)
290 54c30633 Markos Gogoulos
            self.assertEqual(flavor_from_api['ram'], flavor_from_db.ram)
291 7de0c985 Dimitris Moraitis
292 e646ebe5 Dimitris Moraitis
        # Check if we have the right status_code
293 e646ebe5 Dimitris Moraitis
        self.assertTrue(response.status_code in [200, 203])
294 7de0c985 Dimitris Moraitis
295 e646ebe5 Dimitris Moraitis
    def test_flavor_details(self):
296 883de713 Giorgos Verigakis
        """Test if the expected flavor is returned."""
297 aa197ee4 Vangelis Koukis
298 883de713 Giorgos Verigakis
        response = self.client.get('/api/v1.1/flavors/%d' % self.test_flavor_id)
299 668c1361 Markos Gogoulos
        flavor_from_api = json.loads(response.content)['flavor']
300 d2e5f1d1 Markos Gogoulos
        flavor_from_db = Flavor.objects.get(id=self.test_flavor_id)
301 668c1361 Markos Gogoulos
        self.assertEqual(flavor_from_api['cpu'], flavor_from_db.cpu)
302 668c1361 Markos Gogoulos
        self.assertEqual(flavor_from_api['id'], flavor_from_db.id)
303 668c1361 Markos Gogoulos
        self.assertEqual(flavor_from_api['disk'], flavor_from_db.disk)
304 668c1361 Markos Gogoulos
        self.assertEqual(flavor_from_api['name'], flavor_from_db.name)
305 668c1361 Markos Gogoulos
        self.assertEqual(flavor_from_api['ram'], flavor_from_db.ram)
306 e646ebe5 Dimitris Moraitis
        self.assertTrue(response.status_code in [200, 203])
307 668c1361 Markos Gogoulos
308 e646ebe5 Dimitris Moraitis
    def test_wrong_flavor(self):
309 883de713 Giorgos Verigakis
        """Test 404 result when requesting a flavor that does not exist."""
310 aa197ee4 Vangelis Koukis
311 883de713 Giorgos Verigakis
        response = self.client.get('/api/v1.1/flavors/%d' % self.test_wrong_flavor_id)
312 ffffd5fe Markos Gogoulos
        self.assertTrue(response.status_code in [404, 503])
313 668c1361 Markos Gogoulos
314 e646ebe5 Dimitris Moraitis
    def test_image_list(self):
315 883de713 Giorgos Verigakis
        """Test if the expected list of images is returned by the API."""
316 aa197ee4 Vangelis Koukis
317 883de713 Giorgos Verigakis
        response = self.client.get('/api/v1.1/images')
318 883de713 Giorgos Verigakis
        images_from_api = json.loads(response.content)['images']['values']
319 668c1361 Markos Gogoulos
        images_from_db = Image.objects.all()
320 668c1361 Markos Gogoulos
        self.assertEqual(len(images_from_api), len(images_from_db))
321 e646ebe5 Dimitris Moraitis
        self.assertTrue(response.status_code in [200, 203])
322 54c30633 Markos Gogoulos
        for image_from_api in images_from_api:
323 54c30633 Markos Gogoulos
            image_from_db = Image.objects.get(id=image_from_api['id'])
324 54c30633 Markos Gogoulos
            self.assertEqual(image_from_api['id'], image_from_db.id)
325 54c30633 Markos Gogoulos
            self.assertEqual(image_from_api['name'], image_from_db.name)
326 668c1361 Markos Gogoulos
327 e646ebe5 Dimitris Moraitis
    def test_wrong_image(self):
328 883de713 Giorgos Verigakis
        """Test 404 result if a non existent image is requested."""
329 aa197ee4 Vangelis Koukis
330 883de713 Giorgos Verigakis
        response = self.client.get('/api/v1.1/images/%d' % self.test_wrong_image_id)
331 685ab2b6 Markos Gogoulos
        self.assertEqual(response.status_code, 404)
332 73469364 Markos Gogoulos
333 e646ebe5 Dimitris Moraitis
    def test_server_metadata(self):
334 883de713 Giorgos Verigakis
        """Test server's metadata (add, edit)."""
335 aa197ee4 Vangelis Koukis
336 883de713 Giorgos Verigakis
        key = 'name'
337 883de713 Giorgos Verigakis
        request = {'meta': {key: 'a fancy name'}}
338 aa197ee4 Vangelis Koukis
339 883de713 Giorgos Verigakis
        path = '/api/v1.1/servers/%d/meta/%s' % (self.test_server_id, key)
340 883de713 Giorgos Verigakis
        response = self.client.put(path, json.dumps(request), content_type='application/json')
341 883de713 Giorgos Verigakis
        self.assertEqual(response.status_code, 201)
342 a8acbbac Markos Gogoulos
343 a8acbbac Markos Gogoulos
344 968b3e04 Giorgos Verigakis
def create_flavors(n=1):
345 968b3e04 Giorgos Verigakis
    for i in range(n):
346 968b3e04 Giorgos Verigakis
        Flavor.objects.create(
347 968b3e04 Giorgos Verigakis
            cpu=randint(1, 4),
348 80da6543 Giorgos Verigakis
            ram=randint(1, 8) * 512,
349 80da6543 Giorgos Verigakis
            disk=randint(1, 40))
350 968b3e04 Giorgos Verigakis
351 c25cc9ec Vangelis Koukis
352 968b3e04 Giorgos Verigakis
def create_images(n=1):
353 968b3e04 Giorgos Verigakis
    for i in range(n):
354 968b3e04 Giorgos Verigakis
        Image.objects.create(
355 968b3e04 Giorgos Verigakis
            name='Image %d' % (i + 1),
356 968b3e04 Giorgos Verigakis
            state='ACTIVE',
357 244c552b Giorgos Verigakis
            owner='test')
358 968b3e04 Giorgos Verigakis
359 c25cc9ec Vangelis Koukis
360 53b1eea6 Giorgos Verigakis
def create_image_metadata(n=1):
361 53b1eea6 Giorgos Verigakis
    images = Image.objects.all()
362 53b1eea6 Giorgos Verigakis
    for i in range(n):
363 53b1eea6 Giorgos Verigakis
        ImageMetadata.objects.create(
364 53b1eea6 Giorgos Verigakis
            meta_key='Key%d' % (i + 1),
365 53b1eea6 Giorgos Verigakis
            meta_value='Value %d' % (i + 1),
366 c25cc9ec Vangelis Koukis
            image=choice(images))
367 c25cc9ec Vangelis Koukis
368 53b1eea6 Giorgos Verigakis
369 968b3e04 Giorgos Verigakis
def create_servers(n=1):
370 968b3e04 Giorgos Verigakis
    flavors = Flavor.objects.all()
371 968b3e04 Giorgos Verigakis
    images = Image.objects.all()
372 968b3e04 Giorgos Verigakis
    for i in range(n):
373 968b3e04 Giorgos Verigakis
        VirtualMachine.objects.create(
374 968b3e04 Giorgos Verigakis
            name='Server %d' % (i + 1),
375 244c552b Giorgos Verigakis
            owner='test',
376 936afb7b Giorgos Verigakis
            imageid=choice(images).id,
377 968b3e04 Giorgos Verigakis
            hostid=str(i),
378 968b3e04 Giorgos Verigakis
            flavor=choice(flavors))
379 968b3e04 Giorgos Verigakis
380 c25cc9ec Vangelis Koukis
381 968b3e04 Giorgos Verigakis
def create_server_metadata(n=1):
382 968b3e04 Giorgos Verigakis
    servers = VirtualMachine.objects.all()
383 968b3e04 Giorgos Verigakis
    for i in range(n):
384 968b3e04 Giorgos Verigakis
        VirtualMachineMetadata.objects.create(
385 968b3e04 Giorgos Verigakis
            meta_key='Key%d' % (i + 1),
386 968b3e04 Giorgos Verigakis
            meta_value='Value %d' % (i + 1),
387 c25cc9ec Vangelis Koukis
            vm=choice(servers))
388 968b3e04 Giorgos Verigakis
389 968b3e04 Giorgos Verigakis
390 53b1eea6 Giorgos Verigakis
class AssertInvariant(object):
391 53b1eea6 Giorgos Verigakis
    def __init__(self, callable, *args, **kwargs):
392 53b1eea6 Giorgos Verigakis
        self.callable = callable
393 53b1eea6 Giorgos Verigakis
        self.args = args
394 53b1eea6 Giorgos Verigakis
        self.kwargs = kwargs
395 aa197ee4 Vangelis Koukis
396 53b1eea6 Giorgos Verigakis
    def __enter__(self):
397 53b1eea6 Giorgos Verigakis
        self.value = self.callable(*self.args, **self.kwargs)
398 53b1eea6 Giorgos Verigakis
        return self.value
399 aa197ee4 Vangelis Koukis
400 53b1eea6 Giorgos Verigakis
    def __exit__(self, type, value, tb):
401 53b1eea6 Giorgos Verigakis
        assert self.value == self.callable(*self.args, **self.kwargs)
402 53b1eea6 Giorgos Verigakis
403 53b1eea6 Giorgos Verigakis
404 968b3e04 Giorgos Verigakis
class BaseTestCase(TestCase):
405 968b3e04 Giorgos Verigakis
    FLAVORS = 1
406 968b3e04 Giorgos Verigakis
    IMAGES = 1
407 968b3e04 Giorgos Verigakis
    SERVERS = 1
408 968b3e04 Giorgos Verigakis
    SERVER_METADATA = 0
409 53b1eea6 Giorgos Verigakis
    IMAGE_METADATA = 0
410 53481544 Vangelis Koukis
    fixtures = ['users']
411 aa197ee4 Vangelis Koukis
412 883de713 Giorgos Verigakis
    def setUp(self):
413 40777cc8 Giorgos Verigakis
        self.client = AaiClient()
414 968b3e04 Giorgos Verigakis
        create_flavors(self.FLAVORS)
415 968b3e04 Giorgos Verigakis
        create_images(self.IMAGES)
416 53b1eea6 Giorgos Verigakis
        create_image_metadata(self.IMAGE_METADATA)
417 968b3e04 Giorgos Verigakis
        create_servers(self.SERVERS)
418 968b3e04 Giorgos Verigakis
        create_server_metadata(self.SERVER_METADATA)
419 aa197ee4 Vangelis Koukis
420 968b3e04 Giorgos Verigakis
    def assertFault(self, response, status_code, name):
421 968b3e04 Giorgos Verigakis
        self.assertEqual(response.status_code, status_code)
422 968b3e04 Giorgos Verigakis
        fault = json.loads(response.content)
423 968b3e04 Giorgos Verigakis
        self.assertEqual(fault.keys(), [name])
424 aa197ee4 Vangelis Koukis
425 968b3e04 Giorgos Verigakis
    def assertBadRequest(self, response):
426 968b3e04 Giorgos Verigakis
        self.assertFault(response, 400, 'badRequest')
427 968b3e04 Giorgos Verigakis
428 968b3e04 Giorgos Verigakis
    def assertItemNotFound(self, response):
429 968b3e04 Giorgos Verigakis
        self.assertFault(response, 404, 'itemNotFound')
430 aa197ee4 Vangelis Koukis
431 111f6d5b Giorgos Verigakis
    def list_images(self, detail=False):
432 111f6d5b Giorgos Verigakis
        path = '/api/v1.1/images'
433 111f6d5b Giorgos Verigakis
        if detail:
434 111f6d5b Giorgos Verigakis
            path += '/detail'
435 111f6d5b Giorgos Verigakis
        response = self.client.get(path)
436 111f6d5b Giorgos Verigakis
        self.assertTrue(response.status_code in (200, 203))
437 111f6d5b Giorgos Verigakis
        reply = json.loads(response.content)
438 111f6d5b Giorgos Verigakis
        self.assertEqual(reply.keys(), ['images'])
439 111f6d5b Giorgos Verigakis
        self.assertEqual(reply['images'].keys(), ['values'])
440 111f6d5b Giorgos Verigakis
        return reply['images']['values']
441 aa197ee4 Vangelis Koukis
442 53b1eea6 Giorgos Verigakis
    def list_metadata(self, path):
443 53b1eea6 Giorgos Verigakis
        response = self.client.get(path)
444 968b3e04 Giorgos Verigakis
        self.assertTrue(response.status_code in (200, 203))
445 968b3e04 Giorgos Verigakis
        reply = json.loads(response.content)
446 968b3e04 Giorgos Verigakis
        self.assertEqual(reply.keys(), ['metadata'])
447 968b3e04 Giorgos Verigakis
        self.assertEqual(reply['metadata'].keys(), ['values'])
448 968b3e04 Giorgos Verigakis
        return reply['metadata']['values']
449 aa197ee4 Vangelis Koukis
450 53b1eea6 Giorgos Verigakis
    def list_server_metadata(self, server_id):
451 53b1eea6 Giorgos Verigakis
        path = '/api/v1.1/servers/%d/meta' % server_id
452 53b1eea6 Giorgos Verigakis
        return self.list_metadata(path)
453 aa197ee4 Vangelis Koukis
454 53b1eea6 Giorgos Verigakis
    def list_image_metadata(self, image_id):
455 53b1eea6 Giorgos Verigakis
        path = '/api/v1.1/images/%d/meta' % image_id
456 53b1eea6 Giorgos Verigakis
        return self.list_metadata(path)
457 aa197ee4 Vangelis Koukis
458 53b1eea6 Giorgos Verigakis
    def update_metadata(self, path, metadata):
459 ba6ded2a Giorgos Verigakis
        data = json.dumps({'metadata': metadata})
460 ba6ded2a Giorgos Verigakis
        response = self.client.post(path, data, content_type='application/json')
461 968b3e04 Giorgos Verigakis
        self.assertEqual(response.status_code, 201)
462 968b3e04 Giorgos Verigakis
        reply = json.loads(response.content)
463 968b3e04 Giorgos Verigakis
        self.assertEqual(reply.keys(), ['metadata'])
464 968b3e04 Giorgos Verigakis
        return reply['metadata']
465 aa197ee4 Vangelis Koukis
466 53b1eea6 Giorgos Verigakis
    def update_server_metadata(self, server_id, metadata):
467 53b1eea6 Giorgos Verigakis
        path = '/api/v1.1/servers/%d/meta' % server_id
468 53b1eea6 Giorgos Verigakis
        return self.update_metadata(path, metadata)
469 aa197ee4 Vangelis Koukis
470 53b1eea6 Giorgos Verigakis
    def update_image_metadata(self, image_id, metadata):
471 53b1eea6 Giorgos Verigakis
        path = '/api/v1.1/images/%d/meta' % image_id
472 53b1eea6 Giorgos Verigakis
        return self.update_metadata(path, metadata)
473 aa197ee4 Vangelis Koukis
474 53b1eea6 Giorgos Verigakis
    def create_server_meta(self, server_id, meta):
475 53b1eea6 Giorgos Verigakis
        key = meta.keys()[0]
476 53b1eea6 Giorgos Verigakis
        path = '/api/v1.1/servers/%d/meta/%s' % (server_id, key)
477 53b1eea6 Giorgos Verigakis
        data = json.dumps({'meta': meta})
478 53b1eea6 Giorgos Verigakis
        response = self.client.put(path, data, content_type='application/json')
479 53b1eea6 Giorgos Verigakis
        self.assertEqual(response.status_code, 201)
480 53b1eea6 Giorgos Verigakis
        reply = json.loads(response.content)
481 53b1eea6 Giorgos Verigakis
        self.assertEqual(reply.keys(), ['meta'])
482 53b1eea6 Giorgos Verigakis
        response_meta = reply['meta']
483 53b1eea6 Giorgos Verigakis
        self.assertEqual(response_meta, meta)
484 aa197ee4 Vangelis Koukis
485 53b1eea6 Giorgos Verigakis
    def get_all_server_metadata(self):
486 53b1eea6 Giorgos Verigakis
        metadata = defaultdict(dict)
487 53b1eea6 Giorgos Verigakis
        for m in VirtualMachineMetadata.objects.all():
488 53b1eea6 Giorgos Verigakis
            metadata[m.vm.id][m.meta_key] = m.meta_value
489 53b1eea6 Giorgos Verigakis
        return metadata
490 aa197ee4 Vangelis Koukis
491 53b1eea6 Giorgos Verigakis
    def get_all_image_metadata(self):
492 53b1eea6 Giorgos Verigakis
        metadata = defaultdict(dict)
493 53b1eea6 Giorgos Verigakis
        for m in ImageMetadata.objects.all():
494 53b1eea6 Giorgos Verigakis
            metadata[m.image.id][m.meta_key] = m.meta_value
495 53b1eea6 Giorgos Verigakis
        return metadata
496 aa197ee4 Vangelis Koukis
497 13b954b0 Giorgos Verigakis
    def list_networks(self, detail=False):
498 13b954b0 Giorgos Verigakis
        path = '/api/v1.1/networks'
499 13b954b0 Giorgos Verigakis
        if detail:
500 13b954b0 Giorgos Verigakis
            path += '/detail'
501 13b954b0 Giorgos Verigakis
        response = self.client.get(path)
502 13b954b0 Giorgos Verigakis
        self.assertTrue(response.status_code in (200, 203))
503 13b954b0 Giorgos Verigakis
        reply = json.loads(response.content)
504 13b954b0 Giorgos Verigakis
        self.assertEqual(reply.keys(), ['networks'])
505 13b954b0 Giorgos Verigakis
        self.assertEqual(reply['networks'].keys(), ['values'])
506 13b954b0 Giorgos Verigakis
        return reply['networks']['values']
507 aa197ee4 Vangelis Koukis
508 13b954b0 Giorgos Verigakis
    def create_network(self, name):
509 13b954b0 Giorgos Verigakis
        path = '/api/v1.1/networks'
510 13b954b0 Giorgos Verigakis
        data = json.dumps({'network': {'name': name}})
511 13b954b0 Giorgos Verigakis
        response = self.client.post(path, data, content_type='application/json')
512 13b954b0 Giorgos Verigakis
        self.assertEqual(response.status_code, 202)
513 13b954b0 Giorgos Verigakis
        reply = json.loads(response.content)
514 13b954b0 Giorgos Verigakis
        self.assertEqual(reply.keys(), ['network'])
515 13b954b0 Giorgos Verigakis
        return reply
516 aa197ee4 Vangelis Koukis
517 b19653d1 Giorgos Verigakis
    def get_network_details(self, network_id):
518 207b70d5 Giorgos Verigakis
        path = '/api/v1.1/networks/%s' % network_id
519 13b954b0 Giorgos Verigakis
        response = self.client.get(path)
520 13b954b0 Giorgos Verigakis
        self.assertEqual(response.status_code, 200)
521 13b954b0 Giorgos Verigakis
        reply = json.loads(response.content)
522 13b954b0 Giorgos Verigakis
        self.assertEqual(reply.keys(), ['network'])
523 13b954b0 Giorgos Verigakis
        return reply['network']
524 aa197ee4 Vangelis Koukis
525 b19653d1 Giorgos Verigakis
    def update_network_name(self, network_id, new_name):
526 207b70d5 Giorgos Verigakis
        path = '/api/v1.1/networks/%s' % network_id
527 13b954b0 Giorgos Verigakis
        data = json.dumps({'network': {'name': new_name}})
528 13b954b0 Giorgos Verigakis
        response = self.client.put(path, data, content_type='application/json')
529 13b954b0 Giorgos Verigakis
        self.assertEqual(response.status_code, 204)
530 aa197ee4 Vangelis Koukis
531 b19653d1 Giorgos Verigakis
    def delete_network(self, network_id):
532 207b70d5 Giorgos Verigakis
        path = '/api/v1.1/networks/%s' % network_id
533 13b954b0 Giorgos Verigakis
        response = self.client.delete(path)
534 13b954b0 Giorgos Verigakis
        self.assertEqual(response.status_code, 204)
535 aa197ee4 Vangelis Koukis
536 b19653d1 Giorgos Verigakis
    def add_to_network(self, network_id, server_id):
537 207b70d5 Giorgos Verigakis
        path = '/api/v1.1/networks/%s/action' % network_id
538 13b954b0 Giorgos Verigakis
        data = json.dumps({'add': {'serverRef': server_id}})
539 13b954b0 Giorgos Verigakis
        response = self.client.post(path, data, content_type='application/json')
540 13b954b0 Giorgos Verigakis
        self.assertEqual(response.status_code, 202)
541 aa197ee4 Vangelis Koukis
542 b19653d1 Giorgos Verigakis
    def remove_from_network(self, network_id, server_id):
543 207b70d5 Giorgos Verigakis
        path = '/api/v1.1/networks/%s/action' % network_id
544 13b954b0 Giorgos Verigakis
        data = json.dumps({'remove': {'serverRef': server_id}})
545 13b954b0 Giorgos Verigakis
        response = self.client.post(path, data, content_type='application/json')
546 13b954b0 Giorgos Verigakis
        self.assertEqual(response.status_code, 202)
547 13b954b0 Giorgos Verigakis
548 13b954b0 Giorgos Verigakis
549 13b954b0 Giorgos Verigakis
def popdict(l, **kwargs):
550 13b954b0 Giorgos Verigakis
    """Pops a dict from list `l` based on the predicates given as `kwargs`."""
551 aa197ee4 Vangelis Koukis
552 13b954b0 Giorgos Verigakis
    for i in range(len(l)):
553 13b954b0 Giorgos Verigakis
        item = l[i]
554 13b954b0 Giorgos Verigakis
        match = True
555 13b954b0 Giorgos Verigakis
        for key, val in kwargs.items():
556 13b954b0 Giorgos Verigakis
            if item[key] != val:
557 13b954b0 Giorgos Verigakis
                match = False
558 13b954b0 Giorgos Verigakis
                break
559 13b954b0 Giorgos Verigakis
        if match:
560 13b954b0 Giorgos Verigakis
            del l[i]
561 13b954b0 Giorgos Verigakis
            return item
562 13b954b0 Giorgos Verigakis
    return None
563 53b1eea6 Giorgos Verigakis
564 53b1eea6 Giorgos Verigakis
565 111f6d5b Giorgos Verigakis
class ListImages(BaseTestCase):
566 111f6d5b Giorgos Verigakis
    IMAGES = 10
567 aa197ee4 Vangelis Koukis
568 111f6d5b Giorgos Verigakis
    def test_list_images(self):
569 111f6d5b Giorgos Verigakis
        images = self.list_images()
570 111f6d5b Giorgos Verigakis
        keys = set(['id', 'name'])
571 111f6d5b Giorgos Verigakis
        for img in Image.objects.all():
572 13b954b0 Giorgos Verigakis
            image = popdict(images, id=img.id)
573 111f6d5b Giorgos Verigakis
            self.assertTrue(image is not None)
574 111f6d5b Giorgos Verigakis
            self.assertEqual(set(image.keys()), keys)
575 111f6d5b Giorgos Verigakis
            self.assertEqual(image['id'], img.id)
576 111f6d5b Giorgos Verigakis
            self.assertEqual(image['name'], img.name)
577 111f6d5b Giorgos Verigakis
        self.assertEqual(images, [])
578 aa197ee4 Vangelis Koukis
579 111f6d5b Giorgos Verigakis
    def test_list_images_detail(self):
580 111f6d5b Giorgos Verigakis
        images = self.list_images(detail=True)
581 111f6d5b Giorgos Verigakis
        keys = set(['id', 'name', 'updated', 'created', 'status', 'progress'])
582 111f6d5b Giorgos Verigakis
        for img in Image.objects.all():
583 13b954b0 Giorgos Verigakis
            image = popdict(images, id=img.id)
584 111f6d5b Giorgos Verigakis
            self.assertTrue(image is not None)
585 111f6d5b Giorgos Verigakis
            self.assertEqual(set(image.keys()), keys)
586 111f6d5b Giorgos Verigakis
            self.assertEqual(image['id'], img.id)
587 111f6d5b Giorgos Verigakis
            self.assertEqual(image['name'], img.name)
588 111f6d5b Giorgos Verigakis
            self.assertEqual(image['status'], img.state)
589 111f6d5b Giorgos Verigakis
            self.assertEqual(image['progress'], 100 if img.state == 'ACTIVE' else 0)
590 111f6d5b Giorgos Verigakis
        self.assertEqual(images, [])
591 111f6d5b Giorgos Verigakis
592 111f6d5b Giorgos Verigakis
593 53b1eea6 Giorgos Verigakis
class ListServerMetadata(BaseTestCase):
594 53b1eea6 Giorgos Verigakis
    SERVERS = 5
595 53b1eea6 Giorgos Verigakis
    SERVER_METADATA = 100
596 aa197ee4 Vangelis Koukis
597 53b1eea6 Giorgos Verigakis
    def test_list_metadata(self):
598 53b1eea6 Giorgos Verigakis
        with AssertInvariant(self.get_all_server_metadata) as metadata:
599 53b1eea6 Giorgos Verigakis
            for vm in VirtualMachine.objects.all():
600 53b1eea6 Giorgos Verigakis
                response_metadata = self.list_server_metadata(vm.id)
601 53b1eea6 Giorgos Verigakis
                self.assertEqual(response_metadata, metadata[vm.id])
602 aa197ee4 Vangelis Koukis
603 53b1eea6 Giorgos Verigakis
    def test_invalid_server(self):
604 53b1eea6 Giorgos Verigakis
        with AssertInvariant(self.get_all_server_metadata):
605 53b1eea6 Giorgos Verigakis
            response = self.client.get('/api/v1.1/servers/0/meta')
606 53b1eea6 Giorgos Verigakis
            self.assertItemNotFound(response)
607 53b1eea6 Giorgos Verigakis
608 53b1eea6 Giorgos Verigakis
609 53b1eea6 Giorgos Verigakis
class UpdateServerMetadata(BaseTestCase):
610 53b1eea6 Giorgos Verigakis
    SERVER_METADATA = 10
611 aa197ee4 Vangelis Koukis
612 968b3e04 Giorgos Verigakis
    def test_update_metadata(self):
613 53b1eea6 Giorgos Verigakis
        metadata = self.get_all_server_metadata()
614 53b1eea6 Giorgos Verigakis
        server_id = choice(metadata.keys())
615 ba6ded2a Giorgos Verigakis
        new_metadata = {}
616 53b1eea6 Giorgos Verigakis
        for key in sample(metadata[server_id].keys(), 3):
617 ba6ded2a Giorgos Verigakis
            new_metadata[key] = 'New %s value' % key
618 7cc3c7d9 Giorgos Verigakis
        response_metadata = self.update_server_metadata(server_id,
619 7cc3c7d9 Giorgos Verigakis
                                                        new_metadata)
620 53b1eea6 Giorgos Verigakis
        metadata[server_id].update(new_metadata)
621 7cc3c7d9 Giorgos Verigakis
        self.assertEqual(response_metadata, metadata[server_id])
622 53b1eea6 Giorgos Verigakis
        self.assertEqual(metadata, self.get_all_server_metadata())
623 38d247df Kostas Papadimitriou
624 968b3e04 Giorgos Verigakis
    def test_invalid_data(self):
625 53b1eea6 Giorgos Verigakis
        with AssertInvariant(self.get_all_server_metadata) as metadata:
626 53b1eea6 Giorgos Verigakis
            server_id = choice(metadata.keys())
627 53b1eea6 Giorgos Verigakis
            path = '/api/v1.1/servers/%d/meta' % server_id
628 53b1eea6 Giorgos Verigakis
            response = self.client.post(path, 'metadata', content_type='application/json')
629 53b1eea6 Giorgos Verigakis
            self.assertBadRequest(response)
630 aa197ee4 Vangelis Koukis
631 ba6ded2a Giorgos Verigakis
    def test_invalid_server(self):
632 53b1eea6 Giorgos Verigakis
        with AssertInvariant(self.get_all_server_metadata):
633 53b1eea6 Giorgos Verigakis
            path = '/api/v1.1/servers/0/meta'
634 53b1eea6 Giorgos Verigakis
            data = json.dumps({'metadata': {'Key1': 'A Value'}})
635 53b1eea6 Giorgos Verigakis
            response = self.client.post(path, data, content_type='application/json')
636 53b1eea6 Giorgos Verigakis
            self.assertItemNotFound(response)
637 968b3e04 Giorgos Verigakis
638 968b3e04 Giorgos Verigakis
639 968b3e04 Giorgos Verigakis
class GetServerMetadataItem(BaseTestCase):
640 53b1eea6 Giorgos Verigakis
    SERVERS = 5
641 53b1eea6 Giorgos Verigakis
    SERVER_METADATA = 100
642 aa197ee4 Vangelis Koukis
643 968b3e04 Giorgos Verigakis
    def test_get_metadata_item(self):
644 53b1eea6 Giorgos Verigakis
        with AssertInvariant(self.get_all_server_metadata) as metadata:
645 53b1eea6 Giorgos Verigakis
            server_id = choice(metadata.keys())
646 53b1eea6 Giorgos Verigakis
            key = choice(metadata[server_id].keys())
647 53b1eea6 Giorgos Verigakis
            path = '/api/v1.1/servers/%d/meta/%s' % (server_id, key)
648 53b1eea6 Giorgos Verigakis
            response = self.client.get(path)
649 53b1eea6 Giorgos Verigakis
            self.assertTrue(response.status_code in (200, 203))
650 53b1eea6 Giorgos Verigakis
            reply = json.loads(response.content)
651 53b1eea6 Giorgos Verigakis
            self.assertEqual(reply['meta'], {key: metadata[server_id][key]})
652 aa197ee4 Vangelis Koukis
653 ba6ded2a Giorgos Verigakis
    def test_invalid_key(self):
654 53b1eea6 Giorgos Verigakis
        with AssertInvariant(self.get_all_server_metadata) as metadata:
655 53b1eea6 Giorgos Verigakis
            server_id = choice(metadata.keys())
656 53b1eea6 Giorgos Verigakis
            response = self.client.get('/api/v1.1/servers/%d/meta/foo' % server_id)
657 53b1eea6 Giorgos Verigakis
            self.assertItemNotFound(response)
658 aa197ee4 Vangelis Koukis
659 ba6ded2a Giorgos Verigakis
    def test_invalid_server(self):
660 53b1eea6 Giorgos Verigakis
        with AssertInvariant(self.get_all_server_metadata):
661 53b1eea6 Giorgos Verigakis
            response = self.client.get('/api/v1.1/servers/0/meta/foo')
662 53b1eea6 Giorgos Verigakis
            self.assertItemNotFound(response)
663 968b3e04 Giorgos Verigakis
664 968b3e04 Giorgos Verigakis
665 968b3e04 Giorgos Verigakis
class CreateServerMetadataItem(BaseTestCase):
666 ba6ded2a Giorgos Verigakis
    SERVER_METADATA = 10
667 aa197ee4 Vangelis Koukis
668 968b3e04 Giorgos Verigakis
    def test_create_metadata(self):
669 53b1eea6 Giorgos Verigakis
        metadata = self.get_all_server_metadata()
670 53b1eea6 Giorgos Verigakis
        server_id = choice(metadata.keys())
671 ba6ded2a Giorgos Verigakis
        meta = {'Foo': 'Bar'}
672 53b1eea6 Giorgos Verigakis
        self.create_server_meta(server_id, meta)
673 53b1eea6 Giorgos Verigakis
        metadata[server_id].update(meta)
674 53b1eea6 Giorgos Verigakis
        self.assertEqual(metadata, self.get_all_server_metadata())
675 aa197ee4 Vangelis Koukis
676 ba6ded2a Giorgos Verigakis
    def test_update_metadata(self):
677 53b1eea6 Giorgos Verigakis
        metadata = self.get_all_server_metadata()
678 53b1eea6 Giorgos Verigakis
        server_id = choice(metadata.keys())
679 53b1eea6 Giorgos Verigakis
        key = choice(metadata[server_id].keys())
680 ba6ded2a Giorgos Verigakis
        meta = {key: 'New Value'}
681 53b1eea6 Giorgos Verigakis
        self.create_server_meta(server_id, meta)
682 53b1eea6 Giorgos Verigakis
        metadata[server_id].update(meta)
683 53b1eea6 Giorgos Verigakis
        self.assertEqual(metadata, self.get_all_server_metadata())
684 aa197ee4 Vangelis Koukis
685 968b3e04 Giorgos Verigakis
    def test_invalid_server(self):
686 53b1eea6 Giorgos Verigakis
        with AssertInvariant(self.get_all_server_metadata):
687 53b1eea6 Giorgos Verigakis
            path = '/api/v1.1/servers/0/meta/foo'
688 53b1eea6 Giorgos Verigakis
            data = json.dumps({'meta': {'foo': 'bar'}})
689 53b1eea6 Giorgos Verigakis
            response = self.client.put(path, data, content_type='application/json')
690 53b1eea6 Giorgos Verigakis
            self.assertItemNotFound(response)
691 aa197ee4 Vangelis Koukis
692 968b3e04 Giorgos Verigakis
    def test_invalid_key(self):
693 7317d130 Giorgos Verigakis
        with AssertInvariant(self.get_all_server_metadata) as metadata:
694 7317d130 Giorgos Verigakis
            server_id = choice(metadata.keys())
695 7317d130 Giorgos Verigakis
            path = '/api/v1.1/servers/%d/meta/baz' % server_id
696 53b1eea6 Giorgos Verigakis
            data = json.dumps({'meta': {'foo': 'bar'}})
697 53b1eea6 Giorgos Verigakis
            response = self.client.put(path, data, content_type='application/json')
698 53b1eea6 Giorgos Verigakis
            self.assertBadRequest(response)
699 aa197ee4 Vangelis Koukis
700 968b3e04 Giorgos Verigakis
    def test_invalid_data(self):
701 7317d130 Giorgos Verigakis
        with AssertInvariant(self.get_all_server_metadata) as metadata:
702 7317d130 Giorgos Verigakis
            server_id = choice(metadata.keys())
703 7317d130 Giorgos Verigakis
            path = '/api/v1.1/servers/%d/meta/foo' % server_id
704 53b1eea6 Giorgos Verigakis
            response = self.client.put(path, 'meta', content_type='application/json')
705 53b1eea6 Giorgos Verigakis
            self.assertBadRequest(response)
706 968b3e04 Giorgos Verigakis
707 968b3e04 Giorgos Verigakis
708 968b3e04 Giorgos Verigakis
class DeleteServerMetadataItem(BaseTestCase):
709 ba6ded2a Giorgos Verigakis
    SERVER_METADATA = 10
710 aa197ee4 Vangelis Koukis
711 968b3e04 Giorgos Verigakis
    def test_delete_metadata(self):
712 53b1eea6 Giorgos Verigakis
        metadata = self.get_all_server_metadata()
713 53b1eea6 Giorgos Verigakis
        server_id = choice(metadata.keys())
714 53b1eea6 Giorgos Verigakis
        key = choice(metadata[server_id].keys())
715 53b1eea6 Giorgos Verigakis
        path = '/api/v1.1/servers/%d/meta/%s' % (server_id, key)
716 ba6ded2a Giorgos Verigakis
        response = self.client.delete(path)
717 968b3e04 Giorgos Verigakis
        self.assertEqual(response.status_code, 204)
718 53b1eea6 Giorgos Verigakis
        metadata[server_id].pop(key)
719 53b1eea6 Giorgos Verigakis
        self.assertEqual(metadata, self.get_all_server_metadata())
720 aa197ee4 Vangelis Koukis
721 968b3e04 Giorgos Verigakis
    def test_invalid_server(self):
722 53b1eea6 Giorgos Verigakis
        with AssertInvariant(self.get_all_server_metadata):
723 53b1eea6 Giorgos Verigakis
            response = self.client.delete('/api/v1.1/servers/9/meta/Key1')
724 53b1eea6 Giorgos Verigakis
            self.assertItemNotFound(response)
725 aa197ee4 Vangelis Koukis
726 968b3e04 Giorgos Verigakis
    def test_invalid_key(self):
727 53b1eea6 Giorgos Verigakis
        with AssertInvariant(self.get_all_server_metadata) as metadata:
728 53b1eea6 Giorgos Verigakis
            server_id = choice(metadata.keys())
729 53b1eea6 Giorgos Verigakis
            path = '/api/v1.1/servers/%d/meta/foo' % server_id
730 53b1eea6 Giorgos Verigakis
            response = self.client.delete(path)
731 53b1eea6 Giorgos Verigakis
            self.assertItemNotFound(response)
732 53b1eea6 Giorgos Verigakis
733 53b1eea6 Giorgos Verigakis
734 53b1eea6 Giorgos Verigakis
class ListImageMetadata(BaseTestCase):
735 53b1eea6 Giorgos Verigakis
    IMAGES = 5
736 53b1eea6 Giorgos Verigakis
    IMAGE_METADATA = 100
737 53b1eea6 Giorgos Verigakis
738 53b1eea6 Giorgos Verigakis
    def test_list_metadata(self):
739 53b1eea6 Giorgos Verigakis
        with AssertInvariant(self.get_all_image_metadata) as metadata:
740 53b1eea6 Giorgos Verigakis
            for image in Image.objects.all():
741 53b1eea6 Giorgos Verigakis
                response_metadata = self.list_image_metadata(image.id)
742 53b1eea6 Giorgos Verigakis
                self.assertEqual(response_metadata, metadata[image.id])
743 53b1eea6 Giorgos Verigakis
744 53b1eea6 Giorgos Verigakis
    def test_invalid_image(self):
745 53b1eea6 Giorgos Verigakis
        with AssertInvariant(self.get_all_image_metadata):
746 53b1eea6 Giorgos Verigakis
            response = self.client.get('/api/v1.1/images/0/meta')
747 53b1eea6 Giorgos Verigakis
            self.assertItemNotFound(response)
748 53b1eea6 Giorgos Verigakis
749 c25cc9ec Vangelis Koukis
750 53b1eea6 Giorgos Verigakis
class UpdateImageMetadata(BaseTestCase):
751 53b1eea6 Giorgos Verigakis
    IMAGE_METADATA = 10
752 53b1eea6 Giorgos Verigakis
753 53b1eea6 Giorgos Verigakis
    def test_update_metadata(self):
754 53b1eea6 Giorgos Verigakis
        metadata = self.get_all_image_metadata()
755 53b1eea6 Giorgos Verigakis
        image_id = choice(metadata.keys())
756 53b1eea6 Giorgos Verigakis
        new_metadata = {}
757 53b1eea6 Giorgos Verigakis
        for key in sample(metadata[image_id].keys(), 3):
758 53b1eea6 Giorgos Verigakis
            new_metadata[key] = 'New %s value' % key
759 53b1eea6 Giorgos Verigakis
        response_metadata = self.update_image_metadata(image_id, new_metadata)
760 53b1eea6 Giorgos Verigakis
        metadata[image_id].update(new_metadata)
761 7cc3c7d9 Giorgos Verigakis
        self.assertEqual(response_metadata, metadata[image_id])
762 53b1eea6 Giorgos Verigakis
        self.assertEqual(metadata, self.get_all_image_metadata())
763 38d247df Kostas Papadimitriou
764 53b1eea6 Giorgos Verigakis
    def test_invalid_data(self):
765 53b1eea6 Giorgos Verigakis
        with AssertInvariant(self.get_all_image_metadata) as metadata:
766 53b1eea6 Giorgos Verigakis
            image_id = choice(metadata.keys())
767 53b1eea6 Giorgos Verigakis
            path = '/api/v1.1/images/%d/meta' % image_id
768 53b1eea6 Giorgos Verigakis
            response = self.client.post(path, 'metadata', content_type='application/json')
769 53b1eea6 Giorgos Verigakis
            self.assertBadRequest(response)
770 53b1eea6 Giorgos Verigakis
771 53b1eea6 Giorgos Verigakis
    def test_invalid_server(self):
772 53b1eea6 Giorgos Verigakis
        with AssertInvariant(self.get_all_image_metadata):
773 53b1eea6 Giorgos Verigakis
            path = '/api/v1.1/images/0/meta'
774 53b1eea6 Giorgos Verigakis
            data = json.dumps({'metadata': {'Key1': 'A Value'}})
775 53b1eea6 Giorgos Verigakis
            response = self.client.post(path, data, content_type='application/json')
776 53b1eea6 Giorgos Verigakis
            self.assertItemNotFound(response)
777 4cf8adf8 Vangelis Koukis
778 4cf8adf8 Vangelis Koukis
779 4cf8adf8 Vangelis Koukis
class ServerVNCConsole(BaseTestCase):
780 4cf8adf8 Vangelis Koukis
    SERVERS = 1
781 4cf8adf8 Vangelis Koukis
782 4cf8adf8 Vangelis Koukis
    def test_not_active_server(self):
783 4cf8adf8 Vangelis Koukis
        """Test console req for server not in ACTIVE state returns badRequest"""
784 4cf8adf8 Vangelis Koukis
        server_id = choice(VirtualMachine.objects.all()).id
785 4cf8adf8 Vangelis Koukis
        path = '/api/v1.1/servers/%d/action' % server_id
786 4cf8adf8 Vangelis Koukis
        data = json.dumps({'console': {'type': 'vnc'}})
787 4cf8adf8 Vangelis Koukis
        response = self.client.post(path, data, content_type='application/json')
788 4cf8adf8 Vangelis Koukis
        self.assertBadRequest(response)
789 4cf8adf8 Vangelis Koukis
790 4cf8adf8 Vangelis Koukis
    def test_active_server(self):
791 4cf8adf8 Vangelis Koukis
        """Test console req for ACTIVE server"""
792 4cf8adf8 Vangelis Koukis
        server_id = choice(VirtualMachine.objects.all()).id
793 4cf8adf8 Vangelis Koukis
        # FIXME: Start the server properly, instead of tampering with the DB
794 4cf8adf8 Vangelis Koukis
        vm = choice(VirtualMachine.objects.all())
795 4cf8adf8 Vangelis Koukis
        vm.operstate = 'STARTED'
796 4cf8adf8 Vangelis Koukis
        vm.save()
797 4cf8adf8 Vangelis Koukis
        server_id = vm.id
798 c25cc9ec Vangelis Koukis
799 4cf8adf8 Vangelis Koukis
        path = '/api/v1.1/servers/%d/action' % server_id
800 4cf8adf8 Vangelis Koukis
        data = json.dumps({'console': {'type': 'vnc'}})
801 4cf8adf8 Vangelis Koukis
        response = self.client.post(path, data, content_type='application/json')
802 4cf8adf8 Vangelis Koukis
        self.assertEqual(response.status_code, 200)
803 4cf8adf8 Vangelis Koukis
        reply = json.loads(response.content)
804 529178b1 Giorgos Verigakis
        self.assertEqual(reply.keys(), ['console'])
805 529178b1 Giorgos Verigakis
        console = reply['console']
806 529178b1 Giorgos Verigakis
        self.assertEqual(console['type'], 'vnc')
807 529178b1 Giorgos Verigakis
        self.assertEqual(set(console.keys()), set(['type', 'host', 'port', 'password']))
808 1428a464 Georgios Gousios
809 13b954b0 Giorgos Verigakis
810 1428a464 Georgios Gousios
class AaiTestCase(TestCase):
811 53481544 Vangelis Koukis
    fixtures = ['users', 'api_test_data', 'auth_test_data']
812 1428a464 Georgios Gousios
    apibase = '/api/v1.1'
813 1428a464 Georgios Gousios
814 aa519091 Georgios Gousios
    def setUp(self):
815 aa519091 Georgios Gousios
        self.client = Client()
816 1428a464 Georgios Gousios
817 1428a464 Georgios Gousios
    def test_fail_oapi_auth(self):
818 1428a464 Georgios Gousios
        """ test authentication from not registered user using OpenAPI
819 1428a464 Georgios Gousios
        """
820 1428a464 Georgios Gousios
        response = self.client.get(self.apibase + '/servers', {},
821 1428a464 Georgios Gousios
                                   **{'X-Auth-User': 'notme',
822 1428a464 Georgios Gousios
                                      'X-Auth-Key': '0xdeadbabe',
823 c25cc9ec Vangelis Koukis
                                      'TEST-AAI': 'true'})
824 1428a464 Georgios Gousios
        self.assertEquals(response.status_code, 401)
825 1428a464 Georgios Gousios
826 1428a464 Georgios Gousios
    def test_oapi_auth(self):
827 1428a464 Georgios Gousios
        """authentication with user registration
828 1428a464 Georgios Gousios
        """
829 1428a464 Georgios Gousios
        response = self.client.get(self.apibase + '/index.html', {},
830 1428a464 Georgios Gousios
                                   **{'X-Auth-User': 'testdbuser',
831 1428a464 Georgios Gousios
                                      'X-Auth-Key': 'test@synnefo.gr',
832 c25cc9ec Vangelis Koukis
                                      'TEST-AAI': 'true'})
833 1428a464 Georgios Gousios
        self.assertEquals(response.status_code, 204)
834 1428a464 Georgios Gousios
        self.assertNotEqual(response['X-Auth-Token'], None)
835 1428a464 Georgios Gousios
        self.assertEquals(response['X-Server-Management-Url'], '')
836 1428a464 Georgios Gousios
        self.assertEquals(response['X-Storage-Url'], '')
837 1428a464 Georgios Gousios
        self.assertEquals(response['X-CDN-Management-Url'], '')
838 aa519091 Georgios Gousios
839 aa519091 Georgios Gousios
    def test_unauthorized_call(self):
840 aa519091 Georgios Gousios
        request = {'reboot': {'type': 'HARD'}}
841 aa519091 Georgios Gousios
        path = '/api/v1.1/servers/%d/action' % 1
842 6a0e1a00 Georgios Gousios
        response = self.client.post(path, json.dumps(request),
843 6a0e1a00 Georgios Gousios
                                    content_type='application/json')
844 865fd434 Vangelis Koukis
        self.assertEquals(response.status_code, 401)
845 865fd434 Vangelis Koukis
846 865fd434 Vangelis Koukis
847 13b954b0 Giorgos Verigakis
class ListNetworks(BaseTestCase):
848 13b954b0 Giorgos Verigakis
    SERVERS = 5
849 aa197ee4 Vangelis Koukis
850 13b954b0 Giorgos Verigakis
    def setUp(self):
851 13b954b0 Giorgos Verigakis
        BaseTestCase.setUp(self)
852 38d247df Kostas Papadimitriou
853 a3992827 Giorgos Verigakis
        for i in range(5):
854 a3992827 Giorgos Verigakis
            self.create_network('net%d' % i)
855 38d247df Kostas Papadimitriou
856 13b954b0 Giorgos Verigakis
        machines = VirtualMachine.objects.all()
857 13b954b0 Giorgos Verigakis
        for network in Network.objects.all():
858 d54cd53a Giorgos Verigakis
            n = randint(0, self.SERVERS - 1)
859 d44c236b Giorgos Verigakis
            for machine in sample(machines, n):
860 d44c236b Giorgos Verigakis
                machine.nics.create(network=network)
861 38d247df Kostas Papadimitriou
862 13b954b0 Giorgos Verigakis
    def test_list_networks(self):
863 13b954b0 Giorgos Verigakis
        networks = self.list_networks()
864 13b954b0 Giorgos Verigakis
        for net in Network.objects.all():
865 207b70d5 Giorgos Verigakis
            net_id = str(net.id) if not net.public else 'public'
866 207b70d5 Giorgos Verigakis
            network = popdict(networks, id=net_id)
867 b19653d1 Giorgos Verigakis
            self.assertEqual(network['name'], net.name)
868 13b954b0 Giorgos Verigakis
        self.assertEqual(networks, [])
869 aa197ee4 Vangelis Koukis
870 13b954b0 Giorgos Verigakis
    def test_list_networks_detail(self):
871 13b954b0 Giorgos Verigakis
        networks = self.list_networks(detail=True)
872 13b954b0 Giorgos Verigakis
        for net in Network.objects.all():
873 207b70d5 Giorgos Verigakis
            net_id = str(net.id) if not net.public else 'public'
874 207b70d5 Giorgos Verigakis
            network = popdict(networks, id=net_id)
875 b19653d1 Giorgos Verigakis
            self.assertEqual(network['name'], net.name)
876 13b954b0 Giorgos Verigakis
            machines = set(vm.id for vm in net.machines.all())
877 13b954b0 Giorgos Verigakis
            self.assertEqual(set(network['servers']['values']), machines)
878 13b954b0 Giorgos Verigakis
        self.assertEqual(networks, [])
879 13b954b0 Giorgos Verigakis
880 13b954b0 Giorgos Verigakis
881 13b954b0 Giorgos Verigakis
class CreateNetwork(BaseTestCase):
882 13b954b0 Giorgos Verigakis
    def test_create_network(self):
883 d44c236b Giorgos Verigakis
        before = self.list_networks()
884 13b954b0 Giorgos Verigakis
        self.create_network('net')
885 d44c236b Giorgos Verigakis
        after = self.list_networks()
886 d44c236b Giorgos Verigakis
        self.assertEqual(len(after) - len(before), 1)
887 d44c236b Giorgos Verigakis
        found = False
888 d44c236b Giorgos Verigakis
        for network in after:
889 d44c236b Giorgos Verigakis
            if network['name'] == 'net':
890 d44c236b Giorgos Verigakis
                found = True
891 d44c236b Giorgos Verigakis
                break
892 d44c236b Giorgos Verigakis
        self.assertTrue(found)
893 13b954b0 Giorgos Verigakis
894 13b954b0 Giorgos Verigakis
895 13b954b0 Giorgos Verigakis
class GetNetworkDetails(BaseTestCase):
896 13b954b0 Giorgos Verigakis
    SERVERS = 5
897 c25cc9ec Vangelis Koukis
898 13b954b0 Giorgos Verigakis
    def test_get_network_details(self):
899 64938cb0 Giorgos Verigakis
        name = 'net'
900 64938cb0 Giorgos Verigakis
        self.create_network(name)
901 38d247df Kostas Papadimitriou
902 13b954b0 Giorgos Verigakis
        servers = VirtualMachine.objects.all()
903 64938cb0 Giorgos Verigakis
        network = Network.objects.all()[1]
904 aa197ee4 Vangelis Koukis
905 b19653d1 Giorgos Verigakis
        net = self.get_network_details(network.id)
906 64938cb0 Giorgos Verigakis
        self.assertEqual(net['name'], name)
907 13b954b0 Giorgos Verigakis
        self.assertEqual(net['servers']['values'], [])
908 aa197ee4 Vangelis Koukis
909 13b954b0 Giorgos Verigakis
        server_id = choice(servers).id
910 b19653d1 Giorgos Verigakis
        self.add_to_network(network.id, server_id)
911 b19653d1 Giorgos Verigakis
        net = self.get_network_details(network.id)
912 b19653d1 Giorgos Verigakis
        self.assertEqual(net['name'], network.name)
913 13b954b0 Giorgos Verigakis
914 13b954b0 Giorgos Verigakis
915 13b954b0 Giorgos Verigakis
class UpdateNetworkName(BaseTestCase):
916 13b954b0 Giorgos Verigakis
    def test_update_network_name(self):
917 207b70d5 Giorgos Verigakis
        name = 'net'
918 207b70d5 Giorgos Verigakis
        self.create_network(name)
919 13b954b0 Giorgos Verigakis
        networks = self.list_networks(detail=True)
920 207b70d5 Giorgos Verigakis
        priv = [net for net in networks if net['id'] != 'public']
921 207b70d5 Giorgos Verigakis
        network = choice(priv)
922 b19653d1 Giorgos Verigakis
        network_id = network['id']
923 b19653d1 Giorgos Verigakis
        new_name = network['name'] + '_2'
924 b19653d1 Giorgos Verigakis
        self.update_network_name(network_id, new_name)
925 aa197ee4 Vangelis Koukis
926 13b954b0 Giorgos Verigakis
        network['name'] = new_name
927 197398e2 Giorgos Verigakis
        del network['updated']
928 197398e2 Giorgos Verigakis
        net = self.get_network_details(network_id)
929 197398e2 Giorgos Verigakis
        del net['updated']
930 197398e2 Giorgos Verigakis
        self.assertEqual(net, network)
931 13b954b0 Giorgos Verigakis
932 13b954b0 Giorgos Verigakis
933 13b954b0 Giorgos Verigakis
class DeleteNetwork(BaseTestCase):
934 13b954b0 Giorgos Verigakis
    def test_delete_network(self):
935 64938cb0 Giorgos Verigakis
        for i in range(5):
936 64938cb0 Giorgos Verigakis
            self.create_network('net%d' % i)
937 38d247df Kostas Papadimitriou
938 13b954b0 Giorgos Verigakis
        networks = self.list_networks()
939 207b70d5 Giorgos Verigakis
        priv = [net for net in networks if net['id'] != 'public']
940 207b70d5 Giorgos Verigakis
        network = choice(priv)
941 b19653d1 Giorgos Verigakis
        network_id = network['id']
942 b19653d1 Giorgos Verigakis
        self.delete_network(network_id)
943 38d247df Kostas Papadimitriou
944 d44c236b Giorgos Verigakis
        net = self.get_network_details(network_id)
945 d44c236b Giorgos Verigakis
        self.assertEqual(net['status'], 'DELETED')
946 aa197ee4 Vangelis Koukis
947 207b70d5 Giorgos Verigakis
        priv.remove(network)
948 207b70d5 Giorgos Verigakis
        networks = self.list_networks()
949 207b70d5 Giorgos Verigakis
        new_priv = [net for net in networks if net['id'] != 'public']
950 207b70d5 Giorgos Verigakis
        self.assertEqual(priv, new_priv)
951 13b954b0 Giorgos Verigakis
952 13b954b0 Giorgos Verigakis
953 13b954b0 Giorgos Verigakis
class NetworkActions(BaseTestCase):
954 13b954b0 Giorgos Verigakis
    SERVERS = 20
955 aa197ee4 Vangelis Koukis
956 13b954b0 Giorgos Verigakis
    def test_add_remove_server(self):
957 64938cb0 Giorgos Verigakis
        self.create_network('net')
958 38d247df Kostas Papadimitriou
959 13b954b0 Giorgos Verigakis
        server_ids = [vm.id for vm in VirtualMachine.objects.all()]
960 64938cb0 Giorgos Verigakis
        network = self.list_networks(detail=True)[1]
961 b19653d1 Giorgos Verigakis
        network_id = network['id']
962 aa197ee4 Vangelis Koukis
963 13b954b0 Giorgos Verigakis
        to_add = set(sample(server_ids, 10))
964 13b954b0 Giorgos Verigakis
        for server_id in to_add:
965 b19653d1 Giorgos Verigakis
            self.add_to_network(network_id, server_id)
966 38d247df Kostas Papadimitriou
967 13b954b0 Giorgos Verigakis
        to_remove = set(sample(to_add, 5))
968 13b954b0 Giorgos Verigakis
        for server_id in to_remove:
969 b19653d1 Giorgos Verigakis
            self.remove_from_network(network_id, server_id)