Merge branch 'stable-2.6-esi' into stable-2.6-ippool-hotplug-esi
[ganeti-local] / test / ganeti.backend_unittest.py
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 from ganeti import netutils
34
35 import testutils
36
37
38 class TestX509Certificates(unittest.TestCase):
39   def setUp(self):
40     self.tmpdir = tempfile.mkdtemp()
41
42   def tearDown(self):
43     shutil.rmtree(self.tmpdir)
44
45   def test(self):
46     (name, cert_pem) = backend.CreateX509Certificate(300, cryptodir=self.tmpdir)
47
48     self.assertEqual(utils.ReadFile(os.path.join(self.tmpdir, name,
49                                                  backend._X509_CERT_FILE)),
50                      cert_pem)
51     self.assert_(0 < os.path.getsize(os.path.join(self.tmpdir, name,
52                                                   backend._X509_KEY_FILE)))
53
54     (name2, cert_pem2) = \
55       backend.CreateX509Certificate(300, cryptodir=self.tmpdir)
56
57     backend.RemoveX509Certificate(name, cryptodir=self.tmpdir)
58     backend.RemoveX509Certificate(name2, cryptodir=self.tmpdir)
59
60     self.assertEqual(utils.ListVisibleFiles(self.tmpdir), [])
61
62   def testNonEmpty(self):
63     (name, _) = backend.CreateX509Certificate(300, cryptodir=self.tmpdir)
64
65     utils.WriteFile(utils.PathJoin(self.tmpdir, name, "hello-world"),
66                     data="Hello World")
67
68     self.assertRaises(backend.RPCFail, backend.RemoveX509Certificate,
69                       name, cryptodir=self.tmpdir)
70
71     self.assertEqual(utils.ListVisibleFiles(self.tmpdir), [name])
72
73
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")
83
84   def testMasterIPUnreachable(self):
85     # Network 192.0.2.0/24 is reserved for test/documentation as per
86     # RFC 5737
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")
95
96
97 if __name__ == "__main__":
98   testutils.GanetiTestProgram()