Add RPC calls to create and remove X509 certificates
[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 backend
32
33 import testutils
34
35
36 class TestX509Certificates(unittest.TestCase):
37   def setUp(self):
38     self.tmpdir = tempfile.mkdtemp()
39
40   def tearDown(self):
41     shutil.rmtree(self.tmpdir)
42
43   def test(self):
44     (name, cert_pem) = backend.CreateX509Certificate(300, cryptodir=self.tmpdir)
45
46     self.assertEqual(utils.ReadFile(os.path.join(self.tmpdir, name,
47                                                  backend._X509_CERT_FILE)),
48                      cert_pem)
49     self.assert_(0 < os.path.getsize(os.path.join(self.tmpdir, name,
50                                                   backend._X509_KEY_FILE)))
51
52     (name2, cert_pem2) = \
53       backend.CreateX509Certificate(300, cryptodir=self.tmpdir)
54
55     backend.RemoveX509Certificate(name, cryptodir=self.tmpdir)
56     backend.RemoveX509Certificate(name2, cryptodir=self.tmpdir)
57
58     self.assertEqual(utils.ListVisibleFiles(self.tmpdir), [])
59
60   def testNonEmpty(self):
61     (name, _) = backend.CreateX509Certificate(300, cryptodir=self.tmpdir)
62
63     utils.WriteFile(utils.PathJoin(self.tmpdir, name, "hello-world"),
64                     data="Hello World")
65
66     self.assertRaises(backend.RPCFail, backend.RemoveX509Certificate,
67                       name, cryptodir=self.tmpdir)
68
69     self.assertEqual(utils.ListVisibleFiles(self.tmpdir), [name])
70
71
72 if __name__ == "__main__":
73   testutils.GanetiTestProgram()