diff options
author | Bryan McLellan <btm@loftninjas.org> | 2020-03-17 22:42:55 -0400 |
---|---|---|
committer | Bryan McLellan <btm@loftninjas.org> | 2020-03-23 12:57:09 -0400 |
commit | f59c0443f43f6d8f0e7d95a030b34346c6cce482 (patch) | |
tree | 43bd05c9357660d36ace17fc2692ce117db91ad4 /spec/unit/recipe_spec.rb | |
parent | 85005840999a86453175d2345545bf8e8c22c3bc (diff) | |
download | chef-f59c0443f43f6d8f0e7d95a030b34346c6cce482.tar.gz |
Improve errors around loading YAML recipes
YAML is a bit terse. The wrong : or - makes it all fall apart. Be a bit
more helpful.
Signed-off-by: Bryan McLellan <btm@loftninjas.org>
Diffstat (limited to 'spec/unit/recipe_spec.rb')
-rw-r--r-- | spec/unit/recipe_spec.rb | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/spec/unit/recipe_spec.rb b/spec/unit/recipe_spec.rb index 099e6750f6..ef8fb78f90 100644 --- a/spec/unit/recipe_spec.rb +++ b/spec/unit/recipe_spec.rb @@ -576,6 +576,74 @@ describe Chef::Recipe do end end + describe "from_yaml_file" do + it "raises ArgumentError if the YAML file contains multiple documents" do + filename = "multiple_docs.yaml" + yaml = "---\n- resources:\n - type: false\n---\n-resources:\n - type: false\n" + allow(File).to receive(:file?).and_call_original + allow(File).to receive(:readable?).and_call_original + allow(IO).to receive(:read).and_call_original + allow(File).to receive(:file?).with(filename).and_return(true) + allow(File).to receive(:readable?).with(filename).and_return(true) + allow(IO).to receive(:read).with(filename).and_return(yaml) + expect { recipe.from_yaml_file(filename) }.to raise_error(ArgumentError, /contains multiple documents/) + end + + it "raises IOError if the file does not exist" do + filename = "/nonexistent" + allow(File).to receive(:file?).and_call_original + allow(File).to receive(:file?).with(filename).and_return(false) + expect { recipe.from_yaml_file(filename) }.to raise_error(IOError, /Cannot open or read/) + end + end + + describe "from_yaml" do + it "raises ArgumentError if the YAML is not a top-level hash" do + yaml = <<~YAML + --- + - one + - resources + - three + YAML + expect { recipe.from_yaml(yaml) }.to raise_error(ArgumentError, /must contain a top-level 'resources' hash/) + end + + it "raises ArgumentError if the YAML does not contain a resources hash" do + yaml = <<~YAML + --- + - airplanes: + - type: "execute" + command: "whoami" + YAML + expect { recipe.from_yaml(yaml) }.to raise_error(ArgumentError, /must contain a top-level 'resources' hash/) + end + + it "does not raise if the YAML contains a resources hash" do + yaml = <<~YAML + --- + resources: + - type: "execute" + command: "whoami" + YAML + expect(recipe).to receive(:from_hash).with({ "resources" => [{ "command" => "whoami", "type" => "execute" }] }) + recipe.from_yaml(yaml) + end + end + + describe "from_hash" do + it "declares resources from a hash" do + resources = { "resources" => [ + { "name" => "running some commands", "type" => "execute", "command" => "whoami" }, + { "name" => "preparing the bits", "type" => "service", "action" => "start", "service_name" => "bit_launcher" }, + ] } + + recipe.from_hash(resources) + expect(recipe.resources(execute: "running some commands").command).to eql("whoami") + expect(recipe.resources(service: "preparing the bits").service_name).to eql("bit_launcher") + expect(recipe.resources(service: "preparing the bits").action).to eql([:start]) + end + end + describe "included DSL" do it "should respond to :ps_credential from Chef::DSL::Powershell" do expect(recipe.respond_to?(:ps_credential)).to be true |