Statistics
| Branch: | Tag: | Revision:

root / api / tests.py @ 6130effc

History | View | Annotate | Download (18.3 kB)

1 894f6b7e Dimitris Moraitis
#
2 894f6b7e Dimitris Moraitis
# Unit Tests for api
3 894f6b7e Dimitris Moraitis
#
4 285a1fa8 Dimitris Moraitis
# Provides automated tests for api module
5 894f6b7e Dimitris Moraitis
#
6 285a1fa8 Dimitris Moraitis
# Copyright 2011 Greek Research and Technology Network
7 894f6b7e Dimitris Moraitis
#
8 894f6b7e Dimitris Moraitis
9 894f6b7e Dimitris Moraitis
from django.test import TestCase
10 894f6b7e Dimitris Moraitis
from django.test.client import Client
11 7de0c985 Dimitris Moraitis
import simplejson as json
12 a8acbbac Markos Gogoulos
from synnefo.db.models import VirtualMachine, Flavor, Image, VirtualMachineGroup
13 894f6b7e Dimitris Moraitis
14 894f6b7e Dimitris Moraitis
class APITestCase(TestCase):
15 6781c0d0 Dimitris Moraitis
    fixtures = [ 'api_test_data' ]
16 d2e5f1d1 Markos Gogoulos
    test_server_id = 1001
17 d2e5f1d1 Markos Gogoulos
    test_image_id = 1
18 d2e5f1d1 Markos Gogoulos
    test_flavor_id = 1
19 a8acbbac Markos Gogoulos
    test_group_id = 1
20 d2e5f1d1 Markos Gogoulos
    test_wrong_server_id = 99999999
21 d2e5f1d1 Markos Gogoulos
    test_wrong_image_id = 99999999
22 d2e5f1d1 Markos Gogoulos
    test_wrong_flavor_id = 99999999
23 a8acbbac Markos Gogoulos
    test_wrong_group_id = 99999999
24 d2e5f1d1 Markos Gogoulos
    #make the testing with these id's
25 d2e5f1d1 Markos Gogoulos
26 6781c0d0 Dimitris Moraitis
27 894f6b7e Dimitris Moraitis
    def setUp(self):
28 7de0c985 Dimitris Moraitis
        self.client = Client()
29 894f6b7e Dimitris Moraitis
30 894f6b7e Dimitris Moraitis
    def testAPIVersion(self):
31 285a1fa8 Dimitris Moraitis
        """ check rackspace cloud servers API version
32 6781c0d0 Dimitris Moraitis
        """
33 7de0c985 Dimitris Moraitis
        response = self.client.get('/api/v1.0/')
34 894f6b7e Dimitris Moraitis
        # Check that the response is 200 OK.
35 894f6b7e Dimitris Moraitis
        self.assertEqual(response.status_code, 200)
36 54c30633 Markos Gogoulos
        api_version = json.loads(response.content)['version']
37 54c30633 Markos Gogoulos
        self.assertEqual(api_version['status'], 'CURRENT')
38 54c30633 Markos Gogoulos
        self.assertEqual(api_version['wadl'], 'http://docs.rackspacecloud.com/servers/api/v1.0/application.wadl')
39 54c30633 Markos Gogoulos
        self.assertEqual(api_version['docURL'], 'http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf')
40 54c30633 Markos Gogoulos
        self.assertEqual(api_version['id'], 'v1.0')
41 54c30633 Markos Gogoulos
42 54c30633 Markos Gogoulos
43 6781c0d0 Dimitris Moraitis
    def testServerList(self):
44 6781c0d0 Dimitris Moraitis
        """ test if the expected list of servers is returned by the API
45 6781c0d0 Dimitris Moraitis
        """        
46 7de0c985 Dimitris Moraitis
        response = self.client.get('/api/v1.0/servers')
47 6781c0d0 Dimitris Moraitis
        vms_from_api = json.loads(response.content)['servers']
48 668c1361 Markos Gogoulos
        vms_from_db = VirtualMachine.objects.filter(deleted=False)
49 6781c0d0 Dimitris Moraitis
        self.assertEqual(len(vms_from_api), len(vms_from_db))
50 7de0c985 Dimitris Moraitis
        self.assertTrue(response.status_code in [200,203])
51 54c30633 Markos Gogoulos
        for vm_from_api in vms_from_api:
52 54c30633 Markos Gogoulos
            vm_from_db = VirtualMachine.objects.get(id=vm_from_api['id'])
