Ask Your Question
0

Puppetquerydb rspec tests

asked 2016-11-17 05:15:16 -0500

run2cmd gravatar image

I use Puppet 3.8.6 and would like to use benefits of PuppetDB query in my manifest. I have below code:

$icinga_servers = query_nodes('Class[icinga2_profile::server]', 'ipaddress')

class { '::icinga2::nrpe':                
  nrpe_allowed_hosts   => $icinga_servers,
  nrpe_purge_unmanaged => true,           
}                                         

I have rspec that tries to test this:

it 'Install NRPE' do
  is_expected.to contain_class('icinga2::nrpe').with({
    'nrpe_allowed_hosts'   => ['127.0.0.1'],
    'nrpe_purge_unmanaged' => true,
  })
end

However that fail with

 1) icinga2_profile::client with icinga2::enable true allow localhost only Install NRPE
     Failure/Error: resp = http.get(uri, headers)

     Puppet::Error:
       getaddrinfo: Temporary failure in name resolution at /root/icinga2_profile/spec/fixtures/modules/icinga2_profile/manifests/client.pp:9 on node client
     # /usr/local/rvm/gems/ruby-2.1.9/gems/puppet-3.8.6/lib/puppet/network/http/connection.rb:208:in `execute_request'
     # /usr/local/rvm/gems/ruby-2.1.9/gems/puppet-3.8.6/lib/puppet/network/http/connection.rb:176:in `block (2 levels) in request_with_redirects'
     # /usr/local/rvm/gems/ruby-2.1.9/gems/puppet-3.8.6/lib/puppet/network/http/connection.rb:219:in `block in with_connection'
     # /usr/local/rvm/gems/ruby-2.1.9/gems/puppet-3.8.6/lib/puppet/network/http/nocache_pool.rb:15:in `with_connection'
     # /usr/local/rvm/gems/ruby-2.1.9/gems/puppet-3.8.6/lib/puppet/network/http/connection.rb:218:in `with_connection'
     # /usr/local/rvm/gems/ruby-2.1.9/gems/puppet-3.8.6/lib/puppet/network/http/connection.rb:173:in `block in request_with_redirects'
     # /usr/local/rvm/gems/ruby-2.1.9/gems/puppet-3.8.6/lib/puppet/network/http/connection.rb:170:in `upto'
     # /usr/local/rvm/gems/ruby-2.1.9/gems/puppet-3.8.6/lib/puppet/network/http/connection.rb:170:in `request_with_redirects'
     # /usr/local/rvm/gems/ruby-2.1.9/gems/puppet-3.8.6/lib/puppet/network/http/connection.rb:76:in `get'
     # ./spec/fixtures/modules/puppetdbquery/lib/puppetdb/connection.rb:106:in `query'
     # ./spec/fixtures/modules/puppetdbquery/lib/puppetdb/connection.rb:43:in `facts'
     # ./spec/fixtures/modules/puppetdbquery/lib/puppet/parser/functions/query_nodes.rb:28:in `block in <top (required)="">'
     # /usr/local/rvm/gems/ruby-2.1.9/gems/puppet-3.8.6/lib/puppet/parser/functions.rb:164:in `block (2 levels) in newfunction'
     # /usr/local/rvm/gems/ruby-2.1.9/gems/puppet-3.8.6/lib/puppet/util/profiler/around_profiler.rb:58:in `profile'
     # /usr/local/rvm/gems/ruby-2.1.9/gems/puppet-3.8.6/lib/puppet/util/profiler.rb:51:in `profile'
     # /usr/local/rvm/gems/ruby-2.1.9/gems/puppet-3.8.6/lib/puppet/parser/functions.rb:157:in `block in newfunction'
     # /usr/local/rvm/gems/ruby-2.1.9/gems/puppet-3.8.6/lib/puppet/parser/ast/function.rb:42:in `evaluate'
     # /usr/local/rvm/gems/ruby-2.1.9/gems/puppet-3.8.6/lib/puppet/parser/ast.rb:61:in `safeevaluate'
     # /usr/local/rvm/gems/ruby-2.1.9/gems/puppet-3.8.6/lib/puppet/parser/ast/vardef.rb:14:in `evaluate'
     # /usr/local/rvm/gems/ruby-2.1.9/gems/puppet-3.8.6/lib/puppet/parser/ast.rb:61:in `safeevaluate'
     # /usr/local/rvm/gems/ruby-2.1.9 ...
(more)
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2016-11-18 04:46:44 -0500

run2cmd gravatar image

I did not found a method to actually mock entire PuppetDB, however I found a solution to mock query_nodes function. I have added below code to my rspec tests.

before(:each) do
  Puppet::Parser::Functions.newfunction(:query_nodes, :type => :rvalue) { |args|
    raise ArgumentError, 'expect Class[icinga2_profile::server]' unless args[0] == 'Class[icinga2_profile::server]'
    raise ArgumentError, 'expect ipaddress' unless args[1] == 'ipaddress'
    ['10.10.10.1']
  }
end

This is not entirely how it should be, but It's enough for testing rest of your manifest. Hope it helps anyone.

edit flag offensive delete link more

Comments

Hi. I have the same problem, but I don't know where to put the "before(:each) do" code. In which files?

Cristian Falcas gravatar imageCristian Falcas ( 2017-04-05 07:44:37 -0500 )edit

Found it. It's suppose to be after "describe "

Cristian Falcas gravatar imageCristian Falcas ( 2017-04-05 07:51:47 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-11-17 05:15:16 -0500

Seen: 34 times

Last updated: Nov 18 '16