Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / logic / tests / reconciliation.py @ 6e9255ab

History | View | Annotate | Download (15.7 kB)

1 0208770b Christos Stavrakakis
# vim: set fileencoding=utf-8 :
2 0208770b Christos Stavrakakis
# Copyright 2012 GRNET S.A. All rights reserved.
3 0208770b Christos Stavrakakis
#
4 0208770b Christos Stavrakakis
# Redistribution and use in source and binary forms, with or without
5 0208770b Christos Stavrakakis
# modification, are permitted provided that the following conditions
6 0208770b Christos Stavrakakis
# are met:
7 0208770b Christos Stavrakakis
#
8 0208770b Christos Stavrakakis
#   1. Redistributions of source code must retain the above copyright
9 0208770b Christos Stavrakakis
#      notice, this list of conditions and the following disclaimer.
10 0208770b Christos Stavrakakis
#
11 0208770b Christos Stavrakakis
#  2. Redistributions in binary form must reproduce the above copyright
12 0208770b Christos Stavrakakis
#     notice, this list of conditions and the following disclaimer in the
13 0208770b Christos Stavrakakis
#     documentation and/or other materials provided with the distribution.
14 0208770b Christos Stavrakakis
#
15 0208770b Christos Stavrakakis
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
16 0208770b Christos Stavrakakis
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 0208770b Christos Stavrakakis
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 0208770b Christos Stavrakakis
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19 0208770b Christos Stavrakakis
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 0208770b Christos Stavrakakis
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 0208770b Christos Stavrakakis
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 0208770b Christos Stavrakakis
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 0208770b Christos Stavrakakis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 0208770b Christos Stavrakakis
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 0208770b Christos Stavrakakis
# SUCH DAMAGE.
26 0208770b Christos Stavrakakis
#
27 0208770b Christos Stavrakakis
# The views and conclusions contained in the software and documentation are
28 0208770b Christos Stavrakakis
# those of the authors and should not be interpreted as representing official
29 0208770b Christos Stavrakakis
# policies, either expressed or implied, of GRNET S.A.
30 0208770b Christos Stavrakakis
import logging
31 0208770b Christos Stavrakakis
from django.test import TestCase
32 0208770b Christos Stavrakakis
33 be4d8469 Christos Stavrakakis
from synnefo.db.models import VirtualMachine, Network, BackendNetwork
34 0208770b Christos Stavrakakis
from synnefo.db import models_factory as mfactory
35 0208770b Christos Stavrakakis
from synnefo.logic import reconciliation
36 0208770b Christos Stavrakakis
from datetime import timedelta
37 0208770b Christos Stavrakakis
from mock import patch
38 0208770b Christos Stavrakakis
from snf_django.utils.testing import mocked_quotaholder
39 0208770b Christos Stavrakakis
from time import time
40 0208770b Christos Stavrakakis
from synnefo import settings
41 0208770b Christos Stavrakakis
42 0208770b Christos Stavrakakis
43 0208770b Christos Stavrakakis
@patch("synnefo.logic.rapi_pool.GanetiRapiClient")
44 be4d8469 Christos Stavrakakis
class ServerReconciliationTest(TestCase):
45 0208770b Christos Stavrakakis
    @patch("synnefo.logic.rapi_pool.GanetiRapiClient")
46 0208770b Christos Stavrakakis
    def setUp(self, mrapi):
47 0208770b Christos Stavrakakis
        self.backend = mfactory.BackendFactory()
48 0208770b Christos Stavrakakis
        log = logging.getLogger()
49 0208770b Christos Stavrakakis
        options = {"fix_unsynced": True,
50 0208770b Christos Stavrakakis
                   "fix_stale": True,
51 0208770b Christos Stavrakakis
                   "fix_orphans": True,
52 0208770b Christos Stavrakakis
                   "fix_unsynced_nics": True,
53 0208770b Christos Stavrakakis
                   "fix_unsynced_flavors": True}
54 0208770b Christos Stavrakakis
        self.reconciler = reconciliation.BackendReconciler(self.backend,
55 0208770b Christos Stavrakakis
                                                           options=options,
56 0208770b Christos Stavrakakis
                                                           logger=log)
57 0208770b Christos Stavrakakis
58 0208770b Christos Stavrakakis
    def test_building_vm(self, mrapi):