53 54c30633 Markos Gogoulos
            self.assertEqual(vm_from_api['id'], vm_from_db.id)
54 54c30633 Markos Gogoulos
            self.assertEqual(vm_from_api['name'], vm_from_db.name)
55 54c30633 Markos Gogoulos
56 7de0c985 Dimitris Moraitis
57 668c1361 Markos Gogoulos
    def testServerDetails(self):
58 668c1361 Markos Gogoulos
        """ test if the expected server is returned by the API
59 668c1361 Markos Gogoulos
        """        
60 d2e5f1d1 Markos Gogoulos
        response = self.client.get('/api/v1.0/servers/' + str(self.test_server_id))
61 668c1361 Markos Gogoulos
        vm_from_api = json.loads(response.content)['server']
62 d2e5f1d1 Markos Gogoulos
        vm_from_db = VirtualMachine.objects.get(id=self.test_server_id)
63 668c1361 Markos Gogoulos
        self.assertEqual(vm_from_api['flavorId'], vm_from_db.flavor.id)
64 668c1361 Markos Gogoulos
        self.assertEqual(vm_from_api['hostId'], vm_from_db.hostid)
65 668c1361 Markos Gogoulos
        self.assertEqual(vm_from_api['id'], vm_from_db.id)
66 668c1361 Markos Gogoulos
        self.assertEqual(vm_from_api['imageId'], vm_from_db.flavor.id)
67 668c1361 Markos Gogoulos
        self.assertEqual(vm_from_api['name'], vm_from_db.name)
68 668c1361 Markos Gogoulos
        self.assertEqual(vm_from_api['status'], vm_from_db.rsapi_state)
69 668c1361 Markos Gogoulos
        self.assertTrue(response.status_code in [200,203])
70 668c1361 Markos Gogoulos
71 54c30633 Markos Gogoulos
72 685ab2b6 Markos Gogoulos
    def testServersDetails(self):
73 685ab2b6 Markos Gogoulos
        """ test if the servers details are returned by the API
74 685ab2b6 Markos Gogoulos
        """
75 685ab2b6 Markos Gogoulos
        response = self.client.get('/api/v1.0/servers/detail')     
76 54c30633 Markos Gogoulos
        vms_from_db = VirtualMachine.objects.filter(deleted=False)
77 54c30633 Markos Gogoulos
        id_list = [vm.id for vm in vms_from_db]
78 685ab2b6 Markos Gogoulos
        number = 0
79 685ab2b6 Markos Gogoulos
        for vm_id in id_list:
80 685ab2b6 Markos Gogoulos
            vm_from_api = json.loads(response.content)['servers'][number]
81 685ab2b6 Markos Gogoulos
            vm_from_db = VirtualMachine.objects.get(id=vm_id)
82 685ab2b6 Markos Gogoulos
            self.assertEqual(vm_from_api['flavorId'], vm_from_db.flavor.id)
83 685ab2b6 Markos Gogoulos
            self.assertEqual(vm_from_api['hostId'], vm_from_db.hostid)
84 685ab2b6 Markos Gogoulos
            self.assertEqual(vm_from_api['id'], vm_from_db.id)
85 685ab2b6 Markos Gogoulos
            self.assertEqual(vm_from_api['imageId'], vm_from_db.flavor.id)
86 685ab2b6 Markos Gogoulos
            self.assertEqual(vm_from_api['name'], vm_from_db.name)
87 685ab2b6 Markos Gogoulos
            self.assertEqual(vm_from_api['status'], vm_from_db.rsapi_state)
88 685ab2b6 Markos Gogoulos
            number += 1
89 54c30633 Markos Gogoulos
        vms_from_api = json.loads(response.content)['servers']
90 54c30633 Markos Gogoulos
        for vm_from_api in vms_from_api:
91 54c30633 Markos Gogoulos
            vm_from_db = VirtualMachine.objects.get(id=vm_from_api['id'])
92 54c30633 Markos Gogoulos
            self.assertEqual(vm_from_api['flavorId'], vm_from_db.flavor.id)
93 54c30633 Markos Gogoulos
            self.assertEqual(vm_from_api['hostId'], vm_from_db.hostid)
94 54c30633 Markos Gogoulos
            self.assertEqual(vm_from_api['id'], vm_from_db.id)
