verify-disks: Explicitely state nothing has to be done
[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, _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_(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))
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, "mag111")
106     self.assertEqual(CheckRemoteExportDiskInfo(cds, 0, di),
107                      ("node1", 1234, "mag111"))
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 testInvalidHostPort(self):
115     cds = "3ZoJY8KtGJ"
116     salt = "drK5oYiHWD"
117
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)
122
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)
127
128   def testCheckErrors(self):
129     cds = "0776450535a"
130     self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
131                       cds, 0, "")
132     self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
133                       cds, 0, ())
134     self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
135                       cds, 0, ("", 1, 2, 3, 4, 5))
136
137     # No host/port
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, "", "", ""))
144
145     # Wrong hash
146     self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
147                       cds, 0, ("nodeX", 123, "magic", "fakehash", "xyz"))
148
149
150 class TestFormatProgress(unittest.TestCase):
151   def test(self):
152     FormatProgress((0, 0, None, None))
153     FormatProgress((100, 3.3, 30, None))
154     FormatProgress((100, 3.3, 30, 900))
155
156     self.assertEqual(FormatProgress((1500, 12, 30, None)),
157                      "1.5G, 12.0 MiB/s, 30%")
158
159
160 if __name__ == "__main__":
161   testutils.GanetiTestProgram()