59 0208770b Christos Stavrakakis
        mrapi = self.reconciler.client
60 0208770b Christos Stavrakakis
        vm1 = mfactory.VirtualMachineFactory(backend=self.backend,
61 0208770b Christos Stavrakakis
                                             backendjobid=None,
62 0208770b Christos Stavrakakis
                                             operstate="BUILD")
63 0208770b Christos Stavrakakis
        self.reconciler.reconcile()
64 0208770b Christos Stavrakakis
        # Assert not deleted
65 0208770b Christos Stavrakakis
        vm1 = VirtualMachine.objects.get(id=vm1.id)
66 0208770b Christos Stavrakakis
        self.assertFalse(vm1.deleted)
67 0208770b Christos Stavrakakis
        self.assertEqual(vm1.operstate, "BUILD")
68 0208770b Christos Stavrakakis
69 0208770b Christos Stavrakakis
        vm1.created = vm1.created - timedelta(seconds=120)
70 0208770b Christos Stavrakakis
        vm1.save()
71 0208770b Christos Stavrakakis
        with mocked_quotaholder():
72 0208770b Christos Stavrakakis
            self.reconciler.reconcile()
73 0208770b Christos Stavrakakis
        vm1 = VirtualMachine.objects.get(id=vm1.id)
74 0208770b Christos Stavrakakis
        self.assertEqual(vm1.operstate, "ERROR")
75 0208770b Christos Stavrakakis
76 0208770b Christos Stavrakakis
        vm1 = mfactory.VirtualMachineFactory(backend=self.backend,
77 0208770b Christos Stavrakakis
                                             backendjobid=1,
78 0208770b Christos Stavrakakis
                                             deleted=False,
79 0208770b Christos Stavrakakis
                                             operstate="BUILD")
80 0208770b Christos Stavrakakis
        vm1.backendtime = vm1.created - timedelta(seconds=120)
81 0208770b Christos Stavrakakis
        vm1.backendjobid = 10
82 0208770b Christos Stavrakakis
        vm1.save()
83 0208770b Christos Stavrakakis
        for status in ["queued", "waiting", "running"]:
84 0208770b Christos Stavrakakis
            mrapi.GetJobStatus.return_value = {"status": status}
85 0208770b Christos Stavrakakis
            with mocked_quotaholder():
86 0208770b Christos Stavrakakis
                self.reconciler.reconcile()
87 0208770b Christos Stavrakakis
            vm1 = VirtualMachine.objects.get(id=vm1.id)
88 0208770b Christos Stavrakakis
            self.assertFalse(vm1.deleted)
89 0208770b Christos Stavrakakis
            self.assertEqual(vm1.operstate, "BUILD")
90 0208770b Christos Stavrakakis
91 0208770b Christos Stavrakakis
        mrapi.GetJobStatus.return_value = {"status": "error"}
92 0208770b Christos Stavrakakis
        with mocked_quotaholder():
93 0208770b Christos Stavrakakis
            self.reconciler.reconcile()
94 0208770b Christos Stavrakakis
        vm1 = VirtualMachine.objects.get(id=vm1.id)
95 0208770b Christos Stavrakakis
        self.assertFalse(vm1.deleted)
96 0208770b Christos Stavrakakis
        self.assertEqual(vm1.operstate, "ERROR")
97 0208770b Christos Stavrakakis
98 0208770b Christos Stavrakakis
        for status in ["success", "cancelled"]:
99 0208770b Christos Stavrakakis
            vm1.deleted = False
100 0208770b Christos Stavrakakis
            vm1.save()
101 0208770b Christos Stavrakakis
            mrapi.GetJobStatus.return_value = {"status": status}
102 0208770b Christos Stavrakakis
            with mocked_quotaholder():
103 0208770b Christos Stavrakakis
                self.reconciler.reconcile()
104 0208770b Christos Stavrakakis
            vm1 = VirtualMachine.objects.get(id=vm1.id)
105 0208770b Christos Stavrakakis
            self.assertTrue(vm1.deleted)
106 0208770b Christos Stavrakakis
            self.assertEqual(vm1.operstate, "DESTROYED")
107 0208770b Christos Stavrakakis
108 0208770b Christos Stavrakakis
        vm1 = mfactory.VirtualMachineFactory(backend=self.backend,
109 0208770b Christos Stavrakakis
                                             backendjobid=1,
110 0208770b Christos Stavrakakis
                                             operstate="BUILD")
