Revision 2632795d

b/lib/utils.py
1352 1352
  return IsValidIP4(ip) or IsValidIP6(ip)
1353 1353

  
1354 1354

  
1355
def GetAddressFamily(ip):
1356
  """Get the address family of the given address.
1357

  
1358
  @type ip: str
1359
  @param ip: ip address whose family will be returned
1360
  @rtype: int
1361
  @return: socket.AF_INET or socket.AF_INET6
1362
  @raise errors.GenericError: for invalid addresses
1363

  
1364
  """
1365
  if IsValidIP6(ip):
1366
    return socket.AF_INET6
1367
  elif IsValidIP4(ip):
1368
    return socket.AF_INET
1369
  else:
1370
    raise errors.GenericError("Address %s not valid" % ip)
1371

  
1372

  
1355 1373
def IsValidShellParam(word):
1356 1374
  """Verifies is the given word is safe from the shell's p.o.v.
1357 1375

  
b/test/ganeti.utils_unittest.py
2464 2464
    self.assertFalse(utils.IsValidIP("a:g::1"))
2465 2465

  
2466 2466

  
2467
class TestGetAddressFamily(unittest.TestCase):
2468
  def test(self):
2469
    self.assertEqual(utils.GetAddressFamily("127.0.0.1"), socket.AF_INET)
2470
    self.assertEqual(utils.GetAddressFamily("10.2.0.127"), socket.AF_INET)
2471
    self.assertEqual(utils.GetAddressFamily("::1"), socket.AF_INET6)
2472
    self.assertEqual(utils.GetAddressFamily("fe80::a00:27ff:fe08:5048"),
2473
                     socket.AF_INET6)
2474
    self.assertRaises(errors.GenericError, utils.GetAddressFamily, "0")
2475

  
2476

  
2467 2477
if __name__ == '__main__':
2468 2478
  testutils.GanetiTestProgram()

Also available in: Unified diff