Merge pull request #11295 from briancain/fixup/provision-with-name-shell

Fixes #11287: Set top level provisioner name if set in provisioner config
This commit is contained in:
Brian Cain 2020-01-07 09:18:19 -08:00 committed by GitHub
commit 3d1f1d2422
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 70 additions and 33 deletions

View File

@ -25,9 +25,26 @@ module Vagrant
# Store in the type map so that --provision-with works properly # Store in the type map so that --provision-with works properly
@_provisioner_types[result] = provisioner.type @_provisioner_types[result] = provisioner.type
# Set top level provisioner name to provisioner configs name if top level name not set.
# This is mostly for handling the shell provisioner, if a user has set its name like:
#
# config.vm.provision "shell", name: "my_provisioner"
#
# Where `name` is a shell config option, not a top level provisioner class option
#
# Note: `name` is set to a symbol, since it is converted to one via #Config::VM.provision
provisioner_name = provisioner.name
if !provisioner_name
if provisioner.config.name
provisioner_name = provisioner.config.name.to_sym
end
else
provisioner_name = provisioner_name.to_sym
end
# Build up the options # Build up the options
options = { options = {
name: provisioner.name, name: provisioner_name,
run: provisioner.run, run: provisioner.run,
before: provisioner.before, before: provisioner.before,
after: provisioner.after, after: provisioner.after,

View File

@ -9,7 +9,10 @@ module VagrantPlugins
# Unique name for this provisioner # Unique name for this provisioner
# #
# @return [String] # Accepts a string, but is ultimately forced into a symbol in the top level method inside
# #Config::VM.provision method while being parsed from a Vagrantfile
#
# @return [Symbol]
attr_reader :name attr_reader :name
# Internal unique name for this provisioner # Internal unique name for this provisioner

View File

@ -13,7 +13,7 @@ describe Vagrant::Action::Builtin::MixinProvisioners do
sandbox.create_vagrant_env sandbox.create_vagrant_env
end end
let(:provisioner_config){ {} } let(:provisioner_config){ double("provisioner_config", name: nil) }
let(:provisioner_one) do let(:provisioner_one) do
prov = VagrantPlugins::Kernel_V2::VagrantConfigProvisioner.new("spec-test", :shell) prov = VagrantPlugins::Kernel_V2::VagrantConfigProvisioner.new("spec-test", :shell)
prov.config = provisioner_config prov.config = provisioner_config
@ -24,8 +24,14 @@ describe Vagrant::Action::Builtin::MixinProvisioners do
prov.config = provisioner_config prov.config = provisioner_config
prov prov
end end
let(:provisioner_three) do
prov = VagrantPlugins::Kernel_V2::VagrantConfigProvisioner.new(nil, :shell)
provisioner_config = double("provisioner_config", name: "my_shell")
prov.config = provisioner_config
prov
end
let(:provisioner_instances) { [provisioner_one,provisioner_two] } let(:provisioner_instances) { [provisioner_one,provisioner_two,provisioner_three] }
let(:ui) { double("ui") } let(:ui) { double("ui") }
let(:vm) { double("vm", provisioners: provisioner_instances) } let(:vm) { double("vm", provisioners: provisioner_instances) }
@ -54,6 +60,17 @@ describe Vagrant::Action::Builtin::MixinProvisioners do
shell_two = result[1] shell_two = result[1]
expect(shell_two.first).to be_a(VagrantPlugins::Shell::Provisioner) expect(shell_two.first).to be_a(VagrantPlugins::Shell::Provisioner)
end end
it "returns all the instances of configured provisioners" do
result = subject.provisioner_instances(env)
expect(result.size).to eq(provisioner_instances.size)
shell_one = result.first
expect(shell_one[1][:name]).to eq(:"spec-test")
shell_two = result[1]
expect(shell_two[1][:name]).to eq(:"spec-test")
shell_three = result[2]
expect(shell_three[1][:name]).to eq(:"my_shell")
end
end end
context "#sort_provisioner_instances" do context "#sort_provisioner_instances" do
@ -91,9 +108,9 @@ describe Vagrant::Action::Builtin::MixinProvisioners do
it "returns the array in the correct order" do it "returns the array in the correct order" do
result = subject.provisioner_instances(env) result = subject.provisioner_instances(env)
expect(result[0].last[:name]).to eq("before-test") expect(result[0].last[:name]).to eq(:"before-test")
expect(result[1].last[:name]).to eq("root-test") expect(result[1].last[:name]).to eq(:"root-test")
expect(result[2].last[:name]).to eq("after-test") expect(result[2].last[:name]).to eq(:"after-test")
end end
end end
@ -121,10 +138,10 @@ describe Vagrant::Action::Builtin::MixinProvisioners do
it "puts the each shortcut provisioners in place" do it "puts the each shortcut provisioners in place" do
result = subject.provisioner_instances(env) result = subject.provisioner_instances(env)
expect(result[0].last[:name]).to eq("before-test") expect(result[0].last[:name]).to eq(:"before-test")
expect(result[1].last[:name]).to eq("root-test") expect(result[1].last[:name]).to eq(:"root-test")
expect(result[2].last[:name]).to eq("before-test") expect(result[2].last[:name]).to eq(:"before-test")
expect(result[3].last[:name]).to eq("root2-test") expect(result[3].last[:name]).to eq(:"root2-test")
end end
end end
@ -152,10 +169,10 @@ describe Vagrant::Action::Builtin::MixinProvisioners do
it "puts the each shortcut provisioners in place" do it "puts the each shortcut provisioners in place" do
result = subject.provisioner_instances(env) result = subject.provisioner_instances(env)
expect(result[0].last[:name]).to eq("root-test") expect(result[0].last[:name]).to eq(:"root-test")
expect(result[1].last[:name]).to eq("after-test") expect(result[1].last[:name]).to eq(:"after-test")
expect(result[2].last[:name]).to eq("root2-test") expect(result[2].last[:name]).to eq(:"root2-test")
expect(result[3].last[:name]).to eq("after-test") expect(result[3].last[:name]).to eq(:"after-test")
end end
end end
@ -189,12 +206,12 @@ describe Vagrant::Action::Builtin::MixinProvisioners do
it "puts the each shortcut provisioners in place" do it "puts the each shortcut provisioners in place" do
result = subject.provisioner_instances(env) result = subject.provisioner_instances(env)
expect(result[0].last[:name]).to eq("before-test") expect(result[0].last[:name]).to eq(:"before-test")
expect(result[1].last[:name]).to eq("root-test") expect(result[1].last[:name]).to eq(:"root-test")
expect(result[2].last[:name]).to eq("after-test") expect(result[2].last[:name]).to eq(:"after-test")
expect(result[3].last[:name]).to eq("before-test") expect(result[3].last[:name]).to eq(:"before-test")
expect(result[4].last[:name]).to eq("root2-test") expect(result[4].last[:name]).to eq(:"root2-test")
expect(result[5].last[:name]).to eq("after-test") expect(result[5].last[:name]).to eq(:"after-test")
end end
end end
@ -228,10 +245,10 @@ describe Vagrant::Action::Builtin::MixinProvisioners do
it "puts the each shortcut provisioners in place" do it "puts the each shortcut provisioners in place" do
result = subject.provisioner_instances(env) result = subject.provisioner_instances(env)
expect(result[0].last[:name]).to eq("before-test") expect(result[0].last[:name]).to eq(:"before-test")
expect(result[1].last[:name]).to eq("root-test") expect(result[1].last[:name]).to eq(:"root-test")
expect(result[2].last[:name]).to eq("root2-test") expect(result[2].last[:name]).to eq(:"root2-test")
expect(result[3].last[:name]).to eq("after-test") expect(result[3].last[:name]).to eq(:"after-test")
end end
end end
end end

View File

@ -71,7 +71,7 @@ describe Vagrant::Action::Builtin::Provision do
prov.config = provisioner_config prov.config = provisioner_config
prov prov
end end
let(:provisioner_config){ {} } let(:provisioner_config){ double("provisioner_config", name: "spec-test") }
before{ expect(vm_config).to receive(:provisioners).and_return([provisioner]) } before{ expect(vm_config).to receive(:provisioners).and_return([provisioner]) }
@ -107,13 +107,13 @@ describe Vagrant::Action::Builtin::Provision do
end end
it "should not run if provision types are set and provisioner is not included" do it "should not run if provision types are set and provisioner is not included" do
env[:provision_types] = ["other-provisioner", "other-test"] env[:provision_types] = [:"other-provisioner", :"other-test"]
expect(hook).not_to receive(:call).with(:provisioner_run, anything) expect(hook).not_to receive(:call).with(:provisioner_run, anything)
instance.call(env) instance.call(env)
end end
it "should run if provision types are set and include provisioner name" do it "should run if provision types are set and include provisioner name" do
env[:provision_types] = ["spec-test"] env[:provision_types] = [:"spec-test"]
expect(hook).to receive(:call).with(:provisioner_run, anything) expect(hook).to receive(:call).with(:provisioner_run, anything)
instance.call(env) instance.call(env)
end end
@ -142,13 +142,13 @@ describe Vagrant::Action::Builtin::Provision do
end end
it "should not run if provision types are set and provisioner is not included" do it "should not run if provision types are set and provisioner is not included" do
env[:provision_types] = ["other-provisioner", "other-test"] env[:provision_types] = [:"other-provisioner", :"other-test"]
expect(hook).not_to receive(:call).with(:provisioner_run, anything) expect(hook).not_to receive(:call).with(:provisioner_run, anything)
instance.call(env) instance.call(env)
end end
it "should run if provision types are set and include provisioner name" do it "should run if provision types are set and include provisioner name" do
env[:provision_types] = ["spec-test"] env[:provision_types] = [:"spec-test"]
expect(hook).to receive(:call).with(:provisioner_run, anything) expect(hook).to receive(:call).with(:provisioner_run, anything)
instance.call(env) instance.call(env)
end end
@ -177,13 +177,13 @@ describe Vagrant::Action::Builtin::Provision do
end end
it "should not run if provision types are set and provisioner is not included" do it "should not run if provision types are set and provisioner is not included" do
env[:provision_types] = ["other-provisioner", "other-test"] env[:provision_types] = [:"other-provisioner", :"other-test"]
expect(hook).not_to receive(:call).with(:provisioner_run, anything) expect(hook).not_to receive(:call).with(:provisioner_run, anything)
instance.call(env) instance.call(env)
end end
it "should run if provision types are set and include provisioner name" do it "should run if provision types are set and include provisioner name" do
env[:provision_types] = ["spec-test"] env[:provision_types] = [:"spec-test"]
expect(hook).to receive(:call).with(:provisioner_run, anything) expect(hook).to receive(:call).with(:provisioner_run, anything)
instance.call(env) instance.call(env)
end end
@ -192,7 +192,7 @@ describe Vagrant::Action::Builtin::Provision do
File.open(File.join(data_dir.to_s, "action_provision"), "w") do |file| File.open(File.join(data_dir.to_s, "action_provision"), "w") do |file|
file.write("1.5:machine-id") file.write("1.5:machine-id")
end end
env[:provision_types] = ["spec-test"] env[:provision_types] = [:"spec-test"]
expect(hook).to receive(:call).with(:provisioner_run, anything) expect(hook).to receive(:call).with(:provisioner_run, anything)
instance.call(env) instance.call(env)
end end