2

I am new to rspec-puppet, and i am beginning with test writing:

I wrote this code:

describe 'rcjenkins' do
  let(:facts) do
    {
      :osfamily => 'RedHat'
    }
  end
  let(:pre_condition) do
    "$facts = #{facts}"
  end

  let(:params) do
    {
      :host => "127.0.0.1",
      :github_token => "valor",

    }
  end
  it do
    is_expected.to contain_class('rcjenkins::nginx')
  end
end

which should test:

class rcjenkins($host, $github_token) {
  include rcjenkins::package

  class { 'nginx': }

  nginx::resource::upstream { 'jenkins':
    members => [
      'localhost:8080'
    ]
  }

  nginx::resource::vhost { $host:
    proxy => 'http://jenkins'
  }

  nginx::resource::location { 'jenkins_root':
    ensure         => present,
    vhost          => $host,
    location       => '/status',
    location_alias => '/usr/share/nginx/html'
  }

This was more of a "test of the test", but things keep failing. i am getting:

  1) rcjenkins Treco should contain Class[rcjenkins::nginx]
     Failure/Error: is_expected.to contain_class('rcjenkins::nginx')

     Puppet::ParseErrorWithIssue:
       Could not parse for environment rp_env: Syntax error at ':' at line 1:11 on node localhost.localdomain
     # /home/luis.brandao/.rvm/gems/ruby-2.2.6/gems/puppet-4.9.2/lib/puppet/pops/parser/parser_support.rb:127:in `on_error'
     # /home/luis.brandao/.rvm/gems/ruby-2.2.6/gems/puppet-4.9.2/lib/puppet/pops/parser/lexer2.rb:712:in `scan'
     # /home/luis.brandao/.rvm/gems/ruby-2.2.6/gems/puppet-4.9.2/lib/puppet/pops/parser/parser_support.rb:240:in `_parse'
     # /home/luis.brandao/.rvm/gems/ruby-2.2.6/gems/puppet-4.9.2/lib/puppet/pops/parser/parser_support.rb:134:in `parse_string'
     # /home/luis.brandao/.rvm/gems/ruby-2.2.6/gems/puppet-4.9.2/lib/puppet/pops/parser/evaluating_parser.rb:28:in `parse_string'
     # /home/luis.brandao/.rvm/gems/ruby-2.2.6/gems/puppet-4.9.2/lib/puppet/parser/e4_parser_adapter.rb:29:in `parse'
     # /home/luis.brandao/.rvm/gems/ruby-2.2.6/gems/puppet-4.9.2/lib/puppet/node/environment.rb:507:in `perform_initial_import'
     # /home/luis.brandao/.rvm/gems/ruby-2.2.6/gems/puppet-4.9.2/lib/puppet/node/environment.rb:247:in `known_resource_types'
     # /home/luis.brandao/.rvm/gems/ruby-2.2.6/gems/puppet-4.9.2/lib/puppet/parser/compiler.rb:767:in `block in initvars'
     # /home/luis.brandao/.rvm/gems/ruby-2.2.6/gems/puppet-4.9.2/lib/puppet/context.rb:65:in `override'
     # /home/luis.brandao/.rvm/gems/ruby-2.2.6/gems/puppet-4.9.2/lib/puppet.rb:293:in `override'
     # /home/luis.brandao/.rvm/gems/ruby-2.2.6/gems/puppet-4.9.2/lib/puppet/parser/compiler.rb:765:in `initvars'
     # /home/luis.brandao/.rvm/gems/ruby-2.2.6/gems/puppet-4.9.2/lib/puppet/parser/compiler.rb:422:in `initialize'
     # /home/luis.brandao/.rvm/gems/ruby-2.2.6/gems/puppet-4.9.2/lib/puppet/parser/compiler.rb:33:in `new'
     # /home/luis.brandao/.rvm/gems/ruby-2.2.6/gems/puppet-4.9.2/lib/puppet/parser/compiler.rb:33:in `compile'
     # /home/luis.brandao/.rvm/gems/ruby-2.2.6/gems/puppet-4.9.2/lib/puppet/indirector/catalog/compiler.rb:266:in `block (2 levels) in compile'
     # /home/luis.brandao/.rvm/gems/ruby-2.2.6/gems/puppet-4.9.2/lib/puppet/util/profiler/around_profiler.rb:58:in `profile'
     # /home/luis.brandao/.rvm/gems/ruby-2.2.6/gems/puppet-4.9.2/lib/puppet/util/profiler.rb:51:in `profile'
     # /home/luis.brandao/.rvm/gems/ruby-2.2.6/gems/puppet-4.9.2/lib/puppet/indirector/catalog/compiler.rb:264:in `block in compile'
     # /home/luis.brandao/.rvm/gems/ruby-2.2.6/gems/puppet-4.9.2/lib/puppet/util.rb:223:in `block in benchmark'
     # /home/luis.brandao/.rvm/gems/ruby-2.2.6/gems/puppet-4.9.2/lib/puppet/util.rb:222:in `benchmark'
     # /home/luis.brandao/.rvm/gems/ruby-2.2.6/gems/puppet-4.9.2/lib/puppet/indirector/catalog/compiler.rb:262:in `compile'
     # /home/luis.brandao/.rvm/gems/ruby-2.2.6/gems/puppet-4.9.2/lib/puppet/indirector/catalog/compiler.rb:53:in `find'
     # /home/luis.brandao/.rvm/gems/ruby-2.2.6/gems/puppet-4.9.2/lib/puppet/indirector/indirection.rb:194:in `find'
     # /home/luis.brandao/.rvm/gems/ruby-2.2.6/gems/rspec-puppet-2.5.0/lib/rspec-puppet/adapters.rb:74:in `catalog'
     # /home/luis.brandao/.rvm/gems/ruby-2.2.6/gems/rspec-puppet-2.5.0/lib/rspec-puppet/adapters.rb:148:in `catalog'
     # /home/luis.brandao/.rvm/gems/ruby-2.2.6/gems/rspec-puppet-2.5.0/lib/rspec-puppet/support.rb:203:in `build_catalog_without_cache'
     # /home/luis.brandao/.rvm/gems/ruby-2.2.6/gems/rspec-puppet-2.5.0/lib/rspec-puppet/support.rb:212:in `block in build_catalog'
     # /home/luis.brandao/.rvm/gems/ruby-2.2.6/gems/rspec-puppet-2.5.0/lib/rspec-puppet/cache.rb:17:in `call'is_expected.to contain_class('rcjenkins::nginx')
     # /home/luis.brandao/.rvm/gems/ruby-2.2.6/gems/rspec-puppet-2.5.0/lib/rspec-puppet/cache.rb:17:in `get'
     # /home/luis.brandao/.rvm/gems/ruby-2.2.6/gems/rspec-puppet-2.5.0/lib/rspec-puppet/support.rb:211:in `build_catalog'
     # /home/luis.brandao/.rvm/gems/ruby-2.2.6/gems/rspec-puppet-2.5.0/lib/rspec-puppet/support.rb:30:in `block in load_catalogue'
     # /home/luis.brandao/.rvm/gems/ruby-2.2.6/gems/rspec-puppet-2.5.0/lib/rspec-puppet/support.rb:178:in `with_vardir'
     # /home/luis.brandao/.rvm/gems/ruby-2.2.6/gems/rspec-puppet-2.5.0/lib/rspec-puppet/support.rb:19:in `load_catalogue'
     # /home/luis.brandao/.rvm/gems/ruby-2.2.6/gems/rspec-puppet-2.5.0/lib/rspec-puppet/example/class_example_group.rb:7:in `catalogue'
     # /home/luis.brandao/.rvm/gems/ruby-2.2.6/gems/rspec-puppet-2.5.0/lib/rspec-puppet/support.rb:11:in `block in subject'
     # /home/luis.brandao/.rvm/gems/ruby-2.2.6/gems/rspec-puppet-2.5.0/lib/rspec-puppet/matchers/create_generic.rb:82:in `call'
     # /home/luis.brandao/.rvm/gems/ruby-2.2.6/gems/rspec-puppet-2.5.0/lib/rspec-puppet/matchers/create_generic.rb:82:in `matches?'
     # ./spec/classes/rcjenkins_spec.rb:39:in `block (3 levels) in <top (required)>'

