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
|
require 'puppet'
require 'spec_helper'
require 'puppet_spec/compiler'
describe 'function for dynamically creating resources' do
include PuppetSpec::Compiler
before :each do
node = Puppet::Node.new("floppy", :environment => 'production')
@compiler = Puppet::Parser::Compiler.new(node)
@scope = Puppet::Parser::Scope.new(@compiler)
@topscope = @scope.compiler.topscope
@scope.parent = @topscope
Puppet::Parser::Functions.function(:create_resources)
end
it "should exist" do
Puppet::Parser::Functions.function(:create_resources).should == "function_create_resources"
end
it 'should require two or three arguments' do
expect { @scope.function_create_resources(['foo']) }.to raise_error(ArgumentError, 'create_resources(): Wrong number of arguments given (1 for minimum 2)')
expect { @scope.function_create_resources(['foo', 'bar', 'blah', 'baz']) }.to raise_error(ArgumentError, 'create_resources(): wrong number of arguments (4; must be 2 or 3)')
end
it 'should require second argument to be a hash' do
expect { @scope.function_create_resources(['foo','bar']) }.to raise_error(ArgumentError, 'create_resources(): second argument must be a hash')
end
it 'should require optional third argument to be a hash' do
expect { @scope.function_create_resources(['foo',{},'foo']) }.to raise_error(ArgumentError, 'create_resources(): third argument, if provided, must be a hash')
end
describe 'when creating native types' do
it 'empty hash should not cause resources to be added' do
noop_catalog = compile_to_catalog("create_resources('file', {})")
empty_catalog = compile_to_catalog("")
noop_catalog.resources.size.should == empty_catalog.resources.size
end
it 'should be able to add' do
catalog = compile_to_catalog("create_resources('file', {'/etc/foo'=>{'ensure'=>'present'}})")
catalog.resource(:file, "/etc/foo")['ensure'].should == 'present'
end
it 'should be able to add virtual resources' do
catalog = compile_to_catalog("create_resources('@file', {'/etc/foo'=>{'ensure'=>'present'}})\nrealize(File['/etc/foo'])")
catalog.resource(:file, "/etc/foo")['ensure'].should == 'present'
end
it 'should be able to add exported resources' do
catalog = compile_to_catalog("create_resources('@@file', {'/etc/foo'=>{'ensure'=>'present'}}) realize(File['/etc/foo'])")
catalog.resource(:file, "/etc/foo")['ensure'].should == 'present'
catalog.resource(:file, "/etc/foo").exported.should == true
end
it 'should accept multiple types' do
catalog = compile_to_catalog("create_resources('notify', {'foo'=>{'message'=>'one'}, 'bar'=>{'message'=>'two'}})")
catalog.resource(:notify, "foo")['message'].should == 'one'
catalog.resource(:notify, "bar")['message'].should == 'two'
end
it 'should fail to add non-existing type' do
expect do
@scope.function_create_resources(['create-resource-foo', { 'foo' => {} }])
end.to raise_error(ArgumentError, /Invalid resource type create-resource-foo/)
end
it 'should be able to add edges' do
rg = compile_to_relationship_graph("notify { test: }\n create_resources('notify', {'foo'=>{'require'=>'Notify[test]'}})")
test = rg.vertices.find { |v| v.title == 'test' }
foo = rg.vertices.find { |v| v.title == 'foo' }
test.must be
foo.must be
rg.path_between(test,foo).should be
end
it 'should account for default values' do
catalog = compile_to_catalog("create_resources('file', {'/etc/foo'=>{'ensure'=>'present'}, '/etc/baz'=>{'group'=>'food'}}, {'group' => 'bar'})")
catalog.resource(:file, "/etc/foo")['group'].should == 'bar'
catalog.resource(:file, "/etc/baz")['group'].should == 'food'
end
end
describe 'when dynamically creating resource types' do
it 'should be able to create defined resource types' do
catalog = compile_to_catalog(<<-MANIFEST)
define foocreateresource($one) {
notify { $name: message => $one }
}
create_resources('foocreateresource', {'blah'=>{'one'=>'two'}})
MANIFEST
catalog.resource(:notify, "blah")['message'].should == 'two'
end
it 'should fail if defines are missing params' do
expect {
compile_to_catalog(<<-MANIFEST)
define foocreateresource($one) {
notify { $name: message => $one }
}
create_resources('foocreateresource', {'blah'=>{}})
MANIFEST
}.to raise_error(Puppet::Error, 'Must pass one to Foocreateresource[blah] on node foonode')
end
it 'should be able to add multiple defines' do
catalog = compile_to_catalog(<<-MANIFEST)
define foocreateresource($one) {
notify { $name: message => $one }
}
create_resources('foocreateresource', {'blah'=>{'one'=>'two'}, 'blaz'=>{'one'=>'three'}})
MANIFEST
catalog.resource(:notify, "blah")['message'].should == 'two'
catalog.resource(:notify, "blaz")['message'].should == 'three'
end
it 'should be able to add edges' do
rg = compile_to_relationship_graph(<<-MANIFEST)
define foocreateresource($one) {
notify { $name: message => $one }
}
notify { test: }
create_resources('foocreateresource', {'blah'=>{'one'=>'two', 'require' => 'Notify[test]'}})
MANIFEST
test = rg.vertices.find { |v| v.title == 'test' }
blah = rg.vertices.find { |v| v.title == 'blah' }
test.must be
blah.must be
rg.path_between(test,blah).should be
end
it 'should account for default values' do
catalog = compile_to_catalog(<<-MANIFEST)
define foocreateresource($one) {
notify { $name: message => $one }
}
create_resources('foocreateresource', {'blah'=>{}}, {'one' => 'two'})
MANIFEST
catalog.resource(:notify, "blah")['message'].should == 'two'
end
end
describe 'when creating classes' do
it 'should be able to create classes' do
catalog = compile_to_catalog(<<-MANIFEST)
class bar($one) {
notify { test: message => $one }
}
create_resources('class', {'bar'=>{'one'=>'two'}})
MANIFEST
catalog.resource(:notify, "test")['message'].should == 'two'
catalog.resource(:class, "bar").should_not be_nil
end
it 'should fail to create non-existing classes' do
expect do
compile_to_catalog(<<-MANIFEST)
create_resources('class', {'blah'=>{'one'=>'two'}})
MANIFEST
end.to raise_error(Puppet::Error, 'Could not find declared class blah at line 1 on node foonode')
end
it 'should be able to add edges' do
rg = compile_to_relationship_graph(<<-MANIFEST)
class bar($one) {
notify { test: message => $one }
}
notify { tester: }
create_resources('class', {'bar'=>{'one'=>'two', 'require' => 'Notify[tester]'}})
MANIFEST
test = rg.vertices.find { |v| v.title == 'test' }
tester = rg.vertices.find { |v| v.title == 'tester' }
test.must be
tester.must be
rg.path_between(tester,test).should be
end
it 'should account for default values' do
catalog = compile_to_catalog(<<-MANIFEST)
class bar($one) {
notify { test: message => $one }
}
create_resources('class', {'bar'=>{}}, {'one' => 'two'})
MANIFEST
catalog.resource(:notify, "test")['message'].should == 'two'
catalog.resource(:class, "bar").should_not be_nil
end
it 'should fail with a correct error message if the syntax of an imported file is incorrect' do
expect{
Puppet[:modulepath] = my_fixture_dir
compile_to_catalog('include foo')
}.to raise_error(Puppet::Error, /Syntax error at.*/)
end
end
end
|