forked from Normation/ncf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
229 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
####################################################### | ||
# | ||
# 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 { | ||
methods: | ||
"clean" usebundle => file_absent("/tmp/flags.json"); | ||
} | ||
|
||
####################################################### | ||
|
||
bundle agent test | ||
{ | ||
|
||
methods: | ||
"read before" | ||
usebundle => get_flag("test"), | ||
useresult => "free1"; | ||
"reserve" | ||
usebundle => reserve_flag("test"); | ||
"read after" | ||
usebundle => get_flag("test"), | ||
useresult => "free2"; | ||
# "reserve twice" | ||
# usebundle => reserve_flag("test"); | ||
# "read after second" | ||
# usebundle => read_flag("test"), | ||
# useresult => "free3"; | ||
"set flag" | ||
usebundle => set_flag("test"); | ||
"read after set" | ||
usebundle => get_flag("test"), | ||
useresult => "free4"; | ||
} | ||
|
||
bundle agent check | ||
{ | ||
classes: | ||
"ok_1" expression => strcmp("${test.free1[1]}", "free"); | ||
"ok_2" expression => strcmp("${test.free2[1]}", "set"); | ||
"ok_4" expression => strcmp("${test.free4[1]}", "set"); | ||
|
||
"ok" expression => "ok_1.ok_2.ok_4"; | ||
|
||
reports: | ||
ok:: | ||
"$(this.promise_filename) Pass"; | ||
!ok:: | ||
"$(this.promise_filename) FAIL"; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
##################################################################################### | ||
# Copyright 2018 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/>. | ||
# | ||
##################################################################################### | ||
|
||
# Flag library | ||
|
||
# Flags variable is stored in the ncf_flags bundle | ||
|
||
# This bundle initializes the context for push/pop | ||
# It is called in 10_ncf_internals/initialization.cf | ||
bundle agent flag_initialization { | ||
vars: | ||
# designed to be incremented when a global unique ID is needed | ||
"flag_unique.value" string => "1"; | ||
} | ||
|
||
bundle agent reserve_flag(name) { | ||
vars: | ||
"cname" string => canonify("${name}"); | ||
|
||
"flag_unique.value" string => eval("${flag_unique.value}+1", "math", "infix"); | ||
|
||
classes: | ||
"flag_reserved_${cname}" expression => "any", | ||
# 1 day | ||
persistence => "1440"; | ||
|
||
"pass1" expression => "any"; | ||
|
||
methods: | ||
"${flag_unique.value}" usebundle => read_flags; | ||
|
||
|
||
# Read flags | ||
# Check if flag is defined | ||
# Check if persistent class is defined | ||
|
||
# Define persistent class | ||
} | ||
|
||
bundle agent get_flag(name) { | ||
vars: | ||
"cname" string => canonify("${name}"); | ||
"flag_unique.value" string => eval("${flag_unique.value}+1", "math", "infix"); | ||
|
||
classes: | ||
"is_set" expression => isvariable("ncf_flags.data[${cname}]"); | ||
"pass1" expression => "any"; | ||
|
||
methods: | ||
"${flag_unique.value}" usebundle => read_flags; | ||
|
||
reports: | ||
"set" | ||
bundle_return_value_index => "1", | ||
comment => "${flag_unique.value}", | ||
if => "is_set|flag_reserved_${cname}"; | ||
|
||
"free" | ||
bundle_return_value_index => "1", | ||
comment => "${flag_unique.value}", | ||
if => "!is_set.!flag_reserved_${cname}"; | ||
info:: | ||
"Flag '${name}' is set" | ||
comment => "${flag_unique.value}", | ||
if => "is_set"; | ||
|
||
"Flag '${name}' is reserved" | ||
comment => "${flag_unique.value}", | ||
if => "!is_set.flag_reserved_${cname}"; | ||
|
||
"Flag '${name}' is free" | ||
comment => "${flag_unique.value}", | ||
if => "!is_set.!flag_reserved_${cname}"; | ||
} | ||
|
||
bundle agent set_flag(name) { | ||
vars: | ||
"cname" string => canonify("${name}"); | ||
"time" string => strftime("localtime", "%FT%T%z", "${sys.systime}"); | ||
"flag" data => '{ "${cname}": { "created": "${time}" } }'; | ||
pass2:: | ||
"output" data => mergedata("ncf_flags.data", "flag"); | ||
|
||
classes: | ||
"pass3" expression => "pass2"; | ||
"pass2" expression => "pass1"; | ||
"pass1" expression => "any"; | ||
|
||
files: | ||
pass3:: | ||
"${configuration.flag_file}" | ||
create => "true", | ||
template_method => "mustache", | ||
edit_template => "${sys.workdir}/modules/templates/datastate.json.tpl", | ||
template_data => "@{set_flag.output}", | ||
edit_defaults => no_backup; | ||
# TODO handle errors | ||
|
||
methods: | ||
"${flag_unique.value}" usebundle => read_flags; | ||
|
||
reports: | ||
info:: | ||
"Setting flag '${cname}' (created ${time})"; | ||
} | ||
|
||
bundle agent read_flags { | ||
vars: | ||
"flag_unique.value" string => eval("${flag_unique.value}+1", "math", "infix"); | ||
|
||
# Pass3 to avoid reading the json at each pass... | ||
pass3.file_exists:: | ||
"ncf_flags.data" data => readjson("${configuration.flag_file}"), | ||
comment => "${flag_unique.value}"; | ||
pass3.!file_exists:: | ||
"ncf_flags.data" data => '{}'; | ||
|
||
classes: | ||
"file_exists" expression => fileexists("${configuration.flag_file}"); | ||
|
||
"pass3" expression => "pass2"; | ||
"pass2" expression => "pass1"; | ||
"pass1" expression => "any"; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters