Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (10.8 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 TestCase
33
#from snf_django.utils.testing import mocked_quotaholder
34
from synnefo.logic import servers
35
from synnefo.db import models_factory as mfactory
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 synnefo import settings
41

    
42

    
43
@patch("synnefo.logic.rapi_pool.GanetiRapiClient")
44
class ServerTest(TestCase):
45
    def test_create(self, mrapi):
46
        flavor = mfactory.FlavorFactory()
47
        backend = mfactory.BackendFactory()
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
            "use_backend": backend,
58
        }
59

    
60
        mrapi().CreateInstance.return_value = 42
61
        with override_settings(settings,
62
                               DEFAULT_INSTANCE_NETWORKS=[]):
63
            with mocked_quotaholder():
64
                vm = servers.create(**kwargs)
65
        self.assertEqual(vm.nics.count(), 0)
66

    
67
        # test connect in IPv6 only network
68
        net = mfactory.IPv6NetworkFactory(state="ACTIVE")
69
        mfactory.BackendNetworkFactory(network=net)
70
        with override_settings(settings,
71
                               DEFAULT_INSTANCE_NETWORKS=[str(net.id)]):
72
            with mocked_quotaholder():
73
                vm = servers.create(**kwargs)
74
        nics = vm.nics.all()
75
        self.assertEqual(len(nics), 1)
76
        self.assertEqual(nics[0].ipv4, None)
77
        args, kwargs = mrapi().CreateInstance.call_args
78
        ganeti_nic = kwargs["nics"][0]
79
        self.assertEqual(ganeti_nic["ip"], None)
80
        self.assertEqual(ganeti_nic["network"], net.backend_id)
81

    
82
    def test_connect_network(self, mrapi):
83
        # Common connect
84
        net = mfactory.NetworkFactory(subnet="192.168.2.0/24",
85
                                      gateway="192.168.2.1",
86
                                      state="ACTIVE",
87
                                      dhcp=True,
88
                                      flavor="CUSTOM")
89
        vm = mfactory.VirtualMachineFactory(operstate="ACTIVE")
90
        mfactory.BackendNetworkFactory(network=net, backend=vm.backend)
91
        mrapi().ModifyInstance.return_value = 42
92
        servers.connect(vm, net)
93
        pool = net.get_pool(with_lock=False)
94
        self.assertFalse(pool.is_available("192.168.2.2"))
95
        args, kwargs = mrapi().ModifyInstance.call_args
96
        nics = kwargs["nics"][0]
97
        self.assertEqual(args[0], vm.backend_vm_id)
98
        self.assertEqual(nics[0], "add")
99
        self.assertEqual(nics[1]["ip"], "192.168.2.2")
100
        self.assertEqual(nics[1]["network"], net.backend_id)
101

    
102
        # No dhcp
103
        vm = mfactory.VirtualMachineFactory(operstate="ACTIVE")
104
        net = mfactory.NetworkFactory(subnet="192.168.2.0/24",
105
                                      gateway="192.168.2.1",
106
                                      state="ACTIVE",
107
                                      dhcp=False)
108
        mfactory.BackendNetworkFactory(network=net, backend=vm.backend)
109
        servers.connect(vm, net)
110
        pool = net.get_pool(with_lock=False)
111
        self.assertTrue(pool.is_available("192.168.2.2"))
112
        args, kwargs = mrapi().ModifyInstance.call_args
113
        nics = kwargs["nics"][0]
114
        self.assertEqual(args[0], vm.backend_vm_id)
115
        self.assertEqual(nics[0], "add")
116
        self.assertEqual(nics[1]["ip"], None)
117
        self.assertEqual(nics[1]["network"], net.backend_id)
118

    
119
        # Test connect to IPv6 only network
120
        vm = mfactory.VirtualMachineFactory(operstate="ACTIVE")
121
        net = mfactory.NetworkFactory(subnet6="2000::/64",
122
                                      state="ACTIVE",
123
                                      gateway="2000::1")
124
        mfactory.BackendNetworkFactory(network=net, backend=vm.backend)
125
        servers.connect(vm, net)
126
        args, kwargs = mrapi().ModifyInstance.call_args
127
        nics = kwargs["nics"][0]
128
        self.assertEqual(args[0], vm.backend_vm_id)
129
        self.assertEqual(nics[0], "add")
130
        self.assertEqual(nics[1]["ip"], None)
131
        self.assertEqual(nics[1]["network"], net.backend_id)
132

    
133

    
134
@patch("synnefo.logic.rapi_pool.GanetiRapiClient")
135
class ServerCommandTest(TestCase):
136
    def test_pending_task(self, mrapi):
137
        vm = mfactory.VirtualMachineFactory(task="REBOOT", task_job_id=1)
138
        self.assertRaises(faults.BadRequest, servers.start, vm)
139
        vm = mfactory.VirtualMachineFactory(task="BUILD", task_job_id=1)
