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 #13622: Add a flag library #847

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
84 changes: 84 additions & 0 deletions tests/acceptance/20_cfe_basics/flag_lib.cf
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#######################################################
#
# Test flag_lib
#
#######################################################

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:
"existing" string => '{ "already": { "created": "2018-10-15T16:52:20+0200" } }';

methods:
"clean" usebundle => file_absent("${configuration.flag_file}");
}

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

bundle agent test
{

methods:
"read before"
usebundle => get_flag("test"),
useresult => "get1";
"reserve"
usebundle => reserve_flag("test"),
useresult => "reserve2";
"read after"
usebundle => get_flag("test"),
useresult => "get3";
"reserve twice"
usebundle => reserve_flag("test"),
useresult => "reserve4";
"set flag"
usebundle => set_flag("test"),
useresult => "set5";
"read after set"
usebundle => get_flag("test"),
useresult => "get6";
"set another flag without reservation"
usebundle => set_flag("test2"),
useresult => "set7";

"clean" usebundle => file_content("${configuration.flag_file}", "${init.existing}", "true");
"read an existing condition"
usebundle => get_flag("already"),
useresult => "get8";
}

bundle agent check
{
classes:
"ok_1" expression => strcmp("${test.get1[1]}", "free");
"ok_2" expression => strcmp("${test.reserve2[1]}", "ok");
"ok_3" expression => strcmp("${test.get3[1]}", "set");
"ok_4" expression => strcmp("${test.reserve4[1]}", "failed");
"ok_5" expression => strcmp("${test.set5[1]}", "ok");
"ok_6" expression => strcmp("${test.get6[1]}", "set");
"ok_7" expression => strcmp("${test.set7[1]}", "notreserved");
"ok_8" expression => strcmp("${test.get8[1]}", "set");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is very neat


"ok" expression => "ok_1.ok_2.ok_3.ok_4.ok_5.ok_6.ok_7.ok_8";

reports:
ok::
"$(this.promise_filename) Pass";
!ok::
"$(this.promise_filename) FAIL";
}

7 changes: 7 additions & 0 deletions tests/acceptance/default_ncf.cf.sub
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ bundle common ncf_inputs

}

# Load a custom ncf.conf for tests
Copy link
Member Author

@amousset amousset Oct 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was needed to use a different flag file.

bundle common ncf_configuration {
vars:
"ncf_configuration_basedir" string => dirname("${this.promise_filename}");
"ncf_configuration_file" string => "${ncf_configuration_basedir}/ncf.conf";
}

