root / test / ganeti.backend_unittest.py @ 981732fb
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 | a744b676 | Manuel Franceschini | from ganeti import netutils |
34 | f942a838 | Michael Hanselmann | |
35 | f942a838 | Michael Hanselmann | import testutils |
36 | f942a838 | Michael Hanselmann | |
37 | f942a838 | Michael Hanselmann | |
38 | f942a838 | Michael Hanselmann | class TestX509Certificates(unittest.TestCase): |
39 | f942a838 | Michael Hanselmann | def setUp(self): |
40 | f942a838 | Michael Hanselmann | self.tmpdir = tempfile.mkdtemp()
|
41 | f942a838 | Michael Hanselmann | |
42 | f942a838 | Michael Hanselmann | def tearDown(self): |
43 | f942a838 | Michael Hanselmann | shutil.rmtree(self.tmpdir)
|
44 | f942a838 | Michael Hanselmann | |
45 | f942a838 | Michael Hanselmann | def test(self): |
46 | f942a838 | Michael Hanselmann | (name, cert_pem) = backend.CreateX509Certificate(300, cryptodir=self.tmpdir) |
47 | f942a838 | Michael Hanselmann | |
48 | f942a838 | Michael Hanselmann | self.assertEqual(utils.ReadFile(os.path.join(self.tmpdir, name, |
49 | f942a838 | Michael Hanselmann | backend._X509_CERT_FILE)), |
50 | f942a838 | Michael Hanselmann | cert_pem) |
51 | f942a838 | Michael Hanselmann | self.assert_(0 < os.path.getsize(os.path.join(self.tmpdir, name, |
52 | f942a838 | Michael Hanselmann | backend._X509_KEY_FILE))) |
53 | f942a838 | Michael Hanselmann | |
54 | f942a838 | Michael Hanselmann | (name2, cert_pem2) = \ |
55 | f942a838 | Michael Hanselmann | backend.CreateX509Certificate(300, cryptodir=self.tmpdir) |
56 | f942a838 | Michael Hanselmann | |
57 | f942a838 | Michael Hanselmann | backend.RemoveX509Certificate(name, cryptodir=self.tmpdir)
|
58 | f942a838 | Michael Hanselmann | backend.RemoveX509Certificate(name2, cryptodir=self.tmpdir)
|
59 | f942a838 | Michael Hanselmann | |
60 | f942a838 | Michael Hanselmann | self.assertEqual(utils.ListVisibleFiles(self.tmpdir), []) |
61 | f942a838 | Michael Hanselmann | |
62 | f942a838 | Michael Hanselmann | def testNonEmpty(self): |
63 | f942a838 | Michael Hanselmann | (name, _) = backend.CreateX509Certificate(300, cryptodir=self.tmpdir) |
64 | f942a838 | Michael Hanselmann | |
65 | f942a838 | Michael Hanselmann | utils.WriteFile(utils.PathJoin(self.tmpdir, name, "hello-world"), |
66 | f942a838 | Michael Hanselmann | data="Hello World")
|
67 | f942a838 | Michael Hanselmann | |
68 | f942a838 | Michael Hanselmann | self.assertRaises(backend.RPCFail, backend.RemoveX509Certificate,
|
69 | f942a838 | Michael Hanselmann | name, cryptodir=self.tmpdir)
|
70 | f942a838 | Michael Hanselmann | |
71 | f942a838 | Michael Hanselmann | self.assertEqual(utils.ListVisibleFiles(self.tmpdir), [name]) |
72 | f942a838 | Michael Hanselmann | |
73 | f942a838 | Michael Hanselmann | |
74 | a3a5f850 | Iustin Pop | class TestNodeVerify(testutils.GanetiTestCase): |
75 | a3a5f850 | Iustin Pop | def testMasterIPLocalhost(self): |
76 | a3a5f850 | Iustin Pop | # this a real functional test, but requires localhost to be reachable
|
77 | b705c7a6 | Manuel Franceschini | local_data = (netutils.Hostname.GetSysName(), |
78 | b705c7a6 | Manuel Franceschini | constants.IP4_ADDRESS_LOCALHOST) |
79 | a3a5f850 | Iustin Pop | result = backend.VerifyNode({constants.NV_MASTERIP: local_data}, None)
|
80 | a3a5f850 | Iustin Pop | self.failUnless(constants.NV_MASTERIP in result, |
81 | a3a5f850 | Iustin Pop | "Master IP data not returned")
|
82 | a3a5f850 | Iustin Pop | self.failUnless(result[constants.NV_MASTERIP], "Cannot reach localhost") |
83 | a3a5f850 | Iustin Pop | |
84 | a3a5f850 | Iustin Pop | def testMasterIPUnreachable(self): |
85 | a3a5f850 | Iustin Pop | # Network 192.0.2.0/24 is reserved for test/documentation as per
|
86 | 926feaf1 | Manuel Franceschini | # RFC 5737
|
87 | a3a5f850 | Iustin Pop | bad_data = ("master.example.com", "192.0.2.1") |
88 | a3a5f850 | Iustin Pop | # we just test that whatever TcpPing returns, VerifyNode returns too
|
89 | a744b676 | Manuel Franceschini | netutils.TcpPing = lambda a, b, source=None: False |
90 | a3a5f850 | Iustin Pop | result = backend.VerifyNode({constants.NV_MASTERIP: bad_data}, None)
|
91 | a3a5f850 | Iustin Pop | self.failUnless(constants.NV_MASTERIP in result, |
92 | a3a5f850 | Iustin Pop | "Master IP data not returned")
|
93 | a3a5f850 | Iustin Pop | self.failIf(result[constants.NV_MASTERIP],
|
94 | a744b676 | Manuel Franceschini | "Result from netutils.TcpPing corrupted")
|
95 | a3a5f850 | Iustin Pop | |
96 | a3a5f850 | Iustin Pop | |
97 | f942a838 | Michael Hanselmann | if __name__ == "__main__": |
98 | f942a838 | Michael Hanselmann | testutils.GanetiTestProgram() |