-
-
Notifications
You must be signed in to change notification settings - Fork 77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
More JSON factset tests #363
Draft
yakatz
wants to merge
1
commit into
voxpupuli:master
Choose a base branch
from
yakatz:json_tests
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
require 'digest' | ||
require 'pathname' | ||
require 'json_schemer' | ||
|
||
schema = { | ||
'$id': 'https://voxpupuli.org/facterdb/factset.schema.json', | ||
title: 'Factset', | ||
description: 'A saved puppet facter factset', | ||
|
||
'type' => 'object', | ||
|
||
properties: { | ||
aio_agent_version: { 'type' => 'string' }, | ||
os: { | ||
type: 'object', | ||
properties: { | ||
family: { enum: %w[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) | ||
begin | ||
JSON.parse(content) | ||
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 'Factset Schemas are Valid', type: :feature 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 be_empty | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you ask the facter devs if they provide any schema?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not really concerned about the validity of the schema overall, since most of them were generated by Facter (hopefully not modified by hand). I am looking for a way to enforce other changes that wouldn't be part of a facter json schema, for example, IP address always being the same, blanked out UUID values, etc. that would help make creating tests with factsets easier.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah yes, makes sense. I also asked for a schema, so we can properly validate factsets: puppetlabs/facter#2730