Revision 3b2984dc

b/snf-cyclades-app/synnefo/db/pools/__init__.py
78 78
        """Return a value to the pool."""
79 79
        if value is None:
80 80
            raise ValueError
81
        if not self.contains(value):
82
            raise InvalidValue("%s does not belong to pool." % value)
81 83
        index = self.value_to_index(value)
82 84
        self._release(index, external)
83 85

  
84 86
    def reserve(self, value, external=False):
85 87
        """Reserve a value."""
88
        if not self.contains(value):
89
            raise InvalidValue("%s does not belong to pool." % value)
86 90
        index = self.value_to_index(value)
87 91
        self._reserve(index, external)
88 92
        return True
......
114 118
        else:
115 119
            self.available[index] = AVAILABLE
116 120

  
117
    def contains(self, value):
118
        index = self.value_to_index(value)
121
    def contains(self, value, index=False):
122
        if index is False:
123
            index = self.value_to_index(value)
119 124
        return index >= 0 and index < self.pool_size
120 125

  
121 126
    def count_available(self):
......
131 136
        return self.pool_size - self.count_reserved()
132 137

  
133 138
    def is_available(self, value, index=False):
139
        if not self.contains(value, index=index):
140
            raise InvalidValue("%s does not belong to pool." % value)
134 141
        if not index:
135 142
            idx = self.value_to_index(value)
136 143
        else:
......
138 145
        return self.pool[idx] == AVAILABLE
139 146

  
140 147
    def is_reserved(self, value, index=False):
148
        if not self.contains(value, index=index):
149
            raise InvalidValue("%s does not belong to pool." % value)
141 150
        if not index:
142 151
            idx = self.value_to_index(value)
143 152
        else:
b/snf-cyclades-app/synnefo/db/pools/tests.py
102 102
        obj = DummyObject(42)
103 103
        pool = DummyPool(obj)
104 104
        for i in range(42, 48):
105
            self.assertEqual(pool.is_available(i), False)
105
            self.assertRaises(InvalidValue, pool.is_available, i)
106 106
        pool.reserve(32, external=True)
107 107
        values = []
108 108
        while True:
......
189 189
        obj = DummyObject(65636)
190 190
        obj.base = 'ab:ff:ff'
191 191
        pool = MacPrefixPool(obj)
192
        for i in range(0, 65536):
192
        for i in range(0, 65535):
193 193
            self.assertEqual(pool.is_available(i, index=True), False)
194 194

  
195 195
    def test_mac_prefix_conversion(self):
......
209 209

  
210 210

  
211 211
class IPPoolTestCase(TestCase):
212
    def test_auto_reservations(self):
213
        obj = DummyObject(0)
214
        subnet = DummyObject(0)
215
        obj.subnet = subnet
216
        subnet.cidr = '192.168.2.0/24'
217
        subnet.gateway = '192.168.2.1'
218
        pool = IPPool(obj)
219
        self.assertEqual(pool.is_available('192.168.2.0'), False)
220
        self.assertEqual(pool.is_available('192.168.2.1'), False)
221
        self.assertEqual(pool.is_available('192.168.2.255'), False)
222
        self.assertEqual(pool.count_available(), 253)
223
        self.assertEqual(pool.get(), '192.168.2.2')
224
        self.assertTrue(pool.contains("192.168.2.10"))
225
        self.assertFalse(pool.contains("192.168.3.10"))
226

  
227
    def test_auto_reservations_2(self):
228
        obj = DummyObject(0)
229
        subnet = DummyObject(0)
230
        obj.subnet = subnet
231
        subnet.cidr = '192.168.2.0/31'
232
        subnet.gateway = '192.168.2.1'
233
        pool = IPPool(obj)
234
        self.assertEqual(pool.is_available('192.168.2.0'), False)
235
        self.assertEqual(pool.is_available('192.168.2.1'), False)
236
        self.assertEqual(pool.size(), 8)
237
        self.assertEqual(pool.empty(), True)
238

  
239 212
    def test_get_with_value(self):
240
        obj = DummyObject(0)
213
        obj = DummyObject(16)
241 214
        subnet = DummyObject(0)
242 215
        obj.subnet = subnet
243
        subnet.cidr = '192.168.2.0/28'
244
        subnet.gateway = '192.168.2.1'
