Statistics
| Branch: | Tag: | Revision:

root / api / tests.py @ 6130effc

History | View | Annotate | Download (18.3 kB)

1
#
2
# Unit Tests for api
3
#
4
# Provides automated tests for api module
5
#
6
# Copyright 2011 Greek Research and Technology Network
7
#
8

    
9
from django.test import TestCase
10
from django.test.client import Client
11
import simplejson as json
12
from synnefo.db.models import VirtualMachine, Flavor, Image, VirtualMachineGroup
13

    
14
class APITestCase(TestCase):
15
    fixtures = [ 'api_test_data' ]
16
    test_server_id = 1001
17
    test_image_id = 1
18
    test_flavor_id = 1
19
    test_group_id = 1
20
    test_wrong_server_id = 99999999
21
    test_wrong_image_id = 99999999
22
    test_wrong_flavor_id = 99999999
23
    test_wrong_group_id = 99999999
24
    #make the testing with these id's
25

    
26

    
27
    def setUp(self):
28
        self.client = Client()
29

    
30
    def testAPIVersion(self):
31
        """ check rackspace cloud servers API version
32
        """
33
        response = self.client.get('/api/v1.0/')
34
        # Check that the response is 200 OK.
35
        self.assertEqual(response.status_code, 200)
36
        api_version = json.loads(response.content)['version']
37
        self.assertEqual(api_version['status'], 'CURRENT')
38
        self.assertEqual(api_version['wadl'], 'http://docs.rackspacecloud.com/servers/api/v1.0/application.wadl')
39
        self.assertEqual(api_version['docURL'], 'http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf')
40
        self.assertEqual(api_version['id'], 'v1.0')
41

    
42

    
43
    def testServerList(self):
44
        """ test if the expected list of servers is returned by the API
45
        """        
46
        response = self.client.get('/api/v1.0/servers')
47
        vms_from_api = json.loads(response.content)['servers']
48
        vms_from_db = VirtualMachine.objects.filter(deleted=False)
49
        self.assertEqual(len(vms_from_api), len(vms_from_db))
50
        self.assertTrue(response.status_code in [200,203])
51
        for vm_from_api in vms_from_api:
52
            vm_from_db = VirtualMachine.objects.get(id=vm_from_api['id'])
53
            self.assertEqual(vm_from_api['id'], vm_from_db.id)
54
            self.assertEqual(vm_from_api['name'], vm_from_db.name)
55

    
56

    
57
    def testServerDetails(self):
58
        """ test if the expected server is returned by the API
59
        """        
60
        response = self.client.get('/api/v1.0/servers/' + str(self.test_server_id))
61
        vm_from_api = json.loads(response.content)['server']
62
        vm_from_db = VirtualMachine.objects.get(id=self.test_server_id)
63
        self.assertEqual(vm_from_api['flavorId'], vm_from_db.flavor.id)
64
        self.assertEqual(vm_from_api['hostId'], vm_from_db.hostid)
65
        self.assertEqual(vm_from_api['id'], vm_from_db.id)
66
        self.assertEqual(vm_from_api['imageId'], vm_from_db.flavor.id)
67
        self.assertEqual(vm_from_api['name'], vm_from_db.name)
68
        self.assertEqual(vm_from_api['status'], vm_from_db.rsapi_state)
69
        self.assertTrue(response.status_code in [200,203])
70

    
71

    
72
    def testServersDetails(self):
73
        """ test if the servers details are returned by the API
74
        """
75
        response = self.client.get('/api/v1.0/servers/detail')     
76
        vms_from_db = VirtualMachine.objects.filter(deleted=False)
77
        id_list = [vm.id for vm in vms_from_db]
78
        number = 0
79
        for vm_id in id_list:
80
            vm_from_api = json.loads(response.content)['servers'][number]
81
            vm_from_db = VirtualMachine.objects.get(id=vm_id)
82
            self.assertEqual(vm_from_api['flavorId'], vm_from_db.flavor.id)
