Validate pushes in the global config

This commit is contained in:
Seth Vargo 2015-01-07 15:51:20 -05:00
parent c4eb0261bb
commit 39233e802f
5 changed files with 48 additions and 64 deletions

View File

@ -556,7 +556,6 @@ module Vagrant
end end
strategy, config = pushes[name] strategy, config = pushes[name]
push_registry = Vagrant.plugin("2").manager.pushes push_registry = Vagrant.plugin("2").manager.pushes
klass, _ = push_registry.get(strategy) klass, _ = push_registry.get(strategy)
if klass.nil? if klass.nil?
@ -565,11 +564,6 @@ module Vagrant
pushes: push_registry.keys pushes: push_registry.keys
end end
# Validate the global push configuration and our local push configuration
machine = self.machine(self.machine_names.first, self.default_provider)
machine.action_raw(:config_validate, Vagrant::Action::Builtin::ConfigValidate)
config.validate(machine)
klass.new(self, config).push klass.new(self, config).push
end end

View File

@ -19,6 +19,11 @@ module VagrantPlugins
name = validate_pushes!(@env.pushes, argv[0]) name = validate_pushes!(@env.pushes, argv[0])
# Validate the configuration
@env.machine(@env.machine_names.first, @env.default_provider).action_raw(
:config_validate,
Vagrant::Action::Builtin::ConfigValidate)
@logger.debug("'push' environment with strategy: `#{name}'") @logger.debug("'push' environment with strategy: `#{name}'")
@env.push(name) @env.push(name)

View File

@ -115,6 +115,22 @@ module VagrantPlugins
end end
end end
# Validate all pushes
def validate(machine)
errors = { "push" => _detected_errors }
__compiled_pushes.each do |_, push|
config = push[1]
push_errors = config.validate(machine)
if push_errors
errors = Vagrant::Config::V2::Util.merge_errors(errors, push_errors)
end
end
errors
end
# This returns the list of compiled pushes as a hash by name. # This returns the list of compiled pushes as a hash by name.
# #
# @return [Hash<Symbol, Array<Class, Object>>] # @return [Hash<Symbol, Array<Class, Object>>]

View File

@ -6,11 +6,14 @@ describe VagrantPlugins::CommandPush::Command do
include_context "unit" include_context "unit"
include_context "command plugin helpers" include_context "command plugin helpers"
let(:iso_env) { isolated_environment }
let(:env) do let(:env) do
isolated_environment.tap do |env| iso_env.vagrantfile(<<-VF)
env.vagrantfile("") Vagrant.configure("2") do |config|
env.create_vagrant_env config.vm.box = "hashicorp/precise64"
end end
VF
iso_env.create_vagrant_env
end end
let(:argv) { [] } let(:argv) { [] }
@ -39,6 +42,26 @@ describe VagrantPlugins::CommandPush::Command do
expect(env).to receive(:push).once expect(env).to receive(:push).once
subject.execute subject.execute
end end
it "validates the configuration" do
iso_env.vagrantfile <<-EOH
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/precise64"
config.push.define "noop" do |push|
push.bad = "ham"
end
end
EOH
subject = described_class.new(argv, iso_env.create_vagrant_env)
allow(subject).to receive(:validate_pushes!)
.and_return(:noop)
expect { subject.execute }.to raise_error(Vagrant::Errors::ConfigInvalid) { |err|
expect(err.message).to include("The following settings shouldn't exist: bad")
}
end
end end
describe "#validate_pushes!" do describe "#validate_pushes!" do

View File

@ -1005,7 +1005,6 @@ VF
environment = isolated_environment do |env| environment = isolated_environment do |env|
env.vagrantfile(<<-VF.gsub(/^ {10}/, '')) env.vagrantfile(<<-VF.gsub(/^ {10}/, ''))
Vagrant.configure("2") do |config| Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/precise64"
config.push.define "lolwatbacon" config.push.define "lolwatbacon"
end end
VF VF
@ -1016,58 +1015,6 @@ VF
.to raise_error(Vagrant::Errors::PushStrategyNotLoaded) .to raise_error(Vagrant::Errors::PushStrategyNotLoaded)
end end
it "runs global config validation" do
environment = isolated_environment do |env|
env.vagrantfile(<<-VF.gsub(/^ {10}/, ''))
Vagrant.configure("2") do |config|
config.push.define "noop"
end
VF
end
env = environment.create_vagrant_env
expect { env.push("noop") }
.to raise_error(Vagrant::Errors::ConfigInvalid)
end
it "runs push config validations" do
config_class = Class.new(Vagrant.plugin("2", :config)) do
def self.validated?
!!class_variable_get(:@@validated)
end
def validate(machine)
self.class.class_variable_set(:@@validated, true)
{}
end
end
register_plugin("2") do |plugin|
plugin.name "foo"
plugin.config(:foo, :push) do
config_class
end
plugin.push(:foo) do
push_class
end
end
environment = isolated_environment do |env|
env.vagrantfile(<<-VF.gsub(/^ {10}/, ''))
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/precise64"
config.push.define "foo"
end
VF
end
env = environment.create_vagrant_env
env.push("foo")
expect(config_class.validated?).to be(true)
end
it "executes the push action" do it "executes the push action" do
register_plugin("2") do |plugin| register_plugin("2") do |plugin|
plugin.name "foo" plugin.name "foo"
@ -1080,7 +1027,6 @@ VF
environment = isolated_environment do |env| environment = isolated_environment do |env|
env.vagrantfile(<<-VF.gsub(/^ {10}/, '')) env.vagrantfile(<<-VF.gsub(/^ {10}/, ''))
Vagrant.configure("2") do |config| Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/precise64"
config.push.define "foo" config.push.define "foo"
end end
VF VF