From 67ab68df893d64a3e0d31d0397fd61d6f8a556fa Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 20 Jun 2010 02:07:32 -0700 Subject: [PATCH] System `prepare_unison` method + tests --- lib/vagrant/actions/vm/shared_folders.rb | 12 +++ lib/vagrant/systems/base.rb | 7 ++ lib/vagrant/systems/linux.rb | 30 +++---- lib/vagrant/util/template_renderer.rb | 2 +- .../vagrant/actions/vm/shared_folders_test.rb | 20 +++++ test/vagrant/systems/linux_test.rb | 81 ++++++++++--------- test/vagrant/util/template_renderer_test.rb | 6 ++ 7 files changed, 100 insertions(+), 58 deletions(-) diff --git a/lib/vagrant/actions/vm/shared_folders.rb b/lib/vagrant/actions/vm/shared_folders.rb index d19272037..0de027367 100644 --- a/lib/vagrant/actions/vm/shared_folders.rb +++ b/lib/vagrant/actions/vm/shared_folders.rb @@ -20,6 +20,16 @@ module Vagrant end end + # This method returns the list of shared folders which are to + # be synced via unison. + def unison_folders + shared_folders.inject({}) do |acc, data| + key, value = data + acc[key] = value if !!value[:sync] + acc + end + end + def before_boot clear_shared_folders create_metadata @@ -42,6 +52,8 @@ module Vagrant end def setup_unison + return if unison_folders.empty? + # TODO end diff --git a/lib/vagrant/systems/base.rb b/lib/vagrant/systems/base.rb index 4dc913435..31824c357 100644 --- a/lib/vagrant/systems/base.rb +++ b/lib/vagrant/systems/base.rb @@ -55,6 +55,13 @@ module Vagrant # wants the folder mounted. def mount_shared_folder(ssh, name, guestpath); end + # Prepares the system for unison folder syncing. This is called + # once once prior to any `create_unison` calls. + def prepare_unison(ssh); end + + # Creates an entry for folder syncing via unison. + def create_unison(ssh, options); end + # Prepares the system for host only networks. This is called # once prior to any `enable_host_only_network` calls. def prepare_host_only_network; end diff --git a/lib/vagrant/systems/linux.rb b/lib/vagrant/systems/linux.rb index 170acc0d5..011676d6c 100644 --- a/lib/vagrant/systems/linux.rb +++ b/lib/vagrant/systems/linux.rb @@ -52,21 +52,21 @@ module Vagrant chown(ssh, guestpath) end - def create_sync(ssh, opts) - crontab_entry = render_crontab_entry(opts.merge(:syncopts => config.vm.sync_opts, - :scriptname => config.vm.sync_script)) + # def create_sync(ssh, opts) + # crontab_entry = render_crontab_entry(opts.merge(:syncopts => config.vm.sync_opts, + # :scriptname => config.vm.sync_script)) - ssh.exec!("sudo mkdir -p #{opts[:syncpath]}") - chown(ssh, opts[:syncpath]) - ssh.exec!("sudo echo \"#{crontab_entry}\" >> #{config.vm.sync_crontab_entry_file}") - ssh.exec!("crontab #{config.vm.sync_crontab_entry_file}") - end + # ssh.exec!("sudo mkdir -p #{opts[:syncpath]}") + # chown(ssh, opts[:syncpath]) + # ssh.exec!("sudo echo \"#{crontab_entry}\" >> #{config.vm.sync_crontab_entry_file}") + # ssh.exec!("crontab #{config.vm.sync_crontab_entry_file}") + # end - def prepare_sync(ssh) - logger.info "Preparing system for sync..." - vm.ssh.upload!(StringIO.new(render_sync), config.vm.sync_script) - ssh.exec!("sudo chmod +x #{config.vm.sync_script}") - ssh.exec!("sudo rm #{config.vm.sync_crontab_entry_file}", :error_check => false) + def prepare_unison(ssh) + logger.info "Preparing system for unison sync..." + vm.ssh.upload!(StringIO.new(TemplateRenderer.render('/unison/script')), config.unison.script) + ssh.exec!("sudo chmod +x #{config.unison.script}") + ssh.exec!("sudo rm #{config.unison.crontab_entry_file}", :error_check => false) end def prepare_host_only_network @@ -125,10 +125,6 @@ module Vagrant vm.env.config end - def render_sync - TemplateRenderer.render('sync') - end - def render_crontab_entry(opts) TemplateRenderer.render('crontab_entry', opts) end diff --git a/lib/vagrant/util/template_renderer.rb b/lib/vagrant/util/template_renderer.rb index 74971a075..243581a4f 100644 --- a/lib/vagrant/util/template_renderer.rb +++ b/lib/vagrant/util/template_renderer.rb @@ -76,7 +76,7 @@ module Vagrant # # @return [String] def full_template_path - File.join(PROJECT_ROOT, 'templates', "#{template}.erb") + File.join(PROJECT_ROOT, 'templates', "#{template}.erb").squeeze("/") end end end diff --git a/test/vagrant/actions/vm/shared_folders_test.rb b/test/vagrant/actions/vm/shared_folders_test.rb index 3d4f4360a..112f354da 100644 --- a/test/vagrant/actions/vm/shared_folders_test.rb +++ b/test/vagrant/actions/vm/shared_folders_test.rb @@ -83,6 +83,22 @@ class SharedFoldersActionTest < Test::Unit::TestCase end end + context "unison shared folders" do + setup do + @folders = stub_shared_folders do |config| + config.vm.share_folder("foo", "bar", "baz", :sync => true) + config.vm.share_folder("bar", "foo", "baz") + end + end + + should "only return the folders marked for syncing" do + result = @action.unison_folders + assert_equal 1, result.length + assert result.has_key?("foo") + assert !result.has_key?("bar") + end + end + context "clearing shared folders" do setup do @shared_folder = mock("shared_folder") @@ -146,4 +162,8 @@ class SharedFoldersActionTest < Test::Unit::TestCase @action.mount_shared_folders end end + + context "setting up unison" do + + end end diff --git a/test/vagrant/systems/linux_test.rb b/test/vagrant/systems/linux_test.rb index a31b67909..219ce33fb 100644 --- a/test/vagrant/systems/linux_test.rb +++ b/test/vagrant/systems/linux_test.rb @@ -43,65 +43,66 @@ class LinuxSystemTest < Test::Unit::TestCase end end - context "preparing sync" do + context "preparing unison" do setup do @ssh.stubs(:exec!) + @ssh.stubs(:upload!) @vm.stubs(:ssh).returns(@ssh) - @vm.ssh.stubs(:upload!) end - should "upload the sync template" do - @vm.ssh.expects(:upload!).with do |string_io, guest_path| - string_io.string =~ /#!\/bin\/sh/ && guest_path == @mock_env.config.vm.sync_script + should "upload the script" do + @vm.ssh.expects(:upload!).with do |script, path| + assert_equal @mock_env.config.unison.script, path + true end - @instance.prepare_sync(@ssh) + @instance.prepare_unison(@ssh) end - should "remove old crontab entries file" do - @ssh.expects(:exec!).with("sudo rm #{@mock_env.config.vm.sync_crontab_entry_file}", :error_check => false) - @instance.prepare_sync(@ssh) + should "make the script executable" do + @ssh.expects(:exec!).with("sudo chmod +x #{@mock_env.config.unison.script}").once + @instance.prepare_unison(@ssh) end - should "prepare the sync template for execution" do - @ssh.expects(:exec!).with("sudo chmod +x #{@mock_env.config.vm.sync_script}") - @instance.prepare_sync(@ssh) + should "remove old crontab entry file" do + @ssh.expects(:exec!).with("sudo rm #{@mock_env.config.unison.crontab_entry_file}", :error_check => false).once + @instance.prepare_unison(@ssh) end end - context "setting up an sync folder" do - setup do - @ssh.stubs(:exec!) - end + # context "setting up an sync folder" do + # setup do + # @ssh.stubs(:exec!) + # end - should "create the new rysnc destination directory" do - sync_path = 'foo' - @ssh.expects(:exec!).with("sudo mkdir -p #{sync_path}") - @instance.create_sync(@ssh, :syncpath => "foo") - end + # should "create the new rysnc destination directory" do + # sync_path = 'foo' + # @ssh.expects(:exec!).with("sudo mkdir -p #{sync_path}") + # @instance.create_sync(@ssh, :syncpath => "foo") + # end - should "add an entry to the crontab file" do - @instance.expects(:render_crontab_entry).returns('foo') - @ssh.expects(:exec!).with do |cmd| - cmd =~ /echo/ && cmd =~ /foo/ && cmd =~ /#{@mock_env.config.vm.sync_crontab_entry_file}/ - end - @instance.create_sync(@ssh, {}) - end + # should "add an entry to the crontab file" do + # @instance.expects(:render_crontab_entry).returns('foo') + # @ssh.expects(:exec!).with do |cmd| + # cmd =~ /echo/ && cmd =~ /foo/ && cmd =~ /#{@mock_env.config.vm.sync_crontab_entry_file}/ + # end + # @instance.create_sync(@ssh, {}) + # end - should "use the crontab entry file to define vagrant users cron entries" do - @ssh.expects(:exec!).with("crontab #{@mock_env.config.vm.sync_crontab_entry_file}") - @instance.create_sync(@ssh, {}) - end + # should "use the crontab entry file to define vagrant users cron entries" do + # @ssh.expects(:exec!).with("crontab #{@mock_env.config.vm.sync_crontab_entry_file}") + # @instance.create_sync(@ssh, {}) + # end - should "chown the sync directory" do - @instance.expects(:chown).with(@ssh, "foo") - @instance.create_sync(@ssh, :syncpath => "foo") - end + # should "chown the sync directory" do + # @instance.expects(:chown).with(@ssh, "foo") + # @instance.create_sync(@ssh, :syncpath => "foo") + # end - should "return provide a formatted crontab entry that runs every minute" do - assert @instance.render_crontab_entry({}).include?("* * * * *") - end - end + # should "return provide a formatted crontab entry that runs every minute" do + # assert @instance.render_crontab_entry({}).include?("* * * * *") + # end + # end #------------------------------------------------------------------- # "Private" methods tests diff --git a/test/vagrant/util/template_renderer_test.rb b/test/vagrant/util/template_renderer_test.rb index 669430c10..5c2812556 100644 --- a/test/vagrant/util/template_renderer_test.rb +++ b/test/vagrant/util/template_renderer_test.rb @@ -75,6 +75,12 @@ class TemplateRendererUtilTest < Test::Unit::TestCase result = File.join(PROJECT_ROOT, "templates", "#{@template}.erb") assert_equal result, @r.full_template_path end + + should "remove duplicate path separators" do + @r.template = "foo///bar" + result = File.join(PROJECT_ROOT, "templates", "foo", "bar.erb") + assert_equal result, @r.full_template_path + end end context "class methods" do