Provisioning redone to use new environment

This commit is contained in:
Mitchell Hashimoto 2010-03-19 22:15:20 -07:00
parent 11780fb618
commit fee10c47fd
10 changed files with 113 additions and 109 deletions

View File

@ -19,7 +19,7 @@ module Vagrant
end end
if provisioner.is_a?(Class) if provisioner.is_a?(Class)
@provisioner = provisioner.new @provisioner = provisioner.new(@runner.env)
raise ActionException.new(:provisioner_invalid_class) unless @provisioner.is_a?(Provisioners::Base) raise ActionException.new(:provisioner_invalid_class) unless @provisioner.is_a?(Provisioners::Base)
elsif provisioner.is_a?(Symbol) elsif provisioner.is_a?(Symbol)
# We have a few hard coded provisioners for built-ins # We have a few hard coded provisioners for built-ins
@ -30,7 +30,7 @@ module Vagrant
provisioner_klass = mapping[provisioner] provisioner_klass = mapping[provisioner]
raise ActionException.new(:provisioner_unknown_type, :provisioner => provisioner.to_s) if provisioner_klass.nil? raise ActionException.new(:provisioner_unknown_type, :provisioner => provisioner.to_s) if provisioner_klass.nil?
@provisioner = provisioner_klass.new @provisioner = provisioner_klass.new(@runner.env)
end end
logger.info "Provisioning enabled with #{@provisioner.class}" logger.info "Provisioning enabled with #{@provisioner.class}"

View File

@ -7,6 +7,13 @@ module Vagrant
class Base class Base
include Vagrant::Util include Vagrant::Util
# The environment which this is being provisioned in
attr_reader :env
def initialize(env)
@env = env
end
# This is the method called to "prepare" the provisioner. This is called # This is the method called to "prepare" the provisioner. This is called
# before any actions are run by the action runner (see {Vagrant::Actions::Runner}). # before any actions are run by the action runner (see {Vagrant::Actions::Runner}).
# This can be used to setup shared folders, forward ports, etc. Whatever is # This can be used to setup shared folders, forward ports, etc. Whatever is

View File

@ -71,9 +71,9 @@ module Vagrant
def chown_provisioning_folder def chown_provisioning_folder
logger.info "Setting permissions on chef provisioning folder..." logger.info "Setting permissions on chef provisioning folder..."
SSH.execute do |ssh| env.ssh.execute do |ssh|
ssh.exec!("sudo mkdir -p #{Vagrant.config.chef.provisioning_path}") ssh.exec!("sudo mkdir -p #{env.config.chef.provisioning_path}")
ssh.exec!("sudo chown #{Vagrant.config.ssh.username} #{Vagrant.config.chef.provisioning_path}") ssh.exec!("sudo chown #{env.config.ssh.username} #{env.config.chef.provisioning_path}")
end end
end end
@ -82,8 +82,8 @@ module Vagrant
# Set up initial configuration # Set up initial configuration
data = { data = {
:config => Vagrant.config, :config => env.config,
:directory => Vagrant.config.vm.project_directory, :directory => env.config.vm.project_directory,
} }
# And wrap it under the "vagrant" namespace # And wrap it under the "vagrant" namespace
@ -91,11 +91,11 @@ module Vagrant
# Merge with the "extra data" which isn't put under the # Merge with the "extra data" which isn't put under the
# vagrant namespace by default # vagrant namespace by default
data.merge!(Vagrant.config.chef.json) data.merge!(env.config.chef.json)
json = data.to_json json = data.to_json
SSH.upload!(StringIO.new(json), File.join(Vagrant.config.chef.provisioning_path, "dna.json")) env.ssh.upload!(StringIO.new(json), File.join(env.config.chef.provisioning_path, "dna.json"))
end end
end end
end end

View File

