I've been having the exact same conversation with Henrik as I also need to
write tests for hiera-mysql, hiera-http and the Jerakia Hiera 5 backend....
He suggested copying the approach used by yaml_data and json_data Puppet
core but theres a few problems with this.  Firstly it uses
puppet_spec/compiler which isn't loadable (it's contained within the spec/
directory of Puppet) and it only gives you an interface to the Puppet side
of it, eg: validating the data that the lookup produces - it doesn't give
you the ability to mock things in the function and test functionality.

For example, for hiera-http I want to mock out calls to the lookup_http lib
and write tests validating the URLs getting called with different
hiera.yaml options.  It would be nice to have some kind of helper that gave
you a fake Puppet::LookupContext context so you could load and run the
function under rspec.

I'm completely stuck with how to proceed with these now - all three of my
Hiera 5 backends have no unit testing at all :-(

Please do share if you come up with a nice way of doing this.

Craig


On Fri, Sep 29, 2017 at 4:56 PM, <[email protected]> wrote:

> Hi all,
>
> I am writing a custom hiera backend for consul and I want to test the
> functionality.
> But I am unable to create the proper puppet data structures that are
> required by the function definition.
>
>
> Part of the function I want to test:
>
> Puppet::Functions.create_function(:hiera_consul) do
> dispatch :lookup_key do
> param 'Variant[String, Numeric]', :key
> param 'Hash', :options
> param 'Puppet::LookupContext', :context
> end
> def lookup_key(key, options, context)
> if confine_keys = options['confine_to_keys']
> raise ArgumentError, 'confine_to_keys must be an array' unless
> confine_keys.is_a?(Array)
> confine_keys.map! { |r| Regexp.new(r) }
> regex_key_match = Regexp.union(confine_keys)
> unless key[regex_key_match] == key
> context.explain { "Skipping consul backend because key does not match
> confine_to_keys" }
> context.not_found
> end
> end
> ....
> end
> end
>
>
> The test:
> describe 'hiera_consul' do
> it "is confined to certain keys" do
> context = Puppet::Pops::Lookup::Context.new({},{})
>
> is_expected.to run.with_params('test_key', {'k' => 'v'}, context)
> end
> end
>
> The results:
> 1) hiera_consul is confined to certain keys
> Failure/Error: is_expected.to run.with_params('test_key', {'k' => 'v'},
> context)
>
> expected hiera_consul("test_key", {"k"=>"v"}, 
> #<Puppet::Pops::Lookup::Context:0x007fdb7acc7060
> @lookup_invocation={}, @function_context={}>) to have run successfully
> instead of NoMethodError(undefined method `gsub!' for nil:NilClass)
>
>
> What would the right way to construct the arguments for this signature?
>
> Specifically
> - how do i create Puppet::LookupContext. Is this really the way?
> - how do i create a puppet hash construct with data in it?
>
> With regards,
>
> Jos Houtman
>
>
>
> --
> 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 [email protected].
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/puppet-users/2e9a54b7-c7bd-4ce4-8a69-723422845e21%40googlegroups.com
> <https://groups.google.com/d/msgid/puppet-users/2e9a54b7-c7bd-4ce4-8a69-723422845e21%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Enviatics     |      Automation and Configuration Management
Puppet Labs Service Delivery Partner & Certified Consultant
http://www.enviatics.com | @Enviatics | [email protected]

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/puppet-users/CACxdKhG2CSDCCD3ycdZFaw4ZXg0m--b3EbiNzGvCQ8FiwadSGw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to