Skip to content

Commit

Permalink
More JSON factset tests
Browse files Browse the repository at this point in the history
- Test for known IP address
- Test out json-schema testing
  • Loading branch information
yakatz committed May 30, 2024
1 parent 9c33730 commit f1bc9b0
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ gemspec

gem 'facter', ENV.fetch('FACTER_GEM_VERSION', nil), require: false

group :test do
gem 'json_schemer'
end

group :development do
gem 'faraday-retry', require: false
gem 'github_changelog_generator', '>= 1.16.4', require: false
Expand Down
94 changes: 94 additions & 0 deletions spec/facts_schema_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
require 'spec_helper'

Check failure on line 1 in spec/facts_schema_spec.rb

View workflow job for this annotation

GitHub Actions / rubocop

Style/FrozenStringLiteralComment: Missing frozen string literal comment.
require 'digest'
require 'pathname'
require 'json_schemer'

schema = {
"$id": "https://voxpupuli.org/facterdb/factset.schema.json",

Check failure on line 7 in spec/facts_schema_spec.rb

View workflow job for this annotation

GitHub Actions / rubocop

Style/QuotedSymbols: Prefer single-quoted symbols when you don't need string interpolation or special symbols.

Check failure on line 7 in spec/facts_schema_spec.rb

View workflow job for this annotation

GitHub Actions / rubocop

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. (https://rubystyle.guide#consistent-string-literals)
"title": "Factset",

Check failure on line 8 in spec/facts_schema_spec.rb

View workflow job for this annotation

GitHub Actions / rubocop

Lint/SymbolConversion: Unnecessary symbol conversion; use `title:` instead.

Check failure on line 8 in spec/facts_schema_spec.rb

View workflow job for this annotation

GitHub Actions / rubocop

Style/QuotedSymbols: Prefer single-quoted symbols when you don't need string interpolation or special symbols.

Check failure on line 8 in spec/facts_schema_spec.rb

View workflow job for this annotation

GitHub Actions / rubocop

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. (https://rubystyle.guide#consistent-string-literals)
"description": "A saved puppet facter factset",

Check failure on line 9 in spec/facts_schema_spec.rb

View workflow job for this annotation

GitHub Actions / rubocop

Lint/SymbolConversion: Unnecessary symbol conversion; use `description:` instead.

Check failure on line 9 in spec/facts_schema_spec.rb

View workflow job for this annotation

GitHub Actions / rubocop

Style/QuotedSymbols: Prefer single-quoted symbols when you don't need string interpolation or special symbols.

Check failure on line 9 in spec/facts_schema_spec.rb

View workflow job for this annotation

GitHub Actions / rubocop

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. (https://rubystyle.guide#consistent-string-literals)

"type" => "object",

Check failure on line 11 in spec/facts_schema_spec.rb

View workflow job for this annotation

GitHub Actions / rubocop

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. (https://rubystyle.guide#consistent-string-literals)

"properties": {
"aio_agent_version": {"type" => "string"},
"os": {
"type": "object",
"properties": {
"family": { "enum": ["RedHat", "Debian", "Gentoo", "windows", "OpenBSD", "Archlinux", "Suse", "FreeBSD", "Darwin", "Solaris",]}
}
}
},

# "required": ["_meta_generation_date"],

"allOf": [
{
'if': { 'properties': { "os": { "properties": { "family": { "enum": ["Debian"] } } } } },
'then': { 'required': ["aio_agent_version"] },
# 'else': { 'required': ["aio_agent_version"] }
},
]
}

schemer = JSONSchemer.schema(schema)
RSpec::Matchers.define :be_valid_json do
match do |actual|
content = File.binread(actual)
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 'Validate Schemas' do
before do
ENV['FACTERDB_SKIP_DEFAULTDB'] = nil
FacterDB.instance_variable_set(:@database, nil)
end

project_dir = Pathname.new(__dir__).parent
FacterDB.default_fact_files.each do |filepath|
relative_path = Pathname.new(filepath).relative_path_from(project_dir).to_s
describe relative_path do
subject(:content) do
JSON.parse(File.binread(filepath))
end

it 'contains a valid JSON document' do
expect(filepath).to be_valid_json
end

it 'contains a fact set which matches the facter version' do
facter_dir_path = File.basename(File.dirname(filepath))

expect(content['facterversion']).to have_facter_version(facter_dir_path, filepath)
end

it 'matches the schema' do
valid_json = schemer.validate(content).to_a
expect(valid_json).to match_array([])
end

end
end
end
2 changes: 1 addition & 1 deletion spec/facts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@

it 'contains newer networking facts hash' do
if Gem::Version.new(content['facterversion']) >= Gem::Version.new('3.0.0')
expect(content['networking']['ip']).to not_be_nil.and not_be_empty
expect(content['networking']['ip']).to eq('10.0.2.15')
expect(content['networking']['ip6']).to not_be_nil.and not_be_empty
expect(content['networking']['hostname']).to eq('foo')
expect(content['networking']['domain']).to eq('example.com')
Expand Down

0 comments on commit f1bc9b0

Please sign in to comment.