Add tests and docs

This commit is contained in:
sophia 2020-05-07 16:23:58 -04:00
parent 64f5a9e57f
commit 672859e296
5 changed files with 77 additions and 7 deletions

View File

@ -125,6 +125,10 @@ module VagrantPlugins
provisioner_names = provisioners.map { |i| i.name.to_s if i.name != name }.compact
if ![TrueClass, FalseClass].include?(@communicator_required.class)
errors << I18n.t("vagrant.provisioners.base.wrong_type", opt: "communicator_required", type: "boolean")
end
if @before && @after
errors << I18n.t("vagrant.provisioners.base.both_before_after_set")
end
@ -181,10 +185,6 @@ module VagrantPlugins
end
end
if !(@communicator_required.is_a?(TrueClass) || @communicator_required.is_a?(FalseClass))
errors << I18n.t("vagrant.provisioners.base.wrong_type", opt: "communicator_required", type: "boolean")
end
{"provisioner" => errors}
end

View File

@ -134,9 +134,9 @@ en:
container_ready: |-
Container started and ready for use!
not_provisioning: |-
The following provisioners require a communicator, though none is available (this container does not supprt SSH).
The following provisioners require a communicator, though none is available (this container does not support SSH).
Not running the following provisioners:
- %{provisioiners}
- %{provisioners}
status:
host_state_unknown: |-

View File

@ -399,15 +399,18 @@ describe VagrantPlugins::Kernel_V2::VMConfig do
it "stores the provisioners" do
subject.provision("shell", inline: "foo")
subject.provision("shell", inline: "bar", run: "always") { |s| s.path = "baz" }
subject.provision("shell", inline: "foo", communicator_required: false)
subject.finalize!
r = subject.provisioners
expect(r.length).to eql(2)
expect(r.length).to eql(3)
expect(r[0].run).to be_nil
expect(r[0].config.inline).to eql("foo")
expect(r[1].config.inline).to eql("bar")
expect(r[1].config.path).to eql("baz")
expect(r[1].run).to eql(:always)
expect(r[1].communicator_required).to eql(true)
expect(r[2].communicator_required).to eql(false)
end
it "allows provisioner settings to be overridden" do

View File

@ -0,0 +1,62 @@
require_relative "../../../../base"
require_relative "../../../../../../plugins/providers/docker/action/has_provisioner"
describe VagrantPlugins::DockerProvider::Action::HasProvisioner do
include_context "unit"
let(:sandbox) { isolated_environment }
let(:iso_env) do
# We have to create a Vagrantfile so there is a root path
sandbox.vagrantfile("")
sandbox.create_vagrant_env
end
let(:provisioner_one) { double("provisioner_one") }
let(:provisioner_two) { double("provisioner_two") }
let(:provisioners) { [provisioner_one, provisioner_two] }
let(:machine) do
iso_env.machine(iso_env.machine_names[0], :docker).tap do |m|
allow(m).to receive_message_chain(:config, :vm, :provisioners).and_return(provisioners)
end
end
let(:env) {{ machine: machine, ui: machine.ui, root_path: Pathname.new(".") }}
let(:app) { lambda { |*args| }}
subject { described_class.new(app, env) }
after do
sandbox.close
end
describe "#call" do
before do
allow(provisioner_one).to receive(:communicator_required).and_return(true)
allow(provisioner_two).to receive(:communicator_required).and_return(false)
end
it "does not skip any provisioners if provider has ssh" do
env[:machine].provider_config.has_ssh = true
expect(provisioner_one).to_not receive(:communicator_required)
expect(provisioner_two).to_not receive(:communicator_required)
subject.call(env)
expect(env[:skip]).to eq([])
end
it "skips provisioners that require a communicator if provider does not have ssh" do
env[:machine].provider_config.has_ssh = false
expect(provisioner_one).to receive(:communicator_required)
expect(provisioner_two).to receive(:communicator_required)
expect(provisioner_one).to receive(:run=).with(:never)
subject.call(env)
expect(env[:skip]).to eq([provisioner_one])
end
end
end

View File

@ -35,6 +35,11 @@ option is what type a provisioner is:
every root provisioner, or before all provisioners respectively.
**Note**: This option is currently experimental, so it needs to be explicitly
enabled to work. More info can be found [here](/docs/experimental).
* `communicator_required` (boolean) - Specifies the machine must be accessible by
Vagrant in order to run the provisioner. If set to true, the provisioner will
only run if Vagrant can establish communication with the guest. If set to false
the provisioner will run regardless of Vagrant's ability to communicate with the
guest. Defaults to true.
More information about how to use `before` and `after` options can be read [below](#dependency-provisioners).