Shared folder action.

This commit is contained in:
Mitchell Hashimoto 2010-02-14 02:36:16 -08:00
parent b88da5a6d3
commit ddfbe13b1d
2 changed files with 111 additions and 0 deletions

View File

@ -0,0 +1,47 @@
module Vagrant
module Actions
class SharedFolders < Base
def shared_folders
shared_folders = @vm.invoke_callback(:collect_shared_folders)
# Basic filtering of shared folders. Basically only verifies that
# the result is an array of 3 elements. In the future this should
# also verify that the host path exists, the name is valid,
# and that the guest path is valid.
shared_folders.collect do |folder|
if folder.is_a?(Array) && folder.length == 3
folder
else
nil
end
end.compact
end
def before_boot
logger.info "Creating shared folders metadata..."
shared_folders.each do |name, hostpath, guestpath|
folder = VirtualBox::SharedFolder.new
folder.name = name
folder.hostpath = hostpath
@vm.vm.shared_folders << folder
end
@vm.vm.save(true)
end
def after_boot
logger.info "Mounting shared folders..."
Vagrant::SSH.execute do |ssh|
shared_folders.each do |name, hostpath, guestpath|
logger.info "-- #{name}: #{guestpath}"
ssh.exec!("sudo mkdir -p #{guestpath}")
ssh.exec!("sudo mount -t vboxsf #{name} #{guestpath}")
ssh.exec!("sudo chown #{Vagrant.config.ssh.username} #{guestpath}")
end
end
end
end
end
end

View File

@ -0,0 +1,64 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
class SharedFoldersActionTest < Test::Unit::TestCase
setup do
@mock_vm, @vm, @action = mock_action(Vagrant::Actions::SharedFolders)
mock_config
end
def stub_shared_folders
folders = [%w{foo from to}, %w{bar bfrom bto}]
@action.expects(:shared_folders).returns(folders)
folders
end
context "collecting shared folders" do
should "return the arrays that the callback returns" do
result = [[1,2,3],[4,5,6]]
@mock_vm.expects(:invoke_callback).with(:collect_shared_folders).once.returns(result)
assert_equal result, @action.shared_folders
end
should "filter out invalid results" do
result = [[1,2,3],[4,5]]
@mock_vm.expects(:invoke_callback).with(:collect_shared_folders).once.returns(result)
assert_equal [[1,2,3]], @action.shared_folders
end
end
context "setting up shared folder metadata" do
setup do
@folders = stub_shared_folders
end
should "add all shared folders to the VM" do
share_seq = sequence("share_seq")
shared_folders = mock("shared_folders")
shared_folders.expects(:<<).in_sequence(share_seq).with() { |sf| sf.name == "foo" && sf.hostpath == "from" }
shared_folders.expects(:<<).in_sequence(share_seq).with() { |sf| sf.name == "bar" && sf.hostpath == "bfrom" }
@vm.stubs(:shared_folders).returns(shared_folders)
@vm.expects(:save).with(true).once
@action.before_boot
end
end
context "mounting the shared folders" do
setup do
@folders = stub_shared_folders
end
should "mount all shared folders to the VM" do
mount_seq = sequence("mount_seq")
ssh = mock("ssh")
@folders.each do |name, hostpath, guestpath|
ssh.expects(:exec!).with("sudo mkdir -p #{guestpath}").in_sequence(mount_seq)
ssh.expects(:exec!).with("sudo mount -t vboxsf #{name} #{guestpath}").in_sequence(mount_seq)
ssh.expects(:exec!).with("sudo chown #{Vagrant.config.ssh.username} #{guestpath}").in_sequence(mount_seq)
end
Vagrant::SSH.expects(:execute).yields(ssh)
@action.after_boot
end
end
end