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
41 class TestMisc(unittest.TestCase):
42 def testTimeouts(self):
43 tmo = ImportExportTimeouts(0)
44 self.assertEqual(tmo.connect, 0)
45 self.assertEqual(tmo.listen, ImportExportTimeouts.DEFAULT_LISTEN_TIMEOUT)
46 self.assertEqual(tmo.ready, ImportExportTimeouts.DEFAULT_READY_TIMEOUT)
47 self.assertEqual(tmo.error, ImportExportTimeouts.DEFAULT_ERROR_TIMEOUT)
49 tmo = ImportExportTimeouts(999)
50 self.assertEqual(tmo.connect, 999)
52 def testTimeoutExpired(self):
53 self.assert_(_TimeoutExpired(100, 300, _time_fn=lambda: 500))
54 self.assertFalse(_TimeoutExpired(100, 300, _time_fn=lambda: 0))
55 self.assertFalse(_TimeoutExpired(100, 300, _time_fn=lambda: 100))
56 self.assertFalse(_TimeoutExpired(100, 300, _time_fn=lambda: 400))
58 def testDiskImportExportBaseDirect(self):
59 self.assertRaises(AssertionError, _DiskImportExportBase,
60 None, None, None, None, None, None, None)
63 class TestRieHandshake(unittest.TestCase):
66 hs = ComputeRemoteExportHandshake(cds)
67 self.assertEqual(len(hs), 3)
68 self.assertEqual(hs[0], constants.RIE_VERSION)
70 self.assertEqual(CheckRemoteExportHandshake(cds, hs), None)
72 def testCheckErrors(self):
73 self.assert_(CheckRemoteExportHandshake(None, None))
74 self.assert_(CheckRemoteExportHandshake("", ""))
75 self.assert_(CheckRemoteExportHandshake("", ("xyz", "foo")))
77 def testCheckWrongHash(self):
79 self.assert_(CheckRemoteExportHandshake(cds, (0, "fakehash", "xyz")))
81 def testCheckWrongVersion(self):
83 self.assertNotEqual(version, constants.RIE_VERSION)
86 msg = "%s:%s" % (version, constants.RIE_HANDSHAKE)
87 hs = (version, utils.Sha1Hmac(cds, msg, salt=salt), salt)
88 self.assert_(CheckRemoteExportHandshake(cds, hs))
91 class TestRieDiskInfo(unittest.TestCase):
95 di = ComputeRemoteImportDiskInfo(cds, salt, 0, "node1", 1234)
96 self.assertEqual(CheckRemoteExportDiskInfo(cds, 0, di),
99 for i in range(1, 100):
101 self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
104 def testCheckErrors(self):
106 self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
108 self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
110 self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
111 cds, 0, ("", 1, 2, 3, 4, 5))
114 self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
115 cds, 0, ("", 0, "", ""))
118 self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
119 cds, 0, ("nodeX", 123, "fakehash", "xyz"))
122 if __name__ == "__main__":
123 testutils.GanetiTestProgram()