216
        subnet.cidr = "192.168.2.0/28"
217
        obj.base = "192.168.2.0/28"
218
        obj.offset = 0
245 219
        pool = IPPool(obj)
246 220
        # Test if reserved
247 221
        pool.reserve("192.168.2.2")
......
249 223
        # Test if externally reserved
250 224
        pool.reserve("192.168.2.3", external=True)
251 225
        self.assertRaises(ValueNotAvailable, pool.get, "192.168.2.3")
252
        self.assertRaises(ValueNotAvailable, pool.get, "192.168.2.15")
253 226
        self.assertRaises(InvalidValue, pool.get, "192.168.2.16")
b/snf-cyclades-app/synnefo/db/tests.py
52 52
        """Test a flavor object name method."""
53 53
        flavor = mfact.FlavorFactory(cpu=1, ram=1024, disk=40,
54 54
                                     disk_template="temp")
55
        self.assertEqual(flavor.name, "C1R1024D40temp", "flavor.name is not"
56
                " generated correctly. Name is %s instead of C1R1024D40temp" %
57
                flavor.name)
55
        self.assertEqual(
56
            flavor.name, "C1R1024D40temp", "flavor.name is not"
57
            " generated correctly. Name is %s instead of C1R1024D40temp" %
58
            flavor.name)
58 59

  
59 60

  
60 61
class BackendTest(TestCase):
......
194 195
                         len_backends + 3)
195 196

  
196 197
    def test_pool(self):
197
        pool = self.net.get_pool()
198
        pool = self.net.get_ip_pools()[0]
198 199
        pool.network = self.net
199 200
        self.assertTrue(isinstance(pool, IPPool))
200 201

  
201 202
    def test_reserve_ip(self):
202 203
        net1 = mfact.NetworkWithSubnetFactory(subnet__cidr='192.168.2.0/24')
204
        pool = net1.get_ip_pools()[0]
205
        self.assertTrue(pool.is_available('192.168.2.12'))
203 206
        net1.reserve_address('192.168.2.12')
204
        pool = net1.get_pool()
207
        pool = net1.get_ip_pools()[0]
205 208
        self.assertFalse(pool.is_available('192.168.2.12'))
206 209
        net1.release_address('192.168.2.12')
207
        pool = net1.get_pool()
210
        pool = net1.get_ip_pools()[0]
208 211
        self.assertTrue(pool.is_available('192.168.2.12'))
209 212

  
210 213

  
b/snf-cyclades-app/synnefo/logic/tests/callbacks.py
137 137
        # Also create a NIC
138 138
        ip = mfactory.IPv4AddressFactory(nic__machine=vm)
139 139
        nic = ip.nic
140
        nic.network.get_pool().reserve(nic.ipv4_address)
140
        nic.network.get_ip_pools()[0].reserve(nic.ipv4_address)
141 141
        msg = self.create_msg(operation='OP_INSTANCE_REMOVE',
142 142
                              instance=vm.backend_vm_id)
143 143
        with mocked_quotaholder():
......
148 148
        self.assertTrue(db_vm.deleted)
149 149
        # Check that nics are deleted
150 150
        self.assertFalse(db_vm.nics.all())
151
        self.assertTrue(nic.network.get_pool().is_available(ip.address))
151
        self.assertTrue(nic.network.get_ip_pools()[0].is_available(ip.address))
152 152
        vm2 = mfactory.VirtualMachineFactory()
153 153
        fp1 = mfactory.IPv4AddressFactory(nic__machine=vm2, floating_ip=True,
154 154
                                          network__floating_ip_pool=True)
......
156 156
        nic1 = mfactory.NetworkInterfaceFactory(machine=vm2)
157 157
        fp1.nic = nic1
158 158
        fp1.save()
159
        pool = network.get_pool()
159
        pool = network.get_ip_pools()[0]
160 160
        pool.reserve(fp1.address)
161 161
        pool.save()
