Codebase list facter / 7b4c3bd
Merge pull request #1917 from sebastian-miclea/FACT-2592 (FACT-2592) Added Cat method Bogdan Irimie authored 3 years ago GitHub committed 3 years ago
47 changed file(s) with 107 addition(s) and 100 deletion(s). Raw diff Collapse all Expand all
6666 step "should create a cached-custom-facts cache file that containt fact information" do
6767 result = agent.file_exist?("#{cache_folder}/cached-custom-facts")
6868 assert_equal(true, result)
69 on(agent, "cat #{cache_folder}/cached-custom-facts", acceptable_exit_codes: [0]) do |cat_output|
70 assert_match(cached_file_content.chomp, cat_output.stdout, 'Expected cached custom fact file to contain fact information')
71 end
69 cat_output = agent.cat( "#{cache_folder}/cached-custom-facts")
70 assert_match(cached_file_content.chomp, cat_output.strip, 'Expected cached custom fact file to contain fact information')
7271 end
7372
7473 step 'should read from the cached file for a custom fact that has been cached' do
4040 value: "'#{custom_fact_value}'"
4141 )
4242
43 on(agent, facter("--custom-dir=#{custom_fact_dir} timezone")) do |result|
43 on(agent, facter("--custom-dir \"#{custom_fact_dir}\" timezone")) do |result|
4444 assert_match(/#{custom_fact_value}/, result.stdout.chomp, "Facter does not use the custom fact's value when its weight is > 0")
4545 end
4646 end
5757 )
5858 end
5959
60 on(agent, facter("--custom-dir=#{custom_fact_dir} timezone")) do |result|
60 on(agent, facter("--custom-dir \"#{custom_fact_dir}\" timezone")) do |result|
6161 assert_match(/#{builtin_value}/, result.stdout.chomp, "Facter does not use the builtin fact's value when all conflicting custom facts fail to resolve")
6262 end
6363 end
7676 )
7777 end
7878
79 on(agent, facter("--custom-dir=#{custom_fact_dir} timezone")) do |result|
79 on(agent, facter("--custom-dir \"#{custom_fact_dir}\" timezone")) do |result|
8080 assert_match(/#{builtin_value}/, result.stdout.chomp, "Facter does not give precedence to the builtin fact when all custom facts have zero weight")
8181 end
8282 end
9696 )
9797 end
9898
99 on(agent, facter("--custom-dir=#{custom_fact_dir} timezone")) do |result|
99 on(agent, facter("--custom-dir \"#{custom_fact_dir}\" timezone")) do |result|
100100 assert_match(/#{builtin_value}/, result.stdout.chomp, "Facter does not give precedence to the builtin fact when only some custom facts have zero weight")
101101 end
102102 end
2020
2121 # Custom fact with weight >= 10001 should override an external fact
2222 step "Agent #{agent}: resolve a custom fact with weight of 10001 overriding the external fact" do
23 on(agent, facter("--external-dir=#{facts_dir} --custom-dir=#{facts_dir} test")) do |facter_output|
23 on(agent, facter("--external-dir \"#{facts_dir}\" --custom-dir=#{facts_dir} test")) do |facter_output|
2424 assert_equal("CUSTOM", facter_output.stdout.chomp)
2525 end
2626 end
44 require 'timeout'
55
66 content = <<-EOM
7 Facter.add('custom_fact') do
7 Facter.add('custom_fact_ole') do
88 setcode do
99 require 'win32ole'
1010 locator = WIN32OLE.new('WbemScripting.SWbemLocator')
2424
2525 # Test is assumed to have hung if it takes longer than 5 seconds.
2626 Timeout::timeout(5) do
27 on agent, facter('--custom-dir', custom_dir, 'custom_fact') do |facter_result|
27 on agent, facter('--custom-dir', custom_dir, 'custom_fact_ole') do |facter_result|
2828 assert_match(/#<WIN32OLE:0x[0-9a-f]+>/, facter_result.stdout.chomp, 'Custom fact output does not match expected output')
2929 end
3030 end
7878 step "should create a cached-custom-facts cache file that containt fact information from the highest weight fact" do
7979 result = agent.file_exist?("#{cache_folder}/cached-custom-facts")
8080 assert_equal(true, result)
81 on(agent, "cat #{cache_folder}/cached-custom-facts", acceptable_exit_codes: [0]) do |cat_output|
82 assert_match(cached_file_content_highest_weight.chomp, cat_output.stdout, 'Expected cached custom fact file to contain fact information from the highest weight fact')
83 end
81 cat_output = agent.cat("#{cache_folder}/cached-custom-facts")
82 assert_match(cached_file_content_highest_weight.chomp, cat_output.strip, 'Expected cached custom fact file to contain fact information from the highest weight fact')
8483 end
8584
8685 step 'should read from the cached file for a custom fact that has been cached' do
2424 end
2525
2626 step "Agent: #{agent}: ensure external fact resolves correctly" do
27 on(agent, facter("--external-dir '#{external_dir}' #{fact_name}")) do |facter_output|
27 on(agent, facter("--external-dir \"#{external_dir}\" #{fact_name}")) do |facter_output|
2828 assert_equal(fact_value,
2929 facter_output.stdout.chomp,
3030 'Expected external fact to resolve as defined in script')
3232 end
3333
3434 step "Agent #{agent}: the fact value from FACTER_ env var should override the external fact value" do
35 on(agent, facter("--external-dir '#{external_dir}' #{fact_name}",
35 on(agent, facter("--external-dir \"#{external_dir}\" #{fact_name}",
3636 :environment => { "FACTER_#{fact_name}" => override_value })) do |facter_output|
3737 assert_equal(override_value,
3838 facter_output.stdout.chomp,
2727 end
2828
2929 step "Agent #{agent}: the fact value from the custom external dir should override that of facts.d" do
30 on(agent, facter("--external-dir '#{external_dir}' external_fact")) do |facter_output|
30 on(agent, facter("--external-dir \"#{external_dir}\" external_fact")) do |facter_output|
3131 assert_equal('OVERRIDE_value', facter_output.stdout.chomp, 'Expected to resolve override version of the external_fact')
3232 end
3333 end
1919 end
2020
2121 step "Agent #{agent}: resolve an external fact over a custom fact" do
22 on(agent, facter("--external-dir=#{facts_dir} --custom-dir=#{facts_dir} #{fact_name}")) do |facter_output|
22 on(agent, facter("--external-dir \"#{facts_dir}\" --custom-dir=#{facts_dir} #{fact_name}")) do |facter_output|
2323 assert_equal("EXTERNAL", facter_output.stdout.chomp)
2424 end
2525 end
2020
2121 # Custom fact with weight <= 10000 should give precedence to the EXTERNAL fact
2222 step "Agent #{agent}: resolve an external fact over the custom fact with a weight of 10000" do
23 on(agent, facter("--external-dir=#{facts_dir} --custom-dir=#{facts_dir} #{fact_name}")) do |facter_output|
23 on(agent, facter("--external-dir \"#{facts_dir}\" --custom-dir \"#{facts_dir}\" #{fact_name}")) do |facter_output|
2424 assert_equal("EXTERNAL", facter_output.stdout.chomp)
2525 end
2626 end
2525 # External fact should take precedence over a custom fact with a confine
2626 # (from FACT-1413)
2727 step "Agent #{agent}: resolve external fact over a custom fact with a confine" do
28 on(agent, facter("--external-dir=#{facts_dir} --custom-dir=#{facts_dir} test")) do |facter_output|
28 on(agent, facter("--external-dir \"#{facts_dir}\" --custom-dir \"#{facts_dir}\" test")) do |facter_output|
2929 assert_equal("EXTERNAL", facter_output.stdout.chomp)
3030 end
3131 end
3232 end
3333
3434 step 'works if ttl is not enabled' do
35 on(agent, facter("--external-dir '#{external_dir1}' --external-dir '#{external_dir2}' --debug #{fact1} #{fact2}")) do |facter_output|
35 on(agent, facter("--external-dir \"#{external_dir1}\" --external-dir \"#{external_dir2}\" --debug #{fact1} #{fact2}")) do |facter_output|
3636 assert_match(/#{fact1} => #{fact1_value}/, stdout, 'Expected fact to match first fact')
3737 assert_match(/#{fact2} => #{fact2_value}/, stdout, 'Expected fact to match second fact')
3838 end
4848 EOM
4949 agent.mkdir_p(config_dir)
5050 create_remote_file(agent, config_file, config)
51 on(agent, facter("--external-dir '#{external_dir1}' --external-dir '#{external_dir2}' --debug #{fact1} #{fact2}"), :acceptable_exit_codes => 1) do |facter_output|
51 on(agent, facter("--external-dir \"#{external_dir1}\" --external-dir \"#{external_dir2}\" --debug #{fact1} #{fact2}"), :acceptable_exit_codes => 1) do |facter_output|
5252 assert_match(/ERROR.*Caching is enabled for group "#{external_filename}" while there are at least two external facts files with the same filename/, stderr, 'Expected error message')
5353 assert_match(/#{fact1} => #{fact1_value}/, stdout, 'Expected fact to match first fact')
5454 assert_not_match(/#{fact2} => #{fact2_value}/, stdout, 'Expected fact not to match second fact')
1717 end
1818
1919 step "setting --config should cause the config file to be loaded from the specified location" do
20 on(agent, facter("--config '#{config_file}'")) do |facter_output|
20 on(agent, facter("--config \"#{config_file}\"")) do |facter_output|
2121 assert_match(/DEBUG/, facter_output.stderr, "Expected debug output on stderr")
2222 end
2323 end
1919 end
2020
2121 step "blocked facts should not be resolved" do
22 on(agent, facter("--config '#{config_file}'")) do |facter_output|
22 on(agent, facter("--config \"#{config_file}\"")) do |facter_output|
2323 # every platform attempts to resolve at least EC2 facts
2424 assert_match(/blocking collection of .+ facts/, facter_output.stderr, "Expected stderr to contain statement about blocking fact collection")
2525
4646 end
4747
4848 step "Agent #{agent}: resolve a fact from the command line custom-dir and not the config file" do
49 on(agent, facter("--config '#{config_file}' --custom-dir '#{custom_cli_dir}' --json")) do |facter_output|
49 on(agent, facter("--config \"#{config_file}\" --custom-dir \"#{custom_cli_dir}\" --json")) do |facter_output|
5050 results = JSON.parse(facter_output.stdout)
5151 assert_equal("cli_value", results['cli_fact'], "Incorrect custom fact value for cli_fact")
5252 assert_nil(results['config_fact'], "Config fact should not resolve and be nil")
3333 end
3434
3535 step "Agent #{agent}: resolve a fact from the configured custom-dir path" do
36 on(agent, facter("--config '#{config_file}' config_fact")) do |facter_output|
36 on(agent, facter("--config \"#{config_file}\" config_fact")) do |facter_output|
3737 assert_equal("config_value", facter_output.stdout.chomp, "Incorrect custom fact value")
3838 end
3939 end
4646 end
4747
4848 step "Agent #{agent}: resolve a fact from each configured custom-dir path" do
49 on(agent, facter("--config '#{config_file}' --json")) do |facter_output|
49 on(agent, facter("--config \"#{config_file}\" --json")) do |facter_output|
5050 results = JSON.parse(facter_output.stdout)
5151 assert_equal("config_value_1", results['config_fact_1'], "Incorrect custom fact value for config_fact_1")
5252 assert_equal("config_value_2", results['config_fact_2'], "Incorrect custom fact value for config_fact_2")
2121 end
2222
2323 agent.mkdir_p(config_dir)
24
2425 create_remote_file(agent, config_file, config)
2526
2627 teardown do
3030 end
3131
3232 step "Agent #{agent}: resolve a fact from the command line external-dir and not the config file" do
33 on(agent, facter("--config '#{config_file}' --external-dir '#{external_cli_dir}' --json")) do |facter_output|
33 on(agent, facter("--config \"#{config_file}\" --external-dir \"#{external_cli_dir}\" --json")) do |facter_output|
3434 results = JSON.parse(facter_output.stdout)
3535 assert_equal("cli_value", results['cli_fact'], "Incorrect custom fact value for cli_fact")
3636 assert_nil(results['config_fact'], "Config fact should not resolve and be nil")
2828 end
2929
3030 step "Agent #{agent}: resolve a fact in the external-dir in the configuration file" do
31 on(agent, facter("--config '#{config_file}' single_fact")) do |facter_output|
31 on(agent, facter("--config \"#{config_file}\" single_fact")) do |facter_output|
3232 assert_equal("external_value", facter_output.stdout.chomp, "Incorrect external fact value")
3333 end
3434 end
3535 end
3636
3737 step "Agent #{agent}: resolve a fact from each configured external-dir path" do
38 on(agent, facter("--config '#{config_file}' --json")) do |facter_output|
38 on(agent, facter("--config \"#{config_file}\" --json")) do |facter_output|
3939 results = JSON.parse(facter_output.stdout)
4040 assert_equal("external_value_1", results['external_fact_1'], "Incorrect external fact value for external_fact_1")
4141 assert_equal("external_value_2", results['external_fact_2'], "Incorrect external fact value for external_fact_2")
3434 end
3535
3636 step "Agent #{agent}: config option no-custom-facts : true and custom-dir should result in an options conflict error" do
37 on(agent, facter("--config '#{config_file}'"), :acceptable_exit_codes => 1) do |facter_output|
37 on(agent, facter("--config \"#{config_file}\""), :acceptable_exit_codes => 1) do |facter_output|
3838 assert_match(/options conflict/, facter_output.stderr, "Output does not contain error string")
3939 end
4040 end
3434 end
3535
3636 step "Agent #{agent}: no-custom-facts should ignore the FACTERLIB environment variable" do
37 on(agent, facter("--config '#{config_file}' custom_fact", :environment => {'FACTERLIB' => facterlib_dir})) do |facter_output|
37 on(agent, facter("--config \"#{config_file}\" custom_fact", :environment => {'FACTERLIB' => facterlib_dir})) do |facter_output|
3838 assert_equal("", facter_output.stdout.chomp, "Custom fact in FACTERLIB should not have resolved")
3939 end
4040 end
4040 end
4141
4242 step("Agent #{agent}: using config no-custom-facts : true should not resolve facts in facter directories on the $LOAD_PATH") do
43 on(agent, facter("--config '#{config_file}' custom_fact")) do |facter_output|
43 on(agent, facter("--config \"#{config_file}\" custom_fact")) do |facter_output|
4444 assert_equal("", facter_output.stdout.chomp, "Custom fact in $LOAD_PATH/facter should not have resolved")
4545 end
4646 end
2525 end
2626
2727 step "Agent #{agent}: config option no-external-facts : true and external-dir should result in an options conflict error" do
28 on(agent, facter("--config '#{config_file}'"), :acceptable_exit_codes => 1) do |facter_output|
28 on(agent, facter("--config \"#{config_file}\""), :acceptable_exit_codes => 1) do |facter_output|
2929 assert_match(/options conflict/, facter_output.stderr, "Output does not contain error string")
3030 end
3131 end
66 extend Facter::Acceptance::UserFactUtils
77
88 erroring_custom_fact = <<EOM
9 Facter.add('custom_fact') do
9 Facter.add('custom_fact_with_trace') do
1010 setcode do
1111 non_existent_value
1212 end
3939 end
4040
4141 step "trace setting should provide a backtrace for a custom fact with errors" do
42 on(agent, facter("--custom-dir '#{custom_dir}' custom_fact"), :acceptable_exit_codes => [1]) do |facter_output|
42 on(agent, facter("--custom-dir \"#{custom_dir}\" custom_fact_with_trace"), :acceptable_exit_codes => [1]) do |facter_output|
4343 assert_match(/backtrace:\s+#{custom_fact}/, facter_output.stderr, "Expected a backtrace for erroneous custom fact")
4444 end
4545 end
1818 ext = get_external_fact_script_extension(agent['platform'])
1919 external_fact = File.join(external_dir, "#{external_cachegroup}#{ext}")
2020 if agent['platform'] =~ /windows/
21 if agent.is_cygwin?
2122 external_fact_content = <<EOM
2223 cat << EOF
2324 {
2526 }
2627 EOF
2728 EOM
29 else
30 external_fact_content = <<EOM
31 @echo off
32 SetLocal EnableDelayedExpansion
33 echo {"#{cached_fact_name}": "#{initial_fact_value}"}
34
35 EOM
36 end
2837 else
2938 external_fact_content = <<EOM
3039 #!/bin/sh
7079
7180 step "should create a JSON file for a fact that is to be cached" do
7281 agent.rm_rf(cached_facts_dir)
73 on(agent, facter("--external-dir '#{external_dir}' --debug #{cached_fact_name}")) do |facter_output|
82 on(agent, facter("--external-dir \"#{external_dir}\" --debug #{cached_fact_name}")) do |facter_output|
7483 assert_match(/caching values for .+ facts/, facter_output.stderr, "Expected debug message to state that values will be cached")
7584 end
76 on(agent, "cat #{cached_fact_file}", :acceptable_exit_codes => [0]) do |cat_output|
77 assert_match(/#{cached_fact_name}/, cat_output.stdout, "Expected cached fact file to contain fact information")
78 end
85 cat_output = agent.cat(cached_fact_file)
86 assert_match(/#{cached_fact_name}/, cat_output.strip, "Expected cached fact file to contain fact information")
7987 end
8088
8189 step "should read from a cached JSON file for a fact that has been cached" do
8290 agent.rm_rf(cached_facts_dir)
83 on(agent, facter("--external-dir '#{external_dir}' --debug #{cached_fact_name}"))
91 on(agent, facter("--external-dir \"#{external_dir}\" --debug #{cached_fact_name}"))
8492
8593 create_remote_file(agent, cached_fact_file, cached_fact_content)
8694
87 on(agent, facter("--external-dir '#{external_dir}' --debug #{cached_fact_name}")) do |facter_output|
95 on(agent, facter("--external-dir \"#{external_dir}\" --debug #{cached_fact_name}")) do |facter_output|
8896 assert_match(/loading cached values for .+ facts/, stderr, "Expected debug message to state that values are read from cache")
8997 assert_match(/#{cached_fact_value}/, stdout, "Expected fact to match the cached fact file")
9098 end
5252
5353 step "should create a JSON file for a fact that is to be cached" do
5454 agent.rm_rf(cached_facts_dir)
55 on(agent, facter("--external-dir '#{external_dir}' --debug #{cached_fact_name}")) do |facter_output|
55 on(agent, facter("--external-dir \"#{external_dir}\" --debug #{cached_fact_name}")) do |facter_output|
5656 assert_match(/caching values for .+ facts/, facter_output.stderr, "Expected debug message to state that values will be cached")
5757 end
58 on(agent, "cat #{cached_fact_file}", :acceptable_exit_codes => [0]) do |cat_output|
59 assert_match(/#{cached_fact_name}/, cat_output.stdout, "Expected cached fact file to contain fact information")
60 end
58 cat_output = agent.cat(cached_fact_file)
59 assert_match(/#{cached_fact_name}/, cat_output.strip, "Expected cached fact file to contain fact information")
6160 end
6261
6362 step "should read from a cached JSON file for a fact that has been cached" do
6463 agent.rm_rf(cached_facts_dir)
65 on(agent, facter("--external-dir '#{external_dir}' --debug #{cached_fact_name}"))
64 on(agent, facter("--external-dir \"#{external_dir}\" --debug #{cached_fact_name}"))
6665
6766 create_remote_file(agent, cached_fact_file, cached_fact_content)
6867
69 on(agent, facter("--external-dir '#{external_dir}' --debug #{cached_fact_name}")) do |facter_output|
68 on(agent, facter("--external-dir \"#{external_dir}\" --debug #{cached_fact_name}")) do |facter_output|
7069 assert_match(/loading cached values for .+ facts/, stderr, "Expected debug message to state that values are read from cache")
7170 assert_match(/#{cached_fact_value}/, stdout, "Expected fact to match the cached fact file")
7271 end
1818 ext = get_external_fact_script_extension(agent['platform'])
1919 external_fact = File.join(external_dir, "#{external_cachegroup}#{ext}")
2020 if agent['platform'] =~ /windows/
21 if agent.is_cygwin?
2122 external_fact_content = <<EOM
2223 cat << EOF
2324 #{cached_fact_name}: "#{initial_fact_value}"
2425 EOF
2526 EOM
27 else
28 external_fact_content = <<EOM
29 @echo off
30 SetLocal EnableDelayedExpansion
31 echo {"#{cached_fact_name}": "#{initial_fact_value}"}
32
33 EOM
34 end
2635 else
2736 external_fact_content = <<EOM
2837 #!/bin/sh
6574
6675 step "should create a JSON file for a fact that is to be cached" do
6776 agent.rm_rf(cached_facts_dir)
68 on(agent, facter("--external-dir '#{external_dir}' --debug #{cached_fact_name}")) do |facter_output|
77 on(agent, facter("--external-dir \"#{external_dir}\" --debug #{cached_fact_name}")) do |facter_output|
6978 assert_match(/caching values for .+ facts/, facter_output.stderr, "Expected debug message to state that values will be cached")
7079 end
71 on(agent, "cat #{cached_fact_file}", :acceptable_exit_codes => [0]) do |cat_output|
72 assert_match(/#{cached_fact_name}/, cat_output.stdout, "Expected cached fact file to contain fact information")
73 end
80 cat_output = agent.cat(cached_fact_file)
81 assert_match(/#{cached_fact_name}/, cat_output.strip, "Expected cached fact file to contain fact information")
7482 end
7583
7684 step "should read from a cached JSON file for a fact that has been cached" do
7785 agent.rm_rf(cached_facts_dir)
78 on(agent, facter("--external-dir '#{external_dir}' --debug #{cached_fact_name}"))
86 on(agent, facter("--external-dir \"#{external_dir}\" --debug #{cached_fact_name}"))
7987
8088 create_remote_file(agent, cached_fact_file, cached_fact_content)
8189
82 on(agent, facter("--external-dir '#{external_dir}' --debug #{cached_fact_name}")) do |facter_output|
90 on(agent, facter("--external-dir \"#{external_dir}\" --debug #{cached_fact_name}")) do |facter_output|
8391 assert_match(/loading cached values for .+ facts/, stderr, "Expected debug message to state that values are read from cache")
8492 assert_match(/#{cached_fact_value}/, stdout, "Expected fact to match the cached fact file")
8593 end
5656
5757 step "should create a JSON file for a fact that is to be cached" do
5858 agent.rm_rf(cached_facts_dir)
59 on(agent, facter("--external-dir '#{external_dir}' --debug #{cached_fact_name}")) do |facter_output|
59 on(agent, facter("--external-dir \"#{external_dir}\" --debug #{cached_fact_name}")) do |facter_output|
6060 assert_match(/caching values for .+ facts/, facter_output.stderr, "Expected debug message to state that values will be cached")
6161 end
62 on(agent, "cat #{cached_fact_file}", :acceptable_exit_codes => [0]) do |cat_output|
63 assert_match(/#{cached_fact_name}/, cat_output.stdout, "Expected cached fact file to contain fact information")
64 end
62 cat_output = agent.cat(cached_fact_file)
63 assert_match(/#{cached_fact_name}/, cat_output.strip, "Expected cached fact file to contain fact information")
6564 end
6665
6766 step "should read from a cached JSON file for a fact that has been cached" do
6867 agent.rm_rf(cached_facts_dir)
69 on(agent, facter("--external-dir '#{external_dir}' --debug #{cached_fact_name}"))
68 on(agent, facter("--external-dir \"#{external_dir}\" --debug #{cached_fact_name}"))
7069
7170 create_remote_file(agent, cached_fact_file, cached_fact_content)
7271
73 on(agent, facter("--external-dir '#{external_dir}' --debug #{cached_fact_name}")) do |facter_output|
72 on(agent, facter("--external-dir \"#{external_dir}\" --debug #{cached_fact_name}")) do |facter_output|
7473 assert_match(/loading cached values for .+ facts/, stderr, "Expected debug message to state that values are read from cache")
7574 assert_match(/#{cached_fact_value}/, stdout, "Expected fact to match the cached fact file")
7675 end
5555
5656 step "should create a JSON file for a fact that is to be cached" do
5757 agent.rm_rf(cached_facts_dir)
58 on(agent, facter("--external-dir '#{external_dir}' --debug #{cached_fact_name}")) do |facter_output|
58 on(agent, facter("--external-dir \"#{external_dir}\" --debug #{cached_fact_name}")) do |facter_output|
5959 assert_match(/caching values for .+ facts/, facter_output.stderr, "Expected debug message to state that values will be cached")
6060 end
61 on(agent, "cat #{cached_fact_file}", :acceptable_exit_codes => [0]) do |cat_output|
62 assert_match(/#{cached_fact_name}/, cat_output.stdout, "Expected cached fact file to contain fact information")
63 end
61 cat_output = agent.cat(cached_fact_file)
62 assert_match(/#{cached_fact_name}/, cat_output.strip, "Expected cached fact file to contain fact information")
6463 end
6564
6665 step "should read from a cached JSON file for a fact that has been cached" do
6766 agent.rm_rf(cached_facts_dir)
68 on(agent, facter("--external-dir '#{external_dir}' --debug #{cached_fact_name}"))
67 on(agent, facter("--external-dir \"#{external_dir}\" --debug #{cached_fact_name}"))
6968
7069 create_remote_file(agent, cached_fact_file, cached_fact_content)
7170
72 on(agent, facter("--external-dir '#{external_dir}' --debug #{cached_fact_name}")) do |facter_output|
71 on(agent, facter("--external-dir \"#{external_dir}\" --debug #{cached_fact_name}")) do |facter_output|
7372 assert_match(/loading cached values for .+ facts/, stderr, "Expected debug message to state that values are read from cache")
7473 assert_match(/#{cached_fact_value}/, stdout, "Expected fact to match the cached fact file")
7574 end
5555
5656 step "should create a JSON file for a fact that is to be cached" do
5757 agent.rm_rf(cached_facts_dir)
58 on(agent, facter("--external-dir '#{external_dir}' --debug #{cached_fact_name}")) do |facter_output|
58 on(agent, facter("--external-dir \"#{external_dir}\" --debug #{cached_fact_name}")) do |facter_output|
5959 assert_match(/caching values for .+ facts/, facter_output.stderr, "Expected debug message to state that values will be cached")
6060 end
61 on(agent, "cat #{cached_fact_file}", :acceptable_exit_codes => [0]) do |cat_output|
62 assert_match(/#{cached_fact_name}/, cat_output.stdout, "Expected cached fact file to contain fact information")
63 end
61 cat_output = agent.cat(cached_fact_file)
62 assert_match(/#{cached_fact_name}/, cat_output.strip, "Expected cached fact file to contain fact information")
6463 end
6564
6665 step "should read from a cached JSON file for a fact that has been cached" do
6766 agent.rm_rf(cached_facts_dir)
68 on(agent, facter("--external-dir '#{external_dir}' --debug #{cached_fact_name}"))
67 on(agent, facter("--external-dir \"#{external_dir}\" --debug #{cached_fact_name}"))
6968
7069 create_remote_file(agent, cached_fact_file, cached_fact_content)
7170
72 on(agent, facter("--external-dir '#{external_dir}' --debug #{cached_fact_name}")) do |facter_output|
71 on(agent, facter("--external-dir \"#{external_dir}\" --debug #{cached_fact_name}")) do |facter_output|
7372 assert_match(/loading cached values for .+ facts/, stderr, "Expected debug message to state that values are read from cache")
7473 assert_match(/#{cached_fact_value}/, stdout, "Expected fact to match the cached fact file")
7574 end
5858 no_cache_config_file = File.join(config_dir, "no-cache.conf")
5959 create_remote_file(agent, no_cache_config_file, config_no_cache)
6060
61 on(agent, facter("--config '#{no_cache_config_file}'")) do |facter_output|
61 on(agent, facter("--config \"#{no_cache_config_file}\"")) do |facter_output|
6262 assert_no_match(/#{cached_fact_value}/, facter_output.stdout, "Expected to not see the cached fact value")
6363 end
6464
3838 on(agent, facter("--debug")) do |facter_output|
3939 assert_match(/caching values for .+ facts/, facter_output.stderr, "Expected debug message to state that values will be cached")
4040 end
41 on(agent, "cat #{cached_fact_file}", :acceptable_exit_codes => [0]) do |cat_output|
42 assert_match(/#{cached_factname}/, cat_output.stdout, "Expected cached fact file to contain fact information")
43 end
41 cat_output = agent.cat(cached_fact_file)
42 assert_match(/#{cached_factname}/, cat_output.strip, "Expected cached fact file to contain fact information")
4443 end
4544 end
4645 end
5151 on(agent, facter("#{cached_factname}"))
5252
5353 # Read cached fact file content
54 on(agent, "cat #{cached_fact_file}", :acceptable_exit_codes => [0]) do |cat_output|
55 assert_no_match(/#{cached_fact_value}/, cat_output.stdout, "Expected cached fact file to be refreshed")
56 end
54 cat_output = agent.cat(cached_fact_file)
55 assert_no_match(/#{cached_fact_value}/, cat_output.strip, "Expected cached fact file to be refreshed")
5756 end
5857 end
5958 end
4343 on(agent, facter("#{cached_factname}")) do
4444 assert_match(/.+/, stdout, "Expected fact to be resolved")
4545 end
46 on(agent, "cat #{cached_fact_file}", :acceptable_exit_codes => [0]) do |cat_output|
47 assert_match(/#{cached_factname}/, cat_output.stdout, "Expected cachced fact to contain the fact name")
48 end
46 cat_output = agent.cat(cached_fact_file)
47 assert_match(/#{cached_factname}/, cat_output.strip, "Expected cachced fact to contain the fact name")
4948 end
5049 end
5150 end
3636 on(agent, puppet("facts --debug")) do |pupppet_fact_output|
3737 assert_match(/caching values for .+ facts/, pupppet_fact_output.stdout, "Expected debug message to state that values will be cached")
3838 end
39 on(agent, "cat #{cached_fact_file}", :acceptable_exit_codes => [0]) do |cat_output|
40 assert_match(/#{cached_factname}/, cat_output.stdout, "Expected cached fact file to contain fact information")
41 end
39 cat_output = agent.cat(cached_fact_file)
40 assert_match(/#{cached_factname}/, cat_output.strip, "Expected cached fact file to contain fact information")
4241 end
4342 end
4443 end
66 extend Facter::Acceptance::UserFactUtils
77
88 content = <<EOM
9 Facter.add('custom_fact') do
9 Facter.add('single_custom_fact') do
1010 setcode do
1111 "single_fact"
1212 end
2424 end
2525
2626 step "Agent #{agent}: --custom-dir option should resolve custom facts from the specific directory" do
27 on(agent, facter("--custom-dir '#{custom_dir}' custom_fact")) do |facter_output|
27 on(agent, facter("--custom-dir \"#{custom_dir}\" single_custom_fact")) do |facter_output|
2828 assert_equal("single_fact", facter_output.stdout.chomp, "Incorrect custom fact value")
2929 end
3030 end
55 extend Facter::Acceptance::UserFactUtils
66
77 content = <<EOM
8 Facter.add('custom_fact') do
8 Facter.add('custom_fact_facterlib') do
9 # has_weight 1
910 setcode do
1011 "facterlib"
1112 end
2324 end
2425
2526 step "Agent #{agent}: facter should resolve a fact from the directory specified by the environment variable FACTERLIB" do
26 on(agent, facter('custom_fact', :environment => { 'FACTERLIB' => custom_dir })) do |facter_output|
27 on(agent, facter('custom_fact_facterlib', :environment => { 'FACTERLIB' => custom_dir })) do |facter_output|
2728 assert_equal("facterlib", facter_output.stdout.chomp, "Incorrect custom fact value for fact in FACTERLIB")
2829 end
2930 end
3737 end
3838
3939 step "Agent #{agent}: resolve a fact from each specified --custom-dir option" do
40 on(agent, facter("--custom-dir #{custom_dir_1} --custom-dir #{custom_dir_2} --json")) do |facter_output|
40 on(agent, facter("--custom-dir \"#{custom_dir_1}\" --custom-dir \"#{custom_dir_2}\" --json")) do |facter_output|
4141 results = JSON.parse(facter_output.stdout)
4242 assert_equal("testvalue_1", results['custom_fact_1'], "Incorrect custom fact value for custom_fact_1")
4343 assert_equal("testvalue_2", results['custom_fact_2'], "Incorrect custom fact value for custom_fact_2")
1818 end
1919
2020 step "Agent #{agent}: resolve a fact from each specified --external_dir option" do
21 on(agent, facter("--external-dir #{external_dir} single_fact")) do |facter_output|
21 on(agent, facter("--external-dir \"#{external_dir}\" single_fact")) do |facter_output|
2222 assert_equal("external_value", facter_output.stdout.chomp, "Incorrect external fact value")
2323 end
2424 end
2525 end
2626
2727 step "Agent #{agent}: resolve a fact from each specified --external_dir option" do
28 on(agent, facter("--external-dir #{external_dir_1} --external-dir #{external_dir_2} --json")) do |facter_output|
28 on(agent, facter("--external-dir \"#{external_dir_1}\" --external-dir \"#{external_dir_2}\" --json")) do |facter_output|
2929 results = JSON.parse(facter_output.stdout)
3030 assert_equal("external_value_1", results['external_fact_1'], "Incorrect external fact value for external_fact_1")
3131 assert_equal("external_value_2", results['external_fact_2'], "Incorrect external fact value for external_fact_2")
2828 end
2929
3030 step "Agent #{agent}: retrieve output using the --json option" do
31 on(agent, facter("--custom-dir '#{custom_dir}' --json structured_fact")) do
31 on(agent, facter("--custom-dir \"#{custom_dir}\" --json structured_fact")) do
3232 begin
3333 expected = {"structured_fact" => {"foo" => {"nested" => "value1"}, "bar" => "value2", "baz" => "value3", "true" => true, "false" => false}}
3434 assert_equal(expected, JSON.parse(stdout.chomp), "JSON output does not match expected output")
4141 external_fact_script_yaml = File.join(external_dir, "#{external_filename}.yaml")
4242 create_remote_file(agent, external_fact_script_yaml, '')
4343
44 on(agent, facter("--external-dir #{external_dir} --list-cache-groups")) do |facter_output|
44 on(agent, facter("--external-dir \"#{external_dir}\" --list-cache-groups")) do |facter_output|
4545 assert_match(/#{external_filename}#{ext}/, facter_output.stdout, "external facts script files should be listed as cacheable")
4646 assert_match(/#{external_filename}.txt/, facter_output.stdout, "external facts txt files should be listed as cacheable")
4747 assert_match(/#{external_filename}.json/, facter_output.stdout, "external facts json files should be listed as cacheable")
5454 assert_no_match(/caching/, facter_output.stderr, "facter should not have tried to refresh the cache")
5555 end
5656
57 on(agent, "cat '#{cached_fact_file}'", :acceptable_exit_codes => [0]) do |cat_output|
58 assert_match(/#{bad_cached_content}/, cat_output.stdout, "facter should not have updated the cached value")
59 end
57 cat_output = agent.cat(cached_fact_file)
58 assert_match(/#{bad_cached_content.chomp}/, cat_output.strip, "facter should not have updated the cached value")
6059 end
6160 end
6261 end
55 external_dir = agent.puppet['pluginfactdest']
66 external_file = File.join(external_dir, "external.txt")
77 custom_dir = File.join(agent.puppet['plugindest'], "facter")
8 custom_file = "#{custom_dir}/custom.rb"
8 custom_file = File.join(custom_dir, 'custom.rb')
99
1010 teardown do
1111 agent.rm_rf(external_file)
66 extend Facter::Acceptance::UserFactUtils
77
88 content = <<EOM
9 Facter.add('custom_fact') do
9 Facter.add('custom_fact_trace') do
1010 setcode do
1111 non_existent_value
1212 end
2626 end
2727
2828 step "--trace option should provide a backtrace for a custom fact with errors" do
29 on(agent, facter("--custom-dir '#{custom_dir}' --trace custom_fact"), :acceptable_exit_codes => [1]) do
29 on(agent, facter("--custom-dir \"#{custom_dir}\" --trace custom_fact_trace"), :acceptable_exit_codes => [1]) do
3030 assert_match(/backtrace:\s+#{custom_fact}/, stderr, "Expected a backtrace for erroneous custom fact")
3131 end
3232 end
2828 end
2929
3030 step "Agent #{agent}: retrieve output using the --yaml option" do
31 on(agent, facter("--custom-dir '#{custom_dir}' --yaml structured_fact")) do
31 on(agent, facter("--custom-dir \"#{custom_dir}\" --yaml structured_fact")) do
3232 begin
3333 expected = {"structured_fact" => {"foo" => {"nested" => "value1"}, "bar" => "value2", "baz" => "value3" }}
3434 assert_equal(expected, YAML.load(stdout), "YAML output does not match expected output")