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 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
|
#! /usr/bin/env ruby
require 'spec_helper'
require 'puppet/parser/parser_factory'
require 'puppet_spec/compiler'
require 'matchers/resource'
describe "Puppet::Parser::Compiler" do
include PuppetSpec::Compiler
include Matchers::Resource
before :each do
@node = Puppet::Node.new "testnode"
@scope_resource = stub 'scope_resource', :builtin? => true, :finish => nil, :ref => 'Class[main]'
@scope = stub 'scope', :resource => @scope_resource, :source => mock("source")
end
it "should be able to determine the configuration version from a local version control repository" do
pending("Bug #14071 about semantics of Puppet::Util::Execute on Windows", :if => Puppet.features.microsoft_windows?) do
# This should always work, because we should always be
# in the puppet repo when we run this.
version = %x{git rev-parse HEAD}.chomp
Puppet.settings[:config_version] = 'git rev-parse HEAD'
@parser = Puppet::Parser::ParserFactory.parser "development"
@compiler = Puppet::Parser::Compiler.new(@node)
@compiler.catalog.version.should == version
end
end
it "should not create duplicate resources when a class is referenced both directly and indirectly by the node classifier (4792)" do
Puppet[:code] = <<-PP
class foo
{
notify { foo_notify: }
include bar
}
class bar
{
notify { bar_notify: }
}
PP
@node.stubs(:classes).returns(['foo', 'bar'])
catalog = Puppet::Parser::Compiler.compile(@node)
catalog.resource("Notify[foo_notify]").should_not be_nil
catalog.resource("Notify[bar_notify]").should_not be_nil
end
describe "when resolving class references" do
it "should favor local scope, even if there's an included class in topscope" do
Puppet[:code] = <<-PP
class experiment {
class baz {
}
notify {"x" : require => Class[Baz] }
}
class baz {
}
include baz
include experiment
include experiment::baz
PP
catalog = Puppet::Parser::Compiler.compile(Puppet::Node.new("mynode"))
notify_resource = catalog.resource( "Notify[x]" )
notify_resource[:require].title.should == "Experiment::Baz"
end
it "should favor local scope, even if there's an unincluded class in topscope" do
Puppet[:code] = <<-PP
class experiment {
class baz {
}
notify {"x" : require => Class[Baz] }
}
class baz {
}
include experiment
include experiment::baz
PP
catalog = Puppet::Parser::Compiler.compile(Puppet::Node.new("mynode"))
notify_resource = catalog.resource( "Notify[x]" )
notify_resource[:require].title.should == "Experiment::Baz"
end
end
describe "(ticket #13349) when explicitly specifying top scope" do
["class {'::bar::baz':}", "include ::bar::baz"].each do |include|
describe "with #{include}" do
it "should find the top level class" do
Puppet[:code] = <<-MANIFEST
class { 'foo::test': }
class foo::test {
#{include}
}
class bar::baz {
notify { 'good!': }
}
class foo::bar::baz {
notify { 'bad!': }
}
MANIFEST
catalog = Puppet::Parser::Compiler.compile(Puppet::Node.new("mynode"))
catalog.resource("Class[Bar::Baz]").should_not be_nil
catalog.resource("Notify[good!]").should_not be_nil
catalog.resource("Class[Foo::Bar::Baz]").should be_nil
catalog.resource("Notify[bad!]").should be_nil
end
end
end
end
it "should recompute the version after input files are re-parsed" do
Puppet[:code] = 'class foo { }'
Time.stubs(:now).returns(1)
node = Puppet::Node.new('mynode')
Puppet::Parser::Compiler.compile(node).version.should == 1
Time.stubs(:now).returns(2)
Puppet::Parser::Compiler.compile(node).version.should == 1 # no change because files didn't change
Puppet::Resource::TypeCollection.any_instance.stubs(:stale?).returns(true).then.returns(false) # pretend change
Puppet::Parser::Compiler.compile(node).version.should == 2
end
['class', 'define', 'node'].each do |thing|
it "should not allow '#{thing}' inside evaluated conditional constructs" do
Puppet[:code] = <<-PP
if true {
#{thing} foo {
}
notify { decoy: }
}
PP
begin
Puppet::Parser::Compiler.compile(Puppet::Node.new("mynode"))
raise "compilation should have raised Puppet::Error"
rescue Puppet::Error => e
e.message.should =~ /at line 2/
end
end
end
it "should not allow classes inside unevaluated conditional constructs" do
Puppet[:code] = <<-PP
if false {
class foo {
}
}
PP
lambda { Puppet::Parser::Compiler.compile(Puppet::Node.new("mynode")) }.should raise_error(Puppet::Error)
end
describe "when defining relationships" do
def extract_name(ref)
ref.sub(/File\[(\w+)\]/, '\1')
end
let(:node) { Puppet::Node.new('mynode') }
let(:code) do
<<-MANIFEST
file { [a,b,c]:
mode => '0644',
}
file { [d,e]:
mode => '0755',
}
MANIFEST
end
let(:expected_relationships) { [] }
let(:expected_subscriptions) { [] }
before :each do
Puppet[:code] = code
end
after :each do
catalog = Puppet::Parser::Compiler.compile(node)
resources = catalog.resources.select { |res| res.type == 'File' }
actual_relationships, actual_subscriptions = [:before, :notify].map do |relation|
resources.map do |res|
dependents = Array(res[relation])
dependents.map { |ref| [res.title, extract_name(ref)] }
end.inject(&:concat)
end
actual_relationships.should =~ expected_relationships
actual_subscriptions.should =~ expected_subscriptions
end
it "should create a relationship" do
code << "File[a] -> File[b]"
expected_relationships << ['a','b']
end
it "should create a subscription" do
code << "File[a] ~> File[b]"
expected_subscriptions << ['a', 'b']
end
it "should create relationships using title arrays" do
code << "File[a,b] -> File[c,d]"
expected_relationships.concat [
['a', 'c'],
['b', 'c'],
['a', 'd'],
['b', 'd'],
]
end
it "should create relationships using collection expressions" do
code << "File <| mode == 0644 |> -> File <| mode == 0755 |>"
expected_relationships.concat [
['a', 'd'],
['b', 'd'],
['c', 'd'],
['a', 'e'],
['b', 'e'],
['c', 'e'],
]
end
it "should create relationships using resource names" do
code << "'File[a]' -> 'File[b]'"
expected_relationships << ['a', 'b']
end
it "should create relationships using variables" do
code << <<-MANIFEST
$var = File[a]
$var -> File[b]
MANIFEST
expected_relationships << ['a', 'b']
end
it "should create relationships using case statements" do
code << <<-MANIFEST
$var = 10
case $var {
10: {
file { s1: }
}
12: {
file { s2: }
}
}
->
case $var + 2 {
10: {
file { t1: }
}
12: {
file { t2: }
}
}
MANIFEST
expected_relationships << ['s1', 't2']
end
it "should create relationships using array members" do
code << <<-MANIFEST
$var = [ [ [ File[a], File[b] ] ] ]
$var[0][0][0] -> $var[0][0][1]
MANIFEST
expected_relationships << ['a', 'b']
end
it "should create relationships using hash members" do
code << <<-MANIFEST
$var = {'foo' => {'bar' => {'source' => File[a], 'target' => File[b]}}}
$var[foo][bar][source] -> $var[foo][bar][target]
MANIFEST
expected_relationships << ['a', 'b']
end
it "should create relationships using resource declarations" do
code << "file { l: } -> file { r: }"
expected_relationships << ['l', 'r']
end
it "should chain relationships" do
code << "File[a] -> File[b] ~> File[c] <- File[d] <~ File[e]"
expected_relationships << ['a', 'b'] << ['d', 'c']
expected_subscriptions << ['b', 'c'] << ['e', 'd']
end
end
context 'when working with immutable node data' do
context 'and have opted in to immutable_node_data' do
before :each do
Puppet[:immutable_node_data] = true
end
def node_with_facts(facts)
Puppet[:facts_terminus] = :memory
Puppet::Node::Facts.indirection.save(Puppet::Node::Facts.new("testing", facts))
node = Puppet::Node.new("testing")
node.fact_merge
node
end
matcher :fail_compile_with do |node, message_regex|
match do |manifest|
@error = nil
begin
PuppetSpec::Compiler.compile_to_catalog(manifest, node)
false
rescue Puppet::Error => e
@error = e
message_regex.match(e.message)
end
end
failure_message_for_should do
if @error
"failed with #{@error}\n#{@error.backtrace}"
else
"did not fail"
end
end
end
it 'should make $facts available' do
node = node_with_facts('the_facts' => 'straight')
catalog = compile_to_catalog(<<-MANIFEST, node)
notify { 'test': message => $facts[the_facts] }
MANIFEST
catalog.resource("Notify[test]")[:message].should == "straight"
end
it 'should make $facts reserved' do
node = node_with_facts('the_facts' => 'straight')
expect('$facts = {}').to fail_compile_with(node, /assign to a reserved variable name: 'facts'/)
expect('class a { $facts = {} } include a').to fail_compile_with(node, /assign to a reserved variable name: 'facts'/)
end
it 'should make $facts immutable' do
node = node_with_facts('string' => 'value', 'array' => ['string'], 'hash' => { 'a' => 'string' }, 'number' => 1, 'boolean' => true)
expect('$i=inline_template("<% @facts[%q{new}] = 2 %>")').to fail_compile_with(node, /frozen Hash/i)
expect('$i=inline_template("<% @facts[%q{string}].chop! %>")').to fail_compile_with(node, /frozen String/i)
expect('$i=inline_template("<% @facts[%q{array}][0].chop! %>")').to fail_compile_with(node, /frozen String/i)
expect('$i=inline_template("<% @facts[%q{array}][1] = 2 %>")').to fail_compile_with(node, /frozen Array/i)
expect('$i=inline_template("<% @facts[%q{hash}][%q{a}].chop! %>")').to fail_compile_with(node, /frozen String/i)
expect('$i=inline_template("<% @facts[%q{hash}][%q{b}] = 2 %>")').to fail_compile_with(node, /frozen Hash/i)
end
it 'should make $facts available even if there are no facts' do
Puppet[:facts_terminus] = :memory
node = Puppet::Node.new("testing2")
node.fact_merge
catalog = compile_to_catalog(<<-MANIFEST, node)
notify { 'test': message => $facts }
MANIFEST
expect(catalog).to have_resource("Notify[test]").with_parameter(:message, {})
end
end
context 'and have not opted in to immutable_node_data' do
before :each do
Puppet[:immutable_node_data] = false
end
it 'should not make $facts available' do
Puppet[:facts_terminus] = :memory
facts = Puppet::Node::Facts.new("testing", 'the_facts' => 'straight')
Puppet::Node::Facts.indirection.save(facts)
node = Puppet::Node.new("testing")
node.fact_merge
catalog = compile_to_catalog(<<-MANIFEST, node)
notify { 'test': message => "An $facts space" }
MANIFEST
catalog.resource("Notify[test]")[:message].should == "An space"
end
end
end
context 'when working with the trusted data hash' do
context 'and have opted in to trusted_node_data' do
before :each do
Puppet[:trusted_node_data] = true
end
it 'should make $trusted available' do
node = Puppet::Node.new("testing")
node.trusted_data = { "data" => "value" }
catalog = compile_to_catalog(<<-MANIFEST, node)
notify { 'test': message => $trusted[data] }
MANIFEST
catalog.resource("Notify[test]")[:message].should == "value"
end
it 'should not allow assignment to $trusted' do
node = Puppet::Node.new("testing")
node.trusted_data = { "data" => "value" }
expect do
catalog = compile_to_catalog(<<-MANIFEST, node)
$trusted = 'changed'
notify { 'test': message => $trusted == 'changed' }
MANIFEST
catalog.resource("Notify[test]")[:message].should == true
end.to raise_error(Puppet::Error, /Attempt to assign to a reserved variable name: 'trusted'/)
end
it 'should not allow addition to $trusted hash' do
node = Puppet::Node.new("testing")
node.trusted_data = { "data" => "value" }
expect do
catalog = compile_to_catalog(<<-MANIFEST, node)
$trusted['extra'] = 'added'
notify { 'test': message => $trusted['extra'] == 'added' }
MANIFEST
catalog.resource("Notify[test]")[:message].should == true
# different errors depending on regular or future parser
end.to raise_error(Puppet::Error, /(can't modify frozen [hH]ash)|(Illegal attempt to assign)/)
end
it 'should not allow addition to $trusted hash via Ruby inline template' do
node = Puppet::Node.new("testing")
node.trusted_data = { "data" => "value" }
expect do
catalog = compile_to_catalog(<<-MANIFEST, node)
$dummy = inline_template("<% @trusted['extra'] = 'added' %> lol")
notify { 'test': message => $trusted['extra'] == 'added' }
MANIFEST
catalog.resource("Notify[test]")[:message].should == true
end.to raise_error(Puppet::Error, /can't modify frozen [hH]ash/)
end
end
context 'and have not opted in to trusted_node_data' do
before :each do
Puppet[:trusted_node_data] = false
end
it 'should not make $trusted available' do
node = Puppet::Node.new("testing")
node.trusted_data = { "data" => "value" }
catalog = compile_to_catalog(<<-MANIFEST, node)
notify { 'test': message => $trusted == undef }
MANIFEST
catalog.resource("Notify[test]")[:message].should == true
end
it 'should allow assignment to $trusted' do
node = Puppet::Node.new("testing")
catalog = compile_to_catalog(<<-MANIFEST, node)
$trusted = 'changed'
notify { 'test': message => $trusted == 'changed' }
MANIFEST
catalog.resource("Notify[test]")[:message].should == true
end
end
end
context 'when evaluating collection' do
it 'matches on container inherited tags' do
Puppet[:code] = <<-MANIFEST
class xport_test {
tag 'foo_bar'
@notify { 'nbr1':
message => 'explicitly tagged',
tag => 'foo_bar'
}
@notify { 'nbr2':
message => 'implicitly tagged'
}
Notify <| tag == 'foo_bar' |> {
message => 'overridden'
}
}
include xport_test
MANIFEST
catalog = Puppet::Parser::Compiler.compile(Puppet::Node.new("mynode"))
expect(catalog).to have_resource("Notify[nbr1]").with_parameter(:message, 'overridden')
expect(catalog).to have_resource("Notify[nbr2]").with_parameter(:message, 'overridden')
end
end
end
|