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
66 | 66 | step "should create a cached-custom-facts cache file that containt fact information" do |
67 | 67 | result = agent.file_exist?("#{cache_folder}/cached-custom-facts") |
68 | 68 | 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') | |
72 | 71 | end |
73 | 72 | |
74 | 73 | step 'should read from the cached file for a custom fact that has been cached' do |
40 | 40 | value: "'#{custom_fact_value}'" |
41 | 41 | ) |
42 | 42 | |
43 | on(agent, facter("--custom-dir=#{custom_fact_dir} timezone")) do |result| | |
43 | on(agent, facter("--custom-dir \"#{custom_fact_dir}\" timezone")) do |result| | |
44 | 44 | assert_match(/#{custom_fact_value}/, result.stdout.chomp, "Facter does not use the custom fact's value when its weight is > 0") |
45 | 45 | end |
46 | 46 | end |
57 | 57 | ) |
58 | 58 | end |
59 | 59 | |
60 | on(agent, facter("--custom-dir=#{custom_fact_dir} timezone")) do |result| | |
60 | on(agent, facter("--custom-dir \"#{custom_fact_dir}\" timezone")) do |result| | |
61 | 61 | assert_match(/#{builtin_value}/, result.stdout.chomp, "Facter does not use the builtin fact's value when all conflicting custom facts fail to resolve") |
62 | 62 | end |
63 | 63 | end |
76 | 76 | ) |
77 | 77 | end |
78 | 78 | |
79 | on(agent, facter("--custom-dir=#{custom_fact_dir} timezone")) do |result| | |
79 | on(agent, facter("--custom-dir \"#{custom_fact_dir}\" timezone")) do |result| | |
80 | 80 | assert_match(/#{builtin_value}/, result.stdout.chomp, "Facter does not give precedence to the builtin fact when all custom facts have zero weight") |
81 | 81 | end |
82 | 82 | end |
96 | 96 | ) |
97 | 97 | end |
98 | 98 | |
99 | on(agent, facter("--custom-dir=#{custom_fact_dir} timezone")) do |result| | |
99 | on(agent, facter("--custom-dir \"#{custom_fact_dir}\" timezone")) do |result| | |
100 | 100 | assert_match(/#{builtin_value}/, result.stdout.chomp, "Facter does not give precedence to the builtin fact when only some custom facts have zero weight") |
101 | 101 | end |
102 | 102 | end |
+1
-1
20 | 20 | |
21 | 21 | # Custom fact with weight >= 10001 should override an external fact |
22 | 22 | 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| | |
24 | 24 | assert_equal("CUSTOM", facter_output.stdout.chomp) |
25 | 25 | end |
26 | 26 | end |
4 | 4 | require 'timeout' |
5 | 5 | |
6 | 6 | content = <<-EOM |
7 | Facter.add('custom_fact') do | |
7 | Facter.add('custom_fact_ole') do | |
8 | 8 | setcode do |
9 | 9 | require 'win32ole' |
10 | 10 | locator = WIN32OLE.new('WbemScripting.SWbemLocator') |
24 | 24 | |
25 | 25 | # Test is assumed to have hung if it takes longer than 5 seconds. |
26 | 26 | 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| | |
28 | 28 | assert_match(/#<WIN32OLE:0x[0-9a-f]+>/, facter_result.stdout.chomp, 'Custom fact output does not match expected output') |
29 | 29 | end |
30 | 30 | end |
78 | 78 | step "should create a cached-custom-facts cache file that containt fact information from the highest weight fact" do |
79 | 79 | result = agent.file_exist?("#{cache_folder}/cached-custom-facts") |
80 | 80 | 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') | |
84 | 83 | end |
85 | 84 | |
86 | 85 | step 'should read from the cached file for a custom fact that has been cached' do |
24 | 24 | end |
25 | 25 | |
26 | 26 | 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| | |
28 | 28 | assert_equal(fact_value, |
29 | 29 | facter_output.stdout.chomp, |
30 | 30 | 'Expected external fact to resolve as defined in script') |
32 | 32 | end |
33 | 33 | |
34 | 34 | 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}", | |
36 | 36 | :environment => { "FACTER_#{fact_name}" => override_value })) do |facter_output| |
37 | 37 | assert_equal(override_value, |
38 | 38 | facter_output.stdout.chomp, |
27 | 27 | end |
28 | 28 | |
29 | 29 | 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| | |
31 | 31 | assert_equal('OVERRIDE_value', facter_output.stdout.chomp, 'Expected to resolve override version of the external_fact') |
32 | 32 | end |
33 | 33 | end |
19 | 19 | end |
20 | 20 | |
21 | 21 | 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| | |
23 | 23 | assert_equal("EXTERNAL", facter_output.stdout.chomp) |
24 | 24 | end |
25 | 25 | end |
+1
-1
20 | 20 | |
21 | 21 | # Custom fact with weight <= 10000 should give precedence to the EXTERNAL fact |
22 | 22 | 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| | |
24 | 24 | assert_equal("EXTERNAL", facter_output.stdout.chomp) |
25 | 25 | end |
26 | 26 | end |
25 | 25 | # External fact should take precedence over a custom fact with a confine |
26 | 26 | # (from FACT-1413) |
27 | 27 | 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| | |
29 | 29 | assert_equal("EXTERNAL", facter_output.stdout.chomp) |
30 | 30 | end |
31 | 31 | end |
32 | 32 | end |
33 | 33 | |
34 | 34 | 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| | |
36 | 36 | assert_match(/#{fact1} => #{fact1_value}/, stdout, 'Expected fact to match first fact') |
37 | 37 | assert_match(/#{fact2} => #{fact2_value}/, stdout, 'Expected fact to match second fact') |
38 | 38 | end |
48 | 48 | EOM |
49 | 49 | agent.mkdir_p(config_dir) |
50 | 50 | 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| | |
52 | 52 | 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') |
53 | 53 | assert_match(/#{fact1} => #{fact1_value}/, stdout, 'Expected fact to match first fact') |
54 | 54 | assert_not_match(/#{fact2} => #{fact2_value}/, stdout, 'Expected fact not to match second fact') |
17 | 17 | end |
18 | 18 | |
19 | 19 | 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| | |
21 | 21 | assert_match(/DEBUG/, facter_output.stderr, "Expected debug output on stderr") |
22 | 22 | end |
23 | 23 | end |
19 | 19 | end |
20 | 20 | |
21 | 21 | 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| | |
23 | 23 | # every platform attempts to resolve at least EC2 facts |
24 | 24 | assert_match(/blocking collection of .+ facts/, facter_output.stderr, "Expected stderr to contain statement about blocking fact collection") |
25 | 25 |
46 | 46 | end |
47 | 47 | |
48 | 48 | 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| | |
50 | 50 | results = JSON.parse(facter_output.stdout) |
51 | 51 | assert_equal("cli_value", results['cli_fact'], "Incorrect custom fact value for cli_fact") |
52 | 52 | assert_nil(results['config_fact'], "Config fact should not resolve and be nil") |
33 | 33 | end |
34 | 34 | |
35 | 35 | 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| | |
37 | 37 | assert_equal("config_value", facter_output.stdout.chomp, "Incorrect custom fact value") |
38 | 38 | end |
39 | 39 | end |
46 | 46 | end |
47 | 47 | |
48 | 48 | 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| | |
50 | 50 | results = JSON.parse(facter_output.stdout) |
51 | 51 | assert_equal("config_value_1", results['config_fact_1'], "Incorrect custom fact value for config_fact_1") |
52 | 52 | assert_equal("config_value_2", results['config_fact_2'], "Incorrect custom fact value for config_fact_2") |
21 | 21 | end |
22 | 22 | |
23 | 23 | agent.mkdir_p(config_dir) |
24 | ||
24 | 25 | create_remote_file(agent, config_file, config) |
25 | 26 | |
26 | 27 | teardown do |
30 | 30 | end |
31 | 31 | |
32 | 32 | 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| | |
34 | 34 | results = JSON.parse(facter_output.stdout) |
35 | 35 | assert_equal("cli_value", results['cli_fact'], "Incorrect custom fact value for cli_fact") |
36 | 36 | assert_nil(results['config_fact'], "Config fact should not resolve and be nil") |
28 | 28 | end |
29 | 29 | |
30 | 30 | 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| | |
32 | 32 | assert_equal("external_value", facter_output.stdout.chomp, "Incorrect external fact value") |
33 | 33 | end |
34 | 34 | end |
35 | 35 | end |
36 | 36 | |
37 | 37 | 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| | |
39 | 39 | results = JSON.parse(facter_output.stdout) |
40 | 40 | assert_equal("external_value_1", results['external_fact_1'], "Incorrect external fact value for external_fact_1") |
41 | 41 | assert_equal("external_value_2", results['external_fact_2'], "Incorrect external fact value for external_fact_2") |
34 | 34 | end |
35 | 35 | |
36 | 36 | 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| | |
38 | 38 | assert_match(/options conflict/, facter_output.stderr, "Output does not contain error string") |
39 | 39 | end |
40 | 40 | end |
34 | 34 | end |
35 | 35 | |
36 | 36 | 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| | |
38 | 38 | assert_equal("", facter_output.stdout.chomp, "Custom fact in FACTERLIB should not have resolved") |
39 | 39 | end |
40 | 40 | end |
40 | 40 | end |
41 | 41 | |
42 | 42 | 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| | |
44 | 44 | assert_equal("", facter_output.stdout.chomp, "Custom fact in $LOAD_PATH/facter should not have resolved") |
45 | 45 | end |
46 | 46 | end |
25 | 25 | end |
26 | 26 | |
27 | 27 | 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| | |
29 | 29 | assert_match(/options conflict/, facter_output.stderr, "Output does not contain error string") |
30 | 30 | end |
31 | 31 | end |
6 | 6 | extend Facter::Acceptance::UserFactUtils |
7 | 7 | |
8 | 8 | erroring_custom_fact = <<EOM |
9 | Facter.add('custom_fact') do | |
9 | Facter.add('custom_fact_with_trace') do | |
10 | 10 | setcode do |
11 | 11 | non_existent_value |
12 | 12 | end |
39 | 39 | end |
40 | 40 | |
41 | 41 | 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| | |
43 | 43 | assert_match(/backtrace:\s+#{custom_fact}/, facter_output.stderr, "Expected a backtrace for erroneous custom fact") |
44 | 44 | end |
45 | 45 | end |
+14
-6
18 | 18 | ext = get_external_fact_script_extension(agent['platform']) |
19 | 19 | external_fact = File.join(external_dir, "#{external_cachegroup}#{ext}") |
20 | 20 | if agent['platform'] =~ /windows/ |
21 | if agent.is_cygwin? | |
21 | 22 | external_fact_content = <<EOM |
22 | 23 | cat << EOF |
23 | 24 | { |
25 | 26 | } |
26 | 27 | EOF |
27 | 28 | 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 | |
28 | 37 | else |
29 | 38 | external_fact_content = <<EOM |
30 | 39 | #!/bin/sh |
70 | 79 | |
71 | 80 | step "should create a JSON file for a fact that is to be cached" do |
72 | 81 | 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| | |
74 | 83 | assert_match(/caching values for .+ facts/, facter_output.stderr, "Expected debug message to state that values will be cached") |
75 | 84 | 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") | |
79 | 87 | end |
80 | 88 | |
81 | 89 | step "should read from a cached JSON file for a fact that has been cached" do |
82 | 90 | 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}")) | |
84 | 92 | |
85 | 93 | create_remote_file(agent, cached_fact_file, cached_fact_content) |
86 | 94 | |
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| | |
88 | 96 | assert_match(/loading cached values for .+ facts/, stderr, "Expected debug message to state that values are read from cache") |
89 | 97 | assert_match(/#{cached_fact_value}/, stdout, "Expected fact to match the cached fact file") |
90 | 98 | end |
+5
-6
52 | 52 | |
53 | 53 | step "should create a JSON file for a fact that is to be cached" do |
54 | 54 | 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| | |
56 | 56 | assert_match(/caching values for .+ facts/, facter_output.stderr, "Expected debug message to state that values will be cached") |
57 | 57 | 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") | |
61 | 60 | end |
62 | 61 | |
63 | 62 | step "should read from a cached JSON file for a fact that has been cached" do |
64 | 63 | 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}")) | |
66 | 65 | |
67 | 66 | create_remote_file(agent, cached_fact_file, cached_fact_content) |
68 | 67 | |
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| | |
70 | 69 | assert_match(/loading cached values for .+ facts/, stderr, "Expected debug message to state that values are read from cache") |
71 | 70 | assert_match(/#{cached_fact_value}/, stdout, "Expected fact to match the cached fact file") |
72 | 71 | end |
+14
-6
18 | 18 | ext = get_external_fact_script_extension(agent['platform']) |
19 | 19 | external_fact = File.join(external_dir, "#{external_cachegroup}#{ext}") |
20 | 20 | if agent['platform'] =~ /windows/ |
21 | if agent.is_cygwin? | |
21 | 22 | external_fact_content = <<EOM |
22 | 23 | cat << EOF |
23 | 24 | #{cached_fact_name}: "#{initial_fact_value}" |
24 | 25 | EOF |
25 | 26 | 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 | |
26 | 35 | else |
27 | 36 | external_fact_content = <<EOM |
28 | 37 | #!/bin/sh |
65 | 74 | |
66 | 75 | step "should create a JSON file for a fact that is to be cached" do |
67 | 76 | 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| | |
69 | 78 | assert_match(/caching values for .+ facts/, facter_output.stderr, "Expected debug message to state that values will be cached") |
70 | 79 | 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") | |
74 | 82 | end |
75 | 83 | |
76 | 84 | step "should read from a cached JSON file for a fact that has been cached" do |
77 | 85 | 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}")) | |
79 | 87 | |
80 | 88 | create_remote_file(agent, cached_fact_file, cached_fact_content) |
81 | 89 | |
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| | |
83 | 91 | assert_match(/loading cached values for .+ facts/, stderr, "Expected debug message to state that values are read from cache") |
84 | 92 | assert_match(/#{cached_fact_value}/, stdout, "Expected fact to match the cached fact file") |
85 | 93 | end |
56 | 56 | |
57 | 57 | step "should create a JSON file for a fact that is to be cached" do |
58 | 58 | 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| | |
60 | 60 | assert_match(/caching values for .+ facts/, facter_output.stderr, "Expected debug message to state that values will be cached") |
61 | 61 | 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") | |
65 | 64 | end |
66 | 65 | |
67 | 66 | step "should read from a cached JSON file for a fact that has been cached" do |
68 | 67 | 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}")) | |
70 | 69 | |
71 | 70 | create_remote_file(agent, cached_fact_file, cached_fact_content) |
72 | 71 | |
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| | |
74 | 73 | assert_match(/loading cached values for .+ facts/, stderr, "Expected debug message to state that values are read from cache") |
75 | 74 | assert_match(/#{cached_fact_value}/, stdout, "Expected fact to match the cached fact file") |
76 | 75 | end |
55 | 55 | |
56 | 56 | step "should create a JSON file for a fact that is to be cached" do |
57 | 57 | 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| | |
59 | 59 | assert_match(/caching values for .+ facts/, facter_output.stderr, "Expected debug message to state that values will be cached") |
60 | 60 | 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") | |
64 | 63 | end |
65 | 64 | |
66 | 65 | step "should read from a cached JSON file for a fact that has been cached" do |
67 | 66 | 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}")) | |
69 | 68 | |
70 | 69 | create_remote_file(agent, cached_fact_file, cached_fact_content) |
71 | 70 | |
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| | |
73 | 72 | assert_match(/loading cached values for .+ facts/, stderr, "Expected debug message to state that values are read from cache") |
74 | 73 | assert_match(/#{cached_fact_value}/, stdout, "Expected fact to match the cached fact file") |
75 | 74 | end |
55 | 55 | |
56 | 56 | step "should create a JSON file for a fact that is to be cached" do |
57 | 57 | 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| | |
59 | 59 | assert_match(/caching values for .+ facts/, facter_output.stderr, "Expected debug message to state that values will be cached") |
60 | 60 | 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") | |
64 | 63 | end |
65 | 64 | |
66 | 65 | step "should read from a cached JSON file for a fact that has been cached" do |
67 | 66 | 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}")) | |
69 | 68 | |
70 | 69 | create_remote_file(agent, cached_fact_file, cached_fact_content) |
71 | 70 | |
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| | |
73 | 72 | assert_match(/loading cached values for .+ facts/, stderr, "Expected debug message to state that values are read from cache") |
74 | 73 | assert_match(/#{cached_fact_value}/, stdout, "Expected fact to match the cached fact file") |
75 | 74 | end |
+1
-1
58 | 58 | no_cache_config_file = File.join(config_dir, "no-cache.conf") |
59 | 59 | create_remote_file(agent, no_cache_config_file, config_no_cache) |
60 | 60 | |
61 | on(agent, facter("--config '#{no_cache_config_file}'")) do |facter_output| | |
61 | on(agent, facter("--config \"#{no_cache_config_file}\"")) do |facter_output| | |
62 | 62 | assert_no_match(/#{cached_fact_value}/, facter_output.stdout, "Expected to not see the cached fact value") |
63 | 63 | end |
64 | 64 |
38 | 38 | on(agent, facter("--debug")) do |facter_output| |
39 | 39 | assert_match(/caching values for .+ facts/, facter_output.stderr, "Expected debug message to state that values will be cached") |
40 | 40 | 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") | |
44 | 43 | end |
45 | 44 | end |
46 | 45 | end |
+2
-3
51 | 51 | on(agent, facter("#{cached_factname}")) |
52 | 52 | |
53 | 53 | # 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") | |
57 | 56 | end |
58 | 57 | end |
59 | 58 | end |
+2
-3
43 | 43 | on(agent, facter("#{cached_factname}")) do |
44 | 44 | assert_match(/.+/, stdout, "Expected fact to be resolved") |
45 | 45 | 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") | |
49 | 48 | end |
50 | 49 | end |
51 | 50 | end |
+2
-3
36 | 36 | on(agent, puppet("facts --debug")) do |pupppet_fact_output| |
37 | 37 | assert_match(/caching values for .+ facts/, pupppet_fact_output.stdout, "Expected debug message to state that values will be cached") |
38 | 38 | 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") | |
42 | 41 | end |
43 | 42 | end |
44 | 43 | end |
6 | 6 | extend Facter::Acceptance::UserFactUtils |
7 | 7 | |
8 | 8 | content = <<EOM |
9 | Facter.add('custom_fact') do | |
9 | Facter.add('single_custom_fact') do | |
10 | 10 | setcode do |
11 | 11 | "single_fact" |
12 | 12 | end |
24 | 24 | end |
25 | 25 | |
26 | 26 | 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| | |
28 | 28 | assert_equal("single_fact", facter_output.stdout.chomp, "Incorrect custom fact value") |
29 | 29 | end |
30 | 30 | end |
5 | 5 | extend Facter::Acceptance::UserFactUtils |
6 | 6 | |
7 | 7 | content = <<EOM |
8 | Facter.add('custom_fact') do | |
8 | Facter.add('custom_fact_facterlib') do | |
9 | # has_weight 1 | |
9 | 10 | setcode do |
10 | 11 | "facterlib" |
11 | 12 | end |
23 | 24 | end |
24 | 25 | |
25 | 26 | 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| | |
27 | 28 | assert_equal("facterlib", facter_output.stdout.chomp, "Incorrect custom fact value for fact in FACTERLIB") |
28 | 29 | end |
29 | 30 | end |
37 | 37 | end |
38 | 38 | |
39 | 39 | 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| | |
41 | 41 | results = JSON.parse(facter_output.stdout) |
42 | 42 | assert_equal("testvalue_1", results['custom_fact_1'], "Incorrect custom fact value for custom_fact_1") |
43 | 43 | assert_equal("testvalue_2", results['custom_fact_2'], "Incorrect custom fact value for custom_fact_2") |
18 | 18 | end |
19 | 19 | |
20 | 20 | 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| | |
22 | 22 | assert_equal("external_value", facter_output.stdout.chomp, "Incorrect external fact value") |
23 | 23 | end |
24 | 24 | end |
25 | 25 | end |
26 | 26 | |
27 | 27 | 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| | |
29 | 29 | results = JSON.parse(facter_output.stdout) |
30 | 30 | assert_equal("external_value_1", results['external_fact_1'], "Incorrect external fact value for external_fact_1") |
31 | 31 | assert_equal("external_value_2", results['external_fact_2'], "Incorrect external fact value for external_fact_2") |
28 | 28 | end |
29 | 29 | |
30 | 30 | 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 | |
32 | 32 | begin |
33 | 33 | expected = {"structured_fact" => {"foo" => {"nested" => "value1"}, "bar" => "value2", "baz" => "value3", "true" => true, "false" => false}} |
34 | 34 | assert_equal(expected, JSON.parse(stdout.chomp), "JSON output does not match expected output") |
41 | 41 | external_fact_script_yaml = File.join(external_dir, "#{external_filename}.yaml") |
42 | 42 | create_remote_file(agent, external_fact_script_yaml, '') |
43 | 43 | |
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| | |
45 | 45 | assert_match(/#{external_filename}#{ext}/, facter_output.stdout, "external facts script files should be listed as cacheable") |
46 | 46 | assert_match(/#{external_filename}.txt/, facter_output.stdout, "external facts txt files should be listed as cacheable") |
47 | 47 | assert_match(/#{external_filename}.json/, facter_output.stdout, "external facts json files should be listed as cacheable") |
54 | 54 | assert_no_match(/caching/, facter_output.stderr, "facter should not have tried to refresh the cache") |
55 | 55 | end |
56 | 56 | |
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") | |
60 | 59 | end |
61 | 60 | end |
62 | 61 | end |
5 | 5 | external_dir = agent.puppet['pluginfactdest'] |
6 | 6 | external_file = File.join(external_dir, "external.txt") |
7 | 7 | custom_dir = File.join(agent.puppet['plugindest'], "facter") |
8 | custom_file = "#{custom_dir}/custom.rb" | |
8 | custom_file = File.join(custom_dir, 'custom.rb') | |
9 | 9 | |
10 | 10 | teardown do |
11 | 11 | agent.rm_rf(external_file) |
6 | 6 | extend Facter::Acceptance::UserFactUtils |
7 | 7 | |
8 | 8 | content = <<EOM |
9 | Facter.add('custom_fact') do | |
9 | Facter.add('custom_fact_trace') do | |
10 | 10 | setcode do |
11 | 11 | non_existent_value |
12 | 12 | end |
26 | 26 | end |
27 | 27 | |
28 | 28 | 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 | |
30 | 30 | assert_match(/backtrace:\s+#{custom_fact}/, stderr, "Expected a backtrace for erroneous custom fact") |
31 | 31 | end |
32 | 32 | end |
28 | 28 | end |
29 | 29 | |
30 | 30 | 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 | |
32 | 32 | begin |
33 | 33 | expected = {"structured_fact" => {"foo" => {"nested" => "value1"}, "bar" => "value2", "baz" => "value3" }} |
34 | 34 | assert_equal(expected, YAML.load(stdout), "YAML output does not match expected output") |