-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from github/kpaulisse-json-filter
Add JSON equivalence filter
- Loading branch information
Showing
23 changed files
with
347 additions
and
3 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
1.0.0 | ||
1.0.1 |
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
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,38 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../filter' | ||
|
||
require 'json' | ||
|
||
module OctocatalogDiff | ||
module CatalogDiff | ||
class Filter | ||
# Filter based on equivalence of JSON objects for file resources with named extensions. | ||
class JSON < OctocatalogDiff::CatalogDiff::Filter | ||
# Public: Actually do the comparison of JSON objects for appropriate resources. | ||
# Return true if the JSON objects are known to be equivalent. Return false if they | ||
# are not equivalent, or if equivalence cannot be determined. | ||
# | ||
# @param diff_in [OctocatalogDiff::API::V1::Diff] Difference | ||
# @param _options [Hash] Additional options (there are none for this filter) | ||
# @return [Boolean] true if this difference is a JSON file with identical objects, false otherwise | ||
def filtered?(diff, _options = {}) | ||
# Skip additions or removals - focus only on changes | ||
return false unless diff.change? | ||
|
||
# Make sure we are comparing file content for a file ending in .json extension | ||
return false unless diff.type == 'File' && diff.structure == %w(parameters content) | ||
return false unless diff.title =~ /\.json\z/i | ||
|
||
# Attempt to convert the old value and new value into JSON objects. Assuming | ||
# that doesn't error out, the return value is whether or not they're equal. | ||
obj_old = ::JSON.parse(diff.old_value) | ||
obj_new = ::JSON.parse(diff.new_value) | ||
obj_old == obj_new | ||
rescue # Rescue everything - if something failed, we aren't sure what's going on, so we'll return false. | ||
false | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
:backends: | ||
- yaml | ||
:yaml: | ||
:datadir: /var/lib/puppet/environments/%{::environment}/hieradata | ||
:hierarchy: | ||
- roles/%{::role} | ||
- common | ||
:merge_behavior: deeper | ||
:logger: console |
2 changes: 2 additions & 0 deletions
2
spec/octocatalog-diff/fixtures/repos/json-diff/hieradata/common.yaml
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,2 @@ | ||
--- | ||
bar::parameter: default |
2 changes: 2 additions & 0 deletions
2
spec/octocatalog-diff/fixtures/repos/json-diff/hieradata/roles/bar.yaml
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,2 @@ | ||
--- | ||
bar::parameter: bar |
3 changes: 3 additions & 0 deletions
3
spec/octocatalog-diff/fixtures/repos/json-diff/manifests/site.pp
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,3 @@ | ||
node default { | ||
include bar | ||
} |
1 change: 1 addition & 0 deletions
1
spec/octocatalog-diff/fixtures/repos/json-diff/modules/bar/files/different-json.bar
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 @@ | ||
{"value": "bar"} |
1 change: 1 addition & 0 deletions
1
spec/octocatalog-diff/fixtures/repos/json-diff/modules/bar/files/different-json.default
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 @@ | ||
{"value": "default"} |
1 change: 1 addition & 0 deletions
1
spec/octocatalog-diff/fixtures/repos/json-diff/modules/bar/files/identical-json
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 @@ | ||
{"value": "identical"} |
3 changes: 3 additions & 0 deletions
3
spec/octocatalog-diff/fixtures/repos/json-diff/modules/bar/files/not-json.bar
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,3 @@ | ||
--- | ||
title: "This is not JSON" | ||
value: "bar" |
3 changes: 3 additions & 0 deletions
3
spec/octocatalog-diff/fixtures/repos/json-diff/modules/bar/files/not-json.default
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,3 @@ | ||
--- | ||
title: "This is not JSON" | ||
value: "default" |
10 changes: 10 additions & 0 deletions
10
spec/octocatalog-diff/fixtures/repos/json-diff/modules/bar/files/similar-json.bar.json
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,10 @@ | ||
{ | ||
"structure": { | ||
"value": "bar", | ||
"array": [ | ||
"foo", | ||
"bar", | ||
"baz" | ||
] | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
spec/octocatalog-diff/fixtures/repos/json-diff/modules/bar/files/similar-json.default.json
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 @@ | ||
{"structure":{"value":"bar","array":["foo","bar","baz"]}} |
65 changes: 65 additions & 0 deletions
65
spec/octocatalog-diff/fixtures/repos/json-diff/modules/bar/manifests/init.pp
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,65 @@ | ||
class bar ( $parameter ) { | ||
file { '/tmp/template/different-json.json': | ||
content => template('bar/different-json.erb') | ||
} | ||
|
||
file { "/tmp/template/different-json-${parameter}.json": | ||
content => template('bar/different-json.erb') | ||
} | ||
|
||
file { '/tmp/template/identical-json.json': | ||
content => template('bar/identical-json.erb') | ||
} | ||
|
||
file { '/tmp/template/not-json.json': | ||
content => '{ "title": "This is not YAML" }' | ||
} | ||
|
||
file { '/tmp/template/not-json-2.json': | ||
content => template('bar/not-json.erb') | ||
} | ||
|
||
file { "/tmp/template/not-json-${parameter}.json": | ||
content => template('bar/not-json.erb') | ||
} | ||
|
||
file { '/tmp/template/similar-json.yaml': | ||
content => template('bar/similar-json.erb') | ||
} | ||
|
||
file { '/tmp/template/similar-json.json': | ||
content => template('bar/similar-json.erb') | ||
} | ||
|
||
file { '/tmp/static/different-json.json': | ||
source => "puppet:///modules/bar/different-json.${parameter}" | ||
} | ||
|
||
file { "/tmp/static/different-json-${parameter}.json": | ||
source => "puppet:///modules/bar/different-json.${parameter}" | ||
} | ||
|
||
file { '/tmp/static/identical-json.json': | ||
source => 'puppet:///modules/bar/identical-json', | ||
} | ||
|
||
file { '/tmp/static/not-json.json': | ||
source => 'puppet:///modules/bar/not-json.default', | ||
} | ||
|
||
file { '/tmp/static/not-json-2.json': | ||
source => "puppet:///modules/bar/not-json.${parameter}", | ||
} | ||
|
||
file { "/tmp/static/not-json-${parameter}.json": | ||
source => "puppet:///modules/bar/not-json.${parameter}", | ||
} | ||
|
||
file { '/tmp/static/similar-json.yaml': | ||
source => "puppet:///modules/bar/similar-json.${parameter}.json" | ||
} | ||
|
||
file { '/tmp/static/similar-json.json': | ||
source => "puppet:///modules/bar/similar-json.${parameter}.json" | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
spec/octocatalog-diff/fixtures/repos/json-diff/modules/bar/templates/different-json.erb
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 @@ | ||
{ "value": "<%= @parameter %>" } |
1 change: 1 addition & 0 deletions
1
spec/octocatalog-diff/fixtures/repos/json-diff/modules/bar/templates/identical-json.erb
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 @@ | ||
{ "value": "nachos" } |
3 changes: 3 additions & 0 deletions
3
spec/octocatalog-diff/fixtures/repos/json-diff/modules/bar/templates/not-json.erb
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,3 @@ | ||
--- | ||
title: "This is not JSON" | ||
value: "<%= @parameter %>" |
14 changes: 14 additions & 0 deletions
14
spec/octocatalog-diff/fixtures/repos/json-diff/modules/bar/templates/similar-json.erb
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,14 @@ | ||
<% if @parameter == 'default' %> | ||
{ | ||
"structure": { | ||
"value": "bar", | ||
"array": [ | ||
"foo", | ||
"bar", | ||
"baz" | ||
] | ||
} | ||
} | ||
<% else %> | ||
{"structure":{"value":"bar","array":["foo","bar","baz"]}} | ||
<% 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'integration_helper' | ||
require 'json' | ||
|
||
describe 'JSON file suppression for identical diffs' do | ||
context 'with JSON diff suppression disabled' do | ||
before(:all) do | ||
argv = ['-n', 'rspec-node.github.net', '--to-fact-override', 'role=bar'] | ||
hash = { hiera_config: 'hiera.yaml', spec_fact_file: 'facts.yaml', spec_repo: 'json-diff' } | ||
@result = OctocatalogDiff::Integration.integration(hash.merge(argv: argv)) | ||
end | ||
|
||
it 'should compile without error' do | ||
expect(@result.exitcode).to eq(2), OctocatalogDiff::Integration.format_exception(@result) | ||
expect(@result.exception).to be_nil | ||
end | ||
|
||
it 'should contain the correct number of diffs' do | ||
expect(@result.diffs.size).to eq(16) | ||
end | ||
|
||
it 'should contain the "similar JSON" static file as a diff' do | ||
resource = { diff_type: '~', type: 'File', title: '/tmp/static/similar-json.json', structure: %w(parameters content) } | ||
expect(OctocatalogDiff::Spec.diff_match?(@result.diffs, resource)).to eq(true) | ||
end | ||
|
||
it 'should contain the "similar JSON" static file as a diff' do | ||
resource = { diff_type: '~', type: 'File', title: '/tmp/static/similar-json.json', structure: %w(parameters content) } | ||
expect(OctocatalogDiff::Spec.diff_match?(@result.diffs, resource)).to eq(true) | ||
end | ||
|
||
it 'should contain the "similar JSON" template file as a diff' do | ||
resource = { diff_type: '~', type: 'File', title: '/tmp/template/similar-json.json', structure: %w(parameters content) } | ||
expect(OctocatalogDiff::Spec.diff_match?(@result.diffs, resource)).to eq(true) | ||
end | ||
|
||
it 'should contain the "similar JSON" template file as a diff' do | ||
resource = { diff_type: '~', type: 'File', title: '/tmp/template/similar-json.json', structure: %w(parameters content) } | ||
expect(OctocatalogDiff::Spec.diff_match?(@result.diffs, resource)).to eq(true) | ||
end | ||
end | ||
|
||
context 'with JSON diff suppression enabled' do | ||
before(:all) do | ||
argv = ['-n', 'rspec-node.github.net', '--to-fact-override', 'role=bar', '--filters', 'JSON'] | ||
hash = { hiera_config: 'hiera.yaml', spec_fact_file: 'facts.yaml', spec_repo: 'json-diff' } | ||
@result = OctocatalogDiff::Integration.integration(hash.merge(argv: argv)) | ||
end | ||
|
||
it 'should compile without error' do | ||
expect(@result.exitcode).to eq(2), OctocatalogDiff::Integration.format_exception(@result) | ||
expect(@result.exception).to be_nil | ||
end | ||
|
||
it 'should contain the correct number of diffs' do | ||
expect(@result.diffs.size).to eq(14) | ||
end | ||
|
||
it 'should not contain the "similar JSON" static file as a diff' do | ||
resource = { diff_type: '~', type: 'File', title: '/tmp/static/similar-json.json', structure: %w(parameters content) } | ||
expect(OctocatalogDiff::Spec.diff_match?(@result.diffs, resource)).to eq(false) | ||
end | ||
|
||
it 'should contain the "similar JSON" YAML static file as a diff' do | ||
resource = { diff_type: '~', type: 'File', title: '/tmp/static/similar-json.yaml', structure: %w(parameters content) } | ||
expect(OctocatalogDiff::Spec.diff_match?(@result.diffs, resource)).to eq(true) | ||
end | ||
|
||
it 'should not contain the "similar JSON" template file as a diff' do | ||
resource = { diff_type: '~', type: 'File', title: '/tmp/template/similar-json.json', structure: %w(parameters content) } | ||
expect(OctocatalogDiff::Spec.diff_match?(@result.diffs, resource)).to eq(false) | ||
end | ||
|
||
it 'should contain the "similar JSON" YAML template file as a diff' do | ||
resource = { diff_type: '~', type: 'File', title: '/tmp/template/similar-json.yaml', structure: %w(parameters content) } | ||
expect(OctocatalogDiff::Spec.diff_match?(@result.diffs, resource)).to eq(true) | ||
end | ||
end | ||
end |
Oops, something went wrong.