summaryrefslogtreecommitdiff
path: root/tests/unittests
diff options
context:
space:
mode:
authorLars Kellogg-Stedman <lars@redhat.com>2017-04-10 15:52:37 -0400
committerScott Moser <smoser@brickies.net>2017-04-12 11:20:27 -0400
commit721348a622a660b65acfdf7fdf53203b47f80748 (patch)
tree0b0f58342c8bc5cf5cfd1514095cb25b259f0052 /tests/unittests
parent493f6c3e923902d5d4f3d87e1cc4c726ea90ada4 (diff)
downloadvyos-cloud-init-721348a622a660b65acfdf7fdf53203b47f80748.tar.gz
vyos-cloud-init-721348a622a660b65acfdf7fdf53203b47f80748.zip
util: teach write_file about copy_mode option
On centos/fedora/rhel/derivatives, /etc/ssh/sshd_config has mode 0600, but cloud-init unilaterally sets file modes to 0644 when no explicit mode is passed to util.write_file. On ubuntu/debian, this file has mode 0644. With this patch, write_file learns about the copy_mode option, which will cause it to use the mode of the existing file by default, falling back to the explicit mode parameter if the file does not exist. LP: #1644064 Resolves: rhbz#1295984
Diffstat (limited to 'tests/unittests')
-rw-r--r--tests/unittests/test_util.py33
1 files changed, 31 insertions, 2 deletions
diff --git a/tests/unittests/test_util.py b/tests/unittests/test_util.py
index ab74311e..5d21b4b7 100644
--- a/tests/unittests/test_util.py
+++ b/tests/unittests/test_util.py
@@ -103,8 +103,8 @@ class TestWriteFile(helpers.TestCase):
self.assertTrue(os.path.isdir(dirname))
self.assertTrue(os.path.isfile(path))
- def test_custom_mode(self):
- """Verify custom mode works properly."""
+ def test_explicit_mode(self):
+ """Verify explicit file mode works properly."""
path = os.path.join(self.tmp, "NewFile.txt")
contents = "Hey there"
@@ -115,6 +115,35 @@ class TestWriteFile(helpers.TestCase):
file_stat = os.stat(path)
self.assertEqual(0o666, stat.S_IMODE(file_stat.st_mode))
+ def test_copy_mode_no_existing(self):
+ """Verify that file is created with mode 0o644 if copy_mode
+ is true and there is no prior existing file."""
+ path = os.path.join(self.tmp, "NewFile.txt")
+ contents = "Hey there"
+
+ util.write_file(path, contents, copy_mode=True)
+
+ self.assertTrue(os.path.exists(path))
+ self.assertTrue(os.path.isfile(path))
+ file_stat = os.stat(path)
+ self.assertEqual(0o644, stat.S_IMODE(file_stat.st_mode))
+
+ def test_copy_mode_with_existing(self):
+ """Verify that file is created using mode of existing file
+ if copy_mode is true."""
+ path = os.path.join(self.tmp, "NewFile.txt")
+ contents = "Hey there"
+
+ open(path, 'w').close()
+ os.chmod(path, 0o666)
+
+ util.write_file(path, contents, copy_mode=True)
+
+ self.assertTrue(os.path.exists(path))
+ self.assertTrue(os.path.isfile(path))
+ file_stat = os.stat(path)
+ self.assertEqual(0o666, stat.S_IMODE(file_stat.st_mode))
+
def test_custom_omode(self):
"""Verify custom omode works properly."""
path = os.path.join(self.tmp, "NewFile.txt")