Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / logic / tests / servers.py @ 3b2984dc

History | View | Annotate | Download (12.2 kB)

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

    
31
# Provides automated tests for logic module
32
from django.test import TransactionTestCase
33
#from snf_django.utils.testing import mocked_quotaholder
34
from synnefo.logic import servers
35
from synnefo.db import models_factory as mfactory, models
36
from mock import patch
37

    
38
from snf_django.lib.api import faults
39
from snf_django.utils.testing import mocked_quotaholder, override_settings
40
from django.conf import settings
41
from copy import deepcopy
42

    
43

    
44
@patch("synnefo.logic.rapi_pool.GanetiRapiClient")
45
class ServerCreationTest(TransactionTestCase):
46
    def test_create(self, mrapi):
47
        flavor = mfactory.FlavorFactory()
48
        kwargs = {
49
            "userid": "test",
50
            "name": "test_vm",
51
            "password": "1234",
52
            "flavor": flavor,
53
            "image": {"id": "foo", "backend_id": "foo", "format": "diskdump",
54
                      "metadata": "{}"},
55
            "metadata": {"foo": "bar"},
56
            "personality": [],
57
        }
58
        # no backend!
59
        mfactory.BackendFactory(offline=True)
60
        self.assertRaises(faults.ServiceUnavailable, servers.create, **kwargs)
61
        self.assertEqual(models.VirtualMachine.objects.count(), 0)
62

    
63
        subnet = mfactory.IPv4SubnetFactory(network__public=True)
64
        bn = mfactory.BackendNetworkFactory(network=subnet.network)
65
        backend = bn.backend
66

    
67
        # error in nics
68
        req = deepcopy(kwargs)
69
        req["private_networks"] = [42]
70
        self.assertRaises(faults.ItemNotFound, servers.create, **req)
71
        self.assertEqual(models.VirtualMachine.objects.count(), 0)
72

    
73
        # error in enqueue. check the vm is deleted and resources released
74
        mrapi().CreateInstance.side_effect = Exception("ganeti is down")
75
        with mocked_quotaholder():
76
            servers.create(**kwargs)
77
        vm = models.VirtualMachine.objects.get()
78
        self.assertFalse(vm.deleted)
79
        self.assertEqual(vm.operstate, "ERROR")
80
        self.assertEqual(len(vm.nics.all()), 1)
81
        for nic in vm.nics.all():
82
            self.assertEqual(nic.state, "ERROR")
83

    
84
        # success with no nics
85
        mrapi().CreateInstance.side_effect = None
86
        mrapi().CreateInstance.return_value = 42
87
        with override_settings(settings,
88
                               DEFAULT_INSTANCE_NETWORKS=[]):
89
            with mocked_quotaholder():
90
                vm = servers.create(**kwargs)
91
        vm = models.VirtualMachine.objects.get(id=vm.id)
92
        self.assertEqual(vm.nics.count(), 0)
93
        self.assertEqual(vm.backendjobid, 42)
94
        self.assertEqual(vm.task_job_id, 42)
95
        self.assertEqual(vm.task, "BUILD")
96

    
97
        # test connect in IPv6 only network
98
        subnet = mfactory.IPv6SubnetFactory(network__public=True)
99
        net = subnet.network
100
        mfactory.BackendNetworkFactory(network=net, backend=backend,
101
                                       operstate="ACTIVE")
102
        with override_settings(settings,
103
                               DEFAULT_INSTANCE_NETWORKS=[str(net.id)]):
104
            with mocked_quotaholder():
105
                vm = servers.create(**kwargs)
106
        nics = vm.nics.all()
107
        self.assertEqual(len(nics), 1)
108
        self.assertFalse(nics[0].ips.filter(subnet__ipversion=4).exists())
109
        args, kwargs = mrapi().CreateInstance.call_args
110
        ganeti_nic = kwargs["nics"][0]
111
        self.assertEqual(ganeti_nic["ip"], None)
112
        self.assertEqual(ganeti_nic["network"], net.backend_id)
