tests.py 18.4 KB
Newer Older
Dimitris Moraitis's avatar
Dimitris Moraitis committed
1
#
Giorgos Verigakis's avatar
Giorgos Verigakis committed
2
# Copyright (c) 2010 Greek Research and Technology Network
Dimitris Moraitis's avatar
Dimitris Moraitis committed
3
4
#

5
6
7
from email.utils import parsedate
from time import mktime

Markos Gogoulos's avatar
Markos Gogoulos committed
8
import datetime
Giorgos Verigakis's avatar
Giorgos Verigakis committed
9

10
from django.utils import simplejson as json
Dimitris Moraitis's avatar
Dimitris Moraitis committed
11
12
from django.test import TestCase
from django.test.client import Client
Giorgos Verigakis's avatar
Giorgos Verigakis committed
13
14

#from synnefo.api.tests_auth import AuthTestCase
Dimitris Moraitis's avatar
Dimitris Moraitis committed
15
16
from synnefo.db.models import VirtualMachine, VirtualMachineGroup
from synnefo.db.models import Flavor, Image
17
from synnefo.logic import utils
Dimitris Moraitis's avatar
Dimitris Moraitis committed
18

Giorgos Verigakis's avatar
Giorgos Verigakis committed
19

Dimitris Moraitis's avatar
Dimitris Moraitis committed
20
class APITestCase(TestCase):
Giorgos Verigakis's avatar
Giorgos Verigakis committed
21
    fixtures = ['api_test_data']
22
23
24
    test_server_id = 1001
    test_image_id = 1
    test_flavor_id = 1
25
    test_group_id = 1
26
27
28
    test_wrong_server_id = 99999999
    test_wrong_image_id = 99999999
    test_wrong_flavor_id = 99999999
29
    test_wrong_group_id = 99999999
30
31
    #make the testing with these id's

Dimitris Moraitis's avatar
Dimitris Moraitis committed
32
    def setUp(self):
33
        self.client = Client()
Dimitris Moraitis's avatar
Dimitris Moraitis committed
34

Dimitris Moraitis's avatar
Dimitris Moraitis committed
35
    def test_api_version(self):
Giorgos Verigakis's avatar
Giorgos Verigakis committed
36
37
38
        """Check API version."""
        
        response = self.client.get('/api/v1.1/')
Dimitris Moraitis's avatar
Dimitris Moraitis committed
39
        self.assertEqual(response.status_code, 200)
Markos Gogoulos's avatar
Markos Gogoulos committed
40
        api_version = json.loads(response.content)['version']
Giorgos Verigakis's avatar
Giorgos Verigakis committed
41
        self.assertEqual(api_version['id'], 'v1.1')
Markos Gogoulos's avatar
Markos Gogoulos committed
42
43
        self.assertEqual(api_version['status'], 'CURRENT')

Dimitris Moraitis's avatar
Dimitris Moraitis committed
44
    def test_server_list(self):
Giorgos Verigakis's avatar
Giorgos Verigakis committed
45
46
47
48
        """Test if the expected list of servers is returned."""
        
        response = self.client.get('/api/v1.1/servers')
        vms_from_api = json.loads(response.content)['servers']['values']
49
        vms_from_db = VirtualMachine.objects.filter(deleted=False)
50
        self.assertEqual(len(vms_from_api), len(vms_from_db))
Dimitris Moraitis's avatar
Dimitris Moraitis committed
51
        self.assertTrue(response.status_code in [200, 203])
Markos Gogoulos's avatar
Markos Gogoulos committed
52
53
54
55
56
        for vm_from_api in vms_from_api:
            vm_from_db = VirtualMachine.objects.get(id=vm_from_api['id'])
            self.assertEqual(vm_from_api['id'], vm_from_db.id)
            self.assertEqual(vm_from_api['name'], vm_from_db.name)

Dimitris Moraitis's avatar
Dimitris Moraitis committed
57
    def test_server_details(self):