140
        self.assertRaises(faults.BuildInProgress, servers.start, vm)
141
        # Assert always succeeds
142
        vm = mfactory.VirtualMachineFactory(task="BUILD", task_job_id=1)
143
        mrapi().DeleteInstance.return_value = 1
144
        with mocked_quotaholder():
145
            servers.destroy(vm)
146
        vm = mfactory.VirtualMachineFactory(task="REBOOT", task_job_id=1)
147
        with mocked_quotaholder():
148
            servers.destroy(vm)
149

    
150
    def test_deleted_vm(self, mrapi):
151
        vm = mfactory.VirtualMachineFactory(deleted=True)
152
        self.assertRaises(faults.BadRequest, servers.start, vm)
153

    
154
    def test_invalid_operstate_for_action(self, mrapi):
155
        vm = mfactory.VirtualMachineFactory(operstate="STARTED")
156
        self.assertRaises(faults.BadRequest, servers.start, vm)
157
        vm = mfactory.VirtualMachineFactory(operstate="STOPPED")
158
        self.assertRaises(faults.BadRequest, servers.stop, vm)
159
        vm = mfactory.VirtualMachineFactory(operstate="STARTED")
160
        self.assertRaises(faults.BadRequest, servers.resize, vm)
161
        vm = mfactory.VirtualMachineFactory(operstate="STOPPED")
162
        self.assertRaises(faults.BadRequest, servers.stop, vm)
163
        #test valid
164
        mrapi().StartupInstance.return_value = 1
165
        with mocked_quotaholder():
166
            servers.start(vm)
167
        vm.task = None
168
        vm.task_job_id = None
169
        vm.save()
170
        mrapi().RebootInstance.return_value = 1
171
        with mocked_quotaholder():
172
            servers.reboot(vm, "HARD")
173

    
174
    def test_commission(self, mrapi):
175
        vm = mfactory.VirtualMachineFactory(operstate="STOPPED")
176
        # Still pending
177
        vm.serial = mfactory.QuotaHolderSerialFactory(serial=200,
178
                                                      resolved=False,
179
                                                      pending=True)
180
        serial = vm.serial
181
        mrapi().StartupInstance.return_value = 1
182
        with mocked_quotaholder() as m:
183
            servers.start(vm)
184
            m.resolve_commissions.assert_called_once_with('', [],
185
                                                          [serial.serial])
186
            self.assertTrue(m.issue_one_commission.called)
187
        # Not pending, rejct
188
        vm.task = None
189
        vm.serial = mfactory.QuotaHolderSerialFactory(serial=400,
190
                                                      resolved=False,
191
                                                      pending=False,
192
                                                      accept=False)
193
        serial = vm.serial
194
        mrapi().StartupInstance.return_value = 1
195
        with mocked_quotaholder() as m:
196
            servers.start(vm)
197
            m.resolve_commissions.assert_called_once_with('', [],
198
                                                          [serial.serial])
199
            self.assertTrue(m.issue_one_commission.called)
200
        # Not pending, accept
201
        vm.task = None
202
        vm.serial = mfactory.QuotaHolderSerialFactory(serial=600,
203
                                                      resolved=False,
204
                                                      pending=False,
205
                                                      accept=True)
206
        serial = vm.serial
207
        mrapi().StartupInstance.return_value = 1
208
        with mocked_quotaholder() as m:
209
            servers.start(vm)
210
            m.resolve_commissions.assert_called_once_with('', [serial.serial],
211
                                                          [])
212
            self.assertTrue(m.issue_one_commission.called)
213

    
214
        mrapi().StartupInstance.side_effect = ValueError
215
        vm.task = None
216
        vm.serial = None
217
        # Test reject if Ganeti erro
218
        with mocked_quotaholder() as m:
219
            try:
220
                servers.start(vm)
221
            except:
222
                m.resolve_commissions\
223
                 .assert_called_once_with('', [], [vm.serial.serial])
224

    
225
    def test_task_after(self, mrapi):
226
        return
227
        vm = mfactory.VirtualMachineFactory()
228
        mrapi().StartupInstance.return_value = 1
229
        mrapi().ShutdownInstance.return_value = 2
230
        mrapi().RebootInstance.return_value = 2
231
        with mocked_quotaholder():
232
            vm.task = None
233
            vm.operstate = "STOPPED"
234
            servers.start(vm)
235
            self.assertEqual(vm.task, "START")
236
            self.assertEqual(vm.task_job_id, 1)
237
        with mocked_quotaholder():
238
            vm.task = None
239
            vm.operstate = "STARTED"
240
            servers.stop(vm)
241
            self.assertEqual(vm.task, "STOP")
242
            self.assertEqual(vm.task_job_id, 2)
243
        with mocked_quotaholder():
244
            vm.task = None
245
            servers.reboot(vm)
246
            self.assertEqual(vm.task, "REBOOT")
247
            self.assertEqual(vm.task_job_id, 3)