1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
require_relative "../../../base"
require_relative "../../../../../plugins/commands/package/command"
RSpec::Matchers.define :a_machine_named do |name|
match{ |actual| actual.name.to_s == name.to_s }
end
RSpec::Matchers.define :an_existing_directory do
match{ |actual| File.directory?(actual) }
end
describe VagrantPlugins::CommandPackage::Command do
include_context "unit"
let(:argv) { [] }
let(:iso_env) do
# We have to create a Vagrantfile so there is a root path
env = isolated_environment
env.vagrantfile("")
env.create_vagrant_env
end
let(:package_command) { described_class.new(argv, iso_env) }
let(:action_runner) { double("action_runner") }
before do
allow(iso_env).to receive(:action_runner).and_return(action_runner)
end
describe "#execute" do
context "with no arguments" do
it "packages default machine" do
expect(package_command).to receive(:package_vm).with(a_machine_named('default'), {})
package_command.execute
end
end
context "with single argument" do
context "set to default" do
let(:argv){ ['default'] }
it "packages default machine" do
expect(package_command).to receive(:package_vm).with(a_machine_named('default'), {})
package_command.execute
end
end
context "set to undefined vm" do
let(:argv){ ['undefined'] }
it "raises machine not found error" do
expect{ package_command.execute }.to raise_error(Vagrant::Errors::MachineNotFound)
end
end
context "with --output option" do
let(:argv){ ['--output', 'package-output-folder/default'] }
it "packages default machine inside specified folder" do
expect(package_command).to receive(:package_vm).with(
a_machine_named('default'), { output: "package-output-folder/default" }
)
package_command.execute
end
end
end
context "with multiple arguments" do
let(:argv){ ['default', 'undefined'] }
it "ignores the extra arguments" do
expect(package_command).to receive(:package_vm).with(a_machine_named('default'), {})
package_command.execute
end
end
context "with --base option" do
context "and no option value" do
let(:argv){ ['--base'] }
it "shows help" do
expect{ package_command.execute }.to raise_error(Vagrant::Errors::CLIInvalidOptions)
end
end
context "and option value" do
let(:argv){ ['--base', 'machine-id'] }
it "packages vm defined within virtualbox" do
expect(package_command).to receive(:package_base).with({ base: 'machine-id' })
package_command.execute
end
it "provides a machine data directory" do
expect(Vagrant::Machine).to receive(:new).with(
'machine-id', :virtualbox, anything, nil, anything, anything, an_existing_directory,
anything, anything, anything, anything).and_return(double("vm", name: "machine-id"))
allow(package_command).to receive(:package_vm)
package_command.execute
end
end
end
end
describe "#package_vm" do
context "calling the package action" do
let(:options) { {output: "test.box"} }
let(:expected_options) { {"package.output"=>"test.box"} }
let(:machine) { double("machine") }
let(:tmp_dir) { "/home/user/.vagrant.d/tmp/vagrant-package" }
let(:env) { {"export.temp_dir"=>tmp_dir} }
it "ensures that the package tmp dir is cleaned up" do
allow(FileUtils).to receive(:rm_rf).and_return(true)
allow(machine).to receive(:action).with(:package, expected_options).
and_return(env)
expect(FileUtils).to receive(:rm_rf).with(tmp_dir)
package_command.send(:package_vm, machine, options)
end
end
end
end
|