95 54c30633 Markos Gogoulos
            self.assertEqual(vm_from_api['imageId'], vm_from_db.flavor.id)
96 54c30633 Markos Gogoulos
            self.assertEqual(vm_from_api['name'], vm_from_db.name)
97 54c30633 Markos Gogoulos
            self.assertEqual(vm_from_api['status'], vm_from_db.rsapi_state)            
98 685ab2b6 Markos Gogoulos
        self.assertTrue(response.status_code in [200,203])
99 685ab2b6 Markos Gogoulos
100 54c30633 Markos Gogoulos
101 685ab2b6 Markos Gogoulos
    def testWrongServer(self):
102 685ab2b6 Markos Gogoulos
        """ test if a non existent server is asked, if a 404 itemNotFound returned
103 685ab2b6 Markos Gogoulos
        """
104 d2e5f1d1 Markos Gogoulos
        response = self.client.get('/api/v1.0/servers/' + str(self.test_wrong_server_id))
105 685ab2b6 Markos Gogoulos
        self.assertEqual(response.status_code, 404)
106 685ab2b6 Markos Gogoulos
107 54c30633 Markos Gogoulos
108 7de0c985 Dimitris Moraitis
    def testCreateServerEmpty(self):
109 7de0c985 Dimitris Moraitis
        """ test if the create server call returns a 400 badRequest if no
110 7de0c985 Dimitris Moraitis
            attributes are specified
111 7de0c985 Dimitris Moraitis
        """
112 7de0c985 Dimitris Moraitis
        response = self.client.post('/api/v1.0/servers',{})
113 7de0c985 Dimitris Moraitis
        self.assertEqual(response.status_code, 400)
114 54c30633 Markos Gogoulos
115 54c30633 Markos Gogoulos
116 7de0c985 Dimitris Moraitis
    def testCreateServer(self):
117 685ab2b6 Markos Gogoulos
        """ test if the create server call returns the expected response
118 685ab2b6 Markos Gogoulos
            if a valid request has been speficied
119 7de0c985 Dimitris Moraitis
        """
120 7de0c985 Dimitris Moraitis
        request = {
121 285a1fa8 Dimitris Moraitis
                    "server": {
122 285a1fa8 Dimitris Moraitis
                        "name"          : "new-server-test",
123 285a1fa8 Dimitris Moraitis
                        "imageId"       : 1,
124 285a1fa8 Dimitris Moraitis
                        "flavorId"      : 1,
125 285a1fa8 Dimitris Moraitis
                        "metadata"      : {
126 285a1fa8 Dimitris Moraitis
                            "My Server Name": "Apache1"
127 285a1fa8 Dimitris Moraitis
                        },
128 285a1fa8 Dimitris Moraitis
                        "personality"   : []
129 285a1fa8 Dimitris Moraitis
                    }
130 7de0c985 Dimitris Moraitis
        }
131 685ab2b6 Markos Gogoulos
        response = self.client.post('/api/v1.0/servers', 
132 685ab2b6 Markos Gogoulos
                                    json.dumps(request), 
133 685ab2b6 Markos Gogoulos
                                    content_type='application/json')
134 285a1fa8 Dimitris Moraitis
        self.assertEqual(response.status_code, 202)
135 668c1361 Markos Gogoulos
        #TODO: check response.content      
136 668c1361 Markos Gogoulos
        #TODO: check create server with wrong options (eg flavor that not exist)
137 668c1361 Markos Gogoulos
    
138 668c1361 Markos Gogoulos
139 668c1361 Markos Gogoulos
    def testRebootServer(self):
140 668c1361 Markos Gogoulos
        """ test if the specified server is rebooted
141 668c1361 Markos Gogoulos
        """
142 685ab2b6 Markos Gogoulos
        request = {
143 685ab2b6 Markos Gogoulos
            "reboot": '{"type" : "HARD"}'
144 685ab2b6 Markos Gogoulos
            }
145 d2e5f1d1 Markos Gogoulos
        response = self.client.post('/api/v1.0/servers/' + str(self.test_server_id) + '/action', 
146 685ab2b6 Markos Gogoulos
                                    json.dumps(request),
147 685ab2b6 Markos Gogoulos
                                    content_type='application/json')  
148 685ab2b6 Markos Gogoulos
        self.assertEqual(response.status_code, 202)
149 685ab2b6 Markos Gogoulos
        #server id that does not exist
