diff --git a/lib/puppet/provider/check_http/check_http.rb b/lib/puppet/provider/check_http/check_http.rb index f8ab402..8fabbdc 100644 --- a/lib/puppet/provider/check_http/check_http.rb +++ b/lib/puppet/provider/check_http/check_http.rb @@ -21,35 +21,25 @@ def insync?(context, name, attribute_name, _is_hash, should_hash) context.debug("Checking whether #{attribute_name} is up-to-date") uri = URI.parse(should_hash[:url]) - # This callback provides the exception that was raised in the current try, the try_number, the elapsed_time for all tries so far, and the time in seconds of the next_interval. do_this_on_each_retry = proc do |exception, try, elapsed_time, next_interval| context.info("#{exception.class}: '#{exception.message}' - #{try} tries in #{elapsed_time} seconds and #{next_interval} seconds until the next try.") unless exception.nil? end - base_interval = 0 - retried = 0 - Retriable.retriable(tries: should_hash[:retries], max_elapsed_time: should_hash[:request_timeout], base_interval: base_interval, max_interval: should_hash[:max_backoff], + Retriable.retriable(tries: should_hash[:retries], max_elapsed_time: should_hash[:request_timeout], max_interval: should_hash[:max_backoff], multiplier: should_hash[:exponential_backoff_base], on_retry: do_this_on_each_retry) do - retried += 1 - # Update the check_http provider to wait for backoff ** (exponential_backoff_base * (retries - 1) seconds between attempts (up to max_backoff) - base_interval = should_hash[:backoff]**should_hash[:exponential_backoff_base] * (retried - 1) - context.info("The base_interval is '#{base_interval}' and retrying for '#{retried}' time") response = Net::HTTP.get_response(uri) - # Success being defined as having one of the expected_statuses and the body of the response matches body_matcher - unless response.nil? - unless should_hash[:expected_statuses].include? response.code.to_i - raise Puppet::Error, "check_http response code check failed. The return response '#{response.code}' is not matching with the expected_statuses '#{should_hash[:expected_statuses]}.to_s'" - end - context.debug("The return response '#{response.code}' is matching with the expected_statuses '#{should_hash[:expected_statuses]}'") - unless response.body.match(should_hash[:body_matcher]) - raise Puppet::Error, "check_http response body check failed. The return response body '#{response.body[0..99]}' is not matching body_matcher '#{should_hash[:body_matcher].to_s}'" - end - context.debug("The return response body '#{response.body[0..99]}' is matching with body_matcher '#{should_hash[:body_matcher].to_s}'") - context.debug("Successfully connected to '#{name}'") - return true + unless should_hash[:expected_statuses].include? response.code.to_i + raise Puppet::Error, "check_http response code check failed. The return response '#{response.code}' is not matching with the expected_statuses '#{should_hash[:expected_statuses]}.to_s'" + end + context.debug("The return response '#{response.code}' is matching with the expected_statuses '#{should_hash[:expected_statuses]}'") + unless response.body.match(should_hash[:body_matcher]) + raise Puppet::Error, "check_http response body check failed. The return response body '#{response.body[0..99]}' is not matching body_matcher '#{should_hash[:body_matcher].to_s}'" end + context.debug("The return response body '#{response.body[0..99]}' is matching with body_matcher '#{should_hash[:body_matcher].to_s}'") + context.debug("Successfully connected to '#{name}'") + return true end false end diff --git a/spec/unit/puppet/provider/check_http/check_http_spec.rb b/spec/unit/puppet/provider/check_http/check_http_spec.rb index 58e4f8d..d863752 100644 --- a/spec/unit/puppet/provider/check_http/check_http_spec.rb +++ b/spec/unit/puppet/provider/check_http/check_http_spec.rb @@ -1,7 +1,6 @@ # frozen_string_literal: true require 'spec_helper' -require 'pry' require 'webmock/rspec' ensure_module_defined('Puppet::Provider::CheckHttp') @@ -34,10 +33,9 @@ 'User-Agent'=>'Ruby' }).to_return(status: 200, body: "Google", headers: {}) expect(context).to receive(:debug).with('Checking whether foo is up-to-date') - expect(context).to receive(:info).with("The base_interval is '0' and retrying for '1' time") - expect(context).to receive(:info).with("The return response '200' is matching with the expected_statuses '[200]'") - expect(context).to receive(:info).with("The return response body 'Google' is matching with body_matcher '(?-mix:Google).to_s'") - expect(context).to receive(:info).with("Successfully connected to 'foo'") + expect(context).to receive(:debug).with("The return response '200' is matching with the expected_statuses '[200]'") + expect(context).to receive(:debug).with("The return response body 'Google' is matching with body_matcher '(?-mix:Google)'") + expect(context).to receive(:debug).with("Successfully connected to 'foo'") expect(provider.insync?(context, 'foo', 'foo', valid_hash, valid_hash)).to be(true) end end @@ -52,10 +50,9 @@ 'Host'=>'abc.test.net', 'User-Agent'=>'Ruby' }).to_return(status: 500, body: "invalidbody", headers: {}) - allow(context).to receive(:info) + allow(context).to receive(:debug) allow(context).to receive(:debug) expect(context).to receive(:debug).with('Checking whether foo is up-to-date') - expect(context).to receive(:info).with("The base_interval is '0' and retrying for '1' time") expect { provider.insync?(context, 'foo', 'foo', invalid_hash, invalid_hash) }.to raise_error(/check_http response code check failed./) end end @@ -70,18 +67,17 @@ 'Host'=>'abc.test.net', 'User-Agent'=>'Ruby' }).to_return(status: 200, body: "invalidbody", headers: {}) - allow(context).to receive(:info) + allow(context).to receive(:debug) allow(context).to receive(:debug) expect(context).to receive(:debug).with('Checking whether foo is up-to-date') - expect(context).to receive(:info).with("The base_interval is '0' and retrying for '1' time") - expect(context).to receive(:info).with("The return response '200' is matching with the expected_statuses '[200]'") + expect(context).to receive(:debug).with("The return response '200' is matching with the expected_statuses '[200]'") expect { provider.insync?(context, 'foo', 'foo', invalid_hash, invalid_hash) }.to raise_error(/check_http response body check failed./) end end describe 'insync?(context, name, attribute_name, is_hash, should_hash) with Retry' do it 'processes resources' do - allow(context).to receive(:info) + allow(context).to receive(:debug) allow(context).to receive(:debug) expect(context).to receive(:debug).with('Checking whether foo is up-to-date') stub_request(:get, invalid_uri). @@ -92,9 +88,10 @@ 'Host'=>'abc.test.net', 'User-Agent'=>'Ruby' }).to_raise(StandardError) - expect(context).to receive(:info).with("The base_interval is '0' and retrying for '1' time") - expect(context).to receive(:info).with("The base_interval is '1' and retrying for '2' time") - expect(context).to receive(:info).with("The base_interval is '2' and retrying for '3' time") + expect(context).to receive(:info).with(/StandardError: 'Exception from WebMock' - 1 tries/) + expect(context).to receive(:info).with(/StandardError: 'Exception from WebMock' - 2 tries/) + expect(context).to receive(:info).with(/StandardError: 'Exception from WebMock' - 3 tries/) + expect { provider.insync?(context, 'foo', 'foo', invalid_hash, invalid_hash) }.to raise_error(StandardError) end end