Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (27.6 kB)

1 b8033991 Georgios Gousios
# vim: set fileencoding=utf-8 :
2 0208770b Christos Stavrakakis
# Copyright 2013 GRNET S.A. All rights reserved.
3 09c936a7 Vassilios Karakoidas
#
4 48130e66 Georgios Gousios
# Redistribution and use in source and binary forms, with or without
5 48130e66 Georgios Gousios
# modification, are permitted provided that the following conditions
6 48130e66 Georgios Gousios
# are met:
7 09c936a7 Vassilios Karakoidas
#
8 48130e66 Georgios Gousios
#   1. Redistributions of source code must retain the above copyright
9 48130e66 Georgios Gousios
#      notice, this list of conditions and the following disclaimer.
10 48130e66 Georgios Gousios
#
11 48130e66 Georgios Gousios
#  2. Redistributions in binary form must reproduce the above copyright
12 48130e66 Georgios Gousios
#     notice, this list of conditions and the following disclaimer in the
13 48130e66 Georgios Gousios
#     documentation and/or other materials provided with the distribution.
14 09c936a7 Vassilios Karakoidas
#
15 48130e66 Georgios Gousios
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
16 48130e66 Georgios Gousios
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 48130e66 Georgios Gousios
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 48130e66 Georgios Gousios
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19 48130e66 Georgios Gousios
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 48130e66 Georgios Gousios
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 48130e66 Georgios Gousios
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 48130e66 Georgios Gousios
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 48130e66 Georgios Gousios
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 48130e66 Georgios Gousios
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 48130e66 Georgios Gousios
# SUCH DAMAGE.
26 09c936a7 Vassilios Karakoidas
#
27 48130e66 Georgios Gousios
# The views and conclusions contained in the software and documentation are
28 48130e66 Georgios Gousios
# those of the authors and should not be interpreted as representing official
29 48130e66 Georgios Gousios
# policies, either expressed or implied, of GRNET S.A.
30 48130e66 Georgios Gousios
31 48130e66 Georgios Gousios
# Provides automated tests for logic module
32 92c53da1 Vassilios Karakoidas
33 c25cc9ec Vangelis Koukis
from random import randint
34 c25cc9ec Vangelis Koukis
35 c25cc9ec Vangelis Koukis
from django.test import TestCase
36 c25cc9ec Vangelis Koukis
37 0208770b Christos Stavrakakis
from synnefo.db.models import (VirtualMachine, FloatingIP, BackendNetwork,
38 0208770b Christos Stavrakakis
                               Network, BridgePoolTable, MacPrefixPoolTable)
39 47eeffa9 Christos Stavrakakis
from synnefo.db import models_factory as mfactory
40 47eeffa9 Christos Stavrakakis
from synnefo.lib.utils import split_time
41 47eeffa9 Christos Stavrakakis
from datetime import datetime
42 47eeffa9 Christos Stavrakakis
from mock import patch
43 47eeffa9 Christos Stavrakakis
from synnefo.api.util import allocate_resource
44 fd95834e Christos Stavrakakis
from synnefo.logic.callbacks import (update_db, update_network,
45 cc92b70f Christos Stavrakakis
                                     update_build_progress)
46 184a2a8c Christos Stavrakakis
from snf_django.utils.testing import mocked_quotaholder
47 0208770b Christos Stavrakakis
from synnefo import settings
48 47eeffa9 Christos Stavrakakis
49 47eeffa9 Christos Stavrakakis
now = datetime.now
50 47eeffa9 Christos Stavrakakis
from time import time
51 47eeffa9 Christos Stavrakakis
import json
52 47eeffa9 Christos Stavrakakis
53 41a7fae7 Christos Stavrakakis
54 47eeffa9 Christos Stavrakakis
## Test Callbacks
55 47eeffa9 Christos Stavrakakis
@patch('synnefo.lib.amqp.AMQPClient')
56 47eeffa9 Christos Stavrakakis
class UpdateDBTest(TestCase):
57 47eeffa9 Christos Stavrakakis
    def create_msg(self, **kwargs):
58 47eeffa9 Christos Stavrakakis
        """Create snf-ganeti-eventd message"""
59 47eeffa9 Christos Stavrakakis
        msg = {'event_time': split_time(time())}
60 47eeffa9 Christos Stavrakakis
        msg['type'] = 'ganeti-op-status'
61 47eeffa9 Christos Stavrakakis
        msg['status'] = 'success'
62 47eeffa9 Christos Stavrakakis
        msg['jobId'] = 1
63 47eeffa9 Christos Stavrakakis
        msg['logmsg'] = 'Dummy Log'
64 47eeffa9 Christos Stavrakakis
        for key, val in kwargs.items():
65 47eeffa9 Christos Stavrakakis
            msg[key] = val
66 47eeffa9 Christos Stavrakakis
        message = {'body': json.dumps(msg)}
67 47eeffa9 Christos Stavrakakis
        return message
68 47eeffa9 Christos Stavrakakis
69 47eeffa9 Christos Stavrakakis
    def test_missing_attribute(self, client):
70 47eeffa9 Christos Stavrakakis
        update_db(client, json.dumps({'body': {}}))
71 10d9cd27 Christos Stavrakakis
        self.assertTrue(client.basic_reject.called)
72 47eeffa9 Christos Stavrakakis
73 47eeffa9 Christos Stavrakakis
    def test_unhandled_exception(self, client):
74 47eeffa9 Christos Stavrakakis
        update_db(client, {})
75 47eeffa9 Christos Stavrakakis
        client.basic_reject.assert_called_once()
76 47eeffa9 Christos Stavrakakis
77 47eeffa9 Christos Stavrakakis
    def test_missing_instance(self, client):
78 47eeffa9 Christos Stavrakakis
        msg = self.create_msg(operation='OP_INSTANCE_STARTUP',
79 47eeffa9 Christos Stavrakakis
                              instance='foo')
80 47eeffa9 Christos Stavrakakis
        update_db(client, msg)
81 10d9cd27 Christos Stavrakakis
        self.assertTrue(client.basic_ack.called)
82 47eeffa9 Christos Stavrakakis
83 47eeffa9 Christos Stavrakakis
    def test_wrong_type(self, client):
84 47eeffa9 Christos Stavrakakis
        msg = self.create_msg(type="WRONG_TYPE")
85 47eeffa9 Christos Stavrakakis
        update_db(client, msg)
86 10d9cd27 Christos Stavrakakis
        self.assertTrue(client.basic_nack.called)
87 47eeffa9 Christos Stavrakakis
88 b66e4833 Christos Stavrakakis
    def test_old_msg(self, client):
89 b66e4833 Christos Stavrakakis
        from time import sleep
90 b66e4833 Christos Stavrakakis
        from datetime import datetime
91 b66e4833 Christos Stavrakakis
        old_time = time()
92 b66e4833 Christos Stavrakakis
        sleep(0.01)
93 b66e4833 Christos Stavrakakis
        new_time = datetime.fromtimestamp(time())
94 b66e4833 Christos Stavrakakis
        vm = mfactory.VirtualMachineFactory(backendtime=new_time)
95 b66e4833 Christos Stavrakakis
        vm.operstate = 'STOPPED'
96 b66e4833 Christos Stavrakakis
        vm.save()
