Update behavior of `snapshot restore` and `snapshot pop`

Both of these commands failed to default the options disabling
the provisioning from ignoring the sentinel file. This resulted
in different behavior than what was seen with the `up` and
`resume` commands which would only provision items with run set
to "always". This defaults the options to proper match the behavior
of `up` and `resume` to be consistent.

This also adds an extra `--no-start` flag to allow users to restore
a snapshot but not start the restored guest immediately.

Fixes #6752
This commit is contained in:
Chris Roberts 2018-12-07 16:17:44 -08:00
parent f7757b58d9
commit c999e7c3d4
5 changed files with 84 additions and 1 deletions

View File

@ -17,6 +17,9 @@ module VagrantPlugins
def execute def execute
options = {} options = {}
options[:snapshot_delete] = true options[:snapshot_delete] = true
options[:provision_ignore_sentinel] = false
options[:snapshot_start] = true
opts = OptionParser.new do |o| opts = OptionParser.new do |o|
o.banner = "Usage: vagrant snapshot pop [options] [vm-name]" o.banner = "Usage: vagrant snapshot pop [options] [vm-name]"
o.separator "" o.separator ""
@ -26,6 +29,9 @@ module VagrantPlugins
o.on("--no-delete", "Don't delete the snapshot after the restore") do o.on("--no-delete", "Don't delete the snapshot after the restore") do
options[:snapshot_delete] = false options[:snapshot_delete] = false
end end
o.on("--no-start", "Don't start the snapshot after the restore") do
options[:snapshot_start] = false
end
end end
# Parse the options # Parse the options

View File

@ -13,12 +13,18 @@ module VagrantPlugins
def execute def execute
options = {} options = {}
options[:provision_ignore_sentinel] = false
options[:snapshot_start] = true
opts = OptionParser.new do |o| opts = OptionParser.new do |o|
o.banner = "Usage: vagrant snapshot restore [options] [vm-name] <name>" o.banner = "Usage: vagrant snapshot restore [options] [vm-name] <name>"
o.separator "" o.separator ""
build_start_options(o, options) build_start_options(o, options)
o.separator "Restore a snapshot taken previously with snapshot save." o.separator "Restore a snapshot taken previously with snapshot save."
o.on("--no-start", "Don't start the snapshot after the restore") do
options[:snapshot_start] = false
end
end end
# Parse the options # Parse the options

View File

@ -267,7 +267,11 @@ module VagrantPlugins
end end
end end
b2.use action_start b2.use Call, IsEnvSet, :snapshot_start do |env2, b3|
if env2[:result]
b3.use action_start
end
end
end end
end end
end end

View File

@ -25,6 +25,12 @@ describe VagrantPlugins::CommandSnapshot::Command::Pop do
end end
describe "execute" do describe "execute" do
before do
machine.id = "mach"
allow(machine.provider).to receive(:capability).
with(:snapshot_list).and_return(["push_2_0"])
end
it "calls snapshot_restore with the last pushed snapshot" do it "calls snapshot_restore with the last pushed snapshot" do
machine.id = "foo" machine.id = "foo"
@ -48,5 +54,33 @@ describe VagrantPlugins::CommandSnapshot::Command::Pop do
expect(machine).to_not receive(:action) expect(machine).to_not receive(:action)
expect(subject.execute).to eq(0) expect(subject.execute).to eq(0)
end end
it "should disable ignoring sentinel file for provisioning" do
expect(machine).to receive(:action) do |name, opts|
expect(name).to eq(:snapshot_restore)
expect(opts[:provision_ignore_sentinel]).to eq(false)
end
subject.execute
end
it "should start the snapshot" do
expect(machine).to receive(:action) do |name, opts|
expect(name).to eq(:snapshot_restore)
expect(opts[:snapshot_start]).to eq(true)
end
subject.execute
end
context "when --no-start flag is provided" do
let(:argv) { ["--no-start"] }
it "should not start the snapshot" do
expect(machine).to receive(:action) do |name, opts|
expect(name).to eq(:snapshot_restore)
expect(opts[:snapshot_start]).to eq(false)
end
subject.execute
end
end
end end
end end

View File

@ -12,6 +12,7 @@ describe VagrantPlugins::CommandSnapshot::Command::Restore do
env.create_vagrant_env env.create_vagrant_env
end end
let(:snapshot_name) { "snapshot_name" }
let(:guest) { double("guest") } let(:guest) { double("guest") }
let(:host) { double("host") } let(:host) { double("host") }
let(:machine) { iso_env.machine(iso_env.machine_names[0], :dummy) } let(:machine) { iso_env.machine(iso_env.machine_names[0], :dummy) }
@ -31,6 +32,11 @@ describe VagrantPlugins::CommandSnapshot::Command::Restore do
end end
describe "execute" do describe "execute" do
before do
allow(machine.provider).to receive(:capability).
with(:snapshot_list).and_return([snapshot_name])
end
context "with no arguments" do context "with no arguments" do
it "shows help" do it "shows help" do
expect { subject.execute }. expect { subject.execute }.
@ -93,5 +99,32 @@ describe VagrantPlugins::CommandSnapshot::Command::Restore do
to raise_error(Vagrant::Errors::SnapshotNotFound) to raise_error(Vagrant::Errors::SnapshotNotFound)
end end
end end
it "should disable ignoring sentinel file for provisioning" do
argv << snapshot_name
expect(machine).to receive(:action) do |_, opts|
expect(opts[:provision_ignore_sentinel]).to eq(false)
end
subject.execute
end
it "should start the snapshot" do
argv << snapshot_name
expect(machine).to receive(:action) do |_, opts|
expect(opts[:snapshot_start]).to eq(true)
end
subject.execute
end
context "when --no-start flag is provided" do
let(:argv) { [snapshot_name, "--no-start"] }
it "should not start the snapshot" do
expect(machine).to receive(:action) do |_, opts|
expect(opts[:snapshot_start]).to eq(false)
end
subject.execute
end
end
end end
end end