Basic validation added for Chef configuration (both solo and server).

This commit is contained in:
Mitchell Hashimoto 2010-09-30 01:09:36 -07:00
parent 465392f99c
commit ed645417c7
4 changed files with 97 additions and 1 deletions

View File

@ -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]

View File

@ -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

View File

@ -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:

View File

@ -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")