150 d2e5f1d1 Markos Gogoulos
        response = self.client.post('/api/v1.0/servers/' + str(self.test_wrong_server_id) + '/action', 
151 685ab2b6 Markos Gogoulos
                                   json.dumps(request), 
152 685ab2b6 Markos Gogoulos
                                   content_type='application/json')
153 685ab2b6 Markos Gogoulos
        self.assertEqual(response.status_code, 404)
154 668c1361 Markos Gogoulos
155 668c1361 Markos Gogoulos
156 668c1361 Markos Gogoulos
    def testShutdownServer(self):
157 668c1361 Markos Gogoulos
        """ test if the specified server is shutdown
158 668c1361 Markos Gogoulos
        """
159 685ab2b6 Markos Gogoulos
        request = {
160 685ab2b6 Markos Gogoulos
            "shutdown": {"timeout" : "5"}
161 685ab2b6 Markos Gogoulos
            }
162 d2e5f1d1 Markos Gogoulos
        response = self.client.post('/api/v1.0/servers/' + str(self.test_server_id) + '/action',
163 685ab2b6 Markos Gogoulos
                                    json.dumps(request), 
164 685ab2b6 Markos Gogoulos
                                    content_type='application/json')  
165 685ab2b6 Markos Gogoulos
        self.assertEqual(response.status_code, 202)
166 685ab2b6 Markos Gogoulos
        #server id that does not exist
167 d2e5f1d1 Markos Gogoulos
        response = self.client.post('/api/v1.0/servers/' + str(self.test_wrong_server_id) + '/action',
168 685ab2b6 Markos Gogoulos
                                    json.dumps(request), 
169 685ab2b6 Markos Gogoulos
                                    content_type='application/json')
170 685ab2b6 Markos Gogoulos
        self.assertEqual(response.status_code, 404)
171 668c1361 Markos Gogoulos
172 668c1361 Markos Gogoulos
173 668c1361 Markos Gogoulos
    def testStartServer(self):
174 668c1361 Markos Gogoulos
        """ test if the specified server is started
175 668c1361 Markos Gogoulos
        """
176 685ab2b6 Markos Gogoulos
        request = {
177 685ab2b6 Markos Gogoulos
            "start": {"type" : "NORMAL"}
178 685ab2b6 Markos Gogoulos
            }
179 d2e5f1d1 Markos Gogoulos
        response = self.client.post('/api/v1.0/servers/' + str(self.test_server_id) + '/action', 
180 685ab2b6 Markos Gogoulos
                                    json.dumps(request),
181 685ab2b6 Markos Gogoulos
                                    content_type='application/json')  
182 685ab2b6 Markos Gogoulos
        self.assertEqual(response.status_code, 202)
183 685ab2b6 Markos Gogoulos
        #server id that does not exist
184 d2e5f1d1 Markos Gogoulos
        response = self.client.post('/api/v1.0/servers/' + str(self.test_wrong_server_id) + '/action', 
185 685ab2b6 Markos Gogoulos
                                    json.dumps(request), 
186 685ab2b6 Markos Gogoulos
                                    content_type='application/json')
187 685ab2b6 Markos Gogoulos
        self.assertEqual(response.status_code, 404)
188 668c1361 Markos Gogoulos
189 668c1361 Markos Gogoulos
    def testDeleteServer(self):
190 668c1361 Markos Gogoulos
        """ test if the specified server is deleted
191 668c1361 Markos Gogoulos
        """
192 d2e5f1d1 Markos Gogoulos
        response = self.client.delete('/api/v1.0/servers/' + str(self.test_server_id))
193 685ab2b6 Markos Gogoulos
        self.assertEqual(response.status_code, 202)
194 685ab2b6 Markos Gogoulos
        #server id that does not exist      
195 d2e5f1d1 Markos Gogoulos
        response = self.client.delete('/api/v1.0/servers/' + str(self.test_wrong_server_id))  
196 685ab2b6 Markos Gogoulos
        self.assertEqual(response.status_code, 404)
197 668c1361 Markos Gogoulos
198 668c1361 Markos Gogoulos
199 668c1361 Markos Gogoulos
    def testFlavorList(self):
200 668c1361 Markos Gogoulos
        """ test if the expected list of flavors is returned by the API
201 668c1361 Markos Gogoulos
        """        
202 668c1361 Markos Gogoulos
        response = self.client.get('/api/v1.0/flavors')
203 668c1361 Markos Gogoulos
        flavors_from_api = json.loads(response.content)['flavors']
