diff --git a/lib/files.cf b/lib/files.cf index 523d56aef5..2660350ff5 100644 --- a/lib/files.cf +++ b/lib/files.cf @@ -1066,16 +1066,73 @@ 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 +# +# Any content marked with `marker` is removed, then `lines` are +# inserted at *start* of content. Every `line` should contain `marker`. +# +# @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 # 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 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 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