From 64ecadf20539bb0a20d785d7039f4ca9872864aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Peccatte?= Date: Fri, 15 Jul 2016 09:36:29 +0200 Subject: [PATCH] Fixes #8689: Use ncf to initialise technique test --- scenario/technique.py | 8 +- scripts/cleanbox.sh | 2 + scripts/ncf | 112 ++++++++++++++++++++++ spec/spec_helper.rb | 7 ++ spec/tests/techniques/technique_init.rb | 12 +++ spec/tests/techniques/technique_remove.rb | 0 6 files changed, 137 insertions(+), 4 deletions(-) create mode 100755 scripts/ncf create mode 100644 spec/tests/techniques/technique_init.rb delete mode 100644 spec/tests/techniques/technique_remove.rb diff --git a/scenario/technique.py b/scenario/technique.py index bd900d9..4c97a8d 100755 --- a/scenario/technique.py +++ b/scenario/technique.py @@ -20,10 +20,10 @@ for host in scenario.nodes("agent"): run('localhost', 'agent_accept', Err.BREAK, ACCEPT=host) -## Run test init script -#for test in tests: -# if 'init' in test: -# run_on("all", 'techniques/technique_init', Err.BREAK, INIT=test['init']) +# Run test init script +for test in tests: + if 'init' in test: + run_on("all", 'techniques/technique_init', Err.BREAK, INIT=test['init']) # Test all techniques date0 = host_date('wait', Err.CONTINUE, "server") diff --git a/scripts/cleanbox.sh b/scripts/cleanbox.sh index cb25ed9..2520118 100755 --- a/scripts/cleanbox.sh +++ b/scripts/cleanbox.sh @@ -15,6 +15,8 @@ postclean() { fi chmod +x /usr/local/bin/rudder-setup /usr/local/bin/ncf-setup + cp /vagrant/scripts/ncf /usr/local/bin/ + chmod +x /usr/local/bin/ncf id > /tmp/xxx } diff --git a/scripts/ncf b/scripts/ncf new file mode 100755 index 0000000..15f837f --- /dev/null +++ b/scripts/ncf @@ -0,0 +1,112 @@ +#!/bin/sh + +# Run a technique file (or any cfengine file) +ncf_technique() { + file="$1" + CODE=$(cat "${file}") + BUNDLE=$(grep "^bundle agent" "${file}" | head -n 1 | perl -pe 's/^bundle agent\s+(\w+).*/$1/') + run +} + +# Run code from stdin +ncf_code() { + BUNDLE="main" + header=" +bundle agent main { + methods: +" + script=$(perl -pe 's/^(\s*)(\w+\s*\()/$1"any" usebundle => $2/') + footer=" +} +" + CODE="${header}${script}${footer}" + run +} + +# Run a single ncf method +ncf_method() { + method="$1" + shift + parameters="" + first=1 + while [ -n "$1" ] + do + [ ${first} -ne 1 ] && parameters="${parameters}," + parameters="${parameters}\"$1\"" + first=0 + shift + done + BUNDLE="main" + CODE=" +bundle agent main { + methods: + \"any\" usebundle => ${method}(${parameters}); +} +" + run +} + +# Run the buncle $BUNDLE from cfengine source $CODE +run() { + FILE=$(mktemp) + cat > "${FILE}" <<'EOF' +bundle common inputs +{ + vars: + "framework_path" string => "/var/rudder/ncf/common"; + "local_path" string => "/var/rudder/ncf/local"; + "list_compatible_inputs" string => "NCF_CACHE_PATH=/tmp /bin/sh ${framework_path}/10_ncf_internals/list-compatible-inputs"; + + "ncf_internals" string => execresult("${list_compatible_inputs} ${sys.cf_version} ${framework_path} 10_ncf_internals", "useshell"); + "cfe_basics" string => execresult("${list_compatible_inputs} ${sys.cf_version} ${framework_path} 20_cfe_basics", "useshell"); + "generic_methods" string => execresult("${list_compatible_inputs} ${sys.cf_version} ${framework_path} 30_generic_methods", "useshell"); + "it_ops_knowledge" string => execresult("${list_compatible_inputs} ${sys.cf_version} ${framework_path} 40_it_ops_knowledge", "useshell"); + + "local_generic_methods" string => execresult("${list_compatible_inputs} ${sys.cf_version} ${local_path} 30_generic_methods", "useshell"); + "local_it_ops_knowledge" string => execresult("${list_compatible_inputs} ${sys.cf_version} ${local_path} 40_it_ops_knowledge", "useshell"); + + "ncf_internals_files" slist => splitstring("${ncf_internals}", "\n", 10000); + "cfe_basics_files" slist => splitstring("${cfe_basics}", "\n", 10000); + "generic_methods_files" slist => splitstring("${generic_methods}", "\n", 10000); + "it_ops_knowledge_files" slist => splitstring("${it_ops_knowledge}", "\n", 10000); + "local_generic_methods_files" slist => splitstring("${local_generic_methods}", "\n", 10000); + "local_it_ops_knowledge_files" slist => splitstring("${local_it_ops_knowledge}", "\n", 10000); +} + +body common control +{ + inputs => { + @(inputs.ncf_internals_files), + @(inputs.cfe_basics_files), + @(inputs.generic_methods_files), + @(inputs.it_ops_knowledge_files), + @(inputs.local_generic_methods_files), + @(inputs.local_it_ops_knowledge_files) + }; + + bundlesequence => { +EOF + echo " \"${BUNDLE}\"" >> "${FILE}" + cat >> "${FILE}" <<'EOF' + }; +} + +EOF + echo "${CODE}" >> "${FILE}" + cf-agent -f "${FILE}" -KIC + code=$? + [ ${code} -eq 0 ] && rm -f "${FILE}" + exit ${code} +} + +# MAIN +# Simple but enough at the moment +if [ "$1" = "-f" ] +then + ncf_technique "$2" +elif [ "$1" = "-i" ] +then + ncf_code +else + ncf_method "$@" +fi diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 3bc0008..e993262 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -44,6 +44,13 @@ token = $params['TOKEN'] $rudderCli = 'rudder-cli --skip-verify --url=' + url.to_s + ' --token=' + token.to_s +# Functions that can be used in tests +def send_file(from, to) + `mkdir -p sendfile/` + `cp #{from} sendfile/` + host = ENV['TARGET_HOST'] + `vagrant ssh #{host} -c 'mv /vagrant/sendfile/#{from} #{to}'` +end ## monkeypatching serverspec diff --git a/spec/tests/techniques/technique_init.rb b/spec/tests/techniques/technique_init.rb new file mode 100644 index 0000000..dd7d18b --- /dev/null +++ b/spec/tests/techniques/technique_init.rb @@ -0,0 +1,12 @@ +require 'spec_helper' + +init = $params['INIT'] +send_file(init, "/tmp/init_technique.cf") + +describe "Initialise test" do + # run command + describe command("ncf -f /tmp/init_technique.cf") do + its(:exit_status) { should eq 0 } + its(:stdout) { should_not match /error/i } + end +end diff --git a/spec/tests/techniques/technique_remove.rb b/spec/tests/techniques/technique_remove.rb deleted file mode 100644 index e69de29..0000000