Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 58 additions & 1 deletion lib/files.cf
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
#
Comment thread
nickanderson marked this conversation as resolved.
# @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
#
Expand Down
102 changes: 102 additions & 0 deletions tests/acceptance/README.org
Original file line number Diff line number Diff line change
Expand Up @@ -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
47 changes: 47 additions & 0 deletions tests/acceptance/lib/files/edit_line_converge.cf
Original file line number Diff line number Diff line change
@@ -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));
}
2 changes: 2 additions & 0 deletions tests/acceptance/lib/files/edit_line_converge.cf.expected
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions tests/acceptance/lib/files/edit_line_converge.cf.start
Original file line number Diff line number Diff line change
@@ -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.
48 changes: 48 additions & 0 deletions tests/acceptance/lib/files/edit_line_converge_prepend.cf
Original file line number Diff line number Diff line change
@@ -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));
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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.