System `prepare_unison` method + tests

This commit is contained in:
Mitchell Hashimoto 2010-06-20 02:07:32 -07:00
parent 5f0695f776
commit 67ab68df89
7 changed files with 100 additions and 58 deletions

View File

@ -20,6 +20,16 @@ module Vagrant
end end
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 def before_boot
clear_shared_folders clear_shared_folders
create_metadata create_metadata
@ -42,6 +52,8 @@ module Vagrant
end end
def setup_unison def setup_unison
return if unison_folders.empty?
# TODO # TODO
end end

View File

@ -55,6 +55,13 @@ module Vagrant
# wants the folder mounted. # wants the folder mounted.
def mount_shared_folder(ssh, name, guestpath); end 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 # Prepares the system for host only networks. This is called
# once prior to any `enable_host_only_network` calls. # once prior to any `enable_host_only_network` calls.
def prepare_host_only_network; end def prepare_host_only_network; end

View File

@ -52,21 +52,21 @@ module Vagrant
chown(ssh, guestpath) chown(ssh, guestpath)
end end
def create_sync(ssh, opts) # def create_sync(ssh, opts)
crontab_entry = render_crontab_entry(opts.merge(:syncopts => config.vm.sync_opts, # crontab_entry = render_crontab_entry(opts.merge(:syncopts => config.vm.sync_opts,
:scriptname => config.vm.sync_script)) # :scriptname => config.vm.sync_script))
ssh.exec!("sudo mkdir -p #{opts[:syncpath]}") # ssh.exec!("sudo mkdir -p #{opts[:syncpath]}")
chown(ssh, opts[:syncpath]) # chown(ssh, opts[:syncpath])
ssh.exec!("sudo echo \"#{crontab_entry}\" >> #{config.vm.sync_crontab_entry_file}") # ssh.exec!("sudo echo \"#{crontab_entry}\" >> #{config.vm.sync_crontab_entry_file}")
ssh.exec!("crontab #{config.vm.sync_crontab_entry_file}") # ssh.exec!("crontab #{config.vm.sync_crontab_entry_file}")
end # end
def prepare_sync(ssh) def prepare_unison(ssh)
logger.info "Preparing system for sync..." logger.info "Preparing system for unison sync..."
vm.ssh.upload!(StringIO.new(render_sync), config.vm.sync_script) vm.ssh.upload!(StringIO.new(TemplateRenderer.render('/unison/script')), config.unison.script)
ssh.exec!("sudo chmod +x #{config.vm.sync_script}") ssh.exec!("sudo chmod +x #{config.unison.script}")
ssh.exec!("sudo rm #{config.vm.sync_crontab_entry_file}", :error_check => false) ssh.exec!("sudo rm #{config.unison.crontab_entry_file}", :error_check => false)
end end
def prepare_host_only_network def prepare_host_only_network
@ -125,10 +125,6 @@ module Vagrant
vm.env.config vm.env.config
end end
def render_sync
TemplateRenderer.render('sync')
end
def render_crontab_entry(opts) def render_crontab_entry(opts)
TemplateRenderer.render('crontab_entry', opts) TemplateRenderer.render('crontab_entry', opts)
end end

View File

@ -76,7 +76,7 @@ module Vagrant
# #
# @return [String] # @return [String]
def full_template_path def full_template_path
File.join(PROJECT_ROOT, 'templates', "#{template}.erb") File.join(PROJECT_ROOT, 'templates', "#{template}.erb").squeeze("/")
end end
end end
end end

View File

@ -83,6 +83,22 @@ class SharedFoldersActionTest < Test::Unit::TestCase
end end
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 context "clearing shared folders" do
setup do setup do
@shared_folder = mock("shared_folder") @shared_folder = mock("shared_folder")
@ -146,4 +162,8 @@ class SharedFoldersActionTest < Test::Unit::TestCase
@action.mount_shared_folders @action.mount_shared_folders
end end
end end
context "setting up unison" do
end
end end

View File

