Revision 1190610e

b/test/ganeti.network_unittest.py
1
#!/usr/bin/python
2
#
3

  
4
# Copyright (C) 2011 Google Inc.
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
# General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
# 0.0510-1301, USA.
20

  
21

  
22
"""Script for unittesting the network module"""
23

  
24

  
25
import unittest
26

  
27
from ganeti import objects
28
from ganeti import network
29
from ganeti import errors
30

  
31
import testutils
32

  
33
class TestNetwork(unittest.TestCase):
34
  def testNetwork(self):
35
    net = network.ipaddr.IPNetwork("192.0.2.0/24")
36
    gateway = network.ipaddr.IPAddress("192.0.2.1")
37

  
38
    res = network.bitarray(net.numhosts)
39
    res.setall(False)
40
    ext = network.bitarray(net.numhosts)
41
    ext.setall(False)
42

  
43
    extres = [0, -1, -2]
44
    for idx in extres:
45
      ext[idx] = True
46

  
47
    obj = objects.Network(network=str(net), gateway=str(gateway),
48
                        reservations=network.b64encode(res.tostring()),
49
                        ext_reservations=network.b64encode(ext.tostring()),
50
                        family=4)
51

  
52
    pool = network.AddressPool(obj)
53

  
54
    free = pool.GetFreeCount()
55
    reserved = pool.GetReservedCount()
56
    self.assertEqual(free, net.numhosts - len(extres))
57
    self.assertEqual(reserved, len(extres))
58

  
59
    # Our network is not full and must be valid
60
    self.assertEqual(pool.IsFull(), False)
61
    self.assertEqual(pool.Validate(), True)
62

  
63
    # Network address must be reserved
64
    self.assertEqual(pool.IsReserved(net[extres[0]]), True)
65
    self.assertRaises(errors.AddressPoolError, pool.Reserve, net[extres[0]])
66

  
67
    # Release a reserved address and check count consistency
68
    pool.Release(net[extres[-1]], external=True)
69
    self.assertEqual(pool.GetFreeCount(), free + 1)
70
    self.assertEqual(pool.GetReservedCount(), reserved - 1)
71

  
72
    # Check that free addresses are returned in order
73
    self.assertEqual(pool.GetFreeAddress(), str(net[1]))
74
    self.assertEqual(pool.GetFreeAddress(), str(net[2]))
75

  
76

  
77
    # GenerateFree should return an iterator generating
78
    # at most 64 free addresses
79
    i = 0
80
    itr = pool.GenerateFree()
81

  
82
    while True:
83
      try:
84
        itr()
85
        i += 1
86
      except errors.AddressPoolError:
87
        break
88
      except StopIteration:
89
        break
90

  
91
    self.assertEqual(i, 64)
92

  
93
    # Check to see if we can actually exhaust the space
94
    addresses = []
95
    def exhaust(addresses):
96
      for i in range(0, 255):
97
        addresses.append(pool.GetFreeAddress())
98

  
99
    self.assertRaises(errors.AddressPoolError, exhaust, addresses)
100

  
101
    # Check that all reservations where taken from the proper pool
102
    self.assertEqual(pool.reservations.count(True), len(addresses) + 2)
103

  
104
    # Check that we didn't generate any duplicates
105
    self.assertEqual(len(addresses), len(set(addresses)))
106

  
107
    # Check serialization and de-serialization
108
    new_pool = network.AddressPool(obj)
109
    self.assertEqual(new_pool.reservations.to01(), pool.reservations.to01())
110
    self.assertEqual(new_pool.ext_reservations.to01(),
111
                     pool.ext_reservations.to01())
112

  
113

  
114
if __name__ == '__main__':
115
  testutils.GanetiTestProgram()

Also available in: Unified diff