@ -4,13 +4,13 @@ module Vagrant
# with a chef server. # with a chef server.
class ChefServer < Chef class ChefServer < Chef
def prepare def prepare
if Vagrant.config.chef.validation_key_path.nil? if env.config.chef.validation_key_path.nil?
raise Actions::ActionException.new(:chef_server_validation_key_required) raise Actions::ActionException.new(:chef_server_validation_key_required)
elsif !File.file?(Vagrant.config.chef.validation_key_path) elsif !File.file?(env.config.chef.validation_key_path)
raise Actions::ActionException.new(:chef_server_validation_key_doesnt_exist) raise Actions::ActionException.new(:chef_server_validation_key_doesnt_exist)
end end
if Vagrant.config.chef.chef_server_url.nil? if env.config.chef.chef_server_url.nil?
raise Actions::ActionException.new(:chef_server_url_required) raise Actions::ActionException.new(:chef_server_url_required)
end end
end end
@ -26,16 +26,16 @@ module Vagrant
def create_client_key_folder def create_client_key_folder
logger.info "Creating folder to hold client key..." logger.info "Creating folder to hold client key..."
path = Pathname.new(Vagrant.config.chef.client_key_path) path = Pathname.new(env.config.chef.client_key_path)
SSH.execute do |ssh| env.ssh.execute do |ssh|
ssh.exec!("sudo mkdir -p #{path.dirname}") ssh.exec!("sudo mkdir -p #{path.dirname}")
end end
end end
def upload_validation_key def upload_validation_key
logger.info "Uploading chef client validation key..." logger.info "Uploading chef client validation key..."
SSH.upload!(validation_key_path, guest_validation_key_path) env.ssh.upload!(validation_key_path, guest_validation_key_path)
end end
def setup_config def setup_config
@ -43,11 +43,11 @@ module Vagrant
log_level :info log_level :info
log_location STDOUT log_location STDOUT
ssl_verify_mode :verify_none ssl_verify_mode :verify_none
chef_server_url "#{Vagrant.config.chef.chef_server_url}" chef_server_url "#{env.config.chef.chef_server_url}"
validation_client_name "#{Vagrant.config.chef.validation_client_name}" validation_client_name "#{env.config.chef.validation_client_name}"
validation_key "#{guest_validation_key_path}" validation_key "#{guest_validation_key_path}"
client_key "#{Vagrant.config.chef.client_key_path}" client_key "#{env.config.chef.client_key_path}"
file_store_path "/srv/chef/file_store" file_store_path "/srv/chef/file_store"
file_cache_path "/srv/chef/cache" file_cache_path "/srv/chef/cache"
@ -58,13 +58,13 @@ Mixlib::Log::Formatter.show_time = true
solo solo
logger.info "Uploading chef-client configuration script..." logger.info "Uploading chef-client configuration script..."
SSH.upload!(StringIO.new(solo_file), File.join(Vagrant.config.chef.provisioning_path, "client.rb")) env.ssh.upload!(StringIO.new(solo_file), File.join(env.config.chef.provisioning_path, "client.rb"))
end end
def run_chef_client def run_chef_client
logger.info "Running chef-client..." logger.info "Running chef-client..."
SSH.execute do |ssh| env.ssh.execute do |ssh|
ssh.exec!("cd #{Vagrant.config.chef.provisioning_path} && sudo chef-client -c client.rb -j dna.json") do |channel, data, stream| ssh.exec!("cd #{env.config.chef.provisioning_path} && sudo chef-client -c client.rb -j dna.json") do |channel, data, stream|
# TODO: Very verbose. It would be easier to save the data and only show it during # TODO: Very verbose. It would be easier to save the data and only show it during
# an error, or when verbosity level is set high # an error, or when verbosity level is set high
logger.info("#{stream}: #{data}") logger.info("#{stream}: #{data}")
@ -73,11 +73,11 @@ solo
end end
def validation_key_path def validation_key_path
File.expand_path(Vagrant.config.chef.validation_key_path, Env.root_path) File.expand_path(env.config.chef.validation_key_path, env.root_path)
end end
def guest_validation_key_path def guest_validation_key_path
File.join(Vagrant.config.chef.provisioning_path, "validation.pem") File.join(@env.config.chef.provisioning_path, "validation.pem")
end end
end end
end end