83
            self.assertEqual(vm_from_api['hostId'], vm_from_db.hostid)
84
            self.assertEqual(vm_from_api['id'], vm_from_db.id)
85
            self.assertEqual(vm_from_api['imageId'], vm_from_db.flavor.id)
86
            self.assertEqual(vm_from_api['name'], vm_from_db.name)
87
            self.assertEqual(vm_from_api['status'], vm_from_db.rsapi_state)
88
            number += 1
89
        vms_from_api = json.loads(response.content)['servers']
90
        for vm_from_api in vms_from_api:
91
            vm_from_db = VirtualMachine.objects.get(id=vm_from_api['id'])
92
            self.assertEqual(vm_from_api['flavorId'], vm_from_db.flavor.id)
93
            self.assertEqual(vm_from_api['hostId'], vm_from_db.hostid)
94
            self.assertEqual(vm_from_api['id'], vm_from_db.id)
95
            self.assertEqual(vm_from_api['imageId'], vm_from_db.flavor.id)
96
            self.assertEqual(vm_from_api['name'], vm_from_db.name)
97
            self.assertEqual(vm_from_api['status'], vm_from_db.rsapi_state)            
98
        self.assertTrue(response.status_code in [200,203])
99

    
100

    
101
    def testWrongServer(self):
102
        """ test if a non existent server is asked, if a 404 itemNotFound returned
103
        """
104
        response = self.client.get('/api/v1.0/servers/' + str(self.test_wrong_server_id))
105
        self.assertEqual(response.status_code, 404)
106

    
107

    
108
    def testCreateServerEmpty(self):
109
        """ test if the create server call returns a 400 badRequest if no
110
            attributes are specified
111
        """
112
        response = self.client.post('/api/v1.0/servers',{})
113
        self.assertEqual(response.status_code, 400)
114

    
115

    
116
    def testCreateServer(self):
117
        """ test if the create server call returns the expected response
118
            if a valid request has been speficied
119
        """
120
        request = {
121
                    "server": {
122
                        "name"          : "new-server-test",
123
                        "imageId"       : 1,
124
                        "flavorId"      : 1,
125
                        "metadata"      : {
126
                            "My Server Name": "Apache1"
127
                        },
128
                        "personality"   : []
129
                    }
130
        }
131
        response = self.client.post('/api/v1.0/servers', 
132
                                    json.dumps(request), 
133
                                    content_type='application/json')
134
        self.assertEqual(response.status_code, 202)
135
        #TODO: check response.content      
136
        #TODO: check create server with wrong options (eg flavor that not exist)
137
    
138

    
139
    def testRebootServer(self):
140
        """ test if the specified server is rebooted
141
        """
142
        request = {
143
            "reboot": '{"type" : "HARD"}'
144
            }
145
        response = self.client.post('/api/v1.0/servers/' + str(self.test_server_id) + '/action', 
146
                                    json.dumps(request),
147
                                    content_type='application/json')  
148
        self.assertEqual(response.status_code, 202)
149
        #server id that does not exist
150
        response = self.client.post('/api/v1.0/servers/' + str(self.test_wrong_server_id) + '/action', 
151
                                   json.dumps(request), 
152
                                   content_type='application/json')
153
        self.assertEqual(response.status_code, 404)
154

    
155

    
156
    def testShutdownServer(self):
157
        """ test if the specified server is shutdown
158
        """
159
        request = {
160
            "shutdown": {"timeout" : "5"}
161
            }
162
        response = self.client.post('/api/v1.0/servers/' + str(self.test_server_id) + '/action',
163
                                    json.dumps(request), 
164
                                    content_type='application/json')  
165
        self.assertEqual(response.status_code, 202)
166
        #server id that does not exist
167
        response = self.client.post('/api/v1.0/servers/' + str(self.test_wrong_server_id) + '/action',
168
                                    json.dumps(request), 
169
                                    content_type='application/json')
