Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / api / tests / servers.py @ 5d805533

History | View | Annotate | Download (45.5 kB)

1
# Copyright 2012 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34
import json
35
from copy import deepcopy
36

    
37
from snf_django.utils.testing import (BaseAPITest, mocked_quotaholder,
38
                                      override_settings)
39
from synnefo.db.models import (VirtualMachine, VirtualMachineMetadata,
40
                               IPAddress, NetworkInterface, Volume)
41
from synnefo.db import models_factory as mfactory
42
from synnefo.logic.utils import get_rsapi_state
43
from synnefo.cyclades_settings import cyclades_services
44
from synnefo.lib.services import get_service_path
45
from synnefo.lib import join_urls
46
from django.conf import settings
47
from synnefo.logic.rapi import GanetiApiError
48

    
49
from mock import patch, Mock
50

    
51

    
52
class ComputeAPITest(BaseAPITest):
53
    def __init__(self, *args, **kwargs):
54
        super(ComputeAPITest, self).__init__(*args, **kwargs)
55
        self.compute_path = get_service_path(cyclades_services, 'compute',
56
                                             version='v2.0')
57

    
58
    def myget(self, path, *args, **kwargs):
59
        path = join_urls(self.compute_path, path)
60
        return self.get(path, *args, **kwargs)
61

    
62
    def myput(self, path, *args, **kwargs):
63
        path = join_urls(self.compute_path, path)
64
        return self.put(path, *args, **kwargs)
65

    
66
    def mypost(self, path, *args, **kwargs):
67
        path = join_urls(self.compute_path, path)
68
        return self.post(path, *args, **kwargs)
69

    
70
    def mydelete(self, path, *args, **kwargs):
71
        path = join_urls(self.compute_path, path)
72
        return self.delete(path, *args, **kwargs)
73

    
74

    
75
class ServerAPITest(ComputeAPITest):
76
    def setUp(self):
77
        self.user1 = 'user1'
78
        self.user2 = 'user2'
79
        self.vm1 = mfactory.VirtualMachineFactory(userid=self.user1)
80
        self.vm2 = mfactory.VirtualMachineFactory(userid=self.user2)
81
        self.vm3 = mfactory.VirtualMachineFactory(deleted=True,
82
                                                  userid=self.user1)
83
        self.vm4 = mfactory.VirtualMachineFactory(userid=self.user2)
84

    
85
    def test_server_list_1(self):
86
        """Test if the expected list of servers is returned."""
87
        response = self.myget('servers')
88
        self.assertSuccess(response)
89
        servers = json.loads(response.content)['servers']
90
        self.assertEqual(servers, [])
91

    
92
    def test_server_list_2(self):
93
        """Test if the expected list of servers is returned."""
94
        response = self.myget('servers', self.user1)
95
        self.assertSuccess(response)
96
        servers = json.loads(response.content)['servers']
97
        db_server = self.vm1
98
        server = servers[0]
99
        self.assertEqual(server["name"], db_server.name)
100
        self.assertEqual(server["id"], db_server.id)
101

    
102
    def test_server_list_detail(self):
103
        """Test if the servers list details are returned."""
104
        user = self.user2
105
        user_vms = {self.vm2.id: self.vm2,
106
                    self.vm4.id: self.vm4}
107

    
108
        response = self.myget('servers/detail', user)
109
        servers = json.loads(response.content)['servers']
110
        self.assertEqual(len(servers), len(user_vms))
111
        for api_vm in servers:
112
            db_vm = user_vms[api_vm['id']]
113
            self.assertEqual(api_vm['flavor']["id"], db_vm.flavor.id)
114
            self.assertEqual(api_vm['hostId'], db_vm.hostid)
115
            self.assertEqual(api_vm['id'], db_vm.id)
116
            self.assertEqual(api_vm['image']["id"], db_vm.imageid)
117
            self.assertEqual(api_vm['name'], db_vm.name)
118
            self.assertEqual(api_vm['status'], get_rsapi_state(db_vm))
119
            self.assertSuccess(response)
120

    
121
    def test_server_detail(self):
122
        """Test if a server details are returned."""
123
        db_vm = self.vm2
124
        user = self.vm2.userid
125
        ip4 = mfactory.IPv4AddressFactory(nic__machine=self.vm2)
126
        nic = ip4.nic
127
        net = ip4.network
128
        ip6 = mfactory.IPv6AddressFactory(nic=nic, network=net)
129
        nic.mac = "aa:00:11:22:33:44"
130
        nic.save()
131

    
132
        db_vm_meta = mfactory.VirtualMachineMetadataFactory(vm=db_vm)
133

    
134
        response = self.myget('servers/%d' % db_vm.id, user)
135
        server = json.loads(response.content)['server']
136

    
137
        self.assertEqual(server['flavor']["id"], db_vm.flavor.id)
138
        self.assertEqual(server['hostId'], db_vm.hostid)
139
        self.assertEqual(server['id'], db_vm.id)
140
        self.assertEqual(server['image']["id"], db_vm.imageid)
141
        self.assertEqual(server['name'], db_vm.name)
142
        self.assertEqual(server['status'], get_rsapi_state(db_vm))
143
        api_nic = server['attachments'][0]
144
        self.assertEqual(api_nic['network_id'], str(net.id))
145
        self.assertEqual(api_nic['mac_address'], nic.mac)
146
        self.assertEqual(api_nic['firewallProfile'], nic.firewall_profile)
147
        self.assertEqual(api_nic['ipv4'], ip4.address)
148
        self.assertEqual(api_nic['ipv6'], ip6.address)
149
        self.assertEqual(api_nic['OS-EXT-IPS:type'], "fixed")
150
        self.assertEqual(api_nic['id'], nic.id)
151
        api_address = server["addresses"]
152
        self.assertEqual(api_address[str(net.id)], [
153
            {"version": 4, "addr": ip4.address, "OS-EXT-IPS:type": "fixed"},
154
            {"version": 6, "addr": ip6.address, "OS-EXT-IPS:type": "fixed"}
155
        ])