113

    
114

    
115
@patch("synnefo.logic.rapi_pool.GanetiRapiClient")
116
class ServerTest(TransactionTestCase):
117
    def test_connect_network(self, mrapi):
118
        # Common connect
119
        for dhcp in [True, False]:
120
            subnet = mfactory.IPv4SubnetFactory(network__flavor="CUSTOM",
121
                                                cidr="192.168.2.0/24",
122
                                                gateway="192.168.2.1",
123
                                                dhcp=dhcp)
124
            net = subnet.network
125
            vm = mfactory.VirtualMachineFactory(operstate="STARTED")
126
            mfactory.BackendNetworkFactory(network=net, backend=vm.backend)
127
            mrapi().ModifyInstance.return_value = 42
128
            servers.connect(vm, net)
129
            pool = net.get_ip_pools(locked=False)[0]
130
            self.assertFalse(pool.is_available("192.168.2.2"))
131
            args, kwargs = mrapi().ModifyInstance.call_args
132
            nics = kwargs["nics"][0]
133
            self.assertEqual(kwargs["instance"], vm.backend_vm_id)
134
            self.assertEqual(nics[0], "add")
135
            self.assertEqual(nics[1], "-1")
136
            self.assertEqual(nics[2]["ip"], "192.168.2.2")
137
            self.assertEqual(nics[2]["network"], net.backend_id)
138

    
139
        # Test connect to IPv6 only network
140
        vm = mfactory.VirtualMachineFactory(operstate="STARTED")
141
        subnet = mfactory.IPv6SubnetFactory(cidr="2000::/64",
142
                                            gateway="2000::1")
143
        net = subnet.network
144
        mfactory.BackendNetworkFactory(network=net, backend=vm.backend)
145
        servers.connect(vm, net)
146
        args, kwargs = mrapi().ModifyInstance.call_args
147
        nics = kwargs["nics"][0]
148
        self.assertEqual(kwargs["instance"], vm.backend_vm_id)
149
        self.assertEqual(nics[0], "add")
150
        self.assertEqual(nics[1], "-1")
151
        self.assertEqual(nics[2]["ip"], None)
152
        self.assertEqual(nics[2]["network"], net.backend_id)
153

    
154

    
155
@patch("synnefo.logic.rapi_pool.GanetiRapiClient")
156
class ServerCommandTest(TransactionTestCase):
157
    def test_pending_task(self, mrapi):
158
        vm = mfactory.VirtualMachineFactory(task="REBOOT", task_job_id=1)
159
        self.assertRaises(faults.BadRequest, servers.start, vm)
160
        vm = mfactory.VirtualMachineFactory(task="BUILD", task_job_id=1)
161
        self.assertRaises(faults.BuildInProgress, servers.start, vm)
162
        # Assert always succeeds
163
        vm = mfactory.VirtualMachineFactory(task="BUILD", task_job_id=1)
164
        mrapi().DeleteInstance.return_value = 1
165
        with mocked_quotaholder():
166
            servers.destroy(vm)
167
        vm = mfactory.VirtualMachineFactory(task="REBOOT", task_job_id=1)
168
        with mocked_quotaholder():
169
            servers.destroy(vm)
170

    
171
    def test_deleted_vm(self, mrapi):
172
        vm = mfactory.VirtualMachineFactory(deleted=True)
173
        self.assertRaises(faults.BadRequest, servers.start, vm)
174

    
175
    def test_invalid_operstate_for_action(self, mrapi):
176
        vm = mfactory.VirtualMachineFactory(operstate="STARTED")
177
        self.assertRaises(faults.BadRequest, servers.start, vm)
178
        vm = mfactory.VirtualMachineFactory(operstate="STOPPED")
179
        self.assertRaises(faults.BadRequest, servers.stop, vm)
180
        vm = mfactory.VirtualMachineFactory(operstate="STARTED")
181
        self.assertRaises(faults.BadRequest, servers.resize, vm)
182
        # Check that connect/disconnect is allowed only in STOPPED vms
183
        # if hotplug is disabled.
