Debian Codegraph @

magnum_capi_helm/tests/common/test_ca_certificates.py

raw
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12 from unittest import mock
13
14 from magnum.common.x509 import operations as x509
15 from magnum.conductor.handlers.common import cert_manager
16
17 from magnum_capi_helm.common import ca_certificates
18 from magnum_capi_helm.tests import base
19
20
21 class TestCACerts(base.TestCase):
22 @mock.patch.object(cert_manager, "get_cluster_magnum_cert")
23 @mock.patch.object(cert_manager, "get_cluster_ca_certificate")
24 @mock.patch.object(ca_certificates, "_decode_key")
25 @mock.patch.object(ca_certificates, "_decode_cert")
26 def test_ensure_certificate_secrets(
27 self, mock_cert, mock_key, mock_ca, mock_mag
28 ):
29 mock_ca.side_effect = lambda cluster, context, cert="ca": cert
30 mock_key.side_effect = lambda cert: f"key-{cert}"
31 mock_cert.side_effect = lambda cert: f"cert-{cert}"
32 mock_mag.return_value = "cert_mag"
33
34 context = mock.MagicMock()
35 cluster = mock.MagicMock()
36 data = ca_certificates.get_certificate_string_data(context, cluster)
37
38 self.assertEqual(
39 {
40 "ca": {"tls.crt": "cert-ca", "tls.key": "key-ca"},
41 "etcd": {
42 "tls.crt": "cert-etcd",
43 "tls.key": "key-etcd",
44 },
45 "proxy": {
46 "tls.crt": "cert-front_proxy",
47 "tls.key": "key-front_proxy",
48 },
49 "sa": {
50 "tls.crt": "cert-cert_mag",
51 "tls.key": "key-cert_mag",
52 },
53 },
54 data,
55 )
56
57 mock_mag.assert_called_once_with(cluster, context)
58 self.assertEqual(
59 [
60 mock.call(cluster, context),
61 mock.call(cluster, context, "etcd"),
62 mock.call(cluster, context, "front_proxy"),
63 ],
64 mock_ca.call_args_list,
65 )
66
67 def test_decode_cert(self):
68 mock_cert = mock.MagicMock()
69 mock_cert.get_certificate.return_value = "cert"
70
71 result = ca_certificates._decode_cert(mock_cert)
72
73 self.assertEqual("cert", result)
74
75 @mock.patch.object(x509, "decrypt_key")
76 def test_decode_key(self, mock_decrypt):
77 mock_cert = mock.MagicMock()
78 mock_cert.get_private_key.return_value = "private"
79 mock_cert.get_private_key_passphrase.return_value = "pass"
80 mock_decrypt.return_value = "foo"
81
82 result = ca_certificates._decode_key(mock_cert)
83
84 self.assertEqual("foo", result)
85 mock_decrypt.assert_called_once_with("private", "pass")