@ -43,65 +43,66 @@ class LinuxSystemTest < Test::Unit::TestCase
end end
end end
context "preparing sync" do context "preparing unison" do
setup do setup do
@ssh.stubs(:exec!) @ssh.stubs(:exec!)
@ssh.stubs(:upload!)
@vm.stubs(:ssh).returns(@ssh) @vm.stubs(:ssh).returns(@ssh)
@vm.ssh.stubs(:upload!)
end end
should "upload the sync template" do should "upload the script" do
@vm.ssh.expects(:upload!).with do |string_io, guest_path| @vm.ssh.expects(:upload!).with do |script, path|
string_io.string =~ /#!\/bin\/sh/ && guest_path == @mock_env.config.vm.sync_script assert_equal @mock_env.config.unison.script, path
true
end end
@instance.prepare_sync(@ssh) @instance.prepare_unison(@ssh)
end end
should "remove old crontab entries file" do should "make the script executable" do
@ssh.expects(:exec!).with("sudo rm #{@mock_env.config.vm.sync_crontab_entry_file}", :error_check => false) @ssh.expects(:exec!).with("sudo chmod +x #{@mock_env.config.unison.script}").once
@instance.prepare_sync(@ssh) @instance.prepare_unison(@ssh)
end end
should "prepare the sync template for execution" do should "remove old crontab entry file" do
@ssh.expects(:exec!).with("sudo chmod +x #{@mock_env.config.vm.sync_script}") @ssh.expects(:exec!).with("sudo rm #{@mock_env.config.unison.crontab_entry_file}", :error_check => false).once
@instance.prepare_sync(@ssh) @instance.prepare_unison(@ssh)
end end
end end
context "setting up an sync folder" do # context "setting up an sync folder" do
setup do # setup do
@ssh.stubs(:exec!) # @ssh.stubs(:exec!)
end # end
should "create the new rysnc destination directory" do # should "create the new rysnc destination directory" do
sync_path = 'foo' # sync_path = 'foo'
@ssh.expects(:exec!).with("sudo mkdir -p #{sync_path}") # @ssh.expects(:exec!).with("sudo mkdir -p #{sync_path}")
@instance.create_sync(@ssh, :syncpath => "foo") # @instance.create_sync(@ssh, :syncpath => "foo")
end # end
should "add an entry to the crontab file" do # should "add an entry to the crontab file" do
@instance.expects(:render_crontab_entry).returns('foo') # @instance.expects(:render_crontab_entry).returns('foo')
@ssh.expects(:exec!).with do |cmd| # @ssh.expects(:exec!).with do |cmd|
cmd =~ /echo/ && cmd =~ /foo/ && cmd =~ /#{@mock_env.config.vm.sync_crontab_entry_file}/ # cmd =~ /echo/ && cmd =~ /foo/ && cmd =~ /#{@mock_env.config.vm.sync_crontab_entry_file}/
end # end
@instance.create_sync(@ssh, {}) # @instance.create_sync(@ssh, {})
end # end
should "use the crontab entry file to define vagrant users cron entries" do # 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}") # @ssh.expects(:exec!).with("crontab #{@mock_env.config.vm.sync_crontab_entry_file}")
@instance.create_sync(@ssh, {}) # @instance.create_sync(@ssh, {})
end # end
should "chown the sync directory" do # should "chown the sync directory" do
@instance.expects(:chown).with(@ssh, "foo") # @instance.expects(:chown).with(@ssh, "foo")
@instance.create_sync(@ssh, :syncpath => "foo") # @instance.create_sync(@ssh, :syncpath => "foo")
end # end
should "return provide a formatted crontab entry that runs every minute" do # should "return provide a formatted crontab entry that runs every minute" do
assert @instance.render_crontab_entry({}).include?("* * * * *") # assert @instance.render_crontab_entry({}).include?("* * * * *")
end # end
end # end
#------------------------------------------------------------------- #-------------------------------------------------------------------
# "Private" methods tests # "Private" methods tests

View File

@ -75,6 +75,12 @@ class TemplateRendererUtilTest < Test::Unit::TestCase
result = File.join(PROJECT_ROOT, "templates", "#{@template}.erb") result = File.join(PROJECT_ROOT, "templates", "#{@template}.erb")
assert_equal result, @r.full_template_path assert_equal result, @r.full_template_path
end 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 end
context "class methods" do context "class methods" do