2014-01-13 19:01:50 +00:00
|
|
|
require_relative "../../../base"
|
|
|
|
|
2014-01-15 19:30:32 +00:00
|
|
|
require "vagrant/util/platform"
|
|
|
|
|
2014-01-13 19:01:50 +00:00
|
|
|
require Vagrant.source_root.join("plugins/synced_folders/rsync/helper")
|
|
|
|
|
|
|
|
describe VagrantPlugins::SyncedFolderRSync::RsyncHelper do
|
|
|
|
include_context "unit"
|
|
|
|
|
|
|
|
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") }
|
|
|
|
let(:machine) { iso_env.machine(iso_env.machine_names[0], :dummy) }
|
|
|
|
|
|
|
|
subject { described_class }
|
|
|
|
|
|
|
|
before do
|
|
|
|
machine.stub(guest: guest)
|
2014-01-16 05:13:08 +00:00
|
|
|
|
|
|
|
# Don't do all the crazy Cygwin stuff
|
2014-03-14 15:02:07 +00:00
|
|
|
allow(Vagrant::Util::Platform).to receive(:cygwin_path) do |path, **opts|
|
2014-01-16 05:13:08 +00:00
|
|
|
path
|
|
|
|
end
|
2014-01-13 19:01:50 +00:00
|
|
|
end
|
|
|
|
|
2014-03-13 02:40:18 +00:00
|
|
|
describe "#exclude_to_regexp" do
|
|
|
|
let(:path) { "/foo/bar" }
|
|
|
|
|
|
|
|
it "converts a directory match" do
|
|
|
|
expect(described_class.exclude_to_regexp(path, "foo/")).
|
|
|
|
to eq(/^#{Regexp.escape(path)}\/.*foo\//)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "converts the start anchor" do
|
|
|
|
expect(described_class.exclude_to_regexp(path, "/foo")).
|
|
|
|
to eq(/^\/foo\/bar\/foo/)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "converts the **" do
|
|
|
|
expect(described_class.exclude_to_regexp(path, "fo**o")).
|
|
|
|
to eq(/^#{Regexp.escape(path)}\/.*fo.*o/)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "converts the *" do
|
|
|
|
expect(described_class.exclude_to_regexp(path, "fo*o")).
|
|
|
|
to eq(/^#{Regexp.escape(path)}\/.*fo[^\/]*o/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-13 19:01:50 +00:00
|
|
|
describe "#rsync_single" do
|
|
|
|
let(:result) { Vagrant::Util::Subprocess::Result.new(0, "", "") }
|
|
|
|
|
|
|
|
let(:ssh_info) {{
|
2016-08-11 20:07:25 +00:00
|
|
|
private_key_path: [],
|
2014-01-13 19:01:50 +00:00
|
|
|
}}
|
2014-01-13 19:34:49 +00:00
|
|
|
let(:opts) {{
|
|
|
|
hostpath: "/foo",
|
|
|
|
}}
|
2014-01-13 19:01:50 +00:00
|
|
|
let(:ui) { machine.ui }
|
|
|
|
|
|
|
|
before do
|
|
|
|
Vagrant::Util::Subprocess.stub(execute: result)
|
|
|
|
|
|
|
|
guest.stub(capability?: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't raise an error if it succeeds" do
|
|
|
|
subject.rsync_single(machine, ssh_info, opts)
|
|
|
|
end
|
|
|
|
|
2014-03-10 01:29:45 +00:00
|
|
|
it "doesn't call cygwin_path on non-Windows" do
|
2014-01-16 05:13:08 +00:00
|
|
|
Vagrant::Util::Platform.stub(windows?: false)
|
2014-03-14 15:02:07 +00:00
|
|
|
expect(Vagrant::Util::Platform).not_to receive(:cygwin_path)
|
2014-01-16 05:13:08 +00:00
|
|
|
subject.rsync_single(machine, ssh_info, opts)
|
|
|
|
end
|
|
|
|
|
2014-03-10 01:29:45 +00:00
|
|
|
it "calls cygwin_path on Windows" do
|
2014-01-16 05:13:08 +00:00
|
|
|
Vagrant::Util::Platform.stub(windows?: true)
|
2014-03-14 15:02:07 +00:00
|
|
|
expect(Vagrant::Util::Platform).to receive(:cygwin_path).and_return("foo")
|
2014-01-16 05:13:08 +00:00
|
|
|
|
2014-03-14 15:02:07 +00:00
|
|
|
expect(Vagrant::Util::Subprocess).to receive(:execute).with { |*args|
|
2014-01-16 05:30:47 +00:00
|
|
|
expect(args[args.length - 3]).to eql("foo/")
|
2014-03-14 15:02:07 +00:00
|
|
|
}
|
2014-01-16 05:13:08 +00:00
|
|
|
|
|
|
|
subject.rsync_single(machine, ssh_info, opts)
|
|
|
|
end
|
|
|
|
|
2014-01-13 19:01:50 +00:00
|
|
|
it "raises an error if the exit code is non-zero" do
|
|
|
|
Vagrant::Util::Subprocess.stub(
|
|
|
|
execute: Vagrant::Util::Subprocess::Result.new(1, "", ""))
|
|
|
|
|
|
|
|
expect {subject.rsync_single(machine, ssh_info, opts) }.
|
|
|
|
to raise_error(Vagrant::Errors::RSyncError)
|
|
|
|
end
|
|
|
|
|
2014-01-13 19:34:49 +00:00
|
|
|
context "host and guest paths" do
|
|
|
|
it "syncs the hostpath to the guest path" do
|
|
|
|
opts[:hostpath] = "/foo"
|
|
|
|
opts[:guestpath] = "/bar"
|
|
|
|
|
2014-03-14 15:02:07 +00:00
|
|
|
expect(Vagrant::Util::Subprocess).to receive(:execute).with { |*args|
|
2014-01-16 05:30:47 +00:00
|
|
|
expected = Vagrant::Util::Platform.fs_real_path("/foo").to_s
|
|
|
|
expect(args[args.length - 3]).to eql("#{expected}/")
|
2014-01-13 19:34:49 +00:00
|
|
|
expect(args[args.length - 2]).to include("/bar")
|
2014-03-14 15:02:07 +00:00
|
|
|
}
|
2014-01-13 19:34:49 +00:00
|
|
|
|
|
|
|
subject.rsync_single(machine, ssh_info, opts)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "expands the hostpath relative to the root path" do
|
|
|
|
opts[:hostpath] = "foo"
|
|
|
|
opts[:guestpath] = "/bar"
|
|
|
|
|
|
|
|
hostpath_expanded = File.expand_path(opts[:hostpath], machine.env.root_path)
|
|
|
|
|
2014-03-14 15:02:07 +00:00
|
|
|
expect(Vagrant::Util::Subprocess).to receive(:execute).with { |*args|
|
2014-01-16 05:30:47 +00:00
|
|
|
expect(args[args.length - 3]).to eql("#{hostpath_expanded}/")
|
2014-01-13 19:34:49 +00:00
|
|
|
expect(args[args.length - 2]).to include("/bar")
|
2014-03-14 15:02:07 +00:00
|
|
|
}
|
2014-01-13 19:34:49 +00:00
|
|
|
|
|
|
|
subject.rsync_single(machine, ssh_info, opts)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-13 19:01:50 +00:00
|
|
|
it "executes within the root path" do
|
2014-03-14 15:02:07 +00:00
|
|
|
expect(Vagrant::Util::Subprocess).to receive(:execute).with { |*args|
|
2014-01-13 19:01:50 +00:00
|
|
|
expect(args.last).to be_kind_of(Hash)
|
|
|
|
|
|
|
|
opts = args.last
|
|
|
|
expect(opts[:workdir]).to eql(machine.env.root_path.to_s)
|
2014-03-14 15:02:07 +00:00
|
|
|
}
|
2014-01-13 19:01:50 +00:00
|
|
|
|
|
|
|
subject.rsync_single(machine, ssh_info, opts)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "executes the rsync_pre capability first if it exists" do
|
2014-03-14 15:02:07 +00:00
|
|
|
expect(guest).to receive(:capability?).with(:rsync_pre).and_return(true)
|
|
|
|
expect(guest).to receive(:capability).with(:rsync_pre, opts).ordered
|
|
|
|
expect(Vagrant::Util::Subprocess).to receive(:execute).ordered.and_return(result)
|
2014-01-13 19:01:50 +00:00
|
|
|
|
|
|
|
subject.rsync_single(machine, ssh_info, opts)
|
|
|
|
end
|
|
|
|
|
2014-03-13 01:43:59 +00:00
|
|
|
it "executes the rsync_post capability after if it exists" do
|
2014-03-14 15:02:07 +00:00
|
|
|
expect(guest).to receive(:capability?).with(:rsync_post).and_return(true)
|
|
|
|
expect(Vagrant::Util::Subprocess).to receive(:execute).ordered.and_return(result)
|
|
|
|
expect(guest).to receive(:capability).with(:rsync_post, opts).ordered
|
2014-03-13 01:43:59 +00:00
|
|
|
|
|
|
|
subject.rsync_single(machine, ssh_info, opts)
|
|
|
|
end
|
|
|
|
|
2014-01-13 19:01:50 +00:00
|
|
|
context "excluding files" do
|
|
|
|
it "excludes files if given as a string" do
|
|
|
|
opts[:exclude] = "foo"
|
|
|
|
|
2014-03-14 15:02:07 +00:00
|
|
|
expect(Vagrant::Util::Subprocess).to receive(:execute).with { |*args|
|
2014-01-13 19:01:50 +00:00
|
|
|
index = args.find_index("foo")
|
|
|
|
expect(index).to be > 0
|
|
|
|
expect(args[index-1]).to eql("--exclude")
|
2014-03-14 15:02:07 +00:00
|
|
|
}
|
2014-01-13 19:01:50 +00:00
|
|
|
|
|
|
|
subject.rsync_single(machine, ssh_info, opts)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "excludes multiple files" do
|
|
|
|
opts[:exclude] = ["foo", "bar"]
|
|
|
|
|
2014-03-14 15:02:07 +00:00
|
|
|
expect(Vagrant::Util::Subprocess).to receive(:execute).with { |*args|
|
2014-01-13 19:01:50 +00:00
|
|
|
index = args.find_index("foo")
|
|
|
|
expect(index).to be > 0
|
|
|
|
expect(args[index-1]).to eql("--exclude")
|
|
|
|
|
|
|
|
index = args.find_index("bar")
|
|
|
|
expect(index).to be > 0
|
|
|
|
expect(args[index-1]).to eql("--exclude")
|
2014-03-14 15:02:07 +00:00
|
|
|
}
|
2014-01-13 19:01:50 +00:00
|
|
|
|
|
|
|
subject.rsync_single(machine, ssh_info, opts)
|
|
|
|
end
|
|
|
|
end
|
2014-03-06 19:27:58 +00:00
|
|
|
|
|
|
|
context "custom arguments" do
|
|
|
|
it "uses the default arguments if not given" do
|
2014-03-14 15:02:07 +00:00
|
|
|
expect(Vagrant::Util::Subprocess).to receive(:execute).with { |*args|
|
2014-03-06 19:27:58 +00:00
|
|
|
expect(args[1]).to eq("--verbose")
|
|
|
|
expect(args[2]).to eq("--archive")
|
|
|
|
expect(args[3]).to eq("--delete")
|
|
|
|
|
|
|
|
expected = Vagrant::Util::Platform.fs_real_path("/foo").to_s
|
|
|
|
expect(args[args.length - 3]).to eql("#{expected}/")
|
2014-03-14 15:02:07 +00:00
|
|
|
}
|
2014-03-06 19:27:58 +00:00
|
|
|
|
|
|
|
subject.rsync_single(machine, ssh_info, opts)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "uses the custom arguments if given" do
|
|
|
|
opts[:args] = ["--verbose", "-z"]
|
|
|
|
|
2014-03-14 15:02:07 +00:00
|
|
|
expect(Vagrant::Util::Subprocess).to receive(:execute).with { |*args|
|
2014-03-06 19:27:58 +00:00
|
|
|
expect(args[1]).to eq("--verbose")
|
|
|
|
expect(args[2]).to eq("-z")
|
|
|
|
|
|
|
|
expected = Vagrant::Util::Platform.fs_real_path("/foo").to_s
|
|
|
|
expect(args[args.length - 3]).to eql("#{expected}/")
|
2014-03-14 15:02:07 +00:00
|
|
|
}
|
2014-03-06 19:27:58 +00:00
|
|
|
|
|
|
|
subject.rsync_single(machine, ssh_info, opts)
|
|
|
|
end
|
|
|
|
end
|
2014-01-13 19:01:50 +00:00
|
|
|
end
|
2016-07-22 01:15:21 +00:00
|
|
|
|
|
|
|
describe "#rsync_single with custom ssh_info" do
|
|
|
|
let(:result) { Vagrant::Util::Subprocess::Result.new(0, "", "") }
|
|
|
|
|
|
|
|
let(:ssh_info) {{
|
2016-08-11 19:59:26 +00:00
|
|
|
:private_key_path => ['/path/to/key'],
|
2016-07-22 01:15:21 +00:00
|
|
|
:keys_only => true,
|
|
|
|
:paranoid => false,
|
|
|
|
}}
|
|
|
|
let(:opts) {{
|
|
|
|
hostpath: "/foo",
|
|
|
|
}}
|
|
|
|
let(:ui) { machine.ui }
|
|
|
|
|
|
|
|
before do
|
|
|
|
Vagrant::Util::Subprocess.stub(execute: result)
|
|
|
|
|
|
|
|
guest.stub(capability?: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "includes IdentitiesOnly, StrictHostKeyChecking, and UserKnownHostsFile with defaults" do
|
|
|
|
|
|
|
|
expect(Vagrant::Util::Subprocess).to receive(:execute).with { |*args|
|
|
|
|
expect(args[9]).to include('IdentitiesOnly')
|
|
|
|
expect(args[9]).to include('StrictHostKeyChecking')
|
|
|
|
expect(args[9]).to include('UserKnownHostsFile')
|
2016-08-11 19:59:26 +00:00
|
|
|
expect(args[9]).to include("-i '/path/to/key'")
|
2016-07-22 01:15:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
subject.rsync_single(machine, ssh_info, opts)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "omits IdentitiesOnly with keys_only = false" do
|
|
|
|
ssh_info[:keys_only] = false
|
|
|
|
|
|
|
|
Vagrant::Util::Subprocess.should_receive(:execute) do |*args|
|
|
|
|
expect(args[9]).not_to include('IdentitiesOnly')
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
subject.rsync_single(machine, ssh_info, opts)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "omits StrictHostKeyChecking and UserKnownHostsFile with paranoid = true" do
|
|
|
|
ssh_info[:keys_only] = false
|
|
|
|
|
|
|
|
Vagrant::Util::Subprocess.should_receive(:execute) do |*args|
|
|
|
|
expect(args[9]).not_to include('StrictHostKeyChecking ')
|
|
|
|
expect(args[9]).not_to include('UserKnownHostsFile ')
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
subject.rsync_single(machine, ssh_info, opts)
|
|
|
|
end
|
|
|
|
end
|
2014-01-13 19:01:50 +00:00
|
|
|
end
|