import sys
import re
import unittest
+import socket
from ganeti import constants
from ganeti import objects
"ca",
"host",
"port",
+ "ipv4",
+ "ipv6",
"compress",
+ "magic",
"connect_timeout",
"connect_retries",
"cmd_prefix",
comprcmd = "gzip"
for compress in [constants.IEC_NONE, constants.IEC_GZIP]:
- for host in ["localhost", "1.2.3.4", "192.0.2.99"]:
+ for magic in [None, 10 * "-", "HelloWorld", "J9plh4nFo2",
+ "24A02A81-2264-4B51-A882-A2AB9D85B420"]:
+ opts = CmdBuilderConfig(magic=magic, compress=compress)
+ builder = impexpd.CommandBuilder(mode, opts, 1, 2, 3)
+
+ magic_cmd = builder._GetMagicCommand()
+ dd_cmd = builder._GetDdCommand()
+
+ if magic:
+ self.assert_(("M=%s" % magic) in magic_cmd)
+ self.assert_(("M=%s" % magic) in dd_cmd)
+ else:
+ self.assertFalse(magic_cmd)
+
+ 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 |"]:
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()
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")
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)