Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / logic / tests / servers.py @ 9dcfad23

History | View | Annotate | Download (12.6 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
        mfactory.BackendFactory(drained=False)
64
        mfactory.BackendNetworkFactory(network__public=True)
65

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

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

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

    
96
        # test connect in IPv6 only network
97
        net = mfactory.IPv6NetworkFactory(state="ACTIVE")
98
        mfactory.BackendNetworkFactory(network=net)
99
        with override_settings(settings,
100
                               DEFAULT_INSTANCE_NETWORKS=[str(net.id)]):
101
            with mocked_quotaholder():
102
                vm = servers.create(**kwargs)
103
        nics = vm.nics.all()
104
        self.assertEqual(len(nics), 1)
105
        self.assertEqual(nics[0].ipv4, None)
106
        args, kwargs = mrapi().CreateInstance.call_args
107
        ganeti_nic = kwargs["nics"][0]
108
        self.assertEqual(ganeti_nic["ip"], None)
109
        self.assertEqual(ganeti_nic["network"], net.backend_id)
110

    
111

    
112
@patch("synnefo.logic.rapi_pool.GanetiRapiClient")
113
class ServerTest(TransactionTestCase):
114
    def test_connect_network(self, mrapi):
115
        # Common connect
116
        net = mfactory.NetworkFactory(subnet="192.168.2.0/24",
117
                                      gateway="192.168.2.1",
118
                                      state="ACTIVE",
119
                                      dhcp=True,
120
                                      flavor="CUSTOM")
121
        vm = mfactory.VirtualMachineFactory(operstate="STARTED")
122
        mfactory.BackendNetworkFactory(network=net, backend=vm.backend)
123
        mrapi().ModifyInstance.return_value = 42
124
        servers.connect(vm, net)
125
        pool = net.get_pool(with_lock=False)
126
        self.assertFalse(pool.is_available("192.168.2.2"))
127
        args, kwargs = mrapi().ModifyInstance.call_args
128
        nics = kwargs["nics"][0]
129
        self.assertEqual(kwargs["instance"], vm.backend_vm_id)
130
        self.assertEqual(nics[0], "add")
131
        self.assertEqual(nics[1]["ip"], "192.168.2.2")
132
        self.assertEqual(nics[1]["network"], net.backend_id)
133

    
134
        # No dhcp
135
        vm = mfactory.VirtualMachineFactory(operstate="STARTED")
136
        net = mfactory.NetworkFactory(subnet="192.168.2.0/24",
137
                                      gateway="192.168.2.1",
138
                                      state="ACTIVE",
139
                                      dhcp=False)
140
        mfactory.BackendNetworkFactory(network=net, backend=vm.backend)
141
        servers.connect(vm, net)
142
        pool = net.get_pool(with_lock=False)
143
        self.assertTrue(pool.is_available("192.168.2.2"))
144
        args, kwargs = mrapi().ModifyInstance.call_args
145
        nics = kwargs["nics"][0]
146
        self.assertEqual(kwargs["instance"], vm.backend_vm_id)
147
        self.assertEqual(nics[0], "add")
148
        self.assertEqual(nics[1]["ip"], None)
149
        self.assertEqual(nics[1]["network"], net.backend_id)
150

    
151
        # Test connect to IPv6 only network
152
        vm = mfactory.VirtualMachineFactory(operstate="STARTED")
153
        net = mfactory.NetworkFactory(subnet6="2000::/64",
154
                                      state="ACTIVE",
155
                                      gateway="2000::1")
156
        mfactory.BackendNetworkFactory(network=net, backend=vm.backend)
157
        servers.connect(vm, net)
158
        args, kwargs = mrapi().ModifyInstance.call_args
159
        nics = kwargs["nics"][0]
160
        self.assertEqual(kwargs["instance"], vm.backend_vm_id)
161
        self.assertEqual(nics[0], "add")
162
        self.assertEqual(nics[1]["ip"], None)
163
        self.assertEqual(nics[1]["network"], net.backend_id)
164

    
165

    
166
@patch("synnefo.logic.rapi_pool.GanetiRapiClient")
167
class ServerCommandTest(TransactionTestCase):
168
    def test_pending_task(self, mrapi):
169
        vm = mfactory.VirtualMachineFactory(task="REBOOT", task_job_id=1)
170
        self.assertRaises(faults.BadRequest, servers.start, vm)
171
        vm = mfactory.VirtualMachineFactory(task="BUILD", task_job_id=1)
172
        self.assertRaises(faults.BuildInProgress, servers.start, vm)
173
        # Assert always succeeds
174
        vm = mfactory.VirtualMachineFactory(task="BUILD", task_job_id=1)
175
        mrapi().DeleteInstance.return_value = 1
176
        with mocked_quotaholder():
177
            servers.destroy(vm)
178
        vm = mfactory.VirtualMachineFactory(task="REBOOT", task_job_id=1)
179
        with mocked_quotaholder():
180
            servers.destroy(vm)
181

    
182
    def test_deleted_vm(self, mrapi):
183
        vm = mfactory.VirtualMachineFactory(deleted=True)
184
        self.assertRaises(faults.BadRequest, servers.start, vm)
185

    
186
    def test_invalid_operstate_for_action(self, mrapi):
187
        vm = mfactory.VirtualMachineFactory(operstate="STARTED")
188
        self.assertRaises(faults.BadRequest, servers.start, vm)
189
        vm = mfactory.VirtualMachineFactory(operstate="STOPPED")
190
        self.assertRaises(faults.BadRequest, servers.stop, vm)
191
        vm = mfactory.VirtualMachineFactory(operstate="STARTED")
192
        self.assertRaises(faults.BadRequest, servers.resize, vm)
193
        # Check that connect/disconnect is allowed only in STOPPED vms
194
        # if hotplug is disabled.
195
        vm = mfactory.VirtualMachineFactory(operstate="STARTED")
196
        network = mfactory.NetworkFactory(state="ACTIVE")
197
        with override_settings(settings, GANETI_USE_HOTPLUG=False):
198
            self.assertRaises(faults.BadRequest, servers.connect, vm, network)
199
            self.assertRaises(faults.BadRequest, servers.disconnect, vm,
200
                              network)
201
        #test valid
202
        vm = mfactory.VirtualMachineFactory(operstate="STOPPED")
203
        mrapi().StartupInstance.return_value = 1
204
        with mocked_quotaholder():
205
            servers.start(vm)
206
        vm.task = None
207
        vm.task_job_id = None
208
        vm.save()
209
        mrapi().RebootInstance.return_value = 1
210
        with mocked_quotaholder():
211
            servers.reboot(vm, "HARD")
212

    
213
    def test_commission(self, mrapi):
214
        vm = mfactory.VirtualMachineFactory(operstate="STOPPED")
215
        # Still pending
216
        vm.serial = mfactory.QuotaHolderSerialFactory(serial=200,
217
                                                      resolved=False,
218
                                                      pending=True)
219
        serial = vm.serial
220
        mrapi().StartupInstance.return_value = 1
221
        with mocked_quotaholder() as m:
222
            servers.start(vm)
223
            m.resolve_commissions.assert_called_once_with('', [],
224
                                                          [serial.serial])
225
            self.assertTrue(m.issue_one_commission.called)
226
        # Not pending, rejct
227
        vm.task = None
228
        vm.serial = mfactory.QuotaHolderSerialFactory(serial=400,
229
                                                      resolved=False,
230
                                                      pending=False,
231
                                                      accept=False)
232
        serial = vm.serial
233
        mrapi().StartupInstance.return_value = 1
234
        with mocked_quotaholder() as m:
235
            servers.start(vm)
236
            m.resolve_commissions.assert_called_once_with('', [],
237
                                                          [serial.serial])
238
            self.assertTrue(m.issue_one_commission.called)
239
        # Not pending, accept
240
        vm.task = None
241
        vm.serial = mfactory.QuotaHolderSerialFactory(serial=600,
242
                                                      resolved=False,
243
                                                      pending=False,
244
                                                      accept=True)
245
        serial = vm.serial
246
        mrapi().StartupInstance.return_value = 1
247
        with mocked_quotaholder() as m:
248
            servers.start(vm)
249
            m.resolve_commissions.assert_called_once_with('', [serial.serial],
250
                                                          [])
251
            self.assertTrue(m.issue_one_commission.called)
252

    
253
        mrapi().StartupInstance.side_effect = ValueError
254
        vm.task = None
255
        vm.serial = None
256
        # Test reject if Ganeti erro
257
        with mocked_quotaholder() as m:
258
            try:
259
                servers.start(vm)
260
            except:
261
                m.resolve_commissions\
262
                 .assert_called_once_with('', [], [vm.serial.serial])
263

    
264
    def test_task_after(self, mrapi):
265
        return
266
        vm = mfactory.VirtualMachineFactory()
267
        mrapi().StartupInstance.return_value = 1
268
        mrapi().ShutdownInstance.return_value = 2
269
        mrapi().RebootInstance.return_value = 2
270
        with mocked_quotaholder():
271
            vm.task = None
272
            vm.operstate = "STOPPED"
273
            servers.start(vm)
274
            self.assertEqual(vm.task, "START")
275
            self.assertEqual(vm.task_job_id, 1)
276
        with mocked_quotaholder():
277
            vm.task = None
278
            vm.operstate = "STARTED"
279
            servers.stop(vm)
280
            self.assertEqual(vm.task, "STOP")
281
            self.assertEqual(vm.task_job_id, 2)
282
        with mocked_quotaholder():
283
            vm.task = None
284
            servers.reboot(vm)
285
            self.assertEqual(vm.task, "REBOOT")
286
            self.assertEqual(vm.task_job_id, 3)