Handle ESRCH when sending signals
[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   FormatProgress
38
39 import testutils
40
41
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)
51
52     tmo = ImportExportTimeouts(999)
53     self.assertEqual(tmo.connect, 999)
54
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)
61
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))
67
68   def testDiskImportExportBaseDirect(self):
69     self.assertRaises(AssertionError, _DiskImportExportBase,
70                       None, None, None, None, None, None, None)
71
72
73 class TestRieHandshake(unittest.TestCase):
74   def test(self):
75     cds = "cd-secret"
76     hs = ComputeRemoteExportHandshake(cds)
77     self.assertEqual(len(hs), 3)
78     self.assertEqual(hs[0], constants.RIE_VERSION)
79
80     self.assertEqual(CheckRemoteExportHandshake(cds, hs), None)
81
82   def testCheckErrors(self):
83     self.assert_(CheckRemoteExportHandshake(None, None))
84     self.assert_(CheckRemoteExportHandshake("", ""))
85     self.assert_(CheckRemoteExportHandshake("", ("xyz", "foo")))
86
87   def testCheckWrongHash(self):
88     cds = "cd-secret999"
89     self.assert_(CheckRemoteExportHandshake(cds, (0, "fakehash", "xyz")))
90
91   def testCheckWrongVersion(self):
92     version = 14887
93     self.assertNotEqual(version, constants.RIE_VERSION)
94     cds = "c28ac99"
95     salt = "a19cf8cc06"
96     msg = "%s:%s" % (version, constants.RIE_HANDSHAKE)
97     hs = (version, utils.Sha1Hmac(cds, msg, salt=salt), salt)
98     self.assert_(CheckRemoteExportHandshake(cds, hs))
99
100
101 class TestRieDiskInfo(unittest.TestCase):
102   def test(self):
103     cds = "bbf46ea9a"
104     salt = "ee5ad9"
105     di = ComputeRemoteImportDiskInfo(cds, salt, 0, "node1", 1234)
106     self.assertEqual(CheckRemoteExportDiskInfo(cds, 0, di),
107                      ("node1", 1234))
108
109     for i in range(1, 100):
110       # Wrong disk index
111       self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
112                         cds, i, di)
113
114   def testCheckErrors(self):
115     cds = "0776450535a"
116     self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
117                       cds, 0, "")
118     self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
119                       cds, 0, ())
120     self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
121                       cds, 0, ("", 1, 2, 3, 4, 5))
122
123     # No host/port
124     self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
125                       cds, 0, ("", 0, "", ""))
126
127     # Wrong hash
128     self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
129                       cds, 0, ("nodeX", 123, "fakehash", "xyz"))
130
131
132 class TestFormatProgress(unittest.TestCase):
133   def test(self):
134     FormatProgress((0, 0, None, None))
135     FormatProgress((100, 3.3, 30, None))
136     FormatProgress((100, 3.3, 30, 900))
137
138     self.assertEqual(FormatProgress((1500, 12, 30, None)),
139                      "1.5G, 12.0 MiB/s, 30%")
140
141
142 if __name__ == "__main__":
143   testutils.GanetiTestProgram()