provisioners/ansible: add basic config validators

With this change, the `raw_arguments` and `raw_ssh_args` options are:
- STILL automatically converted as an Array when they are set a String
  (no behaviour change)
- rejected if they are not of Array data type otherwise

Additional Notes:
- the 'as_array' tiny helper has been removed since it was no longer
  used.
- there is for now no deeper validation (i.e. verifying that the Array
  elements are only *String* objects)
This commit is contained in:
Gilles Cornu 2016-04-20 22:27:55 +02:00
parent ab036ddd0b
commit b2286388f0
6 changed files with 65 additions and 6 deletions

View File

@ -113,8 +113,16 @@ module VagrantPlugins
end
if raw_arguments
@raw_arguments = Helpers::as_array(raw_arguments)
if raw_arguments.kind_of?(String)
@raw_arguments = [raw_arguments]
elsif !raw_arguments.kind_of?(Array)
@errors << I18n.t(
"vagrant.provisioners.ansible.errors.raw_arguments_invalid",
type: raw_arguments.class.to_s,
value: raw_arguments.to_s)
end
end
end
end
end

View File

@ -34,6 +34,17 @@ module VagrantPlugins
def validate(machine)
super
if raw_ssh_args
if raw_ssh_args.kind_of?(String)
@raw_ssh_args = [raw_ssh_args]
elsif !raw_ssh_args.kind_of?(Array)
@errors << I18n.t(
"vagrant.provisioners.ansible.errors.raw_ssh_args_invalid",
type: raw_ssh_args.class.to_s,
value: raw_ssh_args.to_s)
end
end
{ "ansible remote provisioner" => @errors }
end

View File

@ -12,10 +12,6 @@ module VagrantPlugins
def self.as_list_argument(v)
v.kind_of?(Array) ? v.join(',') : v
end
def self.as_array(v)
v.kind_of?(Array) ? v : [v]
end
end
end
end

View File

@ -243,7 +243,7 @@ module VagrantPlugins
ssh_options << "-o ForwardAgent=yes" if @ssh_info[:forward_agent]
# Unchecked SSH Parameters
ssh_options.concat(Helpers::as_array(config.raw_ssh_args)) if config.raw_ssh_args
ssh_options.concat(config.raw_ssh_args) if config.raw_ssh_args
# Re-enable ControlPersist Ansible defaults,
# which are lost when ANSIBLE_SSH_ARGS is defined.

View File

@ -2121,6 +2121,10 @@ en:
for more information.
extra_vars_invalid: |-
`extra_vars` must be a hash or a path to an existing file. Received: %{value} (as %{type})
raw_arguments_invalid: |-
`raw_arguments` must be an array of strings. Received: %{value} (as %{type})
raw_ssh_args_invalid: |-
`raw_ssh_args` must be an array of strings. Received: %{value} (as %{type})
galaxy_role_file_invalid: |-
`galaxy_role_file` does not exist on the %{system}: %{path}
inventory_path_invalid: |-

View File

@ -200,6 +200,46 @@ describe VagrantPlugins::Ansible::Config::Host do
])
end
it "returns an error if the raw_arguments is of the wrong data type" do
subject.raw_arguments = { arg1: 1, arg2: "foo" }
subject.finalize!
result = subject.validate(machine)
expect(result["ansible remote provisioner"]).to eql([
I18n.t("vagrant.provisioners.ansible.errors.raw_arguments_invalid",
type: subject.raw_arguments.class.to_s,
value: subject.raw_arguments.to_s)
])
end
it "converts a raw_arguments option defined as a String into an Array" do
subject.raw_arguments = "--foo=bar"
subject.finalize!
result = subject.validate(machine)
expect(subject.raw_arguments).to eql(%w(--foo=bar))
end
it "returns an error if the raw_ssh_args is of the wrong data type" do
subject.raw_ssh_args = { arg1: 1, arg2: "foo" }
subject.finalize!
result = subject.validate(machine)
expect(result["ansible remote provisioner"]).to eql([
I18n.t("vagrant.provisioners.ansible.errors.raw_ssh_args_invalid",
type: subject.raw_ssh_args.class.to_s,
value: subject.raw_ssh_args.to_s)
])
end
it "converts a raw_ssh_args option defined as a String into an Array" do
subject.raw_arguments = "-o ControlMaster=no"
subject.finalize!
result = subject.validate(machine)
expect(subject.raw_arguments).to eql(["-o ControlMaster=no"])
end
it "it collects and returns all detected errors" do
subject.playbook = non_existing_file
subject.inventory_path = non_existing_file