Line 39 is is_expected.to contain_class('rcjenkins::nginx'). Anyone know what is happening?

Edit: I did also notice, that ANYTHING i change the test to, give me the same error. What i am missing? what is rp_env?

Techmago
  • 380
  • 4
  • 18
  • Besides the issue that Dominic Cleal brought up with your `let(:pre_condition)`, you are also focusing on the wrong part of the error message. The error message says `Syntax error at ':' at line 1:11 on node localhost.localdomain`. You have a syntax error somewhere else in code you did not post. That will solve your immediate issue. – Matthew Schuchard Feb 17 '17 at 13:01
  • Well, rspec-puppet generates a manifest that contains the `pre_condition` joined with a class declaration with the parameters given. The error's from that generated manifest rather than another file, as Puppet's not given a manifest path for the error. It's probably the `pre_condition` syntax. – Dominic Cleal Feb 19 '17 at 18:17
  • @DominicCleal So it would then fail at the part of the manifest where he is defining a symbol hash key. Makes sense. Going to see how this question evolves, but it does seem like you nailed it so will upvote soon unless he discounts your theory. – Matthew Schuchard Feb 20 '17 at 15:06

2 Answers2

3

Remove or fix your pre_condition, it isn't generating a valid Puppet manifest:

let(:pre_condition) do
  "$facts = #{facts}"
