Skip to content

Commit 6193365

Browse files
authored
Merge pull request #1855 from nickanderson/converge_prepend/master
bundle edit_line converge fixups, converge_prepend addition/master
2 parents 5d12761 + 845bcc9 commit 6193365

8 files changed

Lines changed: 267 additions & 1 deletion

lib/files.cf

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1066,16 +1066,73 @@ bundle edit_line converge(marker, lines)
10661066
#
10671067
# @param marker The marker (not a regular expression; will be escaped)
10681068
# @param lines The lines to insert; all must contain `marker`
1069+
#
1070+
# **Example:**
1071+
#
1072+
# ```cf3
1073+
# bundle agent pam_d_su_include
1074+
# #@brief Ensure /etc/pam.d/su has includes configured properly
1075+
# {
1076+
# files:
1077+
# ubuntu::
1078+
# "/etc/pam.d/su"
1079+
# edit_line => converge( "@include", "@include common-auth
1080+
# @include common-account
1081+
# @include common-session");
1082+
# }
1083+
# ```
1084+
#
1085+
# **History:**
1086+
#
1087+
# * Introduced in 3.6.0
10691088
{
10701089
vars:
10711090
"regex" string => escape($(marker));
10721091

10731092
delete_lines:
1074-
"$(regex)" comment => "Delete lines matching the marker";
1093+
".*$(regex).*" comment => "Delete lines matching the marker";
10751094
insert_lines:
10761095
"$(lines)" comment => "Insert the given lines";
10771096
}
10781097

1098+
bundle edit_line converge_prepend(marker, lines)
1099+
# @brief Converge `lines` marked with `marker` to start of content
1100+
#
1101+
# Any content marked with `marker` is removed, then `lines` are
1102+
# inserted at *start* of content. Every `line` should contain `marker`.
1103+
#
1104+
# @param marker The marker (not a regular expression; will be escaped)
1105+
# @param lines The lines to insert; all must contain `marker`
1106+
#
1107+
# **Example:**
1108+
#
1109+
# ```cf3
1110+
# bundle agent pam_d_su_session
1111+
# #@brief Ensure /etc/pam.d/su has session configured properly
1112+
# {
1113+
# files:
1114+
# ubuntu::
1115+
# "/etc/pam.d/su"
1116+
# edit_line => converge_prepend( "session", "session required pam_env.so readenv=1 envfile=/etc/default/locale
1117+
# session optional pam_mail.so nopen
1118+
# session required pam_limits.so" );
1119+
# }
1120+
# ```
1121+
#
1122+
# **History:**
1123+
#
1124+
# * Introduced in 3.17.0
1125+
{
1126+
vars:
1127+
"regex" string => escape($(marker));
1128+
1129+
delete_lines:
1130+
".*$(regex).*" comment => "Delete lines matching the marker";
1131+
insert_lines:
1132+
"$(lines)" location => start, comment => "Insert the given lines";
1133+
}
1134+
1135+
10791136
bundle edit_line fstab_option_editor(method, mount, option)
10801137
# @brief Add or remove `/etc/fstab` options for a mount
10811138
#

