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 #5061: create generic method to check http status #64

Merged
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
@@ -0,0 +1,68 @@
#####################################################################################
#
# Test that querying an existing URL returns 200, and that a non existing returns 404
#
#####################################################################################

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:
"nonexistant_url" string => "http://www.normation.com/non_existant";
"existant_url" string => "http://www.normation.com";
"canon_nonexistant_url" string => canonify("${nonexistant_url}");
"canon_existant_url" string => canonify("${existant_url}");
"expected_nonexistant" string => "404";
"expected_existant" string => "200";
"headers" string => "";
"method" string => "GET";

}

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

bundle agent test
{

methods:
"ph" usebundle => http_request_check_status_headers("${init.method}", "${init.nonexistant_url}", "${init.expected_nonexistant}", "${init.headers}");
"ph" usebundle => http_request_check_status_headers("${init.method}", "${init.existant_url}", "${init.expected_existant}", "${init.headers}");


}

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

bundle agent check
{
classes:
"non_existant_returns_ok" expression => "http_request_check_status_headers_${init.canon_nonexistant_url}_reached.http_request_check_status_headers_${init.canon_nonexistant_url}_kept";
"existant_returns_ok" expression => "http_request_check_status_headers_${init.canon_existant_url}_reached.http_request_check_status_headers_${init.canon_existant_url}_kept";
"ok" expression => "non_existant_returns_ok.existant_returns_ok";

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

!non_existant_returns_ok::
"Wrong status for ${init.nonexistant_url}";

!existant_returns_ok::
"Wrong status for ${init.existant_url}";
}
@@ -0,0 +1,69 @@
#####################################################################################
#
# Test that invalid expectation fails, when querying an existing URL and expecting 404,
# and that a non existing expecting 200
#
#####################################################################################

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:
"nonexistant_url" string => "http://www.normation.com/non_existant";
"existant_url" string => "http://www.normation.com";
"canon_nonexistant_url" string => canonify("${nonexistant_url}");
"canon_existant_url" string => canonify("${existant_url}");
"expected_nonexistant" string => "200";
"expected_existant" string => "404";
"headers" string => "";
"method" string => "GET";

}

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

bundle agent test
{

methods:
"ph" usebundle => http_request_check_status_headers("${init.method}", "${init.nonexistant_url}", "${init.expected_nonexistant}", "${init.headers}");
"ph" usebundle => http_request_check_status_headers("${init.method}", "${init.existant_url}", "${init.expected_existant}", "${init.headers}");


}

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

bundle agent check
{
classes:
"non_existant_returns_ok" expression => "http_request_check_status_headers_${init.canon_nonexistant_url}_reached.http_request_check_status_headers_${init.canon_nonexistant_url}_not_ok";
"existant_returns_ok" expression => "http_request_check_status_headers_${init.canon_existant_url}_reached.http_request_check_status_headers_${init.canon_existant_url}_not_ok";
"ok" expression => "non_existant_returns_ok.existant_returns_ok";

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

!non_existant_returns_ok::
"Wrong status for ${init.nonexistant_url}";

!existant_returns_ok::
"Wrong status for ${init.existant_url}";
}
14 changes: 14 additions & 0 deletions tree/20_cfe_basics/ncf_lib.cf
Expand Up @@ -177,3 +177,17 @@ body copy_from ncf_remote_cp_method(from, server, method)
portnumber => "${configuration.cfengine_port}";
compare => "${method}";
}

# Define x prefixes/suffixed with promises outcome, based on return codes
body classes classes_generic_return_codes(x, kept_return_code, repaired_return_code, failed_return_code)
{
kept_returncodes => { "${kept_return_code}" };
repaired_returncodes => { "${repaired_return_code}" };
failed_returncodes => { "${failed_return_code}" };

promise_repaired => { "promise_repaired_$(x)", "$(x)_repaired", "$(x)_ok", "$(x)_reached" };
repair_failed => { "repair_failed_$(x)", "$(x)_failed", "$(x)_not_ok", "$(x)_not_kept", "$(x)_not_repaired", "$(x)_reached" };
repair_denied => { "repair_denied_$(x)", "$(x)_denied", "$(x)_not_ok", "$(x)_not_kept", "$(x)_not_repaired", "$(x)_reached" };
repair_timeout => { "repair_timeout_$(x)", "$(x)_timeout", "$(x)_not_ok", "$(x)_not_kept", "$(x)_not_repaired", "$(x)_reached" };
promise_kept => { "promise_kept_$(x)", "$(x)_kept", "$(x)_ok", "$(x)_not_repaired", "$(x)_reached" };
}
48 changes: 48 additions & 0 deletions tree/30_generic_methods/http_request_check_status_headers.cf
@@ -0,0 +1,48 @@
#####################################################################################
# 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 HTTP request check status with headers
# @description Perform a HTTP request on the URL, method and headers provided and check that the response has the expected status code (ie 200, 404, 503, etc)
#
# @parameter method Method to call the URL (GET, POST, PUT, DELETE)
# @parameter url URL to query
# @parameter expected_status Expected status code of the HTTP response
# @parameter headers Headers to include in the HTTP request (as a string, without ')

# @class_prefix http_request_check_status_headers
# @class_parameter url
# This bundle will define a http_request_check_status_headers_${url}_{kept,error,ok,reached}

bundle agent http_request_check_status_headers(method, url, expected_status, headers)
{
vars:
"class_prefix" string => canonify("http_request_check_status_headers_${url}");

commands:
"${paths.path[curl]}"
args => "-I -s -o /dev/null -w \"%{http_code}\" ${url} -X ${method} -H '${headers}' | /bin/grep -q ${expected_status}",
contain => in_shell,
# We don't define repaired class, as we only query and expect, so using 999 to avoid using this feature
classes => classes_generic_return_codes("${class_prefix}", "0", "999", "1");

methods:
"report"
usebundle => _logger("Performing a HTTP ${method} request on ${url}, expecting status ${expected_status}", "${class_prefix}");

}