4 # Copyright (C) 2010 Google Inc.
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.
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.
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
22 """Script for testing ganeti.impexpd"""
29 from ganeti import constants
30 from ganeti import objects
31 from ganeti import compat
32 from ganeti import utils
33 from ganeti import errors
34 from ganeti import impexpd
39 class CmdBuilderConfig(objects.ConfigObject):
55 def CheckCmdWord(cmd, word):
56 wre = re.compile(r"\b%s\b" % re.escape(word))
57 return compat.any(wre.search(i) for i in cmd)
60 class TestCommandBuilder(unittest.TestCase):
62 for mode in [constants.IEM_IMPORT, constants.IEM_EXPORT]:
63 if mode == constants.IEM_IMPORT:
65 elif mode == constants.IEM_EXPORT:
68 for compress in [constants.IEC_NONE, constants.IEC_GZIP]:
69 for host in ["localhost", "1.2.3.4", "192.0.2.99"]:
70 for port in [0, 1, 1234, 7856, 45452]:
71 for cmd_prefix in [None, "PrefixCommandGoesHere|",
72 "dd if=/dev/hda bs=1048576 |"]:
73 for cmd_suffix in [None, "< /some/file/name",
75 opts = CmdBuilderConfig(host=host, port=port, compress=compress,
76 cmd_prefix=cmd_prefix,
77 cmd_suffix=cmd_suffix)
79 builder = impexpd.CommandBuilder(mode, opts, 1, 2, 3)
81 # Check complete command
82 cmd = builder.GetCommand()
83 self.assert_(isinstance(cmd, list))
85 if compress == constants.IEC_GZIP:
86 self.assert_(CheckCmdWord(cmd, comprcmd))
88 if cmd_prefix is not None:
89 self.assert_(cmd_prefix in i for i in cmd)
91 if cmd_suffix is not None:
92 self.assert_(cmd_suffix in i for i in cmd)
95 socat_cmd = builder._GetSocatCommand()
97 if mode == constants.IEM_IMPORT:
98 ssl_addr = socat_cmd[-2].split(",")
99 self.assert_(("OPENSSL-LISTEN:%s" % port) in ssl_addr)
100 elif mode == constants.IEM_EXPORT:
101 ssl_addr = socat_cmd[-1].split(",")
102 self.assert_(("OPENSSL:%s:%s" % (host, port)) in ssl_addr)
104 self.assert_("verify=1" in ssl_addr)
106 def testCommaError(self):
107 opts = CmdBuilderConfig(host="localhost", port=1234,
108 ca="/some/path/with,a/,comma")
110 for mode in [constants.IEM_IMPORT, constants.IEM_EXPORT]:
111 builder = impexpd.CommandBuilder(mode, opts, 1, 2, 3)
112 self.assertRaises(errors.GenericError, builder.GetCommand)
114 def testModeError(self):
117 assert mode not in [constants.IEM_IMPORT, constants.IEM_EXPORT]
119 opts = CmdBuilderConfig(host="localhost", port=1234)
120 builder = impexpd.CommandBuilder(mode, opts, 1, 2, 3)
121 self.assertRaises(errors.GenericError, builder.GetCommand)
124 class TestCalcThroughput(unittest.TestCase):
126 self.assertEqual(impexpd._CalcThroughput([]), None)
127 self.assertEqual(impexpd._CalcThroughput([(0, 0)]), None)
133 self.assertAlmostEqual(impexpd._CalcThroughput(samples), 10.0, 3)
140 self.assertAlmostEqual(impexpd._CalcThroughput(samples), 15.818, 3)
143 if __name__ == "__main__":
144 testutils.GanetiTestProgram()