summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAntony Deepak Thomas <antonydeepak@gmail.com>2021-09-19 22:47:20 -0700
committerAntony Deepak Thomas <antonydeepak@gmail.com>2021-09-20 14:20:55 -0700
commitc9b7a112f3f01bd0dd6536c7108f0fdf857ae7e1 (patch)
tree560026d4cc394230ce96b753fd2cd83092b83c3c
parent6d671b711a5b17d21cf46eb3f4f43bfce474e338 (diff)
downloadchef-c9b7a112f3f01bd0dd6536c7108f0fdf857ae7e1.tar.gz
Add Json verifier
This patch adds Json verifier to the list of built-in verifiers. Json verifier can be used with symbol ':json' on `file` like resources such as `file`, `template` etc. Signed-off-by: Antony Deepak Thomas <antonydeepak@gmail.com>
-rw-r--r--lib/chef/provider/file.rb1
-rw-r--r--lib/chef/resource/file/verification/json.rb52
-rw-r--r--spec/unit/resource/file/verification/json_spec.rb66
3 files changed, 119 insertions, 0 deletions
diff --git a/lib/chef/provider/file.rb b/lib/chef/provider/file.rb
index 8f77cc4641..4ac86a6c8a 100644
--- a/lib/chef/provider/file.rb
+++ b/lib/chef/provider/file.rb
@@ -27,6 +27,7 @@ require_relative "../scan_access_control"
require_relative "../mixin/checksum"
require_relative "../mixin/file_class"
require_relative "../mixin/enforce_ownership_and_permissions"
+require_relative "../resource/file/verification/json"
require_relative "../resource/file/verification/yaml"
require_relative "../util/backup"
require_relative "../util/diff"
diff --git a/lib/chef/resource/file/verification/json.rb b/lib/chef/resource/file/verification/json.rb
new file mode 100644
index 0000000000..c71e6a265e
--- /dev/null
+++ b/lib/chef/resource/file/verification/json.rb
@@ -0,0 +1,52 @@
+#
+# Author:: Antony Thomas (<antonydeepak@gmail.com>)
+# Copyright:: Copyright (c) Chef Software Inc.
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+require "json" unless defined?(JSON)
+
+class Chef
+ class Resource
+ class File
+ class Verification
+
+ #
+ # Extends File verification to provide Json verification
+ #
+ # Example:
+ # file 'foo.json' do
+ # content '{"foo": "bar"}'
+ # verify :json
+ # end
+ #
+ #
+
+ class Json < Chef::Resource::File::Verification
+
+ provides :json
+
+ def verify(path, opts = {})
+ JSON.parse(IO.read(path))
+ true
+ rescue JSON::ParserError => e
+ Chef::Log.error("Json syntax verify failed with : #{e.message}")
+ false
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/spec/unit/resource/file/verification/json_spec.rb b/spec/unit/resource/file/verification/json_spec.rb
new file mode 100644
index 0000000000..22a769054d
--- /dev/null
+++ b/spec/unit/resource/file/verification/json_spec.rb
@@ -0,0 +1,66 @@
+#
+# Author:: Antony Thomas (<antonydeepak@gmail.com>)
+# Copyright:: Copyright (c) Chef Software Inc.
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+require "spec_helper"
+
+describe Chef::Resource::File::Verification::Json do
+ let(:parent_resource) { Chef::Resource.new("llama") }
+
+ before(:all) do
+ @valid_json = "valid-#{Time.now.to_i}.json"
+ f = File.new(@valid_json, "w")
+ f.write('{
+ "foo": "bar"
+ }')
+ f.close
+
+ @invalid_json = "invalid-#{Time.now.to_i}.json"
+ f = File.new(@invalid_json, "w")
+ f.write("{
+ 'foo': 'bar'
+ }")
+ f.close
+
+ @empty_json = "empty-#{Time.now.to_i}.json"
+ File.new(@empty_json, "w").close
+ end
+
+ context "verify" do
+ it "returns true for valid json" do
+ v = Chef::Resource::File::Verification::Json.new(parent_resource, :json, {})
+ expect(v.verify(@valid_json)).to eq(true)
+ end
+
+ it "returns false for invalid json" do
+ v = Chef::Resource::File::Verification::Json.new(parent_resource, :json, {})
+ expect(v.verify(@invalid_json)).to eq(false)
+ end
+
+ it "returns false for empty file" do
+ # empty string is invalid per JSON spec https://stackoverflow.com/questions/30621802/why-does-json-parse-fail-with-the-empty-string
+ v = Chef::Resource::File::Verification::Json.new(parent_resource, :json, {})
+ expect(v.verify(@empty_json)).to eq(false)
+ end
+ end
+
+ after(:all) do
+ File.delete(@valid_json)
+ File.delete(@invalid_json)
+ File.delete(@empty_json)
+ end
+end