root / test / py / ganeti.utils.security_unittest.py @ 560ef132
History | View | Annotate | Download (3.9 kB)
1 |
#!/usr/bin/python
|
---|---|
2 |
#
|
3 |
|
4 |
# Copyright (C) 2013 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 unittesting the ganeti.utils.storage module"""
|
23 |
|
24 |
import mock |
25 |
import os |
26 |
import shutil |
27 |
import tempfile |
28 |
import unittest |
29 |
|
30 |
from ganeti import constants |
31 |
from ganeti.utils import security |
32 |
|
33 |
import testutils |
34 |
|
35 |
|
36 |
class TestCandidateCerts(unittest.TestCase): |
37 |
|
38 |
def setUp(self): |
39 |
self._warn_fn = mock.Mock()
|
40 |
self._info_fn = mock.Mock()
|
41 |
self._candidate_certs = {}
|
42 |
|
43 |
def testAddAndRemoveCerts(self): |
44 |
self.assertEqual(0, len(self._candidate_certs)) |
45 |
|
46 |
node_uuid = "1234"
|
47 |
cert_digest = "foobar"
|
48 |
security.AddNodeToCandidateCerts(node_uuid, cert_digest, |
49 |
self._candidate_certs, warn_fn=self._warn_fn, info_fn=self._info_fn) |
50 |
self.assertEqual(1, len(self._candidate_certs)) |
51 |
|
52 |
# Try adding the same cert again
|
53 |
security.AddNodeToCandidateCerts(node_uuid, cert_digest, |
54 |
self._candidate_certs, warn_fn=self._warn_fn, info_fn=self._info_fn) |
55 |
self.assertEqual(1, len(self._candidate_certs)) |
56 |
self.assertTrue(self._candidate_certs[node_uuid] == cert_digest) |
57 |
|
58 |
# Overriding cert
|
59 |
other_digest = "barfoo"
|
60 |
security.AddNodeToCandidateCerts(node_uuid, other_digest, |
61 |
self._candidate_certs, warn_fn=self._warn_fn, info_fn=self._info_fn) |
62 |
self.assertEqual(1, len(self._candidate_certs)) |
63 |
self.assertTrue(self._candidate_certs[node_uuid] == other_digest) |
64 |
|
65 |
# Try removing a certificate from a node that is not in the list
|
66 |
other_node_uuid = "5678"
|
67 |
security.RemoveNodeFromCandidateCerts( |
68 |
other_node_uuid, self._candidate_certs, warn_fn=self._warn_fn) |
69 |
self.assertEqual(1, len(self._candidate_certs)) |
70 |
|
71 |
# Remove a certificate from a node that is in the list
|
72 |
security.RemoveNodeFromCandidateCerts( |
73 |
node_uuid, self._candidate_certs, warn_fn=self._warn_fn) |
74 |
self.assertEqual(0, len(self._candidate_certs)) |
75 |
|
76 |
|
77 |
class TestGetCertificateDigest(testutils.GanetiTestCase): |
78 |
|
79 |
def setUp(self): |
80 |
testutils.GanetiTestCase.setUp(self)
|
81 |
# certificate file that contains the certificate only
|
82 |
self._certfilename1 = testutils.TestDataFilename("cert1.pem") |
83 |
# (different) certificate file that contains both, certificate
|
84 |
# and private key
|
85 |
self._certfilename2 = testutils.TestDataFilename("cert2.pem") |
86 |
|
87 |
def testGetCertificateDigest(self): |
88 |
digest1 = security.GetCertificateDigest( |
89 |
cert_filename=self._certfilename1)
|
90 |
digest2 = security.GetCertificateDigest( |
91 |
cert_filename=self._certfilename2)
|
92 |
self.assertFalse(digest1 == digest2)
|
93 |
|
94 |
|
95 |
class TestCertVerification(testutils.GanetiTestCase): |
96 |
def setUp(self): |
97 |
testutils.GanetiTestCase.setUp(self)
|
98 |
|
99 |
self.tmpdir = tempfile.mkdtemp()
|
100 |
|
101 |
def tearDown(self): |
102 |
shutil.rmtree(self.tmpdir)
|
103 |
|
104 |
def testVerifyCertificate(self): |
105 |
security.VerifyCertificate(testutils.TestDataFilename("cert1.pem"))
|
106 |
|
107 |
nonexist_filename = os.path.join(self.tmpdir, "does-not-exist") |
108 |
|
109 |
(errcode, msg) = security.VerifyCertificate(nonexist_filename) |
110 |
self.assertEqual(errcode, constants.CV_ERROR)
|
111 |
|
112 |
# Try to load non-certificate file
|
113 |
invalid_cert = testutils.TestDataFilename("bdev-net.txt")
|
114 |
(errcode, msg) = security.VerifyCertificate(invalid_cert) |
115 |
self.assertEqual(errcode, constants.CV_ERROR)
|
116 |
|
117 |
|
118 |
if __name__ == "__main__": |
119 |
testutils.GanetiTestProgram() |