97 b66e4833 Christos Stavrakakis
        msg = self.create_msg(operation='OP_INSTANCE_STARTUP',
98 b66e4833 Christos Stavrakakis
                              event_time=split_time(old_time),
99 b66e4833 Christos Stavrakakis
                              instance=vm.backend_vm_id)
100 b66e4833 Christos Stavrakakis
        update_db(client, msg)
101 10d9cd27 Christos Stavrakakis
        self.assertTrue(client.basic_ack.called)
102 b66e4833 Christos Stavrakakis
        db_vm = VirtualMachine.objects.get(id=vm.id)
103 b66e4833 Christos Stavrakakis
        self.assertEquals(db_vm.operstate, "STOPPED")
104 b66e4833 Christos Stavrakakis
        self.assertEquals(db_vm.backendtime, new_time)
105 b66e4833 Christos Stavrakakis
106 47eeffa9 Christos Stavrakakis
    def test_start(self, client):
107 47eeffa9 Christos Stavrakakis
        vm = mfactory.VirtualMachineFactory()
108 47eeffa9 Christos Stavrakakis
        msg = self.create_msg(operation='OP_INSTANCE_STARTUP',
109 47eeffa9 Christos Stavrakakis
                              instance=vm.backend_vm_id)
110 41a7fae7 Christos Stavrakakis
        with mocked_quotaholder():
111 41a7fae7 Christos Stavrakakis
            update_db(client, msg)
112 10d9cd27 Christos Stavrakakis
        self.assertTrue(client.basic_ack.called)
113 47eeffa9 Christos Stavrakakis
        db_vm = VirtualMachine.objects.get(id=vm.id)
114 47eeffa9 Christos Stavrakakis
        self.assertEqual(db_vm.operstate, 'STARTED')
115 47eeffa9 Christos Stavrakakis
116 47eeffa9 Christos Stavrakakis
    def test_stop(self, client):
117 47eeffa9 Christos Stavrakakis
        vm = mfactory.VirtualMachineFactory()
118 47eeffa9 Christos Stavrakakis
        msg = self.create_msg(operation='OP_INSTANCE_SHUTDOWN',
119 47eeffa9 Christos Stavrakakis
                              instance=vm.backend_vm_id)
120 41a7fae7 Christos Stavrakakis
        with mocked_quotaholder():
121 41a7fae7 Christos Stavrakakis
            update_db(client, msg)
122 10d9cd27 Christos Stavrakakis
        self.assertTrue(client.basic_ack.called)
123 47eeffa9 Christos Stavrakakis
        db_vm = VirtualMachine.objects.get(id=vm.id)
124 47eeffa9 Christos Stavrakakis
        self.assertEqual(db_vm.operstate, 'STOPPED')
125 47eeffa9 Christos Stavrakakis
126 47eeffa9 Christos Stavrakakis
    def test_reboot(self, client):
127 47eeffa9 Christos Stavrakakis
        vm = mfactory.VirtualMachineFactory()
128 47eeffa9 Christos Stavrakakis
        msg = self.create_msg(operation='OP_INSTANCE_REBOOT',
129 47eeffa9 Christos Stavrakakis
                              instance=vm.backend_vm_id)
130 47eeffa9 Christos Stavrakakis
        update_db(client, msg)
131 10d9cd27 Christos Stavrakakis
        self.assertTrue(client.basic_ack.called)
132 47eeffa9 Christos Stavrakakis
        db_vm = VirtualMachine.objects.get(id=vm.id)
133 47eeffa9 Christos Stavrakakis
        self.assertEqual(db_vm.operstate, 'STARTED')
134 47eeffa9 Christos Stavrakakis
135 47eeffa9 Christos Stavrakakis
    def test_remove(self, client):
136 47eeffa9 Christos Stavrakakis
        vm = mfactory.VirtualMachineFactory()
137 47eeffa9 Christos Stavrakakis
        # Also create a NIC
138 72dea98f Christos Stavrakakis
        nic = mfactory.NetworkInterfaceFactory(machine=vm)
139 72dea98f Christos Stavrakakis
        nic.network.get_pool().reserve(nic.ipv4)
140 47eeffa9 Christos Stavrakakis
        msg = self.create_msg(operation='OP_INSTANCE_REMOVE',
141 47eeffa9 Christos Stavrakakis
                              instance=vm.backend_vm_id)
142 184a2a8c Christos Stavrakakis
        with mocked_quotaholder():
143 184a2a8c Christos Stavrakakis
            update_db(client, msg)
144 10d9cd27 Christos Stavrakakis
        self.assertTrue(client.basic_ack.called)
145 47eeffa9 Christos Stavrakakis
        db_vm = VirtualMachine.objects.get(id=vm.id)
146 47eeffa9 Christos Stavrakakis
        self.assertEqual(db_vm.operstate, 'DESTROYED')
147 47eeffa9 Christos Stavrakakis
        self.assertTrue(db_vm.deleted)
148 47eeffa9 Christos Stavrakakis
        # Check that nics are deleted
149 47eeffa9 Christos Stavrakakis
        self.assertFalse(db_vm.nics.all())
150 72dea98f Christos Stavrakakis
        self.assertTrue(nic.network.get_pool().is_available(nic.ipv4))
151 72dea98f Christos Stavrakakis
        vm2 = mfactory.VirtualMachineFactory()
152 0c50c760 Christos Stavrakakis
        network = mfactory.NetworkFactory(floating_ip_pool=True)
153 72dea98f Christos Stavrakakis
        fp1 = mfactory.FloatingIPFactory(machine=vm2, network=network)
154 72dea98f Christos Stavrakakis
        fp2 = mfactory.FloatingIPFactory(machine=vm2, network=network)
155 72dea98f Christos Stavrakakis
        mfactory.NetworkInterfaceFactory(machine=vm2, network=network,
156 5962d9a6 Christos Stavrakakis
                                         ipv4=fp1.ipv4)
157 72dea98f Christos Stavrakakis
        mfactory.NetworkInterfaceFactory(machine=vm2, network=network,
158 5962d9a6 Christos Stavrakakis
                                         ipv4=fp2.ipv4)
159 72dea98f Christos Stavrakakis
        pool = network.get_pool()
160 72dea98f Christos Stavrakakis
        pool.reserve(fp1.ipv4)
161 72dea98f Christos Stavrakakis
        pool.reserve(fp2.ipv4)
162 72dea98f Christos Stavrakakis
        pool.save()
163 72dea98f Christos Stavrakakis
        msg = self.create_msg(operation='OP_INSTANCE_REMOVE',
164 72dea98f Christos Stavrakakis
                              instance=vm2.backend_vm_id)
165 72dea98f Christos Stavrakakis
        with mocked_quotaholder():
166 72dea98f Christos Stavrakakis
            update_db(client, msg)
167 72dea98f Christos Stavrakakis
        client.basic_ack.assert_called_once()
168 72dea98f Christos Stavrakakis
        db_vm = VirtualMachine.objects.get(id=vm.id)
169 72dea98f Christos Stavrakakis
        self.assertEqual(db_vm.operstate, 'DESTROYED')
170 72dea98f Christos Stavrakakis
        self.assertTrue(db_vm.deleted)
171 72dea98f Christos Stavrakakis
        self.assertEqual(FloatingIP.objects.get(id=fp1.id).machine, None)