View File

@ -15,24 +15,24 @@ module Vagrant
def share_cookbook_folders def share_cookbook_folders
host_cookbook_paths.each_with_index do |cookbook, i| host_cookbook_paths.each_with_index do |cookbook, i|
Vagrant.config.vm.share_folder("vagrant-chef-solo-#{i}", cookbook_path(i), cookbook) env.config.vm.share_folder("vagrant-chef-solo-#{i}", cookbook_path(i), cookbook)
end end
end end
def setup_solo_config def setup_solo_config
solo_file = <<-solo solo_file = <<-solo
file_cache_path "#{Vagrant.config.chef.provisioning_path}" file_cache_path "#{env.config.chef.provisioning_path}"
cookbook_path #{cookbooks_path} cookbook_path #{cookbooks_path}
solo solo
logger.info "Uploading chef-solo configuration script..." logger.info "Uploading chef-solo configuration script..."
SSH.upload!(StringIO.new(solo_file), File.join(Vagrant.config.chef.provisioning_path, "solo.rb")) env.ssh.upload!(StringIO.new(solo_file), File.join(env.config.chef.provisioning_path, "solo.rb"))
end end
def run_chef_solo def run_chef_solo
logger.info "Running chef-solo..." logger.info "Running chef-solo..."
SSH.execute do |ssh| env.ssh.execute do |ssh|
ssh.exec!("cd #{Vagrant.config.chef.provisioning_path} && sudo chef-solo -c solo.rb -j dna.json") do |channel, data, stream| ssh.exec!("cd #{env.config.chef.provisioning_path} && sudo chef-solo -c solo.rb -j dna.json") do |channel, data, stream|
# TODO: Very verbose. It would be easier to save the data and only show it during # TODO: Very verbose. It would be easier to save the data and only show it during
# an error, or when verbosity level is set high # an error, or when verbosity level is set high
logger.info("#{stream}: #{data}") logger.info("#{stream}: #{data}")
@ -41,14 +41,14 @@ solo
end end
def host_cookbook_paths def host_cookbook_paths
cookbooks = Vagrant.config.chef.cookbooks_path cookbooks = env.config.chef.cookbooks_path
cookbooks = [cookbooks] unless cookbooks.is_a?(Array) cookbooks = [cookbooks] unless cookbooks.is_a?(Array)
cookbooks.collect! { |cookbook| File.expand_path(cookbook, Env.root_path) } cookbooks.collect! { |cookbook| File.expand_path(cookbook, env.root_path) }
return cookbooks return cookbooks
end end
def cookbook_path(i) def cookbook_path(i)
File.join(Vagrant.config.chef.provisioning_path, "cookbooks-#{i}") File.join(env.config.chef.provisioning_path, "cookbooks-#{i}")
end end
def cookbooks_path def cookbooks_path

View File

@ -47,7 +47,7 @@ class ProvisionActionTest < Test::Unit::TestCase
@instance.stubs(:prepare) @instance.stubs(:prepare)
@klass = mock("klass") @klass = mock("klass")
@klass.stubs(:is_a?).with(Class).returns(true) @klass.stubs(:is_a?).with(Class).returns(true)
@klass.stubs(:new).returns(@instance) @klass.stubs(:new).with(@runner.env).returns(@instance)
mock_config do |config| mock_config do |config|
config.vm.provisioner = @klass config.vm.provisioner = @klass
@ -55,7 +55,7 @@ class ProvisionActionTest < Test::Unit::TestCase
end end
should "set the provisioner to an instantiation of the class" do should "set the provisioner to an instantiation of the class" do
@klass.expects(:new).once.returns(@instance) @klass.expects(:new).with(@runner.env).once.returns(@instance)
assert_nothing_raised { @action.prepare } assert_nothing_raised { @action.prepare }
assert_equal @instance, @action.provisioner assert_equal @instance, @action.provisioner
end end
@ -81,7 +81,7 @@ class ProvisionActionTest < Test::Unit::TestCase
instance = mock("instance") instance = mock("instance")
instance.expects(:prepare).once instance.expects(:prepare).once
provisioner.expects(:new).returns(instance) provisioner.expects(:new).with(@runner.env).returns(instance)
assert_nothing_raised { @action.prepare } assert_nothing_raised { @action.prepare }
assert_equal instance, @action.provisioner assert_equal instance, @action.provisioner
end end

