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