156

    
157
        metadata = server['metadata']
158
        self.assertEqual(len(metadata), 1)
159
        self.assertEqual(metadata[db_vm_meta.meta_key], db_vm_meta.meta_value)
160
        self.assertSuccess(response)
161

    
162
    def test_server_fqdn(self):
163
        vm = mfactory.VirtualMachineFactory()
164
        # Setting set to None
165
        with override_settings(settings,
166
                               CYCLADES_SERVERS_FQDN=None):
167
            response = self.myget("servers/%d" % vm.id, vm.userid)
168
            server = json.loads(response.content)['server']
169
            self.assertEqual(server["SNF:fqdn"], None)
170
        # Unformated setting
171
        with override_settings(settings,
172
                               CYCLADES_SERVERS_FQDN="vm.example.org"):
173
            response = self.myget("servers/%d" % vm.id, vm.userid)
174
            server = json.loads(response.content)['server']
175
            self.assertEqual(server["SNF:fqdn"], "vm.example.org")
176
        # Formatted settings
177
        with override_settings(settings, CYCLADES_SERVERS_FQDN=
178
                               "snf-%(id)s.vm.example.org"):
179
            response = self.myget("servers/%d" % vm.id, vm.userid)
180
            server = json.loads(response.content)['server']
181
            self.assertEqual(server["SNF:fqdn"],
182
                             "snf-%d.vm.example.org" % vm.id)
183
        with override_settings(settings,
184
                               CYCLADES_SERVERS_FQDN=
185
                               "snf-%(id)s.vm-%(id)s.example.org"):
186
            response = self.myget("servers/%d" % vm.id, vm.userid)
187
            server = json.loads(response.content)['server']
188
            self.assertEqual(server["SNF:fqdn"], "snf-%d.vm-%d.example.org" %
189
                             (vm.id, vm.id))
190

    
191
    def test_server_port_forwarding(self):
192
        vm = mfactory.VirtualMachineFactory()
193
        # test None if the server has no public IP
194
        ports = {
195
            22: ("foo", 61000),
196
            80: lambda ip, id, fqdn, user: ("bar", 61001)}
197
        with override_settings(settings,
198
                               CYCLADES_PORT_FORWARDING=ports):
199
            response = self.myget("servers/%d" % vm.id, vm.userid)
200
        server = json.loads(response.content)['server']
201
        self.assertEqual(server["SNF:port_forwarding"], {})
202

    
203
        # Add with public IP
204
        mfactory.IPv4AddressFactory(nic__machine=vm, network__public=True)
205
        with override_settings(settings,
206
                               CYCLADES_PORT_FORWARDING=ports):
207
            response = self.myget("servers/%d" % vm.id, vm.userid)
208
        server = json.loads(response.content)['server']
209
        self.assertEqual(server["SNF:port_forwarding"],
210
                         {"22": {"host": "foo", "port": "61000"},
211
                          "80": {"host": "bar", "port": "61001"}})
212

    
213
        def _port_from_ip(ip, base):
214
            fields = ip.split('.', 4)
215
            return (base + 256*int(fields[2]) + int(fields[3]))
216

    
217
        ports = {
218
            22: lambda ip, id, fqdn, user:
219
            ip and ("gate", _port_from_ip(ip, 10000)) or None}
220
        vm = mfactory.VirtualMachineFactory()
221
        with override_settings(settings,
222
                               CYCLADES_PORT_FORWARDING=ports):
223
            response = self.myget("servers/%d" % vm.id, vm.userid)
224
            server = json.loads(response.content)['server']
225
            self.assertEqual(server["SNF:port_forwarding"], {})
226

    
227
        mfactory.IPv4AddressFactory(nic__machine=vm,
228
                                    network__public=True,
229
                                    address="192.168.2.2")
230
        with override_settings(settings,
231
                               CYCLADES_PORT_FORWARDING=ports):
232
            response = self.myget("servers/%d" % vm.id, vm.userid)
233
            server = json.loads(response.content)['server']
234
            self.assertEqual(server["SNF:port_forwarding"],
235
                             {"22": {"host": "gate", "port": "10514"}})
236

    
237
    def test_server_building_nics(self):
238
        db_vm = self.vm2
239
        user = self.vm2.userid
240
        net1 = mfactory.NetworkFactory()
241
        net2 = mfactory.NetworkFactory()
242
        net3 = mfactory.NetworkFactory()
243
        mfactory.NetworkInterfaceFactory(machine=self.vm2, network=net1,
244
                                         state="BUILD")
245
        nic2 = mfactory.NetworkInterfaceFactory(machine=self.vm2, network=net2,
246
                                                state="ACTIVE")
247
        mfactory.NetworkInterfaceFactory(machine=self.vm2, network=net3,
248
                                         state="BUILD")
249

    
250
        response = self.myget('servers/%d' % db_vm.id, user)
251
        server = json.loads(response.content)['server']
252
        nics = server["attachments"]
253
        self.assertEqual(len(nics), 1)
254
        self.assertEqual(nics[0]["network_id"], str(nic2.network_id))
255

    
256
    def test_noauthorized(self):
257
        """Test 404 for detail of other user vm"""
258
        db_vm = self.vm2
259

    
260
        response = self.myget('servers/%d' % db_vm.id, 'wrong_user')
261
        self.assertItemNotFound(response)
262

    
263
    def test_wrong_server(self):
264
        """Test 404 response if server does not exist."""
265
        response = self.myget('servers/%d' % 5000)
266
        self.assertItemNotFound(response)
267

    
268
    def test_create_server_empty(self):
269
        """Test if the create server call returns a 400 badRequest if
270
           no attributes are specified."""
271

    
272
        response = self.mypost('servers', params={})
273
        self.assertBadRequest(response)
274

    
275
    def test_rename_server(self):
276
        vm = self.vm2
277
        request = {'server': {'name': 'new_name'}}
278
        response = self.myput('servers/%d' % vm.id, vm.userid,
279
                              json.dumps(request), 'json')
280
        self.assertSuccess(response)
