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