1
from mocker import MockerTestCase
3
from cloudinit.util import write_file, delete_dir_contents
4
from cloudinit.CloudConfig.cc_ca_certs import (
5
handle, update_ca_certs, add_ca_certs, remove_default_ca_certs)
6
from logging import getLogger
9
class TestNoConfig(MockerTestCase):
11
super(TestNoConfig, self).setUp()
12
self.name = "ca-certs"
13
self.cloud_init = None
14
self.log = getLogger("TestNoConfig")
17
def test_no_config(self):
19
Test that nothing is done if no ca-certs configuration is provided.
21
config = {"unknown-key": "value"}
23
self.mocker.replace(write_file, passthrough=False)
24
self.mocker.replace(update_ca_certs, passthrough=False)
27
handle(self.name, config, self.cloud_init, self.log, self.args)
30
class TestConfig(MockerTestCase):
32
super(TestConfig, self).setUp()
33
self.name = "ca-certs"
34
self.cloud_init = None
35
self.log = getLogger("TestNoConfig")
38
# Mock out the functions that actually modify the system
39
self.mock_add = self.mocker.replace(add_ca_certs, passthrough=False)
40
self.mock_update = self.mocker.replace(update_ca_certs,
42
self.mock_remove = self.mocker.replace(remove_default_ca_certs,
44
# Order must be correct
47
def test_no_trusted_list(self):
49
Test that no certificates are written if the 'trusted' key is not
52
config = {"ca-certs": {}}
54
# No functions should be called
58
handle(self.name, config, self.cloud_init, self.log, self.args)
60
def test_empty_trusted_list(self):
61
"""Test that no certificate are written if 'trusted' list is empty"""
62
config = {"ca-certs": {"trusted": []}}
64
# No functions should be called
68
handle(self.name, config, self.cloud_init, self.log, self.args)
70
def test_single_trusted(self):
71
"""Test that a single cert gets passed to add_ca_certs"""
72
config = {"ca-certs": {"trusted": ["CERT1"]}}
74
self.mock_add(["CERT1"])
78
handle(self.name, config, self.cloud_init, self.log, self.args)
80
def test_multiple_trusted(self):
81
"""Test that multiple certs get passed to add_ca_certs"""
82
config = {"ca-certs": {"trusted": ["CERT1", "CERT2"]}}
84
self.mock_add(["CERT1", "CERT2"])
88
handle(self.name, config, self.cloud_init, self.log, self.args)
90
def test_remove_default_ca_certs(self):
91
"""Test remove_defaults works as expected"""
92
config = {"ca-certs": {"remove-defaults": True}}
98
handle(self.name, config, self.cloud_init, self.log, self.args)
100
def test_no_remove_defaults_if_false(self):
101
"""Test remove_defaults is not called when config value is False"""
102
config = {"ca-certs": {"remove-defaults": False}}
107
handle(self.name, config, self.cloud_init, self.log, self.args)
109
def test_correct_order_for_remove_then_add(self):
110
"""Test remove_defaults is not called when config value is False"""
111
config = {"ca-certs": {"remove-defaults": True, "trusted": ["CERT1"]}}
114
self.mock_add(["CERT1"])
118
handle(self.name, config, self.cloud_init, self.log, self.args)
121
class TestAddCaCerts(MockerTestCase):
122
def test_no_certs_in_list(self):
123
"""Test that no certificate are written if not provided."""
124
self.mocker.replace(write_file, passthrough=False)
129
def test_single_cert(self):
130
"""Test adding a single certificate to the trusted CAs"""
131
cert = "CERT1\nLINE2\nLINE3"
133
mock_write = self.mocker.replace(write_file, passthrough=False)
134
mock_write("/usr/share/ca-certificates/cloud-init-ca-certs.crt",
136
mock_write("/etc/ca-certificates.conf",
137
"\ncloud-init-ca-certs.crt", omode="a")
142
def test_multiple_certs(self):
143
"""Test adding multiple certificates to the trusted CAs"""
144
certs = ["CERT1\nLINE2\nLINE3", "CERT2\nLINE2\nLINE3"]
145
expected_cert_file = "\n".join(certs)
147
mock_write = self.mocker.replace(write_file, passthrough=False)
148
mock_write("/usr/share/ca-certificates/cloud-init-ca-certs.crt",
149
expected_cert_file, mode=0644)
150
mock_write("/etc/ca-certificates.conf",
151
"\ncloud-init-ca-certs.crt", omode="a")
157
class TestUpdateCaCerts(MockerTestCase):
158
def test_commands(self):
159
mock_check_call = self.mocker.replace("subprocess.check_call",
161
mock_check_call(["update-ca-certificates"])
167
class TestRemoveDefaultCaCerts(MockerTestCase):
168
def test_commands(self):
169
mock_delete_dir_contents = self.mocker.replace(delete_dir_contents,
171
mock_write = self.mocker.replace(write_file, passthrough=False)
173
mock_delete_dir_contents("/usr/share/ca-certificates/")
174
mock_delete_dir_contents("/etc/ssl/certs/")
175
mock_write("/etc/ca-certificates.conf", "", mode=0644)
178
remove_default_ca_certs()