From a700d8878395f9171472e3ab05e4b4a887c43f5d Mon Sep 17 00:00:00 2001
From: Fabio Rehm <fgrehm@gmail.com>
Date: Tue, 9 Apr 2013 23:54:28 -0300
Subject: [PATCH] Store shared folders customizations on a local array instead
 of messing up with provider configs

---
 lib/vagrant-lxc/action/share_folders.rb |  3 +-
 lib/vagrant-lxc/driver.rb               | 14 ++++----
 spec/unit/driver_spec.rb                | 43 +++++++++++++++++++------
 3 files changed, 43 insertions(+), 17 deletions(-)

diff --git a/lib/vagrant-lxc/action/share_folders.rb b/lib/vagrant-lxc/action/share_folders.rb
index 4aefb0e..e7b659f 100644
--- a/lib/vagrant-lxc/action/share_folders.rb
+++ b/lib/vagrant-lxc/action/share_folders.rb
@@ -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
diff --git a/lib/vagrant-lxc/driver.rb b/lib/vagrant-lxc/driver.rb
index 643b950..48fe392 100644
--- a/lib/vagrant-lxc/driver.rb
+++ b/lib/vagrant-lxc/driver.rb
@@ -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
diff --git a/spec/unit/driver_spec.rb b/spec/unit/driver_spec.rb
index e91528e..87fe5fc 100644
--- a/spec/unit/driver_spec.rb
+++ b/spec/unit/driver_spec.rb
@@ -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