NFS cleanup on BSD host

This commit is contained in:
Mitchell Hashimoto 2010-07-13 22:30:54 -07:00
parent fe430ff006
commit dc5a1be320
9 changed files with 108 additions and 1 deletions

View File

@ -0,0 +1,20 @@
require File.join(File.dirname(__FILE__), 'nfs_helpers')
module Vagrant
class Action
module VM
class ClearNFSExports
include NFSHelpers
def initialize(app, env)
@app = app
end
def call(env)
clear_nfs_exports(env)
@app.call(env)
end
end
end
end
end

View File

@ -1,3 +1,5 @@
require File.join(File.dirname(__FILE__), 'nfs_helpers')
module Vagrant
class Action
module VM
@ -15,6 +17,7 @@ module Vagrant
#
class NFS
include ExceptionCatcher
include NFSHelpers
def initialize(app,env)
@app = app
@ -27,7 +30,12 @@ module Vagrant
@env = env
extract_folders
export_folders if !folders.empty?
if !folders.empty?
export_folders
clear_nfs_exports(env)
end
return if env.error?
@app.call(env)

View File

@ -0,0 +1,11 @@
module Vagrant
class Action
module VM
module NFSHelpers
def clear_nfs_exports(env)
env["host"].nfs_cleanup if env["host"]
end
end
end
end
end

View File

@ -67,6 +67,10 @@ module Vagrant
# @param [Hash] folders Shared folders to sync.
def nfs_export(ip, folders)
end
# Cleans up the exports for the current VM.
def nfs_cleanup
end
end
end
end

View File

@ -28,6 +28,16 @@ module Vagrant
# is not starting
system("sudo nfsd restart")
end
def nfs_cleanup
system("cat /etc/exports | grep 'VAGRANT-BEGIN: #{env.vm.uuid}' > /dev/null 2>&1")
if $?.to_i == 0
# Use sed to just strip out the block of code which was inserted
# by Vagrant
system("sudo sed -e '/^# VAGRANT-BEGIN: #{env.vm.uuid}/,/^# VAGRANT-END: #{env.vm.uuid}/ d' -i bak /etc/exports")
end
end
end
end
end

View File

@ -0,0 +1,22 @@
require File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper')
class ClearNFSExportsActionTest < Test::Unit::TestCase
setup do
@klass = Vagrant::Action::VM::ClearNFSExports
@app, @env = mock_action_data
@env.env.stubs(:host).returns(Vagrant::Hosts::Base.new(@env))
@instance = @klass.new(@app, @env)
end
should "include the NFS helpers module" do
assert @klass.included_modules.include?(Vagrant::Action::VM::NFSHelpers)
end
should "clear NFS exports then continue chain" do
seq = sequence("seq")
@instance.expects(:clear_nfs_exports).with(@env).in_sequence(seq)
@app.expects(:call).with(@env).in_sequence(seq)
@instance.call(@env)
end
end

View File

@ -0,0 +1,23 @@
require File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper')
class NFSHelpersVMActionTest < Test::Unit::TestCase
setup do
@klass = Class.new
@klass.send(:include, Vagrant::Action::VM::NFSHelpers)
@app, @env = mock_action_data
@instance = @klass.new
end
should "clear NFS exports for the environment if the host exists" do
@host = mock("host")
@env.env.stubs(:host).returns(@host)
@host.expects(:nfs_cleanup).once
@instance.clear_nfs_exports(@env)
end
should "not do anything if host is nil" do
assert_nothing_raised { @instance.clear_nfs_exports(@env) }
end
end

View File

@ -46,6 +46,7 @@ class NFSVMActionTest < Test::Unit::TestCase
seq = sequence('seq')
@instance.expects(:extract_folders).in_sequence(seq)
@instance.expects(:export_folders).in_sequence(seq)
@instance.expects(:clear_nfs_exports).with(@env).in_sequence(seq)
@app.expects(:call).with(@env).in_sequence(seq)
@instance.expects(:mount_folders).in_sequence(seq)
@instance.call(@env)
@ -57,6 +58,7 @@ class NFSVMActionTest < Test::Unit::TestCase
seq = sequence('seq')
@instance.expects(:extract_folders).in_sequence(seq)
@instance.expects(:export_folders).never
@instance.expects(:clear_nfs_exports).never
@app.expects(:call).with(@env).in_sequence(seq)
@instance.expects(:mount_folders).never
@instance.call(@env)
@ -68,6 +70,7 @@ class NFSVMActionTest < Test::Unit::TestCase
seq = sequence('seq')
@instance.expects(:extract_folders).in_sequence(seq)
@instance.expects(:export_folders).in_sequence(seq)
@instance.expects(:clear_nfs_exports).in_sequence(seq)
@app.expects(:call).never
@instance.call(@env)
end

View File

@ -5,6 +5,8 @@ class BSDHostTest < Test::Unit::TestCase
@klass = Vagrant::Hosts::BSD
@env = mock_environment
@env.stubs(:vm).returns(Vagrant::VM.new(:env => @env))
@env.logger.stubs(:info)
@instance = @klass.new(@env)
end
@ -53,4 +55,8 @@ class BSDHostTest < Test::Unit::TestCase
@instance.nfs_export(@ip, @folders)
end
end
context "nfs cleanup" do
# TODO: How to test all the system calls?
end
end