summaryrefslogtreecommitdiff
path: root/tests/unittests/test_handler
diff options
context:
space:
mode:
authorJoshua Harlow <harlowja@yahoo-inc.com>2012-06-21 17:37:00 -0700
committerJoshua Harlow <harlowja@yahoo-inc.com>2012-06-21 17:37:00 -0700
commitd570f064d6e3407615588c6e03442404b7df1283 (patch)
tree80bfcd0bae3be9fa4eae508fa050118f019ac8ab /tests/unittests/test_handler
parent421556807fe27e6443c67410634f8a05ab84fc37 (diff)
downloadvyos-cloud-init-d570f064d6e3407615588c6e03442404b7df1283.tar.gz
vyos-cloud-init-d570f064d6e3407615588c6e03442404b7df1283.zip
Get both of these tests back in working shape.
Diffstat (limited to 'tests/unittests/test_handler')
-rw-r--r--tests/unittests/test_handler/test_handler_ca_certs.py101
1 files changed, 59 insertions, 42 deletions
diff --git a/tests/unittests/test_handler/test_handler_ca_certs.py b/tests/unittests/test_handler/test_handler_ca_certs.py
index 21d2442f..8d85d331 100644
--- a/tests/unittests/test_handler/test_handler_ca_certs.py
+++ b/tests/unittests/test_handler/test_handler_ca_certs.py
@@ -1,9 +1,12 @@
from mocker import MockerTestCase
-from cloudinit.util import write_file, delete_dir_contents
-from cloudinit.CloudConfig.cc_ca_certs import (
- handle, update_ca_certs, add_ca_certs, remove_default_ca_certs)
-from logging import getLogger
+from cloudinit import util
+from cloudinit import cloud
+from cloudinit import helpers
+
+from cloudinit.config import cc_ca_certs
+
+import logging
class TestNoConfig(MockerTestCase):
@@ -11,36 +14,37 @@ class TestNoConfig(MockerTestCase):
super(TestNoConfig, self).setUp()
self.name = "ca-certs"
self.cloud_init = None
- self.log = getLogger("TestNoConfig")
+ self.log = logging.getLogger("TestNoConfig")
self.args = []
def test_no_config(self):
"""
Test that nothing is done if no ca-certs configuration is provided.
"""
- config = {"unknown-key": "value"}
-
- self.mocker.replace(write_file, passthrough=False)
- self.mocker.replace(update_ca_certs, passthrough=False)
+ config = util.get_builtin_cfg()
+ self.mocker.replace(util.write_file, passthrough=False)
+ self.mocker.replace(cc_ca_certs.update_ca_certs, passthrough=False)
self.mocker.replay()
- handle(self.name, config, self.cloud_init, self.log, self.args)
+ cc_ca_certs.handle(self.name, config, self.cloud_init, self.log, self.args)
class TestConfig(MockerTestCase):
def setUp(self):
super(TestConfig, self).setUp()
self.name = "ca-certs"
- self.cloud_init = None
- self.log = getLogger("TestNoConfig")
+ self.paths = None
+ self.cloud = cloud.Cloud(None, self.paths, None, None, None)
+ self.log = logging.getLogger("TestNoConfig")
self.args = []
# Mock out the functions that actually modify the system
- self.mock_add = self.mocker.replace(add_ca_certs, passthrough=False)
- self.mock_update = self.mocker.replace(update_ca_certs,
+ self.mock_add = self.mocker.replace(cc_ca_certs.add_ca_certs, passthrough=False)
+ self.mock_update = self.mocker.replace(cc_ca_certs.update_ca_certs,
passthrough=False)
- self.mock_remove = self.mocker.replace(remove_default_ca_certs,
+ self.mock_remove = self.mocker.replace(cc_ca_certs.remove_default_ca_certs,
passthrough=False)
+
# Order must be correct
self.mocker.order()
@@ -55,7 +59,7 @@ class TestConfig(MockerTestCase):
self.mock_update()
self.mocker.replay()
- handle(self.name, config, self.cloud_init, self.log, self.args)
+ cc_ca_certs.handle(self.name, config, self.cloud, self.log, self.args)
def test_empty_trusted_list(self):
"""Test that no certificate are written if 'trusted' list is empty"""
@@ -65,37 +69,37 @@ class TestConfig(MockerTestCase):
self.mock_update()
self.mocker.replay()
- handle(self.name, config, self.cloud_init, self.log, self.args)
+ cc_ca_certs.handle(self.name, config, self.cloud, self.log, self.args)
def test_single_trusted(self):
"""Test that a single cert gets passed to add_ca_certs"""
config = {"ca-certs": {"trusted": ["CERT1"]}}
- self.mock_add(["CERT1"])
+ self.mock_add(self.paths, ["CERT1"])
self.mock_update()
self.mocker.replay()
- handle(self.name, config, self.cloud_init, self.log, self.args)
+ cc_ca_certs.handle(self.name, config, self.cloud, self.log, self.args)
def test_multiple_trusted(self):
"""Test that multiple certs get passed to add_ca_certs"""
config = {"ca-certs": {"trusted": ["CERT1", "CERT2"]}}
- self.mock_add(["CERT1", "CERT2"])
+ self.mock_add(self.paths, ["CERT1", "CERT2"])
self.mock_update()
self.mocker.replay()
- handle(self.name, config, self.cloud_init, self.log, self.args)
+ cc_ca_certs.handle(self.name, config, self.cloud, self.log, self.args)
def test_remove_default_ca_certs(self):
"""Test remove_defaults works as expected"""
config = {"ca-certs": {"remove-defaults": True}}
- self.mock_remove()
+ self.mock_remove(self.paths)
self.mock_update()
self.mocker.replay()
- handle(self.name, config, self.cloud_init, self.log, self.args)
+ cc_ca_certs.handle(self.name, config, self.cloud, self.log, self.args)
def test_no_remove_defaults_if_false(self):
"""Test remove_defaults is not called when config value is False"""
@@ -104,72 +108,85 @@ class TestConfig(MockerTestCase):
self.mock_update()
self.mocker.replay()
- handle(self.name, config, self.cloud_init, self.log, self.args)
+ cc_ca_certs.handle(self.name, config, self.cloud, self.log, self.args)
def test_correct_order_for_remove_then_add(self):
"""Test remove_defaults is not called when config value is False"""
config = {"ca-certs": {"remove-defaults": True, "trusted": ["CERT1"]}}
- self.mock_remove()
- self.mock_add(["CERT1"])
+ self.mock_remove(self.paths)
+ self.mock_add(self.paths, ["CERT1"])
self.mock_update()
self.mocker.replay()
- handle(self.name, config, self.cloud_init, self.log, self.args)
+ cc_ca_certs.handle(self.name, config, self.cloud, self.log, self.args)
class TestAddCaCerts(MockerTestCase):
+
+ def setUp(self):
+ super(TestAddCaCerts, self).setUp()
+ self.paths = helpers.Paths({
+ 'cloud_dir': self.makeDir()
+ })
+
def test_no_certs_in_list(self):
"""Test that no certificate are written if not provided."""
- self.mocker.replace(write_file, passthrough=False)
+ self.mocker.replace(util.write_file, passthrough=False)
self.mocker.replay()
-
- add_ca_certs([])
+ cc_ca_certs.add_ca_certs(self.paths, [])
def test_single_cert(self):
"""Test adding a single certificate to the trusted CAs"""
cert = "CERT1\nLINE2\nLINE3"
- mock_write = self.mocker.replace(write_file, passthrough=False)
+ mock_write = self.mocker.replace(util.write_file, passthrough=False)
mock_write("/usr/share/ca-certificates/cloud-init-ca-certs.crt",
cert, mode=0644)
mock_write("/etc/ca-certificates.conf",
- "\ncloud-init-ca-certs.crt", omode="a")
+ "\ncloud-init-ca-certs.crt", omode="ab")
self.mocker.replay()
- add_ca_certs([cert])
+ cc_ca_certs.add_ca_certs(self.paths, [cert])
def test_multiple_certs(self):
"""Test adding multiple certificates to the trusted CAs"""
certs = ["CERT1\nLINE2\nLINE3", "CERT2\nLINE2\nLINE3"]
expected_cert_file = "\n".join(certs)
- mock_write = self.mocker.replace(write_file, passthrough=False)
+ mock_write = self.mocker.replace(util.write_file, passthrough=False)
mock_write("/usr/share/ca-certificates/cloud-init-ca-certs.crt",
expected_cert_file, mode=0644)
mock_write("/etc/ca-certificates.conf",
- "\ncloud-init-ca-certs.crt", omode="a")
+ "\ncloud-init-ca-certs.crt", omode="ab")
self.mocker.replay()
- add_ca_certs(certs)
+ cc_ca_certs.add_ca_certs(self.paths, certs)
class TestUpdateCaCerts(MockerTestCase):
def test_commands(self):
- mock_check_call = self.mocker.replace("subprocess.check_call",
+ mock_check_call = self.mocker.replace(util.subp,
passthrough=False)
mock_check_call(["update-ca-certificates"])
self.mocker.replay()
- update_ca_certs()
+ cc_ca_certs.update_ca_certs()
class TestRemoveDefaultCaCerts(MockerTestCase):
+
+ def setUp(self):
+ super(TestRemoveDefaultCaCerts, self).setUp()
+ self.paths = helpers.Paths({
+ 'cloud_dir': self.makeDir()
+ })
+
def test_commands(self):
- mock_delete_dir_contents = self.mocker.replace(delete_dir_contents,
+ mock_delete_dir_contents = self.mocker.replace(util.delete_dir_contents,
passthrough=False)
- mock_write = self.mocker.replace(write_file, passthrough=False)
- mock_subp = self.mocker.replace("cloudinit.util.subp",
+ mock_write = self.mocker.replace(util.write_file, passthrough=False)
+ mock_subp = self.mocker.replace(util.subp,
passthrough=False)
mock_delete_dir_contents("/usr/share/ca-certificates/")
@@ -179,4 +196,4 @@ class TestRemoveDefaultCaCerts(MockerTestCase):
"ca-certificates ca-certificates/trust_new_crts select no")
self.mocker.replay()
- remove_default_ca_certs()
+ cc_ca_certs.remove_default_ca_certs(self.paths)