1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
|
require 'spec_helper'
require 'puppet/parameter'
describe Puppet::Parameter do
before do
@class = Class.new(Puppet::Parameter) do
@name = :foo
end
@class.initvars
@resource = double('resource')
allow(@resource).to receive(:expects)
allow(@resource).to receive(:pathbuilder)
@parameter = @class.new :resource => @resource
end
it "should create a value collection" do
@class = Class.new(Puppet::Parameter)
expect(@class.value_collection).to be_nil
@class.initvars
expect(@class.value_collection).to be_instance_of(Puppet::Parameter::ValueCollection)
end
it "should return its name as a string when converted to a string" do
expect(@parameter.to_s).to eq(@parameter.name.to_s)
end
[:line, :file, :version].each do |data|
it "should return its resource's #{data} as its #{data}" do
expect(@resource).to receive(data).and_return("foo")
expect(@parameter.send(data)).to eq("foo")
end
end
it "should return the resource's tags plus its name as its tags" do
expect(@resource).to receive(:tags).and_return(%w{one two})
expect(@parameter.tags).to eq(%w{one two foo})
end
it "should have a path" do
expect(@parameter.path).to eq("//foo")
end
describe "when returning the value" do
it "should return nil if no value is set" do
expect(@parameter.value).to be_nil
end
it "should validate the value" do
expect(@parameter).to receive(:validate).with("foo")
@parameter.value = "foo"
end
it "should munge the value and use any result as the actual value" do
expect(@parameter).to receive(:munge).with("foo").and_return("bar")
@parameter.value = "foo"
expect(@parameter.value).to eq("bar")
end
it "should unmunge the value when accessing the actual value" do
@parameter.class.unmunge do |value| value.to_sym end
@parameter.value = "foo"
expect(@parameter.value).to eq(:foo)
end
it "should return the actual value by default when unmunging" do
expect(@parameter.unmunge("bar")).to eq("bar")
end
it "should return any set value" do
@parameter.value = "foo"
expect(@parameter.value).to eq("foo")
end
end
describe "when validating values" do
it "should do nothing if no values or regexes have been defined" do
@parameter.validate("foo")
end
it "should catch abnormal failures thrown during validation" do
@class.validate { |v| raise "This is broken" }
expect { @parameter.validate("eh") }.to raise_error(Puppet::DevError)
end
it "should fail if the value is not a defined value or alias and does not match a regex" do
@class.newvalues :foo
expect { @parameter.validate("bar") }.to raise_error(Puppet::Error)
end
it "should succeed if the value is one of the defined values" do
@class.newvalues :foo
expect { @parameter.validate(:foo) }.to_not raise_error
end
it "should succeed if the value is one of the defined values even if the definition uses a symbol and the validation uses a string" do
@class.newvalues :foo
expect { @parameter.validate("foo") }.to_not raise_error
end
it "should succeed if the value is one of the defined values even if the definition uses a string and the validation uses a symbol" do
@class.newvalues "foo"
expect { @parameter.validate(:foo) }.to_not raise_error
end
it "should succeed if the value is one of the defined aliases" do
@class.newvalues :foo
@class.aliasvalue :bar, :foo
expect { @parameter.validate("bar") }.to_not raise_error
end
it "should succeed if the value matches one of the regexes" do
@class.newvalues %r{\d}
expect { @parameter.validate("10") }.to_not raise_error
end
end
describe "when munging values" do
it "should do nothing if no values or regexes have been defined" do
expect(@parameter.munge("foo")).to eq("foo")
end
it "should catch abnormal failures thrown during munging" do
@class.munge { |v| raise "This is broken" }
expect { @parameter.munge("eh") }.to raise_error(Puppet::DevError)
end
it "should return return any matching defined values" do
@class.newvalues :foo, :bar
expect(@parameter.munge("foo")).to eq(:foo)
end
it "should return any matching aliases" do
@class.newvalues :foo
@class.aliasvalue :bar, :foo
expect(@parameter.munge("bar")).to eq(:foo)
end
it "should return the value if it matches a regex" do
@class.newvalues %r{\w}
expect(@parameter.munge("bar")).to eq("bar")
end
it "should return the value if no other option is matched" do
@class.newvalues :foo
expect(@parameter.munge("bar")).to eq("bar")
end
end
describe "when logging" do
it "should use its resource's log level and the provided message" do
expect(@resource).to receive(:[]).with(:loglevel).and_return(:notice)
expect(@parameter).to receive(:send_log).with(:notice, "mymessage")
@parameter.log "mymessage"
end
end
describe ".format_value_for_display" do
it 'should format strings appropriately' do
expect(described_class.format_value_for_display('foo')).to eq("'foo'")
end
it 'should format numbers appropriately' do
expect(described_class.format_value_for_display(1)).to eq('1')
end
it 'should format symbols appropriately' do
expect(described_class.format_value_for_display(:bar)).to eq("'bar'")
end
it 'should format arrays appropriately' do
expect(described_class.format_value_for_display([1, 'foo', :bar])).to eq("[1, 'foo', 'bar']")
end
it 'should format hashes appropriately' do
expect(described_class.format_value_for_display(
{1 => 'foo', :bar => 2, 'baz' => :qux}
)).to eq(<<-RUBY.unindent.sub(/\n$/, ''))
{
1 => 'foo',
'bar' => 2,
'baz' => 'qux'
}
RUBY
end
it 'should format arrays with nested data appropriately' do
expect(described_class.format_value_for_display(
[1, 'foo', :bar, [1, 2, 3], {1 => 2, 3 => 4}]
)).to eq(<<-RUBY.unindent.sub(/\n$/, ''))
[1, 'foo', 'bar',
[1, 2, 3],
{
1 => 2,
3 => 4
}]
RUBY
end
it 'should format hashes with nested data appropriately' do
expect(described_class.format_value_for_display(
{1 => 'foo', :bar => [2, 3, 4], 'baz' => {:qux => 1, :quux => 'two'}}
)).to eq(<<-RUBY.unindent.sub(/\n$/, ''))
{
1 => 'foo',
'bar' => [2, 3, 4],
'baz' => {
'qux' => 1,
'quux' => 'two'
}
}
RUBY
end
it 'should format hashes with nested Objects appropriately' do
tf = Puppet::Pops::Types::TypeFactory
type = tf.object({'name' => 'MyType', 'attributes' => { 'qux' => tf.integer, 'quux' => tf.string }})
expect(described_class.format_value_for_display(
{1 => 'foo', 'bar' => type.create(1, 'one'), 'baz' => type.create(2, 'two')}
)).to eq(<<-RUBY.unindent.sub(/\n$/, ''))
{
1 => 'foo',
'bar' => MyType({
'qux' => 1,
'quux' => 'one'
}),
'baz' => MyType({
'qux' => 2,
'quux' => 'two'
})
}
RUBY
end
it 'should format Objects with nested Objects appropriately' do
tf = Puppet::Pops::Types::TypeFactory
inner_type = tf.object({'name' => 'MyInnerType', 'attributes' => { 'qux' => tf.integer, 'quux' => tf.string }})
outer_type = tf.object({'name' => 'MyOuterType', 'attributes' => { 'x' => tf.string, 'inner' => inner_type }})
expect(described_class.format_value_for_display(
{'bar' => outer_type.create('a', inner_type.create(1, 'one')), 'baz' => outer_type.create('b', inner_type.create(2, 'two'))}
)).to eq(<<-RUBY.unindent.sub(/\n$/, ''))
{
'bar' => MyOuterType({
'x' => 'a',
'inner' => MyInnerType({
'qux' => 1,
'quux' => 'one'
})
}),
'baz' => MyOuterType({
'x' => 'b',
'inner' => MyInnerType({
'qux' => 2,
'quux' => 'two'
})
})
}
RUBY
end
end
describe 'formatting messages' do
it "formats messages as-is when the parameter is not sensitive" do
expect(@parameter.format("hello %s", "world")).to eq("hello world")
end
it "formats messages with redacted values when the parameter is not sensitive" do
@parameter.sensitive = true
expect(@parameter.format("hello %s", "world")).to eq("hello [redacted]")
end
end
end
|