172 72dea98f Christos Stavrakakis
        self.assertEqual(FloatingIP.objects.get(id=fp2.id).machine, None)
173 72dea98f Christos Stavrakakis
        pool = network.get_pool()
174 72dea98f Christos Stavrakakis
        # Test that floating ips are not released
175 72dea98f Christos Stavrakakis
        self.assertFalse(pool.is_available(fp1.ipv4))
176 72dea98f Christos Stavrakakis
        self.assertFalse(pool.is_available(fp2.ipv4))
177 47eeffa9 Christos Stavrakakis
178 47eeffa9 Christos Stavrakakis
    def test_create(self, client):
179 47eeffa9 Christos Stavrakakis
        vm = mfactory.VirtualMachineFactory()
180 47eeffa9 Christos Stavrakakis
        msg = self.create_msg(operation='OP_INSTANCE_CREATE',
181 47eeffa9 Christos Stavrakakis
                              instance=vm.backend_vm_id)
182 47eeffa9 Christos Stavrakakis
        update_db(client, msg)
183 10d9cd27 Christos Stavrakakis
        self.assertTrue(client.basic_ack.called)
184 47eeffa9 Christos Stavrakakis
        db_vm = VirtualMachine.objects.get(id=vm.id)
185 47eeffa9 Christos Stavrakakis
        self.assertEqual(db_vm.operstate, 'STARTED')
186 47eeffa9 Christos Stavrakakis
187 47eeffa9 Christos Stavrakakis
    def test_create_error(self, client):
188 47eeffa9 Christos Stavrakakis
        """Test that error create sets vm to ERROR state"""
189 47eeffa9 Christos Stavrakakis
        vm = mfactory.VirtualMachineFactory()
190 47eeffa9 Christos Stavrakakis
        msg = self.create_msg(operation='OP_INSTANCE_CREATE',
191 47eeffa9 Christos Stavrakakis
                              instance=vm.backend_vm_id,
192 47eeffa9 Christos Stavrakakis
                              status='error')
193 47eeffa9 Christos Stavrakakis
        update_db(client, msg)
194 10d9cd27 Christos Stavrakakis
        self.assertTrue(client.basic_ack.called)
195 47eeffa9 Christos Stavrakakis
        db_vm = VirtualMachine.objects.get(id=vm.id)
196 47eeffa9 Christos Stavrakakis
        self.assertEqual(db_vm.operstate, 'ERROR')
197 47eeffa9 Christos Stavrakakis
198 47eeffa9 Christos Stavrakakis
    def test_remove_from_error(self, client):
199 47eeffa9 Christos Stavrakakis
        """Test that error removes delete error builds"""
200 47eeffa9 Christos Stavrakakis
        vm = mfactory.VirtualMachineFactory(operstate='ERROR')
201 47eeffa9 Christos Stavrakakis
        # Also create a NIC
202 47eeffa9 Christos Stavrakakis
        mfactory.NetworkInterfaceFactory(machine=vm)
203 47eeffa9 Christos Stavrakakis
        msg = self.create_msg(operation='OP_INSTANCE_REMOVE',
204 47eeffa9 Christos Stavrakakis
                              instance=vm.backend_vm_id)
205 184a2a8c Christos Stavrakakis
        with mocked_quotaholder():
206 184a2a8c Christos Stavrakakis
            update_db(client, msg)
207 10d9cd27 Christos Stavrakakis
        self.assertTrue(client.basic_ack.called)
208 47eeffa9 Christos Stavrakakis
        db_vm = VirtualMachine.objects.get(id=vm.id)
209 47eeffa9 Christos Stavrakakis
        self.assertEqual(db_vm.operstate, 'DESTROYED')
210 47eeffa9 Christos Stavrakakis
        self.assertTrue(db_vm.deleted)
211 47eeffa9 Christos Stavrakakis
        # Check that nics are deleted
212 47eeffa9 Christos Stavrakakis
        self.assertFalse(db_vm.nics.all())
213 47eeffa9 Christos Stavrakakis
214 47eeffa9 Christos Stavrakakis
    def test_other_error(self, client):
215 47eeffa9 Christos Stavrakakis
        """Test that other error messages do no affect the VM"""
216 47eeffa9 Christos Stavrakakis
        vm = mfactory.VirtualMachineFactory()
217 47eeffa9 Christos Stavrakakis
        msg = self.create_msg(operation='OP_INSTANCE_STARTUP',
218 47eeffa9 Christos Stavrakakis
                              instance=vm.backend_vm_id,
219 47eeffa9 Christos Stavrakakis
                              status='error')
220 47eeffa9 Christos Stavrakakis
        update_db(client, msg)
221 10d9cd27 Christos Stavrakakis
        self.assertTrue(client.basic_ack.called)
222 47eeffa9 Christos Stavrakakis
        db_vm = VirtualMachine.objects.get(id=vm.id)
223 47eeffa9 Christos Stavrakakis
        self.assertEqual(db_vm.operstate, vm.operstate)
224 47eeffa9 Christos Stavrakakis
        self.assertEqual(db_vm.backendtime, vm.backendtime)
225 47eeffa9 Christos Stavrakakis
226 ca4d59e3 Christos Stavrakakis
    def test_resize_msg(self, client):
227 ca4d59e3 Christos Stavrakakis
        vm = mfactory.VirtualMachineFactory()
228 ca4d59e3 Christos Stavrakakis
        # Test empty beparams
229 ca4d59e3 Christos Stavrakakis
        for status in ["success", "error"]:
230 ca4d59e3 Christos Stavrakakis
            msg = self.create_msg(operation='OP_INSTANCE_SET_PARAMS',
231 ca4d59e3 Christos Stavrakakis
                                  instance=vm.backend_vm_id,
232 ca4d59e3 Christos Stavrakakis
                                  beparams={},
233 ca4d59e3 Christos Stavrakakis
                                  status=status)
234 ca4d59e3 Christos Stavrakakis
            client.reset_mock()
235 41a7fae7 Christos Stavrakakis
            with mocked_quotaholder():
236 41a7fae7 Christos Stavrakakis
                update_db(client, msg)
237 ca4d59e3 Christos Stavrakakis
            self.assertTrue(client.basic_ack.called)
238 ca4d59e3 Christos Stavrakakis
            db_vm = VirtualMachine.objects.get(id=vm.id)
239 ca4d59e3 Christos Stavrakakis
            self.assertEqual(db_vm.operstate, vm.operstate)
240 ca4d59e3 Christos Stavrakakis
        # Test intermediate states
241 2cd55da6 Christos Stavrakakis
        vm.operstate = "STOPPED"
242 2cd55da6 Christos Stavrakakis
        vm.save()
243 ca4d59e3 Christos Stavrakakis
        for status in ["queued", "waiting", "running"]:
244 ca4d59e3 Christos Stavrakakis
            msg = self.create_msg(operation='OP_INSTANCE_SET_PARAMS',
245 ca4d59e3 Christos Stavrakakis
                                  instance=vm.backend_vm_id,
246 ca4d59e3 Christos Stavrakakis
                                  beparams={"vcpus": 4, "minmem": 2048,
247 ca4d59e3 Christos Stavrakakis
                                            "maxmem": 2048},
248 ca4d59e3 Christos Stavrakakis
                                  status=status)
