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