111 0208770b Christos Stavrakakis
        vm1.backendtime = vm1.created - timedelta(seconds=120)
112 0208770b Christos Stavrakakis
        vm1.backendjobid = 10
113 0208770b Christos Stavrakakis
        vm1.save()
114 0208770b Christos Stavrakakis
        cmrapi = self.reconciler.client
115 0208770b Christos Stavrakakis
        cmrapi.GetInstances.return_value = \
116 0208770b Christos Stavrakakis
            [{"name": vm1.backend_vm_id,
117 0208770b Christos Stavrakakis
             "beparams": {"maxmem": 1024,
118 0208770b Christos Stavrakakis
                          "minmem": 1024,
119 0208770b Christos Stavrakakis
                          "vcpus": 4},
120 0208770b Christos Stavrakakis
             "oper_state": False,
121 0208770b Christos Stavrakakis
             "mtime": time(),
122 0208770b Christos Stavrakakis
             "disk.sizes": [],
123 0208770b Christos Stavrakakis
             "nic.ips": [],
124 0208770b Christos Stavrakakis
             "nic.macs": [],
125 0208770b Christos Stavrakakis
             "nic.networks": [],
126 0208770b Christos Stavrakakis
             "tags": []}]
127 0208770b Christos Stavrakakis
        mrapi.GetJobStatus.return_value = {"status": "running"}
128 0208770b Christos Stavrakakis
        with mocked_quotaholder():
129 0208770b Christos Stavrakakis
            self.reconciler.reconcile()
130 0208770b Christos Stavrakakis
        vm1 = VirtualMachine.objects.get(id=vm1.id)
131 0208770b Christos Stavrakakis
        self.assertEqual(vm1.operstate, "BUILD")
132 0208770b Christos Stavrakakis
        mrapi.GetJobStatus.return_value = {"status": "error"}
133 0208770b Christos Stavrakakis
        with mocked_quotaholder():
134 0208770b Christos Stavrakakis
            self.reconciler.reconcile()
135 0208770b Christos Stavrakakis
        vm1 = VirtualMachine.objects.get(id=vm1.id)
136 0208770b Christos Stavrakakis
        self.assertEqual(vm1.operstate, "ERROR")
137 0208770b Christos Stavrakakis
138 0208770b Christos Stavrakakis
    def test_stale_server(self, mrapi):
139 0208770b Christos Stavrakakis
        mrapi.GetInstances = []
140 0208770b Christos Stavrakakis
        vm1 = mfactory.VirtualMachineFactory(backend=self.backend,
141 0208770b Christos Stavrakakis
                                             deleted=False,
142 0208770b Christos Stavrakakis
                                             operstate="ERROR")
143 0208770b Christos Stavrakakis
        with mocked_quotaholder():
144 0208770b Christos Stavrakakis
            self.reconciler.reconcile()
145 0208770b Christos Stavrakakis
        vm1 = VirtualMachine.objects.get(id=vm1.id)
146 0208770b Christos Stavrakakis
        self.assertTrue(vm1.deleted)
147 0208770b Christos Stavrakakis
148 0208770b Christos Stavrakakis
    def test_orphan_server(self, mrapi):
149 0208770b Christos Stavrakakis
        cmrapi = self.reconciler.client
150 0208770b Christos Stavrakakis
        mrapi().GetInstances.return_value =\
151 0208770b Christos Stavrakakis
            [{"name": "%s22" % settings.BACKEND_PREFIX_ID,
152 0208770b Christos Stavrakakis
             "beparams": {"maxmem": 1024,
153 0208770b Christos Stavrakakis
                          "minmem": 1024,
154 0208770b Christos Stavrakakis
                          "vcpus": 4},
155 0208770b Christos Stavrakakis
             "oper_state": True,
156 0208770b Christos Stavrakakis
             "mtime": time(),
157 0208770b Christos Stavrakakis
             "disk.sizes": [],
158 0208770b Christos Stavrakakis
             "nic.ips": [],
159 0208770b Christos Stavrakakis
             "nic.macs": [],
160 0208770b Christos Stavrakakis
             "nic.networks": [],
161 0208770b Christos Stavrakakis
             "tags": []}]
162 0208770b Christos Stavrakakis
        self.reconciler.reconcile()
