root / test / py / ganeti.utils.security_unittest.py @ b544a3c2
History | View | Annotate | Download (3.1 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 unittest |
26 |
|
27 |
from ganeti.utils import security |
28 |
|
29 |
import testutils |
30 |
|
31 |
|
32 |
class TestCandidateCerts(unittest.TestCase): |
33 |
|
34 |
def setUp(self): |
35 |
self._warn_fn = mock.Mock()
|
36 |
self._info_fn = mock.Mock()
|
37 |
self._candidate_certs = {}
|
38 |
|
39 |
def testAddAndRemoveCerts(self): |
40 |
self.assertEqual(0, len(self._candidate_certs)) |
41 |
|
42 |
node_uuid = "1234"
|
43 |
cert_digest = "foobar"
|
44 |
security.AddNodeToCandidateCerts(node_uuid, cert_digest, |
45 |
self._candidate_certs, warn_fn=self._warn_fn, info_fn=self._info_fn) |
46 |
self.assertEqual(1, len(self._candidate_certs)) |
47 |
|
48 |
# Try adding the same cert again
|
49 |
security.AddNodeToCandidateCerts(node_uuid, cert_digest, |
50 |
self._candidate_certs, warn_fn=self._warn_fn, info_fn=self._info_fn) |
51 |
self.assertEqual(1, len(self._candidate_certs)) |
52 |
self.assertTrue(self._candidate_certs[node_uuid] == cert_digest) |
53 |
|
54 |
# Overriding cert
|
55 |
other_digest = "barfoo"
|
56 |
security.AddNodeToCandidateCerts(node_uuid, other_digest, |
57 |
self._candidate_certs, warn_fn=self._warn_fn, info_fn=self._info_fn) |
58 |
self.assertEqual(1, len(self._candidate_certs)) |
59 |
self.assertTrue(self._candidate_certs[node_uuid] == other_digest) |
60 |
|
61 |
# Try removing a certificate from a node that is not in the list
|
62 |
other_node_uuid = "5678"
|
63 |
security.RemoveNodeFromCandidateCerts( |
64 |
other_node_uuid, self._candidate_certs, warn_fn=self._warn_fn) |
65 |
self.assertEqual(1, len(self._candidate_certs)) |
66 |
|
67 |
# Remove a certificate from a node that is in the list
|
68 |
security.RemoveNodeFromCandidateCerts( |
69 |
node_uuid, self._candidate_certs, warn_fn=self._warn_fn) |
70 |
self.assertEqual(0, len(self._candidate_certs)) |
71 |
|
72 |
|
73 |
class TestGetCertificateDigest(testutils.GanetiTestCase): |
74 |
|
75 |
def setUp(self): |
76 |
testutils.GanetiTestCase.setUp(self)
|
77 |
# certificate file that contains the certificate only
|
78 |
self._certfilename1 = testutils.TestDataFilename("cert1.pem") |
79 |
# (different) certificate file that contains both, certificate
|
80 |
# and private key
|
81 |
self._certfilename2 = testutils.TestDataFilename("cert2.pem") |
82 |
|
83 |
def testGetCertificateDigest(self): |
84 |
digest1 = security.GetClientCertificateDigest( |
85 |
cert_filename=self._certfilename1)
|
86 |
digest2 = security.GetClientCertificateDigest( |
87 |
cert_filename=self._certfilename2)
|
88 |
self.assertFalse(digest1 == digest2)
|
89 |
|
90 |
|
91 |
if __name__ == "__main__": |
92 |
testutils.GanetiTestProgram() |