204 668c1361 Markos Gogoulos
        flavors_from_db = Flavor.objects.all()
205 668c1361 Markos Gogoulos
        self.assertEqual(len(flavors_from_api), len(flavors_from_db))
206 668c1361 Markos Gogoulos
        self.assertTrue(response.status_code in [200,203])
207 54c30633 Markos Gogoulos
        for flavor_from_api in flavors_from_api:
208 54c30633 Markos Gogoulos
            flavor_from_db = Flavor.objects.get(id=flavor_from_api['id'])
209 54c30633 Markos Gogoulos
            self.assertEqual(flavor_from_api['id'], flavor_from_db.id)
210 54c30633 Markos Gogoulos
            self.assertEqual(flavor_from_api['name'], flavor_from_db.name)
211 668c1361 Markos Gogoulos
212 668c1361 Markos Gogoulos
213 685ab2b6 Markos Gogoulos
    def testFlavorsDetails(self):
214 685ab2b6 Markos Gogoulos
        """ test if the flavors details are returned by the API
215 668c1361 Markos Gogoulos
        """
216 f3af8b40 Dimitris Moraitis
        response = self.client.get('/api/v1.0/flavors/detail')
217 f3af8b40 Dimitris Moraitis
        flavors_from_db = Flavor.objects.all()
218 f3af8b40 Dimitris Moraitis
        flavors_from_api = json.loads(response.content)['flavors']
219 f3af8b40 Dimitris Moraitis
        
220 f3af8b40 Dimitris Moraitis
        # Assert that all flavors in the db appear inthe API call result
221 f3af8b40 Dimitris Moraitis
        for i in range(0, len(flavors_from_db)):
222 f3af8b40 Dimitris Moraitis
            flavor_from_api = flavors_from_api[i]
223 f3af8b40 Dimitris Moraitis
            flavor_from_db = Flavor.objects.get(id=flavors_from_db[i].id)
224 685ab2b6 Markos Gogoulos
            self.assertEqual(flavor_from_api['cpu'], flavor_from_db.cpu)
225 685ab2b6 Markos Gogoulos
            self.assertEqual(flavor_from_api['id'], flavor_from_db.id)
226 685ab2b6 Markos Gogoulos
            self.assertEqual(flavor_from_api['disk'], flavor_from_db.disk)
227 685ab2b6 Markos Gogoulos
            self.assertEqual(flavor_from_api['name'], flavor_from_db.name)
228 685ab2b6 Markos Gogoulos
            self.assertEqual(flavor_from_api['ram'], flavor_from_db.ram)
229 f3af8b40 Dimitris Moraitis
230 f3af8b40 Dimitris Moraitis
        # Assert that all flavors that appear in the API call result are also in the db             
231 54c30633 Markos Gogoulos
        for flavor_from_api in flavors_from_api:
232 54c30633 Markos Gogoulos
            flavor_from_db = Flavor.objects.get(id=flavor_from_api['id'])
233 54c30633 Markos Gogoulos
            self.assertEqual(flavor_from_api['cpu'], flavor_from_db.cpu)
234 54c30633 Markos Gogoulos
            self.assertEqual(flavor_from_api['id'], flavor_from_db.id)
235 54c30633 Markos Gogoulos
            self.assertEqual(flavor_from_api['disk'], flavor_from_db.disk)
236 54c30633 Markos Gogoulos
            self.assertEqual(flavor_from_api['name'], flavor_from_db.name)
237 54c30633 Markos Gogoulos
            self.assertEqual(flavor_from_api['ram'], flavor_from_db.ram)
238 f3af8b40 Dimitris Moraitis
        
239 f3af8b40 Dimitris Moraitis
        # Check if we have the right status_code
240 685ab2b6 Markos Gogoulos
        self.assertTrue(response.status_code in [200,203])
241 7de0c985 Dimitris Moraitis
242 7de0c985 Dimitris Moraitis
243 685ab2b6 Markos Gogoulos
    def testFlavorDetails(self):
244 685ab2b6 Markos Gogoulos
        """ test if the expected flavor is returned by the API
245 668c1361 Markos Gogoulos
        """
246 d2e5f1d1 Markos Gogoulos
        response = self.client.get('/api/v1.0/flavors/' + str(self.test_flavor_id))
247 668c1361 Markos Gogoulos
        flavor_from_api = json.loads(response.content)['flavor']