bundle common test_utils {

vars:
Expand Down
19 changes: 19 additions & 0 deletions tests/acceptance/ncf.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# This file can be used to adjust some elements in ncf.
#
# This is a Mustache template file, and allows variation
# of configuration based on CFEngine classes
# see http://mustache.github.io/ for the full mustache specification
# and https://docs.cfengine.com/docs/3.6/reference-promise-types-files.html for
# CFEngine implementation
#
# Which logger(s) should be used in ncf?
# Comma separated list, default is '_log_default', use '' to disable all loggers
loggers=_log_default
# Which abort handler(s) should be used in ncf?
# Comma separated list, default is '_abort_default', use '' to disable all abort handlers
abort_handlers=_abort_default
# Which port should be for CFEngine connections/data transfers
# default is 5308
cfengine_port=5308
# Where to store persistent flags, default is '/var/rudder/agent-data/flags.json'
flag_file=/tmp/flags.json
34 changes: 18 additions & 16 deletions tree/10_ncf_internals/configuration.cf
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,18 @@ bundle agent configuration
{

vars:
# We expand the template in the agent folder, so that it is not
# overwritten by the policy server expanded version, and if there are multiple
# agents on the node, each conf file does not compete
"ncf_configuration_file" string => "${sys.workdir}/ncf.conf.cache";

pass1.!ncf_override_conf_file_defined::
"ncf_configuration_basedir" string => dirname("${this.promise_filename}");

"ncf_configuration_file_source" string => "${ncf_configuration_basedir}/../ncf.conf";

pass1.ncf_override_conf_file_defined::
"ncf_configuration_file_source" string => "${ncf_configuration.ncf_configuration_file}";

pass1::
# We expand the template in the agent folder, so that it is not
# overwritten by the policy server expanded version, and if there are multiple
# agents on the node, each conf file does not compete
"ncf_configuration_file" string => "${sys.workdir}/ncf.conf.cache";
"ncf_configuration_file_source" string => "${ncf_configuration.ncf_configuration_file}";

pass2::
# Read all entries from the configuration file
Expand All @@ -47,18 +45,21 @@ bundle agent configuration
"enabled_loggers" slist => splitstring("${ncf_config[loggers][1]}",",","10");
"enabled_abort_handlers" slist => splitstring("${ncf_config[abort_handlers][1]}",",","10");
"cfengine_port" string => "${ncf_config[cfengine_port][1]}";
"flag_file" string => "${ncf_config[flag_file][1]}";

# Add a default empty list to allow skipping those handlers
pass3.enabled_loggers_empty::
pass2.enabled_loggers_empty::
"enabled_loggers" slist => {};
pass3.enabled_abort_handlers_empty::
pass2.enabled_abort_handlers_empty::
"enabled_abort_handlers" slist => {};
pass3.!enabled_loggers_defined::
pass2.!enabled_loggers_defined::
"enabled_loggers" slist => { "_log_default" };
pass3.!enabled_abort_handlers_defined::
pass2.!enabled_abort_handlers_defined::
"enabled_abort_handlers" slist => { "_abort_default" };
pass3.!cfengine_port_defined::
pass2.!cfengine_port_defined::
"cfengine_port" string => "5308";
pass2.!flag_file_defined::
"flag_file" string => "/var/rudder/agent-data/flags.json";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need something to create this folder

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, this should be part of the system techniques I think


any::
# Constants
Expand All @@ -69,12 +70,12 @@ bundle agent configuration
"trace" string => "[TRACE]";

classes:
"ncf_override_conf_file_defined" expression => fileexists("${ncf_configuration.ncf_configuration_file}");

"pass3" expression => "pass1";
"pass3" expression => "pass2";
"pass2" expression => "pass1";
"pass1" expression => "any";

"ncf_override_conf_file_defined" expression => fileexists("${ncf_configuration.ncf_configuration_file}");

# Define ncf verbosity classes according to classes
# defined by the agent after verbosity options (-I, -v, -d)
"info" expression => "inform_mode",
Expand All @@ -90,7 +91,7 @@ bundle agent configuration
"info" expression => "debug",
scope => "namespace";

pass3::
pass2::
# this line will not be evaluated by cfengine if destination_prefix contains a variable that does not exist
# and it will always be evaluated to true otherwise
# Using the source variable for empty check to allow trailing comma
Expand All @@ -99,6 +100,7 @@ bundle agent configuration
"enabled_abort_handlers_defined" expression => strcmp("${enabled_abort_handlers}", "${enabled_abort_handlers}");
"enabled_abort_handlers_empty" expression => strcmp("${ncf_config[abort_handlers][1]}", "");
"cfengine_port_defined" not => strcmp("${cfengine_port}", "${ncf_config[cfengine_port][1]}");
"flag_file_defined" not => strcmp("${flag_file}", "${ncf_config[flag_file][1]}");

# We cannot use the generic method to expand template, as it relies on the logger,
# which is not yet defined
Expand Down
1 change: 1 addition & 0 deletions tree/10_ncf_internals/initialization.cf
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ bundle agent initialization
# the ncf_init_* classes can be used to get the agregated result of ncf intialization
"copy classes for reporting" usebundle => _classes_copy("ncf_internals_modules_update", "ncf_init");
"init dry_run context" usebundle => dry_run_initialization;
"init flag context" usebundle => flag_initialization;

reports:
info::
Expand Down
Loading