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 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
|
require 'spec_helper'
require 'puppet/pops'
require 'puppet/parser/parser_factory'
require 'puppet_spec/compiler'
require 'puppet_spec/pops'
require 'puppet_spec/scope'
require 'matchers/resource'
require 'rgen/metamodel_builder'
# Test compilation using the future evaluator
describe "Puppet::Parser::Compiler" do
include PuppetSpec::Compiler
include Matchers::Resource
before :each do
Puppet[:parser] = 'future'
end
describe "the compiler when using future parser and evaluator" do
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'
compiler = Puppet::Parser::Compiler.new(Puppet::Node.new("testnode"))
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
node = Puppet::Node.new("testnodex")
node.classes = ['foo', 'bar']
catalog = compile_to_catalog(<<-PP, node)
class foo
{
notify { foo_notify: }
include bar
}
class bar
{
notify { bar_notify: }
}
PP
catalog = Puppet::Parser::Compiler.compile(node)
expect(catalog).to have_resource("Notify[foo_notify]")
expect(catalog).to have_resource("Notify[bar_notify]")
end
it 'applies defaults for defines with qualified names (PUP-2302)' do
catalog = compile_to_catalog(<<-CODE)
define my::thing($msg = 'foo') { notify {'check_me': message => $msg } }
My::Thing { msg => 'evoe' }
my::thing { 'name': }
CODE
expect(catalog).to have_resource("Notify[check_me]").with_parameter(:message, "evoe")
end
it 'Applies defaults from dynamic scopes (3x and future with reverted PUP-867)' do
catalog = compile_to_catalog(<<-CODE)
class a {
Notify { message => "defaulted" }
include b
notify { bye: }
}
class b { notify { hi: } }
include a
CODE
expect(catalog).to have_resource("Notify[hi]").with_parameter(:message, "defaulted")
expect(catalog).to have_resource("Notify[bye]").with_parameter(:message, "defaulted")
end
it 'gets default from inherited class (PUP-867)' do
catalog = compile_to_catalog(<<-CODE)
class a {
Notify { message => "defaulted" }
include c
notify { bye: }
}
class b { Notify { message => "inherited" } }
class c inherits b { notify { hi: } }
include a
CODE
expect(catalog).to have_resource("Notify[hi]").with_parameter(:message, "inherited")
expect(catalog).to have_resource("Notify[bye]").with_parameter(:message, "defaulted")
end
it 'looks up default parameter values from inherited class (PUP-2532)' do
catalog = compile_to_catalog(<<-CODE)
class a {
Notify { message => "defaulted" }
include c
notify { bye: }
}
class b { Notify { message => "inherited" } }
class c inherits b { notify { hi: } }
include a
notify {hi_test: message => Notify[hi][message] }
notify {bye_test: message => Notify[bye][message] }
CODE
expect(catalog).to have_resource("Notify[hi_test]").with_parameter(:message, "inherited")
expect(catalog).to have_resource("Notify[bye_test]").with_parameter(:message, "defaulted")
end
it 'does not allow override of class parameters using a resource override expression' do
expect do
compile_to_catalog(<<-CODE)
Class[a] { x => 2}
CODE
end.to raise_error(/Resource Override can only.*got: Class\[a\].*/)
end
describe "when resolving class references" do
it "should not favor local scope (with class included in topscope)" do
catalog = compile_to_catalog(<<-PP)
class experiment {
class baz {
}
notify {"x" : require => Class[Baz] }
notify {"y" : require => Class[Experiment::Baz] }
}
class baz {
}
include baz
include experiment
include experiment::baz
PP
expect(catalog).to have_resource("Notify[x]").with_parameter(:require, be_resource("Class[Baz]"))
expect(catalog).to have_resource("Notify[y]").with_parameter(:require, be_resource("Class[Experiment::Baz]"))
end
it "should not favor local scope, (with class not included in topscope)" do
catalog = compile_to_catalog(<<-PP)
class experiment {
class baz {
}
notify {"x" : require => Class[Baz] }
notify {"y" : require => Class[Experiment::Baz] }
}
class baz {
}
include experiment
include experiment::baz
PP
expect(catalog).to have_resource("Notify[x]").with_parameter(:require, be_resource("Class[Baz]"))
expect(catalog).to have_resource("Notify[y]").with_parameter(:require, be_resource("Class[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
catalog = compile_to_catalog(<<-MANIFEST)
class { 'foo::test': }
class foo::test {
#{include}
}
class bar::baz {
notify { 'good!': }
}
class foo::bar::baz {
notify { 'bad!': }
}
MANIFEST
expect(catalog).to have_resource("Class[Bar::Baz]")
expect(catalog).to have_resource("Notify[good!]")
expect(catalog).to_not have_resource("Class[Foo::Bar::Baz]")
expect(catalog).to_not have_resource("Notify[bad!]")
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
['define', 'class', 'node'].each do |thing|
it "'#{thing}' is not allowed inside evaluated conditional constructs" do
expect do
compile_to_catalog(<<-PP)
if true {
#{thing} foo {
}
notify { decoy: }
}
PP
end.to raise_error(Puppet::Error, /Classes, definitions, and nodes may only appear at toplevel/)
end
it "'#{thing}' is not allowed inside un-evaluated conditional constructs" do
expect do
compile_to_catalog(<<-PP)
if false {
#{thing} foo {
}
notify { decoy: }
}
PP
end.to raise_error(Puppet::Error, /Classes, definitions, and nodes may only appear at toplevel/)
end
end
describe "relationships can be formed" do
def extract_name(ref)
ref.sub(/File\[(\w+)\]/, '\1')
end
def assert_creates_relationships(relationship_code, expectations)
base_manifest = <<-MANIFEST
file { [a,b,c]:
mode => '0644',
}
file { [d,e]:
mode => '0755',
}
MANIFEST
catalog = compile_to_catalog(base_manifest + relationship_code)
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 =~ (expectations[:relationships] || [])
actual_subscriptions.should =~ (expectations[:subscriptions] || [])
end
it "of regular type" do
assert_creates_relationships("File[a] -> File[b]",
:relationships => [['a','b']])
end
it "of subscription type" do
assert_creates_relationships("File[a] ~> File[b]",
:subscriptions => [['a', 'b']])
end
it "between multiple resources expressed as resource with multiple titles" do
assert_creates_relationships("File[a,b] -> File[c,d]",
:relationships => [['a', 'c'],
['b', 'c'],
['a', 'd'],
['b', 'd']])
end
it "between collection expressions" do
assert_creates_relationships("File <| mode == 0644 |> -> File <| mode == 0755 |>",
:relationships => [['a', 'd'],
['b', 'd'],
['c', 'd'],
['a', 'e'],
['b', 'e'],
['c', 'e']])
end
it "between resources expressed as Strings" do
assert_creates_relationships("'File[a]' -> 'File[b]'",
:relationships => [['a', 'b']])
end
it "between resources expressed as variables" do
assert_creates_relationships(<<-MANIFEST, :relationships => [['a', 'b']])
$var = File[a]
$var -> File[b]
MANIFEST
end
it "between resources expressed as case statements" do
assert_creates_relationships(<<-MANIFEST, :relationships => [['s1', 't2']])
$var = 10
case $var {
10: {
file { s1: }
}
12: {
file { s2: }
}
}
->
case $var + 2 {
10: {
file { t1: }
}
12: {
file { t2: }
}
}
MANIFEST
end
it "using deep access in array" do
assert_creates_relationships(<<-MANIFEST, :relationships => [['a', 'b']])
$var = [ [ [ File[a], File[b] ] ] ]
$var[0][0][0] -> $var[0][0][1]
MANIFEST
end
it "using deep access in hash" do
assert_creates_relationships(<<-MANIFEST, :relationships => [['a', 'b']])
$var = {'foo' => {'bar' => {'source' => File[a], 'target' => File[b]}}}
$var[foo][bar][source] -> $var[foo][bar][target]
MANIFEST
end
it "using resource declarations" do
assert_creates_relationships("file { l: } -> file { r: }", :relationships => [['l', 'r']])
end
it "between entries in a chain of relationships" do
assert_creates_relationships("File[a] -> File[b] ~> File[c] <- File[d] <~ File[e]",
:relationships => [['a', 'b'], ['d', 'c']],
:subscriptions => [['b', 'c'], ['e', 'd']])
end
end
context "when dealing with variable references" do
it 'an initial underscore in a variable name is ok' do
catalog = compile_to_catalog(<<-MANIFEST)
class a { $_a = 10}
include a
notify { 'test': message => $a::_a }
MANIFEST
expect(catalog).to have_resource("Notify[test]").with_parameter(:message, 10)
end
it 'an initial underscore in not ok if elsewhere than last segment' do
expect do
catalog = compile_to_catalog(<<-MANIFEST)
class a { $_a = 10}
include a
notify { 'test': message => $_a::_a }
MANIFEST
end.to raise_error(/Illegal variable name/)
end
it 'a missing variable as default value becomes undef' do
# strict variables not on
catalog = compile_to_catalog(<<-MANIFEST)
class a ($b=$x) { notify {test: message=>"yes ${undef == $b}" } }
include a
MANIFEST
expect(catalog).to have_resource("Notify[test]").with_parameter(:message, "yes true")
end
end
context 'when working with the trusted data hash' do
context 'and have opted in to hashed_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
expect(catalog).to have_resource("Notify[test]").with_parameter(:message, "value")
end
it 'should not allow assignment to $trusted' do
node = Puppet::Node.new("testing")
node.trusted_data = { "data" => "value" }
expect do
compile_to_catalog(<<-MANIFEST, node)
$trusted = 'changed'
notify { 'test': message => $trusted == 'changed' }
MANIFEST
end.to raise_error(Puppet::Error, /Attempt to assign to a reserved variable name: 'trusted'/)
end
end
context 'and have not opted in to hashed_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
expect(catalog).to have_resource("Notify[test]").with_parameter(:message, true)
end
it 'should allow assignment to $trusted' do
catalog = compile_to_catalog(<<-MANIFEST)
$trusted = 'changed'
notify { 'test': message => $trusted == 'changed' }
MANIFEST
expect(catalog).to have_resource("Notify[test]").with_parameter(:message, true)
end
end
end
context 'when using typed parameters in definition' do
it 'accepts type compliant arguments' do
catalog = compile_to_catalog(<<-MANIFEST)
define foo(String $x) { }
foo { 'test': x =>'say friend' }
MANIFEST
expect(catalog).to have_resource("Foo[test]").with_parameter(:x, 'say friend')
end
it 'accepts undef as the default for an Optional argument' do
catalog = compile_to_catalog(<<-MANIFEST)
define foo(Optional[String] $x = undef) {
notify { "expected": message => $x == undef }
}
foo { 'test': }
MANIFEST
expect(catalog).to have_resource("Notify[expected]").with_parameter(:message, true)
end
it 'accepts anything when parameters are untyped' do
expect do
catalog = compile_to_catalog(<<-MANIFEST)
define foo($a, $b, $c) { }
foo { 'test': a => String, b=>10, c=>undef }
MANIFEST
end.to_not raise_error()
end
it 'denies non type compliant arguments' do
expect do
catalog = compile_to_catalog(<<-MANIFEST)
define foo(Integer $x) { }
foo { 'test': x =>'say friend' }
MANIFEST
end.to raise_error(/type Integer, got String/)
end
it 'denies undef for a non-optional type' do
expect do
catalog = compile_to_catalog(<<-MANIFEST)
define foo(Integer $x) { }
foo { 'test': x => undef }
MANIFEST
end.to raise_error(/type Integer, got Undef/)
end
it 'denies non type compliant default argument' do
expect do
catalog = compile_to_catalog(<<-MANIFEST)
define foo(Integer $x = 'pow') { }
foo { 'test': }
MANIFEST
end.to raise_error(/type Integer, got String/)
end
it 'denies undef as the default for a non-optional type' do
expect do
catalog = compile_to_catalog(<<-MANIFEST)
define foo(Integer $x = undef) { }
foo { 'test': }
MANIFEST
end.to raise_error(/type Integer, got Undef/)
end
it 'accepts a Resource as a Type' do
catalog = compile_to_catalog(<<-MANIFEST)
define foo(Type[Bar] $x) {
notify { 'test': message => $x[text] }
}
define bar($text) { }
bar { 'joke': text => 'knock knock' }
foo { 'test': x => Bar[joke] }
MANIFEST
expect(catalog).to have_resource("Notify[test]").with_parameter(:message, 'knock knock')
end
end
context 'when using typed parameters in class' do
it 'accepts type compliant arguments' do
catalog = compile_to_catalog(<<-MANIFEST)
class foo(String $x) { }
class { 'foo': x =>'say friend' }
MANIFEST
expect(catalog).to have_resource("Class[Foo]").with_parameter(:x, 'say friend')
end
it 'accepts undef as the default for an Optional argument' do
catalog = compile_to_catalog(<<-MANIFEST)
class foo(Optional[String] $x = undef) {
notify { "expected": message => $x == undef }
}
class { 'foo': }
MANIFEST
expect(catalog).to have_resource("Notify[expected]").with_parameter(:message, true)
end
it 'accepts anything when parameters are untyped' do
expect do
catalog = compile_to_catalog(<<-MANIFEST)
class foo($a, $b, $c) { }
class { 'foo': a => String, b=>10, c=>undef }
MANIFEST
end.to_not raise_error()
end
it 'denies non type compliant arguments' do
expect do
catalog = compile_to_catalog(<<-MANIFEST)
class foo(Integer $x) { }
class { 'foo': x =>'say friend' }
MANIFEST
end.to raise_error(/type Integer, got String/)
end
it 'denies undef for a non-optional type' do
expect do
catalog = compile_to_catalog(<<-MANIFEST)
class foo(Integer $x) { }
class { 'foo': x => undef }
MANIFEST
end.to raise_error(/type Integer, got Undef/)
end
it 'denies non type compliant default argument' do
expect do
catalog = compile_to_catalog(<<-MANIFEST)
class foo(Integer $x = 'pow') { }
class { 'foo': }
MANIFEST
end.to raise_error(/type Integer, got String/)
end
it 'denies undef as the default for a non-optional type' do
expect do
catalog = compile_to_catalog(<<-MANIFEST)
class foo(Integer $x = undef) { }
class { 'foo': }
MANIFEST
end.to raise_error(/type Integer, got Undef/)
end
it 'accepts a Resource as a Type' do
catalog = compile_to_catalog(<<-MANIFEST)
class foo(Type[Bar] $x) {
notify { 'test': message => $x[text] }
}
define bar($text) { }
bar { 'joke': text => 'knock knock' }
class { 'foo': x => Bar[joke] }
MANIFEST
expect(catalog).to have_resource("Notify[test]").with_parameter(:message, 'knock knock')
end
end
context 'when using typed parameters in lambdas' do
it 'accepts type compliant arguments' do
catalog = compile_to_catalog(<<-MANIFEST)
with('value') |String $x| { notify { "$x": } }
MANIFEST
expect(catalog).to have_resource("Notify[value]")
end
it 'handles an array as a single argument' do
catalog = compile_to_catalog(<<-MANIFEST)
with(['value', 'second']) |$x| { notify { "${x[0]} ${x[1]}": } }
MANIFEST
expect(catalog).to have_resource("Notify[value second]")
end
it 'denies when missing required arguments' do
expect do
compile_to_catalog(<<-MANIFEST)
with(1) |$x, $y| { }
MANIFEST
end.to raise_error(/Parameter \$y is required but no value was given/m)
end
it 'accepts anything when parameters are untyped' do
catalog = compile_to_catalog(<<-MANIFEST)
['value', 1, true, undef].each |$x| { notify { "value: $x": } }
MANIFEST
expect(catalog).to have_resource("Notify[value: value]")
expect(catalog).to have_resource("Notify[value: 1]")
expect(catalog).to have_resource("Notify[value: true]")
expect(catalog).to have_resource("Notify[value: ]")
end
it 'accepts type-compliant, slurped arguments' do
catalog = compile_to_catalog(<<-MANIFEST)
with(1, 2) |Integer *$x| { notify { "${$x[0] + $x[1]}": } }
MANIFEST
expect(catalog).to have_resource("Notify[3]")
end
it 'denies non-type-compliant arguments' do
expect do
compile_to_catalog(<<-MANIFEST)
with(1) |String $x| { }
MANIFEST
end.to raise_error(/expected.*String.*actual.*Integer/m)
end
it 'denies non-type-compliant, slurped arguments' do
expect do
compile_to_catalog(<<-MANIFEST)
with(1, "hello") |Integer *$x| { }
MANIFEST
end.to raise_error(/called with mis-matched arguments.*expected.*Integer.*actual.*Integer, String/m)
end
it 'denies non-type-compliant default argument' do
expect do
compile_to_catalog(<<-MANIFEST)
with(1) |$x, String $defaulted = 1| { notify { "${$x + $defaulted}": }}
MANIFEST
end.to raise_error(/expected.*Any.*String.*actual.*Integer.*Integer/m)
end
it 'raises an error when a default argument value is an incorrect type and there are no arguments passed' do
expect do
compile_to_catalog(<<-MANIFEST)
with() |String $defaulted = 1| {}
MANIFEST
end.to raise_error(/expected.*String.*actual.*Integer/m)
end
it 'raises an error when the default argument for a slurped parameter is an incorrect type' do
expect do
compile_to_catalog(<<-MANIFEST)
with() |String *$defaulted = 1| {}
MANIFEST
end.to raise_error(/expected.*String.*actual.*Integer/m)
end
it 'allows using an array as the default slurped value' do
catalog = compile_to_catalog(<<-MANIFEST)
with() |String *$defaulted = [hi]| { notify { $defaulted[0]: } }
MANIFEST
expect(catalog).to have_resource('Notify[hi]')
end
it 'allows using a value of the type as the default slurped value' do
catalog = compile_to_catalog(<<-MANIFEST)
with() |String *$defaulted = hi| { notify { $defaulted[0]: } }
MANIFEST
expect(catalog).to have_resource('Notify[hi]')
end
it 'allows specifying the type of a slurped parameter as an array' do
catalog = compile_to_catalog(<<-MANIFEST)
with() |Array[String] *$defaulted = hi| { notify { $defaulted[0]: } }
MANIFEST
expect(catalog).to have_resource('Notify[hi]')
end
it 'raises an error when the number of default values does not match the parameter\'s size specification' do
expect do
compile_to_catalog(<<-MANIFEST)
with() |Array[String, 2] *$defaulted = hi| { }
MANIFEST
end.to raise_error(/expected.*arg count \{2,\}.*actual.*arg count \{1\}/m)
end
it 'raises an error when the number of passed values does not match the parameter\'s size specification' do
expect do
compile_to_catalog(<<-MANIFEST)
with(hi) |Array[String, 2] *$passed| { }
MANIFEST
end.to raise_error(/expected.*arg count \{2,\}.*actual.*arg count \{1\}/m)
end
it 'matches when the number of arguments passed for a slurp parameter match the size specification' do
catalog = compile_to_catalog(<<-MANIFEST)
with(hi, bye) |Array[String, 2] *$passed| {
$passed.each |$n| { notify { $n: } }
}
MANIFEST
expect(catalog).to have_resource('Notify[hi]')
expect(catalog).to have_resource('Notify[bye]')
end
it 'raises an error when the number of allowed slurp parameters exceeds the size constraint' do
expect do
compile_to_catalog(<<-MANIFEST)
with(hi, bye) |Array[String, 1, 1] *$passed| { }
MANIFEST
end.to raise_error(/expected.*arg count \{1\}.*actual.*arg count \{2\}/m)
end
it 'allows passing slurped arrays by specifying an array of arrays' do
catalog = compile_to_catalog(<<-MANIFEST)
with([hi], [bye]) |Array[Array[String, 1, 1]] *$passed| {
notify { $passed[0][0]: }
notify { $passed[1][0]: }
}
MANIFEST
expect(catalog).to have_resource('Notify[hi]')
expect(catalog).to have_resource('Notify[bye]')
end
it 'raises an error when a required argument follows an optional one' do
expect do
compile_to_catalog(<<-MANIFEST)
with() |$y = first, $x, Array[String, 1] *$passed = bye| {}
MANIFEST
end.to raise_error(/Parameter \$x is required/)
end
it 'raises an error when the minimum size of a slurped argument makes it required and it follows an optional argument' do
expect do
compile_to_catalog(<<-MANIFEST)
with() |$x = first, Array[String, 1] *$passed| {}
MANIFEST
end.to raise_error(/Parameter \$passed is required/)
end
it 'allows slurped arguments with a minimum size of 0 after an optional argument' do
catalog = compile_to_catalog(<<-MANIFEST)
with() |$x = first, Array[String, 0] *$passed| {
notify { $x: }
}
MANIFEST
expect(catalog).to have_resource('Notify[first]')
end
it 'accepts a Resource as a Type' do
catalog = compile_to_catalog(<<-MANIFEST)
define bar($text) { }
bar { 'joke': text => 'knock knock' }
with(Bar[joke]) |Type[Bar] $joke| { notify { "${joke[text]}": } }
MANIFEST
expect(catalog).to have_resource("Notify[knock knock]")
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
|