From 23c4bcfaa0295d1dd638eff7a4afcc6127c24604 Mon Sep 17 00:00:00 2001 From: Swann Croiset Date: Tue, 20 Oct 2015 14:34:12 +0200 Subject: [PATCH] Fix Puppet unit tests Failures: 1) Puppet::Provider::Plugin_zabbix when making an API request should fail if Zabbix returns error Failure/Error: expect { expected Puppet::Error with message matching /Zabbix API returned/, got # 'http://localhost', 'username' => 'Admin', 'password' => 'zabbix'}, {}) unsatisfied expectations: - expected exactly once, invoked twice: Puppet::Provider::Plugin_zabbix.make_request(any_parameters) satisfied expectations: - allowed any number of times, not yet invoked: #.root?(any_parameters) > with backtrace: # ./lib/puppet/provider/plugin_zabbix.rb:65:in `block in api_request' # ./lib/puppet/provider/plugin_zabbix.rb:62:in `times' # ./lib/puppet/provider/plugin_zabbix.rb:62:in `api_request' # ./spec/unit/provider/plugin_zabbix_spec.rb:35:in `block (4 levels) in ' # ./spec/unit/provider/plugin_zabbix_spec.rb:34:in `block (3 levels) in ' # ./spec/unit/provider/plugin_zabbix_spec.rb:34:in `block (3 levels) in ' 2) Puppet::Type.type(:plugin_zabbix_host) should accept string for group list Failure/Error: @zabbix_host[:groups] = 'ManagedByPuppet' Puppet::ResourceError: Parameter groups failed on Plugin_zabbix_host[testhost]: Validate method failed for class groups: undefined method `each' for "ManagedByPuppet":String # ./lib/puppet/type/plugin_zabbix_host.rb:69:in `block (3 levels) in ' # ./spec/unit/type/plugin_zabbix_host_spec.rb:59:in `block (2 levels) in ' 3) Puppet::Type.type(:plugin_zabbix_host) should not accept non-string hostname Failure/Error: expect { expected Puppet::Error with message matching /Invalid value/ but nothing was raised # ./spec/unit/type/plugin_zabbix_host_spec.rb:92:in `block (2 levels) in ' Finished in 0.12965 seconds (files took 0.65121 seconds to load) 64 examples, 3 failures Failed examples: rspec ./spec/unit/provider/plugin_zabbix_spec.rb:26 # Puppet::Provider::Plugin_zabbix when making an API request should fail if Zabbix returns error rspec ./spec/unit/type/plugin_zabbix_host_spec.rb:58 # Puppet::Type.type(:plugin_zabbix_host) should accept string for group list rspec ./spec/unit/type/plugin_zabbix_host_spec.rb:91 # Puppet::Type.type(:plugin_zabbix_host) should not accept non-string hostname Change-Id: If4be35785f1cabe063d1c6bad27c36d0d3d75137 --- .../plugin_zabbix/lib/puppet/provider/plugin_zabbix.rb | 7 +++---- .../plugin_zabbix/lib/puppet/type/plugin_zabbix_host.rb | 7 +++++++ .../plugin_zabbix/spec/unit/provider/plugin_zabbix_spec.rb | 4 ++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/deployment_scripts/puppet/modules/plugin_zabbix/lib/puppet/provider/plugin_zabbix.rb b/deployment_scripts/puppet/modules/plugin_zabbix/lib/puppet/provider/plugin_zabbix.rb index ae369e9..c579348 100644 --- a/deployment_scripts/puppet/modules/plugin_zabbix/lib/puppet/provider/plugin_zabbix.rb +++ b/deployment_scripts/puppet/modules/plugin_zabbix/lib/puppet/provider/plugin_zabbix.rb @@ -55,8 +55,7 @@ class Puppet::Provider::Plugin_zabbix < Puppet::Provider result end - def self.api_request(api, body) - retries = 10 + def self.api_request(api, body, retries=10) cooldown = 1 Puppet.info("Trying to make a request to zabbix server, will try #{retries} times with #{cooldown} seconds between tries") retries.times do |r| @@ -71,8 +70,8 @@ class Puppet::Provider::Plugin_zabbix < Puppet::Provider return result["result"] rescue => e - if r == retries - Puppet.error("Out of retries to make a request to zabbix server (#{retries})") + if r == retries - 1 + Puppet.warning("Out of retries to make a request to zabbix server (#{retries})") raise e else Puppet.warning("Could not make request to zabbix: #{e}, sleeping #{cooldown*r} (retry (##{r}/#{retries}))") diff --git a/deployment_scripts/puppet/modules/plugin_zabbix/lib/puppet/type/plugin_zabbix_host.rb b/deployment_scripts/puppet/modules/plugin_zabbix/lib/puppet/type/plugin_zabbix_host.rb index a5f85bc..eb0a083 100644 --- a/deployment_scripts/puppet/modules/plugin_zabbix/lib/puppet/type/plugin_zabbix_host.rb +++ b/deployment_scripts/puppet/modules/plugin_zabbix/lib/puppet/type/plugin_zabbix_host.rb @@ -66,6 +66,9 @@ Puppet::Type.newtype(:plugin_zabbix_host) do validate do |value| fail("groups is not an array") unless value.kind_of?(Array) or value.kind_of?(String) fail("groups array is empty") if value.empty? + if value.kind_of?(String) then + value = [value] + end value.each do |item| fail("group name is not a string") unless item.kind_of?(String) fail("group name is empty") unless item =~ /.+/ @@ -75,6 +78,10 @@ Puppet::Type.newtype(:plugin_zabbix_host) do newparam(:hostname) do desc 'Visible name of the host.' + + validate do |value| + raise(Puppet::Error, 'Invalid value') unless value.kind_of?(String) + end newvalues(/.+/) end diff --git a/deployment_scripts/puppet/modules/plugin_zabbix/spec/unit/provider/plugin_zabbix_spec.rb b/deployment_scripts/puppet/modules/plugin_zabbix/spec/unit/provider/plugin_zabbix_spec.rb index 5810f68..eecb8cb 100644 --- a/deployment_scripts/puppet/modules/plugin_zabbix/spec/unit/provider/plugin_zabbix_spec.rb +++ b/deployment_scripts/puppet/modules/plugin_zabbix/spec/unit/provider/plugin_zabbix_spec.rb @@ -27,12 +27,12 @@ describe Puppet::Provider::Plugin_zabbix do mock = {'error' => {'code' => 0, 'message' => 'test error', 'data' => 'not a real error'}} - Puppet::Provider::Plugin_zabbix.expects(:make_request).returns(mock) + Puppet::Provider::Plugin_zabbix.expects(:make_request).at_least(2).returns(mock) fake_api = {'endpoint' => 'http://localhost', 'username' => 'Admin', 'password' => 'zabbix'} expect { - cls.api_request(fake_api, {}) + cls.api_request(fake_api, {}, 2) }.to raise_error(Puppet::Error, /Zabbix API returned/) end