Add getparam function to get defined resource parameters
As far as i know there's no other puppet-dsl-like way to get parameter of
defined resource, so that's why i implemented getparam function, which takes
resource reference and parameter name and returns parameter value.
Here's another example why this function is really useful:
define config($path, $config_param1, $config_param2) { }
define example_resource($config) {
$path = getparam($config, "path")
notice("Path is $path")
}
define example_resource2($example_resource, $config = getparam($example_resource, "config")) {
$config_param1 = getparam($config, "config_param1")
notice("Config parameter is $config_param1")
}
define example_resource3($example_resource, $config = getparam($example_resource, "config")) {
$config_param2 = getparam($config, "config_param2")
notice("Config parameter is $config_param2")
}
class test_getparam {
config { "config_instance":
path => "/some/config/path",
config_param1 => "someconfigtext1",
config_param2 => "someconfigtext2",
}
example_resource { "example_resource_instance":
config => Config["config_instance"]
}
example_resource2 { "example_resource_instance":
example_resource => Example_resource["example_resource_instance"]
}
example_resource3 { "example_resource_instance":
example_resource => Example_resource2["example_resource_instance"]
}
}
class { "test_getparam": }
2013-01-02 13:10:43 +01:00
|
|
|
require 'spec_helper'
|
2014-03-05 21:43:58 +01:00
|
|
|
|
Add getparam function to get defined resource parameters
As far as i know there's no other puppet-dsl-like way to get parameter of
defined resource, so that's why i implemented getparam function, which takes
resource reference and parameter name and returns parameter value.
Here's another example why this function is really useful:
define config($path, $config_param1, $config_param2) { }
define example_resource($config) {
$path = getparam($config, "path")
notice("Path is $path")
}
define example_resource2($example_resource, $config = getparam($example_resource, "config")) {
$config_param1 = getparam($config, "config_param1")
notice("Config parameter is $config_param1")
}
define example_resource3($example_resource, $config = getparam($example_resource, "config")) {
$config_param2 = getparam($config, "config_param2")
notice("Config parameter is $config_param2")
}
class test_getparam {
config { "config_instance":
path => "/some/config/path",
config_param1 => "someconfigtext1",
config_param2 => "someconfigtext2",
}
example_resource { "example_resource_instance":
config => Config["config_instance"]
}
example_resource2 { "example_resource_instance":
example_resource => Example_resource["example_resource_instance"]
}
example_resource3 { "example_resource_instance":
example_resource => Example_resource2["example_resource_instance"]
}
}
class { "test_getparam": }
2013-01-02 13:10:43 +01:00
|
|
|
describe 'getparam' do
|
2015-06-01 13:21:59 +02:00
|
|
|
it { is_expected.not_to eq(nil) }
|
|
|
|
it { is_expected.to run.with_params().and_raise_error(ArgumentError, /Must specify a reference/) }
|
|
|
|
it { is_expected.to run.with_params('User[one]').and_raise_error(ArgumentError, /Must specify name of a parameter/) }
|
|
|
|
it { is_expected.to run.with_params('User[one]', 2).and_raise_error(ArgumentError, /Must specify name of a parameter/) }
|
|
|
|
it { is_expected.to run.with_params('User[one]', []).and_raise_error(ArgumentError, /Must specify name of a parameter/) }
|
|
|
|
it { is_expected.to run.with_params('User[one]', {}).and_raise_error(ArgumentError, /Must specify name of a parameter/) }
|
|
|
|
|
|
|
|
describe 'when compared against a user resource with no params' do
|
|
|
|
let(:pre_condition) { 'user { "one": }' }
|
|
|
|
|
|
|
|
it { is_expected.to run.with_params('User[one]', 'ensure').and_return('') }
|
|
|
|
it { is_expected.to run.with_params('User[two]', 'ensure').and_return('') }
|
|
|
|
it { is_expected.to run.with_params('User[one]', 'shell').and_return('') }
|
Add getparam function to get defined resource parameters
As far as i know there's no other puppet-dsl-like way to get parameter of
defined resource, so that's why i implemented getparam function, which takes
resource reference and parameter name and returns parameter value.
Here's another example why this function is really useful:
define config($path, $config_param1, $config_param2) { }
define example_resource($config) {
$path = getparam($config, "path")
notice("Path is $path")
}
define example_resource2($example_resource, $config = getparam($example_resource, "config")) {
$config_param1 = getparam($config, "config_param1")
notice("Config parameter is $config_param1")
}
define example_resource3($example_resource, $config = getparam($example_resource, "config")) {
$config_param2 = getparam($config, "config_param2")
notice("Config parameter is $config_param2")
}
class test_getparam {
config { "config_instance":
path => "/some/config/path",
config_param1 => "someconfigtext1",
config_param2 => "someconfigtext2",
}
example_resource { "example_resource_instance":
config => Config["config_instance"]
}
example_resource2 { "example_resource_instance":
example_resource => Example_resource["example_resource_instance"]
}
example_resource3 { "example_resource_instance":
example_resource => Example_resource2["example_resource_instance"]
}
}
class { "test_getparam": }
2013-01-02 13:10:43 +01:00
|
|
|
end
|
2014-03-05 21:43:58 +01:00
|
|
|
|
2015-06-01 13:21:59 +02:00
|
|
|
describe 'when compared against a user resource with params' do
|
|
|
|
let(:pre_condition) { 'user { "one": ensure => present, shell => "/bin/sh", managehome => false, }' }
|
2014-03-05 21:43:58 +01:00
|
|
|
|
2015-06-01 13:21:59 +02:00
|
|
|
it { is_expected.to run.with_params('User[one]', 'ensure').and_return('present') }
|
|
|
|
it { is_expected.to run.with_params('User[two]', 'ensure').and_return('') }
|
|
|
|
it { is_expected.to run.with_params('User[one]', 'shell').and_return('/bin/sh') }
|
|
|
|
it {
|
|
|
|
pending("both rspec-puppet as well as the function do the wrong thing here.")
|
|
|
|
is_expected.to run.with_params('User[one]', 'managehome').and_return(false)
|
|
|
|
}
|
Add getparam function to get defined resource parameters
As far as i know there's no other puppet-dsl-like way to get parameter of
defined resource, so that's why i implemented getparam function, which takes
resource reference and parameter name and returns parameter value.
Here's another example why this function is really useful:
define config($path, $config_param1, $config_param2) { }
define example_resource($config) {
$path = getparam($config, "path")
notice("Path is $path")
}
define example_resource2($example_resource, $config = getparam($example_resource, "config")) {
$config_param1 = getparam($config, "config_param1")
notice("Config parameter is $config_param1")
}
define example_resource3($example_resource, $config = getparam($example_resource, "config")) {
$config_param2 = getparam($config, "config_param2")
notice("Config parameter is $config_param2")
}
class test_getparam {
config { "config_instance":
path => "/some/config/path",
config_param1 => "someconfigtext1",
config_param2 => "someconfigtext2",
}
example_resource { "example_resource_instance":
config => Config["config_instance"]
}
example_resource2 { "example_resource_instance":
example_resource => Example_resource["example_resource_instance"]
}
example_resource3 { "example_resource_instance":
example_resource => Example_resource2["example_resource_instance"]
}
}
class { "test_getparam": }
2013-01-02 13:10:43 +01:00
|
|
|
end
|
|
|
|
end
|