Lookup latest available installed box if required on update

When performing a box update and the box version has been updated
to be different than the installed version, perform a lookup for
the latest available installed box to allow the update command to
continue successfully
This commit is contained in:
Chris Roberts 2018-08-02 16:12:10 -07:00
parent 03d8965ef7
commit 00b783a6a5
2 changed files with 37 additions and 4 deletions

View File

@ -100,10 +100,14 @@ module VagrantPlugins
end
if !machine.box
machine.ui.output(I18n.t(
"vagrant.errors.box_update_no_box",
name: machine.config.vm.box))
next
collection = Vagrant::BoxCollection.new(@env.boxes_path)
machine.box = collection.find(machine.config.vm.box, provider || @env.default_provider, "> 0")
if !machine.box
machine.ui.output(I18n.t(
"vagrant.errors.box_update_no_box",
name: machine.config.vm.box))
next
end
end
name = machine.box.name

View File

@ -307,6 +307,35 @@ describe VagrantPlugins::CommandBox::Command::Update do
subject.execute
end
context "when box version is updated but previous box exists" do
let(:collection) { double("collection") }
it "updates the box" do
# First call gets nil result to for lookup
expect(machine).to receive(:box).and_return(nil)
expect(Vagrant::BoxCollection).to receive(:new).and_return(collection)
expect(collection).to receive(:find).and_return(box)
expect(box).to receive(:has_update?).
with(machine.config.vm.box_version,
{download_options:
{ca_cert: nil, ca_path: nil, client_cert: nil,
insecure: false}}).
and_return([md, md.version("1.1"), md.version("1.1").provider("virtualbox")])
expect(action_runner).to receive(:run).with(any_args) { |action, opts|
expect(opts[:box_url]).to eq(box.metadata_url)
expect(opts[:box_provider]).to eq("virtualbox")
expect(opts[:box_version]).to eq("1.1")
expect(opts[:ui]).to equal(machine.ui)
true
}
subject.execute
end
end
context "machine has download options" do
before do
machine.config.vm.box_download_ca_cert = "oof"