281
        self.assertEqual(VirtualMachine.objects.get(id=vm.id).name, "new_name")
282

    
283
    def test_catch_wrong_api_paths(self):
284
        response = self.myget('nonexistent')
285
        self.assertEqual(response.status_code, 400)
286
        try:
287
            json.loads(response.content)
288
        except ValueError:
289
            self.assertTrue(False)
290

    
291
    def test_method_not_allowed(self, *args):
292
        # /servers/ allows only POST, GET
293
        response = self.myput('servers', '', '')
294
        self.assertMethodNotAllowed(response)
295
        response = self.mydelete('servers')
296
        self.assertMethodNotAllowed(response)
297

    
298
        # /servers/<srvid>/ allows only GET, PUT, DELETE
299
        response = self.mypost("servers/42")
300
        self.assertMethodNotAllowed(response)
301

    
302
        # /imags/<srvid>/metadata/ allows only POST, GET
303
        response = self.myput('servers/42/metadata', '', '')
304
        self.assertMethodNotAllowed(response)
305
        response = self.mydelete('servers/42/metadata')
306
        self.assertMethodNotAllowed(response)
307

    
308
        # /imags/<srvid>/metadata/ allows only POST, GET
309
        response = self.myput('servers/42/metadata', '', '')
310
        self.assertMethodNotAllowed(response)
311
        response = self.mydelete('servers/42/metadata')
312
        self.assertMethodNotAllowed(response)
313

    
314
        # /imags/<srvid>/metadata/<key> allows only PUT, GET, DELETE
315
        response = self.mypost('servers/42/metadata/foo')
316
        self.assertMethodNotAllowed(response)
317

    
318
fixed_image = Mock()
319
fixed_image.return_value = {'location': 'pithos://foo',
320
                            'checksum': '1234',
321
                            "id": 1,
322
                            "name": "test_image",
323
                            "size": 1024,
324
                            "is_snapshot": False,
325
                            'disk_format': 'diskdump'}
326

    
327

    
328
@patch('synnefo.api.util.get_image', fixed_image)
329
@patch('synnefo.volume.util.get_snapshot', fixed_image)
330
@patch('synnefo.logic.rapi_pool.GanetiRapiClient')
331
class ServerCreateAPITest(ComputeAPITest):
332
    def setUp(self):
333
        self.flavor = mfactory.FlavorFactory()
334
        self.backend = mfactory.BackendFactory()
335
        self.request = {
336
            "server": {
337
                "name": "new-server-test",
338
                "userid": "test_user",
339
                "imageRef": 1,
340
                "flavorRef": self.flavor.id,
341
                "metadata": {
342
                    "My Server Name": "Apache1"
343
                },
344
                "personality": []
345
            }
346
        }
347
        # Create dummy public IPv6 network
348
        sub6 = mfactory.IPv6SubnetFactory(network__public=True)
349
        self.net6 = sub6.network
350
        self.network_settings = {
351
            "CYCLADES_DEFAULT_SERVER_NETWORKS": [],
352
            "CYCLADES_FORCED_SERVER_NETWORKS": ["SNF:ANY_PUBLIC_IPV6"]
353
        }
354

    
355
    def test_create_server(self, mrapi):
356
        """Test if the create server call returns the expected response
357
           if a valid request has been speficied."""
358

    
359
        mrapi().CreateInstance.return_value = 12
360
        with override_settings(settings, **self.network_settings):
361
            with mocked_quotaholder():
362
                response = self.mypost('servers', 'test_user',
363
                                       json.dumps(self.request), 'json')
364
        self.assertEqual(response.status_code, 202)
365
        mrapi().CreateInstance.assert_called_once()
366

    
367
        api_server = json.loads(response.content)['server']
368
        self.assertEqual(api_server['status'], "BUILD")
369
        self.assertEqual(api_server['progress'], 0)
370
        self.assertEqual(api_server['metadata'],
371
                         {"My Server Name":  "Apache1"})
372
        self.assertTrue('adminPass' in api_server)
373

    
374
        db_vm = VirtualMachine.objects.get(userid='test_user')
375
        self.assertEqual(api_server['name'], db_vm.name)
376
        self.assertEqual(api_server['status'], db_vm.operstate)
377

    
378
    def test_create_server_wrong_flavor(self, mrapi):
379
        # Test with a flavor that does not exist
380
        request = deepcopy(self.request)
381
        request["server"]["flavorRef"] = 42
382
        with override_settings(settings, **self.network_settings):
383
            with mocked_quotaholder():
384
                response = self.mypost('servers', 'test_user',
385
                                       json.dumps(request), 'json')
386
        self.assertItemNotFound(response)
387

    
388
        # Test with an flavor that is not allowed
389
        flavor = mfactory.FlavorFactory(allow_create=False)
390
        request["server"]["flavorRef"] = flavor.id
391
        with override_settings(settings, **self.network_settings):
392
            with mocked_quotaholder():
393
                response = self.mypost('servers', 'test_user',
394
                                       json.dumps(request), 'json')
395
        self.assertEqual(response.status_code, 403)
396

    
397
    def test_create_server_error(self, mrapi):
398
        """Test if the create server call returns the expected response
399
           if a valid request has been speficied."""
400
        mrapi().CreateInstance.side_effect = GanetiApiError("..ganeti is down")
401

    
402
        request = self.request
403
        with override_settings(settings, **self.network_settings):
404
            with mocked_quotaholder():
405
                response = self.mypost('servers', 'test_user',
406
                                       json.dumps(request), 'json')
407
        self.assertEqual(response.status_code, 202)
408
        mrapi().CreateInstance.assert_called_once()
409
        vm = VirtualMachine.objects.get()
410
        # The VM has not been deleted
411
        self.assertFalse(vm.deleted)
412
        # but is in "ERROR" operstate
413
        self.assertEqual(vm.operstate, "ERROR")
414

    
415
    def test_create_network_info(self, mrapi):
416
        mrapi().CreateInstance.return_value = 12
417

    
418
        # User requested private networks
419
        s1 = mfactory.IPv4SubnetFactory(network__userid="test")