184
        vm = mfactory.VirtualMachineFactory(operstate="STARTED")
185
        network = mfactory.NetworkFactory(state="ACTIVE")
186
        with override_settings(settings, GANETI_USE_HOTPLUG=False):
187
            self.assertRaises(faults.BadRequest, servers.connect, vm, network)
188
            self.assertRaises(faults.BadRequest, servers.disconnect, vm,
189
                              network)
190
        #test valid
191
        vm = mfactory.VirtualMachineFactory(operstate="STOPPED")
192
        mrapi().StartupInstance.return_value = 1
193
        with mocked_quotaholder():
194
            servers.start(vm)
195
        vm.task = None
196
        vm.task_job_id = None
197
        vm.save()
198
        mrapi().RebootInstance.return_value = 1
199
        with mocked_quotaholder():
200
            servers.reboot(vm, "HARD")
201

    
202
    def test_commission(self, mrapi):
203
        vm = mfactory.VirtualMachineFactory(operstate="STOPPED")
204
        # Still pending
205
        vm.serial = mfactory.QuotaHolderSerialFactory(serial=200,
206
                                                      resolved=False,
207
                                                      pending=True)
208
        serial = vm.serial
209
        mrapi().StartupInstance.return_value = 1
210
        with mocked_quotaholder() as m:
211
            servers.start(vm)
212
            m.resolve_commissions.assert_called_once_with('', [],
213
                                                          [serial.serial])
214
            self.assertTrue(m.issue_one_commission.called)
215
        # Not pending, rejct
216
        vm.task = None
217
        vm.serial = mfactory.QuotaHolderSerialFactory(serial=400,
218
                                                      resolved=False,
219
                                                      pending=False,
220
                                                      accept=False)
221
        serial = vm.serial
222
        mrapi().StartupInstance.return_value = 1
223
        with mocked_quotaholder() as m:
224
            servers.start(vm)
225
            m.resolve_commissions.assert_called_once_with('', [],
226
                                                          [serial.serial])
227
            self.assertTrue(m.issue_one_commission.called)
228
        # Not pending, accept
229
        vm.task = None
230
        vm.serial = mfactory.QuotaHolderSerialFactory(serial=600,
231
                                                      resolved=False,
232
                                                      pending=False,
233
                                                      accept=True)
234
        serial = vm.serial
235
        mrapi().StartupInstance.return_value = 1
236
        with mocked_quotaholder() as m:
237
            servers.start(vm)
238
            m.resolve_commissions.assert_called_once_with('', [serial.serial],
239
                                                          [])
240
            self.assertTrue(m.issue_one_commission.called)
241

    
242
        mrapi().StartupInstance.side_effect = ValueError
243
        vm.task = None
244
        vm.serial = None
245
        # Test reject if Ganeti erro
246
        with mocked_quotaholder() as m:
247
            try:
248
                servers.start(vm)
249
            except:
250
                m.resolve_commissions\
251
                 .assert_called_once_with('', [], [vm.serial.serial])
252

    
253
    def test_task_after(self, mrapi):
254
        return
255
        vm = mfactory.VirtualMachineFactory()
256
        mrapi().StartupInstance.return_value = 1
257
        mrapi().ShutdownInstance.return_value = 2
258
        mrapi().RebootInstance.return_value = 2
259
        with mocked_quotaholder():
260
            vm.task = None
261
            vm.operstate = "STOPPED"
262
            servers.start(vm)
263
            self.assertEqual(vm.task, "START")
264
            self.assertEqual(vm.task_job_id, 1)
265
        with mocked_quotaholder():
266
            vm.task = None
267
            vm.operstate = "STARTED"
268
            servers.stop(vm)
269
            self.assertEqual(vm.task, "STOP")
270
            self.assertEqual(vm.task_job_id, 2)
271
        with mocked_quotaholder():
272
            vm.task = None
273
            servers.reboot(vm)
274
            self.assertEqual(vm.task, "REBOOT")
275
            self.assertEqual(vm.task_job_id, 3)