Testing yaml files being passed as parameters using rspec-puppet

632 views
Skip to first unread message

choffee

unread,
Nov 6, 2015, 9:38:03 AM11/6/15
to Puppet Users
I have a define which takes a yaml file as a parameter and want to check that my template creates valid yaml.

I am currently using regexp to test bits of it like so

  should contain_mydefine('foo').with_yaml_content(/---\s+-part1/m)

It would be nice to be able to do

  should contain_mydefine('foo').with_yaml_content.valid_yaml()

Is there a simple way to check this?

As an alternative, if I assign the yaml to a local available is there a way in rspec to check that value?

Thanks

john

Martin Alfke

unread,
Nov 10, 2015, 7:02:09 AM11/10/15
to puppet...@googlegroups.com
Hi,
On 06 Nov 2015, at 14:38, choffee <cho...@gmail.com> wrote:

> I have a define which takes a yaml file as a parameter and want to check that my template creates valid yaml.
>
> I am currently using regexp to test bits of it like so
>
> should contain_mydefine('foo').with_yaml_content(/---\s+-part1/m)

You can directly access the content attribute:

content = catalogue.resource(‘mydefine', ‘foo').send(:parameters)[:content]

Then you can use default spec matchers for checking what is in local variable content.
I am unsure how to check for valid yaml.

Best,
Martin



>
> It would be nice to be able to do
>
> should contain_mydefine('foo').with_yaml_content.valid_yaml()
>
> Is there a simple way to check this?
>
> As an alternative, if I assign the yaml to a local available is there a way in rspec to check that value?
>
> Thanks
>
> john
>
> --
> You received this message because you are subscribed to the Google Groups "Puppet Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/79782f5f-f006-4094-a29e-3414ea7ba5c8%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

choffee

unread,
Nov 11, 2015, 7:20:44 AM11/11/15
to Puppet Users
Hi Martin,

Thanks for that. It worked a treat.

I just did some simple validation in the end.

     yaml_object = YAML.load(content)
     expect(yaml_object.length).to be > 0

I validate the content elsewhere but this is just an a basic check that it's able to parse it. I have been using regexp to validate the content up to now so I suppose I could move to using this yaml module to check that as well.

john
Reply all
Reply to author
Forward
0 new messages