From 9c56061fa9c9c411145fb732a53f3bc9dea292d1 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Thu, 30 Oct 2014 11:42:25 -0400 Subject: [PATCH] Add tests for Chef Client config --- .../chef/config/chef_client_test.rb | 136 ++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 test/unit/plugins/provisioners/chef/config/chef_client_test.rb diff --git a/test/unit/plugins/provisioners/chef/config/chef_client_test.rb b/test/unit/plugins/provisioners/chef/config/chef_client_test.rb new file mode 100644 index 000000000..17f2dc321 --- /dev/null +++ b/test/unit/plugins/provisioners/chef/config/chef_client_test.rb @@ -0,0 +1,136 @@ +require_relative "../../../../base" + +require Vagrant.source_root.join("plugins/provisioners/chef/config/chef_client") + +describe VagrantPlugins::Chef::Config::ChefClient do + include_context "unit" + + subject { described_class.new } + + let(:machine) { double("machine") } + + describe "#chef_server_url" do + it "defaults to nil" do + subject.finalize! + expect(subject.chef_server_url).to be(nil) + end + end + + describe "#client_key_path" do + it "defaults to /etc/chef/client.pem" do + subject.finalize! + expect(subject.client_key_path).to eq("/etc/chef/client.pem") + end + end + + describe "#delete_client" do + it "defaults to false" do + subject.finalize! + expect(subject.delete_client).to be(false) + end + end + + describe "#delete_node" do + it "defaults to false" do + subject.finalize! + expect(subject.delete_node).to be(false) + end + end + + describe "#validation_key_path" do + it "defaults to nil" do + subject.finalize! + expect(subject.validation_key_path).to be(nil) + end + end + + describe "#validation_client_name" do + it "defaults to chef-validator" do + subject.finalize! + expect(subject.validation_client_name).to eq("chef-validator") + end + end + + describe "#validate" do + before do + allow(machine).to receive(:env) + .and_return(double("env", + root_path: "", + )) + + subject.chef_server_url = "https://example.com" + subject.validation_key_path = "/path/to/key.pem" + end + + let(:result) { subject.validate(machine) } + let(:errors) { result["chef client provisioner"] } + + context "when the chef_server_url is nil" do + it "returns an error" do + subject.chef_server_url = nil + subject.finalize! + expect(errors).to eq([I18n.t("vagrant.config.chef.server_url_empty")]) + end + end + + context "when the chef_server_url is blank" do + it "returns an error" do + subject.chef_server_url = " " + subject.finalize! + expect(errors).to eq([I18n.t("vagrant.config.chef.server_url_empty")]) + end + end + + context "when the validation_key_path is nil" do + it "returns an error" do + subject.validation_key_path = nil + subject.finalize! + expect(errors).to eq([I18n.t("vagrant.config.chef.validation_key_path")]) + end + end + + context "when the validation_key_path is blank" do + it "returns an error" do + subject.validation_key_path = " " + subject.finalize! + expect(errors).to eq([I18n.t("vagrant.config.chef.validation_key_path")]) + end + end + + context "when #delete_client is given" do + before { subject.delete_client = true } + + context "when knife does not exist" do + before do + allow(Vagrant::Util::Which) + .to receive(:which) + .with("knife") + .and_return(nil) + end + + it "returns an error" do + subject.finalize! + expect(errors).to eq([I18n.t("vagrant.chef_config_knife_not_found")]) + end + end + end + + context "when #delete_node is given" do + before { subject.delete_node = true } + + context "when knife does not exist" do + before do + allow(Vagrant::Util::Which) + .to receive(:which) + .with("knife") + .and_return(nil) + end + + it "returns an error" do + subject.finalize! + expect(errors).to eq([I18n.t("vagrant.chef_config_knife_not_found")]) + end + end + end + end +end