248 d2e5f1d1 Markos Gogoulos
        flavor_from_db = Flavor.objects.get(id=self.test_flavor_id)
249 668c1361 Markos Gogoulos
        self.assertEqual(flavor_from_api['cpu'], flavor_from_db.cpu)
250 668c1361 Markos Gogoulos
        self.assertEqual(flavor_from_api['id'], flavor_from_db.id)
251 668c1361 Markos Gogoulos
        self.assertEqual(flavor_from_api['disk'], flavor_from_db.disk)
252 668c1361 Markos Gogoulos
        self.assertEqual(flavor_from_api['name'], flavor_from_db.name)
253 668c1361 Markos Gogoulos
        self.assertEqual(flavor_from_api['ram'], flavor_from_db.ram)
254 668c1361 Markos Gogoulos
        self.assertTrue(response.status_code in [200,203])
255 668c1361 Markos Gogoulos
256 668c1361 Markos Gogoulos
257 668c1361 Markos Gogoulos
    def testWrongFlavor(self):
258 668c1361 Markos Gogoulos
        """ test if a non existent flavor is asked, if a 404 itemNotFound returned
259 668c1361 Markos Gogoulos
        """
260 d2e5f1d1 Markos Gogoulos
        response = self.client.get('/api/v1.0/flavors/' + str(self.test_wrong_flavor_id))
261 ffffd5fe Markos Gogoulos
        self.assertTrue(response.status_code in [404, 503])
262 668c1361 Markos Gogoulos
263 668c1361 Markos Gogoulos
    def testImageList(self):
264 668c1361 Markos Gogoulos
        """ test if the expected list of images is returned by the API
265 668c1361 Markos Gogoulos
        """        
266 668c1361 Markos Gogoulos
        response = self.client.get('/api/v1.0/images')
267 668c1361 Markos Gogoulos
        images_from_api = json.loads(response.content)['images']
268 668c1361 Markos Gogoulos
        images_from_db = Image.objects.all()
269 668c1361 Markos Gogoulos
        self.assertEqual(len(images_from_api), len(images_from_db))
270 668c1361 Markos Gogoulos
        self.assertTrue(response.status_code in [200,203])
271 54c30633 Markos Gogoulos
        for image_from_api in images_from_api:
272 54c30633 Markos Gogoulos
            image_from_db = Image.objects.get(id=image_from_api['id'])
273 54c30633 Markos Gogoulos
            self.assertEqual(image_from_api['id'], image_from_db.id)
274 54c30633 Markos Gogoulos
            self.assertEqual(image_from_api['name'], image_from_db.name)
275 668c1361 Markos Gogoulos
276 668c1361 Markos Gogoulos
277 668c1361 Markos Gogoulos
    def testImageDetails(self):
278 668c1361 Markos Gogoulos
        """ test if the expected image is returned by the API
279 668c1361 Markos Gogoulos
        """
280 d2e5f1d1 Markos Gogoulos
        response = self.client.get('/api/v1.0/images/' + str(self.test_image_id))
281 668c1361 Markos Gogoulos
        image_from_api = json.loads(response.content)['image']
282 d2e5f1d1 Markos Gogoulos
        image_from_db = Image.objects.get(id=self.test_image_id)
283 668c1361 Markos Gogoulos
        self.assertEqual(image_from_api['name'], image_from_db.name)
284 668c1361 Markos Gogoulos
        self.assertEqual(image_from_api['id'], image_from_db.id)
285 668c1361 Markos Gogoulos
        self.assertEqual(image_from_api['serverId'], image_from_db.sourcevm and image_from_db.sourcevm.id or "")
286 668c1361 Markos Gogoulos
        self.assertEqual(image_from_api['size'], image_from_db.size)
287 668c1361 Markos Gogoulos
        self.assertEqual(image_from_api['status'], image_from_db.state)
288 668c1361 Markos Gogoulos
        self.assertEqual(image_from_api['description'], image_from_db.description)
289 668c1361 Markos Gogoulos
        self.assertTrue(response.status_code in [200,203])
290 668c1361 Markos Gogoulos
291 668c1361 Markos Gogoulos
292 668c1361 Markos Gogoulos
    def testImagesDetails(self):
293 668c1361 Markos Gogoulos
        """ test if the images details are returned by the API
294 668c1361 Markos Gogoulos
        """
295 f3af8b40 Dimitris Moraitis
        response = self.client.get('/api/v1.0/images/detail')