249 ca4d59e3 Christos Stavrakakis
            client.reset_mock()
250 ca4d59e3 Christos Stavrakakis
            update_db(client, msg)
251 ca4d59e3 Christos Stavrakakis
            self.assertTrue(client.basic_ack.called)
252 ca4d59e3 Christos Stavrakakis
            db_vm = VirtualMachine.objects.get(id=vm.id)
253 41a7fae7 Christos Stavrakakis
            self.assertEqual(db_vm.operstate, "STOPPED")
254 ca4d59e3 Christos Stavrakakis
        # Test operstate after error
255 ca4d59e3 Christos Stavrakakis
        msg = self.create_msg(operation='OP_INSTANCE_SET_PARAMS',
256 ca4d59e3 Christos Stavrakakis
                              instance=vm.backend_vm_id,
257 ca4d59e3 Christos Stavrakakis
                              beparams={"vcpus": 4},
258 ca4d59e3 Christos Stavrakakis
                              status="error")
259 ca4d59e3 Christos Stavrakakis
        client.reset_mock()
260 41a7fae7 Christos Stavrakakis
        with mocked_quotaholder():
261 41a7fae7 Christos Stavrakakis
            update_db(client, msg)
262 ca4d59e3 Christos Stavrakakis
        self.assertTrue(client.basic_ack.called)
263 ca4d59e3 Christos Stavrakakis
        db_vm = VirtualMachine.objects.get(id=vm.id)
264 ca4d59e3 Christos Stavrakakis
        self.assertEqual(db_vm.operstate, "STOPPED")
265 ca4d59e3 Christos Stavrakakis
        # Test success
266 ca4d59e3 Christos Stavrakakis
        f1 = mfactory.FlavorFactory(cpu=4, ram=1024, disk_template="drbd",
267 ca4d59e3 Christos Stavrakakis
                                    disk=1024)
268 ca4d59e3 Christos Stavrakakis
        vm.flavor = f1
269 ca4d59e3 Christos Stavrakakis
        vm.save()
270 ca4d59e3 Christos Stavrakakis
        f2 = mfactory.FlavorFactory(cpu=8, ram=2048, disk_template="drbd",
271 ca4d59e3 Christos Stavrakakis
                                    disk=1024)
272 ca4d59e3 Christos Stavrakakis
        msg = self.create_msg(operation='OP_INSTANCE_SET_PARAMS',
273 ca4d59e3 Christos Stavrakakis
                              instance=vm.backend_vm_id,
274 ca4d59e3 Christos Stavrakakis
                              beparams={"vcpus": 8, "minmem": 2048,
275 ca4d59e3 Christos Stavrakakis
                                        "maxmem": 2048},
276 ca4d59e3 Christos Stavrakakis
                              status="success")
277 ca4d59e3 Christos Stavrakakis
        client.reset_mock()
278 41a7fae7 Christos Stavrakakis
        with mocked_quotaholder():
279 41a7fae7 Christos Stavrakakis
            update_db(client, msg)
280 ca4d59e3 Christos Stavrakakis
        self.assertTrue(client.basic_ack.called)
281 ca4d59e3 Christos Stavrakakis
        db_vm = VirtualMachine.objects.get(id=vm.id)
282 ca4d59e3 Christos Stavrakakis
        self.assertEqual(db_vm.operstate, "STOPPED")
283 ca4d59e3 Christos Stavrakakis
        self.assertEqual(db_vm.flavor, f2)
284 ca4d59e3 Christos Stavrakakis
        msg = self.create_msg(operation='OP_INSTANCE_SET_PARAMS',
285 ca4d59e3 Christos Stavrakakis
                              instance=vm.backend_vm_id,
286 ca4d59e3 Christos Stavrakakis
                              beparams={"vcpus": 100, "minmem": 2048,
287 ca4d59e3 Christos Stavrakakis
                                        "maxmem": 2048},
288 ca4d59e3 Christos Stavrakakis
                              status="success")
289 ca4d59e3 Christos Stavrakakis
        client.reset_mock()
290 41a7fae7 Christos Stavrakakis
        with mocked_quotaholder():
291 41a7fae7 Christos Stavrakakis
            update_db(client, msg)
292 ca4d59e3 Christos Stavrakakis
        self.assertTrue(client.basic_reject.called)
293 ca4d59e3 Christos Stavrakakis
294 47eeffa9 Christos Stavrakakis
295 47eeffa9 Christos Stavrakakis
@patch('synnefo.lib.amqp.AMQPClient')
296 47eeffa9 Christos Stavrakakis
class UpdateNetTest(TestCase):
297 47eeffa9 Christos Stavrakakis
    def create_msg(self, **kwargs):
298 47eeffa9 Christos Stavrakakis
        """Create snf-ganeti-hook message"""
299 47eeffa9 Christos Stavrakakis
        msg = {'event_time': split_time(time())}
300 fd95834e Christos Stavrakakis
        msg['type'] = 'ganeti-op-status'
301 fd95834e Christos Stavrakakis
        msg['operation'] = 'OP_INSTANCE_SET_PARAMS'
302 47eeffa9 Christos Stavrakakis
        msg['status'] = 'success'
303 47eeffa9 Christos Stavrakakis
        msg['jobId'] = 1
304 47eeffa9 Christos Stavrakakis
        msg['logmsg'] = 'Dummy Log'
305 47eeffa9 Christos Stavrakakis
        for key, val in kwargs.items():
306 47eeffa9 Christos Stavrakakis
            msg[key] = val
307 47eeffa9 Christos Stavrakakis
        message = {'body': json.dumps(msg)}
308 47eeffa9 Christos Stavrakakis
        return message
309 47eeffa9 Christos Stavrakakis
310 47eeffa9 Christos Stavrakakis
    def test_missing_attribute(self, client):
311 fd95834e Christos Stavrakakis
        update_db(client, json.dumps({'body': {}}))
312 10d9cd27 Christos Stavrakakis
        self.assertTrue(client.basic_reject.called)
313 47eeffa9 Christos Stavrakakis
314 47eeffa9 Christos Stavrakakis
    def test_unhandled_exception(self, client):
315 fd95834e Christos Stavrakakis
        update_db(client, {})
316 47eeffa9 Christos Stavrakakis
        client.basic_reject.assert_called_once()
317 47eeffa9 Christos Stavrakakis
318 47eeffa9 Christos Stavrakakis
    def test_wrong_type(self, client):
319 47eeffa9 Christos Stavrakakis
        msg = self.create_msg(type="WRONG_TYPE")
320 fd95834e Christos Stavrakakis
        update_db(client, msg)
321 10d9cd27 Christos Stavrakakis
        self.assertTrue(client.basic_nack.called)
322 47eeffa9 Christos Stavrakakis
323 47eeffa9 Christos Stavrakakis
    def test_missing_instance(self, client):
324 47eeffa9 Christos Stavrakakis
        msg = self.create_msg(operation='OP_INSTANCE_STARTUP',
325 47eeffa9 Christos Stavrakakis
                              instance='foo')
326 fd95834e Christos Stavrakakis
        update_db(client, msg)
327 10d9cd27 Christos Stavrakakis
        self.assertTrue(client.basic_ack.called)