162 162
        msg = self.create_msg(operation='OP_INSTANCE_REMOVE',
......
168 168
        self.assertEqual(db_vm.operstate, 'DESTROYED')
169 169
        self.assertTrue(db_vm.deleted)
170 170
        self.assertEqual(IPAddress.objects.get(id=fp1.id).nic, None)
171
        pool = network.get_pool()
171
        pool = network.get_ip_pools()[0]
172 172
        # Test that floating ips are not released
173 173
        self.assertFalse(pool.is_available(fp1.address))
174 174

  
......
367 367
        network = ip.network
368 368
        subnet = ip.subnet
369 369
        vm = ip.nic.machine
370
        pool = subnet.get_pool()
370
        pool = subnet.get_ip_pools()[0]
371 371
        pool.reserve("10.0.0.2")
372 372
        pool.save()
373 373

  
......
384 384
        self.assertEqual(nics[0].index, 0)
385 385
        self.assertEqual(nics[0].ipv4_address, '10.0.0.3')
386 386
        self.assertEqual(nics[0].mac, 'aa:bb:cc:00:11:22')
387
        pool = subnet.get_pool()
387
        pool = subnet.get_ip_pools()[0]
388 388
        self.assertTrue(pool.is_available('10.0.0.2'))
389 389
        self.assertFalse(pool.is_available('10.0.0.3'))
390 390
        pool.save()
......
600 600
                                                               "10.0.0.20"]})
601 601
        update_network(client, msg)
602 602
        self.assertTrue(client.basic_ack.called)
603
        pool = network.get_pool()
603
        pool = network.get_ip_pools()[0]
604 604
        self.assertTrue(pool.is_reserved('10.0.0.10'))
605 605
        self.assertTrue(pool.is_reserved('10.0.0.20'))
606 606
        pool.save()
......
613 613
                                                          "10.0.0.20"]})
614 614
        update_network(client, msg)
615 615
        #self.assertTrue(client.basic_ack.called)
616
        pool = network.get_pool()
616
        pool = network.get_ip_pools()[0]
617 617
        self.assertTrue(pool.is_reserved('10.0.0.10'))
618 618
        self.assertTrue(pool.is_reserved('10.0.0.20'))
619 619

  
b/snf-cyclades-app/synnefo/logic/tests/networks.py
152 152
        subnet6 = net.subnets.get(ipversion=6)
153 153
        self.assertEqual(subnet6.cidr, "2001:648:2ffc:1112::/64")
154 154
        self.assertEqual(subnet6.gateway, "2001:648:2ffc:1112::1")
155
        self.assertRaises(Exception, net.get_pool)
155
        self.assertEqual(net.get_ip_pools(), [])
b/snf-cyclades-app/synnefo/logic/tests/reconciliation.py
202 202
             "mtime": time(),
203 203
             "disk.sizes": [],
204 204
             "nic.names": [nic.backend_uuid],
205
             "nic.ips": ["192.168.2.1"],
205
             "nic.ips": ["192.168.2.5"],
206 206
             "nic.macs": ["aa:00:bb:cc:dd:ee"],
207 207
             "nic.networks.names": [network2.backend_id],
208 208
             "tags": []}]
......
212 212
        self.assertEqual(vm1.operstate, "STARTED")
213 213
        nic = vm1.nics.all()[0]
214 214
        self.assertEqual(nic.network, network2)
215
        self.assertEqual(nic.ipv4_address, "192.168.2.1")
215
        self.assertEqual(nic.ipv4_address, "192.168.2.5")
216 216
        self.assertEqual(nic.mac, "aa:00:bb:cc:dd:ee")
217 217

  
218 218

  
b/snf-cyclades-app/synnefo/logic/tests/servers.py
120 120
            subnet = mfactory.IPv4SubnetFactory(network__flavor="CUSTOM",
121 121
                                                cidr="192.168.2.0/24",
122 122
                                                gateway="192.168.2.1",
123
                                                dhcp=True)
123
                                                dhcp=dhcp)
124 124
            net = subnet.network
125 125
            vm = mfactory.VirtualMachineFactory(operstate="STARTED")
126 126
            mfactory.BackendNetworkFactory(network=net, backend=vm.backend)
127 127
            mrapi().ModifyInstance.return_value = 42
128 128
            servers.connect(vm, net)
129
            pool = net.get_pool(locked=False)
129
            pool = net.get_ip_pools(locked=False)[0]
130 130
            self.assertFalse(pool.is_available("192.168.2.2"))
131 131
            args, kwargs = mrapi().ModifyInstance.call_args
132 132
            nics = kwargs["nics"][0]

Also available in: Unified diff