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, _TimeoutExpired, _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_(_TimeoutExpired(100, 300, _time_fn=lambda: 500))
64 self.assertFalse(_TimeoutExpired(100, 300, _time_fn=lambda: 0))
65 self.assertFalse(_TimeoutExpired(100, 300, _time_fn=lambda: 100))
66 self.assertFalse(_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)
106 self.assertEqual(CheckRemoteExportDiskInfo(cds, 0, di),
109 for i in range(1, 100):
111 self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
114 def testCheckErrors(self):
116 self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
118 self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
120 self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
121 cds, 0, ("", 1, 2, 3, 4, 5))
124 self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
125 cds, 0, ("", 0, "", ""))
128 self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
129 cds, 0, ("nodeX", 123, "fakehash", "xyz"))
132 class TestFormatProgress(unittest.TestCase):
134 FormatProgress((0, 0, None, None))
135 FormatProgress((100, 3.3, 30, None))
136 FormatProgress((100, 3.3, 30, 900))
138 self.assertEqual(FormatProgress((1500, 12, 30, None)),
139 "1.5G, 12.0 MiB/s, 30%")
142 if __name__ == "__main__":
143 testutils.GanetiTestProgram()