Giorgos Verigakis's avatar
Giorgos Verigakis committed
58
59
60
        """Test if the expected server is returned."""
        
        response = self.client.get('/api/v1.1/servers/%d' % 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['flavorRef'], vm_from_db.flavor.id)
64
65
        self.assertEqual(vm_from_api['hostId'], vm_from_db.hostid)
        self.assertEqual(vm_from_api['id'], vm_from_db.id)
66
        self.assertEqual(vm_from_api['imageRef'], vm_from_db.flavor.id)
67
        self.assertEqual(vm_from_api['name'], vm_from_db.name)
68
        self.assertEqual(vm_from_api['status'], utils.get_rsapi_state(vm_from_db))
Giorgos Verigakis's avatar
Giorgos Verigakis committed
69
        self.assertTrue(response.status_code in [200, 203])
70

Markos Gogoulos's avatar
Markos Gogoulos committed
71

Dimitris Moraitis's avatar
Dimitris Moraitis committed
72
    def test_servers_details(self):
Giorgos Verigakis's avatar
Giorgos Verigakis committed
73
74
75
        """Test if the servers details are returned."""
        
        response = self.client.get('/api/v1.1/servers/detail')
Markos Gogoulos's avatar
Markos Gogoulos committed
76
77
        vms_from_db = VirtualMachine.objects.filter(deleted=False)
        id_list = [vm.id for vm in vms_from_db]
Markos Gogoulos's avatar
Markos Gogoulos committed
78
79
        number = 0
        for vm_id in id_list:
Giorgos Verigakis's avatar
Giorgos Verigakis committed
80
            vm_from_api = json.loads(response.content)['servers']['values'][number]
Markos Gogoulos's avatar
Markos Gogoulos committed
81
            vm_from_db = VirtualMachine.objects.get(id=vm_id)
82
            self.assertEqual(vm_from_api['flavorRef'], vm_from_db.flavor.id)
Markos Gogoulos's avatar
Markos Gogoulos committed
83
84
            self.assertEqual(vm_from_api['hostId'], vm_from_db.hostid)
            self.assertEqual(vm_from_api['id'], vm_from_db.id)
85
            self.assertEqual(vm_from_api['imageRef'], vm_from_db.flavor.id)
Markos Gogoulos's avatar
Markos Gogoulos committed
86
            self.assertEqual(vm_from_api['name'], vm_from_db.name)
87
            self.assertEqual(vm_from_api['status'], utils.get_rsapi_state(vm_from_db))
Markos Gogoulos's avatar
Markos Gogoulos committed
88
            number += 1
Giorgos Verigakis's avatar
Giorgos Verigakis committed
89
        vms_from_api = json.loads(response.content)['servers']['values']
Markos Gogoulos's avatar
Markos Gogoulos committed
90
91
        for vm_from_api in vms_from_api:
            vm_from_db = VirtualMachine.objects.get(id=vm_from_api['id'])
92
            self.assertEqual(vm_from_api['flavorRef'], vm_from_db.flavor.id)
Markos Gogoulos's avatar
Markos Gogoulos committed
93
94
            self.assertEqual(vm_from_api['hostId'], vm_from_db.hostid)
            self.assertEqual(vm_from_api['id'], vm_from_db.id)
95
            self.assertEqual(vm_from_api['imageRef'], vm_from_db.flavor.id)
Markos Gogoulos's avatar
Markos Gogoulos committed
96
            self.assertEqual(vm_from_api['name'], vm_from_db.name)
97
            self.assertEqual(vm_from_api['status'], utils.get_rsapi_state(vm_from_db))
Markos Gogoulos's avatar
Markos Gogoulos committed
98
99
        self.assertTrue(response.status_code in [200,203])

Markos Gogoulos's avatar
Markos Gogoulos committed
100

Dimitris Moraitis's avatar
Dimitris Moraitis committed
101
    def test_wrong_server(self):
Giorgos Verigakis's avatar
Giorgos Verigakis committed
102
103
104
        """Test 404 response if server does not exist."""
        
        response = self.client.get('/api/v1.1/servers/%d' % self.test_wrong_server_id)
