root / test / py / ganeti.utils.security_unittest.py @ b544a3c2
History | View | Annotate | Download (3.1 kB)
1 | 3338a9ce | Helga Velroyen | #!/usr/bin/python
|
---|---|---|---|
2 | 3338a9ce | Helga Velroyen | #
|
3 | 3338a9ce | Helga Velroyen | |
4 | 3338a9ce | Helga Velroyen | # Copyright (C) 2013 Google Inc.
|
5 | 3338a9ce | Helga Velroyen | #
|
6 | 3338a9ce | Helga Velroyen | # This program is free software; you can redistribute it and/or modify
|
7 | 3338a9ce | Helga Velroyen | # it under the terms of the GNU General Public License as published by
|
8 | 3338a9ce | Helga Velroyen | # the Free Software Foundation; either version 2 of the License, or
|
9 | 3338a9ce | Helga Velroyen | # (at your option) any later version.
|
10 | 3338a9ce | Helga Velroyen | #
|
11 | 3338a9ce | Helga Velroyen | # This program is distributed in the hope that it will be useful, but
|
12 | 3338a9ce | Helga Velroyen | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 3338a9ce | Helga Velroyen | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | 3338a9ce | Helga Velroyen | # General Public License for more details.
|
15 | 3338a9ce | Helga Velroyen | #
|
16 | 3338a9ce | Helga Velroyen | # You should have received a copy of the GNU General Public License
|
17 | 3338a9ce | Helga Velroyen | # along with this program; if not, write to the Free Software
|
18 | 3338a9ce | Helga Velroyen | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | 3338a9ce | Helga Velroyen | # 02110-1301, USA.
|
20 | 3338a9ce | Helga Velroyen | |
21 | 3338a9ce | Helga Velroyen | |
22 | 3338a9ce | Helga Velroyen | """Script for unittesting the ganeti.utils.storage module"""
|
23 | 3338a9ce | Helga Velroyen | |
24 | 3338a9ce | Helga Velroyen | import mock |
25 | 3338a9ce | Helga Velroyen | import unittest |
26 | 3338a9ce | Helga Velroyen | |
27 | 3338a9ce | Helga Velroyen | from ganeti.utils import security |
28 | 3338a9ce | Helga Velroyen | |
29 | 3338a9ce | Helga Velroyen | import testutils |
30 | 3338a9ce | Helga Velroyen | |
31 | 3338a9ce | Helga Velroyen | |
32 | 3338a9ce | Helga Velroyen | class TestCandidateCerts(unittest.TestCase): |
33 | 3338a9ce | Helga Velroyen | |
34 | 3338a9ce | Helga Velroyen | def setUp(self): |
35 | 3338a9ce | Helga Velroyen | self._warn_fn = mock.Mock()
|
36 | 3338a9ce | Helga Velroyen | self._info_fn = mock.Mock()
|
37 | 3338a9ce | Helga Velroyen | self._candidate_certs = {}
|
38 | 3338a9ce | Helga Velroyen | |
39 | 3338a9ce | Helga Velroyen | def testAddAndRemoveCerts(self): |
40 | 3338a9ce | Helga Velroyen | self.assertEqual(0, len(self._candidate_certs)) |
41 | 3338a9ce | Helga Velroyen | |
42 | 3338a9ce | Helga Velroyen | node_uuid = "1234"
|
43 | 3338a9ce | Helga Velroyen | cert_digest = "foobar"
|
44 | 3338a9ce | Helga Velroyen | security.AddNodeToCandidateCerts(node_uuid, cert_digest, |
45 | 3338a9ce | Helga Velroyen | self._candidate_certs, warn_fn=self._warn_fn, info_fn=self._info_fn) |
46 | 3338a9ce | Helga Velroyen | self.assertEqual(1, len(self._candidate_certs)) |
47 | 3338a9ce | Helga Velroyen | |
48 | 3338a9ce | Helga Velroyen | # Try adding the same cert again
|
49 | 3338a9ce | Helga Velroyen | security.AddNodeToCandidateCerts(node_uuid, cert_digest, |
50 | 3338a9ce | Helga Velroyen | self._candidate_certs, warn_fn=self._warn_fn, info_fn=self._info_fn) |
51 | 3338a9ce | Helga Velroyen | self.assertEqual(1, len(self._candidate_certs)) |
52 | 3338a9ce | Helga Velroyen | self.assertTrue(self._candidate_certs[node_uuid] == cert_digest) |
53 | 3338a9ce | Helga Velroyen | |
54 | 3338a9ce | Helga Velroyen | # Overriding cert
|
55 | 3338a9ce | Helga Velroyen | other_digest = "barfoo"
|
56 | 3338a9ce | Helga Velroyen | security.AddNodeToCandidateCerts(node_uuid, other_digest, |
57 | 3338a9ce | Helga Velroyen | self._candidate_certs, warn_fn=self._warn_fn, info_fn=self._info_fn) |
58 | 3338a9ce | Helga Velroyen | self.assertEqual(1, len(self._candidate_certs)) |
59 | 3338a9ce | Helga Velroyen | self.assertTrue(self._candidate_certs[node_uuid] == other_digest) |
60 | 3338a9ce | Helga Velroyen | |
61 | 3338a9ce | Helga Velroyen | # Try removing a certificate from a node that is not in the list
|
62 | 3338a9ce | Helga Velroyen | other_node_uuid = "5678"
|
63 | 3338a9ce | Helga Velroyen | security.RemoveNodeFromCandidateCerts( |
64 | 3338a9ce | Helga Velroyen | other_node_uuid, self._candidate_certs, warn_fn=self._warn_fn) |
65 | 3338a9ce | Helga Velroyen | self.assertEqual(1, len(self._candidate_certs)) |
66 | 3338a9ce | Helga Velroyen | |
67 | 3338a9ce | Helga Velroyen | # Remove a certificate from a node that is in the list
|
68 | 3338a9ce | Helga Velroyen | security.RemoveNodeFromCandidateCerts( |
69 | 3338a9ce | Helga Velroyen | node_uuid, self._candidate_certs, warn_fn=self._warn_fn) |
70 | 3338a9ce | Helga Velroyen | self.assertEqual(0, len(self._candidate_certs)) |
71 | 3338a9ce | Helga Velroyen | |
72 | 3338a9ce | Helga Velroyen | |
73 | b544a3c2 | Helga Velroyen | class TestGetCertificateDigest(testutils.GanetiTestCase): |
74 | b544a3c2 | Helga Velroyen | |
75 | b544a3c2 | Helga Velroyen | def setUp(self): |
76 | b544a3c2 | Helga Velroyen | testutils.GanetiTestCase.setUp(self)
|
77 | b544a3c2 | Helga Velroyen | # certificate file that contains the certificate only
|
78 | b544a3c2 | Helga Velroyen | self._certfilename1 = testutils.TestDataFilename("cert1.pem") |
79 | b544a3c2 | Helga Velroyen | # (different) certificate file that contains both, certificate
|
80 | b544a3c2 | Helga Velroyen | # and private key
|
81 | b544a3c2 | Helga Velroyen | self._certfilename2 = testutils.TestDataFilename("cert2.pem") |
82 | b544a3c2 | Helga Velroyen | |
83 | b544a3c2 | Helga Velroyen | def testGetCertificateDigest(self): |
84 | b544a3c2 | Helga Velroyen | digest1 = security.GetClientCertificateDigest( |
85 | b544a3c2 | Helga Velroyen | cert_filename=self._certfilename1)
|
86 | b544a3c2 | Helga Velroyen | digest2 = security.GetClientCertificateDigest( |
87 | b544a3c2 | Helga Velroyen | cert_filename=self._certfilename2)
|
88 | b544a3c2 | Helga Velroyen | self.assertFalse(digest1 == digest2)
|
89 | b544a3c2 | Helga Velroyen | |
90 | b544a3c2 | Helga Velroyen | |
91 | 3338a9ce | Helga Velroyen | if __name__ == "__main__": |
92 | 3338a9ce | Helga Velroyen | testutils.GanetiTestProgram() |