163 0208770b Christos Stavrakakis
        cmrapi.DeleteInstance\
164 0208770b Christos Stavrakakis
              .assert_called_once_with("%s22" % settings.BACKEND_PREFIX_ID)
165 0208770b Christos Stavrakakis
166 0208770b Christos Stavrakakis
    def test_unsynced_operstate(self, mrapi):
167 0208770b Christos Stavrakakis
        vm1 = mfactory.VirtualMachineFactory(backend=self.backend,
168 0208770b Christos Stavrakakis
                                             deleted=False,
169 0208770b Christos Stavrakakis
                                             operstate="STOPPED")
170 0208770b Christos Stavrakakis
        mrapi().GetInstances.return_value =\
171 0208770b Christos Stavrakakis
            [{"name": vm1.backend_vm_id,
172 0208770b Christos Stavrakakis
             "beparams": {"maxmem": 1024,
173 0208770b Christos Stavrakakis
                          "minmem": 1024,
174 0208770b Christos Stavrakakis
                          "vcpus": 4},
175 0208770b Christos Stavrakakis
             "oper_state": True,
176 0208770b Christos Stavrakakis
             "mtime": time(),
177 0208770b Christos Stavrakakis
             "disk.sizes": [],
178 0208770b Christos Stavrakakis
             "nic.ips": [],
179 0208770b Christos Stavrakakis
             "nic.macs": [],
180 0208770b Christos Stavrakakis
             "nic.networks": [],
181 0208770b Christos Stavrakakis
             "tags": []}]
182 0208770b Christos Stavrakakis
        with mocked_quotaholder():
183 0208770b Christos Stavrakakis
            self.reconciler.reconcile()
184 0208770b Christos Stavrakakis
        vm1 = VirtualMachine.objects.get(id=vm1.id)
185 0208770b Christos Stavrakakis
        self.assertEqual(vm1.operstate, "STARTED")
186 0208770b Christos Stavrakakis
187 0208770b Christos Stavrakakis
    def test_unsynced_flavor(self, mrapi):
188 0208770b Christos Stavrakakis
        flavor1 = mfactory.FlavorFactory(cpu=2, ram=1024, disk=1,
189 0208770b Christos Stavrakakis
                                         disk_template="drbd")
190 0208770b Christos Stavrakakis
        flavor2 = mfactory.FlavorFactory(cpu=4, ram=2048, disk=1,
191 0208770b Christos Stavrakakis
                                         disk_template="drbd")
192 0208770b Christos Stavrakakis
        vm1 = mfactory.VirtualMachineFactory(backend=self.backend,
193 0208770b Christos Stavrakakis
                                             deleted=False,
194 0208770b Christos Stavrakakis
                                             flavor=flavor1,
195 0208770b Christos Stavrakakis
                                             operstate="STARTED")
196 0208770b Christos Stavrakakis
        mrapi().GetInstances.return_value =\
197 0208770b Christos Stavrakakis
            [{"name": vm1.backend_vm_id,
198 0208770b Christos Stavrakakis
             "beparams": {"maxmem": 2048,
199 0208770b Christos Stavrakakis
                          "minmem": 2048,
200 0208770b Christos Stavrakakis
                          "vcpus": 4},
201 0208770b Christos Stavrakakis
             "oper_state": True,
202 0208770b Christos Stavrakakis
             "mtime": time(),
203 0208770b Christos Stavrakakis
             "disk.sizes": [],
204 0208770b Christos Stavrakakis
             "nic.ips": [],
205 0208770b Christos Stavrakakis
             "nic.macs": [],
206 0208770b Christos Stavrakakis
             "nic.networks": [],
207 0208770b Christos Stavrakakis
             "tags": []}]
208 0208770b Christos Stavrakakis
        with mocked_quotaholder():
209 0208770b Christos Stavrakakis
            self.reconciler.reconcile()
210 0208770b Christos Stavrakakis
        vm1 = VirtualMachine.objects.get(id=vm1.id)
211 0208770b Christos Stavrakakis
        self.assertEqual(vm1.flavor, flavor2)
212 0208770b Christos Stavrakakis
        self.assertEqual(vm1.operstate, "STARTED")
213 0208770b Christos Stavrakakis
214 0208770b Christos Stavrakakis
    def test_unsynced_nics(self, mrapi):