Markos Gogoulos's avatar
Markos Gogoulos committed
105
106
        self.assertEqual(response.status_code, 404)

Dimitris Moraitis's avatar
Dimitris Moraitis committed
107
    def test_create_server_empty(self):
Giorgos Verigakis's avatar
Giorgos Verigakis committed
108
109
110
111
        """Test if the create server call returns a 400 badRequest if
           no attributes are specified."""
        
        response = self.client.post('/api/v1.1/servers', {})
112
        self.assertEqual(response.status_code, 400)
Markos Gogoulos's avatar
Markos Gogoulos committed
113

Dimitris Moraitis's avatar
Dimitris Moraitis committed
114
    def test_create_server(self):
Giorgos Verigakis's avatar
Giorgos Verigakis committed
115
116
117
        """Test if the create server call returns the expected response
           if a valid request has been speficied."""
        
118
        request = {
119
                    "server": {
Dimitris Moraitis's avatar
Dimitris Moraitis committed
120
121
122
123
                        "name": "new-server-test",
                        "imageRef": 1,
                        "flavorRef": 1,
                        "metadata": {
124
125
                            "My Server Name": "Apache1"
                        },
Dimitris Moraitis's avatar
Dimitris Moraitis committed
126
                        "personality": []
127
                    }
128
        }
Giorgos Verigakis's avatar
Giorgos Verigakis committed
129
        response = self.client.post('/api/v1.1/servers', json.dumps(request),
Markos Gogoulos's avatar
Markos Gogoulos committed
130
                                    content_type='application/json')
131
        self.assertEqual(response.status_code, 202)
Dimitris Moraitis's avatar
Dimitris Moraitis committed
132
133
        #TODO: check response.content
        #TODO: check create server with wrong options (eg non existing flavor)
134

Dimitris Moraitis's avatar
Dimitris Moraitis committed
135
    def test_server_polling(self):
Giorgos Verigakis's avatar
Giorgos Verigakis committed
136
137
138
139
        """Test if the server polling works as expected."""
        
        response = self.client.get('/api/v1.1/servers/detail')
        vms_from_api_initial = json.loads(response.content)['servers']['values']
140
141
142
        ts = mktime(parsedate(response['Date']))
        since = datetime.datetime.fromtimestamp(ts).isoformat() + 'Z'
        response = self.client.get('/api/v1.1/servers/detail?changes-since=%s' % since)
Markos Gogoulos's avatar
Markos Gogoulos committed
143
144
145
146
147
        self.assertEqual(len(response.content), 0)

        #now create a machine. Then check if it is on the list
        request = {
                    "server": {
Dimitris Moraitis's avatar
Dimitris Moraitis committed
148
149
150
151
                        "name": "new-server-test",
                        "imageRef": 1,
                        "flavorRef": 1,
                        "metadata": {
Markos Gogoulos's avatar
Markos Gogoulos committed
152
153
                            "My Server Name": "Apache1"
                        },
Dimitris Moraitis's avatar
Dimitris Moraitis committed
154
                        "personality": []
Markos Gogoulos's avatar
Markos Gogoulos committed
155
156
                    }
        }
Giorgos Verigakis's avatar
Giorgos Verigakis committed
157
158
159
        
        path = '/api/v1.1/servers'
        response = self.client.post(path, json.dumps(request), content_type='application/json')
Markos Gogoulos's avatar
Markos Gogoulos committed
160
161
        self.assertEqual(response.status_code, 202)

162
        response = self.client.get('/api/v1.1/servers/detail?changes-since=%s' % since)
163
        self.assertEqual(response.status_code, 200)
164
        vms_from_api_after = json.loads(response.content)['servers']['values']
Dimitris Moraitis's avatar
Dimitris Moraitis committed
165
        #make sure the newly created server is included on the updated list
Markos Gogoulos's avatar
Markos Gogoulos committed
166
167
        self.assertEqual(len(vms_from_api_after), 1)

Dimitris Moraitis's avatar
Dimitris Moraitis committed
168
    def test_reboot_server(self):