View File

@ -7,7 +7,13 @@ class BaseProvisionerTest < Test::Unit::TestCase
context "base instance" do context "base instance" do
setup do setup do
@base = Vagrant::Provisioners::Base.new @env = mock_environment
@base = Vagrant::Provisioners::Base.new(@env)
end
should "set the environment" do
base = Vagrant::Provisioners::Base.new(@env)
assert_equal @env, base.env
end end
should "implement provision! which does nothing" do should "implement provision! which does nothing" do

View File

@ -2,10 +2,8 @@ require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
class ChefServerProvisionerTest < Test::Unit::TestCase class ChefServerProvisionerTest < Test::Unit::TestCase
setup do setup do
@action = Vagrant::Provisioners::ChefServer.new @env = mock_environment
@action = Vagrant::Provisioners::ChefServer.new(@env)
Vagrant::SSH.stubs(:execute)
Vagrant::SSH.stubs(:upload!)
mock_config mock_config
end end
@ -29,56 +27,68 @@ class ChefServerProvisionerTest < Test::Unit::TestCase
end end
should "not raise an exception if validation_key_path is set" do should "not raise an exception if validation_key_path is set" do
mock_config do |config| @env = mock_environment do |config|
config.chef.validation_key_path = "7" config.chef.validation_key_path = "7"
end end
@action.stubs(:env).returns(@env)
assert_nothing_raised { @action.prepare } assert_nothing_raised { @action.prepare }
end end
should "raise an exception if validation_key_path is nil" do should "raise an exception if validation_key_path is nil" do
mock_config do |config| @env = mock_environment do |config|
config.chef.validation_key_path = nil config.chef.validation_key_path = nil
end end
@action.stubs(:env).returns(@env)
assert_raises(Vagrant::Actions::ActionException) { assert_raises(Vagrant::Actions::ActionException) {
@action.prepare @action.prepare
} }
end end
should "not raise an exception if validation_key_path does exist" do should "not raise an exception if validation_key_path does exist" do
mock_config do |config| @env = mock_environment do |config|
config.chef.validation_key_path = "7" config.chef.validation_key_path = "7"
end end
File.expects(:file?).with(Vagrant.config.chef.validation_key_path).returns(true) @action.stubs(:env).returns(@env)
File.expects(:file?).with(@env.config.chef.validation_key_path).returns(true)
assert_nothing_raised { @action.prepare } assert_nothing_raised { @action.prepare }
end end
should "raise an exception if validation_key_path doesn't exist" do should "raise an exception if validation_key_path doesn't exist" do
mock_config do |config| @env = mock_environment do |config|
config.chef.validation_key_path = "7" config.chef.validation_key_path = "7"
end end
File.expects(:file?).with(Vagrant.config.chef.validation_key_path).returns(false) @action.stubs(:env).returns(@env)
File.expects(:file?).with(@env.config.chef.validation_key_path).returns(false)
assert_raises(Vagrant::Actions::ActionException) { assert_raises(Vagrant::Actions::ActionException) {
@action.prepare @action.prepare
} }
end end
should "not raise an exception if chef_server_url is set" do should "not raise an exception if chef_server_url is set" do
mock_config do |config| @env = mock_environment do |config|
config.chef.chef_server_url = "7" config.chef.chef_server_url = "7"
end end
@action.stubs(:env).returns(@env)
assert_nothing_raised { @action.prepare } assert_nothing_raised { @action.prepare }
end end
should "raise an exception if chef_server_url is nil" do should "raise an exception if chef_server_url is nil" do
mock_config do |config| @env = mock_environment do |config|
config.chef.chef_server_url = nil config.chef.chef_server_url = nil
end end
@action.stubs(:env).returns(@env)
assert_raises(Vagrant::Actions::ActionException) { assert_raises(Vagrant::Actions::ActionException) {
@action.prepare @action.prepare
} }
@ -88,9 +98,7 @@ class ChefServerProvisionerTest < Test::Unit::TestCase
context "creating the client key folder" do context "creating the client key folder" do
setup do setup do
@raw_path = "/foo/bar/baz.pem" @raw_path = "/foo/bar/baz.pem"
mock_config do |config| @env.config.chef.client_key_path = @raw_path
config.chef.client_key_path = @raw_path
end
@path = Pathname.new(@raw_path) @path = Pathname.new(@raw_path)
end end
@ -98,7 +106,7 @@ class ChefServerProvisionerTest < Test::Unit::TestCase
should "create the folder using the dirname of the path" do should "create the folder using the dirname of the path" do
ssh = mock("ssh") ssh = mock("ssh")
ssh.expects(:exec!).with("sudo mkdir -p #{@path.dirname}").once ssh.expects(:exec!).with("sudo mkdir -p #{@path.dirname}").once
Vagrant::SSH.expects(:execute).yields(ssh) @env.ssh.expects(:execute).yields(ssh)
@action.create_client_key_folder @action.create_client_key_folder
end end
end end
@ -107,7 +115,7 @@ class ChefServerProvisionerTest < Test::Unit::TestCase
should "upload the validation key to the provisioning path" do should "upload the validation key to the provisioning path" do
@action.expects(:validation_key_path).once.returns("foo") @action.expects(:validation_key_path).once.returns("foo")
@action.expects(:guest_validation_key_path).once.returns("bar") @action.expects(:guest_validation_key_path).once.returns("bar")
Vagrant::SSH.expects(:upload!).with("foo", "bar").once @env.ssh.expects(:upload!).with("foo", "bar").once
@action.upload_validation_key @action.upload_validation_key
end end
end end
@ -115,7 +123,7 @@ class ChefServerProvisionerTest < Test::Unit::TestCase
context "the validation key path" do context "the validation key path" do
should "expand the configured key path" do should "expand the configured key path" do
result = mock("result") result = mock("result")
File.expects(:expand_path).with(Vagrant.config.chef.validation_key_path, Vagrant::Env.root_path).once.returns(result) File.expects(:expand_path).with(@env.config.chef.validation_key_path, @env.root_path).once.returns(result)
assert_equal result, @action.validation_key_path assert_equal result, @action.validation_key_path
end end
end end
@ -123,7 +131,7 @@ class ChefServerProvisionerTest < Test::Unit::TestCase
context "the guest validation key path" do context "the guest validation key path" do
should "be the provisioning path joined with validation.pem" do should "be the provisioning path joined with validation.pem" do
result = mock("result") result = mock("result")
File.expects(:join).with(Vagrant.config.chef.provisioning_path, "validation.pem").once.returns(result) File.expects(:join).with(@env.config.chef.provisioning_path, "validation.pem").once.returns(result)
assert_equal result, @action.guest_validation_key_path assert_equal result, @action.guest_validation_key_path
end end
end end
@ -131,6 +139,8 @@ class ChefServerProvisionerTest < Test::Unit::TestCase
context "generating and uploading chef client configuration file" do context "generating and uploading chef client configuration file" do
setup do setup do
@action.stubs(:guest_validation_key_path).returns("foo") @action.stubs(:guest_validation_key_path).returns("foo")
@env.ssh.stubs(:upload!)
end end
should "upload properly generate the configuration file using configuration data" do should "upload properly generate the configuration file using configuration data" do
@ -138,11 +148,11 @@ class ChefServerProvisionerTest < Test::Unit::TestCase
log_level :info log_level :info
log_location STDOUT log_location STDOUT
ssl_verify_mode :verify_none ssl_verify_mode :verify_none
chef_server_url "#{Vagrant.config.chef.chef_server_url}" chef_server_url "#{@env.config.chef.chef_server_url}"
validation_client_name "#{Vagrant.config.chef.validation_client_name}" validation_client_name "#{@env.config.chef.validation_client_name}"
validation_key "#{@action.guest_validation_key_path}" validation_key "#{@action.guest_validation_key_path}"
client_key "#{Vagrant.config.chef.client_key_path}" client_key "#{@env.config.chef.client_key_path}"
file_store_path "/srv/chef/file_store" file_store_path "/srv/chef/file_store"
file_cache_path "/srv/chef/cache" file_cache_path "/srv/chef/cache"
@ -158,8 +168,8 @@ config
should "upload this file as client.rb to the provisioning folder" do should "upload this file as client.rb to the provisioning folder" do
StringIO.expects(:new).returns("foo") StringIO.expects(:new).returns("foo")
File.expects(:join).with(Vagrant.config.chef.provisioning_path, "client.rb").once.returns("bar") File.expects(:join).with(@env.config.chef.provisioning_path, "client.rb").once.returns("bar")
Vagrant::SSH.expects(:upload!).with("foo", "bar").once @env.ssh.expects(:upload!).with("foo", "bar").once
@action.setup_config @action.setup_config
end end
end end
@ -167,8 +177,8 @@ config
context "running chef client" do context "running chef client" do
should "cd into the provisioning directory and run chef client" do should "cd into the provisioning directory and run chef client" do
ssh = mock("ssh") ssh = mock("ssh")
ssh.expects(:exec!).with("cd #{Vagrant.config.chef.provisioning_path} && sudo chef-client -c client.rb -j dna.json").once ssh.expects(:exec!).with("cd #{@env.config.chef.provisioning_path} && sudo chef-client -c client.rb -j dna.json").once
Vagrant::SSH.expects(:execute).yields(ssh) @env.ssh.expects(:execute).yields(ssh)
@action.run_chef_client @action.run_chef_client
end end
end end