215 0208770b Christos Stavrakakis
        network1 = mfactory.NetworkFactory(subnet="10.0.0.0/24")
216 0208770b Christos Stavrakakis
        network2 = mfactory.NetworkFactory(subnet="192.168.2.0/24")
217 0208770b Christos Stavrakakis
        vm1 = mfactory.VirtualMachineFactory(backend=self.backend,
218 0208770b Christos Stavrakakis
                                             deleted=False,
219 0208770b Christos Stavrakakis
                                             operstate="STOPPED")
220 0208770b Christos Stavrakakis
        mfactory.NetworkInterfaceFactory(machine=vm1, network=network1,
221 0208770b Christos Stavrakakis
                                         ipv4="10.0.0.0")
222 0208770b Christos Stavrakakis
        mrapi().GetInstances.return_value =\
223 0208770b Christos Stavrakakis
            [{"name": vm1.backend_vm_id,
224 0208770b Christos Stavrakakis
             "beparams": {"maxmem": 2048,
225 0208770b Christos Stavrakakis
                          "minmem": 2048,
226 0208770b Christos Stavrakakis
                          "vcpus": 4},
227 0208770b Christos Stavrakakis
             "oper_state": True,
228 0208770b Christos Stavrakakis
             "mtime": time(),
229 0208770b Christos Stavrakakis
             "disk.sizes": [],
230 0208770b Christos Stavrakakis
             "nic.ips": ["192.168.2.1"],
231 0208770b Christos Stavrakakis
             "nic.macs": ["aa:00:bb:cc:dd:ee"],
232 0208770b Christos Stavrakakis
             "nic.networks": [network2.backend_id],
233 0208770b Christos Stavrakakis
             "tags": []}]
234 0208770b Christos Stavrakakis
        with mocked_quotaholder():
235 0208770b Christos Stavrakakis
            self.reconciler.reconcile()
236 0208770b Christos Stavrakakis
        vm1 = VirtualMachine.objects.get(id=vm1.id)
237 0208770b Christos Stavrakakis
        self.assertEqual(vm1.operstate, "STARTED")
238 0208770b Christos Stavrakakis
        nic = vm1.nics.all()[0]
239 0208770b Christos Stavrakakis
        self.assertEqual(nic.network, network2)
240 0208770b Christos Stavrakakis
        self.assertEqual(nic.ipv4, "192.168.2.1")
241 0208770b Christos Stavrakakis
        self.assertEqual(nic.mac, "aa:00:bb:cc:dd:ee")
242 be4d8469 Christos Stavrakakis
243 be4d8469 Christos Stavrakakis
244 be4d8469 Christos Stavrakakis
@patch("synnefo.logic.rapi_pool.GanetiRapiClient")
245 be4d8469 Christos Stavrakakis
class NetworkReconciliationTest(TestCase):
246 be4d8469 Christos Stavrakakis
    def setUp(self):
247 be4d8469 Christos Stavrakakis
        self.backend = mfactory.BackendFactory()
248 be4d8469 Christos Stavrakakis
        log = logging.getLogger()
249 be4d8469 Christos Stavrakakis
        self.reconciler = reconciliation.NetworkReconciler(
250 be4d8469 Christos Stavrakakis
            logger=log,
251 be4d8469 Christos Stavrakakis
            fix=True)
252 be4d8469 Christos Stavrakakis
253 be4d8469 Christos Stavrakakis
    def test_parted_network(self, mrapi):
254 be4d8469 Christos Stavrakakis
        net1 = mfactory.NetworkFactory(subnet="192.168.0.0/30", public=False)
255 be4d8469 Christos Stavrakakis
        mrapi().GetNetworks.return_value = []
256 be4d8469 Christos Stavrakakis
        # Test nothing if Ganeti returns nothing
257 be4d8469 Christos Stavrakakis
        self.assertEqual(net1.backend_networks.count(), 0)
258 be4d8469 Christos Stavrakakis
        self.reconciler.reconcile_networks()
259 be4d8469 Christos Stavrakakis
        self.assertEqual(net1.backend_networks.count(), 0)
260 be4d8469 Christos Stavrakakis
261 be4d8469 Christos Stavrakakis
        # Test creation if exists in Ganeti
262 be4d8469 Christos Stavrakakis
        self.assertEqual(net1.backend_networks.count(), 0)
