Skip to content

Commit

Permalink
Merge pull request #248 from afiune/afiune/COOL-321/kitchen-cache
Browse files Browse the repository at this point in the history
Add a synced folder to persist chef omnibus packages
  • Loading branch information
mwrock authored Nov 23, 2016
2 parents a3fdf32 + 20858aa commit aa748b2
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 6 deletions.
28 changes: 28 additions & 0 deletions lib/kitchen/driver/vagrant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,13 @@ def winrm_transport?
instance.transport.name.downcase =~ /win_?rm/
end

# Setting up the `cache_directory` to store omnibus packages in cache
# and share a local folder to that directory so that we don't pull them
# down every single time
def cache_directory
windows_os? ? "$env:TEMP\\omnibus\\cache" : "/tmp/omnibus/cache"
end

protected

WEBSITE = "http://www.vagrantup.com/downloads.html".freeze
Expand Down Expand Up @@ -248,6 +255,21 @@ def finalize_synced_folders!
options || "nil"
]
end
add_extra_synced_folders!
end

# We would like to sync a local folder to the instance so we can
# take advantage of the packages that we might have in cache,
# therefore we wont download a package we already have
def add_extra_synced_folders!
if cache_directory
FileUtils.mkdir_p(local_kitchen_cache)
config[:synced_folders].push([
local_kitchen_cache,
cache_directory,
"create: true"
])
end
end

# Truncates the length of `:vm_hostname` to 12 characters for
Expand Down Expand Up @@ -389,6 +411,12 @@ def update_state(state)
state[:rdp_port] = hash["RDPPort"] if hash["RDPPort"]
end

# @return [String] full absolute path to the kitchen cache directory
# @api private
def local_kitchen_cache
@local_kitchen_cache ||= File.expand_path("~/.kitchen/cache")
end

# @return [String] full local path to the directory containing the
# instance's Vagrantfile
# @api private
Expand Down
50 changes: 44 additions & 6 deletions spec/kitchen/driver/vagrant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@ def run_command(_cmd, options = {})

describe "configuration" do

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

%W[centos debian fedora opensuse ubuntu].each do |name|

context "for known bento platform names starting with #{name}" do
Expand Down Expand Up @@ -316,8 +324,8 @@ def run_command(_cmd, options = {})
expect(driver[:ssh]).to eq(:a => "b", :c => { :d => "e" })
end

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

it "sets :synced_folders to a custom value" do
Expand All @@ -326,7 +334,11 @@ def run_command(_cmd, options = {})
]

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

Expand All @@ -336,7 +348,8 @@ def run_command(_cmd, options = {})
]

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

Expand All @@ -346,7 +359,8 @@ def run_command(_cmd, options = {})
]

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

Expand All @@ -356,7 +370,8 @@ def run_command(_cmd, options = {})
]

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

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

context "for windows os_types" do

let(:win_cache_directory_array) do
[
File.expand_path("~/.kitchen/cache"),
"$env:TEMP\\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 @@ -429,6 +452,21 @@ 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
end

Expand Down

0 comments on commit aa748b2

Please sign in to comment.