Giorgos Verigakis's avatar
Giorgos Verigakis committed
169
170
171
172
173
        """Test if the specified server is rebooted."""
        
        request = {'reboot': {'type': 'HARD'}}
        path = '/api/v1.1/servers/%d/action' % self.test_server_id
        response = self.client.post(path, json.dumps(request), content_type='application/json')
Markos Gogoulos's avatar
Markos Gogoulos committed
174
175
        self.assertEqual(response.status_code, 202)
        #server id that does not exist
Giorgos Verigakis's avatar
Giorgos Verigakis committed
176
177
        path = '/api/v1.1/servers/%d/action' % self.test_wrong_server_id
        response = self.client.post(path, json.dumps(request), content_type='application/json')
Markos Gogoulos's avatar
Markos Gogoulos committed
178
        self.assertEqual(response.status_code, 404)
179

Dimitris Moraitis's avatar
Dimitris Moraitis committed
180
    def test_shutdown_server(self):
Giorgos Verigakis's avatar
Giorgos Verigakis committed
181
182
183
184
185
        """Test if the specified server is shutdown."""
        
        request = {'shutdown': {}}
        path = '/api/v1.1/servers/%d/action' % self.test_server_id
        response = self.client.post(path, json.dumps(request), content_type='application/json')
Markos Gogoulos's avatar
Markos Gogoulos committed
186
187
        self.assertEqual(response.status_code, 202)
        #server id that does not exist
Giorgos Verigakis's avatar
Giorgos Verigakis committed
188
189
        path = '/api/v1.1/servers/%d/action' % self.test_wrong_server_id
        response = self.client.post(path, json.dumps(request), content_type='application/json')
Markos Gogoulos's avatar
Markos Gogoulos committed
190
        self.assertEqual(response.status_code, 404)
191

Dimitris Moraitis's avatar
Dimitris Moraitis committed
192
    def test_start_server(self):
Giorgos Verigakis's avatar
Giorgos Verigakis committed
193
194
195
196
197
        """Test if the specified server is started."""
        
        request = {'start': {}}
        path = '/api/v1.1/servers/%d/action' % self.test_server_id
        response = self.client.post(path, json.dumps(request), content_type='application/json')
Markos Gogoulos's avatar
Markos Gogoulos committed
198
199
        self.assertEqual(response.status_code, 202)
        #server id that does not exist
Giorgos Verigakis's avatar
Giorgos Verigakis committed
200
201
        path = '/api/v1.1/servers/%d/action' % self.test_wrong_server_id
        response = self.client.post(path, json.dumps(request), content_type='application/json')
Markos Gogoulos's avatar
Markos Gogoulos committed
202
        self.assertEqual(response.status_code, 404)
203

Dimitris Moraitis's avatar
Dimitris Moraitis committed
204
    def test_delete_server(self):
Giorgos Verigakis's avatar
Giorgos Verigakis committed
205
206
207
        """Test if the specified server is deleted."""
        response = self.client.delete('/api/v1.1/servers/%d' % self.test_server_id)
        self.assertEqual(response.status_code, 204)
Dimitris Moraitis's avatar
Dimitris Moraitis committed
208
        #server id that does not exist
Giorgos Verigakis's avatar
Giorgos Verigakis committed
209
        response = self.client.delete('/api/v1.1/servers/%d' % self.test_wrong_server_id)
Markos Gogoulos's avatar
Markos Gogoulos committed
210
        self.assertEqual(response.status_code, 404)
211

Dimitris Moraitis's avatar
Dimitris Moraitis committed
212
    def test_flavor_list(self):
Giorgos Verigakis's avatar
Giorgos Verigakis committed
213
214
215
216
        """Test if the expected list of flavors is returned by."""
        
        response = self.client.get('/api/v1.1/flavors')
        flavors_from_api = json.loads(response.content)['flavors']['values']
217
218
        flavors_from_db = Flavor.objects.all()
        self.assertEqual(len(flavors_from_api), len(flavors_from_db))
Dimitris Moraitis's avatar
Dimitris Moraitis committed
219
        self.assertTrue(response.status_code in [200, 203])
