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
|
require 'spec_helper'
require 'puppet/application/lookup'
require 'puppet/pops/lookup'
describe Puppet::Application::Lookup do
def run_lookup(lookup)
capture = StringIO.new
saved_stdout = $stdout
begin
$stdout = capture
expect { lookup.run_command }.to exit_with(0)
ensure
$stdout = saved_stdout
end
# Drop end of line and an optional yaml end of document
capture.string.gsub(/\n(\.\.\.\n)?\Z/m, '')
end
context "when running with incorrect command line options" do
let (:lookup) { Puppet::Application[:lookup] }
it "errors if no keys are given via the command line" do
lookup.options[:node] = 'dantooine.local'
expected_error = "No keys were given to lookup."
expect { lookup.run_command }.to raise_error(RuntimeError, expected_error)
end
it "does not allow invalid arguments for '--merge'" do
lookup.options[:node] = 'dantooine.local'
lookup.options[:merge] = 'something_bad'
allow(lookup.command_line).to receive(:args).and_return(['atton', 'kreia'])
expected_error = "The --merge option only accepts 'first', 'hash', 'unique', or 'deep'\nRun 'puppet lookup --help' for more details"
expect { lookup.run_command }.to raise_error(RuntimeError, expected_error)
end
it "does not allow deep merge options if '--merge' was not set to deep" do
lookup.options[:node] = 'dantooine.local'
lookup.options[:merge_hash_arrays] = true
lookup.options[:merge] = 'hash'
allow(lookup.command_line).to receive(:args).and_return(['atton', 'kreia'])
expected_error = "The options --knock-out-prefix, --sort-merged-arrays, and --merge-hash-arrays are only available with '--merge deep'\nRun 'puppet lookup --help' for more details"
expect { lookup.run_command }.to raise_error(RuntimeError, expected_error)
end
end
context "when running with correct command line options" do
let (:lookup) { Puppet::Application[:lookup] }
it "calls the lookup method with the correct arguments" do
lookup.options[:node] = 'dantooine.local'
lookup.options[:render_as] = :s;
lookup.options[:merge_hash_arrays] = true
lookup.options[:merge] = 'deep'
allow(lookup.command_line).to receive(:args).and_return(['atton', 'kreia'])
allow(lookup).to receive(:generate_scope).and_yield('scope')
expected_merge = { "strategy" => "deep", "sort_merged_arrays" => false, "merge_hash_arrays" => true }
expect(Puppet::Pops::Lookup).to receive(:lookup).with(['atton', 'kreia'], nil, nil, false, expected_merge, anything).and_return('rand')
expect(run_lookup(lookup)).to eql("rand")
end
%w(first unique hash deep).each do |opt|
it "accepts --merge #{opt}" do
lookup.options[:node] = 'dantooine.local'
lookup.options[:merge] = opt
lookup.options[:render_as] = :s
allow(lookup.command_line).to receive(:args).and_return(['atton', 'kreia'])
allow(lookup).to receive(:generate_scope).and_yield('scope')
allow(Puppet::Pops::Lookup).to receive(:lookup).and_return('rand')
expect(run_lookup(lookup)).to eql("rand")
end
end
it "prints the value found by lookup" do
lookup.options[:node] = 'dantooine.local'
lookup.options[:render_as] = :s
allow(lookup.command_line).to receive(:args).and_return(['atton', 'kreia'])
allow(lookup).to receive(:generate_scope).and_yield('scope')
allow(Puppet::Pops::Lookup).to receive(:lookup).and_return('rand')
expect(run_lookup(lookup)).to eql("rand")
end
end
context 'when given a valid configuration' do
let (:lookup) { Puppet::Application[:lookup] }
# There is a fully configured 'sample' environment in fixtures at this location
let(:environmentpath) { File.absolute_path(File.join(my_fixture_dir(), '../environments')) }
let(:facts) { Puppet::Node::Facts.new("facts", {}) }
let(:node) { Puppet::Node.new("testnode", :facts => facts, :environment => 'production') }
let(:expected_json_hash) {
{
'branches' =>
[
{
'branches'=>
[
{
'key'=>'lookup_options',
'event'=>'not_found',
'type'=>'data_provider',
'name'=>'Global Data Provider (hiera configuration version 5)'
},
{
'branches'=>
[
{
'branches'=>
[
{
'key' => 'lookup_options',
'value' => {'a'=>'first'},
'event'=>'found',
'type'=>'path',
'original_path'=>'common.yaml',
'path'=>"#{environmentpath}/production/data/common.yaml"
}
],
'type'=>'data_provider',
'name'=>'Hierarchy entry "Common"'
}
],
'type'=>'data_provider',
'name'=>'Environment Data Provider (hiera configuration version 5)'
}
],
'key'=>'lookup_options',
'type'=>'root'
},
{
'branches'=>
[
{
'key'=>'a',
'event'=>'not_found',
'type'=>'data_provider',
'name'=>'Global Data Provider (hiera configuration version 5)'
},
{
'branches'=>
[
{
'branches'=>
[
{
'key'=>'a',
'value'=>'This is A',
'event'=>'found',
'type'=>'path',
'original_path'=>'common.yaml',
'path'=>"#{environmentpath}/production/data/common.yaml"
}
],
'type'=>'data_provider',
'name'=>'Hierarchy entry "Common"'
}
],
'type'=>'data_provider',
'name'=>'Environment Data Provider (hiera configuration version 5)'
}
],
'key'=>'a',
'type'=>'root'
}
]
}
}
let(:expected_yaml_hash) {
{
:branches =>
[
{
:branches=>
[
{
:key=>'lookup_options',
:event=>:not_found,
:type=>:data_provider,
:name=>'Global Data Provider (hiera configuration version 5)'
},
{
:branches=>
[
{
:branches=>
[
{
:key => 'lookup_options',
:value => {'a'=>'first'},
:event=>:found,
:type=>:path,
:original_path=>'common.yaml',
:path=>"#{environmentpath}/production/data/common.yaml"
}
],
:type=>:data_provider,
:name=>'Hierarchy entry "Common"'
}
],
:type=>:data_provider,
:name=>'Environment Data Provider (hiera configuration version 5)'
}
],
:key=>'lookup_options',
:type=>:root
},
{
:branches=>
[
{
:key=>'a',
:event=>:not_found,
:type=>:data_provider,
:name=>'Global Data Provider (hiera configuration version 5)'
},
{
:branches=>
[
{
:branches=>
[
{
:key=>'a',
:value=>'This is A',
:event=>:found,
:type=>:path,
:original_path=>'common.yaml',
:path=>"#{environmentpath}/production/data/common.yaml"
}
],
:type=>:data_provider,
:name=>'Hierarchy entry "Common"'
}
],
:type=>:data_provider,
:name=>'Environment Data Provider (hiera configuration version 5)'
}
],
:key=>'a',
:type=>:root
}
]
}
}
around(:each) do |example|
# Initialize settings to get a full compile as close as possible to a real
# environment load
Puppet.settings.initialize_global_settings
loader = Puppet::Environments::Directories.new(environmentpath, [])
Puppet.override(:environments => loader) do
example.run
end
end
it '--explain produces human readable text by default and does not produce output to debug logger' do
lookup.options[:node] = node
lookup.options[:explain] = true
allow(lookup.command_line).to receive(:args).and_return(['a'])
logs = []
Puppet::Util::Log.with_destination(Puppet::Test::LogCollector.new(logs)) do
expect(run_lookup(lookup)).to eql(<<-EXPLANATION.chomp)
Searching for "lookup_options"
Global Data Provider (hiera configuration version 5)
No such key: "lookup_options"
Environment Data Provider (hiera configuration version 5)
Hierarchy entry "Common"
Path "#{environmentpath}/production/data/common.yaml"
Original path: "common.yaml"
Found key: "lookup_options" value: {
"a" => "first"
}
Searching for "a"
Global Data Provider (hiera configuration version 5)
No such key: "a"
Environment Data Provider (hiera configuration version 5)
Hierarchy entry "Common"
Path "#{environmentpath}/production/data/common.yaml"
Original path: "common.yaml"
Found key: "a" value: "This is A"
EXPLANATION
end
expect(logs.any? { |log| log.level == :debug }).to be_falsey
end
it '--debug using multiple interpolation functions produces output to the logger' do
lookup.options[:node] = node
allow(lookup.command_line).to receive(:args).and_return(['ab'])
Puppet.debug = true
logs = []
begin
Puppet::Util::Log.with_destination(Puppet::Test::LogCollector.new(logs)) do
expect { lookup.run_command }.to output(<<-VALUE.unindent).to_stdout
--- This is A and This is B
...
VALUE
end
rescue SystemExit => e
expect(e.status).to eq(0)
end
logs = logs.select { |log| log.level == :debug }.map { |log| log.message }
expect(logs).to include(/Found key: "ab" value: "This is A and This is B"/)
end
it '--explain produces human readable text by default and --debug produces the same output to debug logger' do
lookup.options[:node] = node
lookup.options[:explain] = true
allow(lookup.command_line).to receive(:args).and_return(['a'])
Puppet.debug = true
logs = []
Puppet::Util::Log.with_destination(Puppet::Test::LogCollector.new(logs)) do
expect(run_lookup(lookup)).to eql(<<-EXPLANATION.chomp)
Searching for "lookup_options"
Global Data Provider (hiera configuration version 5)
No such key: "lookup_options"
Environment Data Provider (hiera configuration version 5)
Hierarchy entry "Common"
Path "#{environmentpath}/production/data/common.yaml"
Original path: "common.yaml"
Found key: "lookup_options" value: {
"a" => "first"
}
Searching for "a"
Global Data Provider (hiera configuration version 5)
No such key: "a"
Environment Data Provider (hiera configuration version 5)
Hierarchy entry "Common"
Path "#{environmentpath}/production/data/common.yaml"
Original path: "common.yaml"
Found key: "a" value: "This is A"
EXPLANATION
end
logs = logs.select { |log| log.level == :debug }.map { |log| log.message }
expect(logs).to include(<<-EXPLANATION.chomp)
Lookup of 'a'
Searching for "lookup_options"
Global Data Provider (hiera configuration version 5)
No such key: "lookup_options"
Environment Data Provider (hiera configuration version 5)
Hierarchy entry "Common"
Path "#{environmentpath}/production/data/common.yaml"
Original path: "common.yaml"
Found key: "lookup_options" value: {
"a" => "first"
}
Searching for "a"
Global Data Provider (hiera configuration version 5)
No such key: "a"
Environment Data Provider (hiera configuration version 5)
Hierarchy entry "Common"
Path "#{environmentpath}/production/data/common.yaml"
Original path: "common.yaml"
Found key: "a" value: "This is A"
EXPLANATION
end
it '--explain-options produces human readable text of a hash merge' do
lookup.options[:node] = node
lookup.options[:explain_options] = true
expect(run_lookup(lookup)).to eql(<<-EXPLANATION.chomp)
Merge strategy hash
Global Data Provider (hiera configuration version 5)
No such key: "lookup_options"
Environment Data Provider (hiera configuration version 5)
Hierarchy entry "Common"
Path "#{environmentpath}/production/data/common.yaml"
Original path: "common.yaml"
Found key: "lookup_options" value: {
"a" => "first"
}
Merged result: {
"a" => "first"
}
EXPLANATION
end
it '--explain-options produces human readable text of a hash merge and --debug produces the same output to debug logger' do
lookup.options[:node] = node
lookup.options[:explain_options] = true
Puppet.debug = true
logs = []
Puppet::Util::Log.with_destination(Puppet::Test::LogCollector.new(logs)) do
expect(run_lookup(lookup)).to eql(<<-EXPLANATION.chomp)
Merge strategy hash
Global Data Provider (hiera configuration version 5)
No such key: "lookup_options"
Environment Data Provider (hiera configuration version 5)
Hierarchy entry "Common"
Path "#{environmentpath}/production/data/common.yaml"
Original path: "common.yaml"
Found key: "lookup_options" value: {
"a" => "first"
}
Merged result: {
"a" => "first"
}
EXPLANATION
logs = logs.select { |log| log.level == :debug }.map { |log| log.message }
expect(logs).to include(<<-EXPLANATION.chomp)
Lookup of '__global__'
Merge strategy hash
Global Data Provider (hiera configuration version 5)
No such key: "lookup_options"
Environment Data Provider (hiera configuration version 5)
Hierarchy entry "Common"
Path "#{environmentpath}/production/data/common.yaml"
Original path: "common.yaml"
Found key: "lookup_options" value: {
"a" => "first"
}
Merged result: {
"a" => "first"
}
EXPLANATION
end
end
it '--explain produces human readable text of a hash merge when using both --explain and --explain-options' do
lookup.options[:node] = node
lookup.options[:explain] = true
lookup.options[:explain_options] = true
allow(lookup.command_line).to receive(:args).and_return(['a'])
expect(run_lookup(lookup)).to eql(<<-EXPLANATION.chomp)
Searching for "lookup_options"
Global Data Provider (hiera configuration version 5)
No such key: "lookup_options"
Environment Data Provider (hiera configuration version 5)
Hierarchy entry "Common"
Path "#{environmentpath}/production/data/common.yaml"
Original path: "common.yaml"
Found key: "lookup_options" value: {
"a" => "first"
}
Searching for "a"
Global Data Provider (hiera configuration version 5)
No such key: "a"
Environment Data Provider (hiera configuration version 5)
Hierarchy entry "Common"
Path "#{environmentpath}/production/data/common.yaml"
Original path: "common.yaml"
Found key: "a" value: "This is A"
EXPLANATION
end
it 'can produce a yaml explanation' do
lookup.options[:node] = node
lookup.options[:explain] = true
lookup.options[:render_as] = :yaml
allow(lookup.command_line).to receive(:args).and_return(['a'])
output = run_lookup(lookup)
expect(Puppet::Util::Yaml.safe_load(output, [Symbol])).to eq(expected_yaml_hash)
end
it 'can produce a json explanation' do
lookup.options[:node] = node
lookup.options[:explain] = true
lookup.options[:render_as] = :json
allow(lookup.command_line).to receive(:args).and_return(['a'])
output = run_lookup(lookup)
expect(JSON.parse(output)).to eq(expected_json_hash)
end
it 'can access values using dotted keys' do
lookup.options[:node] = node
lookup.options[:render_as] = :json
allow(lookup.command_line).to receive(:args).and_return(['d.one.two.three'])
output = run_lookup(lookup)
expect(JSON.parse("[#{output}]")).to eq(['the value'])
end
it 'can access values using quoted dotted keys' do
lookup.options[:node] = node
lookup.options[:render_as] = :json
allow(lookup.command_line).to receive(:args).and_return(['"e.one.two.three"'])
output = run_lookup(lookup)
expect(JSON.parse("[#{output}]")).to eq(['the value'])
end
it 'can access values using mix of dotted keys and quoted dotted keys' do
lookup.options[:node] = node
lookup.options[:render_as] = :json
allow(lookup.command_line).to receive(:args).and_return(['"f.one"."two.three".1'])
output = run_lookup(lookup)
expect(JSON.parse("[#{output}]")).to eq(['second value'])
end
context 'the global scope' do
include PuppetSpec::Files
it "is unaffected by global variables unless '--compile' is used" do
lookup.options[:node] = node
lookup.options[:render_as] = :s
allow(lookup.command_line).to receive(:args).and_return(['c'])
expect(run_lookup(lookup)).to eql("This is")
end
it "is affected by global variables when '--compile' is used" do
lookup.options[:node] = node
lookup.options[:compile] = true
lookup.options[:render_as] = :s
allow(lookup.command_line).to receive(:args).and_return(['c'])
expect(run_lookup(lookup)).to eql("This is C from site.pp")
end
it 'receives extra facts in top scope' do
file_path = file_containing('facts.yaml', <<~CONTENT)
---
cx: ' C from facts'
CONTENT
lookup.options[:node] = node
lookup.options[:fact_file] = file_path
lookup.options[:render_as] = :s
allow(lookup.command_line).to receive(:args).and_return(['c'])
expect(run_lookup(lookup)).to eql("This is C from facts")
end
it 'receives extra facts in the facts hash' do
file_path = file_containing('facts.yaml', <<~CONTENT)
---
cx: ' G from facts'
CONTENT
lookup.options[:node] = node
lookup.options[:fact_file] = file_path
lookup.options[:render_as] = :s
allow(lookup.command_line).to receive(:args).and_return(['g'])
expect(run_lookup(lookup)).to eql("This is G from facts in facts hash")
end
describe 'when retrieving given facts' do
before do
lookup.options[:node] = node
allow(lookup.command_line).to receive(:args).and_return(['g'])
end
it 'loads files with yaml extension as yaml on first try' do
file_path = file_containing('facts.yaml', <<~CONTENT)
---
cx: ' G from facts'
CONTENT
lookup.options[:fact_file] = file_path
expect(Puppet::Util::Yaml).to receive(:safe_load_file).with(file_path, []).and_call_original
run_lookup(lookup)
end
it 'loads files with yml extension as yaml on first try' do
file_path = file_containing('facts.yml', <<~CONTENT)
---
cx: ' G from facts'
CONTENT
lookup.options[:fact_file] = file_path
expect(Puppet::Util::Yaml).to receive(:safe_load_file).with(file_path, []).and_call_original
run_lookup(lookup)
end
it 'loads files with json extension as json on first try' do
file_path = file_containing('facts.json', <<~CONTENT)
{
"cx": " G from facts"
}
CONTENT
lookup.options[:fact_file] = file_path
expect(Puppet::Util::Json).to receive(:load_file).with(file_path, {}).and_call_original
run_lookup(lookup)
end
it 'detects file format accordingly even with random file extension' do
file_path = file_containing('facts.txt', <<~CONTENT)
{
"cx": " G from facts"
}
CONTENT
lookup.options[:fact_file] = file_path
expect(Puppet::Util::Json).to receive(:load_file_if_valid).with(file_path).and_call_original
expect(Puppet::Util::Yaml).not_to receive(:safe_load_file_if_valid).with(file_path)
run_lookup(lookup)
end
it 'detects file without extension as json due to valid contents' do
file_path = file_containing('facts', <<~CONTENT)
{
"cx": " G from facts"
}
CONTENT
lookup.options[:fact_file] = file_path
expect(Puppet::Util::Json).to receive(:load_file_if_valid).with(file_path).and_call_original
expect(Puppet::Util::Yaml).not_to receive(:safe_load_file_if_valid).with(file_path)
run_lookup(lookup)
end
it 'detects file without extension as yaml due to valid contents' do
file_path = file_containing('facts', <<~CONTENT)
---
cx: ' G from facts'
CONTENT
lookup.options[:fact_file] = file_path
expect(Puppet::Util::Json.load_file_if_valid(file_path)).to be_nil
expect(Puppet::Util::Yaml).to receive(:safe_load_file_if_valid).with(file_path).and_call_original
run_lookup(lookup)
end
it 'raises error due to invalid contents' do
file_path = file_containing('facts.yml', <<~CONTENT)
INVALID CONTENT
CONTENT
lookup.options[:fact_file] = file_path
expect {
lookup.run_command
}.to raise_error(/Incorrectly formatted data in .+ given via the --facts flag \(only accepts yaml and json files\)/)
end
it "fails when a node doesn't have facts" do
lookup.options[:node] = "bad.node"
allow(lookup.command_line).to receive(:args).and_return(['c'])
expected_error = "No facts available for target node: #{lookup.options[:node]}"
expect { lookup.run_command }.to raise_error(RuntimeError, expected_error)
end
it 'raises error due to missing trusted information facts in --facts file' do
file_path = file_containing('facts.yaml', <<~CONTENT)
---
fqdn: some.fqdn.com
CONTENT
lookup.options[:fact_file] = file_path
expect {
lookup.run_command
}.to raise_error(/When overriding any of the hostname,domain,fqdn,clientcert facts with #{file_path} given via the --facts flag, they must all be overridden./)
end
it 'does not fail when all trusted information facts are provided via --facts file' do
file_path = file_containing('facts.yaml', <<~CONTENT)
---
fqdn: some.fqdn.com
hostname: some.hostname
domain: some.domain
clientcert: some.clientcert
CONTENT
lookup.options[:fact_file] = file_path
expect {
lookup.run_command
}.to exit_with(0)
end
end
end
context 'using a puppet function as data provider' do
let(:node) { Puppet::Node.new("testnode", :facts => facts, :environment => 'puppet_func_provider') }
it "works OK in the absense of '--compile'" do
lookup.options[:node] = node
allow(lookup.command_line).to receive(:args).and_return(['c'])
lookup.options[:render_as] = :s
expect(run_lookup(lookup)).to eql("This is C from data.pp")
end
it "global scope is affected by global variables when '--compile' is used" do
lookup.options[:node] = node
lookup.options[:compile] = true
lookup.options[:render_as] = :s
allow(lookup.command_line).to receive(:args).and_return(['c'])
expect(run_lookup(lookup)).to eql("This is C from site.pp")
end
end
end
end
|