2015-06-07 15:09:02 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
# copyright: 2015, Dominik Richter
|
|
|
|
# license: All rights reserved
|
2015-06-07 19:41:54 +00:00
|
|
|
require 'vulcano/base_rule'
|
2015-06-07 15:09:02 +00:00
|
|
|
|
2015-06-19 18:11:26 +00:00
|
|
|
module Vulcano
|
|
|
|
class Rule < VulcanoBaseRule
|
|
|
|
include Serverspec::Helper::Type
|
|
|
|
extend Serverspec::Helper::Type
|
|
|
|
include RSpec::Core::DSL
|
|
|
|
|
|
|
|
# Override RSpec methods to add
|
|
|
|
# IDs to each example group
|
|
|
|
# TODO: remove this once IDs are in rspec-core
|
|
|
|
def describe(sth, &block)
|
2015-06-19 19:17:56 +00:00
|
|
|
@checks ||= []
|
|
|
|
@checks.push(['describe', [sth], block])
|
2015-06-19 18:11:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# redirect all regular method calls to the
|
|
|
|
# core DSL (which is attached to the class)
|
|
|
|
def method_missing(m, *a, &b)
|
|
|
|
VulcanoRule.__send__(m, *a, &b)
|
|
|
|
end
|
|
|
|
|
2015-06-07 15:09:02 +00:00
|
|
|
end
|
2015-06-19 18:11:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
module Vulcano::DSL
|
|
|
|
def rule id, &block
|
2015-06-19 19:52:57 +00:00
|
|
|
__register_rule Vulcano::Rule.new(id, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def __register_rule r
|
|
|
|
r
|
2015-06-19 18:11:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def require_rules id, &block
|
2015-06-19 19:52:57 +00:00
|
|
|
::Vulcano::DSL.load_spec_files_for_profile id, false, &block
|
2015-06-07 15:09:02 +00:00
|
|
|
end
|
2015-06-07 17:20:16 +00:00
|
|
|
|
2015-06-19 18:11:26 +00:00
|
|
|
def include_rules id, &block
|
2015-06-19 19:52:57 +00:00
|
|
|
::Vulcano::DSL.load_spec_files_for_profile id, true, &block
|
2015-06-18 15:32:40 +00:00
|
|
|
end
|
|
|
|
|
2015-06-19 19:43:04 +00:00
|
|
|
# Register a given rule with RSpec and
|
|
|
|
# let it run. This happens after everything
|
|
|
|
# else is merged in.
|
|
|
|
def self.execute_rule r
|
|
|
|
checks = r.instance_variable_get(:@checks)
|
|
|
|
id = r.instance_variable_get(:@id)
|
|
|
|
checks.each do |m,a,b|
|
|
|
|
cres = ::Vulcano::Rule.__send__(m, *a, &b)
|
|
|
|
if m=='describe'
|
|
|
|
set_rspec_ids(cres, id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-06-07 17:20:16 +00:00
|
|
|
private
|
|
|
|
|
2015-06-19 19:17:56 +00:00
|
|
|
# Attach an ID attribute to the
|
|
|
|
# metadata of all examples
|
|
|
|
# TODO: remove this once IDs are in rspec-core
|
2015-06-19 19:43:04 +00:00
|
|
|
def self.set_rspec_ids(obj, id)
|
2015-06-19 19:17:56 +00:00
|
|
|
obj.examples.each {|ex|
|
|
|
|
ex.metadata[:id] = id
|
|
|
|
}
|
|
|
|
obj.children.each {|c|
|
2015-06-19 19:43:04 +00:00
|
|
|
self.set_rspec_ids(c, id)
|
2015-06-19 19:17:56 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2015-06-19 19:52:57 +00:00
|
|
|
def self.load_spec_files_for_profile id, include_all, &block
|
|
|
|
files = get_spec_files_for_profile id
|
|
|
|
# p "LOAD FILES: #{id} -- #{include_all}"
|
|
|
|
# files.each do |file|
|
|
|
|
# eval(File::read(file), file, 1)
|
|
|
|
# end
|
|
|
|
end
|
|
|
|
|
2015-06-19 19:43:04 +00:00
|
|
|
def self.get_spec_files_for_profile id
|
2015-06-19 18:11:26 +00:00
|
|
|
base_path = '/etc/vulcanosec/tests'
|
|
|
|
path = File.join( base_path, id )
|
|
|
|
# find all files to be included
|
|
|
|
files = []
|
|
|
|
if File::directory? path
|
|
|
|
# include all library paths, if they exist
|
|
|
|
libdir = File::join(path, 'lib')
|
|
|
|
if File::directory? libdir and !$LOAD_PATH.include?(libdir)
|
|
|
|
$LOAD_PATH.unshift(libdir)
|
|
|
|
end
|
|
|
|
files = Dir[File.join(path, 'spec','*_spec.rb')]
|
|
|
|
end
|
|
|
|
return files
|
2015-06-07 17:20:16 +00:00
|
|
|
end
|
|
|
|
|
2015-06-07 15:09:02 +00:00
|
|
|
end
|
2015-06-18 15:32:40 +00:00
|
|
|
|
2015-06-19 19:43:04 +00:00
|
|
|
module Vulcano::GlobalDSL
|
2015-06-19 19:52:57 +00:00
|
|
|
def __register_rule r
|
2015-06-19 19:43:04 +00:00
|
|
|
::Vulcano::DSL.execute_rule(r)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-06-19 18:11:26 +00:00
|
|
|
module Vulcano::DSLHelper
|
|
|
|
def self.bind_dsl(scope)
|
|
|
|
(class << scope; self; end).class_exec do
|
|
|
|
include Vulcano::DSL
|
2015-06-19 19:43:04 +00:00
|
|
|
include Vulcano::GlobalDSL
|
2015-06-18 15:32:40 +00:00
|
|
|
end
|
|
|
|
end
|
2015-06-19 18:11:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
::Vulcano::DSLHelper.bind_dsl(self)
|