On 25 July 2017 at 00:21, James Perry <[email protected]> wrote:

> So I have a simple class, for now, where I am trying to write RSPEC tests
> to check the following:
>
> 1. Smart Parameter $current_version is in catalogue.
> 2. If $current_version = $installed_version (custom fact that will be
> stubbed as a :fact), that 'class foo' exits without doing anything.
> 3. if $current_version != $installed_version a class foo::cleanup is
> called.
>
> I have looked all over to try to work this out and keep hitting posts
> about "can't check variables" but when I do a p catalogue.resources I see:
>
> foo
> [Stage[main]{:name=>"main"}, Class[Settings]{:name=>"Settings"},
> Class[main]{:name=>"main"}, Class[Foo]{:name=>"Foo",
> :mypath=>"/opt/mypath", :current_ver=>"0.1.2"}]
>
> In foo.pp:
> class foo ( $mypath="/usr/local/mypath", $current_version="0.1.2" ) {
>   if $installed_version == $current_version { # Goodie. Nothing to do }
>   else { include foo::cleanup }
>
> }
>
> In the spec file .spec/classes/init_spec.rb:
> require 'spec_helper'
> describe 'foo' do
>    context 'foo current version' do
>    let :params do {
>        :mypath => '/opt/mypath',
>       # :current_version => '1.2.3',
>        }
>     end
>
>    it {p catalogue.resources}
>    it do
>       is expected_to contain_class('foo').with(:current_version =>
> "0.1.2" )
>    end
> end
>
> While this is very basic, and I have tried multiple ways, it error out
> with:
>      Failure/Error: should contain_class('epops_hyperic')
> .with_current_version("0.1.2")
>        expected that the catalogue would contain Class[epops_hyperic] with
> current_version set to "0.1.2" but it is set to nil
>
> Yet the p catalogue.resources function shows the shows it correctly as
> noted in the Stage[main] above.
>
> If I uncomment the parameter :current_version => '1.2.3'  the catalogue
> print has :current_version => "1.2.3" as expected (since it is overridden)
> and the test fails with the expected message :current_ver set to "0.1.2"
> but it is set to "1.2.3". This I know how to deal with, but that then
> causes the problems with 2 and 3 above.
>
> What I want to test in the spec is if current_ver == expected version, the
> module doesn't need to do anything and exits. If not it will call class
> foo::cleanup. I read a lot on this today and have seen various generalized
> suggestions about 3, but nothing about 2.
>
> How do I test if the class exits after finding nothing is needed to be
> done or it included the class to do a cleanup accordingly?
>

Under the assumption that $installed_version is a fact, you can provide
stubbed fact values in your test setup. See
http://rspec-puppet.com/documentation/classes/#specifying-facts for details.

In your case, you then can have test cases for $installed_version, and
$current_version set to various different values and have your expectations
set up to expect, or not, the cleanup class:

context "when nothing to do" do
  let(:facts) {{ installed_version: '1.2.3' }}
  let(:params) {{ configured_version: '1.2.3' }}

  it { is_expected.not_to contain_class('foo::cleanup') }
end

context "when updating" do
  let(:facts) {{ installed_version: '1.0.0' }}
  let(:params) {{ configured_version: '1.2.3' }}

  it { is_expected.to contain_class('foo::cleanup') }
end

Cheers, David


>
> All of the tutorials give basics, but I can't find anything digging more
> specific that seems to work.
>
> Can anyone point me to the right places to look to research this?  So far
> the spec files seem really good for TDD, but without a good reference to
> these sorts of non file, package, service checks it is confusing.
>
> Thanks!
>
> --
> You received this message because you are subscribed to the Google Groups
> "Puppet Developers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/puppet-dev/aa734244-5887-485a-bb77-d88648b889de%40googlegroups.com
> <https://groups.google.com/d/msgid/puppet-dev/aa734244-5887-485a-bb77-d88648b889de%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/puppet-dev/CALF7fHY-k5BwiCDtqEaa-6wnAhL%3DGeAFXkmUti6w4xwQwi7_2Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to