diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c996dcc4..d74421af6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## 0.6.4 (unreleased) + - Basic validation added for Chef configuration (both solo and server). - Top config class is now available in all `Vagrant::Config::Base` subclasses, which is useful for config validation. - Subcommand help shows proper full command in task listing. [GH-168] diff --git a/lib/vagrant/provisioners/chef.rb b/lib/vagrant/provisioners/chef.rb index b98e2018d..ed3f2ecb6 100644 --- a/lib/vagrant/provisioners/chef.rb +++ b/lib/vagrant/provisioners/chef.rb @@ -125,6 +125,24 @@ module Vagrant result.delete("json") result end + + def validate(errors) + if top.vm.provisioner == :chef_solo + # Validate chef solo settings + errors.add(I18n.t("vagrant.config.chef.cookbooks_path_empty")) if !cookbooks_path || [cookbooks_path].flatten.empty? + end + + if top.vm.provisioner == :chef_server + # Validate chef server settings + errors.add(I18n.t("vagrant.config.chef.server_url_empty")) if !chef_server_url || chef_server_url.strip == "" + errors.add(I18n.t("vagrant.config.chef.validation_key_path")) if !validation_key_path + end + + if [:chef_solo, :chef_server].include?(top.vm.provisioner) + # Validations shared by both chef solo and server + errors.add(I18n.t("vagrant.config.chef.run_list_empty")) if !run_list || run_list.empty? + end + end end end end diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 8b5e2a69a..e38f2331d 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -99,6 +99,11 @@ en: config: common: error_empty: `%{field}` must be filled in. + chef: + run_list_empty: "Run list must not be empty." + cookbooks_path_empty: "Must specify a cookbooks path for chef solo." + server_url_empty: "Chef server URL must be populated." + validation_key_path: "Validation key path must be valid path to your chef server validation key." ssh: private_key_missing: "`private_key_path` file must exist: %{path}" vm: diff --git a/test/vagrant/provisioners/chef_test.rb b/test/vagrant/provisioners/chef_test.rb index 1a218a31a..29a8ec6bf 100644 --- a/test/vagrant/provisioners/chef_test.rb +++ b/test/vagrant/provisioners/chef_test.rb @@ -21,7 +21,6 @@ class ChefProvisionerTest < Test::Unit::TestCase context "config" do setup do @config = Vagrant::Provisioners::Chef::ChefConfig.new - @config.run_list.clear end should "not include the 'json' key in the config dump" do @@ -44,6 +43,10 @@ class ChefProvisionerTest < Test::Unit::TestCase } end + should "have an empty run list to begin with" do + assert @config.run_list.empty? + end + should "add a recipe to the run list" do @config.add_recipe("foo") assert_equal "recipe[foo]", @config.run_list[0] @@ -63,6 +66,74 @@ class ChefProvisionerTest < Test::Unit::TestCase @config.add_role("role[user]") assert_equal "role[user]", @config.run_list[0] end + + context "validation" do + setup do + @errors = Vagrant::Config::ErrorRecorder.new + @top = @config.top = Vagrant::Config::Top.new(@env) + end + + context "chef solo" do + setup do + @top.vm.provisioner = :chef_solo + + @config.run_list = ["foo"] + @config.cookbooks_path = "cookbooks" + end + + should "be valid if provisioner is not chef solo" do + @top.vm.provisioner = nil + @config.validate(@errors) + assert @errors.errors.empty? + end + + should "be invalid if run list is empty" do + @config.run_list = [] + @config.validate(@errors) + assert !@errors.errors.empty? + end + + should "be invalid if cookbooks path is empty" do + @config.cookbooks_path = nil + @config.validate(@errors) + assert !@errors.errors.empty? + end + end + + context "chef server" do + setup do + @top.vm.provisioner = :chef_server + + @config.run_list = ["foo"] + @config.chef_server_url = "foo" + @config.validation_key_path = "foo" + end + + should "be valid if provisioner is not chef solo" do + @top.vm.provisioner = nil + @config.validate(@errors) + assert @errors.errors.empty? + end + + should "be invalid if run list is empty" do + @config.run_list = [] + @config.validate(@errors) + assert !@errors.errors.empty? + end + + should "be invalid if run list is empty" do + @config.chef_server_url = nil + @config.validate(@errors) + assert !@errors.errors.empty? + end + + should "be invalid if run list is empty" do + @config.validation_key_path = nil + @config.validate(@errors) + assert !@errors.errors.empty? + end + end + end end context "verifying binary" do @@ -77,6 +148,7 @@ class ChefProvisionerTest < Test::Unit::TestCase @action.verify_binary(binary) end end + context "permissions on provisioning folder" do should "create and chown the folder to the ssh user" do ssh_seq = sequence("ssh_seq")