From f23518e4ec9aabf3c79d8f05e49140adbf70bd96 Mon Sep 17 00:00:00 2001 From: Felix Dallidet Date: Thu, 10 Dec 2020 18:19:15 +0100 Subject: [PATCH] Work in progress --- .../file_copy_from_local_source.cf | 25 +-- .../file_copy_from_local_source_recursion.cf | 32 ++-- .../file_from_local_source_recursion.cf | 109 +++++++++++++ .../file_from_template_jinja2.cf | 100 ------------ .../file_from_template_jinja2.cf | 148 ++++++++++++++++++ .../resources/test_0.expected} | 0 .../resources/test_0.source} | 0 .../resources/test_1.expected | 16 ++ .../resources/test_1.source | 16 ++ .../resources/test_2.expected} | 0 .../resources/test_2.source} | 0 .../resources/test_3.expected | 13 ++ .../resources/test_3.source | 14 ++ .../file_copy_from_local_source.cf | 4 +- .../file_copy_from_local_source_recursion.cf | 19 +-- .../file_from_local_source_recursion.cf | 46 ++++-- .../file_from_template_jinja2.cf | 25 +-- 17 files changed, 408 insertions(+), 159 deletions(-) create mode 100644 tests/acceptance/30_generic_methods/file_from_local_source_recursion.cf delete mode 100644 tests/acceptance/30_generic_methods/file_from_template_jinja2.cf create mode 100644 tests/acceptance/30_generic_methods/file_from_template_jinja2/file_from_template_jinja2.cf rename tests/acceptance/30_generic_methods/{file_from_template_jinja2.cf_1.expected => file_from_template_jinja2/resources/test_0.expected} (100%) rename tests/acceptance/30_generic_methods/{file_from_template_jinja2.cf_1.j2 => file_from_template_jinja2/resources/test_0.source} (100%) create mode 100644 tests/acceptance/30_generic_methods/file_from_template_jinja2/resources/test_1.expected create mode 100644 tests/acceptance/30_generic_methods/file_from_template_jinja2/resources/test_1.source rename tests/acceptance/30_generic_methods/{file_from_template_jinja2.cf_2.expected => file_from_template_jinja2/resources/test_2.expected} (100%) rename tests/acceptance/30_generic_methods/{file_from_template_jinja2.cf_2.j2 => file_from_template_jinja2/resources/test_2.source} (100%) create mode 100644 tests/acceptance/30_generic_methods/file_from_template_jinja2/resources/test_3.expected create mode 100644 tests/acceptance/30_generic_methods/file_from_template_jinja2/resources/test_3.source diff --git a/tests/acceptance/30_generic_methods/file_copy_from_local_source.cf b/tests/acceptance/30_generic_methods/file_copy_from_local_source.cf index a06548ab7..c6ead1e91 100644 --- a/tests/acceptance/30_generic_methods/file_copy_from_local_source.cf +++ b/tests/acceptance/30_generic_methods/file_copy_from_local_source.cf @@ -23,16 +23,19 @@ bundle agent init { vars: "tmp" string => getenv("TEMP", 1024); - "source_file" string => "${tmp}/source_test"; - "destination_file" string => "${tmp}/destination_test"; - "destination_file_canon" string => canonify("${destination_file}"); - "mode" string => "644"; - "owner" string => "root"; - "group" string => "0"; + "source_file[0]" string => "${tmp}/source_test_0"; + "destination_file[0]" string => "${tmp}/destination_test_0"; + "expected_result[0]" string => "repaired"; + + "mode" string => "644"; + "owner" string => "root"; + "group" string => "0"; + + "indices" slist => getindices("source_file"); files: - "${source_file}" + "${source_file[0]}" create => "true", perms => mog("${mode}", "${owner}", "${group}"); @@ -42,8 +45,11 @@ bundle agent init bundle agent test { + vars: + "args${init.indices}" slist => { "${init.source_file[${init.indices}]}", "${init.destination_file[0]}" }; + methods: - "ph1" usebundle => file_copy_from_local_source("${init.source_file}", "${init.destination_file}"); + "ph0" usebundle => apply_gm("file_copy_from_local_source", @{args0}, "${init.expected_result[0]}", "ph0", "enforce" ); } ####################################################### @@ -70,7 +76,8 @@ bundle agent check expression => returnszero("${permissions_test_group}", "useshell"), ifvarclass => canonify("file_copy_from_local_source_${init.destination_file}_reached"); - "ok" expression => "permissions_test_mode_ok.permissions_test_owner_ok.permissions_test_group_ok.file_copy_from_local_source_${init.destination_file_canon}_ok.!file_copy_from_local_source_${init.destination_file_canon}_error"; + "classes_ok" expression => "ph0_ok"; + "ok" expression => "permissions_test_mode_ok.permissions_test_owner_ok.permissions_test_group_ok.classes_ok"; reports: ok:: diff --git a/tests/acceptance/30_generic_methods/file_copy_from_local_source_recursion.cf b/tests/acceptance/30_generic_methods/file_copy_from_local_source_recursion.cf index eabd19b89..f568d9e37 100644 --- a/tests/acceptance/30_generic_methods/file_copy_from_local_source_recursion.cf +++ b/tests/acceptance/30_generic_methods/file_copy_from_local_source_recursion.cf @@ -23,17 +23,20 @@ bundle agent init { vars: "tmp" string => getenv("TEMP", 1024); - "source_file" string => "${tmp}/source_test"; - "destination_file" string => "${tmp}/destination_test"; - "destination_file_canon" string => canonify("${destination_file}"); - "recursion" string => "0"; + "source_file[0]" string => "${tmp}/source_test_0"; + "destination_file[0]" string => "${tmp}/destination_test_0"; + "expected_result[0]" string => "repaired"; + "recursion[0]" string => "0"; - "mode" string => "644"; - "owner" string => "root"; - "group" string => "0"; + "mode" string => "665"; + "owner" string => "root"; + "group" string => "0"; + + + "indices" slist => getindices("source_file"); files: - "${source_file}" + "${source_file[0]}" create => "true", perms => mog("${mode}", "${owner}", "${group}"); @@ -43,8 +46,11 @@ bundle agent init bundle agent test { + vars: + "args${init.indices}" slist => { "${init.source_file[${init.indices}]}", "${init.destination_file[0]}", "${init.recursion[${init.indices}]}" }; + methods: - "ph1" usebundle => file_copy_from_local_source_recursion("${init.source_file}", "${init.destination_file}", "${init.recursion}"); + "ph0" usebundle => apply_gm("file_copy_from_local_source_recursion", @{args0}, "${init.expected_result[0]}", "ph0", "enforce" ); } ####################################################### @@ -53,9 +59,9 @@ bundle agent check { vars: "owner_id" int => getuid("${init.owner}"); - "permissions_test_mode" string => "/usr/bin/test ${const.dollar}(${test_utils.file_perms} ${init.destination_file}) = \"${init.mode}\""; - "permissions_test_owner" string => "/usr/bin/test ${const.dollar}(${test_utils.file_owner} ${init.destination_file}) = \"${owner_id}\""; - "permissions_test_group" string => "/usr/bin/test ${const.dollar}(${test_utils.file_group} ${init.destination_file}) = \"${init.group}\""; + "permissions_test_mode" string => "/usr/bin/test ${const.dollar}(${test_utils.file_perms} ${init.destination_file[0]}) = \"${init.mode}\""; + "permissions_test_owner" string => "/usr/bin/test ${const.dollar}(${test_utils.file_owner} ${init.destination_file[0]}) = \"${owner_id}\""; + "permissions_test_group" string => "/usr/bin/test ${const.dollar}(${test_utils.file_group} ${init.destination_file[0]}) = \"${init.group}\""; classes: # By default, file_copy_from_local_source_type_recursion should create the file if it doesn't exist @@ -71,7 +77,7 @@ bundle agent check expression => returnszero("${permissions_test_group}", "useshell"), ifvarclass => canonify("file_copy_from_local_source_${init.destination_file}_reached"); - "ok" expression => "permissions_test_mode_ok.permissions_test_owner_ok.permissions_test_group_ok.file_copy_from_local_source_${init.destination_file_canon}_ok.!file_copy_from_local_source_${init.destination_file_canon}_error"; + "ok" expression => "permissions_test_mode_ok.permissions_test_owner_ok.permissions_test_group_ok.ph0_ok"; reports: ok:: diff --git a/tests/acceptance/30_generic_methods/file_from_local_source_recursion.cf b/tests/acceptance/30_generic_methods/file_from_local_source_recursion.cf new file mode 100644 index 000000000..72868507d --- /dev/null +++ b/tests/acceptance/30_generic_methods/file_from_local_source_recursion.cf @@ -0,0 +1,109 @@ +####################################################### +# +# Test checking if a file can be copied from a local source +# +####################################################### + +bundle common acc_path +{ + vars: + "root" string => getenv("NCF_TESTS_ACCEPTANCE", 1024); +} + +body common control +{ + inputs => { "${acc_path.root}/default.cf.sub", "${acc_path.root}/default_ncf.cf.sub", "@{ncf_inputs.default_files}" }; + bundlesequence => { configuration, default("${this.promise_filename}") }; + version => "1.0"; +} + +####################################################### + +bundle agent init +{ + vars: + "tmp" string => getenv("TEMP", 1024); + + ## Copy a single file, create the containing folder + "source_file[0]" string => "${this.promise_filename}"; + "destination_file[0]" string => "${tmp}/0/destination_test_0"; + "expected_result[0]" string => "repaired"; + "recursion[0]" string => "0"; + "mode[0]" string => "enforce"; + + # Redo the #0, expect a success, recursion change is needed + # otherwise the bundle call will be skipped + "source_file[1]" string => "${this.promise_filename}"; + "destination_file[1]" string => "${tmp}/0/destination_test_0"; + "expected_result[1]" string => "success"; + "recursion[1]" string => "inf"; + "mode[1]" string => "enforce"; + + ## Copy a folder structure, create the containing folder + "source_file[2]" string => "${this.promise_dirname}"; + "destination_file[2]" string => "${tmp}/2/"; + "expected_result[2]" string => "repaired"; + "recursion[2]" string => "99"; + "mode[2]" string => "enforce"; + # Redo the #2, expect a success + # otherwise the bundle call will be skipped + "source_file[3]" string => "${this.promise_dirname}"; + "destination_file[3]" string => "${tmp}/2/"; + "expected_result[3]" string => "success"; + "recursion[3]" string => "inf"; + "mode[3]" string => "enforce"; + + ## Try to copy a non-existing file + "source_file[4]" string => "/file/that/does/not/exist"; + "destination_file[4]" string => "${tmp}/4/destination_test_4"; + "expected_result[4]" string => "error"; + "recursion[4]" string => "0"; + "mode[4]" string => "enforce"; + + #### Audit + ## Same file, expect success + "source_file[10]" string => "${this.promise_filename}"; + "destination_file[10]" string => "${this.promise_filename}"; + "expected_result[10]" string => "error"; + "recursion[10]" string => "inf"; + "mode[10]" string => "audit"; + + "indices" slist => getindices("source_file"); +} + +####################################################### + +bundle agent test +{ + vars: + "args${init.indices}" slist => { "${init.source_file[${init.indices}]}", "${init.destination_file[${init.indices}]}", "${init.recursion[${init.indices}]}" }; + + methods: + "ph0" usebundle => apply_gm("file_from_local_source_recursion", @{args0}, "${init.expected_result[0]}", "ph0", "${init.mode[0]}" ); + "ph1" usebundle => apply_gm("file_from_local_source_recursion", @{args1}, "${init.expected_result[1]}", "ph1", "${init.mode[1]}" ); + "ph2" usebundle => apply_gm("file_from_local_source_recursion", @{args2}, "${init.expected_result[2]}", "ph2", "${init.mode[2]}" ); + "ph3" usebundle => apply_gm("file_from_local_source_recursion", @{args3}, "${init.expected_result[3]}", "ph3", "${init.mode[3]}" ); + "ph4" usebundle => apply_gm("file_from_local_source_recursion", @{args4}, "${init.expected_result[4]}", "ph4", "${init.mode[4]}" ); + + "ph10" usebundle => apply_gm("file_from_local_source_recursion", @{args10}, "${init.expected_result[10]}", "ph10", "${init.mode[10]}" ); +} + +####################################################### + +bundle agent check +{ + vars: + "hash_source[${init.indices}]" string => execresult("${ncf_paths.path[find]} ${init.source_file[${init.indices}]} -type f -exec md5sum '{}' +| ${ncf_paths.path[cut]} -d' ' -f1 | md5sum", "useshell"); + "hash_destination[${init.indices}]" string => execresult("${ncf_paths.path[find]} ${init.destination_file[${init.indices}]} -type f -exec md5sum '{}' +| ${ncf_paths.path[cut]} -d' ' -f1 | md5sum", "useshell"); + classes: + "hash_ok_${init.indices}" expression => strcmp("${hash_source_[${init_indices}]}", "${hash_destination_[${init_indices}]}"); + "copy_not_ok" not => "hash_ok_${init.indices}"; + + "ok" expression => "copy_not_ok.ph0_ok.ph1_ok.ph2_ok.ph3_ok.ph4_ok.ph10_ok"; + + reports: + ok:: + "$(this.promise_filename) Pass"; + !ok:: + "$(this.promise_filename) FAIL"; +} diff --git a/tests/acceptance/30_generic_methods/file_from_template_jinja2.cf b/tests/acceptance/30_generic_methods/file_from_template_jinja2.cf deleted file mode 100644 index 9abe029c0..000000000 --- a/tests/acceptance/30_generic_methods/file_from_template_jinja2.cf +++ /dev/null @@ -1,100 +0,0 @@ -#################################################################################### -# Copyright 2016 Normation SAS -##################################################################################### -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, Version 3. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -# -##################################################################################### - -# agent_version >= 3.9 - -####################################################### -# -# Test checking if a file can be generated from a local template -# -####################################################### - -bundle common acc_path -{ - vars: - "root" string => getenv("NCF_TESTS_ACCEPTANCE", 1024); -} - -body common control -{ - inputs => { "${acc_path.root}/default.cf.sub", "${acc_path.root}/default_ncf.cf.sub", "@{ncf_inputs.default_files}" }; - bundlesequence => { configuration, initialization, default("${this.promise_filename}") }; - version => "1.0"; -} - -####################################################### - -bundle agent init -{ - vars: - "tmp" string => getenv("TEMP", 1024); - "source_template_1" string => "${this.promise_filename}_1.j2"; - "source_template_2" string => "${this.promise_filename}_2.j2"; - "expected_file_1" string => "${this.promise_filename}_1.expected"; - "expected_file_2" string => "${this.promise_filename}_2.expected"; - - "destination_file_1" string => "${tmp}/destination_test_1"; - "destination_file_2" string => "${tmp}/destination_test_2"; - "destination_file_canon_1" string => canonify("${destination_file_1}"); - "destination_file_canon_2" string => canonify("${destination_file_2}"); - - "test_variable" string => "test_value"; - "my_list" slist => { "test1", "test2" }; - - classes: - "my_global_class" expression => "any", scope => "namespace"; - -} - -####################################################### - -bundle agent test -{ - methods: - "ph1" usebundle => file_from_template_jinja2("${init.source_template_1}", "${init.destination_file_1}"); - "ph2" usebundle => file_from_template_jinja2("${init.source_template_2}", "${init.destination_file_2}"); -} - -####################################################### - -bundle agent check -{ - - vars: - "file_diff_test_1" string => "/usr/bin/diff \"${init.destination_file_1}\" \"${init.expected_file_1}\""; - "file_diff_test_2" string => "/usr/bin/diff \"${init.destination_file_2}\" \"${init.expected_file_2}\""; - - classes: - "file_ok_1" expression => returnszero("${file_diff_test_1}", "noshell"), - ifvarclass => "file_from_template_${init.destination_file_canon_1}_reached"; - "file_ok_2" expression => returnszero("${file_diff_test_2}", "noshell"), - ifvarclass => "file_from_template_${init.destination_file_canon_2}_reached"; - - "ok_1" expression => "file_ok_1.file_from_template_${init.destination_file_canon_1}_ok.!file_from_template_${init.destination_file_canon_1}_error"; - "ok_2" expression => "file_ok_2.file_from_template_${init.destination_file_canon_2}_ok.!file_from_template_${init.destination_file_canon_2}_error"; - "ok" expression => "ok_1.ok_2"; - - reports: - ok:: - "$(this.promise_filename) Pass"; - !ok_1:: - "$(this.promise_filename) FAIL: diff command doesn't returns 0 (command: ${file_diff_test_1} )"; - !ok_2:: - "$(this.promise_filename) FAIL: diff command doesn't returns 0 (command: ${file_diff_test_2} )"; - -} diff --git a/tests/acceptance/30_generic_methods/file_from_template_jinja2/file_from_template_jinja2.cf b/tests/acceptance/30_generic_methods/file_from_template_jinja2/file_from_template_jinja2.cf new file mode 100644 index 000000000..8ed3b97f2 --- /dev/null +++ b/tests/acceptance/30_generic_methods/file_from_template_jinja2/file_from_template_jinja2.cf @@ -0,0 +1,148 @@ +#################################################################################### +# Copyright 2016 Normation SAS +##################################################################################### +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, Version 3. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +##################################################################################### + +# agent_version >= 3.9 + +####################################################### +# +# Test checking if a file can be generated from a local template +# +####################################################### + +bundle common acc_path +{ + vars: + "root" string => getenv("NCF_TESTS_ACCEPTANCE", 1024); +} + +body common control +{ + inputs => { "${acc_path.root}/default.cf.sub", "${acc_path.root}/default_ncf.cf.sub", "@{ncf_inputs.default_files}" }; + bundlesequence => { configuration, initialization, default("${this.promise_filename}") }; + version => "1.0"; +} + +####################################################### + +bundle agent init +{ + vars: + "tmp" string => getenv("TEMP", 1024); + # An empty expected_file means that no destination file is expected to be created + + "source_template[0]" string => "${this.promise_dirname}/resources/test_0.source"; + "destination_file[0]" string => "${tmp}/destination_0"; + "expected_file[0]" string => "${this.promise_dirname}/resources/test_0.expected"; + "expected_result[0]" string => "repaired"; + "mode" string => "enforce"; + + # Unicode chars + "source_template[1]" string => "${this.promise_dirname}/resources/test_1.source"; + "destination_file[1]" string => "${tmp}/destination_1"; + "expected_file[1]" string => "${this.promise_dirname}/resources/test_1.expected"; + "expected_result[1]" string => "repaired"; + "mode" string => "enforce"; + + "source_template[2]" string => "${this.promise_dirname}/resources/test_2.source"; + "destination_file[2]" string => "${tmp}/destination_2"; + "expected_file[2]" string => "${this.promise_dirname}/resources/test_2.expected"; + "expected_result[2]" string => "repaired"; + "mode" string => "enforce"; + + # undefined variable + "source_template[3]" string => "${this.promise_dirname}/resources/test_3.source"; + "destination_file[3]" string => "${tmp}/destination_3"; + "expected_file[3]" string => ""; + "expected_result[3]" string => "error"; + "mode" string => "enforce"; + + # undefined source + "source_template[3]" string => "${this.promise_dirname}/resources/test_3.source_that_does_not_exists"; + "destination_file[3]" string => "${tmp}/destination_3"; + "expected_file[3]" string => ""; + "expected_result[3]" string => "error"; + "mode" string => "enforce"; + + "test_variable" string => "test_value"; + "my_list" slist => { "test1", "test2" }; + + "indices" slist => getindices("source_template"); + + classes: + "my_global_class" expression => "any", scope => "namespace"; + +} + +####################################################### + +bundle agent test +{ + vars: + "args${init.indices}" slist => { "${init.source_template[${init.indices}]}", "${init.destination_file[${init.indices}]}" }; + + methods: + "ph0" usebundle => apply_gm("file_from_template_jinja2", @{args0}, "${init.expected_result[0]}", "ph0", "${init.mode[0]}" ); + "ph1" usebundle => apply_gm("file_from_template_jinja2", @{args1}, "${init.expected_result[1]}", "ph1", "${init.mode[1]}" ); + "ph2" usebundle => apply_gm("file_from_template_jinja2", @{args2}, "${init.expected_result[2]}", "ph2", "${init.mode[2]}" ); + "ph3" usebundle => apply_gm("file_from_template_jinja2", @{args3}, "${init.expected_result[3]}", "ph3", "${init.mode[3]}" ); +} + +####################################################### + +bundle agent check +{ + + vars: + # Without guarding class it will be executed too soon + pass1:: + "hash_expected_file[${init.indices}]" string => execresult("/usr/bin/md5sum ${init.expected_file[${init.indices}]} | ${paths.path[cut]} -d' ' -f1", "useshell"); + "hash_destination_file[${init.indices}]" string => execresult("/usr/bin/md5sum ${init.destination_file[${init.indices}]} | ${paths.path[cut]} -d' ' -f1", "useshell"); + + classes: + "pass3" expression => "pass2"; + "pass2" expression => "pass1"; + "pass1" expression => "any"; + "skip_${init.indices}" expression => strcmp("${init.expected_file[${init.indices}]}", ""); + + pass2:: + "classes_ok" expression => "ph0_ok.ph1_ok.ph2_ok.ph3_ok"; + + # Test if files destination file are modified as expected + "file_ok_${init.indices}" expression => strcmp("${hash_expected_file[${init.indices}]}", "${hash_destination_file[${init.indices}]}"), + ifvarclass => "!skip_${init.indices}"; + # Test if files in error are not created + "file_ok_${init.indices}" not => fileexists("${init.destination_file[${init.indices}]}"), + ifvarclass => "skip_${init.indices}"; + + "files_not_ok" not => "file_ok_${init.indices}"; + + "ok" expression => "classes_ok.!files_not_ok"; + + reports: + pass3:: + "FAIL test ${init.indices}: '${hash_expected_file[${init.indices}]}' != '${hash_destination_file[${init.indices}]}' +${init.expected_file[${init.indices}]} != ${init.destination_file[${init.indices}]}" + ifvarclass => "!file_ok_${init.indices}.!skip_${init.indices}"; + "FAIL test ${init.indices}: file ${init.destination_file[${init.indices}]} should not be created" + ifvarclass => "!file_ok_${init.indices}.skip_${init.indices}"; + pass3.ok:: + "$(this.promise_filename) Pass"; + pass3.!ok:: + "$(this.promise_filename) Fail"; + +} diff --git a/tests/acceptance/30_generic_methods/file_from_template_jinja2.cf_1.expected b/tests/acceptance/30_generic_methods/file_from_template_jinja2/resources/test_0.expected similarity index 100% rename from tests/acceptance/30_generic_methods/file_from_template_jinja2.cf_1.expected rename to tests/acceptance/30_generic_methods/file_from_template_jinja2/resources/test_0.expected diff --git a/tests/acceptance/30_generic_methods/file_from_template_jinja2.cf_1.j2 b/tests/acceptance/30_generic_methods/file_from_template_jinja2/resources/test_0.source similarity index 100% rename from tests/acceptance/30_generic_methods/file_from_template_jinja2.cf_1.j2 rename to tests/acceptance/30_generic_methods/file_from_template_jinja2/resources/test_0.source diff --git a/tests/acceptance/30_generic_methods/file_from_template_jinja2/resources/test_1.expected b/tests/acceptance/30_generic_methods/file_from_template_jinja2/resources/test_1.expected new file mode 100644 index 000000000..cea40a34f --- /dev/null +++ b/tests/acceptance/30_generic_methods/file_from_template_jinja2/resources/test_1.expected @@ -0,0 +1,16 @@ +Same template but with extra specials characters such +as äeiöü +my var is test_value + +I can iterate over: + + +display test1 + +display test2 + + + +display this + + diff --git a/tests/acceptance/30_generic_methods/file_from_template_jinja2/resources/test_1.source b/tests/acceptance/30_generic_methods/file_from_template_jinja2/resources/test_1.source new file mode 100644 index 000000000..18b9aac3a --- /dev/null +++ b/tests/acceptance/30_generic_methods/file_from_template_jinja2/resources/test_1.source @@ -0,0 +1,16 @@ +Same template but with extra specials characters such +as äeiöü +my var is {{ vars.init.test_variable }} + +I can iterate over: + +{% for item in vars.init.my_list %} +display {{ item }} +{% endfor %} + +{% if classes.my_global_class is defined %} +display this +{% endif %} +{% if classes.does_not_exists is defined %} +do not display this +{% endif %} diff --git a/tests/acceptance/30_generic_methods/file_from_template_jinja2.cf_2.expected b/tests/acceptance/30_generic_methods/file_from_template_jinja2/resources/test_2.expected similarity index 100% rename from tests/acceptance/30_generic_methods/file_from_template_jinja2.cf_2.expected rename to tests/acceptance/30_generic_methods/file_from_template_jinja2/resources/test_2.expected diff --git a/tests/acceptance/30_generic_methods/file_from_template_jinja2.cf_2.j2 b/tests/acceptance/30_generic_methods/file_from_template_jinja2/resources/test_2.source similarity index 100% rename from tests/acceptance/30_generic_methods/file_from_template_jinja2.cf_2.j2 rename to tests/acceptance/30_generic_methods/file_from_template_jinja2/resources/test_2.source diff --git a/tests/acceptance/30_generic_methods/file_from_template_jinja2/resources/test_3.expected b/tests/acceptance/30_generic_methods/file_from_template_jinja2/resources/test_3.expected new file mode 100644 index 000000000..73df97840 --- /dev/null +++ b/tests/acceptance/30_generic_methods/file_from_template_jinja2/resources/test_3.expected @@ -0,0 +1,13 @@ +my var is test_value + +I can iterate over: + + +display test1 + +display test2 + + + +display this + diff --git a/tests/acceptance/30_generic_methods/file_from_template_jinja2/resources/test_3.source b/tests/acceptance/30_generic_methods/file_from_template_jinja2/resources/test_3.source new file mode 100644 index 000000000..e0ab633c3 --- /dev/null +++ b/tests/acceptance/30_generic_methods/file_from_template_jinja2/resources/test_3.source @@ -0,0 +1,14 @@ +my var is {{ vars.init.test_variable_that_do_not_exist}} + +I can iterate over: + +{% for item in vars.init.my_list %} +display {{ item }} +{% endfor %} + +{% if classes.my_global_class is defined %} +display this +{% endif %} +{% if classes.does_not_exists is defined %} +do not display this +{% endif %} diff --git a/tree/30_generic_methods/file_copy_from_local_source.cf b/tree/30_generic_methods/file_copy_from_local_source.cf index 99d1025e1..2a9249198 100644 --- a/tree/30_generic_methods/file_copy_from_local_source.cf +++ b/tree/30_generic_methods/file_copy_from_local_source.cf @@ -23,7 +23,7 @@ # # @parameter source Source file (absolute path on the target node) # @parameter destination Destination file (absolute path on the target node) -# +# # @class_prefix file_copy_from_local_source # @class_parameter destination @@ -50,6 +50,8 @@ bundle agent file_copy_from_local_source(source, destination) "copy without recursion" usebundle => file_copy_from_local_source_recursion("${source}", "${destination}", "0"); "reenable_reporting_${class_prefix}" usebundle => enable_reporting, ifvarclass => "should_report"; + #"old result classes" usebundle => _classes_copy("${inner_class_prefix}", "${old_class_prefix}"), + # ifvarclass => "${inner_class_prefix}_reached"; "new result classes" usebundle => _classes_copy("${inner_class_prefix}", "${class_prefix}"), ifvarclass => "${inner_class_prefix}_reached"; diff --git a/tree/30_generic_methods/file_copy_from_local_source_recursion.cf b/tree/30_generic_methods/file_copy_from_local_source_recursion.cf index 46291b3d7..9c46a70c7 100644 --- a/tree/30_generic_methods/file_copy_from_local_source_recursion.cf +++ b/tree/30_generic_methods/file_copy_from_local_source_recursion.cf @@ -24,7 +24,7 @@ # @parameter source Source file (absolute path on the target node) # @parameter destination Destination file (absolute path on the target node) # @parameter recursion Recursion depth to enforce for this path (0, 1, 2, ..., inf) -# +# # @class_prefix file_copy_from_local_source # @class_parameter destination @@ -34,15 +34,12 @@ bundle agent file_copy_from_local_source_recursion(source, destination, recursio "old_class_prefix" string => canonify("file_copy_from_local_source_${destination}"); "args" slist => { "${source}", "${destination}", "${recursion}" }; "report_param" string => join("_", args); - "full_class_prefix" string => canonify("file_copy_from_local_source_${report_param}"); + "full_class_prefix" string => canonify("file_copy_from_local_source_recursion_${report_param}"); "class_prefix" string => string_head("${full_class_prefix}", "1000"); - "full_inner_class_prefix" string => canonify("file_from_local_source_${report_param}"); + "full_inner_class_prefix" string => canonify("file_from_local_source_recursion_${report_param}"); "inner_class_prefix" string => string_head("${full_inner_class_prefix}", "1000"); - "canonified_file" string => canonify("${destination}"); - - classes: "should_report" expression => "${report_data.should_report}"; @@ -53,9 +50,13 @@ bundle agent file_copy_from_local_source_recursion(source, destination, recursio "reenable_reporting_${class_prefix}" usebundle => enable_reporting, ifvarclass => "should_report"; - "class copy" usebundle => _classes_copy("file_from_local_source_${canonified_file}", "${old_class_prefix}"), - ifvarclass => "file_from_local_source_${canonified_file}_reached"; + + "old result classes" usebundle => _classes_copy("${inner_class_prefix}", "${old_class_prefix}"), + ifvarclass => "${inner_class_prefix}_reached"; "new result classes" usebundle => _classes_copy("${inner_class_prefix}", "${class_prefix}"), ifvarclass => "${inner_class_prefix}_reached"; - "report" usebundle => _log_v3("Copying ${destination} from local ${source}, recursion level ${recursion}", "${destination}", "${old_class_prefix}", "${class_prefix}", @{args}); + + "report" usebundle => _log_v3("Copying ${destination} from local ${source}, recursion level ${recursion}", "${destination}", "${old_class_prefix}", "${class_prefix}", @{args}), + ifvarclass => "${inner_class_prefix}_reached"; + } diff --git a/tree/30_generic_methods/file_from_local_source_recursion.cf b/tree/30_generic_methods/file_from_local_source_recursion.cf index 52108b211..43affcd5a 100644 --- a/tree/30_generic_methods/file_from_local_source_recursion.cf +++ b/tree/30_generic_methods/file_from_local_source_recursion.cf @@ -22,7 +22,7 @@ # @parameter source Source file (absolute path on the target node) # @parameter destination Destination file (absolute path on the target node) # @parameter recursion Recursion depth to enforce for this path (0, 1, 2, ..., inf) -# +# # @class_prefix file_from_local_source # @class_parameter destination @@ -32,40 +32,54 @@ bundle agent file_from_local_source_recursion(source, destination, recursion) "old_class_prefix" string => canonify("file_from_local_source_${destination}"); "args" slist => { "${source}", "${destination}", "${recursion}" }; "report_param" string => join("_", args); - "full_class_prefix" string => canonify("file_from_local_source_${report_param}"); + "full_class_prefix" string => canonify("file_from_local_source_recursion_${report_param}"); "class_prefix" string => string_head("${full_class_prefix}", "1000"); + "full_inner_class_prefix" string => canonify("inner_file_from_local_source_recursion_${report_param}"); + "inner_class_prefix" string => string_head("${full_inner_class_prefix}", "1000"); + classes: # depth_search prevents from copying file/symlink # so we need to detect if we want to copy a folder or a file # If we check if it is a file, the class will always be true # event if was in fact a directory. Then, we test if it is a dir. - "is_dir_copy" expression => isdir("${destination}"); + "is_dir_copy" expression => isdir("${source}"); # We won't be able to know if it is really a directory # or not before the first iteration - "iteration_2" expression => "iteration_1"; - "iteration_1" expression => "any"; + "pass2" expression => "pass1"; + "pass1" expression => "any"; files: - !is_dir_copy:: + pass2.!is_dir_copy:: "${destination}" copy_from => ncf_local_cp_method("${source}", "digest"), - classes => classes_generic_two("${old_class_prefix}", "${class_prefix}"); + classes => classes_generic("${inner_class_prefix}"); - is_dir_copy:: + pass2.is_dir_copy:: "${destination}" copy_from => ncf_local_cp_method("${source}", "digest"), depth_search => recurse("${recursion}"), - classes => classes_generic_two("${old_class_prefix}", "${class_prefix}"); + classes => classes_generic("${inner_class_prefix}"); methods: + # inner classes must be canceled and sanitized to avoid call conflict and + # since the ncf_local_cp_method defines too much classes + pass1:: + "cancel" usebundle => _classes_cancel("${inner_class_prefix}"); + pass2:: + "sanitize" usebundle => _classes_sanitize("${inner_class_prefix}"); + + "old result classes" usebundle => _classes_copy("${inner_class_prefix}", "${old_class_prefix}"), + ifvarclass => "${inner_class_prefix}_reached"; + "new result classes" usebundle => _classes_copy("${inner_class_prefix}", "${class_prefix}"), + ifvarclass => "${inner_class_prefix}_reached"; + # The reports should only be made after we know if this is a # directory (to apply recursion) or a file - iteration_2.!is_dir_copy:: - "report" - usebundle => _log_v3("Copying ${destination} from ${source}", "${destination}", "${old_class_prefix}", "${class_prefix}", @{args}); - - iteration_2.is_dir_copy:: - "report" - usebundle => _log_v3("Copying ${destination} from local ${source}, recursion level ${recursion}", "${destination}", "${old_class_prefix}", "${class_prefix}", @{args}); + "report file" + usebundle => _log_v3("Copying ${destination} from ${source}", "${destination}", "${old_class_prefix}", "${class_prefix}", @{args}), + ifvarclass => "!is_dir_copy"; + "report directory" + usebundle => _log_v3("Copying ${destination} from local ${source}, recursion level ${recursion}", "${destination}", "${old_class_prefix}", "${class_prefix}", @{args}), + ifvarclass => "is_dir_copy"; } diff --git a/tree/30_generic_methods/file_from_template_jinja2.cf b/tree/30_generic_methods/file_from_template_jinja2.cf index ba7bec751..45606a38c 100644 --- a/tree/30_generic_methods/file_from_template_jinja2.cf +++ b/tree/30_generic_methods/file_from_template_jinja2.cf @@ -211,12 +211,11 @@ bundle agent file_from_template_jinja2(source_template, destination) { vars: - "old_class_prefix" string => canonify("file_from_template_${destination}"); "args" slist => { "${source_template}", "${destination}" }; "report_param" string => join("_", args); - "full_class_prefix" string => canonify("file_from_template_${report_param}"); + "full_class_prefix" string => canonify("file_from_template_jinja2_${report_param}"); "class_prefix" string => string_head("${full_class_prefix}", "1000"); "jinja2_script" string => "${sys.workdir}/modules/templates/jinja2-templating.py"; @@ -231,12 +230,12 @@ bundle agent file_from_template_jinja2(source_template, destination) # Here we put the output into the errors variable. As the stdout is sent to a file, the output is only stderr, # and can be used to check for errors. - "errors" string => execresult("${jinja2_script} --strict \"${source_template}\" \"${datastate_file}\" > \"${destination_tmp}\"", "useshell"), + "errors_${class_prefix}" string => execresult("${jinja2_script} --strict \"${source_template}\" \"${datastate_file}\" > \"${destination_tmp}\"", "useshell"), ifvarclass => "state_dumped_${destination_canon}_ok"; classes: # If the stderr of the python templating command was empty, we consider that the templating succeeeded. - "templating_ok" expression => strcmp("${errors}", ""); + "templating_ok_${class_prefix}" expression => strcmp("${errors_${class_prefix}}", ""); "pass3" expression => "pass2"; "pass2" expression => "pass1"; @@ -256,11 +255,15 @@ bundle agent file_from_template_jinja2(source_template, destination) methods: "disable_reporting_${class_prefix}" usebundle => disable_reporting; # Use a file copy to avoid any file size limitations of CFEngine variables. - pass2.templating_ok:: - "copy file" usebundle => file_copy_from_local_source("${destination_tmp}", "${destination}"); - pass2.templating_ok:: - "copy classes" usebundle => _classes_copy("file_copy_from_local_source_${destination_canon}", "${old_class_prefix}"); - "copy new classes" usebundle => _classes_copy("${inner_class_prefix}", "${class_prefix}"); + pass2:: + "copy file" usebundle => file_copy_from_local_source("${destination_tmp}", "${destination}"), + ifvarclass => "templating_ok_${class_prefix}"; + pass2:: + "copy classes" usebundle => _classes_copy("file_copy_from_local_source_${destination_canon}", "${old_class_prefix}"), + ifvarclass => "templating_ok_${class_prefix}"; + "copy new classes" usebundle => _classes_copy("file_copy_from_local_source_${destination_canon}", "${class_prefix}"), + ifvarclass => "templating_ok_${class_prefix}"; + #"copy new classes" usebundle => _classes_copy("${inner_class_prefix}", "${class_prefix}"); pass3:: # In non audit mode, we need to force enforce mode to remove the temp files. @@ -275,11 +278,11 @@ bundle agent file_from_template_jinja2(source_template, destination) "failure" usebundle => _classes_failure("${class_prefix}"), ifvarclass => "!${class_prefix}_reached"; - "reenable_reporting_${class_prefix}" usebundle => enable_reporting, + "reenable_reporting_${class_prefix}" usebundle => enable_reporting, ifvarclass => "should_report"; "report" usebundle => _log_v3("Build file ${destination} from jinja2 template ${source_template}", "${destination}", "${old_class_prefix}", "${class_prefix}", @{args}); # Report errors - "report" usebundle => log_rudder_mode("log_warn", "Jinja templating failed with the following error:${const.endl}${errors}", "${destination}", "${class_prefix}"), + "report" usebundle => log_rudder_mode("log_warn", "Jinja templating failed with the following error:${const.endl}${errors_${class_prefix}}", "${destination}", "${class_prefix}"), ifvarclass => "state_dumped_${destination_canon}_ok.!templating_ok"; }