Markos Gogoulos's avatar
Markos Gogoulos committed
220
221
222
223
        for flavor_from_api in flavors_from_api:
            flavor_from_db = Flavor.objects.get(id=flavor_from_api['id'])
            self.assertEqual(flavor_from_api['id'], flavor_from_db.id)
            self.assertEqual(flavor_from_api['name'], flavor_from_db.name)
224

Dimitris Moraitis's avatar
Dimitris Moraitis committed
225
    def test_flavors_details(self):
Giorgos Verigakis's avatar
Giorgos Verigakis committed
226
227
228
        """Test if the flavors details are returned."""
        
        response = self.client.get('/api/v1.1/flavors/detail')
Dimitris Moraitis's avatar
Dimitris Moraitis committed
229
        flavors_from_db = Flavor.objects.all()
Giorgos Verigakis's avatar
Giorgos Verigakis committed
230
        flavors_from_api = json.loads(response.content)['flavors']['values']
Dimitris Moraitis's avatar
Dimitris Moraitis committed
231

Dimitris Moraitis's avatar
Dimitris Moraitis committed
232
233
234
235
        # Assert that all flavors in the db appear inthe API call result
        for i in range(0, len(flavors_from_db)):
            flavor_from_api = flavors_from_api[i]
            flavor_from_db = Flavor.objects.get(id=flavors_from_db[i].id)
Markos Gogoulos's avatar
Markos Gogoulos committed
236
237
238
239
240
            self.assertEqual(flavor_from_api['cpu'], flavor_from_db.cpu)
            self.assertEqual(flavor_from_api['id'], flavor_from_db.id)
            self.assertEqual(flavor_from_api['disk'], flavor_from_db.disk)
            self.assertEqual(flavor_from_api['name'], flavor_from_db.name)
            self.assertEqual(flavor_from_api['ram'], flavor_from_db.ram)
Dimitris Moraitis's avatar
Dimitris Moraitis committed
241

Dimitris Moraitis's avatar
Dimitris Moraitis committed
242
        # Assert that all flavors returned by the API also exist in the db
Markos Gogoulos's avatar
Markos Gogoulos committed
243
244
245
246
247
248
249
        for flavor_from_api in flavors_from_api:
            flavor_from_db = Flavor.objects.get(id=flavor_from_api['id'])
            self.assertEqual(flavor_from_api['cpu'], flavor_from_db.cpu)
            self.assertEqual(flavor_from_api['id'], flavor_from_db.id)
            self.assertEqual(flavor_from_api['disk'], flavor_from_db.disk)
            self.assertEqual(flavor_from_api['name'], flavor_from_db.name)
            self.assertEqual(flavor_from_api['ram'], flavor_from_db.ram)
250

Dimitris Moraitis's avatar
Dimitris Moraitis committed
251
252
        # Check if we have the right status_code
        self.assertTrue(response.status_code in [200, 203])
253

Dimitris Moraitis's avatar
Dimitris Moraitis committed
254
    def test_flavor_details(self):
Giorgos Verigakis's avatar
Giorgos Verigakis committed
255
256
257
        """Test if the expected flavor is returned."""
        
        response = self.client.get('/api/v1.1/flavors/%d' % self.test_flavor_id)
258
        flavor_from_api = json.loads(response.content)['flavor']
259
        flavor_from_db = Flavor.objects.get(id=self.test_flavor_id)
260
261
262
263
264
        self.assertEqual(flavor_from_api['cpu'], flavor_from_db.cpu)
        self.assertEqual(flavor_from_api['id'], flavor_from_db.id)
        self.assertEqual(flavor_from_api['disk'], flavor_from_db.disk)
        self.assertEqual(flavor_from_api['name'], flavor_from_db.name)
        self.assertEqual(flavor_from_api['ram'], flavor_from_db.ram)
Dimitris Moraitis's avatar
Dimitris Moraitis committed
265
        self.assertTrue(response.status_code in [200, 203])
266

Dimitris Moraitis's avatar
Dimitris Moraitis committed
267
    def test_wrong_flavor(self):
