Merge branch 'devel-2.5'
[ganeti-local] / test / ganeti.impexpd_unittest.py
index 18ca2bd..17c2489 100755 (executable)
@@ -25,6 +25,7 @@ import os
 import sys
 import re
 import unittest
+import socket
 
 from ganeti import constants
 from ganeti import objects
@@ -44,6 +45,8 @@ class CmdBuilderConfig(objects.ConfigObject):
     "ca",
     "host",
     "port",
+    "ipv4",
+    "ipv6",
     "compress",
     "magic",
     "connect_timeout",
@@ -81,7 +84,7 @@ class TestCommandBuilder(unittest.TestCase):
           else:
             self.assertFalse(magic_cmd)
 
-        for host in ["localhost", "1.2.3.4", "192.0.2.99"]:
+        for host in ["localhost", "198.51.100.4", "192.0.2.99"]:
           for port in [0, 1, 1234, 7856, 45452]:
             for cmd_prefix in [None, "PrefixCommandGoesHere|",
                                "dd if=/dev/hda bs=1048576 |"]:
@@ -101,10 +104,10 @@ class TestCommandBuilder(unittest.TestCase):
                   self.assert_(CheckCmdWord(cmd, comprcmd))
 
                 if cmd_prefix is not None:
-                  self.assert_(cmd_prefix in i for i in cmd)
+                  self.assert_(compat.any(cmd_prefix in i for i in cmd))
 
                 if cmd_suffix is not None:
-                  self.assert_(cmd_suffix in i for i in cmd)
+                  self.assert_(compat.any(cmd_suffix in i for i in cmd))
 
                 # Check socat command
                 socat_cmd = builder._GetSocatCommand()
@@ -118,6 +121,34 @@ class TestCommandBuilder(unittest.TestCase):
 
                 self.assert_("verify=1" in ssl_addr)
 
+  def testIPv6(self):
+    for mode in [constants.IEM_IMPORT, constants.IEM_EXPORT]:
+      opts = CmdBuilderConfig(host="localhost", port=6789,
+                              ipv4=False, ipv6=False)
+      builder = impexpd.CommandBuilder(mode, opts, 1, 2, 3)
+      cmd = builder._GetSocatCommand()
+      self.assert_(compat.all("pf=" not in i for i in cmd))
+
+      # IPv4
+      opts = CmdBuilderConfig(host="localhost", port=6789,
+                              ipv4=True, ipv6=False)
+      builder = impexpd.CommandBuilder(mode, opts, 1, 2, 3)
+      cmd = builder._GetSocatCommand()
+      self.assert_(compat.any(",pf=ipv4" in i for i in cmd))
+
+      # IPv6
+      opts = CmdBuilderConfig(host="localhost", port=6789,
+                              ipv4=False, ipv6=True)
+      builder = impexpd.CommandBuilder(mode, opts, 1, 2, 3)
+      cmd = builder._GetSocatCommand()
+      self.assert_(compat.any(",pf=ipv6" in i for i in cmd))
+
+      # IPv4 and IPv6
+      opts = CmdBuilderConfig(host="localhost", port=6789,
+                              ipv4=True, ipv6=True)
+      builder = impexpd.CommandBuilder(mode, opts, 1, 2, 3)
+      self.assertRaises(AssertionError, builder._GetSocatCommand)
+
   def testCommaError(self):
     opts = CmdBuilderConfig(host="localhost", port=1234,
                             ca="/some/path/with,a/,comma")
@@ -155,6 +186,39 @@ class TestCommandBuilder(unittest.TestCase):
     self.assertRaises(errors.GenericError, builder.GetCommand)
 
 
+class TestVerifyListening(unittest.TestCase):
+  def test(self):
+    self.assertEqual(impexpd._VerifyListening(socket.AF_INET,
+                                              "192.0.2.7", 1234),
+                     ("192.0.2.7", 1234))
+    self.assertEqual(impexpd._VerifyListening(socket.AF_INET6, "::1", 9876),
+                     ("::1", 9876))
+    self.assertEqual(impexpd._VerifyListening(socket.AF_INET6, "[::1]", 4563),
+                     ("::1", 4563))
+    self.assertEqual(impexpd._VerifyListening(socket.AF_INET6,
+                                              "[2001:db8::1:4563]", 4563),
+                     ("2001:db8::1:4563", 4563))
+
+  def testError(self):
+    for family in [socket.AF_UNIX, socket.AF_INET, socket.AF_INET6]:
+      self.assertRaises(errors.GenericError, impexpd._VerifyListening,
+                        family, "", 1234)
+      self.assertRaises(errors.GenericError, impexpd._VerifyListening,
+                        family, "192", 999)
+
+    for family in [socket.AF_UNIX, socket.AF_INET6]:
+      self.assertRaises(errors.GenericError, impexpd._VerifyListening,
+                        family, "192.0.2.7", 1234)
+      self.assertRaises(errors.GenericError, impexpd._VerifyListening,
+                        family, "[2001:db8::1", 1234)
+      self.assertRaises(errors.GenericError, impexpd._VerifyListening,
+                        family, "2001:db8::1]", 1234)
+
+    for family in [socket.AF_UNIX, socket.AF_INET]:
+      self.assertRaises(errors.GenericError, impexpd._VerifyListening,
+                        family, "::1", 1234)
+
+
 class TestCalcThroughput(unittest.TestCase):
   def test(self):
     self.assertEqual(impexpd._CalcThroughput([]), None)