diff options
author | Mike Milner <mike.milner@canonical.com> | 2012-01-17 13:35:31 -0400 |
---|---|---|
committer | Mike Milner <mike.milner@canonical.com> | 2012-01-17 13:35:31 -0400 |
commit | f52ffe2dda01dd0314523fcac559e6f3fbb3578e (patch) | |
tree | bdc11f80cc0212b1f91adec044769c5beb3975fc | |
parent | 93b733862c1f5f41a5597aa640c434610ad76231 (diff) | |
download | vyos-cloud-init-f52ffe2dda01dd0314523fcac559e6f3fbb3578e.tar.gz vyos-cloud-init-f52ffe2dda01dd0314523fcac559e6f3fbb3578e.zip |
Lint fixes.
-rw-r--r-- | cloudinit/CloudConfig/cc_ca_certs.py | 19 | ||||
-rw-r--r-- | tests/unittests/test_handler_ca_certs.py | 22 | ||||
-rw-r--r-- | tests/unittests/test_util.py | 7 |
3 files changed, 28 insertions, 20 deletions
diff --git a/cloudinit/CloudConfig/cc_ca_certs.py b/cloudinit/CloudConfig/cc_ca_certs.py index ef651f8b..e6cdc3f5 100644 --- a/cloudinit/CloudConfig/cc_ca_certs.py +++ b/cloudinit/CloudConfig/cc_ca_certs.py @@ -14,26 +14,23 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. import os -import pwd -import socket from subprocess import check_call -import json -import StringIO -import ConfigParser -import cloudinit.CloudConfig as cc -from cloudinit.util import write_file, get_cfg_option_list_or_str, delete_dir_contents +from cloudinit.util import (write_file, get_cfg_option_list_or_str, + delete_dir_contents) CA_CERT_PATH = "/usr/share/ca-certificates/" CA_CERT_FILENAME = "cloud-init-ca-certs.crt" CA_CERT_CONFIG = "/etc/ca-certificates.conf" CA_CERT_SYSTEM_PATH = "/etc/ssl/certs/" + def update_ca_certs(): """ Updates the CA certificate cache on the current machine. """ check_call(["update-ca-certificates"]) + def add_ca_certs(certs): """ Adds certificates to the system. To actually apply the new certificates @@ -48,6 +45,7 @@ def add_ca_certs(certs): # Append cert filename to CA_CERT_CONFIG file. write_file(CA_CERT_CONFIG, "\n%s" % CA_CERT_FILENAME, omode="a") + def remove_default_ca_certs(): """ Removes all default trusted CA certificates from the system. To actually @@ -57,6 +55,7 @@ def remove_default_ca_certs(): delete_dir_contents(CA_CERT_SYSTEM_PATH) write_file(CA_CERT_CONFIG, "", mode=0644) + def handle(name, cfg, cloud, log, args): """ Call to handle ca-cert sections in cloud-config file. @@ -68,7 +67,7 @@ def handle(name, cfg, cloud, log, args): @param args: Any module arguments from cloud.cfg """ # If there isn't a ca-certs section in the configuration don't do anything - if not cfg.has_key('ca-certs'): + if "ca-certs" not in cfg: return ca_cert_cfg = cfg['ca-certs'] @@ -78,8 +77,8 @@ def handle(name, cfg, cloud, log, args): remove_default_ca_certs() # If we are given any new trusted CA certs to add, add them. - if ca_cert_cfg.has_key('trusted'): - trusted_certs = get_cfg_option_list_or_str(ca_cert_cfg, 'trusted') + if "trusted" in ca_cert_cfg: + trusted_certs = get_cfg_option_list_or_str(ca_cert_cfg, "trusted") if trusted_certs: add_ca_certs(trusted_certs) diff --git a/tests/unittests/test_handler_ca_certs.py b/tests/unittests/test_handler_ca_certs.py index 92460088..c289a4f6 100644 --- a/tests/unittests/test_handler_ca_certs.py +++ b/tests/unittests/test_handler_ca_certs.py @@ -1,8 +1,8 @@ -from unittest import TestCase 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 cloudinit.CloudConfig.cc_ca_certs import ( + handle, update_ca_certs, add_ca_certs, remove_default_ca_certs) class TestNoConfig(MockerTestCase): @@ -36,8 +36,10 @@ class TestConfig(MockerTestCase): # 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, passthrough=False) - self.mock_remove = self.mocker.replace(remove_default_ca_certs, passthrough=False) + self.mock_update = self.mocker.replace(update_ca_certs, + passthrough=False) + self.mock_remove = self.mocker.replace(remove_default_ca_certs, + passthrough=False) # Order must be correct self.mocker.order() @@ -118,7 +120,7 @@ class TestConfig(MockerTestCase): class TestAddCaCerts(MockerTestCase): def test_no_certs_in_list(self): """Test that no certificate are written if not provided.""" - mock = self.mocker.replace(write_file, passthrough=False) + self.mocker.replace(write_file, passthrough=False) self.mocker.replay() add_ca_certs([]) @@ -130,7 +132,8 @@ class TestAddCaCerts(MockerTestCase): mock_write = self.mocker.replace(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") + mock_write("/etc/ca-certificates.conf", + "\ncloud-init-ca-certs.crt", omode="a") self.mocker.replay() add_ca_certs([cert]) @@ -143,7 +146,8 @@ class TestAddCaCerts(MockerTestCase): mock_write = self.mocker.replace(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") + mock_write("/etc/ca-certificates.conf", + "\ncloud-init-ca-certs.crt", omode="a") self.mocker.replay() add_ca_certs(certs) @@ -161,13 +165,13 @@ class TestUpdateCaCerts(MockerTestCase): class TestRemoveDefaultCaCerts(MockerTestCase): def test_commands(self): - mock_delete_dir_contents = self.mocker.replace(delete_dir_contents, passthrough=False) + mock_delete_dir_contents = self.mocker.replace(delete_dir_contents, + passthrough=False) mock_write = self.mocker.replace(write_file, passthrough=False) mock_delete_dir_contents("/usr/share/ca-certificates/") mock_delete_dir_contents("/etc/ssl/certs/") mock_write("/etc/ca-certificates.conf", "", mode=0644) - self.mocker.replay() remove_default_ca_certs() diff --git a/tests/unittests/test_util.py b/tests/unittests/test_util.py index f2b2ee3d..d8da8bc9 100644 --- a/tests/unittests/test_util.py +++ b/tests/unittests/test_util.py @@ -5,7 +5,9 @@ from shutil import rmtree import os import stat -from cloudinit.util import mergedict, get_cfg_option_list_or_str, write_file, delete_dir_contents +from cloudinit.util import (mergedict, get_cfg_option_list_or_str, write_file, + delete_dir_contents) + class TestMergeDict(TestCase): def test_simple_merge(self): @@ -65,6 +67,7 @@ class TestMergeDict(TestCase): result = mergedict(source, candidate) self.assertEqual(source, result) + class TestGetCfgOptionListOrStr(TestCase): def test_not_found_no_default(self): """None is returned if key is not found and no default given.""" @@ -96,6 +99,7 @@ class TestGetCfgOptionListOrStr(TestCase): result = get_cfg_option_list_or_str(config, "key") self.assertEqual([], result) + class TestWriteFile(MockerTestCase): def setUp(self): super(TestWriteFile, self).setUp() @@ -174,6 +178,7 @@ class TestWriteFile(MockerTestCase): write_file(path, contents) + class TestDeleteDirContents(TestCase): def setUp(self): super(TestDeleteDirContents, self).setUp() |