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.masterd.instance"""
28 from ganeti import constants
29 from ganeti import errors
30 from ganeti import utils
31 from ganeti import masterd
33 from ganeti.masterd.instance import \
34 ImportExportTimeouts, _DiskImportExportBase, \
35 ComputeRemoteExportHandshake, CheckRemoteExportHandshake, \
36 ComputeRemoteImportDiskInfo, CheckRemoteExportDiskInfo, \
42 class TestMisc(unittest.TestCase):
43 def testTimeouts(self):
44 tmo = ImportExportTimeouts(0)
45 self.assertEqual(tmo.connect, 0)
46 self.assertEqual(tmo.listen, ImportExportTimeouts.DEFAULT_LISTEN_TIMEOUT)
47 self.assertEqual(tmo.ready, ImportExportTimeouts.DEFAULT_READY_TIMEOUT)
48 self.assertEqual(tmo.error, ImportExportTimeouts.DEFAULT_ERROR_TIMEOUT)
49 self.assertEqual(tmo.progress,
50 ImportExportTimeouts.DEFAULT_PROGRESS_INTERVAL)
52 tmo = ImportExportTimeouts(999)
53 self.assertEqual(tmo.connect, 999)
55 tmo = ImportExportTimeouts(1, listen=2, error=3, ready=4, progress=5)
56 self.assertEqual(tmo.connect, 1)
57 self.assertEqual(tmo.listen, 2)
58 self.assertEqual(tmo.error, 3)
59 self.assertEqual(tmo.ready, 4)
60 self.assertEqual(tmo.progress, 5)
62 def testTimeoutExpired(self):
63 self.assert_(utils.TimeoutExpired(100, 300, _time_fn=lambda: 500))
64 self.assertFalse(utils.TimeoutExpired(100, 300, _time_fn=lambda: 0))
65 self.assertFalse(utils.TimeoutExpired(100, 300, _time_fn=lambda: 100))
66 self.assertFalse(utils.TimeoutExpired(100, 300, _time_fn=lambda: 400))
68 def testDiskImportExportBaseDirect(self):
69 self.assertRaises(AssertionError, _DiskImportExportBase,
70 None, None, None, None, None, None, None)
73 class TestRieHandshake(unittest.TestCase):
76 hs = ComputeRemoteExportHandshake(cds)
77 self.assertEqual(len(hs), 3)
78 self.assertEqual(hs[0], constants.RIE_VERSION)
80 self.assertEqual(CheckRemoteExportHandshake(cds, hs), None)
82 def testCheckErrors(self):
83 self.assert_(CheckRemoteExportHandshake(None, None))
84 self.assert_(CheckRemoteExportHandshake("", ""))
85 self.assert_(CheckRemoteExportHandshake("", ("xyz", "foo")))
87 def testCheckWrongHash(self):
89 self.assert_(CheckRemoteExportHandshake(cds, (0, "fakehash", "xyz")))
91 def testCheckWrongVersion(self):
93 self.assertNotEqual(version, constants.RIE_VERSION)
96 msg = "%s:%s" % (version, constants.RIE_HANDSHAKE)
97 hs = (version, utils.Sha1Hmac(cds, msg, salt=salt), salt)
98 self.assert_(CheckRemoteExportHandshake(cds, hs))
101 class TestRieDiskInfo(unittest.TestCase):
105 di = ComputeRemoteImportDiskInfo(cds, salt, 0, "node1", 1234, "mag111")
106 self.assertEqual(CheckRemoteExportDiskInfo(cds, 0, di),
107 ("node1", 1234, "mag111"))
109 for i in range(1, 100):
111 self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
114 def testInvalidHostPort(self):
118 for host in [",", "...", "Hello World", "`", "!", "#", "\\"]:
119 di = ComputeRemoteImportDiskInfo(cds, salt, 0, host, 1234, "magic")
120 self.assertRaises(errors.OpPrereqError,
121 CheckRemoteExportDiskInfo, cds, 0, di)
123 for port in [-1, 792825908, "HelloWorld!", "`#", "\\\"", "_?_"]:
124 di = ComputeRemoteImportDiskInfo(cds, salt, 0, "localhost", port, "magic")
125 self.assertRaises(errors.OpPrereqError,
126 CheckRemoteExportDiskInfo, cds, 0, di)
128 def testCheckErrors(self):
130 self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
132 self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
134 self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
135 cds, 0, ("", 1, 2, 3, 4, 5))
138 self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
139 cds, 0, ("", 1234, "magic", "", ""))
140 self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
141 cds, 0, ("host", 0, "magic", "", ""))
142 self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
143 cds, 0, ("host", 1234, "", "", ""))
146 self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
147 cds, 0, ("nodeX", 123, "magic", "fakehash", "xyz"))
150 class TestFormatProgress(unittest.TestCase):
152 FormatProgress((0, 0, None, None))
153 FormatProgress((100, 3.3, 30, None))
154 FormatProgress((100, 3.3, 30, 900))
156 self.assertEqual(FormatProgress((1500, 12, 30, None)),
157 "1.5G, 12.0 MiB/s, 30%")
160 if __name__ == "__main__":
161 testutils.GanetiTestProgram()