root / test / ganeti.backend_unittest.py @ 53dbf14c
History | View | Annotate | Download (3.3 kB)
1 | f942a838 | Michael Hanselmann | #!/usr/bin/python
|
---|---|---|---|
2 | f942a838 | Michael Hanselmann | #
|
3 | f942a838 | Michael Hanselmann | |
4 | f942a838 | Michael Hanselmann | # Copyright (C) 2010 Google Inc.
|
5 | f942a838 | Michael Hanselmann | #
|
6 | f942a838 | Michael Hanselmann | # This program is free software; you can redistribute it and/or modify
|
7 | f942a838 | Michael Hanselmann | # it under the terms of the GNU General Public License as published by
|
8 | f942a838 | Michael Hanselmann | # the Free Software Foundation; either version 2 of the License, or
|
9 | f942a838 | Michael Hanselmann | # (at your option) any later version.
|
10 | f942a838 | Michael Hanselmann | #
|
11 | f942a838 | Michael Hanselmann | # This program is distributed in the hope that it will be useful, but
|
12 | f942a838 | Michael Hanselmann | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | f942a838 | Michael Hanselmann | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | f942a838 | Michael Hanselmann | # General Public License for more details.
|
15 | f942a838 | Michael Hanselmann | #
|
16 | f942a838 | Michael Hanselmann | # You should have received a copy of the GNU General Public License
|
17 | f942a838 | Michael Hanselmann | # along with this program; if not, write to the Free Software
|
18 | f942a838 | Michael Hanselmann | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | f942a838 | Michael Hanselmann | # 02110-1301, USA.
|
20 | f942a838 | Michael Hanselmann | |
21 | f942a838 | Michael Hanselmann | |
22 | f942a838 | Michael Hanselmann | """Script for testing ganeti.backend"""
|
23 | f942a838 | Michael Hanselmann | |
24 | f942a838 | Michael Hanselmann | import os |
25 | f942a838 | Michael Hanselmann | import sys |
26 | f942a838 | Michael Hanselmann | import shutil |
27 | f942a838 | Michael Hanselmann | import tempfile |
28 | f942a838 | Michael Hanselmann | import unittest |
29 | f942a838 | Michael Hanselmann | |
30 | f942a838 | Michael Hanselmann | from ganeti import utils |
31 | 31ff0247 | Michael Hanselmann | from ganeti import constants |
32 | f942a838 | Michael Hanselmann | from ganeti import backend |
33 | f942a838 | Michael Hanselmann | |
34 | f942a838 | Michael Hanselmann | import testutils |
35 | f942a838 | Michael Hanselmann | |
36 | f942a838 | Michael Hanselmann | |
37 | f942a838 | Michael Hanselmann | class TestX509Certificates(unittest.TestCase): |
38 | f942a838 | Michael Hanselmann | def setUp(self): |
39 | f942a838 | Michael Hanselmann | self.tmpdir = tempfile.mkdtemp()
|
40 | f942a838 | Michael Hanselmann | |
41 | f942a838 | Michael Hanselmann | def tearDown(self): |
42 | f942a838 | Michael Hanselmann | shutil.rmtree(self.tmpdir)
|
43 | f942a838 | Michael Hanselmann | |
44 | f942a838 | Michael Hanselmann | def test(self): |
45 | f942a838 | Michael Hanselmann | (name, cert_pem) = backend.CreateX509Certificate(300, cryptodir=self.tmpdir) |
46 | f942a838 | Michael Hanselmann | |
47 | f942a838 | Michael Hanselmann | self.assertEqual(utils.ReadFile(os.path.join(self.tmpdir, name, |
48 | f942a838 | Michael Hanselmann | backend._X509_CERT_FILE)), |
49 | f942a838 | Michael Hanselmann | cert_pem) |
50 | f942a838 | Michael Hanselmann | self.assert_(0 < os.path.getsize(os.path.join(self.tmpdir, name, |
51 | f942a838 | Michael Hanselmann | backend._X509_KEY_FILE))) |
52 | f942a838 | Michael Hanselmann | |
53 | f942a838 | Michael Hanselmann | (name2, cert_pem2) = \ |
54 | f942a838 | Michael Hanselmann | backend.CreateX509Certificate(300, cryptodir=self.tmpdir) |
55 | f942a838 | Michael Hanselmann | |
56 | f942a838 | Michael Hanselmann | backend.RemoveX509Certificate(name, cryptodir=self.tmpdir)
|
57 | f942a838 | Michael Hanselmann | backend.RemoveX509Certificate(name2, cryptodir=self.tmpdir)
|
58 | f942a838 | Michael Hanselmann | |
59 | f942a838 | Michael Hanselmann | self.assertEqual(utils.ListVisibleFiles(self.tmpdir), []) |
60 | f942a838 | Michael Hanselmann | |
61 | f942a838 | Michael Hanselmann | def testNonEmpty(self): |
62 | f942a838 | Michael Hanselmann | (name, _) = backend.CreateX509Certificate(300, cryptodir=self.tmpdir) |
63 | f942a838 | Michael Hanselmann | |
64 | f942a838 | Michael Hanselmann | utils.WriteFile(utils.PathJoin(self.tmpdir, name, "hello-world"), |
65 | f942a838 | Michael Hanselmann | data="Hello World")
|
66 | f942a838 | Michael Hanselmann | |
67 | f942a838 | Michael Hanselmann | self.assertRaises(backend.RPCFail, backend.RemoveX509Certificate,
|
68 | f942a838 | Michael Hanselmann | name, cryptodir=self.tmpdir)
|
69 | f942a838 | Michael Hanselmann | |
70 | f942a838 | Michael Hanselmann | self.assertEqual(utils.ListVisibleFiles(self.tmpdir), [name]) |
71 | f942a838 | Michael Hanselmann | |
72 | f942a838 | Michael Hanselmann | |
73 | a3a5f850 | Iustin Pop | class TestNodeVerify(testutils.GanetiTestCase): |
74 | a3a5f850 | Iustin Pop | def testMasterIPLocalhost(self): |
75 | a3a5f850 | Iustin Pop | # this a real functional test, but requires localhost to be reachable
|
76 | a3a5f850 | Iustin Pop | local_data = (utils.HostInfo().name, constants.LOCALHOST_IP_ADDRESS) |
77 | a3a5f850 | Iustin Pop | result = backend.VerifyNode({constants.NV_MASTERIP: local_data}, None)
|
78 | a3a5f850 | Iustin Pop | self.failUnless(constants.NV_MASTERIP in result, |
79 | a3a5f850 | Iustin Pop | "Master IP data not returned")
|
80 | a3a5f850 | Iustin Pop | self.failUnless(result[constants.NV_MASTERIP], "Cannot reach localhost") |
81 | a3a5f850 | Iustin Pop | |
82 | a3a5f850 | Iustin Pop | def testMasterIPUnreachable(self): |
83 | a3a5f850 | Iustin Pop | # Network 192.0.2.0/24 is reserved for test/documentation as per
|
84 | a3a5f850 | Iustin Pop | # RFC 5735
|
85 | a3a5f850 | Iustin Pop | bad_data = ("master.example.com", "192.0.2.1") |
86 | a3a5f850 | Iustin Pop | # we just test that whatever TcpPing returns, VerifyNode returns too
|
87 | a3a5f850 | Iustin Pop | utils.TcpPing = lambda a, b, source=None: False |
88 | a3a5f850 | Iustin Pop | result = backend.VerifyNode({constants.NV_MASTERIP: bad_data}, None)
|
89 | a3a5f850 | Iustin Pop | self.failUnless(constants.NV_MASTERIP in result, |
90 | a3a5f850 | Iustin Pop | "Master IP data not returned")
|
91 | a3a5f850 | Iustin Pop | self.failIf(result[constants.NV_MASTERIP],
|
92 | a3a5f850 | Iustin Pop | "Result from utils.TcpPing corrupted")
|
93 | a3a5f850 | Iustin Pop | |
94 | a3a5f850 | Iustin Pop | |
95 | f942a838 | Michael Hanselmann | if __name__ == "__main__": |
96 | f942a838 | Michael Hanselmann | testutils.GanetiTestProgram() |