From ddfbe13b1dd56b4abfcf6dbcd0925284480894b2 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 14 Feb 2010 02:36:16 -0800 Subject: [PATCH] Shared folder action. --- lib/vagrant/actions/shared_folders.rb | 47 +++++++++++++++ test/vagrant/actions/shared_folders_test.rb | 64 +++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 lib/vagrant/actions/shared_folders.rb create mode 100644 test/vagrant/actions/shared_folders_test.rb diff --git a/lib/vagrant/actions/shared_folders.rb b/lib/vagrant/actions/shared_folders.rb new file mode 100644 index 000000000..dd6da5841 --- /dev/null +++ b/lib/vagrant/actions/shared_folders.rb @@ -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 \ No newline at end of file diff --git a/test/vagrant/actions/shared_folders_test.rb b/test/vagrant/actions/shared_folders_test.rb new file mode 100644 index 000000000..84f8de990 --- /dev/null +++ b/test/vagrant/actions/shared_folders_test.rb @@ -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