c58629
From 8ffdc78b211c025c19636a5f5dcd12d12191aee4 Mon Sep 17 00:00:00 2001
c58629
From: Fraser Tweedale <ftweedal@redhat.com>
c58629
Date: Tue, 5 Dec 2017 15:00:18 +1100
c58629
Subject: [PATCH] Add tests for installutils.set_directive
c58629
c58629
Part of: https://pagure.io/freeipa/issue/7288
c58629
c58629
Reviewed-By: Florence Blanc-Renaud <frenaud@redhat.com>
c58629
---
c58629
 .../test_install/test_installutils.py              | 57 ++++++++++++++++++++++
c58629
 1 file changed, 57 insertions(+)
c58629
 create mode 100644 ipatests/test_ipaserver/test_install/test_installutils.py
c58629
c58629
diff --git a/ipatests/test_ipaserver/test_install/test_installutils.py b/ipatests/test_ipaserver/test_install/test_installutils.py
c58629
new file mode 100644
c58629
index 0000000000000000000000000000000000000000..cc8fd3cf3f82c2d9af48287f506a566ffbfc39f6
c58629
--- /dev/null
c58629
+++ b/ipatests/test_ipaserver/test_install/test_installutils.py
c58629
@@ -0,0 +1,57 @@
c58629
+#
c58629
+# Copyright (C) 2017  FreeIPA Contributors.  See COPYING for license
c58629
+#
c58629
+
c58629
+import os
c58629
+import tempfile
c58629
+
c58629
+from ipaserver.install import installutils
c58629
+
c58629
+EXAMPLE_CONFIG = [
c58629
+    'foo=1\n',
c58629
+    'foobar=2\n',
c58629
+]
c58629
+
c58629
+
c58629
+class test_set_directive_lines(object):
c58629
+    def test_remove_directive(self):
c58629
+        lines = installutils.set_directive_lines(
c58629
+            False, '=', 'foo', None, EXAMPLE_CONFIG)
c58629
+        assert list(lines) == ['foobar=2\n']
c58629
+
c58629
+    def test_add_directive(self):
c58629
+        lines = installutils.set_directive_lines(
c58629
+            False, '=', 'baz', '4', EXAMPLE_CONFIG)
c58629
+        assert list(lines) == ['foo=1\n', 'foobar=2\n', 'baz=4\n']
c58629
+
c58629
+    def test_set_directive_does_not_clobber_suffix_key(self):
c58629
+        lines = installutils.set_directive_lines(
c58629
+            False, '=', 'foo', '3', EXAMPLE_CONFIG)
c58629
+        assert list(lines) == ['foo=3\n', 'foobar=2\n']
c58629
+
c58629
+
c58629
+class test_set_directive(object):
c58629
+    def test_set_directive(self):
c58629
+        """Check that set_directive writes the new data and preserves mode."""
c58629
+        fd, filename = tempfile.mkstemp()
c58629
+        try:
c58629
+            os.close(fd)
c58629
+            stat_pre = os.stat(filename)
c58629
+
c58629
+            with open(filename, 'w') as f:
c58629
+                for line in EXAMPLE_CONFIG:
c58629
+                    f.write(line)
c58629
+
c58629
+            installutils.set_directive(filename, 'foo', '3', False, '=')
c58629
+
c58629
+            stat_post = os.stat(filename)
c58629
+            with open(filename, 'r') as f:
c58629
+                lines = list(f)
c58629
+
c58629
+            assert lines == ['foo=3\n', 'foobar=2\n']
c58629
+            assert stat_pre.st_mode == stat_post.st_mode
c58629
+            assert stat_pre.st_uid == stat_post.st_uid
c58629
+            assert stat_pre.st_gid == stat_post.st_gid
c58629
+
c58629
+        finally:
c58629
+            os.remove(filename)
c58629
-- 
c58629
2.13.6
c58629