create new pluggable profile context

Signed-off-by: Dominik Richter <dominik.richter@gmail.com>
This commit is contained in:
Dominik Richter 2015-08-27 13:59:58 -07:00
parent a1af0ad24b
commit 90a2d45462
3 changed files with 77 additions and 40 deletions

View file

@ -0,0 +1,74 @@
require 'vulcano/backend'
module Vulcano
class ProfileContext
attr_reader :rules, :only_ifs
def initialize profile_id, profile_registry, only_ifs
@profile_id = profile_id
@rules = profile_registry
@only_ifs = only_ifs
__CTX = self
backend = Vulcano::Backend::Mock::Runner.new
# This is the heart of the profile context
# An instantiated object which has all resources registered to it
# and exposes them to the a test file.
ctx = Class.new do
include Serverspec::Helper::Type
extend Serverspec::Helper::Type
include Vulcano::DSL
define_method :__register_rule do |*args|
__CTX.register_rule(*args)
end
define_method :__unregister_rule do |*args|
__CTX.unregister_rule(*args)
end
Vulcano::Resources.modules.each do |id, r|
define_method id.to_sym do |*args|
res = Class.new(Vulcano::Resource) do
include r
def initialize(backend, *args)
@vulcano = backend
create(*args)
end
end
res.new(backend, *args)
end
end
def to_s
'Profile Context Run'
end
end
@profile_context = ctx.new
end
def load(content, source, line)
@profile_context.instance_eval(content, source, line)
end
def unregister_rule id
full_id = VulcanoBaseRule::full_id(@profile_id, id)
@rules[full_id] = nil
end
def register_rule r
# get the full ID
full_id = VulcanoBaseRule::full_id(@profile_id, r)
if full_id.nil?
# TODO error
return
end
# add the rule to the registry
existing = @rules[full_id]
if existing.nil?
@rules[full_id] = r
else
VulcanoBaseRule::merge(existing, r)
end
end
end
end

View file

@ -173,43 +173,6 @@ module Vulcano::DSL
end end
module Vulcano
class ProfileContext
include Serverspec::Helper::Type
extend Serverspec::Helper::Type
include Vulcano::DSL
def initialize profile_id, profile_registry, only_ifs
@profile_id = profile_id
@rules = profile_registry
@only_ifs = only_ifs
end
def __unregister_rule id
full_id = VulcanoBaseRule::full_id(@profile_id, id)
@rules[full_id] = nil
end
def __register_rule r
# get the full ID
full_id = VulcanoBaseRule::full_id(@profile_id, r)
if full_id.nil?
# TODO error
return
end
# add the rule to the registry
existing = @rules[full_id]
if existing.nil?
@rules[full_id] = r
else
VulcanoBaseRule::merge(existing, r)
end
end
end
end
module Vulcano::GlobalDSL module Vulcano::GlobalDSL
def __register_rule r def __register_rule r
# make sure the profile id is attached to the rule # make sure the profile id is attached to the rule

View file

@ -5,6 +5,7 @@
require 'uri' require 'uri'
require 'vulcano/backend' require 'vulcano/backend'
require 'vulcano/targets' require 'vulcano/targets'
require 'vulcano/profile_context'
# spec requirements # spec requirements
require 'rspec' require 'rspec'
require 'rspec/its' require 'rspec/its'
@ -49,11 +50,10 @@ module Vulcano
ctx = Vulcano::ProfileContext.new(@profile_id, {}, []) ctx = Vulcano::ProfileContext.new(@profile_id, {}, [])
# evaluate all tests # evaluate all tests
ctx.instance_eval(content, source, line || 1) ctx.load(content, source, line || 1)
# process the resulting rules # process the resulting rules
rules = ctx.instance_variable_get(:@rules) ctx.rules.each do |rule_id, rule|
rules.each do |rule_id, rule|
#::Vulcano::DSL.execute_rule(rule, profile_id) #::Vulcano::DSL.execute_rule(rule, profile_id)
checks = rule.instance_variable_get(:@checks) checks = rule.instance_variable_get(:@checks)
checks.each do |m,a,b| checks.each do |m,a,b|