420
        s2 = mfactory.IPv6SubnetFactory(network__userid="test")
421
        # and a public IPv6
422
        request = deepcopy(self.request)
423
        request["server"]["networks"] = [{"uuid": s1.network_id},
424
                                         {"uuid": s2.network_id}]
425
        with override_settings(settings, **self.network_settings):
426
            with mocked_quotaholder():
427
                response = self.mypost('servers', "test",
428
                                       json.dumps(request), 'json')
429
        self.assertEqual(response.status_code, 202)
430
        name, args, kwargs = mrapi().CreateInstance.mock_calls[0]
431
        self.assertEqual(len(kwargs["nics"]), 3)
432
        self.assertEqual(kwargs["nics"][0]["network"], self.net6.backend_id)
433
        self.assertEqual(kwargs["nics"][1]["network"], s1.network.backend_id)
434
        self.assertEqual(kwargs["nics"][2]["network"], s2.network.backend_id)
435

    
436
        # but fail if others user network
437
        s3 = mfactory.IPv6SubnetFactory(network__userid="test_other")
438
        request = deepcopy(self.request)
439
        request["server"]["networks"] = [{"uuid": s3.network_id}]
440
        response = self.mypost('servers', "test", json.dumps(request), 'json')
441
        self.assertEqual(response.status_code, 404)
442

    
443
        # User requested public networks
444
        # but no floating IP..
445
        s1 = mfactory.IPv4SubnetFactory(network__public=True)
446
        request = deepcopy(self.request)
447
        request["server"]["networks"] = [{"uuid": s1.network_id}]
448
        response = self.mypost('servers', "test", json.dumps(request), 'json')
449
        self.assertEqual(response.status_code, 409)
450

    
451
        # Add one floating IP
452
        fp1 = mfactory.IPv4AddressFactory(userid="test", subnet=s1,
453
                                          network=s1.network,
454
                                          floating_ip=True, nic=None)
455
        self.assertEqual(fp1.nic, None)
456
        request = deepcopy(self.request)
457
        request["server"]["networks"] = [{"uuid": s1.network_id,
458
                                          "fixed_ip": fp1.address}]
459
        with mocked_quotaholder():
460
            with override_settings(settings, **self.network_settings):
461
                response = self.mypost('servers', "test",
462
                                       json.dumps(request), 'json')
463
        self.assertEqual(response.status_code, 202)
464
        server_id = json.loads(response.content)["server"]["id"]
465
        fp1 = IPAddress.objects.get(id=fp1.id)
466
        self.assertEqual(fp1.nic.machine_id, server_id)
467

    
468
        # check used floating IP
469
        response = self.mypost('servers', "test", json.dumps(request), 'json')
470
        self.assertEqual(response.status_code, 409)
471

    
472
        # Add more floating IP. but check auto-reserve
473
        fp2 = mfactory.IPv4AddressFactory(userid="test", subnet=s1,
474
                                          network=s1.network,
475
                                          floating_ip=True, nic=None)
476
        self.assertEqual(fp2.nic, None)
477
        request = deepcopy(self.request)
478
        request["server"]["networks"] = [{"uuid": s1.network_id}]
479
        with mocked_quotaholder():
480
            with override_settings(settings, **self.network_settings):
481
                response = self.mypost('servers', "test",
482
                                       json.dumps(request), 'json')
483
        self.assertEqual(response.status_code, 202)
484
        server_id = json.loads(response.content)["server"]["id"]
485
        fp2 = IPAddress.objects.get(id=fp2.id)
486
        self.assertEqual(fp2.nic.machine_id, server_id)
487

    
488
        name, args, kwargs = mrapi().CreateInstance.mock_calls[-1]
489
        self.assertEqual(len(kwargs["nics"]), 2)
490
        self.assertEqual(kwargs["nics"][0]["network"], self.net6.backend_id)
491
        self.assertEqual(kwargs["nics"][1]["network"], fp2.network.backend_id)
492

    
493
    def test_create_network_settings(self, mrapi):
494
        mrapi().CreateInstance.return_value = 12
495
        # User requested private networks
496
        # no public IPv4
497
        network_settings = {
498
            "CYCLADES_DEFAULT_SERVER_NETWORKS": [],
499
            "CYCLADES_FORCED_SERVER_NETWORKS": ["SNF:ANY_PUBLIC_IPV4"]
500
        }
501
        with override_settings(settings, **network_settings):
502
            response = self.mypost('servers', "test", json.dumps(self.request),
503
                                   'json')
504
        self.assertEqual(response.status_code, 503)
505
        # no public IPv4, IPv6 exists
506
        network_settings = {
507
            "CYCLADES_DEFAULT_SERVER_NETWORKS": [],
508
            "CYCLADES_FORCED_SERVER_NETWORKS": ["SNF:ANY_PUBLIC"]
509
        }
510
        with override_settings(settings, **network_settings):
511
            response = self.mypost('servers', "test", json.dumps(self.request),
512
                                   'json')
513
        self.assertEqual(response.status_code, 202)
514
        server_id = json.loads(response.content)["server"]["id"]
515
        vm = VirtualMachine.objects.get(id=server_id)
516
        self.assertEqual(vm.nics.get().ipv4_address, None)
517

    
518
        # IPv4 exists
519
        mfactory.IPv4SubnetFactory(network__public=True,
520
                                   cidr="192.168.2.0/24",
521
                                   pool__offset=2,
522
                                   pool__size=1)
523
        with override_settings(settings, **network_settings):
524
            response = self.mypost('servers', "test", json.dumps(self.request),
525
                                   'json')
526
        self.assertEqual(response.status_code, 202)
527
        server_id = json.loads(response.content)["server"]["id"]
528
        vm = VirtualMachine.objects.get(id=server_id)
529
        self.assertEqual(vm.nics.get().ipv4_address, "192.168.2.2")
530

    
531
        # Fixed networks
532
        net1 = mfactory.NetworkFactory(userid="test")
533
        net2 = mfactory.NetworkFactory(userid="test")