View File

@ -2,7 +2,8 @@ require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
class ChefSoloProvisionerTest < Test::Unit::TestCase class ChefSoloProvisionerTest < Test::Unit::TestCase
setup do setup do
@action = Vagrant::Provisioners::ChefSolo.new @env = mock_environment
@action = Vagrant::Provisioners::ChefSolo.new(@env)
Vagrant::SSH.stubs(:execute) Vagrant::SSH.stubs(:execute)
Vagrant::SSH.stubs(:upload!) Vagrant::SSH.stubs(:upload!)
@ -37,7 +38,7 @@ class ChefSoloProvisionerTest < Test::Unit::TestCase
should "share each cookbook folder" do should "share each cookbook folder" do
share_seq = sequence("share_seq") share_seq = sequence("share_seq")
@host_cookbook_paths.each_with_index do |cookbook, i| @host_cookbook_paths.each_with_index do |cookbook, i|
Vagrant.config.vm.expects(:share_folder).with("vagrant-chef-solo-#{i}", @action.cookbook_path(i), cookbook).in_sequence(share_seq) @env.config.vm.expects(:share_folder).with("vagrant-chef-solo-#{i}", @action.cookbook_path(i), cookbook).in_sequence(share_seq)
end end
@action.share_cookbook_folders @action.share_cookbook_folders
@ -45,38 +46,19 @@ class ChefSoloProvisionerTest < Test::Unit::TestCase
end end
context "host cookbooks paths" do context "host cookbooks paths" do
should "expand the path of the cookbooks relative to the environment root path" do
@cookbook = "foo"
@expanded = "bar"
File.expects(:expand_path).with(@cookbook, Vagrant::Env.root_path).returns(@expanded)
mock_config do |config|
config.chef.cookbooks_path = @cookbook
end
assert_equal [@expanded], @action.host_cookbook_paths
end
should "return as an array if was originally a string" do should "return as an array if was originally a string" do
File.stubs(:expand_path).returns("foo") File.stubs(:expand_path).returns("foo")
@env.config.chef.cookbooks_path = "foo"
mock_config do |config|
config.chef.cookbooks_path = "foo"
end
assert_equal ["foo"], @action.host_cookbook_paths assert_equal ["foo"], @action.host_cookbook_paths
end end
should "return the array of cookbooks if its an array" do should "return the array of cookbooks if its an array" do
cookbooks = ["foo", "bar"] cookbooks = ["foo", "bar"]
mock_config do |config| @env.config.chef.cookbooks_path = cookbooks
config.chef.cookbooks_path = cookbooks
end
expand_seq = sequence('expand_seq') expand_seq = sequence('expand_seq')
cookbooks.each do |cookbook| cookbooks.collect! { |cookbook| File.expand_path(cookbook, @env.root_path) }
File.expects(:expand_path).with(cookbook, Vagrant::Env.root_path).returns(cookbook)
end
assert_equal cookbooks, @action.host_cookbook_paths assert_equal cookbooks, @action.host_cookbook_paths
end end
@ -84,7 +66,7 @@ class ChefSoloProvisionerTest < Test::Unit::TestCase
context "cookbooks path" do context "cookbooks path" do
should "return a proper path to a single cookbook" do should "return a proper path to a single cookbook" do
expected = File.join(Vagrant.config.chef.provisioning_path, "cookbooks-5") expected = File.join(@env.config.chef.provisioning_path, "cookbooks-5")
assert_equal expected, @action.cookbook_path(5) assert_equal expected, @action.cookbook_path(5)
end end
@ -93,28 +75,26 @@ class ChefSoloProvisionerTest < Test::Unit::TestCase
acc << @action.cookbook_path(i) acc << @action.cookbook_path(i)
end end
mock_config do |config| @env.config.chef.cookbooks_path = @cookbooks
config.chef.cookbooks_path = @cookbooks
end
assert_equal @cookbooks.to_json, @action.cookbooks_path assert_equal @cookbooks.to_json, @action.cookbooks_path
end end
should "return a single string representation if cookbook paths is single" do should "return a single string representation if cookbook paths is single" do
@cookbooks = @action.cookbook_path(0) @cookbooks = @action.cookbook_path(0)
mock_config do |config| @env.config.chef.cookbooks_path = @cookbooks
config.chef.cookbooks_path = @cookbooks
end
assert_equal @cookbooks.to_json, @action.cookbooks_path assert_equal @cookbooks.to_json, @action.cookbooks_path
end end
end end
context "generating and uploading chef solo configuration file" do context "generating and uploading chef solo configuration file" do
setup do
@env.ssh.stubs(:upload!)
end
should "upload properly generate the configuration file using configuration data" do should "upload properly generate the configuration file using configuration data" do
expected_config = <<-config expected_config = <<-config
file_cache_path "#{Vagrant.config.chef.provisioning_path}" file_cache_path "#{@env.config.chef.provisioning_path}"
cookbook_path #{@action.cookbooks_path} cookbook_path #{@action.cookbooks_path}
config config
@ -125,8 +105,8 @@ config
should "upload this file as solo.rb to the provisioning folder" do should "upload this file as solo.rb to the provisioning folder" do
@action.expects(:cookbooks_path).returns("cookbooks") @action.expects(:cookbooks_path).returns("cookbooks")
StringIO.expects(:new).returns("foo") StringIO.expects(:new).returns("foo")
File.expects(:join).with(Vagrant.config.chef.provisioning_path, "solo.rb").once.returns("bar") File.expects(:join).with(@env.config.chef.provisioning_path, "solo.rb").once.returns("bar")
Vagrant::SSH.expects(:upload!).with("foo", "bar").once @env.ssh.expects(:upload!).with("foo", "bar").once
@action.setup_solo_config @action.setup_solo_config
end end
end end
@ -134,8 +114,8 @@ config
context "running chef solo" do context "running chef solo" do
should "cd into the provisioning directory and run chef solo" do should "cd into the provisioning directory and run chef solo" do
ssh = mock("ssh") ssh = mock("ssh")
ssh.expects(:exec!).with("cd #{Vagrant.config.chef.provisioning_path} && sudo chef-solo -c solo.rb -j dna.json").once ssh.expects(:exec!).with("cd #{@env.config.chef.provisioning_path} && sudo chef-solo -c solo.rb -j dna.json").once
Vagrant::SSH.expects(:execute).yields(ssh) @env.ssh.expects(:execute).yields(ssh)
@action.run_chef_solo @action.run_chef_solo
end end
end end