Giorgos Verigakis's avatar
Giorgos Verigakis committed
268
269
270
        """Test 404 result when requesting a flavor that does not exist."""
        
        response = self.client.get('/api/v1.1/flavors/%d' % self.test_wrong_flavor_id)
271
        self.assertTrue(response.status_code in [404, 503])
272

Dimitris Moraitis's avatar
Dimitris Moraitis committed
273
    def test_image_list(self):
Giorgos Verigakis's avatar
Giorgos Verigakis committed
274
275
276
277
        """Test if the expected list of images is returned by the API."""
        
        response = self.client.get('/api/v1.1/images')
        images_from_api = json.loads(response.content)['images']['values']
278
279
        images_from_db = Image.objects.all()
        self.assertEqual(len(images_from_api), len(images_from_db))
Dimitris Moraitis's avatar
Dimitris Moraitis committed
280
        self.assertTrue(response.status_code in [200, 203])
Markos Gogoulos's avatar
Markos Gogoulos committed
281
282
283
284
        for image_from_api in images_from_api:
            image_from_db = Image.objects.get(id=image_from_api['id'])
            self.assertEqual(image_from_api['id'], image_from_db.id)
            self.assertEqual(image_from_api['name'], image_from_db.name)
285

Dimitris Moraitis's avatar
Dimitris Moraitis committed
286
    def test_image_details(self):
Giorgos Verigakis's avatar
Giorgos Verigakis committed
287
288
289
        """Test if the expected image is returned."""
        
        response = self.client.get('/api/v1.1/images/%d' % self.test_image_id)
290
        image_from_api = json.loads(response.content)['image']
291
        image_from_db = Image.objects.get(id=self.test_image_id)
292
293
        self.assertEqual(image_from_api['name'], image_from_db.name)
        self.assertEqual(image_from_api['id'], image_from_db.id)
Giorgos Verigakis's avatar
Giorgos Verigakis committed
294
295
        self.assertEqual(image_from_api.get('serverRef', ''),
                        image_from_db.sourcevm and image_from_db.sourcevm.id or '')
296
        self.assertEqual(image_from_api['status'], image_from_db.state)
Dimitris Moraitis's avatar
Dimitris Moraitis committed
297
        self.assertTrue(response.status_code in [200, 203])
298

Dimitris Moraitis's avatar
Dimitris Moraitis committed
299
    def test_images_details(self):
Giorgos Verigakis's avatar
Giorgos Verigakis committed
300
301
302
303
        """Test if the images details are returned."""
        
        response = self.client.get('/api/v1.1/images/detail')
        images_from_api = json.loads(response.content)['images']['values']
Dimitris Moraitis's avatar
Dimitris Moraitis committed
304
305
306
307
        images_from_db = Image.objects.all()
        for i in range(0, len(images_from_db)):
            image_from_db = Image.objects.get(id=images_from_db[i].id)
            image_from_api = images_from_api[i]
Markos Gogoulos's avatar
Markos Gogoulos committed
308
309
            self.assertEqual(image_from_api['name'], image_from_db.name)
            self.assertEqual(image_from_api['id'], image_from_db.id)
Giorgos Verigakis's avatar
Giorgos Verigakis committed
310
            self.assertEqual(image_from_api.get('serverRef', ''),
Dimitris Moraitis's avatar
Dimitris Moraitis committed
311
312
                             image_from_db.sourcevm and
                             image_from_db.sourcevm.id or "")
Markos Gogoulos's avatar
Markos Gogoulos committed
313
            self.assertEqual(image_from_api['status'], image_from_db.state)
Dimitris Moraitis's avatar
Dimitris Moraitis committed
314

Markos Gogoulos's avatar
Markos Gogoulos committed
315
316
317
318
        for image_from_api in images_from_api:
            image_from_db = Image.objects.get(id=image_from_api['id'])
            self.assertEqual(image_from_api['name'], image_from_db.name)
            self.assertEqual(image_from_api['id'], image_from_db.id)