end

If the Ruby variable facts is { :osfamily => 'RedHat' } then this generates a Puppet manifest like this:

$facts = {:osfamily=>'RedHat'}

The colon in front of the osfamily hash key isn't valid in a Puppet manifest (it would have to be a string, e.g. 'osfamily').

I don't think there's any reason to set pre_condition here, your let(:facts) ought to be sufficient.

Dominic Cleal
  • 3,205
  • 19
  • 22
  • Without that precondition, i keep getting "Evaluation Error: Operator '[]' is not applicable to an Undef Value" ref: https://github.com/rodjek/rspec-puppet/issues/344 – Techmago Feb 17 '17 at 11:41
  • @Techmago That issue is an absolute lie. I use `let(:facts) {{ fact: 'fact_value' }}` all the time in RSpec Puppet and it populates the `$facts` hash. You and he are having some other issue here. – Matthew Schuchard Feb 17 '17 at 12:53
  • Yeah, I cannot reproduce that issue and it's been supported in rspec-puppet for over a year. If you can show more detail about your error (the source of the syntax error) when removing `pre_condition` by editing the question, we may be able to help further. – Dominic Cleal Feb 20 '17 at 10:05
  • Yes, you were right. I am making testes for some puppet recipes someone-else made... and i started learning puppet from scratch last week. The tests are still broke, but for an entirely diferent reason now. http://stackoverflow.com/questions/42303648/rspec-puppet-nested-fixtures – Techmago Feb 20 '17 at 17:13
  • Well, looks like this was the correct theory. Good find. – Matthew Schuchard Feb 21 '17 at 13:01
0

The OP also asked about rp_env.

The environment rp_env stands for "Rspec-Puppet Environment" and is a fake value for the environment added by Rspec-puppet itself.

It is defined here in the code.

If you wish to set the environment yourself in your Rspec-puppet code, there is a useful post here that explains how to do it.

Alex Harvey
  • 14,494
  • 5
  • 61
  • 97