328 47eeffa9 Christos Stavrakakis
329 47eeffa9 Christos Stavrakakis
    def test_no_nics(self, client):
330 47eeffa9 Christos Stavrakakis
        vm = mfactory.VirtualMachineFactory(operstate='ERROR')
331 47eeffa9 Christos Stavrakakis
        mfactory.NetworkInterfaceFactory(machine=vm)
332 47eeffa9 Christos Stavrakakis
        mfactory.NetworkInterfaceFactory(machine=vm)
333 47eeffa9 Christos Stavrakakis
        mfactory.NetworkInterfaceFactory(machine=vm)
334 47eeffa9 Christos Stavrakakis
        self.assertEqual(len(vm.nics.all()), 3)
335 47eeffa9 Christos Stavrakakis
        msg = self.create_msg(nics=[],
336 47eeffa9 Christos Stavrakakis
                              instance=vm.backend_vm_id)
337 fd95834e Christos Stavrakakis
        update_db(client, msg)
338 10d9cd27 Christos Stavrakakis
        self.assertTrue(client.basic_ack.called)
339 47eeffa9 Christos Stavrakakis
        db_vm = VirtualMachine.objects.get(id=vm.id)
340 47eeffa9 Christos Stavrakakis
        self.assertEqual(len(db_vm.nics.all()), 0)
341 47eeffa9 Christos Stavrakakis
342 47eeffa9 Christos Stavrakakis
    def test_empty_nic(self, client):
343 47eeffa9 Christos Stavrakakis
        vm = mfactory.VirtualMachineFactory(operstate='ERROR')
344 47eeffa9 Christos Stavrakakis
        for public in [True, False]:
345 5962d9a6 Christos Stavrakakis
            net = mfactory.NetworkFactory(public=public, subnet6=None)
346 cc92b70f Christos Stavrakakis
            msg = self.create_msg(nics=[{'network': net.backend_id}],
347 47eeffa9 Christos Stavrakakis
                                  instance=vm.backend_vm_id)
348 fd95834e Christos Stavrakakis
            update_db(client, msg)
349 10d9cd27 Christos Stavrakakis
            self.assertTrue(client.basic_ack.called)
350 47eeffa9 Christos Stavrakakis
            db_vm = VirtualMachine.objects.get(id=vm.id)
351 47eeffa9 Christos Stavrakakis
            nics = db_vm.nics.all()
352 47eeffa9 Christos Stavrakakis
            self.assertEqual(len(nics), 1)
353 47eeffa9 Christos Stavrakakis
            self.assertEqual(nics[0].index, 0)
354 8d325d4b Christos Stavrakakis
            self.assertEqual(nics[0].ipv4, None)
355 8d325d4b Christos Stavrakakis
            self.assertEqual(nics[0].ipv6, None)
356 8d325d4b Christos Stavrakakis
            self.assertEqual(nics[0].mac, None)
357 47eeffa9 Christos Stavrakakis
            if public:
358 47eeffa9 Christos Stavrakakis
                self.assertEqual(nics[0].firewall_profile,
359 47eeffa9 Christos Stavrakakis
                                 settings.DEFAULT_FIREWALL_PROFILE)
360 47eeffa9 Christos Stavrakakis
            else:
361 8d325d4b Christos Stavrakakis
                self.assertEqual(nics[0].firewall_profile, None)
362 47eeffa9 Christos Stavrakakis
363 47eeffa9 Christos Stavrakakis
    def test_full_nic(self, client):
364 47eeffa9 Christos Stavrakakis
        vm = mfactory.VirtualMachineFactory(operstate='ERROR')
365 5962d9a6 Christos Stavrakakis
        net = mfactory.NetworkFactory(subnet='10.0.0.0/24', subnet6=None)
366 47eeffa9 Christos Stavrakakis
        pool = net.get_pool()
367 47eeffa9 Christos Stavrakakis
        self.assertTrue(pool.is_available('10.0.0.22'))
368 47eeffa9 Christos Stavrakakis
        pool.save()
369 cc92b70f Christos Stavrakakis
        msg = self.create_msg(nics=[{'network': net.backend_id,
370 47eeffa9 Christos Stavrakakis
                                     'ip': '10.0.0.22',
371 47eeffa9 Christos Stavrakakis
                                     'mac': 'aa:bb:cc:00:11:22'}],
372 47eeffa9 Christos Stavrakakis
                              instance=vm.backend_vm_id)
373 fd95834e Christos Stavrakakis
        update_db(client, msg)
374 10d9cd27 Christos Stavrakakis
        self.assertTrue(client.basic_ack.called)
375 47eeffa9 Christos Stavrakakis
        db_vm = VirtualMachine.objects.get(id=vm.id)
376 47eeffa9 Christos Stavrakakis
        nics = db_vm.nics.all()
377 47eeffa9 Christos Stavrakakis
        self.assertEqual(len(nics), 1)
378 47eeffa9 Christos Stavrakakis
        self.assertEqual(nics[0].index, 0)
379 47eeffa9 Christos Stavrakakis
        self.assertEqual(nics[0].ipv4, '10.0.0.22')
380 8d325d4b Christos Stavrakakis
        self.assertEqual(nics[0].ipv6, None)
381 47eeffa9 Christos Stavrakakis
        self.assertEqual(nics[0].mac, 'aa:bb:cc:00:11:22')
382 47eeffa9 Christos Stavrakakis
        pool = net.get_pool()
383 47eeffa9 Christos Stavrakakis
        self.assertFalse(pool.is_available('10.0.0.22'))
384 47eeffa9 Christos Stavrakakis
        pool.save()
385 47eeffa9 Christos Stavrakakis
386 47eeffa9 Christos Stavrakakis
387 47eeffa9 Christos Stavrakakis
@patch('synnefo.lib.amqp.AMQPClient')
388 47eeffa9 Christos Stavrakakis
class UpdateNetworkTest(TestCase):
389 47eeffa9 Christos Stavrakakis
    def create_msg(self, **kwargs):
390 47eeffa9 Christos Stavrakakis
        """Create snf-ganeti-eventd message"""
391 47eeffa9 Christos Stavrakakis
        msg = {'event_time': split_time(time())}
392 47eeffa9 Christos Stavrakakis
        msg['type'] = 'ganeti-network-status'
393 47eeffa9 Christos Stavrakakis
        msg['status'] = 'success'
394 47eeffa9 Christos Stavrakakis
        msg['jobId'] = 1
395 47eeffa9 Christos Stavrakakis
        msg['logmsg'] = 'Dummy Log'
396 47eeffa9 Christos Stavrakakis
        for key, val in kwargs.items():
397 47eeffa9 Christos Stavrakakis
            msg[key] = val
398 47eeffa9 Christos Stavrakakis
        message = {'body': json.dumps(msg)}
399 47eeffa9 Christos Stavrakakis
        return message
400 47eeffa9 Christos Stavrakakis
401 47eeffa9 Christos Stavrakakis
    def test_missing_attribute(self, client):
402 47eeffa9 Christos Stavrakakis
        update_network(client, json.dumps({'body': {}}))
403 10d9cd27 Christos Stavrakakis
        self.assertTrue(client.basic_reject.called)
404 47eeffa9 Christos Stavrakakis
405 47eeffa9 Christos Stavrakakis
    def test_unhandled_exception(self, client):