296 f3af8b40 Dimitris Moraitis
        images_from_api = json.loads(response.content)['images']
297 f3af8b40 Dimitris Moraitis
        images_from_db = Image.objects.all()
298 f3af8b40 Dimitris Moraitis
        for i in range(0, len(images_from_db)):
299 f3af8b40 Dimitris Moraitis
            image_from_db = Image.objects.get(id=images_from_db[i].id)
300 f3af8b40 Dimitris Moraitis
            image_from_api = images_from_api[i]
301 685ab2b6 Markos Gogoulos
            self.assertEqual(image_from_api['name'], image_from_db.name)
302 685ab2b6 Markos Gogoulos
            self.assertEqual(image_from_api['id'], image_from_db.id)
303 685ab2b6 Markos Gogoulos
            self.assertEqual(image_from_api['serverId'], image_from_db.sourcevm and image_from_db.sourcevm.id or "")
304 685ab2b6 Markos Gogoulos
            self.assertEqual(image_from_api['size'], image_from_db.size)
305 685ab2b6 Markos Gogoulos
            self.assertEqual(image_from_api['status'], image_from_db.state)
306 685ab2b6 Markos Gogoulos
            self.assertEqual(image_from_api['description'], image_from_db.description)
307 f3af8b40 Dimitris Moraitis
308 54c30633 Markos Gogoulos
        for image_from_api in images_from_api:
309 54c30633 Markos Gogoulos
            image_from_db = Image.objects.get(id=image_from_api['id'])
310 54c30633 Markos Gogoulos
            self.assertEqual(image_from_api['name'], image_from_db.name)
311 54c30633 Markos Gogoulos
            self.assertEqual(image_from_api['id'], image_from_db.id)
312 54c30633 Markos Gogoulos
            self.assertEqual(image_from_api['serverId'], image_from_db.sourcevm and image_from_db.sourcevm.id or "")
313 54c30633 Markos Gogoulos
            self.assertEqual(image_from_api['size'], image_from_db.size)
314 54c30633 Markos Gogoulos
            self.assertEqual(image_from_api['status'], image_from_db.state)
315 54c30633 Markos Gogoulos
            self.assertEqual(image_from_api['description'], image_from_db.description)
316 f3af8b40 Dimitris Moraitis
            
317 685ab2b6 Markos Gogoulos
        self.assertTrue(response.status_code in [200,203])
318 685ab2b6 Markos Gogoulos
319 668c1361 Markos Gogoulos
320 668c1361 Markos Gogoulos
    def testWrongImage(self):
321 668c1361 Markos Gogoulos
        """ test if a non existent image is asked, if a 404 itemNotFound returned
322 668c1361 Markos Gogoulos
        """
323 d2e5f1d1 Markos Gogoulos
        response = self.client.get('/api/v1.0/images/' + str(self.test_wrong_image_id))
324 685ab2b6 Markos Gogoulos
        self.assertEqual(response.status_code, 404)
325 73469364 Markos Gogoulos
326 73469364 Markos Gogoulos
327 73469364 Markos Gogoulos
    def testServerMetadata(self):
328 73469364 Markos Gogoulos
        """ test server's metadata (add, edit)
329 73469364 Markos Gogoulos
        """
330 73469364 Markos Gogoulos
        request = {
331 73469364 Markos Gogoulos
            "metadata": {"metadata_key" : "name", "metadata_value" : "a fancy name"}
332 73469364 Markos Gogoulos
            }
333 73469364 Markos Gogoulos
        response = self.client.put('/api/v1.0/servers' + str(self.test_server_id), 
334 73469364 Markos Gogoulos
                                    json.dumps(request),
335 73469364 Markos Gogoulos
                                    content_type='application/json')  
336 73469364 Markos Gogoulos
        self.assertEqual(response.status_code, 404)
337 73469364 Markos Gogoulos
        #TODO: not working atm, due to problem with django piston and PUT
338 a8acbbac Markos Gogoulos
339 a8acbbac Markos Gogoulos
    def testVMgroupList(self):
340 a8acbbac Markos Gogoulos
        """ test if the expected list of groups is returned by the API
341 a8acbbac Markos Gogoulos
        """        
342 a8acbbac Markos Gogoulos
        response = self.client.get('/api/v1.0/groups')
343 a8acbbac Markos Gogoulos
        groups_from_api = json.loads(response.content)['groups']
