Ask Your Question
0

how to pass multiple arguments in defines rspec-puppet

asked 2016-04-08 09:11:20 -0500

Chutki gravatar image

updated 2016-04-13 06:05:21 -0500

Hi,

I have below manifests

 class module{
  $var = {
    'test1' => {
      'ensure'         => 'present',
      'uid'            => '5463',
      'gid'            => '400',
    },
    'test2' => {
      'ensure'        => 'present',
      'uid'           => '32688',
      'gid'           => '389',
    }
  }
  create_resources('module_def', $var)
}

define module_def(
  $ensure,
  $uid,
  $gid,
  $var_name = $title,
) {
  user { $var_name :
    ensure  => $ensure,
    uid     => $uid,
    gid     => $gid,
  }
}

include module

I have tried to pass multiple value in rspec-puppet

require 'spec_helper'

describe 'module_def', :type => 'define' do


var{ 'test1' => {
      'ensure'       => 'present',
      'uid'            => '5463',
      'gid'            => '400',
      },
    'test2' => {
      'ensure'      => 'present',
      'uid'           => '32688',
      'gid'           => '389',
    }
}

 let :title do
    'test1'
  end

  var.each do|user,value|
    usr_val1     = value["ensure"]
    usr_val2      = value["uid"]
    usr_val3     = value["gid"]


   it { should contain_user(user).with(
    'ensure'           => usr_val1,
    'uid'              => usr_val2,
    'gid'              => usr_val3,

   )}
end

But I am getting below error,

Failures:

  1) module_name::module_def should contain User[test1] with ensure => "present", uid => "54693", gid => "400"
     Failure/Error: )}
     Puppet::Error:
       Must pass ensure to module_name::module_def[test1] at line 3 on node 
     # ./spec/defines/module_def_spec.rb:55:in `block (3 levels) in <top (required)>'

  2) module_name::module_def should contain User[test2] with ensure => "present", uid => "32688", gid => "389"
     Failure/Error: )}
     Puppet::Error:
       Must pass ensure to module_name::module_def[test2] at line 3 on node 
     # ./spec/defines/module_def_spec.rb:55:in `block (3 levels) in <top (required)>'

I am not sure the cause of the error. Also, is it possible to pass multiple hiera parameter in rspec-puppet.

================================================================

Actual code

class modulename::user {

  $usr = {
    'asifa' => {
      'ensure'           => 'present',
      'uid'              => '43422',
      'gid'              => '242',
      'home'             => '/home/asifa',
      'shell'            => '/usr/bin/ksh',
      'comment'          => 'ASIFA agent,<=APP=>',
      'password'         => 'redacted',
      'mode'             => '0700',
      'password_max_age' => '-1',
    },
    'rioa' => {
      'ensure'           => 'present',
      'uid'              => '22497',
      'gid'              => '242',
      'home'             => '/home/rioa',
      'shell'            => '/usr/bin/ksh',
      'comment'          => 'RIOA Priv,<=APP=>',
      'password'         => 'redacted',
      'mode'             => '0700',
      'password_max_age' => '-1',
     }
  }
  create_resources('user::usrdef', $usr)
}

Define Tpye

define user::adduserdef (
  $ensure,
  $uid,
  $gid,
  $home,
  $shell,
  $comment,
  $password,
  $mode,
  $groups           = undef,
  $password_max_age = undef,
  $managehome       = undef,
  $user_name        = $title,
) {

  user { $user_name :
    ensure           => $ensure,
    uid              => $uid,
    gid              => $gid,
    groups           => $groups,
    home             => $home,
    shell            => $shell,
    comment          => $comment,
    password         => $password,
    password_max_age => $password_max_age,
    managehome       => $managehome,
  }
}

Rspec-puppet

require 'spec_helper'

describe 'user::adduserdef', :type => 'define' do
 end

  usr = {
    'asifa' => {
      'ensure'           => 'present',
      'uid'              => '43422',
      'gid'              => '242',
      'home'             => '/home/asifa',
      'shell'            => '/usr/bin/ksh',
      'comment'          => 'ASIFA agent,<=APP=>',
      'password'         => 'redacted',
      'mode'             => '0700',
      'password_max_age' => '-1',
    },
    'rioa'  => {
      'ensure'           => 'present',
      'uid'              => '22497',
      'gid'              => '100',
      'home'             => '/home/rioa',
      'shell'            => '/usr/bin/ksh',
      'comment'          => 'RIOA Priv,<=APP=>',
      'password'         => 'redacted',
      'mode'             => '0700',
      'password_max_age' => '-1',
    },
  }
  let :title do
    'asifa'
  end
 usr.each do|user,value|
    #ensure   = value["ensure"]
    home     = value["home"]
    usr_uid      = value["uid"]
    usr_gid      = value["gid"]
    usr_comment  = value["comment"]
    usr_password = value["password"]

   it { should contain_user(user).with(
    'ensure'           => 'present',
    'uid'              => usr_uid,
    'gid'              => usr_gid,
    'groups'           => 'undef',
    'home'             => home,
    'shell'            => '/usr/bin/ksh',
    'comment'          => usr_comment,
    'password'         => usr_password,
    'password_max_age' => '-1',
    'managehome'       => 'undef',
  )}
  end
