Revision 685ab2b6

b/api/tests.py
49 49
        self.assertEqual(vm_from_api['status'], vm_from_db.rsapi_state)
50 50
        self.assertTrue(response.status_code in [200,203])
51 51

  
52
    def testServersDetails(self):
53
        """ test if the servers details are returned by the API
54
        """
55
        response = self.client.get('/api/v1.0/servers/detail')     
56
        id_list = [vm.id for vm in VirtualMachine.objects.all()]
57
        number = 0
58
        for vm_id in id_list:
59
            vm_from_api = json.loads(response.content)['servers'][number]
60
            vm_from_db = VirtualMachine.objects.get(id=vm_id)
61
            self.assertEqual(vm_from_api['flavorId'], vm_from_db.flavor.id)
62
            self.assertEqual(vm_from_api['hostId'], vm_from_db.hostid)
63
            self.assertEqual(vm_from_api['id'], vm_from_db.id)
64
            self.assertEqual(vm_from_api['imageId'], vm_from_db.flavor.id)
65
            self.assertEqual(vm_from_api['name'], vm_from_db.name)
66
            self.assertEqual(vm_from_api['status'], vm_from_db.rsapi_state)
67
            number += 1
68
        self.assertTrue(response.status_code in [200,203])
69

  
70
    def testWrongServer(self):
71
        """ test if a non existent server is asked, if a 404 itemNotFound returned
72
        """
73
        response = self.client.get('/api/v1.0/servers/1231231001')
74
        self.assertEqual(response.status_code, 404)
75

  
52 76
    def testCreateServerEmpty(self):
53 77
        """ test if the create server call returns a 400 badRequest if no
54 78
            attributes are specified
......
57 81
        self.assertEqual(response.status_code, 400)
58 82
                                    
59 83
    def testCreateServer(self):
60
        """ test if the create server call returns the expected response if a valid request has been speficied
84
        """ test if the create server call returns the expected response
85
            if a valid request has been speficied
61 86
        """
62 87
        request = {
63 88
                    "server": {
......
70 95
                        "personality"   : []
71 96
                    }
72 97
        }
73
        response = self.client.post('/api/v1.0/servers', json.dumps(request), content_type='application/json')
98
        response = self.client.post('/api/v1.0/servers', 
99
                                    json.dumps(request), 
100
                                    content_type='application/json')
74 101
        self.assertEqual(response.status_code, 202)
75 102
        #TODO: check response.content      
76 103
        #TODO: check create server with wrong options (eg flavor that not exist)
77 104
    
78
    def testServersDetails(self):
79
        """ test if the servers details are returned by the API
80
        """
81
        #TODO
82
        pass
83

  
84 105

  
85 106
    def testRebootServer(self):
86 107
        """ test if the specified server is rebooted
87 108
        """
88
        #TODO
89
        pass
109
        request = {
110
            "reboot": '{"type" : "HARD"}'
111
            }
112
        response = self.client.post('/api/v1.0/servers/1004/action', 
113
                                    json.dumps(request),
114
                                    content_type='application/json')  
115
        self.assertEqual(response.status_code, 202)
116
        #server id that does not exist
117
        response = self.client.post('/api/v1.0/servers/665544331004/action', 
118
                                   json.dumps(request), 
119
                                   content_type='application/json')
120
        self.assertEqual(response.status_code, 404)
90 121

  
91 122

  
92 123
    def testShutdownServer(self):
93 124
        """ test if the specified server is shutdown
94 125
        """
95
        #TODO
96
        pass
126
        request = {
127
            "shutdown": {"timeout" : "5"}
128
            }
129
        response = self.client.post('/api/v1.0/servers/1004/action',
130
                                    json.dumps(request), 
131
                                    content_type='application/json')  
132
        self.assertEqual(response.status_code, 202)
133
        #server id that does not exist
134
        response = self.client.post('/api/v1.0/servers/665544331004/action',
135
                                    json.dumps(request), 
136
                                    content_type='application/json')
137
        self.assertEqual(response.status_code, 404)
97 138

  
98 139

  
99 140
    def testStartServer(self):
100 141
        """ test if the specified server is started
101 142
        """
102
        #TODO
103
        pass
143
        request = {
144
            "start": {"type" : "NORMAL"}
145
            }
146
        response = self.client.post('/api/v1.0/servers/1004/action', 
147
                                    json.dumps(request),
148
                                    content_type='application/json')  
149
        self.assertEqual(response.status_code, 202)
150
        #server id that does not exist