170
        self.assertEqual(response.status_code, 404)
171

    
172

    
173
    def testStartServer(self):
174
        """ test if the specified server is started
175
        """
176
        request = {
177
            "start": {"type" : "NORMAL"}
178
            }
179
        response = self.client.post('/api/v1.0/servers/' + str(self.test_server_id) + '/action', 
180
                                    json.dumps(request),
181
                                    content_type='application/json')  
182
        self.assertEqual(response.status_code, 202)
183
        #server id that does not exist
184
        response = self.client.post('/api/v1.0/servers/' + str(self.test_wrong_server_id) + '/action', 
185
                                    json.dumps(request), 
186
                                    content_type='application/json')
187
        self.assertEqual(response.status_code, 404)
188

    
189
    def testDeleteServer(self):
190
        """ test if the specified server is deleted
191
        """
192
        response = self.client.delete('/api/v1.0/servers/' + str(self.test_server_id))
193
        self.assertEqual(response.status_code, 202)
194
        #server id that does not exist      
195
        response = self.client.delete('/api/v1.0/servers/' + str(self.test_wrong_server_id))  
196
        self.assertEqual(response.status_code, 404)
197

    
198

    
199
    def testFlavorList(self):
200
        """ test if the expected list of flavors is returned by the API
201
        """        
202
        response = self.client.get('/api/v1.0/flavors')
203
        flavors_from_api = json.loads(response.content)['flavors']
204
        flavors_from_db = Flavor.objects.all()
205
        self.assertEqual(len(flavors_from_api), len(flavors_from_db))
206
        self.assertTrue(response.status_code in [200,203])
207
        for flavor_from_api in flavors_from_api:
208
            flavor_from_db = Flavor.objects.get(id=flavor_from_api['id'])
209
            self.assertEqual(flavor_from_api['id'], flavor_from_db.id)
210
            self.assertEqual(flavor_from_api['name'], flavor_from_db.name)
211

    
212

    
213
    def testFlavorsDetails(self):
214
        """ test if the flavors details are returned by the API
215
        """
216
        response = self.client.get('/api/v1.0/flavors/detail')
217
        flavors_from_db = Flavor.objects.all()
218
        flavors_from_api = json.loads(response.content)['flavors']
219
        
220
        # Assert that all flavors in the db appear inthe API call result
221
        for i in range(0, len(flavors_from_db)):
222
            flavor_from_api = flavors_from_api[i]
223
            flavor_from_db = Flavor.objects.get(id=flavors_from_db[i].id)
224
            self.assertEqual(flavor_from_api['cpu'], flavor_from_db.cpu)
225
            self.assertEqual(flavor_from_api['id'], flavor_from_db.id)
226
            self.assertEqual(flavor_from_api['disk'], flavor_from_db.disk)
227
            self.assertEqual(flavor_from_api['name'], flavor_from_db.name)
228
            self.assertEqual(flavor_from_api['ram'], flavor_from_db.ram)
229

    
230
        # Assert that all flavors that appear in the API call result are also in the db             
231
        for flavor_from_api in flavors_from_api:
232
            flavor_from_db = Flavor.objects.get(id=flavor_from_api['id'])
233
            self.assertEqual(flavor_from_api['cpu'], flavor_from_db.cpu)
234
            self.assertEqual(flavor_from_api['id'], flavor_from_db.id)
235
            self.assertEqual(flavor_from_api['disk'], flavor_from_db.disk)
236
            self.assertEqual(flavor_from_api['name'], flavor_from_db.name)
237
            self.assertEqual(flavor_from_api['ram'], flavor_from_db.ram)
238
        
239
        # Check if we have the right status_code
240
        self.assertTrue(response.status_code in [200,203])
241

    
242

    
243
    def testFlavorDetails(self):
244
        """ test if the expected flavor is returned by the API
245
        """
246
        response = self.client.get('/api/v1.0/flavors/' + str(self.test_flavor_id))
