Sha256: 7ad88652132b8e47c6eb486d47606c2572149bb1f0ce8a6db1a15cfb55264d72

Contents?: true

Size: 1.63 KB

Versions: 16

Compression:

Stored size: 1.63 KB

Contents

# This test is intended to ensure that the --json command-line option works
# properly. This option causes Facter to output facts in JSON format.
# A custom fact is used to test for parity between Facter's output and
# the expected JSON output.
test_name "C99966, C98083: --json command-line option results in valid JSON output" do

  require 'json'
  require 'facter/acceptance/user_fact_utils'
  extend Facter::Acceptance::UserFactUtils

  content = <<EOM
Facter.add('structured_fact') do
  setcode do
    { "foo" => {"nested" => "value1"}, "bar" => "value2", "baz" => "value3", "true" => true, "false" => false }
  end
end
EOM

  agents.each do |agent|
    step "Agent #{agent}: create a structured custom fact" do
      custom_dir = get_user_fact_dir(agent['platform'], on(agent, facter('kernelmajversion')).stdout.chomp.to_f)
      custom_fact = File.join(custom_dir, 'custom_fact.rb')
      on(agent, "mkdir -p '#{custom_dir}'")
      create_remote_file(agent, custom_fact, content)
      on(agent, "chmod +x '#{custom_fact}'")

      teardown do
        on(agent, "rm -f '#{custom_fact}'")
      end

      step "Agent #{agent}: retrieve output using the --json option" do
        on(agent, facter("--custom-dir '#{custom_dir}' --json structured_fact")) do
          begin
            expected = JSON.pretty_generate({"structured_fact" => {"foo" => {"nested" => "value1"}, "bar" => "value2", "baz" => "value3", "true" => true, "false" => false}})
            assert_equal(expected, stdout.chomp, "JSON output does not match expected output")
          rescue
            fail_test "Couldn't parse output as JSON"
          end
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 2 rubygems

Version Path
facter-3.12.2.cfacter.20181217 ext/facter/facter/acceptance/tests/options/json.rb
facter-3.12.1.cfacter.20181031 ext/facter/facter/acceptance/tests/options/json.rb
facter-3.11.6.cfacter.20181031 ext/facter/facter/acceptance/tests/options/json.rb
facter-3.12.1.cfacter.20181023 ext/facter/facter/acceptance/tests/options/json.rb
facter-3.11.5.cfacter.20181022 ext/facter/facter/acceptance/tests/options/json.rb
facter-3.12.0.cfacter.20181004 ext/facter/facter/acceptance/tests/options/json.rb
facter-3.12.0.cfacter.20181001 ext/facter/facter/acceptance/tests/options/json.rb
facter-3.12.0.cfacter.20180918 ext/facter/facter/acceptance/tests/options/json.rb
facter-3.11.4.cfacter.20180821 ext/facter/facter/acceptance/tests/options/json.rb
facter-3.11.3.cfacter.20180716 ext/facter/facter/acceptance/tests/options/json.rb
facter-3.11.2.cfacter.20180612 ext/facter/facter/acceptance/tests/options/json.rb
facter-3.9.6.cfacter.20180612 ext/facter/facter/acceptance/tests/options/json.rb
facter-3.11.2.cfacter.20180606 ext/facter/facter/acceptance/tests/options/json.rb
facter-3.9.6.cfacter.20180606 ext/facter/facter/acceptance/tests/options/json.rb
facter-3.11.0.cfacter.20180319 ext/facter/facter/acceptance/tests/options/json.rb
cfacter-3.11.0.rc.20180314 ext/facter/facter/acceptance/tests/options/json.rb