end
edit retag flag offensive close merge delete

Comments

Both of the manifest files and your rspec examples are syntactically wrong. Based on your error messages, though, I'm guessing you copied it in here incorrectly. Can you fix that up please? Thanks.

Alex Harvey gravatar imageAlex Harvey ( 2016-04-09 01:25:02 -0500 )edit

I have edited the code.

Chutki gravatar imageChutki ( 2016-04-11 00:31:31 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2016-04-11 23:21:07 -0500

updated 2016-04-13 06:35:01 -0500

This is what you mean:

require 'spec_helper'

describe 'test::usrdef', :type => 'define' do

  usr = {
    'asifa' => {
      'ensure'           => 'present',
      'uid'              => '43422',
      'gid'              => '242',
      'home'             => '/home/asifa',
      'shell'            => '/usr/bin/ksh',
      'comment'          => 'ASIFA agent,<=APP=>',
      'password'         => 'redacted',
      'mode'             => '0700',
      'password_max_age' => '-1',
    },
    'rioa'  => {
      'ensure'           => 'present',
      'uid'              => '22497',
      'gid'              => '100',
      'home'             => '/home/rioa',
      'shell'            => '/usr/bin/ksh',
      'comment'          => 'RIOA Priv,<=APP=>',
      'password'         => 'redacted',
      'mode'             => '0700',
      'password_max_age' => '-1',
    },
  }

  usr.each do |title, data|
    context title do
      let(:title) { title }
      let(:params) {{
        'ensure'           => data['ensure'],
        'uid'              => data['uid'],
        'gid'              => data['gid'],
        'home'             => data['home'],
        'shell'            => data['shell'],
        'comment'          => data['comment'],
        'password'         => data['password'],
        'password_max_age' => data['password_max_age'],
        'mode'             => data['mode'],
      }}
      it { should contain_user(title).with(
        'ensure'           => data['ensure'],
        'uid'              => data['uid'],
        'gid'              => data['gid'],
        'groups'           => nil,
        'home'             => data['home'],
        'shell'            => data['shell'],
        'comment'          => data['comment'],
        'password'         => data['password'],
        'password_max_age' => data['password_max_age'],
        'mode'             => nil,
        'managehome'       => nil,
      )}
    end
  end
end
edit flag offensive delete link more

Comments

I am sorry about that. I have edited and updated the code

Chutki gravatar imageChutki ( 2016-04-13 02:25:04 -0500 )edit

Thanks it worked

Chutki gravatar imageChutki ( 2016-04-13 07:08:17 -0500 )edit
1

answered 2016-04-12 12:29:31 -0500

rnelson0 gravatar image

updated 2016-04-13 04:15:07 -0500

You probably want to set :params in your spec file after setting the title. See here:

let(:title) { 'myWebSite' }
let(:params) { {
  app_pool: 'myAppPool.example.com',
  host_header: 'myHost.example.com',
  site_path: 'C:\inetpub\wwwroot\myWebSite'
} }

If your params expect a hash, create the hash as you normally would, then reference it in the :params section and later in the it {} block as well.

edit flag offensive delete link more

Comments

That's my guess too, although the defined type as provided does not expect any mandatory parameter 'ensure', as the error message mentioned in Rspec implies.

Alex Harvey gravatar imageAlex Harvey ( 2016-04-13 00:02:07 -0500 )edit

Thanks for an update. Yes I already tried with :params for single parameter values and it worked. But my doubt is, how we can pass multiple parameter in test case. Is it possible in :params.

Chutki gravatar imageChutki ( 2016-04-13 02:33:53 -0500 )edit

Seriously dude. Rob's shown how to pass in 3 parameters. Meanwhile, the Rspec code in your post is syntactically incorrect, as I said, and in many ways. If that Rspec was really your code, it would error out with a syntax errors. We don't know what you did wrong unless you show your actual code.

Alex Harvey gravatar imageAlex Harvey ( 2016-04-13 02:45:29 -0500 )edit

Yes, Rob has updated with three params. But how it possible with multiple titles ("test1" and "test2"). I had look at the url what Rob updated, it seems I need to pass each title value in a separate :params and write the test case for each param.

Chutki gravatar imageChutki ( 2016-04-13 04:07:52 -0500 )edit

It's not possible with multiple titles, because a resource can only have one title. I won't be replying here again until I see your code.

Alex Harvey gravatar imageAlex Harvey ( 2016-04-13 04:18:26 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-04-08 09:11:20 -0500

Seen: 180 times

Last updated: Apr 13 '16