Help with mocks / stubs on user provider

31 views
Skip to first unread message

James Perry

unread,
Jan 14, 2015, 3:27:00 PM1/14/15
to puppe...@googlegroups.com
I am a bit stuck here with a modification to the HPUX user provider module. 

What I wrote works fine when running the rspec against it on HP-UX but fails everywhere else.  Part of the problem I is that to find if the system is trusted I have to run a shell command. 

To get the modify command to work correctly for trusted and untrusted systems, the code needs to be able to add a second program to the command line to reset password expiration to 0.

In the code I have a call to a routing to check for the system being a trusted computer. When bundler / rspec runs against it on my Linux test box, it fails because ther command is HPUX specific, as expected.

How do I stub / mock how to get it to have the rspec not try to make that call and take the provided return as if it had done so?  I tried stubs and mocks with no success, and I have tried as many ways as I could to figure this out with no success.

Code in hpux.rb:
 def modifycmd(param,value)
     cmd = super(param, value)
     cmd << "-F"
     if self.trusted == "Trusted"
        cmd << ";"
        cmd << "/usr/lbin/modprpw"
        cmd << "-v"
        cmd << "-l"
        cmd << "#{resource.name}"
     end
     cmd
  end


hpux_spec.rb subset:
  it "should add /usr/lbin/modprpw -v -l when modifying user if trusted" do
       resource.stubs(:allowdupe?).returns true
       provider.expects(:execute).with() { |args|  args.include?('/usr/lbin/modprpw') and args.include?("-v") and args.include?("-l") }
       provider.uid = 1000
  end



Michael Smith

unread,
Jan 14, 2015, 7:37:12 PM1/14/15
to puppe...@googlegroups.com

The piece that's important here is the definition of #trusted. That may need to be stubbed so that it returns true (or false, it's worth testing both cases in a spec test) to get the behavior you want.

One technique we often use is, when we have to call executables, wrapping those calls in their own method so it can be stubbed. For example, you might write

def exec_getprpw(user):
  %x(/usr/lbin/getprpw #{user} 2>&1)
end

then in the test

provider.stubs(:exec_getprpw).with('root').returns('Some text')

This simulates the call to getprpw without having a dependency on a particular OS.


--
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 puppet-dev+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-dev/3a21b300-eda7-4168-b1fe-44c28d932d2f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

James Perry

unread,
Jan 14, 2015, 7:44:48 PM1/14/15
to puppe...@googlegroups.com
Thanks for the pointers!  That is the understanding I was missing and I will rework the code to rewrite it to get that working. 
Reply all
Reply to author
Forward
0 new messages