406 47eeffa9 Christos Stavrakakis
        update_network(client, {})
407 47eeffa9 Christos Stavrakakis
        client.basic_reject.assert_called_once()
408 47eeffa9 Christos Stavrakakis
409 47eeffa9 Christos Stavrakakis
    def test_wrong_type(self, client):
410 47eeffa9 Christos Stavrakakis
        msg = self.create_msg(type="WRONG_TYPE")
411 47eeffa9 Christos Stavrakakis
        update_network(client, msg)
412 10d9cd27 Christos Stavrakakis
        self.assertTrue(client.basic_nack.called)
413 47eeffa9 Christos Stavrakakis
414 47eeffa9 Christos Stavrakakis
    def test_missing_network(self, client):
415 47eeffa9 Christos Stavrakakis
        msg = self.create_msg(operation='OP_NETWORK_CREATE',
416 47eeffa9 Christos Stavrakakis
                              network='foo')
417 47eeffa9 Christos Stavrakakis
        update_network(client, msg)
418 10d9cd27 Christos Stavrakakis
        self.assertTrue(client.basic_ack.called)
419 47eeffa9 Christos Stavrakakis
420 47eeffa9 Christos Stavrakakis
    def test_create(self, client):
421 47eeffa9 Christos Stavrakakis
        back_network = mfactory.BackendNetworkFactory(operstate='PENDING')
422 47eeffa9 Christos Stavrakakis
        net = back_network.network
423 1d04ef8b Christos Stavrakakis
        net.state = 'ACTIVE'
424 1d04ef8b Christos Stavrakakis
        net.save()
425 47eeffa9 Christos Stavrakakis
        back1 = back_network.backend
426 47eeffa9 Christos Stavrakakis
427 47eeffa9 Christos Stavrakakis
        back_network2 = mfactory.BackendNetworkFactory(operstate='PENDING',
428 47eeffa9 Christos Stavrakakis
                                                       network=net)
429 47eeffa9 Christos Stavrakakis
        back2 = back_network2.backend
430 47eeffa9 Christos Stavrakakis
        # Message from first backend network
431 47eeffa9 Christos Stavrakakis
        msg = self.create_msg(operation='OP_NETWORK_CONNECT',
432 47eeffa9 Christos Stavrakakis
                              network=net.backend_id,
433 47eeffa9 Christos Stavrakakis
                              cluster=back1.clustername)
434 47eeffa9 Christos Stavrakakis
        update_network(client, msg)
435 10d9cd27 Christos Stavrakakis
        self.assertTrue(client.basic_ack.called)
436 47eeffa9 Christos Stavrakakis
437 47eeffa9 Christos Stavrakakis
        back_net = BackendNetwork.objects.get(id=back_network.id)
438 47eeffa9 Christos Stavrakakis
        self.assertEqual(back_net.operstate, 'ACTIVE')
439 47eeffa9 Christos Stavrakakis
        db_net = Network.objects.get(id=net.id)
440 1d04ef8b Christos Stavrakakis
        self.assertEqual(db_net.state, 'ACTIVE')
441 47eeffa9 Christos Stavrakakis
        # msg from second backend network
442 47eeffa9 Christos Stavrakakis
        msg = self.create_msg(operation='OP_NETWORK_CONNECT',
443 47eeffa9 Christos Stavrakakis
                              network=net.backend_id,
444 47eeffa9 Christos Stavrakakis
                              cluster=back2.clustername)
445 47eeffa9 Christos Stavrakakis
        update_network(client, msg)
446 10d9cd27 Christos Stavrakakis
        self.assertTrue(client.basic_ack.called)
447 47eeffa9 Christos Stavrakakis
448 47eeffa9 Christos Stavrakakis
        db_net = Network.objects.get(id=net.id)
449 47eeffa9 Christos Stavrakakis
        self.assertEqual(db_net.state, 'ACTIVE')
450 47eeffa9 Christos Stavrakakis
        back_net = BackendNetwork.objects.get(id=back_network.id)
451 47eeffa9 Christos Stavrakakis
        self.assertEqual(back_net.operstate, 'ACTIVE')
452 47eeffa9 Christos Stavrakakis
453 039e3e61 Christos Stavrakakis
    def test_create_offline_backend(self, client):
454 039e3e61 Christos Stavrakakis
        """Test network creation when a backend is offline"""
455 1d04ef8b Christos Stavrakakis
        net = mfactory.NetworkFactory(state='ACTIVE')
456 039e3e61 Christos Stavrakakis
        bn1 = mfactory.BackendNetworkFactory(network=net)
457 5962d9a6 Christos Stavrakakis
        mfactory.BackendNetworkFactory(network=net,
458 5962d9a6 Christos Stavrakakis
                                       backend__offline=True)
459 039e3e61 Christos Stavrakakis
        msg = self.create_msg(operation='OP_NETWORK_CONNECT',
460 039e3e61 Christos Stavrakakis
                              network=net.backend_id,
461 039e3e61 Christos Stavrakakis
                              cluster=bn1.backend.clustername)
462 039e3e61 Christos Stavrakakis
        update_network(client, msg)
463 10d9cd27 Christos Stavrakakis
        self.assertTrue(client.basic_ack.called)
464 039e3e61 Christos Stavrakakis
        new_net = Network.objects.get(id=net.id)
465 039e3e61 Christos Stavrakakis
        self.assertEqual(new_net.state, 'ACTIVE')
466 039e3e61 Christos Stavrakakis
467 47eeffa9 Christos Stavrakakis
    def test_disconnect(self, client):
468 47eeffa9 Christos Stavrakakis
        bn1 = mfactory.BackendNetworkFactory(operstate='ACTIVE')
469 47eeffa9 Christos Stavrakakis
        net1 = bn1.network
470 1d04ef8b Christos Stavrakakis
        net1.state = "ACTIVE"
471 1d04ef8b Christos Stavrakakis
        net1.state = 'ACTIVE'
472 47eeffa9 Christos Stavrakakis
        net1.save()
473 47eeffa9 Christos Stavrakakis
        bn2 = mfactory.BackendNetworkFactory(operstate='ACTIVE',
474 47eeffa9 Christos Stavrakakis
                                             network=net1)
475 47eeffa9 Christos Stavrakakis
        msg = self.create_msg(operation='OP_NETWORK_DISCONNECT',
476 47eeffa9 Christos Stavrakakis
                              network=net1.backend_id,
477 47eeffa9 Christos Stavrakakis
                              cluster=bn2.backend.clustername)
478 47eeffa9 Christos Stavrakakis
        update_network(client, msg)
479 10d9cd27 Christos Stavrakakis
        self.assertTrue(client.basic_ack.called)
480 1d04ef8b Christos Stavrakakis
        self.assertEqual(Network.objects.get(id=net1.id).state, 'ACTIVE')
481 47eeffa9 Christos Stavrakakis
        self.assertEqual(BackendNetwork.objects.get(id=bn2.id).operstate,
482 cc92b70f Christos Stavrakakis
                         'PENDING')
483 47eeffa9 Christos Stavrakakis
484 47eeffa9 Christos Stavrakakis
    def test_remove(self, client):
485 47eeffa9 Christos Stavrakakis
        mfactory.MacPrefixPoolTableFactory()
