Adding customized user/group as configure flags
[ganeti-local] / test / ganeti.masterd.instance_unittest.py
1 #!/usr/bin/python
2 #
3
4 # Copyright (C) 2010 Google Inc.
5 #
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.
10 #
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.
15 #
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
19 # 02110-1301, USA.
20
21
22 """Script for testing ganeti.masterd.instance"""
23
24 import os
25 import sys
26 import unittest
27
28 from ganeti import constants
29 from ganeti import errors
30 from ganeti import utils
31 from ganeti import masterd
32
33 from ganeti.masterd.instance import \
34   ImportExportTimeouts, _TimeoutExpired, _DiskImportExportBase, \
35   ComputeRemoteExportHandshake, CheckRemoteExportHandshake, \
36   ComputeRemoteImportDiskInfo, CheckRemoteExportDiskInfo
37
38 import testutils
39
40
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)
48
49     tmo = ImportExportTimeouts(999)
50     self.assertEqual(tmo.connect, 999)
51
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))
57
58   def testDiskImportExportBaseDirect(self):
59     self.assertRaises(AssertionError, _DiskImportExportBase,
60                       None, None, None, None, None, None, None)
61
62
63 class TestRieHandshake(unittest.TestCase):
64   def test(self):
65     cds = "cd-secret"
66     hs = ComputeRemoteExportHandshake(cds)
67     self.assertEqual(len(hs), 3)
68     self.assertEqual(hs[0], constants.RIE_VERSION)
69
70     self.assertEqual(CheckRemoteExportHandshake(cds, hs), None)
71
72   def testCheckErrors(self):
73     self.assert_(CheckRemoteExportHandshake(None, None))
74     self.assert_(CheckRemoteExportHandshake("", ""))
75     self.assert_(CheckRemoteExportHandshake("", ("xyz", "foo")))
76
77   def testCheckWrongHash(self):
78     cds = "cd-secret999"
79     self.assert_(CheckRemoteExportHandshake(cds, (0, "fakehash", "xyz")))
80
81   def testCheckWrongVersion(self):
82     version = 14887
83     self.assertNotEqual(version, constants.RIE_VERSION)
84     cds = "c28ac99"
85     salt = "a19cf8cc06"
86     msg = "%s:%s" % (version, constants.RIE_HANDSHAKE)
87     hs = (version, utils.Sha1Hmac(cds, msg, salt=salt), salt)
88     self.assert_(CheckRemoteExportHandshake(cds, hs))
89
90
91 class TestRieDiskInfo(unittest.TestCase):
92   def test(self):
93     cds = "bbf46ea9a"
94     salt = "ee5ad9"
95     di = ComputeRemoteImportDiskInfo(cds, salt, 0, "node1", 1234)
96     self.assertEqual(CheckRemoteExportDiskInfo(cds, 0, di),
97                      ("node1", 1234))
98
99     for i in range(1, 100):
100       # Wrong disk index
101       self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
102                         cds, i, di)
103
104   def testCheckErrors(self):
105     cds = "0776450535a"
106     self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
107                       cds, 0, "")
108     self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
109                       cds, 0, ())
110     self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
111                       cds, 0, ("", 1, 2, 3, 4, 5))
112
113     # No host/port
114     self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
115                       cds, 0, ("", 0, "", ""))
116
117     # Wrong hash
118     self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
119                       cds, 0, ("nodeX", 123, "fakehash", "xyz"))
120
121
122 if __name__ == "__main__":
123   testutils.GanetiTestProgram()