Hi bodik,

On Tue, Nov 8, 2016 at 2:03 AM, bodik <[email protected]> wrote:

> hello,
>
> I'm getting into troubles while using rake (not important) for --noop
> tests during modules development. Using
>
>         puppet apply -e 'include class'
>
> everything works fine, but using .pp file containing exactly the same
>
>         puppet apply tests/class.pp
>
> results in "Unknown function:" as shown below, where whole usecase is
> documented.
>
> Acording to strace myfunc.rb is found by lstat and fopen in both cases,
> but in the latter (error one), myfunc.rb is searched also in gems
> directories ...
>
> any advice would be appreciated
>
>
> thanks
> bodik
>
>
> ===============================================
> ===============================================
>
>
>
> root@tester:~/pb1# find .
> ./mod1
> ./mod1/manifests
> ./mod1/manifests/init.pp
> ./mod1/lib
> ./mod1/lib/facter
> ./mod1/lib/facter/myfunc.rb
> ./mod1/tests
> ./mod1/tests/init.pp
> ./run.sh
>
>
>
> root@tester:~/pb1# cat ./mod1/manifests/init.pp
> class mod1::puppet_function_resolution() {
>     notice(myfunc(1))
> }
>
>
> root@tester:~/pb1# cat ./mod1/lib/facter/myfunc.rb
>

This is not the correct place for Puppet 3.x custom functions (this
location is for custom facts).

The correct location would be mod1/lib/puppet/parser/functions/myfunc.rb.

This is probably why the compiler can't find it.


> # simple wrapper for custom execs
> require "puppet"
> module Puppet::Parser::Functions
>         newfunction(:myfunc, :type => :rvalue) do |args|
>         return "returned value"
>         end
> end
>
>
>
>
> root@tester:~/pb1# cat ./mod1/tests/init.pp
> include mod1::puppet_function_resolution
>
>
> root@tester:~/pb1# cat ./run.sh
> #!/bin/sh
> set -x
>
> puppet apply --modulepath=/root/pb1 -e 'include
> mod1::puppet_function_resolution' --noop
>
> puppet apply --modulepath=/root/pb1 "mod1/tests/init.pp" --noop
>
>
>
>
> root@tester:~/pb1# sh ./run.sh
>
> + puppet apply --modulepath=/root/pb1 -e include
> mod1::puppet_function_resolution --noop
> Notice: Scope(Class[Mod1::Puppet_function_resolution]): returned value
> Notice: Compiled catalog for tester in environment production in 0.04
> seconds
> Notice: Applied catalog in 0.63 seconds
>
>
> + puppet apply --modulepath=/root/pb1 mod1/tests/init.pp --noop
> Error: Evaluation Error: Unknown function: 'myfunc'. at
> /root/pb1/mod1/manifests/init.pp:2:9 on node tester
>
>
>
>
> root@tester:~/pb1# puppet --version
> 4.5.2
>

Given that you're using Puppet 4.x, I recommend using the Puppet 4.x
function API rather than the 3.x API.

As an example for your custom function, in
mod1/lib/puppet/functions/myfunc.rb you would have:

```
Puppet::Functions.create_function(:'mod1::myfunc') do
  dispatch :myfunc do
    param :Integer, :arg
  end

  def myfunc(arg)
     "returning value #{arg}"
  end
end
```

And this would be invoked like `mod1::myfunc(1)`.

Cheers,

Peter


>
> --
> 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/ms
> gid/puppet-users/5821A30B.1040104%40cesnet.cz.
> For more options, visit https://groups.google.com/d/optout.
>



-- 
--
Peter Huene - Senior Software Engineer
[email protected] | @peterhuene

-- 
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/CACZQQfMU_7RcTf5%3D%2B1H9AZhfOuNPtF6AYYOnM3KqPFw%2BLq5XGg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to