tests/acceptance/README.org

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,105 @@ Skipped tests: 0
158158
Soft failures: 0
159159
Total tests: 1
160160
#+END_EXAMPLE
161+
162+
** Check test
163+
164+
#+NAME: run-test
165+
#+CAPTION: Running a test manually
166+
#+begin_src sh :results output :exports both :var TESTFILE="./lib/files/edit_line_converge.cf"
167+
exec 2>&1
168+
find ../../lib/ -name "*.cf" | xargs chmod 600
169+
chmod 600 ${TESTFILE}
170+
./testall \
171+
--bindir="/var/cfengine/bin/" \
172+
--printlog ${TESTFILE}
173+
:
174+
#+end_src
175+
176+
#+CALL: run-test(TESTFILE="./lib/files/edit_line_converge.cf ./lib/files/edit_line_converge_prepend.cf")
177+
178+
#+RESULTS:
179+
#+begin_example
180+
======================================================================
181+
Testsuite started at 2020-10-29 11:49:33
182+
----------------------------------------------------------------------
183+
Total tests: 2
184+
185+
COMMON_TESTS: enabled
186+
TIMED_TESTS: enabled
187+
SLOW_TESTS: enabled
188+
ERROREXIT_TESTS: enabled
189+
SERIAL_TESTS: enabled
190+
NETWORK_TESTS: enabled
191+
LIBXML2_TESTS: enabled
192+
LIBCURL_TESTS: enabled
193+
UNSAFE_TESTS: disabled
194+
STAGING_TESTS: disabled
195+
196+
Test run is not parallel
197+
198+
./lib/files/edit_line_converge.cf Pass
199+
./lib/files/edit_line_converge_prepend.cf Pass
200+
201+
======================================================================
202+
Testsuite finished at 2020-10-29 11:49:35 (2 seconds)
203+
204+
Passed tests: 2
205+
Failed tests: 0
206+
Skipped tests: 0
207+
Soft failures: 0
208+
Total tests: 2
209+
======================================================================
210+
Testsuite started at 2020-10-29 11:49:33
211+
----------------------------------------------------------------------
212+
Total tests: 2
213+
214+
COMMON_TESTS: enabled
215+
TIMED_TESTS: enabled
216+
SLOW_TESTS: enabled
217+
ERROREXIT_TESTS: enabled
218+
SERIAL_TESTS: enabled
219+
NETWORK_TESTS: enabled
220+
LIBXML2_TESTS: enabled
221+
LIBCURL_TESTS: enabled
222+
UNSAFE_TESTS: disabled
223+
STAGING_TESTS: disabled
224+
225+
Test run is not parallel
226+
227+
----------------------------------------------------------------------
228+
./lib/files/edit_line_converge.cf
229+
----------------------------------------------------------------------
230+
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!
231+
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!
232+
R: test description: Test that converge edit_line bundle works as expected
233+
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
234+
R: /home/nickanderson/Northern.Tech/CFEngine/masterfiles/tests/acceptance/./lib/files/edit_line_converge.cf Pass
235+
236+
Return code is 0.
237+
238+
==> Pass
239+
240+
----------------------------------------------------------------------
241+
./lib/files/edit_line_converge_prepend.cf
242+
----------------------------------------------------------------------
243+
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!
244+
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!
245+
R: test description: Test that converge_prepend works as expected
246+
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
247+
R: /home/nickanderson/Northern.Tech/CFEngine/masterfiles/tests/acceptance/./lib/files/edit_line_converge_prepend.cf Pass
248+
249+
Return code is 0.
250+
251+
==> Pass
252+
253+
254+
======================================================================
255+
Testsuite finished at 2020-10-29 11:49:35 (2 seconds)
256+
257+
Passed tests: 2
258+
Failed tests: 0
259+
Skipped tests: 0
260+
Soft failures: 0
261+
Total tests: 2
262+
#+end_example
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
body common control
2+
{
3+
inputs => { '../../default.cf.sub' };
4+
bundlesequence => { default("$(this.promise_filename)") };
5+
version => "1.0";
6+
}
7+
#######################################################
8+
9+
bundle common classes
10+
{
11+
classes:
12+
"testing_masterfiles_policy_framework"
13+
scope => "namespace",
14+
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.";
15+
}
16+
bundle agent init
17+
{
18+
19+
20+
files:
21+
# The tested file "actual" is copied from our seeded starting position.
22+
"$(G.testfile)"
23+
copy_from => local_cp("$(this.promise_filename).start");
24+
}
25+
26+
#######################################################
27+
28+
bundle agent test
29+
{
30+
meta:
31+
"description" -> { "CFE-3482" }
32+
string => "Test that converge edit_line bundle works as expected";
33+
34+
files:
35+
"$(G.testfile)"
36+
edit_line => converge( "covfefe", "Despite the constant negative press covfefe" );
37+
38+
}
39+
40+
#######################################################
41+
42+
bundle agent check
43+
{
44+
methods:
45+
"Pass/FAIL"
46+
usebundle => dcs_check_diff($(G.testfile), "$(this.promise_filename).expected", $(this.promise_filename));
47+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
It has nothing to do with the oranges of the investigation, global waming, or hamberders
2+
Despite the constant negative press covfefe
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
hashtag #covfefe
2+
What is covfefe?
3+
It has nothing to do with the oranges of the investigation, global waming, or hamberders
4+
However, covfefe is bigger than the 2015 paris climate agreement.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
body common control
2+
{
3+
inputs => { '../../default.cf.sub' };
4+
bundlesequence => { default("$(this.promise_filename)") };
5+
version => "1.0";
6+
}
7+
#######################################################
8+
9+
bundle common classes
10+
{
11+
classes:
12+
13+
"testing_masterfiles_policy_framework"
14+
scope => "namespace",
15+
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.";
16+
}
17+
18+
bundle agent init
19+
{
20+
files:
21+
# The tested file "actual" is copied from our seeded starting position.
22+
"$(G.testfile)"
23+
copy_from => local_cp("$(this.promise_filename).start");
24+
25+
}
26+
27+
#######################################################
28+
29+
bundle agent test
30+
{
31+
meta:
32+
"description" -> { "CFE-3483" }
33+
string => "Test that converge_prepend works as expected";
34+
35+
files:
36+
"$(G.testfile)"
37+
edit_line => converge_prepend( "covfefe", "Despite the constant negative press covfefe" );
38+
39+
}
40+
41+
#######################################################
42+
43+
bundle agent check
44+
{
45+
methods:
46+
"Pass/FAIL"
47+
usebundle => dcs_check_diff($(G.testfile), "$(this.promise_filename).expected", $(this.promise_filename));
48+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Despite the constant negative press covfefe
2+
It has nothing to do with the oranges of the investigation, global waming, or hamberders
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
hashtag #covfefe
2+
What is covfefe?
3+
It has nothing to do with the oranges of the investigation, global waming, or hamberders
4+
However, covfefe is bigger than the 2015 paris climate agreement.

0 commit comments

Comments
 (0)