Skip to content

Commit

Permalink
(voxpupuli#78) Add tests for facter version
Browse files Browse the repository at this point in the history
Previously many factset files were in the wrong directory which made maintenance
and debugging difficult.  This commit adds tests to ensure that the factset
files are indeed JSON parsable and that the facter version in the file matches
the directory it is located in.

This test is maked as skipped until all of the bad factset files can be changed.
  • Loading branch information
glennsarti committed Mar 26, 2018
1 parent 48e0e7e commit cde701c
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions spec/facts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,34 @@
end
end

RSpec::Matchers.define :be_valid_json do
match do |actual|
content = File.open(actual, 'rb') { |file| file.read }
valid = false
begin
obj = JSON.parse(content)
valid = true
rescue JSON::ParserError => e
raise "Invalid JSON file #{actual}.\n#{e}"
end
end

failure_message do |actual|
"expected that fact file #{actual} was a valid JSON file."
end
end

RSpec::Matchers.define :have_facter_version do |expected_facter_version, filepath|
match do |actual|
# Simple but naive regex check
actual =~ /^#{expected_facter_version}($|\.)/
end

failure_message do |actual|
"expected that fact file #{filepath} with facter version #{actual} had a facter version that matched #{expected_facter_version}"
end
end

describe 'Default Facts' do
before(:each) do
ENV['FACTERDB_SKIP_DEFAULTDB'] = nil
Expand All @@ -31,5 +59,23 @@
expect(file_hashes[file_hash]).to have_a_unique_hash
end
end

it 'should contain fact sets which match are valid JSON' do
FacterDB.default_fact_files.each do |filepath|
expect(filepath).to be_valid_json
end
end

xit 'should contain fact sets which match the facter version' do
FacterDB.default_fact_files.each do |filepath|
facter_dir_path = File.basename(File.dirname(filepath))

content = File.open(filepath, 'rb') { |file| file.read }
obj = JSON.parse(content)
file_facter_version = obj['facterversion']

expect(file_facter_version).to have_facter_version(facter_dir_path, filepath)
end
end
end
end

0 comments on commit cde701c

Please sign in to comment.