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