534
        net3 = mfactory.NetworkFactory(userid="test")
535
        network_settings = {
536
            "CYCLADES_DEFAULT_SERVER_NETWORKS": [],
537
            "CYCLADES_FORCED_SERVER_NETWORKS": [net1.id, [net2.id, net3.id],
538
                                                (net3.id, net2.id)]
539
        }
540
        with override_settings(settings, **network_settings):
541
            response = self.mypost('servers', "test", json.dumps(self.request),
542
                                   'json')
543
        self.assertEqual(response.status_code, 202)
544
        server_id = json.loads(response.content)["server"]["id"]
545
        vm = VirtualMachine.objects.get(id=server_id)
546
        self.assertEqual(len(vm.nics.all()), 3)
547

    
548
    def test_create_server_with_port(self, mrapi):
549
        # Test invalid networks
550
        request = deepcopy(self.request)
551
        request["server"]["networks"] = {"foo": "lala"}
552
        with override_settings(settings, **self.network_settings):
553
            response = self.mypost("servers", "dummy_user",
554
                                   json.dumps(request), 'json')
555
        self.assertBadRequest(response)
556
        request["server"]["networks"] = ['1', '2']
557
        with override_settings(settings, **self.network_settings):
558
            response = self.mypost("servers", "dummy_user",
559
                                   json.dumps(request), 'json')
560
        self.assertBadRequest(response)
561
        mrapi().CreateInstance.return_value = 42
562
        ip = mfactory.IPv4AddressFactory(nic__machine=None)
563
        port1 = ip.nic
564
        request = deepcopy(self.request)
565
        request["server"]["networks"] = [{"port": port1.id}]
566
        with override_settings(settings, **self.network_settings):
567
            with mocked_quotaholder():
568
                response = self.mypost("servers", port1.userid,
569
                                       json.dumps(request), 'json')
570
        self.assertEqual(response.status_code, 202)
571
        vm_id = json.loads(response.content)["server"]["id"]
572
        port1 = NetworkInterface.objects.get(id=port1.id)
573
        self.assertEqual(port1.machine_id, vm_id)
574
        # 409 if already used
575
        with override_settings(settings, **self.network_settings):
576
            with mocked_quotaholder():
577
                response = self.mypost("servers", port1.userid,
578
                                       json.dumps(request), 'json')
579
        self.assertConflict(response)
580
        # Test permissions
581
        ip = mfactory.IPv4AddressFactory(userid="user1", nic__userid="user1")
582
        port2 = ip.nic
583
        request["server"]["networks"] = [{"port": port2.id}]
584
        with override_settings(settings, **self.network_settings):
585
            with mocked_quotaholder():
586
                response = self.mypost("servers", "user2",
587
                                       json.dumps(request), 'json')
588
        self.assertEqual(response.status_code, 404)
589

    
590
    def test_create_server_with_volumes(self, mrapi):
591
        user = "test_user"
592
        mrapi().CreateInstance.return_value = 42
593
        # Test creation without any volumes. Server will use flavor+image
594
        request = deepcopy(self.request)
595
        request["server"]["block_device_mapping_v2"] = []
596
        with mocked_quotaholder():
597
            response = self.mypost("servers", user,
598
                                   json.dumps(request), 'json')
599
        self.assertEqual(response.status_code, 202, msg=response.content)
600
        vm_id = json.loads(response.content)["server"]["id"]
601
        volume = Volume.objects.get(machine_id=vm_id)
602
        self.assertEqual(volume.disk_template, self.flavor.disk_template)
603
        self.assertEqual(volume.size, self.flavor.disk)
604
        self.assertEqual(volume.source, "image:%s" % fixed_image()["id"])
605
        self.assertEqual(volume.delete_on_termination, True)
606
        self.assertEqual(volume.userid, user)
607

    
608
        # Test using an image
609
        request["server"]["block_device_mapping_v2"] = [
610
            {"source_type": "image",
611
             "uuid": fixed_image()["id"],
612
             "volume_size": 10,
613
             "delete_on_termination": False}
614
        ]
615
        with mocked_quotaholder():
616
            response = self.mypost("servers", user,
617
                                   json.dumps(request), 'json')
618
        self.assertEqual(response.status_code, 202, msg=response.content)
619
        vm_id = json.loads(response.content)["server"]["id"]
620
        volume = Volume.objects.get(machine_id=vm_id)
621
        self.assertEqual(volume.disk_template, self.flavor.disk_template)
622
        self.assertEqual(volume.size, 10)
623
        self.assertEqual(volume.source, "image:%s" % fixed_image()["id"])
624
        self.assertEqual(volume.delete_on_termination, False)
625
        self.assertEqual(volume.userid, user)
626
        self.assertEqual(volume.origin, "pithos:" + fixed_image()["checksum"])
627

    
628
        # Test using a snapshot
629
        request["server"]["block_device_mapping_v2"] = [
630
            {"source_type": "snapshot",
631
             "uuid": fixed_image()["id"],
632
             "volume_size": 10,
633
             "delete_on_termination": False}
634
        ]
635
        with mocked_quotaholder():
636
            response = self.mypost("servers", user,
637
                                   json.dumps(request), 'json')
638
        self.assertEqual(response.status_code, 202, msg=response.content)
639
        vm_id = json.loads(response.content)["server"]["id"]
640
        volume = Volume.objects.get(machine_id=vm_id)
641
        self.assertEqual(volume.disk_template, self.flavor.disk_template)
642
        self.assertEqual(volume.size, 10)
643
        self.assertEqual(volume.source, "snapshot:%s" % fixed_image()["id"])
644
        self.assertEqual(volume.origin, fixed_image()["checksum"])
645
        self.assertEqual(volume.delete_on_termination, False)
646
        self.assertEqual(volume.userid, user)
647

    
648
        source_volume = volume
649
        # Test using source volume
650
        request["server"]["block_device_mapping_v2"] = [
651
            {"source_type": "volume",
652
             "uuid": source_volume.id,
653
             "volume_size": source_volume.size,
654
             "delete_on_termination": False}
655
        ]