Giorgos Verigakis's avatar
Giorgos Verigakis committed
319
            self.assertEqual(image_from_api.get('serverRef', ''),
Dimitris Moraitis's avatar
Dimitris Moraitis committed
320
321
                             image_from_db.sourcevm and
                             image_from_db.sourcevm.id or "")
Markos Gogoulos's avatar
Markos Gogoulos committed
322
            self.assertEqual(image_from_api['status'], image_from_db.state)
Markos Gogoulos's avatar
Markos Gogoulos committed
323

Dimitris Moraitis's avatar
Dimitris Moraitis committed
324
        self.assertTrue(response.status_code in [200, 203])
325

Dimitris Moraitis's avatar
Dimitris Moraitis committed
326
    def test_wrong_image(self):
Giorgos Verigakis's avatar
Giorgos Verigakis committed
327
328
329
        """Test 404 result if a non existent image is requested."""
        
        response = self.client.get('/api/v1.1/images/%d' % self.test_wrong_image_id)
Markos Gogoulos's avatar
Markos Gogoulos committed
330
        self.assertEqual(response.status_code, 404)
331

Dimitris Moraitis's avatar
Dimitris Moraitis committed
332
    def test_server_metadata(self):
Giorgos Verigakis's avatar
Giorgos Verigakis committed
333
334
335
336
337
338
339
340
        """Test server's metadata (add, edit)."""
        
        key = 'name'
        request = {'meta': {key: 'a fancy name'}}
        
        path = '/api/v1.1/servers/%d/meta/%s' % (self.test_server_id, key)
        response = self.client.put(path, json.dumps(request), content_type='application/json')
        self.assertEqual(response.status_code, 201)
341
342


Giorgos Verigakis's avatar
Giorgos Verigakis committed
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
class APITestCase2(TestCase):
    """An attempt for a more thorough test scenario."""
    
    fixtures = [ 'api_test_data2' ]

    def setUp(self):
        self.client = Client()
        self.server_id = 0

    def create_server_name(self):
        self.server_id += 1
        return 'server%d' % self.server_id

    def test_create_server_json(self):
        TEMPLATE = '''
        {
            "server" : {
                "name" : "%(name)s",
                "flavorRef" : "%(flavorRef)s",
                "imageRef" : "%(imageRef)s"
            }
        }
        '''

        def new_server(imageRef=1, flavorRef=1):
            name = self.create_server_name()
            return name, TEMPLATE % dict(name=name, imageRef=imageRef, flavorRef=flavorRef)

        def verify_response(response, name):
            assert response.status_code == 202
            reply =  json.loads(response.content)
            server = reply['server']
            assert server['name'] == name
            assert server['imageRef'] == 1
            assert server['flavorRef'] == 1
            assert server['status'] == 'BUILD'
            assert server['adminPass']
            assert server['addresses']

        def verify_error(response, code, name):
            assert response.status_code == code
            reply =  json.loads(response.content)
            assert name in reply
            assert reply[name]['code'] == code

        name, data = new_server()
        url = '/api/v1.1/servers'
        response = self.client.post(url, content_type='application/json', data=data)
        verify_response(response, name)

        name, data = new_server()
        url = '/api/v1.1/servers.json'
        response = self.client.post(url, content_type='application/json', data=data)
        verify_response(response, name)

        name, data = new_server()
        url = '/api/v1.1/servers.json'
        response = self.client.post(url, content_type='application/json', data=data,
                                    HTTP_ACCEPT='application/xml')
        verify_response(response, name)

        name, data = new_server(imageRef=0)
        url = '/api/v1.1/servers'
        response = self.client.post(url, content_type='application/json', data=data)
        verify_error(response, 404, 'itemNotFound')

        name, data = new_server(flavorRef=0)
        url = '/api/v1.1/servers'
        response = self.client.post(url, content_type='application/json', data=data)
        verify_error(response, 404, 'itemNotFound')

        url = '/api/v1.1/servers'
        response = self.client.post(url, content_type='application/json', data='INVALID')
        verify_error(response, 400, 'badRequest')