diff options
author | Antony Deepak Thomas <antonydeepak@gmail.com> | 2021-09-19 22:47:20 -0700 |
---|---|---|
committer | Antony Deepak Thomas <antonydeepak@gmail.com> | 2021-09-20 14:20:55 -0700 |
commit | c9b7a112f3f01bd0dd6536c7108f0fdf857ae7e1 (patch) | |
tree | 560026d4cc394230ce96b753fd2cd83092b83c3c /spec/unit/resource | |
parent | 6d671b711a5b17d21cf46eb3f4f43bfce474e338 (diff) | |
download | chef-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>
Diffstat (limited to 'spec/unit/resource')
-rw-r--r-- | spec/unit/resource/file/verification/json_spec.rb | 66 |
1 files changed, 66 insertions, 0 deletions
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 |