Skip to content

Commit

Permalink
Merge pull request #303 from test-kitchen/fix-296
Browse files Browse the repository at this point in the history
Only enable the cache when using known bento boxes. Fix #296
  • Loading branch information
Seth Thomas authored Mar 31, 2017
2 parents 1f05826 + 1fd9acd commit 28c2eb9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 48 deletions.
31 changes: 18 additions & 13 deletions lib/kitchen/driver/vagrant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ class Vagrant < Kitchen::Driver::Base
driver.windows_os? ? nil : "#{driver.instance.name}.vagrantup.com"
end

default_config(:cache_directory) do |driver|
driver.windows_os? ? "/omnibus/cache" : "/tmp/omnibus/cache"
end
default_config :cache_directory, false

default_config :kitchen_cache_directory,
File.expand_path("~/.kitchen/cache")
Expand Down Expand Up @@ -199,8 +197,11 @@ def winrm_transport?
# and share a local folder to that directory so that we don't pull them
# down every single time
def cache_directory
return if disable_cache?
config[:cache_directory]
if enable_cache? && !config[:cache_directory]
windows_os? ? "/omnibus/cache" : "/tmp/omnibus/cache"
else
config[:cache_directory]
end
end

protected
Expand Down Expand Up @@ -231,17 +232,21 @@ def bento_box?(name)
name =~ /^(centos|debian|fedora|freebsd|opensuse|ubuntu|oracle)-/
end

# Return true if we found the criteria to disable the cache_directory
# Returns whether or not the we expect the box to work with shared folders
# by matching against a whitelist of bento boxes
# @return [TrueClass,FalseClass] whether or not the box shoud work with
# shared folders
# @api private
def safe_share?(box)
box =~ /^bento\/(centos|debian|fedora|opensuse|ubuntu|oracle)-/
end

# Return true if we found the criteria to enable the cache_directory
# functionality
def disable_cache?
# Disable for Windows not using Virtualbox
if windows_host? && config[:provider] != "virtualbox" ||
instance.platform.name =~ /(freebsd|macos|osx)/ ||
# Disable if cache_directory is set to "false" on .kitchen.yml
!config[:cache_directory]
def enable_cache?
if safe_share?(config[:box])
return true
end

# Otherwise
false
end
Expand Down
37 changes: 2 additions & 35 deletions spec/kitchen/driver/vagrant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -340,32 +340,16 @@ def run_command(_cmd, options = {})
expect(driver[:ssh]).to eq(:a => "b", :c => { :d => "e" })
end

it "sets :synced_folders with the cache_directory by default" do
it "sets :synced_folders with the cache_directory for select bento boxes" do
config[:box] = "bento/centos-99"
expect(driver[:synced_folders]).to eq([cache_directory_array])
end

it "does not set :synced_folders to cache_directory on windows/non-vbox" do
allow(RbConfig::CONFIG).to receive(:[]).with("host_os").
and_return("mingw")
config[:provider] = "notvbox"
expect(driver[:synced_folders]).to eq([])
end

it "does not set :synced_folders to cache_directory on freebsd systems" do
allow(platform).to receive(:name).and_return("freebsd-99")
expect(driver[:synced_folders]).to eq([])
end

it "does not set :synced_folders to cache_directory on macos systems" do
allow(platform).to receive(:name).and_return("macos")
expect(driver[:synced_folders]).to eq([])
end

it "does not set :synced_folders to cache_directory on osx systems" do
allow(platform).to receive(:name).and_return("osx-99")
expect(driver[:synced_folders]).to eq([])
end

it "sets :synced_folders to a custom value" do
config[:synced_folders] = [
["/host_path", "/vm_path", "create: true, type: :nfs"],
Expand All @@ -376,7 +360,6 @@ def run_command(_cmd, options = {})
File.expand_path("/host_path"),
"/vm_path", "create: true, type: :nfs"
],
cache_directory_array,
])
end

Expand All @@ -387,7 +370,6 @@ def run_command(_cmd, options = {})

expect(driver[:synced_folders]).to eq([
[File.expand_path("/root/suitey-fooos-99"), "/vm_path", "stuff"],
cache_directory_array,
])
end

Expand All @@ -398,7 +380,6 @@ def run_command(_cmd, options = {})

expect(driver[:synced_folders]).to eq([
[File.expand_path("/kroot/a"), "/vm_path", "stuff"],
cache_directory_array,
])
end

Expand All @@ -409,7 +390,6 @@ def run_command(_cmd, options = {})

expect(driver[:synced_folders]).to eq([
[File.expand_path("/host_path"), "/vm_path", "nil"],
cache_directory_array,
])
end

Expand Down Expand Up @@ -471,14 +451,6 @@ def run_command(_cmd, options = {})

context "for windows os_types" do

let(:win_cache_directory_array) do
[
File.expand_path("~/.kitchen/cache"),
"/omnibus/cache",
"create: true",
]
end

before { allow(platform).to receive(:os_type).and_return("windows") }

it "sets :vm_hostname to nil by default" do
Expand All @@ -491,18 +463,13 @@ def run_command(_cmd, options = {})
expect(driver[:vm_hostname]).to eq("this-is-a--k")
end

it "sets :synced_folders with the cache_directory by default" do
expect(driver[:synced_folders]).to eq([win_cache_directory_array])
end

it "replaces %{instance_name} with instance name in :synced_folders" do
config[:synced_folders] = [
["/root/%{instance_name}", "/vm_path", "stuff"],
]

expect(driver[:synced_folders]).to eq([
[File.expand_path("/root/suitey-fooos-99"), "/vm_path", "stuff"],
win_cache_directory_array,
])
end
end
Expand Down

0 comments on commit 28c2eb9

Please sign in to comment.