root / test / ganeti.masterd.instance_unittest.py @ 81f7ea25
History | View | Annotate | Download (5.7 kB)
1 | 033a1d00 | Michael Hanselmann | #!/usr/bin/python
|
---|---|---|---|
2 | 033a1d00 | Michael Hanselmann | #
|
3 | 033a1d00 | Michael Hanselmann | |
4 | 033a1d00 | Michael Hanselmann | # Copyright (C) 2010 Google Inc.
|
5 | 033a1d00 | Michael Hanselmann | #
|
6 | 033a1d00 | Michael Hanselmann | # This program is free software; you can redistribute it and/or modify
|
7 | 033a1d00 | Michael Hanselmann | # it under the terms of the GNU General Public License as published by
|
8 | 033a1d00 | Michael Hanselmann | # the Free Software Foundation; either version 2 of the License, or
|
9 | 033a1d00 | Michael Hanselmann | # (at your option) any later version.
|
10 | 033a1d00 | Michael Hanselmann | #
|
11 | 033a1d00 | Michael Hanselmann | # This program is distributed in the hope that it will be useful, but
|
12 | 033a1d00 | Michael Hanselmann | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 033a1d00 | Michael Hanselmann | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | 033a1d00 | Michael Hanselmann | # General Public License for more details.
|
15 | 033a1d00 | Michael Hanselmann | #
|
16 | 033a1d00 | Michael Hanselmann | # You should have received a copy of the GNU General Public License
|
17 | 033a1d00 | Michael Hanselmann | # along with this program; if not, write to the Free Software
|
18 | 033a1d00 | Michael Hanselmann | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | 033a1d00 | Michael Hanselmann | # 02110-1301, USA.
|
20 | 033a1d00 | Michael Hanselmann | |
21 | 033a1d00 | Michael Hanselmann | |
22 | 033a1d00 | Michael Hanselmann | """Script for testing ganeti.masterd.instance"""
|
23 | 033a1d00 | Michael Hanselmann | |
24 | 033a1d00 | Michael Hanselmann | import os |
25 | 033a1d00 | Michael Hanselmann | import sys |
26 | 033a1d00 | Michael Hanselmann | import unittest |
27 | 033a1d00 | Michael Hanselmann | |
28 | 1410fa8d | Michael Hanselmann | from ganeti import constants |
29 | 4a96f1d1 | Michael Hanselmann | from ganeti import errors |
30 | 033a1d00 | Michael Hanselmann | from ganeti import utils |
31 | 033a1d00 | Michael Hanselmann | from ganeti import masterd |
32 | 033a1d00 | Michael Hanselmann | |
33 | 033a1d00 | Michael Hanselmann | from ganeti.masterd.instance import \ |
34 | f8326fca | Andrea Spadaccini | ImportExportTimeouts, _DiskImportExportBase, \ |
35 | 4a96f1d1 | Michael Hanselmann | ComputeRemoteExportHandshake, CheckRemoteExportHandshake, \ |
36 | 1a2e7fe9 | Michael Hanselmann | ComputeRemoteImportDiskInfo, CheckRemoteExportDiskInfo, \ |
37 | 1a2e7fe9 | Michael Hanselmann | FormatProgress
|
38 | 033a1d00 | Michael Hanselmann | |
39 | 033a1d00 | Michael Hanselmann | import testutils |
40 | 033a1d00 | Michael Hanselmann | |
41 | 033a1d00 | Michael Hanselmann | |
42 | 033a1d00 | Michael Hanselmann | class TestMisc(unittest.TestCase): |
43 | 033a1d00 | Michael Hanselmann | def testTimeouts(self): |
44 | 033a1d00 | Michael Hanselmann | tmo = ImportExportTimeouts(0)
|
45 | 033a1d00 | Michael Hanselmann | self.assertEqual(tmo.connect, 0) |
46 | 033a1d00 | Michael Hanselmann | self.assertEqual(tmo.listen, ImportExportTimeouts.DEFAULT_LISTEN_TIMEOUT)
|
47 | 033a1d00 | Michael Hanselmann | self.assertEqual(tmo.ready, ImportExportTimeouts.DEFAULT_READY_TIMEOUT)
|
48 | 033a1d00 | Michael Hanselmann | self.assertEqual(tmo.error, ImportExportTimeouts.DEFAULT_ERROR_TIMEOUT)
|
49 | 1a2e7fe9 | Michael Hanselmann | self.assertEqual(tmo.progress,
|
50 | 1a2e7fe9 | Michael Hanselmann | ImportExportTimeouts.DEFAULT_PROGRESS_INTERVAL) |
51 | 033a1d00 | Michael Hanselmann | |
52 | 033a1d00 | Michael Hanselmann | tmo = ImportExportTimeouts(999)
|
53 | 033a1d00 | Michael Hanselmann | self.assertEqual(tmo.connect, 999) |
54 | 033a1d00 | Michael Hanselmann | |
55 | 1a2e7fe9 | Michael Hanselmann | tmo = ImportExportTimeouts(1, listen=2, error=3, ready=4, progress=5) |
56 | 1a2e7fe9 | Michael Hanselmann | self.assertEqual(tmo.connect, 1) |
57 | 1a2e7fe9 | Michael Hanselmann | self.assertEqual(tmo.listen, 2) |
58 | 1a2e7fe9 | Michael Hanselmann | self.assertEqual(tmo.error, 3) |
59 | 1a2e7fe9 | Michael Hanselmann | self.assertEqual(tmo.ready, 4) |
60 | 1a2e7fe9 | Michael Hanselmann | self.assertEqual(tmo.progress, 5) |
61 | 1a2e7fe9 | Michael Hanselmann | |
62 | 033a1d00 | Michael Hanselmann | def testTimeoutExpired(self): |
63 | f8326fca | Andrea Spadaccini | self.assert_(utils.TimeoutExpired(100, 300, _time_fn=lambda: 500)) |
64 | f8326fca | Andrea Spadaccini | self.assertFalse(utils.TimeoutExpired(100, 300, _time_fn=lambda: 0)) |
65 | f8326fca | Andrea Spadaccini | self.assertFalse(utils.TimeoutExpired(100, 300, _time_fn=lambda: 100)) |
66 | f8326fca | Andrea Spadaccini | self.assertFalse(utils.TimeoutExpired(100, 300, _time_fn=lambda: 400)) |
67 | 033a1d00 | Michael Hanselmann | |
68 | 033a1d00 | Michael Hanselmann | def testDiskImportExportBaseDirect(self): |
69 | 033a1d00 | Michael Hanselmann | self.assertRaises(AssertionError, _DiskImportExportBase, |
70 | 033a1d00 | Michael Hanselmann | None, None, None, None, None, None, None) |
71 | 033a1d00 | Michael Hanselmann | |
72 | 033a1d00 | Michael Hanselmann | |
73 | 1410fa8d | Michael Hanselmann | class TestRieHandshake(unittest.TestCase): |
74 | 1410fa8d | Michael Hanselmann | def test(self): |
75 | 1410fa8d | Michael Hanselmann | cds = "cd-secret"
|
76 | 1410fa8d | Michael Hanselmann | hs = ComputeRemoteExportHandshake(cds) |
77 | 1410fa8d | Michael Hanselmann | self.assertEqual(len(hs), 3) |
78 | 1410fa8d | Michael Hanselmann | self.assertEqual(hs[0], constants.RIE_VERSION) |
79 | 1410fa8d | Michael Hanselmann | |
80 | 1410fa8d | Michael Hanselmann | self.assertEqual(CheckRemoteExportHandshake(cds, hs), None) |
81 | 1410fa8d | Michael Hanselmann | |
82 | 1410fa8d | Michael Hanselmann | def testCheckErrors(self): |
83 | 1410fa8d | Michael Hanselmann | self.assert_(CheckRemoteExportHandshake(None, None)) |
84 | 1410fa8d | Michael Hanselmann | self.assert_(CheckRemoteExportHandshake("", "")) |
85 | 1410fa8d | Michael Hanselmann | self.assert_(CheckRemoteExportHandshake("", ("xyz", "foo"))) |
86 | 1410fa8d | Michael Hanselmann | |
87 | 1410fa8d | Michael Hanselmann | def testCheckWrongHash(self): |
88 | 1410fa8d | Michael Hanselmann | cds = "cd-secret999"
|
89 | 1410fa8d | Michael Hanselmann | self.assert_(CheckRemoteExportHandshake(cds, (0, "fakehash", "xyz"))) |
90 | 1410fa8d | Michael Hanselmann | |
91 | 1410fa8d | Michael Hanselmann | def testCheckWrongVersion(self): |
92 | 1410fa8d | Michael Hanselmann | version = 14887
|
93 | 1410fa8d | Michael Hanselmann | self.assertNotEqual(version, constants.RIE_VERSION)
|
94 | 1410fa8d | Michael Hanselmann | cds = "c28ac99"
|
95 | 1410fa8d | Michael Hanselmann | salt = "a19cf8cc06"
|
96 | 1410fa8d | Michael Hanselmann | msg = "%s:%s" % (version, constants.RIE_HANDSHAKE)
|
97 | 1410fa8d | Michael Hanselmann | hs = (version, utils.Sha1Hmac(cds, msg, salt=salt), salt) |
98 | 1410fa8d | Michael Hanselmann | self.assert_(CheckRemoteExportHandshake(cds, hs))
|
99 | 1410fa8d | Michael Hanselmann | |
100 | 1410fa8d | Michael Hanselmann | |
101 | 4a96f1d1 | Michael Hanselmann | class TestRieDiskInfo(unittest.TestCase): |
102 | 4a96f1d1 | Michael Hanselmann | def test(self): |
103 | 4a96f1d1 | Michael Hanselmann | cds = "bbf46ea9a"
|
104 | 4a96f1d1 | Michael Hanselmann | salt = "ee5ad9"
|
105 | d51ae04c | Michael Hanselmann | di = ComputeRemoteImportDiskInfo(cds, salt, 0, "node1", 1234, "mag111") |
106 | 4a96f1d1 | Michael Hanselmann | self.assertEqual(CheckRemoteExportDiskInfo(cds, 0, di), |
107 | d51ae04c | Michael Hanselmann | ("node1", 1234, "mag111")) |
108 | 4a96f1d1 | Michael Hanselmann | |
109 | 4a96f1d1 | Michael Hanselmann | for i in range(1, 100): |
110 | 4a96f1d1 | Michael Hanselmann | # Wrong disk index
|
111 | 4a96f1d1 | Michael Hanselmann | self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
|
112 | 4a96f1d1 | Michael Hanselmann | cds, i, di) |
113 | 4a96f1d1 | Michael Hanselmann | |
114 | acd65a16 | Michael Hanselmann | def testInvalidHostPort(self): |
115 | acd65a16 | Michael Hanselmann | cds = "3ZoJY8KtGJ"
|
116 | acd65a16 | Michael Hanselmann | salt = "drK5oYiHWD"
|
117 | acd65a16 | Michael Hanselmann | |
118 | acd65a16 | Michael Hanselmann | for host in [",", "...", "Hello World", "`", "!", "#", "\\"]: |
119 | d51ae04c | Michael Hanselmann | di = ComputeRemoteImportDiskInfo(cds, salt, 0, host, 1234, "magic") |
120 | acd65a16 | Michael Hanselmann | self.assertRaises(errors.OpPrereqError,
|
121 | acd65a16 | Michael Hanselmann | CheckRemoteExportDiskInfo, cds, 0, di)
|
122 | acd65a16 | Michael Hanselmann | |
123 | acd65a16 | Michael Hanselmann | for port in [-1, 792825908, "HelloWorld!", "`#", "\\\"", "_?_"]: |
124 | d51ae04c | Michael Hanselmann | di = ComputeRemoteImportDiskInfo(cds, salt, 0, "localhost", port, "magic") |
125 | acd65a16 | Michael Hanselmann | self.assertRaises(errors.OpPrereqError,
|
126 | acd65a16 | Michael Hanselmann | CheckRemoteExportDiskInfo, cds, 0, di)
|
127 | acd65a16 | Michael Hanselmann | |
128 | 4a96f1d1 | Michael Hanselmann | def testCheckErrors(self): |
129 | 4a96f1d1 | Michael Hanselmann | cds = "0776450535a"
|
130 | 4a96f1d1 | Michael Hanselmann | self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
|
131 | 4a96f1d1 | Michael Hanselmann | cds, 0, "") |
132 | 4a96f1d1 | Michael Hanselmann | self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
|
133 | 4a96f1d1 | Michael Hanselmann | cds, 0, ())
|
134 | 4a96f1d1 | Michael Hanselmann | self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
|
135 | 4a96f1d1 | Michael Hanselmann | cds, 0, ("", 1, 2, 3, 4, 5)) |
136 | 4a96f1d1 | Michael Hanselmann | |
137 | 4a96f1d1 | Michael Hanselmann | # No host/port
|
138 | 4a96f1d1 | Michael Hanselmann | self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
|
139 | d51ae04c | Michael Hanselmann | cds, 0, ("", 1234, "magic", "", "")) |
140 | d51ae04c | Michael Hanselmann | self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
|
141 | d51ae04c | Michael Hanselmann | cds, 0, ("host", 0, "magic", "", "")) |
142 | d51ae04c | Michael Hanselmann | self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
|
143 | d51ae04c | Michael Hanselmann | cds, 0, ("host", 1234, "", "", "")) |
144 | 4a96f1d1 | Michael Hanselmann | |
145 | 4a96f1d1 | Michael Hanselmann | # Wrong hash
|
146 | 4a96f1d1 | Michael Hanselmann | self.assertRaises(errors.GenericError, CheckRemoteExportDiskInfo,
|
147 | d51ae04c | Michael Hanselmann | cds, 0, ("nodeX", 123, "magic", "fakehash", "xyz")) |
148 | 4a96f1d1 | Michael Hanselmann | |
149 | 4a96f1d1 | Michael Hanselmann | |
150 | 1a2e7fe9 | Michael Hanselmann | class TestFormatProgress(unittest.TestCase): |
151 | 1a2e7fe9 | Michael Hanselmann | def test(self): |
152 | 1a2e7fe9 | Michael Hanselmann | FormatProgress((0, 0, None, None)) |
153 | 1a2e7fe9 | Michael Hanselmann | FormatProgress((100, 3.3, 30, None)) |
154 | 1a2e7fe9 | Michael Hanselmann | FormatProgress((100, 3.3, 30, 900)) |
155 | 1a2e7fe9 | Michael Hanselmann | |
156 | 1a2e7fe9 | Michael Hanselmann | self.assertEqual(FormatProgress((1500, 12, 30, None)), |
157 | 1a2e7fe9 | Michael Hanselmann | "1.5G, 12.0 MiB/s, 30%")
|
158 | 1a2e7fe9 | Michael Hanselmann | |
159 | 1a2e7fe9 | Michael Hanselmann | |
160 | 033a1d00 | Michael Hanselmann | if __name__ == "__main__": |
161 | 033a1d00 | Michael Hanselmann | testutils.GanetiTestProgram() |