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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
|
require File.expand_path("../../../../base", __FILE__)
require Vagrant.source_root.join("plugins/commands/upload/command")
describe VagrantPlugins::CommandUpload::Command do
include_context "unit"
include_context "virtualbox"
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(:guest) { double("guest", capability_host_chain: guest_chain) }
let(:host) { double("host", capability_host_chain: host_chain) }
let(:machine) { iso_env.machine(iso_env.machine_names[0], :dummy) }
let(:communicator) { double("communicator") }
let(:host_chain){ [[]] }
let(:guest_chain){ [[]] }
let(:argv) { [] }
let(:config) {
double("config")
}
subject { described_class.new(argv, iso_env) }
before do
allow(machine).to receive(:communicate).and_return(communicator)
allow(machine).to receive(:config).and_return(config)
allow(subject).to receive(:with_target_vms)
end
it "should raise invalid usage error by default" do
expect { subject.execute }.to raise_error(Vagrant::Errors::CLIInvalidUsage)
end
context "when three arguments are provided" do
let(:argv) { ["source", "destination", "guest"] }
before { allow(File).to receive(:file?).and_return(true) }
it "should use third argument as name of guest" do
expect(subject).to receive(:with_target_vms).with("guest", any_args)
subject.execute
end
it "should use first argument as source and second as destination" do
allow(subject).to receive(:with_target_vms) { |&block| block.call machine }
expect(communicator).to receive(:upload).with("source", "destination")
subject.execute
end
end
context "when two arguments are provided" do
let(:argv) { ["source", "ambiguous"] }
let(:active_machines) { [] }
before do
allow(File).to receive(:file?).and_return(true)
allow(iso_env).to receive(:active_machines).and_return(active_machines)
end
it "should use the the second argument as destination when not a machine name" do
allow(subject).to receive(:with_target_vms) { |&block| block.call machine }
expect(communicator).to receive(:upload).with("source", "ambiguous")
subject.execute
end
context "when active machine matches second argument" do
let(:active_machines) { [["ambiguous"]] }
it "should use second argument as guest name and generate destination" do
allow(subject).to receive(:with_target_vms).with("ambiguous", any_args) { |&block| block.call machine }
expect(communicator).to receive(:upload).with("source", "source")
subject.execute
end
end
end
context "when single argument is provided" do
let(:argv) { ["item"] }
before do
allow(File).to receive(:file?)
allow(File).to receive(:directory?)
end
it "should check if source is a file" do
expect(File).to receive(:file?).with("item").and_return(true)
subject.execute
end
it "should check if source is a directory" do
expect(File).to receive(:directory?).with("item").and_return(true)
subject.execute
end
it "should raise error if source is not a directory or file" do
expect { subject.execute }.to raise_error(Vagrant::Errors::UploadSourceMissing)
end
context "when source path ends with double quote" do
let(:argv) { [".\\item\""] }
it "should remove trailing quote" do
expect(File).to receive(:file?).with(".\\item").and_return(true)
subject.execute
end
end
context "when source path ends with single quote" do
let(:argv) { ['.\item\''] }
it "should remove trailing quote" do
expect(File).to receive(:file?).with(".\\item").and_return(true)
subject.execute
end
end
context "when source is a directory" do
before do
allow(File).to receive(:file?).with("item").and_return(false)
allow(File).to receive(:directory?).with("item").and_return(true)
allow(communicator).to receive(:upload)
allow(subject).to receive(:with_target_vms) { |&block| block.call machine }
end
it "should upload the directory" do
expect(communicator).to receive(:upload).with(/item/, anything)
subject.execute
end
it "should append separator and dot to source path for upload" do
expect(communicator).to receive(:upload).with("item/.", anything)
subject.execute
end
end
context "when source is a file" do
before do
allow(File).to receive(:file?).with("item").and_return(true)
allow(communicator).to receive(:upload)
allow(subject).to receive(:with_target_vms) { |&block| block.call machine }
allow(machine).to receive(:guest).and_return(guest)
allow(machine).to receive(:env).and_return(double("env", host: host))
allow(guest).to receive(:capability?).and_return(true)
allow(guest).to receive(:capability)
end
it "should upload the file" do
expect(communicator).to receive(:upload).with("item", anything)
subject.execute
end
it "should name destination after the source" do
expect(communicator).to receive(:upload).with("item", "item")
subject.execute
end
context "when temporary option is set" do
before { argv << "-t" }
it "should get temporary path for destination from guest" do
expect(guest).to receive(:capability).with(:create_tmp_path, any_args).and_return("TMP_PATH")
expect(communicator).to receive(:upload).with("item", "TMP_PATH")
subject.execute
end
end
context "when compress option is set" do
before do
argv << "-c"
allow(guest).to receive(:capability).with(:create_tmp_path, any_args).and_return("TMP")
allow(subject).to receive(:compress_source_zip).and_return("COMPRESS_SOURCE")
allow(subject).to receive(:compress_source_tgz).and_return("COMPRESS_SOURCE")
allow(FileUtils).to receive(:rm).with("COMPRESS_SOURCE")
end
it "should check for guest decompression" do
expect(guest).to receive(:capability?).with(:decompress_tgz).and_return(true)
subject.execute
end
it "should compress the source" do
expect(subject).to receive(:compress_source_tgz).with("item").and_return("COMPRESS_SOURCE")
subject.execute
end
it "should cleanup compressed source" do
expect(FileUtils).to receive(:rm).with("COMPRESS_SOURCE")
subject.execute
end
it "should upload the compressed source" do
expect(communicator).to receive(:upload).with("COMPRESS_SOURCE", anything)
subject.execute
end
it "should upload compressed source to temporary location" do
expect(communicator).to receive(:upload).with("COMPRESS_SOURCE", "TMP")
subject.execute
end
it "should have guest decompress file" do
expect(guest).to receive(:capability).with(:decompress_tgz, "TMP", any_args)
subject.execute
end
it "should provide destination for guest decompression of file" do
expect(guest).to receive(:capability).with(:decompress_tgz, "TMP", "item", any_args)
subject.execute
end
it "should provide the destination type for guest decompression" do
expect(guest).to receive(:capability).with(:decompress_tgz, "TMP", "item", hash_including(type: :file))
subject.execute
end
context "with compression type set to zip" do
before { argv << "-C" << "zip" }
it "should test guest for decompression capability" do
expect(guest).to receive(:capability?).with(:decompress_zip).and_return(true)
subject.execute
end
it "should compress source using zip" do
expect(subject).to receive(:compress_source_zip)
subject.execute
end
it "should have guest decompress file using zip" do
expect(guest).to receive(:capability).with(:decompress_zip, any_args)
subject.execute
end
end
end
end
end
end
|