From 8d2a63e9e7699588074dc68345e7bf3819fc5719 Mon Sep 17 00:00:00 2001 From: Ted Zlatanov Date: Tue, 27 Oct 2020 07:14:09 -0400 Subject: [PATCH 1/5] Added converge_prepend Ticket: CFE-3483 Changelog: Added bundle edit_line converge_prepend with same behavior as bundle edit_line converge, but inserting at start of content. --- lib/files.cf | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/lib/files.cf b/lib/files.cf index 523d56aef5..d7460aedd6 100644 --- a/lib/files.cf +++ b/lib/files.cf @@ -1076,6 +1076,59 @@ bundle edit_line converge(marker, lines) "$(lines)" comment => "Insert the given lines"; } +bundle edit_line converge_prepend(marker, lines) +# @brief Converge `lines` marked with `marker` to start of content. +# +# Any content marked with `marker` is removed, then `lines` are +# inserted at *start* of content. Every `line` should contain `marker`. +# +# This is helpful for files where order matters, like `pam.d/common-auth`, +# and you want the content at the top. If you want to maintain the order +# of multiple lines, pass one item with a newline in it like this: +# +# **Example:** +# +# ```cf3 +# files: +# "/etc/pam.conf" +# edit_line => converge_prepend("session", "session required pam_loginuid.so +#session required pam_env.so +#session required pam_env.so envfile=/etc/default/locale +#session required pam_limits.so"); +# ``` +# +# @param marker The marker (not a regular expression; will be escaped) +# @param lines The lines to insert; all must contain `marker` +# +# **Example:** +# +# ```cf3 +# bundle agent pam_d_su_session +# # @brief Ensure /etc/pam.d/su has session configured properly +# { +# files: +# ubuntu:: +# "/etc/pam.d/su" +# edit_line => converge_prepend( "session", "session required pam_env.so readenv=1 envfile=/etc/default/locale +# session optional pam_mail.so nopen +# session required pam_limits.so" ); +# } +# ``` +# +# **History:** +# +# * Introduced in 3.17.0 +{ + vars: + "regex" string => escape($(marker)); + + delete_lines: + ".*$(regex).*" comment => "Delete lines matching the marker"; + insert_lines: + "$(lines)" location => start, comment => "Insert the given lines"; +} + + bundle edit_line fstab_option_editor(method, mount, option) # @brief Add or remove `/etc/fstab` options for a mount # From c4159ebeb3a698e6fb6ab319bf09ced1b3ff69ed Mon Sep 17 00:00:00 2001 From: Nick Anderson Date: Wed, 28 Oct 2020 12:44:01 -0500 Subject: [PATCH 2/5] Added test for converge_prepend --- lib/files.cf | 2 +- .../lib/files/edit_line_converge_prepend.cf | 48 +++++++++++++++++++ .../edit_line_converge_prepend.cf.expected | 2 + .../files/edit_line_converge_prepend.cf.start | 4 ++ 4 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 tests/acceptance/lib/files/edit_line_converge_prepend.cf create mode 100644 tests/acceptance/lib/files/edit_line_converge_prepend.cf.expected create mode 100644 tests/acceptance/lib/files/edit_line_converge_prepend.cf.start diff --git a/lib/files.cf b/lib/files.cf index d7460aedd6..12ea73e179 100644 --- a/lib/files.cf +++ b/lib/files.cf @@ -1104,7 +1104,7 @@ bundle edit_line converge_prepend(marker, lines) # # ```cf3 # bundle agent pam_d_su_session -# # @brief Ensure /etc/pam.d/su has session configured properly +# #@brief Ensure /etc/pam.d/su has session configured properly # { # files: # ubuntu:: diff --git a/tests/acceptance/lib/files/edit_line_converge_prepend.cf b/tests/acceptance/lib/files/edit_line_converge_prepend.cf new file mode 100644 index 0000000000..f34a3b8ca1 --- /dev/null +++ b/tests/acceptance/lib/files/edit_line_converge_prepend.cf @@ -0,0 +1,48 @@ +body common control +{ + inputs => { '../../default.cf.sub' }; + bundlesequence => { default("$(this.promise_filename)") }; + version => "1.0"; +} +####################################################### + +bundle common classes +{ + classes: + + "testing_masterfiles_policy_framework" + scope => "namespace", + comment => "This class is needed so that dcs.cf.sub includes the stdlib instead of using plucked.cf.sub from core which might get out of date and cause us to not test current code from the MPF."; +} + +bundle agent init +{ + files: + # The tested file "actual" is copied from our seeded starting position. + "$(G.testfile)" + copy_from => local_cp("$(this.promise_filename).start"); + +} + +####################################################### + +bundle agent test +{ + meta: + "description" -> { "CFE-3483" } + string => "Test that converge_prepend works as expected"; + + files: + "$(G.testfile)" + edit_line => converge_prepend( "covfefe", "Despite the constant negative press covfefe" ); + +} + +####################################################### + +bundle agent check +{ + methods: + "Pass/FAIL" + usebundle => dcs_check_diff($(G.testfile), "$(this.promise_filename).expected", $(this.promise_filename)); +} diff --git a/tests/acceptance/lib/files/edit_line_converge_prepend.cf.expected b/tests/acceptance/lib/files/edit_line_converge_prepend.cf.expected new file mode 100644 index 0000000000..d11c8b55c4 --- /dev/null +++ b/tests/acceptance/lib/files/edit_line_converge_prepend.cf.expected @@ -0,0 +1,2 @@ +Despite the constant negative press covfefe +It has nothing to do with the oranges of the investigation, global waming, or hamberders diff --git a/tests/acceptance/lib/files/edit_line_converge_prepend.cf.start b/tests/acceptance/lib/files/edit_line_converge_prepend.cf.start new file mode 100644 index 0000000000..ddbdd2f69f --- /dev/null +++ b/tests/acceptance/lib/files/edit_line_converge_prepend.cf.start @@ -0,0 +1,4 @@ +hashtag #covfefe +What is covfefe? +It has nothing to do with the oranges of the investigation, global waming, or hamberders +However, covfefe is bigger than the 2015 paris climate agreement. \ No newline at end of file From ca39f9b29008cf7bdd59cfdb007976e44a862a6d Mon Sep 17 00:00:00 2001 From: Ted Zlatanov Date: Tue, 27 Oct 2020 07:14:09 -0400 Subject: [PATCH 3/5] Fixed converge edit_line bundle not deleting lines containing marker In my testing with 3.16, the deletion doesn't work today, so `converge` is not functioning properly. (cherry picked from commit fae4505cde777ff53057f867e10d40aebb0b5a31) Ticket: CFE-3482 Changelog: Title --- lib/files.cf | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/lib/files.cf b/lib/files.cf index 12ea73e179..2660350ff5 100644 --- a/lib/files.cf +++ b/lib/files.cf @@ -1066,37 +1066,41 @@ bundle edit_line converge(marker, lines) # # @param marker The marker (not a regular expression; will be escaped) # @param lines The lines to insert; all must contain `marker` +# +# **Example:** +# +# ```cf3 +# bundle agent pam_d_su_include +# #@brief Ensure /etc/pam.d/su has includes configured properly +# { +# files: +# ubuntu:: +# "/etc/pam.d/su" +# edit_line => converge( "@include", "@include common-auth +# @include common-account +# @include common-session"); +# } +# ``` +# +# **History:** +# +# * Introduced in 3.6.0 { vars: "regex" string => escape($(marker)); delete_lines: - "$(regex)" comment => "Delete lines matching the marker"; + ".*$(regex).*" comment => "Delete lines matching the marker"; insert_lines: "$(lines)" comment => "Insert the given lines"; } bundle edit_line converge_prepend(marker, lines) -# @brief Converge `lines` marked with `marker` to start of content. +# @brief Converge `lines` marked with `marker` to start of content # # Any content marked with `marker` is removed, then `lines` are # inserted at *start* of content. Every `line` should contain `marker`. # -# This is helpful for files where order matters, like `pam.d/common-auth`, -# and you want the content at the top. If you want to maintain the order -# of multiple lines, pass one item with a newline in it like this: -# -# **Example:** -# -# ```cf3 -# files: -# "/etc/pam.conf" -# edit_line => converge_prepend("session", "session required pam_loginuid.so -#session required pam_env.so -#session required pam_env.so envfile=/etc/default/locale -#session required pam_limits.so"); -# ``` -# # @param marker The marker (not a regular expression; will be escaped) # @param lines The lines to insert; all must contain `marker` # From 70cf0fab72a1353d94ba85a737d7ee067e435f5c Mon Sep 17 00:00:00 2001 From: Nick Anderson Date: Wed, 28 Oct 2020 13:06:03 -0500 Subject: [PATCH 4/5] Added test for bundle edit_line converge Ticket: CFE-3482 Changelog: None --- .../lib/files/edit_line_converge.cf | 47 +++++++++++++++++++ .../lib/files/edit_line_converge.cf.expected | 2 + .../lib/files/edit_line_converge.cf.start | 4 ++ 3 files changed, 53 insertions(+) create mode 100644 tests/acceptance/lib/files/edit_line_converge.cf create mode 100644 tests/acceptance/lib/files/edit_line_converge.cf.expected create mode 100644 tests/acceptance/lib/files/edit_line_converge.cf.start diff --git a/tests/acceptance/lib/files/edit_line_converge.cf b/tests/acceptance/lib/files/edit_line_converge.cf new file mode 100644 index 0000000000..37ed838878 --- /dev/null +++ b/tests/acceptance/lib/files/edit_line_converge.cf @@ -0,0 +1,47 @@ +body common control +{ + inputs => { '../../default.cf.sub' }; + bundlesequence => { default("$(this.promise_filename)") }; + version => "1.0"; +} +####################################################### + +bundle common classes +{ + classes: + "testing_masterfiles_policy_framework" + scope => "namespace", + comment => "This class is needed so that dcs.cf.sub includes the stdlib instead of using plucked.cf.sub from core which might get out of date and cause us to not test current code from the MPF."; +} +bundle agent init +{ + + + files: + # The tested file "actual" is copied from our seeded starting position. + "$(G.testfile)" + copy_from => local_cp("$(this.promise_filename).start"); +} + +####################################################### + +bundle agent test +{ + meta: + "description" -> { "CFE-3482" } + string => "Test that converge edit_line bundle works as expected"; + + files: + "$(G.testfile)" + edit_line => converge( "covfefe", "Despite the constant negative press covfefe" ); + +} + +####################################################### + +bundle agent check +{ + methods: + "Pass/FAIL" + usebundle => dcs_check_diff($(G.testfile), "$(this.promise_filename).expected", $(this.promise_filename)); +} diff --git a/tests/acceptance/lib/files/edit_line_converge.cf.expected b/tests/acceptance/lib/files/edit_line_converge.cf.expected new file mode 100644 index 0000000000..abdc95fe15 --- /dev/null +++ b/tests/acceptance/lib/files/edit_line_converge.cf.expected @@ -0,0 +1,2 @@ +It has nothing to do with the oranges of the investigation, global waming, or hamberders +Despite the constant negative press covfefe diff --git a/tests/acceptance/lib/files/edit_line_converge.cf.start b/tests/acceptance/lib/files/edit_line_converge.cf.start new file mode 100644 index 0000000000..ddbdd2f69f --- /dev/null +++ b/tests/acceptance/lib/files/edit_line_converge.cf.start @@ -0,0 +1,4 @@ +hashtag #covfefe +What is covfefe? +It has nothing to do with the oranges of the investigation, global waming, or hamberders +However, covfefe is bigger than the 2015 paris climate agreement. \ No newline at end of file From 845bcc9531851eb10bb16eab26229a6c077ed2be Mon Sep 17 00:00:00 2001 From: Nick Anderson Date: Thu, 29 Oct 2020 11:54:56 -0500 Subject: [PATCH 5/5] Added src block to make it easy to run tests from org-mode --- tests/acceptance/README.org | 102 ++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/tests/acceptance/README.org b/tests/acceptance/README.org index 5df111626a..05fdda230d 100644 --- a/tests/acceptance/README.org +++ b/tests/acceptance/README.org @@ -158,3 +158,105 @@ Skipped tests: 0 Soft failures: 0 Total tests: 1 #+END_EXAMPLE + +** Check test + +#+NAME: run-test +#+CAPTION: Running a test manually +#+begin_src sh :results output :exports both :var TESTFILE="./lib/files/edit_line_converge.cf" + exec 2>&1 + find ../../lib/ -name "*.cf" | xargs chmod 600 + chmod 600 ${TESTFILE} + ./testall \ + --bindir="/var/cfengine/bin/" \ + --printlog ${TESTFILE} + : +#+end_src + +#+CALL: run-test(TESTFILE="./lib/files/edit_line_converge.cf ./lib/files/edit_line_converge_prepend.cf") + +#+RESULTS: +#+begin_example +====================================================================== +Testsuite started at 2020-10-29 11:49:33 +---------------------------------------------------------------------- +Total tests: 2 + + COMMON_TESTS: enabled + TIMED_TESTS: enabled + SLOW_TESTS: enabled + ERROREXIT_TESTS: enabled + SERIAL_TESTS: enabled + NETWORK_TESTS: enabled + LIBXML2_TESTS: enabled + LIBCURL_TESTS: enabled + UNSAFE_TESTS: disabled + STAGING_TESTS: disabled + +Test run is not parallel + +./lib/files/edit_line_converge.cf Pass +./lib/files/edit_line_converge_prepend.cf Pass + +====================================================================== +Testsuite finished at 2020-10-29 11:49:35 (2 seconds) + +Passed tests: 2 +Failed tests: 0 +Skipped tests: 0 +Soft failures: 0 +Total tests: 2 +====================================================================== +Testsuite started at 2020-10-29 11:49:33 +---------------------------------------------------------------------- +Total tests: 2 + + COMMON_TESTS: enabled + TIMED_TESTS: enabled + SLOW_TESTS: enabled + ERROREXIT_TESTS: enabled + SERIAL_TESTS: enabled + NETWORK_TESTS: enabled + LIBXML2_TESTS: enabled + LIBCURL_TESTS: enabled + UNSAFE_TESTS: disabled + STAGING_TESTS: disabled + +Test run is not parallel + +---------------------------------------------------------------------- +./lib/files/edit_line_converge.cf +---------------------------------------------------------------------- +2020-10-29T11:49:33-0500 error: UNTRUSTED: Module directory /home/nickanderson/Northern.Tech/CFEngine/masterfiles/tests/acceptance/workdir/__lib_files_edit_line_converge_cf/modules (mode 775) was not private! + error: UNTRUSTED: Module directory /home/nickanderson/Northern.Tech/CFEngine/masterfiles/tests/acceptance/workdir/__lib_files_edit_line_converge_cf/modules (mode 775) was not private! +R: test description: Test that converge edit_line bundle works as expected +R: Diff command: /usr/bin/diff -u /home/nickanderson/Northern.Tech/CFEngine/masterfiles/tests/acceptance/./lib/files/edit_line_converge.cf.expected /home/nickanderson/Northern.Tech/CFEngine/masterfiles/tests/acceptance/workdir/__lib_files_edit_line_converge_cf/tmp/TEST.cfengine 2>/dev/null +R: /home/nickanderson/Northern.Tech/CFEngine/masterfiles/tests/acceptance/./lib/files/edit_line_converge.cf Pass + +Return code is 0. + + ==> Pass + +---------------------------------------------------------------------- +./lib/files/edit_line_converge_prepend.cf +---------------------------------------------------------------------- +2020-10-29T11:49:34-0500 error: UNTRUSTED: Module directory /home/nickanderson/Northern.Tech/CFEngine/masterfiles/tests/acceptance/workdir/__lib_files_edit_line_converge_prepend_cf/modules (mode 775) was not private! + error: UNTRUSTED: Module directory /home/nickanderson/Northern.Tech/CFEngine/masterfiles/tests/acceptance/workdir/__lib_files_edit_line_converge_prepend_cf/modules (mode 775) was not private! +R: test description: Test that converge_prepend works as expected +R: Diff command: /usr/bin/diff -u /home/nickanderson/Northern.Tech/CFEngine/masterfiles/tests/acceptance/./lib/files/edit_line_converge_prepend.cf.expected /home/nickanderson/Northern.Tech/CFEngine/masterfiles/tests/acceptance/workdir/__lib_files_edit_line_converge_prepend_cf/tmp/TEST.cfengine 2>/dev/null +R: /home/nickanderson/Northern.Tech/CFEngine/masterfiles/tests/acceptance/./lib/files/edit_line_converge_prepend.cf Pass + +Return code is 0. + + ==> Pass + + +====================================================================== +Testsuite finished at 2020-10-29 11:49:35 (2 seconds) + +Passed tests: 2 +Failed tests: 0 +Skipped tests: 0 +Soft failures: 0 +Total tests: 2 +#+end_example