Provisioner config object should have access to the top config
This commit is contained in:
parent
fea6c1cf60
commit
8d24f779f0
|
@ -4,9 +4,6 @@ module Vagrant
|
|||
# basic things such as the environment instance variable which all
|
||||
# config classes need as well as a basic `to_json` implementation.
|
||||
class Base
|
||||
# {Environment} that this config belongs to
|
||||
attr_accessor :env
|
||||
|
||||
# {Top} of this configuration stack
|
||||
attr_accessor :top
|
||||
|
||||
|
@ -32,6 +29,14 @@ module Vagrant
|
|||
end
|
||||
end
|
||||
|
||||
# A helper to access the environment that this configuration is for.
|
||||
# This is obtained by getting the env from the {Top}.
|
||||
#
|
||||
# @return [Vagrant::Envrionment]
|
||||
def env
|
||||
top.env
|
||||
end
|
||||
|
||||
# Allows setting options from a hash. By default this simply calls
|
||||
# the `#{key}=` method on the config class with the value, which is
|
||||
# the expected behavior most of the time.
|
||||
|
|
|
@ -9,6 +9,9 @@ module Vagrant
|
|||
class Top < Base
|
||||
@@configures = {} if !defined?(@@configures)
|
||||
|
||||
# The environment that this configuration is for.
|
||||
attr_reader :env
|
||||
|
||||
class << self
|
||||
# The list of registered configuration classes as well as the key
|
||||
# they're registered under.
|
||||
|
@ -27,7 +30,6 @@ module Vagrant
|
|||
def initialize(env=nil)
|
||||
self.class.configures_list.each do |key, klass|
|
||||
config = klass.new
|
||||
config.env = env
|
||||
config.top = self
|
||||
instance_variable_set("@#{key}".to_sym, config)
|
||||
end
|
||||
|
|
|
@ -61,7 +61,7 @@ module Vagrant
|
|||
end
|
||||
|
||||
def provision(name, options=nil, &block)
|
||||
@provisioners << Provisioner.new(name, options, &block)
|
||||
@provisioners << Provisioner.new(top, name, options, &block)
|
||||
end
|
||||
|
||||
# This shows an error message to smooth the transition for the
|
||||
|
|
|
@ -3,11 +3,13 @@ module Vagrant
|
|||
class VMConfig < Base
|
||||
# Represents a single configured provisioner for a VM.
|
||||
class Provisioner
|
||||
attr_reader :top
|
||||
attr_reader :shortcut
|
||||
attr_reader :provisioner
|
||||
attr_reader :config
|
||||
|
||||
def initialize(shortcut, options=nil, &block)
|
||||
def initialize(top, shortcut, options=nil, &block)
|
||||
@top = top
|
||||
@shortcut = shortcut
|
||||
@provisioner = shortcut
|
||||
@provisioner = Provisioners::Base.registered[shortcut] if shortcut.is_a?(Symbol)
|
||||
|
@ -29,6 +31,7 @@ module Vagrant
|
|||
|
||||
# Instantiate the config class and configure it
|
||||
@config = @provisioner.const_get(*const_args).new
|
||||
@config.top = top
|
||||
@config.set_options(options) if options
|
||||
block.call(@config) if block
|
||||
end
|
||||
|
|
|
@ -42,12 +42,6 @@ class ConfigBaseTest < Test::Unit::TestCase
|
|||
assert_equal @json, @base.to_json
|
||||
end
|
||||
|
||||
should "not include env in the JSON hash" do
|
||||
@base.env = "FOO"
|
||||
hash = @base.instance_variables_hash
|
||||
assert !hash.has_key?(:env)
|
||||
end
|
||||
|
||||
should "not include top in the JSON hash" do
|
||||
@base.top = "FOO"
|
||||
hash = @base.instance_variables_hash
|
||||
|
|
|
@ -3,36 +3,47 @@ require "test_helper"
|
|||
class ConfigVMProvisionerTest < Test::Unit::TestCase
|
||||
setup do
|
||||
@klass = Vagrant::Config::VMConfig::Provisioner
|
||||
@top = Vagrant::Config::Top.new(nil)
|
||||
end
|
||||
|
||||
context "initializing" do
|
||||
should "expose the top instance that the provisioner belongs to" do
|
||||
instance = @klass.new(@top, :chef_solo)
|
||||
assert_equal @top, instance.top
|
||||
end
|
||||
|
||||
should "expose the shortcut used" do
|
||||
instance = @klass.new(:chef_solo)
|
||||
instance = @klass.new(@top, :chef_solo)
|
||||
assert_equal :chef_solo, instance.shortcut
|
||||
end
|
||||
|
||||
should "expose the provisioner class if its a valid shortcut" do
|
||||
instance = @klass.new(:chef_solo)
|
||||
instance = @klass.new(@top, :chef_solo)
|
||||
assert_equal Vagrant::Provisioners::ChefSolo, instance.provisioner
|
||||
end
|
||||
|
||||
should "expose the provisioner class if its a valid class" do
|
||||
instance = @klass.new(Vagrant::Provisioners::ChefSolo)
|
||||
instance = @klass.new(@top, Vagrant::Provisioners::ChefSolo)
|
||||
assert_equal Vagrant::Provisioners::ChefSolo, instance.provisioner
|
||||
end
|
||||
|
||||
should "have a nil provisioner class if invalid" do
|
||||
instance = @klass.new(:i_shall_never_exist)
|
||||
instance = @klass.new(@top, :i_shall_never_exist)
|
||||
assert_nil instance.provisioner
|
||||
end
|
||||
|
||||
should "have a nil config instance if invalid" do
|
||||
instance = @klass.new(:i_shall_never_exist)
|
||||
instance = @klass.new(@top, :i_shall_never_exist)
|
||||
assert_nil instance.config
|
||||
end
|
||||
|
||||
should "set the top of the config object to the given top" do
|
||||
instance = @klass.new(@top, :chef_solo)
|
||||
assert_equal @top, instance.config.top
|
||||
end
|
||||
|
||||
should "configure the provisioner if valid" do
|
||||
instance = @klass.new(:chef_solo) do |chef|
|
||||
instance = @klass.new(@top, :chef_solo) do |chef|
|
||||
chef.cookbooks_path = "foo"
|
||||
end
|
||||
|
||||
|
@ -40,7 +51,7 @@ class ConfigVMProvisionerTest < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
should "configure the provisioner with a hash if valid" do
|
||||
instance = @klass.new(:chef_solo, :cookbooks_path => "foo")
|
||||
instance = @klass.new(@top, :chef_solo, :cookbooks_path => "foo")
|
||||
assert_equal "foo", instance.config.cookbooks_path
|
||||
end
|
||||
end
|
||||
|
@ -51,20 +62,20 @@ class ConfigVMProvisionerTest < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
should "be invalid if provisioner is valid" do
|
||||
instance = @klass.new(:i_shall_never_exist)
|
||||
instance = @klass.new(@top, :i_shall_never_exist)
|
||||
instance.validate(@errors)
|
||||
assert !@errors.errors.empty?
|
||||
end
|
||||
|
||||
should "be invalid if provisioner doesn't inherit from provisioners base" do
|
||||
klass = Class.new
|
||||
instance = @klass.new(klass)
|
||||
instance = @klass.new(@top, klass)
|
||||
instance.validate(@errors)
|
||||
assert !@errors.errors.empty?
|
||||
end
|
||||
|
||||
should "be valid with a valid provisioner" do
|
||||
instance = @klass.new(:chef_solo) do |chef|
|
||||
instance = @klass.new(@top, :chef_solo) do |chef|
|
||||
chef.add_recipe "foo"
|
||||
end
|
||||
|
||||
|
@ -73,7 +84,7 @@ class ConfigVMProvisionerTest < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
should "be invalid if a provisioner's config is invalid" do
|
||||
instance = @klass.new(:chef_solo)
|
||||
instance = @klass.new(@top, :chef_solo)
|
||||
instance.validate(@errors)
|
||||
assert !@errors.errors.empty?
|
||||
end
|
||||
|
|
|
@ -21,7 +21,6 @@ class ConfigTest < Test::Unit::TestCase
|
|||
|
||||
context "with an instance" do
|
||||
setup do
|
||||
# @env = vagrant_env
|
||||
@instance = @klass.new
|
||||
end
|
||||
|
||||
|
@ -112,7 +111,6 @@ class ConfigTest < Test::Unit::TestCase
|
|||
key = "key#{i}"
|
||||
klass = mock("klass#{i}")
|
||||
instance = mock("instance#{i}")
|
||||
instance.expects(:env=).with(env)
|
||||
instance.expects(:top=).with() do |top|
|
||||
assert top.is_a?(@klass::Top)
|
||||
true
|
||||
|
@ -128,7 +126,6 @@ class ConfigTest < Test::Unit::TestCase
|
|||
key = "my_foo_bar_key"
|
||||
klass = mock("klass")
|
||||
instance = mock("instance")
|
||||
instance.stubs(:env=)
|
||||
instance.stubs(:top=)
|
||||
klass.expects(:new).returns(instance)
|
||||
@klass::Top.configures(key, klass)
|
||||
|
|
Loading…
Reference in New Issue