247
        flavor_from_api = json.loads(response.content)['flavor']
248
        flavor_from_db = Flavor.objects.get(id=self.test_flavor_id)
249
        self.assertEqual(flavor_from_api['cpu'], flavor_from_db.cpu)
250
        self.assertEqual(flavor_from_api['id'], flavor_from_db.id)
251
        self.assertEqual(flavor_from_api['disk'], flavor_from_db.disk)
252
        self.assertEqual(flavor_from_api['name'], flavor_from_db.name)
253
        self.assertEqual(flavor_from_api['ram'], flavor_from_db.ram)
254
        self.assertTrue(response.status_code in [200,203])
255

    
256

    
257
    def testWrongFlavor(self):
258
        """ test if a non existent flavor is asked, if a 404 itemNotFound returned
259
        """
260
        response = self.client.get('/api/v1.0/flavors/' + str(self.test_wrong_flavor_id))
261
        self.assertTrue(response.status_code in [404, 503])
262

    
263
    def testImageList(self):
264
        """ test if the expected list of images is returned by the API
265
        """        
266
        response = self.client.get('/api/v1.0/images')
267
        images_from_api = json.loads(response.content)['images']
268
        images_from_db = Image.objects.all()
269
        self.assertEqual(len(images_from_api), len(images_from_db))
270
        self.assertTrue(response.status_code in [200,203])
271
        for image_from_api in images_from_api:
272
            image_from_db = Image.objects.get(id=image_from_api['id'])
273
            self.assertEqual(image_from_api['id'], image_from_db.id)
274
            self.assertEqual(image_from_api['name'], image_from_db.name)
275

    
276

    
277
    def testImageDetails(self):
278
        """ test if the expected image is returned by the API
279
        """
280
        response = self.client.get('/api/v1.0/images/' + str(self.test_image_id))
281
        image_from_api = json.loads(response.content)['image']
282
        image_from_db = Image.objects.get(id=self.test_image_id)
283
        self.assertEqual(image_from_api['name'], image_from_db.name)
284
        self.assertEqual(image_from_api['id'], image_from_db.id)
285
        self.assertEqual(image_from_api['serverId'], image_from_db.sourcevm and image_from_db.sourcevm.id or "")
286
        self.assertEqual(image_from_api['size'], image_from_db.size)
287
        self.assertEqual(image_from_api['status'], image_from_db.state)
288
        self.assertEqual(image_from_api['description'], image_from_db.description)
289
        self.assertTrue(response.status_code in [200,203])
290

    
291

    
292
    def testImagesDetails(self):
293
        """ test if the images details are returned by the API
294
        """
295
        response = self.client.get('/api/v1.0/images/detail')
296
        images_from_api = json.loads(response.content)['images']
297
        images_from_db = Image.objects.all()
298
        for i in range(0, len(images_from_db)):
299
            image_from_db = Image.objects.get(id=images_from_db[i].id)
300
            image_from_api = images_from_api[i]
301
            self.assertEqual(image_from_api['name'], image_from_db.name)
302
            self.assertEqual(image_from_api['id'], image_from_db.id)
303
            self.assertEqual(image_from_api['serverId'], image_from_db.sourcevm and image_from_db.sourcevm.id or "")
304
            self.assertEqual(image_from_api['size'], image_from_db.size)
305
            self.assertEqual(image_from_api['status'], image_from_db.state)
306
            self.assertEqual(image_from_api['description'], image_from_db.description)
307

    
308
        for image_from_api in images_from_api:
309
            image_from_db = Image.objects.get(id=image_from_api['id'])
310
            self.assertEqual(image_from_api['name'], image_from_db.name)
311
            self.assertEqual(image_from_api['id'], image_from_db.id)
312
            self.assertEqual(image_from_api['serverId'], image_from_db.sourcevm and image_from_db.sourcevm.id or "")
313
            self.assertEqual(image_from_api['size'], image_from_db.size)