486 47eeffa9 Christos Stavrakakis
        mfactory.BridgePoolTableFactory()
487 47eeffa9 Christos Stavrakakis
        bn = mfactory.BackendNetworkFactory(operstate='ACTIVE')
488 47eeffa9 Christos Stavrakakis
        for old_state in ['success', 'canceled', 'error']:
489 47eeffa9 Christos Stavrakakis
            for flavor in Network.FLAVORS.keys():
490 47eeffa9 Christos Stavrakakis
                bn.operstate = old_state
491 47eeffa9 Christos Stavrakakis
                bn.save()
492 47eeffa9 Christos Stavrakakis
                net = bn.network
493 47eeffa9 Christos Stavrakakis
                net.state = 'ACTIVE'
494 47eeffa9 Christos Stavrakakis
                net.flavor = flavor
495 47eeffa9 Christos Stavrakakis
                if flavor == 'PHYSICAL_VLAN':
496 47eeffa9 Christos Stavrakakis
                    net.link = allocate_resource('bridge')
497 47eeffa9 Christos Stavrakakis
                if flavor == 'MAC_FILTERED':
498 47eeffa9 Christos Stavrakakis
                    net.mac_prefix = allocate_resource('mac_prefix')
499 47eeffa9 Christos Stavrakakis
                net.save()
500 47eeffa9 Christos Stavrakakis
                msg = self.create_msg(operation='OP_NETWORK_REMOVE',
501 47eeffa9 Christos Stavrakakis
                                      network=net.backend_id,
502 47eeffa9 Christos Stavrakakis
                                      cluster=bn.backend.clustername)
503 184a2a8c Christos Stavrakakis
                with mocked_quotaholder():
504 184a2a8c Christos Stavrakakis
                    update_network(client, msg)
505 10d9cd27 Christos Stavrakakis
                self.assertTrue(client.basic_ack.called)
506 47eeffa9 Christos Stavrakakis
                db_bnet = BackendNetwork.objects.get(id=bn.id)
507 47eeffa9 Christos Stavrakakis
                self.assertEqual(db_bnet.operstate,
508 cc92b70f Christos Stavrakakis
                                 'DELETED')
509 47eeffa9 Christos Stavrakakis
                db_net = Network.objects.get(id=net.id)
510 47eeffa9 Christos Stavrakakis
                self.assertEqual(db_net.state, 'DELETED', flavor)
511 47eeffa9 Christos Stavrakakis
                self.assertTrue(db_net.deleted)
512 47eeffa9 Christos Stavrakakis
                if flavor == 'PHYSICAL_VLAN':
513 47eeffa9 Christos Stavrakakis
                    pool = BridgePoolTable.get_pool()
514 47eeffa9 Christos Stavrakakis
                    self.assertTrue(pool.is_available(net.link))
515 47eeffa9 Christos Stavrakakis
                if flavor == 'MAC_FILTERED':
516 47eeffa9 Christos Stavrakakis
                    pool = MacPrefixPoolTable.get_pool()
517 47eeffa9 Christos Stavrakakis
                    self.assertTrue(pool.is_available(net.mac_prefix))
518 47eeffa9 Christos Stavrakakis
519 039e3e61 Christos Stavrakakis
    def test_remove_offline_backend(self, client):
520 039e3e61 Christos Stavrakakis
        """Test network removing when a backend is offline"""
521 3f77e63d Christos Stavrakakis
        mfactory.BridgePoolTableFactory()
522 3f77e63d Christos Stavrakakis
        net = mfactory.NetworkFactory(flavor='PHYSICAL_VLAN',
523 3f77e63d Christos Stavrakakis
                                      state='ACTIVE',
524 3f77e63d Christos Stavrakakis
                                      link='prv12')
525 039e3e61 Christos Stavrakakis
        bn1 = mfactory.BackendNetworkFactory(network=net)
526 039e3e61 Christos Stavrakakis
        mfactory.BackendNetworkFactory(network=net,
527 c6c889f7 Christos Stavrakakis
                                       operstate="ACTIVE",
528 3f77e63d Christos Stavrakakis
                                       backend__offline=True)
529 039e3e61 Christos Stavrakakis
        msg = self.create_msg(operation='OP_NETWORK_REMOVE',
530 039e3e61 Christos Stavrakakis
                              network=net.backend_id,
531 039e3e61 Christos Stavrakakis
                              cluster=bn1.backend.clustername)
532 184a2a8c Christos Stavrakakis
        with mocked_quotaholder():
533 184a2a8c Christos Stavrakakis
            update_network(client, msg)
534 10d9cd27 Christos Stavrakakis
        self.assertTrue(client.basic_ack.called)
535 039e3e61 Christos Stavrakakis
        new_net = Network.objects.get(id=net.id)
536 1d04ef8b Christos Stavrakakis
        self.assertEqual(new_net.state, 'ACTIVE')
537 1d04ef8b Christos Stavrakakis
        self.assertFalse(new_net.deleted)
538 039e3e61 Christos Stavrakakis
539 47eeffa9 Christos Stavrakakis
    def test_error_opcode(self, client):
540 c6c889f7 Christos Stavrakakis
        mfactory.MacPrefixPoolTableFactory()
541 c6c889f7 Christos Stavrakakis
        mfactory.BridgePoolTableFactory()
542 47eeffa9 Christos Stavrakakis
        for state, _ in Network.OPER_STATES:
543 c6c889f7 Christos Stavrakakis
            bn = mfactory.BackendNetworkFactory(operstate="ACTIVE")
544 47eeffa9 Christos Stavrakakis
            bn.operstate = state
545 47eeffa9 Christos Stavrakakis
            bn.save()
546 47eeffa9 Christos Stavrakakis
            network = bn.network
547 47eeffa9 Christos Stavrakakis
            network.state = state
548 47eeffa9 Christos Stavrakakis
            network.save()
549 47eeffa9 Christos Stavrakakis
            for opcode, _ in BackendNetwork.BACKEND_OPCODES:
550 47eeffa9 Christos Stavrakakis
                if opcode in ['OP_NETWORK_REMOVE', 'OP_NETWORK_ADD']:
551 47eeffa9 Christos Stavrakakis
                    continue
552 47eeffa9 Christos Stavrakakis
                msg = self.create_msg(operation=opcode,
553 47eeffa9 Christos Stavrakakis
                                      network=bn.network.backend_id,
554 47eeffa9 Christos Stavrakakis
                                      status='error',
555 c6c889f7 Christos Stavrakakis
                                      add_reserved_ips=[],
556 c6c889f7 Christos Stavrakakis
                                      remove_reserved_ips=[],
557 47eeffa9 Christos Stavrakakis
                                      cluster=bn.backend.clustername)
558 c6c889f7 Christos Stavrakakis
                with mocked_quotaholder():
559 c6c889f7 Christos Stavrakakis
                    update_network(client, msg)
560 10d9cd27 Christos Stavrakakis
                self.assertTrue(client.basic_ack.called)
561 47eeffa9 Christos Stavrakakis
                db_bnet = BackendNetwork.objects.get(id=bn.id)
562 47eeffa9 Christos Stavrakakis
                self.assertEqual(bn.operstate, db_bnet.operstate)