263 be4d8469 Christos Stavrakakis
        mrapi().GetNetworks.return_value = [{"name": net1.backend_id,
264 be4d8469 Christos Stavrakakis
                                             "group_list": ["default"],
265 be4d8469 Christos Stavrakakis
                                             "network": net1.subnet,
266 be4d8469 Christos Stavrakakis
                                             "map": "....",
267 be4d8469 Christos Stavrakakis
                                             "external_reservations": ""}]
268 be4d8469 Christos Stavrakakis
        self.reconciler.reconcile_networks()
269 be4d8469 Christos Stavrakakis
        self.assertTrue(net1.backend_networks
270 be4d8469 Christos Stavrakakis
                            .filter(backend=self.backend).exists())
271 be4d8469 Christos Stavrakakis
        # ..but not if it is destroying
272 be4d8469 Christos Stavrakakis
        net1.backend_networks.all().delete()
273 be4d8469 Christos Stavrakakis
        net1.action = "DESTROY"
274 be4d8469 Christos Stavrakakis
        net1.save()
275 be4d8469 Christos Stavrakakis
        self.reconciler.reconcile_networks()
276 be4d8469 Christos Stavrakakis
        self.assertFalse(net1.backend_networks
277 be4d8469 Christos Stavrakakis
                             .filter(backend=self.backend).exists())
278 be4d8469 Christos Stavrakakis
        # or network is public!
279 be4d8469 Christos Stavrakakis
        net1.action = "CREATE"
280 be4d8469 Christos Stavrakakis
        net1.public = True
281 be4d8469 Christos Stavrakakis
        net1.save()
282 be4d8469 Christos Stavrakakis
        self.reconciler.reconcile_networks()
283 be4d8469 Christos Stavrakakis
        self.assertFalse(net1.backend_networks
284 be4d8469 Christos Stavrakakis
                             .filter(backend=self.backend).exists())
285 be4d8469 Christos Stavrakakis
        # Test creation if network is a floating IP pool
286 be4d8469 Christos Stavrakakis
        net2 = mfactory.NetworkFactory(subnet="192.168.0.0/30",
287 be4d8469 Christos Stavrakakis
                                       floating_ip_pool=True)
288 be4d8469 Christos Stavrakakis
        mrapi().GetNetworks.return_value = []
289 be4d8469 Christos Stavrakakis
        self.assertEqual(net2.backend_networks.count(), 0)
290 be4d8469 Christos Stavrakakis
        self.reconciler.reconcile_networks()
291 be4d8469 Christos Stavrakakis
        self.assertTrue(net2.backend_networks
292 be4d8469 Christos Stavrakakis
                            .filter(backend=self.backend).exists())
293 be4d8469 Christos Stavrakakis
294 be4d8469 Christos Stavrakakis
    def test_stale_network(self, mrapi):
295 be4d8469 Christos Stavrakakis
        # Test that stale network will be deleted from DB, if network action is
296 be4d8469 Christos Stavrakakis
        # destroy
297 be4d8469 Christos Stavrakakis
        net1 = mfactory.NetworkFactory(subnet="192.168.0.0/30", public=False,
298 be4d8469 Christos Stavrakakis
                                       flavor="IP_LESS_ROUTED",
299 be4d8469 Christos Stavrakakis
                                       action="DESTROY", deleted=False)
300 be4d8469 Christos Stavrakakis
        bn1 = mfactory.BackendNetworkFactory(network=net1,
301 be4d8469 Christos Stavrakakis
                                             backend=self.backend,
302 be4d8469 Christos Stavrakakis
                                             operstate="ACTIVE")
303 be4d8469 Christos Stavrakakis
        mrapi().GetNetworks.return_value = []
304 be4d8469 Christos Stavrakakis
        self.assertFalse(net1.deleted)
305 be4d8469 Christos Stavrakakis
        with mocked_quotaholder():
306 be4d8469 Christos Stavrakakis
            self.reconciler.reconcile_networks()
307 be4d8469 Christos Stavrakakis
        bn1 = BackendNetwork.objects.get(id=bn1.id)
308 be4d8469 Christos Stavrakakis
        net1 = Network.objects.get(id=net1.id)
309 be4d8469 Christos Stavrakakis
        self.assertEqual(bn1.operstate, "DELETED")
