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 #8689: Use ncf to initialise technique test #68

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions scenario/technique.py
Expand Up @@ -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")
Expand Down
2 changes: 2 additions & 0 deletions scripts/cleanbox.sh
Expand Up @@ -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
}
Expand Down
112 changes: 112 additions & 0 deletions 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
7 changes: 7 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -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

Expand Down
12 changes: 12 additions & 0 deletions 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
Copy link
Member

Choose a reason for hiding this comment

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

I see -t in the ncf options for this.

its(:exit_status) { should eq 0 }
its(:stdout) { should_not match /error/i }
end
end
Empty file.