Search code examples
rspecpuppet

How to convert rspec-puppet tests from 1.x to 2.0.1?


When switching from rspec-puppet 1.0.1 to rspec-puppet 2.0.1 hundreds of tests started to fail. Most of them include the error undefined method resource for #<Proc

For example: 1) haproxy on supported platforms for linux operating systems on Debian family operatingsystems Base concat fragment contents should contain global and defaults sections Failure/Error: let(:contents) { param_value(subject, 'concat::fragment', 'haproxy-haproxy-base', 'content').split("\n") } NoMethodError: undefined method `resource' for #<Proc:0x0000000308a590> # /home/travis/.rvm/gems/ruby-2.1.5/gems/puppetlabs_spec_helper-0.10.2/lib/puppetlabs_spec_helper/module_spec_helper.rb:6:in `param_value' # ./spec/classes/haproxy_spec.rb:89:in `block (7 levels) in <top (required)>' # ./spec/classes/haproxy_spec.rb:92:in `block (7 levels) in <top (required)>'

How do I fix these tests?


Solution

  • Old tests refer to a variable subject but in rspec-puppet 2.0.1, this was changed to catalogue. This incompatible change lead to the major version bump. See the comment in this commit.

    However the proper way to upgrade old tests is not obvious. One can't simply do a global search-and-replace changing subject to catalogue.

    A strategy that worked for me was to find the first instance of the problem and fix it. This will eliminate the that error as well as many that follow. Repeating this process over and over eventually fixed all the issues.

    In the above example, the first file mentioned in the error is spec/classes/haproxy_spec.rb:89 (line 89). This appears as:

    let(:contents) { param_value(subject, 'concat::fragment', 'haproxy-haproxy-base', 'content').split("\n") }

    Change "subject" to "catalogue" like so:

    let(:contents) { param_value(catalogue, 'concat::fragment', 'haproxy-haproxy-base', 'content').split("\n") }

    This change eliminated the next 7 errors.

    You'll also see lines like these:

    verify_contents(subject, '/etc/default/haproxy', ['ENABLED=1'])
    

    or

    expect { subject }.to raise_error Puppet::Error, /Invalid IP address/
    

    Each of these fixes the error in question plus all others in that context.

    Optimization: You don't have to re-run spec after each change. You'll notice each change fixes a run of many errors. In the above example a sequence of 7 errors all indicated spec/classes/haproxy_spec.rb:89. Looking for the first error that mentioned some other line number or an entirely different file, usually found another line mentioning subject. In this case, the next example was spec/classes/haproxy_spec.rb:157

    There may be other incompatibilities but this fixed the major problems for me.