314
            self.assertEqual(image_from_api['status'], image_from_db.state)
315
            self.assertEqual(image_from_api['description'], image_from_db.description)
316
            
317
        self.assertTrue(response.status_code in [200,203])
318

    
319

    
320
    def testWrongImage(self):
321
        """ test if a non existent image is asked, if a 404 itemNotFound returned
322
        """
323
        response = self.client.get('/api/v1.0/images/' + str(self.test_wrong_image_id))
324
        self.assertEqual(response.status_code, 404)
325

    
326

    
327
    def testServerMetadata(self):
328
        """ test server's metadata (add, edit)
329
        """
330
        request = {
331
            "metadata": {"metadata_key" : "name", "metadata_value" : "a fancy name"}
332
            }
333
        response = self.client.put('/api/v1.0/servers' + str(self.test_server_id), 
334
                                    json.dumps(request),
335
                                    content_type='application/json')  
336
        self.assertEqual(response.status_code, 404)
337
        #TODO: not working atm, due to problem with django piston and PUT
338

    
339
    def testVMgroupList(self):
340
        """ test if the expected list of groups is returned by the API
341
        """        
342
        response = self.client.get('/api/v1.0/groups')
343
        groups_from_api = json.loads(response.content)['groups']
344
        groups_from_db = VirtualMachineGroup.objects.all()
345
        self.assertEqual(len(groups_from_api), len(groups_from_db))
346
        self.assertTrue(response.status_code in [200,203])
347
        for group_from_api in groups_from_api:
348
            group_from_db = VirtualMachineGroup.objects.get(id=group_from_api['id'])
349
            self.assertEqual(group_from_api['id'], group_from_db.id)
350
            self.assertEqual(group_from_api['name'], group_from_db.name)
351

    
352
    def testVMgroupDetails(self):
353
        """ test if the expected virtual machine group is returned by the API
354
        """
355
        response = self.client.get('/api/v1.0/groups/' + str(self.test_group_id))
356
        group_from_api = json.loads(response.content)['group']
357
        group_from_db = VirtualMachineGroup.objects.get(id=self.test_group_id)
358
        self.assertEqual(group_from_api['name'], group_from_db.name)
359
        self.assertEqual(group_from_api['id'], group_from_db.id)
360
        self.assertEqual(group_from_api['server_id'], [machine.id for machine in group_from_db.machines.all()])
361
        self.assertTrue(response.status_code in [200,203])
362

    
363
    def testWrongVMgroup(self):
364
        """ test if a non existent VMgroup is asked, if a 404 itemNotFound returned
365
        """
366
        response = self.client.get('/api/v1.0/groups/' + str(self.test_wrong_group_id))
367
        self.assertEqual(response.status_code, 404)
368

    
369
    def testgroupsDetails(self):
370
        """ test if the groups details are returned by the API
371
        """
372
        response = self.client.get('/api/v1.0/groups/detail')
373
        groups_from_api = json.loads(response.content)['groups']
374
        groups_from_db = VirtualMachineGroup.objects.all()
375
        for i in range(0, len(groups_from_db)):
376
            group_from_db = VirtualMachineGroup.objects.get(id=groups_from_db[i].id)
377
            group_from_api = groups_from_api[i]
378
            self.assertEqual(group_from_api['name'], group_from_db.name)
379
            self.assertEqual(group_from_api['id'], group_from_db.id)
380
            self.assertEqual(group_from_api['server_id'], [machine.id for machine in group_from_db.machines.all()])
381
        for group_from_api in groups_from_api:
382
            group_from_db = VirtualMachineGroup.objects.get(id=group_from_api['id'])
383
            self.assertEqual(group_from_api['name'], group_from_db.name)
384
            self.assertEqual(group_from_api['id'], group_from_db.id)
385
            self.assertEqual(group_from_api['server_id'], [machine.id for machine in group_from_db.machines.all()])            
386
        self.assertTrue(response.status_code in [200,203])
387