From 9771c8bd52839e547f459a855773212921d8934c Mon Sep 17 00:00:00 2001 From: Teemu Matilainen Date: Sat, 1 Feb 2014 00:31:01 -0300 Subject: [PATCH 1/5] provisioners/file: Add unit tests for the config --- .../plugins/provisioners/file/config_test.rb | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 test/unit/plugins/provisioners/file/config_test.rb diff --git a/test/unit/plugins/provisioners/file/config_test.rb b/test/unit/plugins/provisioners/file/config_test.rb new file mode 100644 index 000000000..e41cac966 --- /dev/null +++ b/test/unit/plugins/provisioners/file/config_test.rb @@ -0,0 +1,71 @@ +require_relative "../../../base" + +require Vagrant.source_root.join("plugins/provisioners/file/config") + +describe VagrantPlugins::FileUpload::Config do + include_context "unit" + + subject { described_class.new } + + let(:machine) { double("machine") } + + describe "#validate" do + it "returns an error if destination is not specified" do + existing_file = File.expand_path(__FILE__) + + subject.source = existing_file + subject.finalize! + + result = subject.validate(machine) + expect(result["File provisioner"]).to eql([ + I18n.t("vagrant.provisioners.file.no_dest_file") + ]) + end + + it "returns an error if source is not specified" do + subject.destination = "/tmp/foo" + subject.finalize! + + result = subject.validate(machine) + expect(result["File provisioner"]).to eql([ + I18n.t("vagrant.provisioners.file.no_source_file") + ]) + end + + it "returns an error if source file does not exist" do + non_existing_file = "this/does/not/exist" + + subject.source = non_existing_file + subject.destination = "/tmp/foo" + subject.finalize! + + result = subject.validate(machine) + expect(result["File provisioner"]).to eql([ + I18n.t("vagrant.provisioners.file.path_invalid", + path: File.expand_path(non_existing_file)) + ]) + end + + it "passes with absolute source path" do + existing_absolute_path = File.expand_path(__FILE__) + + subject.source = existing_absolute_path + subject.destination = "/tmp/foo" + subject.finalize! + + result = subject.validate(machine) + expect(result["File provisioner"]).to eql([]) + end + + it "passes with relative source path" do + existing_relative_path = __FILE__ + + subject.source = existing_relative_path + subject.destination = "/tmp/foo" + subject.finalize! + + result = subject.validate(machine) + expect(result["File provisioner"]).to eql([]) + end + end +end From b52958bfb65e0d3fdb652dfa3d9d75ab91f6dc4c Mon Sep 17 00:00:00 2001 From: Teemu Matilainen Date: Sat, 1 Feb 2014 01:23:09 -0300 Subject: [PATCH 2/5] provisioners/file: expand destination path if capable --- plugins/provisioners/file/provisioner.rb | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/plugins/provisioners/file/provisioner.rb b/plugins/provisioners/file/provisioner.rb index 237e32043..7b997a7dc 100644 --- a/plugins/provisioners/file/provisioner.rb +++ b/plugins/provisioners/file/provisioner.rb @@ -3,12 +3,25 @@ module VagrantPlugins class Provisioner < Vagrant.plugin("2", :provisioner) def provision @machine.communicate.tap do |comm| + destination = expand_guest_path(config.destination) + # Make sure the remote path exists - command = "mkdir -p %s" % File.dirname(config.destination) - comm.execute(command) + command = "mkdir -p %s" % File.dirname(destination) + comm.execute(command) # now upload the file - comm.upload(File.expand_path(config.source), config.destination) + comm.upload(File.expand_path(config.source), destination) + end + end + + private + + # Expand the guest path if the guest has the capability + def expand_guest_path(destination) + if machine.guest.capability?(:shell_expand_guest_path) + machine.guest.capability(:shell_expand_guest_path, destination) + else + destination end end end From bc96a054be37dde4dd22aa0350c8b63a869003bf Mon Sep 17 00:00:00 2001 From: Teemu Matilainen Date: Sat, 1 Feb 2014 01:23:57 -0300 Subject: [PATCH 3/5] provisioners/file: add unit tests for the provisioner --- .../provisioners/file/provisioner_test.rb | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 test/unit/plugins/provisioners/file/provisioner_test.rb diff --git a/test/unit/plugins/provisioners/file/provisioner_test.rb b/test/unit/plugins/provisioners/file/provisioner_test.rb new file mode 100644 index 000000000..4dc692eac --- /dev/null +++ b/test/unit/plugins/provisioners/file/provisioner_test.rb @@ -0,0 +1,75 @@ +require_relative "../../../base" + +require Vagrant.source_root.join("plugins/provisioners/file/provisioner") + +describe VagrantPlugins::FileUpload::Provisioner do + include_context "unit" + + subject { described_class.new(machine, config) } + + 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(:machine) { iso_env.machine(iso_env.machine_names[0], :dummy) } + let(:config) { double("config") } + let(:communicator) { double("comm") } + let(:guest) { double("guest") } + + before do + machine.stub(communicate: communicator) + machine.stub(guest: guest) + + communicator.stub(execute: true) + communicator.stub(upload: true) + + guest.stub(capability?: false) + end + + describe "#provision" do + it "creates the destination directory" do + config.stub(source: "/source") + config.stub(destination: "/foo/bar") + + expect(communicator).to receive(:execute).with("mkdir -p /foo") + + subject.provision + end + + it "uploads the file" do + config.stub(source: "/source") + config.stub(destination: "/foo/bar") + + expect(communicator).to receive(:upload).with("/source", "/foo/bar") + + subject.provision + end + + it "expands the source file path" do + config.stub(source: "source") + config.stub(destination: "/foo/bar") + + expect(communicator).to receive(:upload).with( + File.expand_path("source"), "/foo/bar") + + subject.provision + end + + it "expands the destination file path if capable" do + config.stub(source: "/source") + config.stub(destination: "$HOME/foo") + + expect(guest).to receive(:capability?). + with(:shell_expand_guest_path).and_return(true) + expect(guest).to receive(:capability). + with(:shell_expand_guest_path, "$HOME/foo").and_return("/home/foo") + + expect(communicator).to receive(:upload).with("/source", "/home/foo") + + subject.provision + end + end +end From e634cdc824af128fdd0db9199a2bcdad9649f049 Mon Sep 17 00:00:00 2001 From: Teemu Matilainen Date: Sun, 2 Feb 2014 17:26:54 -0300 Subject: [PATCH 4/5] guests: rename LinuxShellExpandFailed error to ShellExpandFailed Make the error generic for all guests (`DarwinShellExpandFailed` didn't even exist) and not tied to synced folder. --- lib/vagrant/errors.rb | 8 ++++---- plugins/guests/darwin/cap/shell_expand_guest_path.rb | 2 +- plugins/guests/linux/cap/shell_expand_guest_path.rb | 2 +- templates/locales/en.yml | 10 +++++----- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index 0a7b1f479..204abd258 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -304,10 +304,6 @@ module Vagrant error_key(:linux_nfs_mount_failed) end - class LinuxShellExpandFailed < VagrantError - error_key(:linux_shell_expand_failed) - end - class LocalDataDirectoryNotAccessible < VagrantError error_key(:local_data_dir_not_accessible) end @@ -444,6 +440,10 @@ module Vagrant error_key(:shared_folder_create_failed) end + class ShellExpandFailed < VagrantError + error_key(:shell_expand_failed) + end + class SSHAuthenticationFailed < VagrantError error_key(:ssh_authentication_failed) end diff --git a/plugins/guests/darwin/cap/shell_expand_guest_path.rb b/plugins/guests/darwin/cap/shell_expand_guest_path.rb index 13a82979b..1fdcf5215 100644 --- a/plugins/guests/darwin/cap/shell_expand_guest_path.rb +++ b/plugins/guests/darwin/cap/shell_expand_guest_path.rb @@ -14,7 +14,7 @@ module VagrantPlugins if !real_path # If no real guest path was detected, this is really strange # and we raise an exception because this is a bug. - raise DarwinShellExpandFailed + raise ShellExpandFailed end # Chomp the string so that any trailing newlines are killed diff --git a/plugins/guests/linux/cap/shell_expand_guest_path.rb b/plugins/guests/linux/cap/shell_expand_guest_path.rb index 114edc415..341c0a1a8 100644 --- a/plugins/guests/linux/cap/shell_expand_guest_path.rb +++ b/plugins/guests/linux/cap/shell_expand_guest_path.rb @@ -17,7 +17,7 @@ module VagrantPlugins if !real_path # If no real guest path was detected, this is really strange # and we raise an exception because this is a bug. - raise LinuxShellExpandFailed + raise ShellExpandFailed end # Chomp the string so that any trailing newlines are killed diff --git a/templates/locales/en.yml b/templates/locales/en.yml index fd4590754..27d50e411 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -463,11 +463,6 @@ en: that the NFS client software is properly installed, and consult any resources specific to the linux distro you're using for more information on how to do this. - linux_shell_expand_failed: |- - Vagrant failed to determine the shell expansion of the guest path - for one of your shared folders. This is an extremely rare error case - and most likely indicates an unusual configuration of the guest system. - Please report a bug with your Vagrantfile. machine_guest_not_ready: |- Guest-specific operations were attempted on a machine that is not ready for guest communication. This should not happen and a bug @@ -591,6 +586,11 @@ en: %{path} Please create the folder manually or specify another path to share. + shell_expand_failed: |- + Vagrant failed to determine the shell expansion of a guest path + (probably for one of your shared folders). This is an extremely rare + error case and most likely indicates an unusual configuration of the + guest system. Please report a bug with your Vagrantfile and debug log. ssh_authentication_failed: |- SSH authentication failed! This is typically caused by the public/private keypair for the SSH user not being properly set on the guest VM. Please From 023d3115dd5a74f406600ba450f1a2a1ed188fcf Mon Sep 17 00:00:00 2001 From: Teemu Matilainen Date: Sun, 2 Feb 2014 17:38:24 -0300 Subject: [PATCH 5/5] guests/{free,net,open}bsd: Add `shell_expand_guest_path` capability --- .../freebsd/cap/shell_expand_guest_path.rb | 27 +++++++++++++++++++ plugins/guests/freebsd/plugin.rb | 5 ++++ .../netbsd/cap/shell_expand_guest_path.rb | 26 ++++++++++++++++++ plugins/guests/netbsd/plugin.rb | 5 ++++ .../openbsd/cap/shell_expand_guest_path.rb | 26 ++++++++++++++++++ plugins/guests/openbsd/plugin.rb | 5 ++++ 6 files changed, 94 insertions(+) create mode 100644 plugins/guests/freebsd/cap/shell_expand_guest_path.rb create mode 100644 plugins/guests/netbsd/cap/shell_expand_guest_path.rb create mode 100644 plugins/guests/openbsd/cap/shell_expand_guest_path.rb diff --git a/plugins/guests/freebsd/cap/shell_expand_guest_path.rb b/plugins/guests/freebsd/cap/shell_expand_guest_path.rb new file mode 100644 index 000000000..6d2f6e16a --- /dev/null +++ b/plugins/guests/freebsd/cap/shell_expand_guest_path.rb @@ -0,0 +1,27 @@ +module VagrantPlugins + module GuestFreeBSD + module Cap + class ShellExpandGuestPath + def self.shell_expand_guest_path(machine, path) + real_path = nil + machine.communicate.execute("printf #{path}", + shell: "sh") do |type, data| + if type == :stdout + real_path ||= "" + real_path += data + end + end + + if !real_path + # If no real guest path was detected, this is really strange + # and we raise an exception because this is a bug. + raise ShellExpandFailed + end + + # Chomp the string so that any trailing newlines are killed + return real_path.chomp + end + end + end + end +end diff --git a/plugins/guests/freebsd/plugin.rb b/plugins/guests/freebsd/plugin.rb index 8e147087b..4392a1614 100644 --- a/plugins/guests/freebsd/plugin.rb +++ b/plugins/guests/freebsd/plugin.rb @@ -30,6 +30,11 @@ module VagrantPlugins require_relative "cap/mount_nfs_folder" Cap::MountNFSFolder end + + guest_capability("freebsd", "shell_expand_guest_path") do + require_relative "cap/shell_expand_guest_path" + Cap::ShellExpandGuestPath + end end end end diff --git a/plugins/guests/netbsd/cap/shell_expand_guest_path.rb b/plugins/guests/netbsd/cap/shell_expand_guest_path.rb new file mode 100644 index 000000000..8471f4724 --- /dev/null +++ b/plugins/guests/netbsd/cap/shell_expand_guest_path.rb @@ -0,0 +1,26 @@ +module VagrantPlugins + module GuestNetBSD + module Cap + class ShellExpandGuestPath + def self.shell_expand_guest_path(machine, path) + real_path = nil + machine.communicate.execute("printf #{path}") do |type, data| + if type == :stdout + real_path ||= "" + real_path += data + end + end + + if !real_path + # If no real guest path was detected, this is really strange + # and we raise an exception because this is a bug. + raise ShellExpandFailed + end + + # Chomp the string so that any trailing newlines are killed + return real_path.chomp + end + end + end + end +end diff --git a/plugins/guests/netbsd/plugin.rb b/plugins/guests/netbsd/plugin.rb index ca55706fc..382754ffe 100644 --- a/plugins/guests/netbsd/plugin.rb +++ b/plugins/guests/netbsd/plugin.rb @@ -30,6 +30,11 @@ module VagrantPlugins require_relative "cap/mount_nfs_folder" Cap::MountNFSFolder end + + guest_capability("netbsd", "shell_expand_guest_path") do + require_relative "cap/shell_expand_guest_path" + Cap::ShellExpandGuestPath + end end end end diff --git a/plugins/guests/openbsd/cap/shell_expand_guest_path.rb b/plugins/guests/openbsd/cap/shell_expand_guest_path.rb new file mode 100644 index 000000000..653493548 --- /dev/null +++ b/plugins/guests/openbsd/cap/shell_expand_guest_path.rb @@ -0,0 +1,26 @@ +module VagrantPlugins + module GuestOpenBSD + module Cap + class ShellExpandGuestPath + def self.shell_expand_guest_path(machine, path) + real_path = nil + machine.communicate.execute("printf #{path}") do |type, data| + if type == :stdout + real_path ||= "" + real_path += data + end + end + + if !real_path + # If no real guest path was detected, this is really strange + # and we raise an exception because this is a bug. + raise ShellExpandFailed + end + + # Chomp the string so that any trailing newlines are killed + return real_path.chomp + end + end + end + end +end diff --git a/plugins/guests/openbsd/plugin.rb b/plugins/guests/openbsd/plugin.rb index fc1043a53..cf6dec82e 100644 --- a/plugins/guests/openbsd/plugin.rb +++ b/plugins/guests/openbsd/plugin.rb @@ -30,6 +30,11 @@ module VagrantPlugins require_relative "cap/mount_nfs_folder" Cap::MountNFSFolder end + + guest_capability("openbsd", "shell_expand_guest_path") do + require_relative "cap/shell_expand_guest_path" + Cap::ShellExpandGuestPath + end end end end