656
        with mocked_quotaholder():
657
            response = self.mypost("servers", user,
658
                                   json.dumps(request), 'json')
659
        # This will fail because the volume is not AVAILABLE.
660
        self.assertBadRequest(response)
661

    
662
        # Test using a blank volume
663
        request["server"]["block_device_mapping_v2"] = [
664
            {"source_type": "blank",
665
             "volume_size": 10,
666
             "delete_on_termination": True}
667
        ]
668
        with mocked_quotaholder():
669
            response = self.mypost("servers", user,
670
                                   json.dumps(request), 'json')
671
        self.assertBadRequest(response)
672

    
673

    
674
@patch('synnefo.logic.rapi_pool.GanetiRapiClient')
675
class ServerDestroyAPITest(ComputeAPITest):
676
    def test_delete_server(self, mrapi):
677
        vm = mfactory.VirtualMachineFactory()
678
        mrapi().DeleteInstance.return_value = 12
679
        response = self.mydelete('servers/%d' % vm.id, vm.userid)
680
        self.assertEqual(response.status_code, 204)
681
        mrapi().DeleteInstance.assert_called_once()
682

    
683
    def test_non_existing_delete_server(self, mrapi):
684
        vm = mfactory.VirtualMachineFactory()
685
        response = self.mydelete('servers/%d' % 42, vm.userid)
686
        self.assertItemNotFound(response)
687
        self.assertFalse(mrapi.mock_calls)
688

    
689

    
690
class ServerMetadataAPITest(ComputeAPITest):
691
    def setUp(self):
692
        self.vm = mfactory.VirtualMachineFactory()
693
        self.metadata = mfactory.VirtualMachineMetadataFactory(vm=self.vm)
694
        super(ServerMetadataAPITest, self).setUp()
695

    
696
    def test_get_metadata(self):
697
        vm = self.vm
698
        create_meta = lambda: mfactory.VirtualMachineMetadataFactory(vm=vm)
699
        metadata = [create_meta(), create_meta(), create_meta()]
700
        response = self.myget('servers/%d/metadata' % vm.id, vm.userid)
701
        self.assertTrue(response.status_code in [200, 203])
702
        api_metadata = json.loads(response.content)['metadata']
703
        self.assertEqual(len(api_metadata), len(metadata) + 1)
704
        for db_m in metadata:
705
            self.assertEqual(api_metadata[db_m.meta_key], db_m.meta_value)
706

    
707
        request = {
708
            'metadata': {
709
                'foo': 'bar'
710
            },
711
            metadata[0].meta_key: 'bar2'
712
        }
713
        response = self.mypost('servers/%d/metadata' % vm.id,
714
                               vm.userid, json.dumps(request), 'json')
715
        metadata2 = VirtualMachineMetadata.objects.filter(vm=vm)
716
        response = self.myget('servers/%d/metadata' % vm.id, vm.userid)
717
        self.assertTrue(response.status_code in [200, 203])
718
        api_metadata2 = json.loads(response.content)['metadata']
719
        self.assertTrue('foo' in api_metadata2.keys())
720
        self.assertTrue(api_metadata2[metadata[0].meta_key], 'bar2')
721
        self.assertEqual(len(api_metadata2), len(metadata2))
722
        for db_m in metadata2:
723
            self.assertEqual(api_metadata2[db_m.meta_key], db_m.meta_value)
724

    
725
        # Create new meta
726
        request = {'meta': {'foo2': 'bar2'}}
727
        response = self.myput('servers/%d/metadata/foo2' % vm.id,
728
                              vm.userid, json.dumps(request), 'json')
729

    
730
        # Get the new meta
731
        response = self.myget('servers/%d/metadata/foo2' % vm.id, vm.userid)
732
        meta = json.loads(response.content)['meta']
733
        self.assertEqual(meta['foo2'], 'bar2')
734

    
735
        # Delete the new meta
736
        response = self.mydelete('servers/%d/metadata/foo2' % vm.id, vm.userid)
737
        self.assertEqual(response.status_code, 204)
738

    
739
        # Try to get the deleted meta: should raise 404
740
        response = self.myget('servers/%d/metadata/foo2' % vm.id, vm.userid)
741
        self.assertEqual(response.status_code, 404)
742

    
743
    def test_invalid_metadata(self):
744
        vm = self.vm
745
        response = self.mypost('servers/%d/metadata' % vm.id, vm.userid)
746
        self.assertBadRequest(response)
747
        self.assertEqual(len(vm.metadata.all()), 1)
748

    
749
    def test_invalid_metadata_server(self):
750
        response = self.mypost('servers/42/metadata', 'user')
751
        self.assertItemNotFound(response)
752

    
753
    def test_get_meta_invalid_key(self):
754
        vm = self.vm
755
        response = self.myget('servers/%d/metadata/foo2' % vm.id, vm.userid)
756
        self.assertItemNotFound(response)
757

    
758

    
759
@patch('synnefo.api.util.get_image')
760
@patch('synnefo.logic.rapi_pool.GanetiRapiClient')
761
class ServerActionAPITest(ComputeAPITest):
762
    def test_actions(self, mrapi, mimage):
763
        actions = ['start', 'shutdown', 'reboot']
764
        vm = mfactory.VirtualMachineFactory()
765
        vm.operstate = "STOPPED"
766
        vm.save()
767
        mrapi().StartupInstance.return_value = 0
768
        mrapi().ShutdownInstance.return_value = 1
769
        mrapi().RebootInstance.return_value = 2
770
        for jobId, action in enumerate(actions):
771
            if action in ["shutdown", "reboot"]:
772
                vm.operstate = "STARTED"
773
            else:
774
                vm.operstate = "STOPPED"
775
            vm.task = None
776
            vm.task_job_id = None
777
            vm.save()
778
            val = {'type': 'HARD'} if action == 'reboot' else {}
779
            request = {action: val}
780
            response = self.mypost('servers/%d/action' % vm.id,
781
                                   vm.userid, json.dumps(request), 'json')
