Revision 37531236 test/ganeti.netutils_unittest.py

b/test/ganeti.netutils_unittest.py
22 22
"""Script for unittesting the netutils module"""
23 23

  
24 24
import os
25
import re
25 26
import shutil
26 27
import socket
27 28
import tempfile
......
171 172
    self.assertFalse(netutils.IPAddress.Own("192.0.2.1"),
172 173
                     "Should not own IP address 192.0.2.1")
173 174

  
175
  def testFamilyVersionConversions(self):
176
    # IPAddress.GetAddressFamilyFromVersion
177
    self.assertEqual(
178
        netutils.IPAddress.GetAddressFamilyFromVersion(constants.IP4_VERSION),
179
        socket.AF_INET)
180
    self.assertEqual(
181
        netutils.IPAddress.GetAddressFamilyFromVersion(constants.IP6_VERSION),
182
        socket.AF_INET6)
183
    self.assertRaises(errors.ProgrammerError,
184
        netutils.IPAddress.GetAddressFamilyFromVersion, 3)
185

  
186
    # IPAddress.GetVersionFromAddressFamily
187
    self.assertEqual(
188
        netutils.IPAddress.GetVersionFromAddressFamily(socket.AF_INET),
189
        constants.IP4_VERSION)
190
    self.assertEqual(
191
        netutils.IPAddress.GetVersionFromAddressFamily(socket.AF_INET6),
192
        constants.IP6_VERSION)
193
    self.assertRaises(errors.ProgrammerError,
194
        netutils.IPAddress.GetVersionFromAddressFamily, socket.AF_UNIX)
195

  
174 196

  
175 197
class TestIP4Address(unittest.TestCase):
176 198
  def testGetIPIntFromString(self):
......
421 443
    self.assertRaises(errors.ParameterError, netutils.FormatAddress,
422 444
                      ("::1"), family=socket.AF_INET )
423 445

  
446
class TestIpParsing(testutils.GanetiTestCase):
447
  """Test the code that parses the ip command output"""
448

  
449
  def testIp4(self):
450
    valid_addresses = [constants.IP4_ADDRESS_ANY,
451
                       constants.IP4_ADDRESS_LOCALHOST,
452
                       "192.0.2.1",     # RFC5737, IPv4 address blocks for docs
453
                       "198.51.100.1",
454
                       "203.0.113.1",
455
                      ]
456
    for addr in valid_addresses:
457
      self.failUnless(re.search(netutils._IP_RE_TEXT, addr))
458

  
459
  def testIp6(self):
460
    valid_addresses = [constants.IP6_ADDRESS_ANY,
461
                       constants.IP6_ADDRESS_LOCALHOST,
462
                       "0:0:0:0:0:0:0:1", # other form for IP6_ADDRESS_LOCALHOST
463
                       "0:0:0:0:0:0:0:0", # other form for IP6_ADDRESS_ANY
464
                       "2001:db8:85a3::8a2e:370:7334", # RFC3849 IP6 docs block
465
                       "2001:0db8:85a3:0000:0000:8a2e:0370:7334",
466
                       "0:0:0:0:0:FFFF:192.0.2.1",  # IPv4-compatible IPv6
467
                       "::FFFF:192.0.2.1",
468
                       "0:0:0:0:0:0:203.0.113.1",   # IPv4-mapped IPv6
469
                       "::203.0.113.1",
470
                      ]
471
    for addr in valid_addresses:
472
      self.failUnless(re.search(netutils._IP_RE_TEXT, addr))
473

  
474
  def testParseIpCommandOutput(self):
475
    # IPv4-only, fake loopback interface
476
    tests = ["ip-addr-show-lo-ipv4.txt", "ip-addr-show-lo-oneline-ipv4.txt"]
477
    for test_file in tests:
478
      data = self._ReadTestData(test_file)
479
      addr = netutils._GetIpAddressesFromIpOutput(data)
480
      self.failUnless(len(addr[4]) == 1 and addr[4][0] == "127.0.0.1" and not
481
                      addr[6])
482

  
483
    # IPv6-only, fake loopback interface
484
    tests = ["ip-addr-show-lo-ipv6.txt", "ip-addr-show-lo-ipv6.txt"]
485
    for test_file in tests:
486
      data = self._ReadTestData(test_file)
487
      addr = netutils._GetIpAddressesFromIpOutput(data)
488
      self.failUnless(len(addr[6]) == 1 and addr[6][0] == "::1" and not addr[4])
489

  
490
    # IPv4 and IPv6, fake loopback interface
491
    tests = ["ip-addr-show-lo.txt", "ip-addr-show-lo-oneline.txt"]
492
    for test_file in tests:
493
      data = self._ReadTestData(test_file)
494
      addr = netutils._GetIpAddressesFromIpOutput(data)
495
      self.failUnless(len(addr[6]) == 1 and addr[6][0] == "::1" and
496
                      len(addr[4]) == 1 and addr[4][0] == "127.0.0.1")
497

  
498
    # IPv4 and IPv6, dummy interface
499
    data = self._ReadTestData("ip-addr-show-dummy0.txt")
500
    addr = netutils._GetIpAddressesFromIpOutput(data)
501
    self.failUnless(len(addr[6]) == 1 and
502
                    addr[6][0] == "2001:db8:85a3::8a2e:370:7334" and
503
                    len(addr[4]) == 1 and
504
                    addr[4][0] == "192.0.2.1")
505

  
424 506

  
425 507
if __name__ == "__main__":
426 508
  testutils.GanetiTestProgram()

Also available in: Unified diff