From 88b64f93a4eb39cbb831197dfe6922229c65b470 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 24 Jan 2014 18:13:37 -0800 Subject: [PATCH] commands/box: "update" command --- plugins/commands/box/command/root.rb | 5 + plugins/commands/box/command/update.rb | 26 ++++- templates/locales/en.yml | 2 +- .../commands/box/command/update_test.rb | 94 +++++++++++++++++++ 4 files changed, 121 insertions(+), 6 deletions(-) create mode 100644 test/unit/plugins/commands/box/command/update_test.rb diff --git a/plugins/commands/box/command/root.rb b/plugins/commands/box/command/root.rb index 96f599308..138a10de9 100644 --- a/plugins/commands/box/command/root.rb +++ b/plugins/commands/box/command/root.rb @@ -38,6 +38,11 @@ module VagrantPlugins require File.expand_path("../repackage", __FILE__) Repackage end + + @subcommands.register(:update) do + require_relative "update" + Update + end end def execute diff --git a/plugins/commands/box/command/update.rb b/plugins/commands/box/command/update.rb index a89ea4877..0db5b5776 100644 --- a/plugins/commands/box/command/update.rb +++ b/plugins/commands/box/command/update.rb @@ -29,11 +29,27 @@ module VagrantPlugins return if !argv with_target_vms(argv) do |machine| - @env.action_runner.run(Vagrant::Action.action_box_update, { - box_outdated_force: true, - box_outdated_refresh: true, - box_outdated_success_ui: true, - machine: machine, + if !machine.box + machine.ui.output(I18n.t( + "vagrant.errors.box_update_no_box", + name: machine.config.vm.box)) + next + end + + box = machine.box + update = box.has_update?(machine.config.vm.box_version) + if !update + machine.ui.success(I18n.t( + "vagrant.box_up_to_date_single", + name: box.name, version: box.version)) + next + end + + @env.action_runner.run(Vagrant::Action.action_box_add, { + box_url: box.metadata_url, + box_provider: update[2].name, + box_version: update[1].version, + ui: machine.ui, }) end end diff --git a/templates/locales/en.yml b/templates/locales/en.yml index eaf799045..9c52b26a5 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -51,7 +51,7 @@ en: box_up_to_date: |- * '%{name}' (v%{version}) is up to date box_up_to_date_single: |- - Your box '%{name}' (v%{version}) is running the latest version. + Box '%{name}' (v%{version}) is running the latest version. cfengine_bootstrapping: |- Bootstrapping CFEngine with policy server: %{policy_server}... cfengine_bootstrapping_policy_hub: |- diff --git a/test/unit/plugins/commands/box/command/update_test.rb b/test/unit/plugins/commands/box/command/update_test.rb new file mode 100644 index 000000000..3f2c5cd46 --- /dev/null +++ b/test/unit/plugins/commands/box/command/update_test.rb @@ -0,0 +1,94 @@ +require File.expand_path("../../../../../base", __FILE__) + +require Vagrant.source_root.join("plugins/commands/box/command/update") + +describe VagrantPlugins::CommandBox::Command::Update do + include_context "unit" + + let(:argv) { [] } + let(:iso_env) do + # We have to create a Vagrantfile so there is a root path + test_iso_env.vagrantfile("") + test_iso_env.create_vagrant_env + end + let(:test_iso_env) { isolated_environment } + + let(:action_runner) { double("action_runner") } + let(:machine) { iso_env.machine(iso_env.machine_names[0], :dummy) } + + subject { described_class.new(argv, iso_env) } + + before do + iso_env.stub(action_runner: action_runner) + end + + describe "execute" do + context "updating environment machines" do + before do + subject.stub(:with_target_vms) { |&block| block.call machine } + end + + let(:box) do + box_dir = test_iso_env.box3("foo", "1.0", :virtualbox) + box = Vagrant::Box.new( + "foo", :virtualbox, "1.0", box_dir, metadata_url: "foo") + box.stub(has_update?: nil) + box + end + + it "ignores machines without boxes" do + action_runner.should_receive(:run).never + + subject.execute + end + + it "doesn't update boxes if they're up-to-date" do + machine.stub(box: box) + box.should_receive(:has_update?). + with(machine.config.vm.box_version). + and_return(nil) + + action_runner.should_receive(:run).never + + subject.execute + end + + it "updates boxes if they have an update" do + md = Vagrant::BoxMetadata.new(StringIO.new(<<-RAW)) + { + "name": "foo", + "versions": [ + { + "version": "1.0" + }, + { + "version": "1.1", + "providers": [ + { + "name": "virtualbox", + "url": "bar" + } + ] + } + ] + } + RAW + + machine.stub(box: box) + box.should_receive(:has_update?). + with(machine.config.vm.box_version). + and_return([md, md.version("1.1"), md.version("1.1").provider("virtualbox")]) + + action_runner.should_receive(:run).with do |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 + end + + subject.execute + end + end + end +end