782
            self.assertEqual(response.status_code, 202)
783
            if action == 'shutdown':
784
                self.assertEqual(VirtualMachine.objects.get(id=vm.id).task,
785
                                 "STOP")
786
            else:
787
                self.assertEqual(VirtualMachine.objects.get(id=vm.id).task,
788
                                 action.upper())
789
            self.assertEqual(VirtualMachine.objects.get(id=vm.id).task_job_id,
790
                             jobId)
791

    
792
    def test_action_in_building_vm(self, mrapi, mimage):
793
        """Test building in progress"""
794
        vm = mfactory.VirtualMachineFactory(operstate="BUILD")
795
        request = {'start': {}}
796
        with mocked_quotaholder():
797
            response = self.mypost('servers/%d/action' % vm.id,
798
                                   vm.userid, json.dumps(request), 'json')
799
        self.assertEqual(response.status_code, 409)
800
        self.assertFalse(mrapi.mock_calls)
801

    
802
    def test_destroy_build_vm(self, mrapi, mimage):
803
        """Test building in progress"""
804
        vm = mfactory.VirtualMachineFactory()
805
        mrapi().DeleteInstance.return_value = 2
806
        response = self.mydelete('servers/%d' % vm.id,
807
                                 vm.userid)
808
        self.assertSuccess(response)
809
        mrapi().RemoveInstance.assert_called_once()
810

    
811
    def test_firewall(self, mrapi, mimage):
812
        vm = mfactory.VirtualMachineFactory()
813
        vm.operstate = "STOPPED"
814
        vm.save()
815
        request = {'firewallProfile': {'profile': 'PROTECTED'}}
816
        response = self.mypost('servers/%d/action' % vm.id,
817
                               vm.userid, json.dumps(request), 'json')
818
        self.assertBadRequest(response)
819
        request = {'firewallProfile': {'profile': 'PROTECTED', "nic": "10"}}
820
        response = self.mypost('servers/%d/action' % vm.id,
821
                               vm.userid, json.dumps(request), 'json')
822
        self.assertItemNotFound(response)
823
        nic = mfactory.NetworkInterfaceFactory(machine=vm)
824
        request = {'firewallProfile': {'profile': 'PROTECTED', "nic": nic.id}}
825
        response = self.mypost('servers/%d/action' % vm.id,
826
                               vm.userid, json.dumps(request), 'json')
827
        self.assertSuccess(response)
828
        mrapi().ModifyInstance.assert_called_once()
829

    
830
    def test_unsupported_firewall(self, mrapi, mimage):
831
        vm = mfactory.VirtualMachineFactory()
832
        vm.operstate = "STOPPED"
833
        vm.save()
834
        request = {'firewallProfile': {'profile': 'FOO'}}
835
        response = self.mypost('servers/%d/action' % vm.id,
836
                               vm.userid, json.dumps(request), 'json')
837
        self.assertBadRequest(response)
838
        self.assertFalse(mrapi.mock_calls)
839

    
840
    def test_resize_vm(self, mrapi, mimage):
841
        flavor = mfactory.FlavorFactory(cpu=1, ram=1024)
842
        # Check building VM
843
        vm = self.get_vm(flavor=flavor, operstate="BUILD")
844
        request = {'resize': {'flavorRef': flavor.id}}
845
        response = self.mypost('servers/%d/action' % vm.id,
846
                               vm.userid, json.dumps(request), 'json')
847
        self.assertFault(response, 409, "buildInProgress")
848
        # Check same Flavor
849
        vm = self.get_vm(flavor=flavor, operstate="STOPPED")
850
        request = {'resize': {'flavorRef': flavor.id}}
851
        response = self.mypost('servers/%d/action' % vm.id,
852
                               vm.userid, json.dumps(request), 'json')
853
        self.assertBadRequest(response)
854
        # Check flavor with different disk
855
        flavor2 = mfactory.FlavorFactory(disk=1024)
856
        flavor3 = mfactory.FlavorFactory(disk=2048)
857
        vm = self.get_vm(flavor=flavor2, operstate="STOPPED")
858
        request = {'resize': {'flavorRef': flavor3.id}}
859
        response = self.mypost('servers/%d/action' % vm.id,
860
                               vm.userid, json.dumps(request), 'json')
861
        self.assertBadRequest(response)
862
        flavor2 = mfactory.FlavorFactory(disk_template="foo")
863
        flavor3 = mfactory.FlavorFactory(disk_template="baz")
864
        vm = self.get_vm(flavor=flavor2, operstate="STOPPED")
865
        request = {'resize': {'flavorRef': flavor3.id}}
866
        response = self.mypost('servers/%d/action' % vm.id,
867
                               vm.userid, json.dumps(request), 'json')
868
        self.assertBadRequest(response)
869
        # Check success
870
        vm = self.get_vm(flavor=flavor, operstate="STOPPED")
871
        flavor4 = mfactory.FlavorFactory(disk_template=flavor.disk_template,
872
                                         disk=flavor.disk,
873
                                         cpu=4, ram=2048)
874
        request = {'resize': {'flavorRef': flavor4.id}}
875
        mrapi().ModifyInstance.return_value = 42
876
        response = self.mypost('servers/%d/action' % vm.id,
877
                               vm.userid, json.dumps(request), 'json')
878
        self.assertEqual(response.status_code, 202)
879
        vm = VirtualMachine.objects.get(id=vm.id)
880
        self.assertEqual(vm.task_job_id, 42)
881
        name, args, kwargs = mrapi().ModifyInstance.mock_calls[0]
882
        self.assertEqual(kwargs["beparams"]["vcpus"], 4)
883
        self.assertEqual(kwargs["beparams"]["minmem"], 2048)
884
        self.assertEqual(kwargs["beparams"]["maxmem"], 2048)
885

    
886
    def test_action_on_resizing_vm(self, mrapi, mimage):
887
        vm = mfactory.VirtualMachineFactory()
888
        vm.operstate = "RESIZE"
889
        vm.save()
890
        for action in VirtualMachine.ACTIONS:
