From fb0ff769bdce25497949770d392f43b2888a732b Mon Sep 17 00:00:00 2001
From: Mike Milner <mike.milner@canonical.com>
Date: Thu, 12 Jan 2012 18:51:48 +0100
Subject: Add tests for ca-certs handler.

---
 tests/unittests/test_handler_ca_certs.py | 64 ++++++++++++++++++++++++++++++++
 1 file changed, 64 insertions(+)
 create mode 100644 tests/unittests/test_handler_ca_certs.py

(limited to 'tests/unittests')

diff --git a/tests/unittests/test_handler_ca_certs.py b/tests/unittests/test_handler_ca_certs.py
new file mode 100644
index 00000000..21eddf18
--- /dev/null
+++ b/tests/unittests/test_handler_ca_certs.py
@@ -0,0 +1,64 @@
+from unittest import TestCase
+from mocker import MockerTestCase
+
+from cloudinit.CloudConfig.cc_ca_certs import handle, write_file
+
+class TestAddCaCerts(MockerTestCase):
+    def setUp(self):
+        super(TestAddCaCerts, self).setUp()
+        self.name = "ca-certs"
+        self.cloud_init = None
+        self.log = None
+        self.args = []
+
+    def test_no_config(self):
+        """Test that no certificate are written if not provided."""
+        config = {"unknown-key": "value"}
+
+        mock = self.mocker.replace(write_file, passthrough=False)
+        self.mocker.replay()
+
+        handle(self.name, config, self.cloud_init, self.log, self.args)
+
+    def test_no_trusted_list(self):
+        """Test that no certificate are written if not provided."""
+        config = {"ca-certs": {}}
+
+        mock = self.mocker.replace(write_file, passthrough=False)
+        self.mocker.replay()
+
+        handle(self.name, config, self.cloud_init, self.log, self.args)
+
+    def test_no_certs_in_list(self):
+        """Test that no certificate are written if not provided."""
+        config = {"ca-certs": {"trusted": []}}
+
+        mock = self.mocker.replace(write_file, passthrough=False)
+        self.mocker.replay()
+
+        handle(self.name, config, self.cloud_init, self.log, self.args)
+
+    def test_single_cert(self):
+        """Test adding a single certificate to the trusted CAs"""
+        cert = "CERT1\nLINE2\nLINE3"
+        config = {"ca-certs": {"trusted": cert}}
+
+        mock = self.mocker.replace(write_file, passthrough=False)
+        mock("/usr/share/ca-certificates/cloud-init-provided.crt",
+             cert, "root", "root", "644")
+        self.mocker.replay()
+
+        handle(self.name, config, self.cloud_init, self.log, self.args)
+
+    def test_multiple_certs(self):
+        """Test adding multiple certificate to the trusted CAs"""
+        certs = ["CERT1\nLINE2\nLINE3", "CERT2\nLINE2\nLINE3"]
+        cert_file = "\n".join(certs)
+        config = {"ca-certs": {"trusted": certs}}
+
+        mock = self.mocker.replace(write_file, passthrough=False)
+        mock("/usr/share/ca-certificates/cloud-init-provided.crt",
+             cert_file, "root", "root", "644")
+        self.mocker.replay()
+
+        handle(self.name, config, self.cloud_init, self.log, self.args)
-- 
cgit v1.2.3