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 #<Mocha::ExpectationError: unexpected invocation: Puppet::Provider::Plugin_zabbix.make_request({'endpoint' => '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: #<Puppet::Util::Feature:0x27c6238>.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 <top (required)>'
         # ./spec/unit/provider/plugin_zabbix_spec.rb:34:in `block (3 levels) in <top (required)>'
     # ./spec/unit/provider/plugin_zabbix_spec.rb:34:in `block (3 levels) in <top (required)>'

  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 <top (required)>'
     # ./spec/unit/type/plugin_zabbix_host_spec.rb:59:in `block (2 levels) in <top (required)>'

  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 <top (required)>'

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
(cherry picked from commit 23c4bcfaa0)
This commit is contained in:
Swann Croiset 2015-10-20 14:34:12 +02:00 committed by Olivier Bourdon
parent 5d400ecebd
commit 8f7d1ac8c3
3 changed files with 12 additions and 6 deletions

View File

@ -53,8 +53,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|
@ -69,8 +68,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}))")

View File

@ -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

View File

@ -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