891
            request = {action[0]: ""}
892
            response = self.mypost('servers/%d/action' % vm.id,
893
                                   vm.userid, json.dumps(request), 'json')
894
            self.assertBadRequest(response)
895
        # however you can destroy
896
        mrapi().DeleteInstance.return_value = 42
897
        response = self.mydelete('servers/%d' % vm.id,
898
                                 vm.userid)
899
        self.assertSuccess(response)
900

    
901
    def get_vm(self, flavor, operstate):
902
        vm = mfactory.VirtualMachineFactory(flavor=flavor)
903
        vm.operstate = operstate
904
        vm.backendjobstatus = "success"
905
        vm.save()
906
        return vm
907

    
908

    
909
class ServerVNCConsole(ComputeAPITest):
910
    def test_not_active_server(self):
911
        """Test console req for not ACTIVE server returns badRequest"""
912
        vm = mfactory.VirtualMachineFactory(operstate="BUILD")
913
        data = json.dumps({'console': {'type': 'vnc'}})
914
        response = self.mypost('servers/%d/action' % vm.id,
915
                               vm.userid, data, 'json')
916
        self.assertBadRequest(response)
917

    
918
    def test_active_server(self):
919
        """Test console req for ACTIVE server"""
920
        vm = mfactory.VirtualMachineFactory()
921
        vm.operstate = 'STARTED'
922
        vm.save()
923

    
924
        data = json.dumps({'console': {'type': 'vnc'}})
925
        with override_settings(settings, TEST=True):
926
            response = self.mypost('servers/%d/action' % vm.id,
927
                                   vm.userid, data, 'json')
928
        self.assertEqual(response.status_code, 200)
929
        reply = json.loads(response.content)
930
        self.assertEqual(reply.keys(), ['console'])
931
        console = reply['console']
932
        self.assertEqual(console['type'], 'vnc')
933
        self.assertEqual(set(console.keys()),
934
                         set(['type', 'host', 'port', 'password']))
935

    
936
    def test_wrong_console_type(self):
937
        """Test console req for ACTIVE server"""
938
        vm = mfactory.VirtualMachineFactory()
939
        vm.operstate = 'STARTED'
940
        vm.save()
941

    
942
        data = json.dumps({'console': {'type': 'foo'}})
943
        response = self.mypost('servers/%d/action' % vm.id,
944
                               vm.userid, data, 'json')
945
        self.assertBadRequest(response)
946

    
947

    
948
@patch('synnefo.logic.rapi_pool.GanetiRapiClient')
949
class ServerAttachments(ComputeAPITest):
950
    def test_list_attachments(self, mrapi):
951
        # Test default volume
952
        vol = mfactory.VolumeFactory()
953
        vm = vol.machine
954

    
955
        response = self.myget("servers/%d/os-volume_attachments" % vm.id,
956
                              vm.userid)
957
        self.assertSuccess(response)
958
        attachments = json.loads(response.content)
959
        self.assertEqual(len(attachments), 1)
960
        self.assertEqual(attachments["volumeAttachments"][0],
961
                         {"volumeId": vol.id,
962
                          "serverId": vm.id,
963
                          "id": vol.id,
964
                          "device": ""})
965

    
966
        # Test deleted Volume
967
        dvol = mfactory.VolumeFactory(machine=vm, deleted=True)
968
        response = self.myget("servers/%d/os-volume_attachments" % vm.id,
969
                              vm.userid)
970
        self.assertSuccess(response)
971
        attachments = json.loads(response.content)["volumeAttachments"]
972
        self.assertEqual(len([d for d in attachments if d["id"] == dvol.id]),
973
                         0)
974

    
975
    def test_attach_detach_volume(self, mrapi):
976
        vol = mfactory.VolumeFactory(status="AVAILABLE")
977
        vm = vol.machine
978
        disk_template = vm.flavor.disk_template
979
        # Test that we cannot detach the root volume
980
        response = self.mydelete("servers/%d/os-volume_attachments/%d" %
981
                                 (vm.id, vol.id), vm.userid)
982
        self.assertBadRequest(response)
983

    
984
        # Test that we cannot attach a used volume
985
        vol1 = mfactory.VolumeFactory(status="IN_USE",
986
                                      disk_template=disk_template,
987
                                      userid=vm.userid)
988
        request = json.dumps({"volumeAttachment": {"volumeId": vol1.id}})
989
        response = self.mypost("servers/%d/os-volume_attachments" %
990
                               vm.id, vm.userid,
991
                               request, "json")
992
        self.assertBadRequest(response)
993

    
994
        # We cannot attach a volume of different disk template
995
        vol1.status = "AVAILABLE"
996
        vol1.disk_template = "lalalal"
997
        vol1.save()
998
        response = self.mypost("servers/%d/os-volume_attachments/" %
999
                               vm.id, vm.userid,
1000
                               request, "json")
1001
        self.assertBadRequest(response)
1002

    
1003
        vol1.disk_template = disk_template
1004
        vol1.save()
1005
        mrapi().ModifyInstance.return_value = 43
1006
        response = self.mypost("servers/%d/os-volume_attachments" %
1007
                               vm.id, vm.userid,
1008
                               request, "json")
1009
        self.assertEqual(response.status_code, 202, response.content)
1010
        attachment = json.loads(response.content)["volumeAttachment"]
1011
        self.assertEqual(attachment, {"volumeId": vol1.id,
1012
                                      "serverId": vm.id,
1013
                                      "id": vol1.id,
1014
                                      "device": ""})
1015
        # And we delete it...will fail because of status
1016
        response = self.mydelete("servers/%d/os-volume_attachments/%d" %
1017
                                 (vm.id, vol1.id), vm.userid)
1018
        self.assertBadRequest(response)
1019
        vm.task = None
1020
        vm.save()
1021
        vm.volumes.all().update(status="IN_USE")
1022
        response = self.mydelete("servers/%d/os-volume_attachments/%d" %
1023
                                 (vm.id, vol1.id), vm.userid)
1024
        self.assertEqual(response.status_code, 202, response.content)