4 # Copyright (C) 2010 Google Inc.
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.
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.
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
22 """Script for testing ganeti.backend"""
30 from ganeti import utils
31 from ganeti import constants
32 from ganeti import backend
33 from ganeti import netutils
38 class TestX509Certificates(unittest.TestCase):
40 self.tmpdir = tempfile.mkdtemp()
43 shutil.rmtree(self.tmpdir)
46 (name, cert_pem) = backend.CreateX509Certificate(300, cryptodir=self.tmpdir)
48 self.assertEqual(utils.ReadFile(os.path.join(self.tmpdir, name,
49 backend._X509_CERT_FILE)),
51 self.assert_(0 < os.path.getsize(os.path.join(self.tmpdir, name,
52 backend._X509_KEY_FILE)))
54 (name2, cert_pem2) = \
55 backend.CreateX509Certificate(300, cryptodir=self.tmpdir)
57 backend.RemoveX509Certificate(name, cryptodir=self.tmpdir)
58 backend.RemoveX509Certificate(name2, cryptodir=self.tmpdir)
60 self.assertEqual(utils.ListVisibleFiles(self.tmpdir), [])
62 def testNonEmpty(self):
63 (name, _) = backend.CreateX509Certificate(300, cryptodir=self.tmpdir)
65 utils.WriteFile(utils.PathJoin(self.tmpdir, name, "hello-world"),
68 self.assertRaises(backend.RPCFail, backend.RemoveX509Certificate,
69 name, cryptodir=self.tmpdir)
71 self.assertEqual(utils.ListVisibleFiles(self.tmpdir), [name])
74 class TestNodeVerify(testutils.GanetiTestCase):
75 def testMasterIPLocalhost(self):
76 # this a real functional test, but requires localhost to be reachable
77 local_data = (netutils.Hostname.GetSysName(),
78 constants.IP4_ADDRESS_LOCALHOST)
79 result = backend.VerifyNode({constants.NV_MASTERIP: local_data}, None)
80 self.failUnless(constants.NV_MASTERIP in result,
81 "Master IP data not returned")
82 self.failUnless(result[constants.NV_MASTERIP], "Cannot reach localhost")
84 def testMasterIPUnreachable(self):
85 # Network 192.0.2.0/24 is reserved for test/documentation as per
87 bad_data = ("master.example.com", "192.0.2.1")
88 # we just test that whatever TcpPing returns, VerifyNode returns too
89 netutils.TcpPing = lambda a, b, source=None: False
90 result = backend.VerifyNode({constants.NV_MASTERIP: bad_data}, None)
91 self.failUnless(constants.NV_MASTERIP in result,
92 "Master IP data not returned")
93 self.failIf(result[constants.NV_MASTERIP],
94 "Result from netutils.TcpPing corrupted")
97 if __name__ == "__main__":
98 testutils.GanetiTestProgram()