151
        response = self.client.post('/api/v1.0/servers/665544331004/action', 
152
                                    json.dumps(request), 
153
                                    content_type='application/json')
154
        self.assertEqual(response.status_code, 404)
104 155

  
105 156
    def testDeleteServer(self):
106 157
        """ test if the specified server is deleted
107 158
        """
108
        #TODO
109
        pass
110

  
111
    def testRebootWrongServer(self):
112
        """ test if the reboot server call returns a 404 itemNotFound if a not existing server
113
            is specified
114
        """
115
        #TODO
116
        pass
117

  
118
    def testServerWrongAction(self):
119
        """ test if the action sent to the specified server is not one of 
120
            create, start, reboot, stop or destroy if 501 notImplemented is returned
121
        """
122
        #TODO
123
        pass
124

  
125
    def testWrongServer(self):
126
        """ test if a non existent server is asked, if a 404 itemNotFound returned
127
        """
128
        #TODO
129
        pass
159
        response = self.client.delete('/api/v1.0/servers/1001')  
160
        self.assertEqual(response.status_code, 202)
161
        #server id that does not exist      
162
        response = self.client.delete('/api/v1.0/servers/1231231231231001')  
163
        self.assertEqual(response.status_code, 404)
130 164

  
131 165

  
132 166
    def testFlavorList(self):
......
139 173
        self.assertTrue(response.status_code in [200,203])
140 174

  
141 175

  
142
    def testFlavorDetails(self):
143
        """ test if the expected flavor is returned by the API
176
    def testFlavorsDetails(self):
177
        """ test if the flavors details are returned by the API
144 178
        """
145
        #TODO
146
        pass
179
        response = self.client.get('/api/v1.0/flavors/detail')     
180
        for number in range(0, len(Flavor.objects.all())):
181
            flavor_from_api = json.loads(response.content)['flavors'][number]
182
            flavor_from_db = Flavor.objects.get(id=number+1)
183
            self.assertEqual(flavor_from_api['cpu'], flavor_from_db.cpu)
184
            self.assertEqual(flavor_from_api['id'], flavor_from_db.id)
185
            self.assertEqual(flavor_from_api['disk'], flavor_from_db.disk)
186
            self.assertEqual(flavor_from_api['name'], flavor_from_db.name)
187
            self.assertEqual(flavor_from_api['ram'], flavor_from_db.ram)
188
        self.assertTrue(response.status_code in [200,203])
147 189

  
148 190

  
149
    def testFlavorsDetails(self):
150
        """ test if the flavors details are returned by the API
191
    def testFlavorDetails(self):
192
        """ test if the expected flavor is returned by the API
151 193
        """
152 194
        response = self.client.get('/api/v1.0/flavors/1')
153 195
        flavor_from_api = json.loads(response.content)['flavor']
......
163 205
    def testWrongFlavor(self):
164 206
        """ test if a non existent flavor is asked, if a 404 itemNotFound returned
165 207
        """
166
        #TODO
167
        pass
208
        response = self.client.get('/api/v1.0/flavors/1231231001')
209
        self.assertEqual(response.status_code, 404)
168 210

  
169 211

  
170 212
    def testImageList(self):
......
196 238
    def testImagesDetails(self):
197 239
        """ test if the images details are returned by the API
198 240
        """
199
        #TODO
200
        pass
241
        response = self.client.get('/api/v1.0/images/detail')     
242
        for number in range(0, len(Image.objects.all())):
243
            image_from_api = json.loads(response.content)['images'][number]
244
            image_from_db = Image.objects.get(id=number+1)
245
            self.assertEqual(image_from_api['name'], image_from_db.name)
246
            self.assertEqual(image_from_api['id'], image_from_db.id)
247
            self.assertEqual(image_from_api['serverId'], image_from_db.sourcevm and image_from_db.sourcevm.id or "")
248
            self.assertEqual(image_from_api['size'], image_from_db.size)
249
            self.assertEqual(image_from_api['status'], image_from_db.state)
250
            self.assertEqual(image_from_api['description'], image_from_db.description)
251
        self.assertTrue(response.status_code in [200,203])
252

  
201 253

  
202 254

  
203 255
    def testWrongImage(self):
204 256
        """ test if a non existent image is asked, if a 404 itemNotFound returned
205 257
        """
206
        #TODO
207
        pass
258
        response = self.client.get('/api/v1.0/images/1231231001')
259
        self.assertEqual(response.status_code, 404)
208 260

  

Also available in: Unified diff