View File

@ -2,7 +2,8 @@ require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
class ChefProvisionerTest < Test::Unit::TestCase class ChefProvisionerTest < Test::Unit::TestCase
setup do setup do
@action = Vagrant::Provisioners::Chef.new @env = mock_environment
@action = Vagrant::Provisioners::Chef.new(@env)
Vagrant::SSH.stubs(:execute) Vagrant::SSH.stubs(:execute)
Vagrant::SSH.stubs(:upload!) Vagrant::SSH.stubs(:upload!)
@ -69,16 +70,16 @@ class ChefProvisionerTest < Test::Unit::TestCase
should "create and chown the folder to the ssh user" do should "create and chown the folder to the ssh user" do
ssh_seq = sequence("ssh_seq") ssh_seq = sequence("ssh_seq")
ssh = mock("ssh") ssh = mock("ssh")
ssh.expects(:exec!).with("sudo mkdir -p #{Vagrant.config.chef.provisioning_path}").once.in_sequence(ssh_seq) ssh.expects(:exec!).with("sudo mkdir -p #{@env.config.chef.provisioning_path}").once.in_sequence(ssh_seq)
ssh.expects(:exec!).with("sudo chown #{Vagrant.config.ssh.username} #{Vagrant.config.chef.provisioning_path}").once.in_sequence(ssh_seq) ssh.expects(:exec!).with("sudo chown #{@env.config.ssh.username} #{@env.config.chef.provisioning_path}").once.in_sequence(ssh_seq)
Vagrant::SSH.expects(:execute).yields(ssh) @env.ssh.expects(:execute).yields(ssh)
@action.chown_provisioning_folder @action.chown_provisioning_folder
end end
end end
context "generating and uploading json" do context "generating and uploading json" do
def assert_json def assert_json
Vagrant::SSH.expects(:upload!).with do |json, path| @env.ssh.expects(:upload!).with do |json, path|
data = JSON.parse(json.read) data = JSON.parse(json.read)
yield data yield data
true true
@ -88,7 +89,7 @@ class ChefProvisionerTest < Test::Unit::TestCase
end end
should "merge in the extra json specified in the config" do should "merge in the extra json specified in the config" do
Vagrant.config.chef.json = { :foo => "BAR" } @env.config.chef.json = { :foo => "BAR" }
assert_json do |data| assert_json do |data|
assert_equal "BAR", data["foo"] assert_equal "BAR", data["foo"]
end end
@ -96,20 +97,20 @@ class ChefProvisionerTest < Test::Unit::TestCase
should "add the directory as a special case to the JSON" do should "add the directory as a special case to the JSON" do
assert_json do |data| assert_json do |data|
assert_equal Vagrant.config.vm.project_directory, data["vagrant"]["directory"] assert_equal @env.config.vm.project_directory, data["vagrant"]["directory"]
end end
end end
should "add the config to the JSON" do should "add the config to the JSON" do
assert_json do |data| assert_json do |data|
assert_equal Vagrant.config.vm.project_directory, data["vagrant"]["config"]["vm"]["project_directory"] assert_equal @env.config.vm.project_directory, data["vagrant"]["config"]["vm"]["project_directory"]
end end
end end
should "upload a StringIO to dna.json" do should "upload a StringIO to dna.json" do
StringIO.expects(:new).with(anything).returns("bar") StringIO.expects(:new).with(anything).returns("bar")
File.expects(:join).with(Vagrant.config.chef.provisioning_path, "dna.json").once.returns("baz") File.expects(:join).with(@env.config.chef.provisioning_path, "dna.json").once.returns("baz")
Vagrant::SSH.expects(:upload!).with("bar", "baz").once @env.ssh.expects(:upload!).with("bar", "baz").once
@action.setup_json @action.setup_json
end end
end end