563 47eeffa9 Christos Stavrakakis
                self.assertEqual(bn.network.state, db_bnet.network.state)
564 47eeffa9 Christos Stavrakakis
565 47eeffa9 Christos Stavrakakis
    def test_ips(self, client):
566 47eeffa9 Christos Stavrakakis
        network = mfactory.NetworkFactory(subnet='10.0.0.0/24')
567 47eeffa9 Christos Stavrakakis
        bn = mfactory.BackendNetworkFactory(network=network)
568 47eeffa9 Christos Stavrakakis
        msg = self.create_msg(operation='OP_NETWORK_SET_PARAMS',
569 47eeffa9 Christos Stavrakakis
                              network=network.backend_id,
570 47eeffa9 Christos Stavrakakis
                              cluster=bn.backend.clustername,
571 47eeffa9 Christos Stavrakakis
                              status='success',
572 47eeffa9 Christos Stavrakakis
                              add_reserved_ips=['10.0.0.10', '10.0.0.20'],
573 47eeffa9 Christos Stavrakakis
                              remove_reserved_ips=[])
574 47eeffa9 Christos Stavrakakis
        update_network(client, msg)
575 10d9cd27 Christos Stavrakakis
        self.assertTrue(client.basic_ack.called)
576 47eeffa9 Christos Stavrakakis
        pool = network.get_pool()
577 47eeffa9 Christos Stavrakakis
        self.assertTrue(pool.is_reserved('10.0.0.10'))
578 47eeffa9 Christos Stavrakakis
        self.assertTrue(pool.is_reserved('10.0.0.20'))
579 47eeffa9 Christos Stavrakakis
        pool.save()
580 47eeffa9 Christos Stavrakakis
        # Release them
581 47eeffa9 Christos Stavrakakis
        msg = self.create_msg(operation='OP_NETWORK_SET_PARAMS',
582 47eeffa9 Christos Stavrakakis
                              network=network.backend_id,
583 47eeffa9 Christos Stavrakakis
                              cluster=bn.backend.clustername,
584 47eeffa9 Christos Stavrakakis
                              add_reserved_ips=[],
585 47eeffa9 Christos Stavrakakis
                              remove_reserved_ips=['10.0.0.10', '10.0.0.20'])
586 47eeffa9 Christos Stavrakakis
        update_network(client, msg)
587 10d9cd27 Christos Stavrakakis
        self.assertTrue(client.basic_ack.called)
588 47eeffa9 Christos Stavrakakis
        pool = network.get_pool()
589 47eeffa9 Christos Stavrakakis
        self.assertFalse(pool.is_reserved('10.0.0.10'))
590 47eeffa9 Christos Stavrakakis
        self.assertFalse(pool.is_reserved('10.0.0.20'))
591 95aee02c Vangelis Koukis
592 92c53da1 Vassilios Karakoidas
593 c8908e51 Christos Stavrakakis
@patch('synnefo.lib.amqp.AMQPClient')
594 c8908e51 Christos Stavrakakis
class UpdateBuildProgressTest(TestCase):
595 c8908e51 Christos Stavrakakis
    def setUp(self):
596 c8908e51 Christos Stavrakakis
        self.vm = mfactory.VirtualMachineFactory()
597 95aee02c Vangelis Koukis
598 c8908e51 Christos Stavrakakis
    def get_db_vm(self):
599 c8908e51 Christos Stavrakakis
        return VirtualMachine.objects.get(id=self.vm.id)
600 95aee02c Vangelis Koukis
601 c8908e51 Christos Stavrakakis
    def create_msg(self, **kwargs):
602 c8908e51 Christos Stavrakakis
        """Create snf-progress-monitor message"""
603 c8908e51 Christos Stavrakakis
        msg = {'event_time': split_time(time())}
604 c8908e51 Christos Stavrakakis
        msg['type'] = 'image-copy-progress'
605 c8908e51 Christos Stavrakakis
        msg['progress'] = 0
606 c8908e51 Christos Stavrakakis
        for key, val in kwargs.items():
607 c8908e51 Christos Stavrakakis
            msg[key] = val
608 c8908e51 Christos Stavrakakis
        message = {'body': json.dumps(msg)}
609 c8908e51 Christos Stavrakakis
        return message
610 95aee02c Vangelis Koukis
611 c8908e51 Christos Stavrakakis
    def test_missing_attribute(self, client):
612 c8908e51 Christos Stavrakakis
        update_build_progress(client, json.dumps({'body': {}}))
613 10d9cd27 Christos Stavrakakis
        self.assertTrue(client.basic_reject.called)
614 95aee02c Vangelis Koukis
615 c8908e51 Christos Stavrakakis
    def test_unhandled_exception(self, client):
616 c8908e51 Christos Stavrakakis
        update_build_progress(client, {})
617 c8908e51 Christos Stavrakakis
        client.basic_reject.assert_called_once()
618 c25cc9ec Vangelis Koukis
619 c8908e51 Christos Stavrakakis
    def test_missing_instance(self, client):
620 c8908e51 Christos Stavrakakis
        msg = self.create_msg(instance='foo')
621 c8908e51 Christos Stavrakakis
        update_build_progress(client, msg)
622 10d9cd27 Christos Stavrakakis
        self.assertTrue(client.basic_ack.called)
623 9068cd85 Georgios Gousios
624 c8908e51 Christos Stavrakakis
    def test_wrong_type(self, client):
625 c8908e51 Christos Stavrakakis
        msg = self.create_msg(type="WRONG_TYPE")
626 c8908e51 Christos Stavrakakis
        update_build_progress(client, msg)
627 10d9cd27 Christos Stavrakakis
        self.assertTrue(client.basic_nack.called)
628 9068cd85 Georgios Gousios
629 c8908e51 Christos Stavrakakis
    def test_progress_update(self, client):
630 c25cc9ec Vangelis Koukis
        rprogress = randint(10, 100)
631 c8908e51 Christos Stavrakakis
        msg = self.create_msg(progress=rprogress,
632 c8908e51 Christos Stavrakakis
                              instance=self.vm.backend_vm_id)
633 c8908e51 Christos Stavrakakis
        update_build_progress(client, msg)
634 10d9cd27 Christos Stavrakakis
        self.assertTrue(client.basic_ack.called)
635 c8908e51 Christos Stavrakakis
        vm = self.get_db_vm()
636 c8908e51 Christos Stavrakakis
        self.assertEqual(vm.buildpercentage, rprogress)
637 c8908e51 Christos Stavrakakis
638 c8908e51 Christos Stavrakakis
    def test_invalid_value(self, client):
639 c8908e51 Christos Stavrakakis
        old = self.vm.buildpercentage
640 c8908e51 Christos Stavrakakis
        for rprogress in [0, -1, 'a']:
641 c8908e51 Christos Stavrakakis
            msg = self.create_msg(progress=rprogress,
642 c8908e51 Christos Stavrakakis
                                  instance=self.vm.backend_vm_id)
643 c8908e51 Christos Stavrakakis
            update_build_progress(client, msg)
644 10d9cd27 Christos Stavrakakis
            self.assertTrue(client.basic_ack.called)
645 c8908e51 Christos Stavrakakis
            vm = self.get_db_vm()
646 c8908e51 Christos Stavrakakis
            self.assertEqual(vm.buildpercentage, old)