344 a8acbbac Markos Gogoulos
        groups_from_db = VirtualMachineGroup.objects.all()
345 a8acbbac Markos Gogoulos
        self.assertEqual(len(groups_from_api), len(groups_from_db))
346 a8acbbac Markos Gogoulos
        self.assertTrue(response.status_code in [200,203])
347 a8acbbac Markos Gogoulos
        for group_from_api in groups_from_api:
348 a8acbbac Markos Gogoulos
            group_from_db = VirtualMachineGroup.objects.get(id=group_from_api['id'])
349 a8acbbac Markos Gogoulos
            self.assertEqual(group_from_api['id'], group_from_db.id)
350 a8acbbac Markos Gogoulos
            self.assertEqual(group_from_api['name'], group_from_db.name)
351 a8acbbac Markos Gogoulos
352 a8acbbac Markos Gogoulos
    def testVMgroupDetails(self):
353 a8acbbac Markos Gogoulos
        """ test if the expected virtual machine group is returned by the API
354 a8acbbac Markos Gogoulos
        """
355 a8acbbac Markos Gogoulos
        response = self.client.get('/api/v1.0/groups/' + str(self.test_group_id))
356 a8acbbac Markos Gogoulos
        group_from_api = json.loads(response.content)['group']
357 a8acbbac Markos Gogoulos
        group_from_db = VirtualMachineGroup.objects.get(id=self.test_group_id)
358 a8acbbac Markos Gogoulos
        self.assertEqual(group_from_api['name'], group_from_db.name)
359 a8acbbac Markos Gogoulos
        self.assertEqual(group_from_api['id'], group_from_db.id)
360 a8acbbac Markos Gogoulos
        self.assertEqual(group_from_api['server_id'], [machine.id for machine in group_from_db.machines.all()])
361 a8acbbac Markos Gogoulos
        self.assertTrue(response.status_code in [200,203])
362 a8acbbac Markos Gogoulos
363 a8acbbac Markos Gogoulos
    def testWrongVMgroup(self):
364 a8acbbac Markos Gogoulos
        """ test if a non existent VMgroup is asked, if a 404 itemNotFound returned
365 a8acbbac Markos Gogoulos
        """
366 a8acbbac Markos Gogoulos
        response = self.client.get('/api/v1.0/groups/' + str(self.test_wrong_group_id))
367 a8acbbac Markos Gogoulos
        self.assertEqual(response.status_code, 404)
368 a8acbbac Markos Gogoulos
369 a8acbbac Markos Gogoulos
    def testgroupsDetails(self):
370 a8acbbac Markos Gogoulos
        """ test if the groups details are returned by the API
371 a8acbbac Markos Gogoulos
        """
372 a8acbbac Markos Gogoulos
        response = self.client.get('/api/v1.0/groups/detail')
373 a8acbbac Markos Gogoulos
        groups_from_api = json.loads(response.content)['groups']
374 a8acbbac Markos Gogoulos
        groups_from_db = VirtualMachineGroup.objects.all()
375 a8acbbac Markos Gogoulos
        for i in range(0, len(groups_from_db)):
376 a8acbbac Markos Gogoulos
            group_from_db = VirtualMachineGroup.objects.get(id=groups_from_db[i].id)
377 a8acbbac Markos Gogoulos
            group_from_api = groups_from_api[i]
378 a8acbbac Markos Gogoulos
            self.assertEqual(group_from_api['name'], group_from_db.name)
379 a8acbbac Markos Gogoulos
            self.assertEqual(group_from_api['id'], group_from_db.id)
380 a8acbbac Markos Gogoulos
            self.assertEqual(group_from_api['server_id'], [machine.id for machine in group_from_db.machines.all()])
381 a8acbbac Markos Gogoulos
        for group_from_api in groups_from_api:
382 a8acbbac Markos Gogoulos
            group_from_db = VirtualMachineGroup.objects.get(id=group_from_api['id'])
383 a8acbbac Markos Gogoulos
            self.assertEqual(group_from_api['name'], group_from_db.name)
384 a8acbbac Markos Gogoulos
            self.assertEqual(group_from_api['id'], group_from_db.id)
385 a8acbbac Markos Gogoulos
            self.assertEqual(group_from_api['server_id'], [machine.id for machine in group_from_db.machines.all()])            
386 a8acbbac Markos Gogoulos
        self.assertTrue(response.status_code in [200,203])