310 be4d8469 Christos Stavrakakis
        self.assertTrue(net1.deleted)
311 be4d8469 Christos Stavrakakis
        # But not if action is not DESTROY
312 be4d8469 Christos Stavrakakis
        net2 = mfactory.NetworkFactory(subnet="192.168.0.0/30", public=False,
313 be4d8469 Christos Stavrakakis
                                       action="CREATE")
314 be4d8469 Christos Stavrakakis
        mfactory.BackendNetworkFactory(network=net2, backend=self.backend)
315 be4d8469 Christos Stavrakakis
        self.assertFalse(net2.deleted)
316 be4d8469 Christos Stavrakakis
        self.reconciler.reconcile_networks()
317 be4d8469 Christos Stavrakakis
        self.assertFalse(net2.deleted)
318 be4d8469 Christos Stavrakakis
319 be4d8469 Christos Stavrakakis
    def test_missing_network(self, mrapi):
320 be4d8469 Christos Stavrakakis
        net2 = mfactory.NetworkFactory(subnet="192.168.0.0/30", public=False,
321 be4d8469 Christos Stavrakakis
                                       action="CREATE")
322 be4d8469 Christos Stavrakakis
        mfactory.BackendNetworkFactory(network=net2, backend=self.backend)
323 be4d8469 Christos Stavrakakis
        mrapi().GetNetworks.return_value = []
324 be4d8469 Christos Stavrakakis
        self.reconciler.reconcile_networks()
325 be4d8469 Christos Stavrakakis
        self.assertEqual(len(mrapi().CreateNetwork.mock_calls), 1)
326 be4d8469 Christos Stavrakakis
327 be4d8469 Christos Stavrakakis
    #def test_hanging_networks(self, mrapi):
328 be4d8469 Christos Stavrakakis
    #    pass
329 be4d8469 Christos Stavrakakis
330 be4d8469 Christos Stavrakakis
    def test_unsynced_networks(self, mrapi):
331 be4d8469 Christos Stavrakakis
        net = mfactory.NetworkFactory(subnet="192.168.0.0/30", public=False,
332 be4d8469 Christos Stavrakakis
                                      state="PENDING",
333 be4d8469 Christos Stavrakakis
                                      action="CREATE", deleted=False)
334 be4d8469 Christos Stavrakakis
        bn = mfactory.BackendNetworkFactory(network=net, backend=self.backend,
335 be4d8469 Christos Stavrakakis
                                            operstate="PENDING")
336 be4d8469 Christos Stavrakakis
        mrapi().GetNetworks.return_value = [{"name": net.backend_id,
337 be4d8469 Christos Stavrakakis
                                             "group_list": [],
338 be4d8469 Christos Stavrakakis
                                             "network": net.subnet,
339 be4d8469 Christos Stavrakakis
                                             "map": "....",
340 be4d8469 Christos Stavrakakis
                                             "external_reservations": ""}]
341 be4d8469 Christos Stavrakakis
        self.assertEqual(bn.operstate, "PENDING")
342 be4d8469 Christos Stavrakakis
        self.reconciler.reconcile_networks()
343 be4d8469 Christos Stavrakakis
        bn = BackendNetwork.objects.get(id=bn.id)
344 be4d8469 Christos Stavrakakis
        self.assertEqual(bn.operstate, "ACTIVE")
345 be4d8469 Christos Stavrakakis
346 be4d8469 Christos Stavrakakis
    def test_orphan_networks(self, mrapi):
347 be4d8469 Christos Stavrakakis
        net = mfactory.NetworkFactory(subnet="192.168.0.0/30", public=False,
348 be4d8469 Christos Stavrakakis
                                      action="CREATE", deleted=True)
349 be4d8469 Christos Stavrakakis
        mrapi().GetNetworks.return_value = [{"name": net.backend_id,
350 be4d8469 Christos Stavrakakis
                                             "group_list": [],
351 be4d8469 Christos Stavrakakis
                                             "network": net.subnet,
352 be4d8469 Christos Stavrakakis
                                             "map": "....",
353 be4d8469 Christos Stavrakakis
                                             "external_reservations": ""}]
354 be4d8469 Christos Stavrakakis
        self.reconciler.reconcile_networks()
355 be4d8469 Christos Stavrakakis
        mrapi().DeleteNetwork.assert_called_once_with(net.backend_id, [])