Store shared folders customizations on a local array instead of messing up with provider configs

This commit is contained in:
Fabio Rehm 2013-04-09 23:54:28 -03:00
parent 7187556b6a
commit a700d88783
3 changed files with 43 additions and 17 deletions

View file

@ -60,8 +60,7 @@ module Vagrant
@env[:ui].info(I18n.t("vagrant.actions.vm.share_folders.mounting_entry",
:guest_path => data[:guestpath]))
end
config = @env[:machine].provider_config
@env[:machine].provider.driver.share_folders(folders, config)
@env[:machine].provider.driver.share_folders(folders)
end
end
end

View file

@ -14,12 +14,14 @@ module Vagrant
# a name.
class ContainerNotFound < StandardError; end
attr_reader :container_name
attr_reader :container_name,
:customizations
def initialize(container_name, cli = CLI.new(container_name))
@container_name = container_name
@cli = cli
@logger = Log4r::Logger.new("vagrant::provider::lxc::driver")
@customizations = []
end
def validate!
@ -43,20 +45,19 @@ module Vagrant
end
end
def share_folders(folders, config)
def share_folders(folders)
folders.each do |folder|
guestpath = rootfs_path.join(folder[:guestpath].gsub(/^\//, ''))
unless guestpath.directory?
begin
@logger.debug("Guest path doesn't exist, creating: #{guestpath}")
system "sudo mkdir -p #{guestpath.to_s}"
rescue Errno::EACCES
raise Vagrant::Errors::SharedFolderCreateFailed,
:path => guestpath.to_s
raise Vagrant::Errors::SharedFolderCreateFailed, :path => guestpath.to_s
end
end
# TODO: Move outside
config.customize 'mount.entry', "#{folder[:hostpath]} #{guestpath} none bind 0 0"
@customizations << ['mount.entry', "#{folder[:hostpath]} #{guestpath} none bind 0 0"]
end
end
@ -66,6 +67,7 @@ module Vagrant
if ENV['LXC_START_LOG_FILE']
extra = ['-o', ENV['LXC_START_LOG_FILE'], '-l', 'DEBUG']
end
customizations = customizations + @customizations
@cli.transition_to(:running) { |c| c.start(customizations, (extra || nil)) }
end

View file

@ -70,24 +70,24 @@ describe Vagrant::LXC::Driver do
end
describe 'start' do
let(:name) { 'container-name' }
let(:customizations) { [['a', '1'], ['b', '2']] }
let(:cli) { fire_double('Vagrant::LXC::Driver::CLI', start: true) }
let(:customizations) { [['a', '1'], ['b', '2']] }
let(:internal_customization) { ['internal', 'customization'] }
let(:cli) { fire_double('Vagrant::LXC::Driver::CLI', start: true) }
subject { described_class.new(name, cli) }
subject { described_class.new('name', cli) }
before do
cli.stub(:transition_to).and_yield(cli)
subject.customizations << internal_customization
subject.start(customizations)
end
it 'starts container with configured lxc settings' do
cli.should_receive(:start).with(customizations, nil)
subject.start(customizations)
it 'starts container with configured customizations' do
cli.should have_received(:start).with(customizations + [internal_customization], nil)
end
it 'expects a transition to running state to take place' do
cli.should_receive(:transition_to).with(:running)
subject.start(customizations)
cli.should have_received(:transition_to).with(:running)
end
end
@ -149,4 +149,29 @@ describe Vagrant::LXC::Driver do
end
end
end
describe 'folder sharing' do
let(:shared_folder) { {guestpath: '/vagrant', hostpath: '/path/to/host/dir'} }
let(:folders) { [shared_folder] }
let(:rootfs_path) { Pathname('/path/to/rootfs') }
let(:expected_guest_path) { "#{rootfs_path}/vagrant" }
subject { described_class.new('container-name') }
before do
subject.stub(rootfs_path: rootfs_path, system: true)
subject.share_folders(folders)
end
it "creates guest folder under container's rootfs" do
subject.should have_received(:system).with("sudo mkdir -p #{expected_guest_path}")
end
it 'adds a mount.entry to its local customizations' do
subject.customizations.should include [
'mount.entry',
"#{shared_folder[:hostpath]} #{expected_guest_path} none bind 0 0"
]
end
end
end