Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #5378: We need a method for the mustache template #93

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
76 changes: 76 additions & 0 deletions tests/acceptance/30_generic_methods/file_from_template_mustache.cf
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#######################################################
#
# 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 => { default("${this.promise_filename}") };
version => "1.0";
}

#######################################################

bundle agent init
{
vars:
"tmp" string => getenv("TEMP", 1024);
"source_template" string => "${tmp}/source_template";
"destination_file" string => "${tmp}/destination_test";
"destination_file_canon" string => canonify("${destination_file}");

files:
"${source_template}"
create => "true",
edit_line => insert_lines("sys.host is: {{{vars.sys.host}}}");

}

#######################################################

bundle agent test
{
methods:
"ph1" usebundle => file_from_template_mustache("${init.source_template}", "${init.destination_file}");
}

#######################################################

bundle agent check
{
vars:
"file_length_check" string => "/bin/cat ${init.destination_file} | wc -l";
"file_content_check" string => "/bin/cat ${init.destination_file} | head -n1 | grep '^sys.host is: ${sys.host}$'";

classes:
# By default, should create the file if it doesn't exist
"file_exists" expression => fileexists("${init.destination_file}");
"file_length_ok"
expression => strcmp(execresult("${file_length_check}", "useshell"), "1"),
ifvarclass => canonify("file_from_template_${init.destination_file}_reached");
"file_content_ok"
expression => returnszero("${file_content_check}", "useshell"),
ifvarclass => canonify("file_from_template_${init.destination_file}_reached");

"ok" expression => "file_exists.file_length_ok.file_content_ok.file_from_template_${init.destination_file_canon}_ok.!file_from_template_${init.destination_file_canon}_error";

reports:
ok::
"$(this.promise_filename) Pass";
!ok::
"$(this.promise_filename) FAIL";
!file_exists::
"File ${init.destination_file} didn't appear to exist";
!file_length_ok::
"There was not exactly one line in ${init.destination_file}";
!file_content_ok::
"The file content in ${init.destination_file} was not exactly \"sys.host is: ${sys.host}\"";
}
14 changes: 2 additions & 12 deletions tree/30_generic_methods/file_from_template.cf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#####################################################################################
# Copyright 2013 Normation SAS
# Copyright 2014 Normation SAS
#####################################################################################
#
# This program is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -28,16 +28,6 @@

bundle agent file_from_template(source_template, destination)
{
vars:
"class_prefix" string => canonify("file_from_template_${destination}");

files:
"${destination}"
create => "true",
edit_template => "${source_template}",
classes => classes_generic("${class_prefix}");

methods:
"report" usebundle => _logger("Build file ${destination} from template ${source_template}", "${class_prefix}");

"placeholder" usebundle => file_from_template_type("${source_template}", "${destination}", "cfengine");
}
33 changes: 33 additions & 0 deletions tree/30_generic_methods/file_from_template_mustache.cf
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#####################################################################################
# Copyright 2014 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 <http://www.gnu.org/licenses/>.
#
#####################################################################################

# @name File from a mustache template
# @description This is a bundle to build a file from a mustache template
#
# @parameter source_template Source file containing a template to be expanded
# @parameter destination Destination file
#
# @class_prefix file_from_template
# @class_parameter destination
# This bundle will define a class file_from_template_${destination}_{kept,repaired,error,ok,reached}

bundle agent file_from_template_mustache(source_template, destination)
{
methods:
"placeholder" usebundle => file_from_template_type("${source_template}", "${destination}", "mustache");
}
45 changes: 45 additions & 0 deletions tree/30_generic_methods/file_from_template_type.cf
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#####################################################################################
# Copyright 2014 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 <http://www.gnu.org/licenses/>.
#
#####################################################################################

# @name File from template with type
# @description This is a bundle to build a file from a template
#
# @parameter source_template Source file containing a template to be expanded
# @parameter destination Destination file
# @parameter template_type Template type (cfengine or mustache)
#
# @class_prefix file_from_template
# @class_parameter destination
# This bundle will define a class file_from_template_${destination}_{kept,repaired,error,ok,reached}

bundle agent file_from_template_type(source_template, destination, template_type)
{
vars:
"class_prefix" string => canonify("file_from_template_${destination}");

files:
"${destination}"
create => "true",
edit_template => "${source_template}",
template_method => "${template_type}",
classes => classes_generic("${class_prefix}");

methods:
"report" usebundle => _logger("Build file ${destination} from ${template_type} template ${source_template}", "${class_prefix}");

}