2015-06-07 21:41:54 +02:00
|
|
|
# encoding: utf-8
|
|
|
|
# copyright: 2015, Dominik Richter
|
|
|
|
# license: All rights reserved
|
2015-06-19 16:45:36 +02:00
|
|
|
require 'verify/specfile'
|
2015-06-19 15:06:44 +02:00
|
|
|
require 'vulcano/log'
|
2015-06-07 21:41:54 +02:00
|
|
|
|
|
|
|
module Vulcano
|
|
|
|
# Handle Vulcano Profiles
|
|
|
|
class Profiles
|
|
|
|
attr_reader :profiles
|
2015-09-03 20:43:58 +02:00
|
|
|
def initialize(opts = {})
|
2015-06-07 21:41:54 +02:00
|
|
|
@profiles = {}
|
2015-06-25 17:45:46 +02:00
|
|
|
@profile_id = opts[:id]
|
2015-06-07 21:41:54 +02:00
|
|
|
@log = Log.new(opts)
|
|
|
|
end
|
|
|
|
|
2015-09-03 20:43:58 +02:00
|
|
|
def add_folder(f)
|
2015-09-05 16:07:54 +02:00
|
|
|
path = File.expand_path(f)
|
2015-06-07 21:41:54 +02:00
|
|
|
if File.directory? path
|
|
|
|
add_specs_in_folder path
|
|
|
|
else
|
|
|
|
@log.error "Path is not a folder: #{path}"
|
|
|
|
end
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2015-09-03 20:43:58 +02:00
|
|
|
def valid_folder?(f)
|
2015-09-05 16:07:54 +02:00
|
|
|
path = File.expand_path(f)
|
2015-06-07 21:41:54 +02:00
|
|
|
if !File.directory? path
|
|
|
|
return @log.error "This is not a folder: #{path}"
|
|
|
|
else
|
2015-09-03 23:18:28 +02:00
|
|
|
@log.ok 'Valid directory'
|
2015-06-07 21:41:54 +02:00
|
|
|
end
|
|
|
|
|
2015-06-25 17:45:46 +02:00
|
|
|
metadata = Metadata.for_path(path, @profile_id)
|
2015-09-03 23:18:28 +02:00
|
|
|
@log.ok 'vmetadata.rb' unless metadata.nil? or !metadata.valid?
|
2015-06-07 21:41:54 +02:00
|
|
|
|
|
|
|
specs = Dir["#{path}/spec/*_spec.rb"]
|
|
|
|
if specs.empty?
|
|
|
|
@log.warn "No tests found in #{path}"
|
|
|
|
end
|
2015-09-05 16:07:54 +02:00
|
|
|
specs.each { |s| valid_spec? s, metadata }
|
2015-06-07 21:41:54 +02:00
|
|
|
end
|
|
|
|
|
2015-09-03 20:43:58 +02:00
|
|
|
def valid_spec?(f, metadata)
|
2015-09-05 16:07:54 +02:00
|
|
|
return @log.error "Can't find spec file #{f}" unless File.file? f
|
2015-06-20 01:36:27 +02:00
|
|
|
# validation tracking
|
2015-06-07 21:41:54 +02:00
|
|
|
valid = true
|
2015-07-27 17:31:08 +02:00
|
|
|
invalid = lambda {|type, msg|
|
2015-09-05 16:07:54 +02:00
|
|
|
@log.send type, "#{msg} (#{File.basename f})"
|
2015-07-27 17:31:08 +02:00
|
|
|
valid = false if type == :error
|
2015-06-20 01:36:27 +02:00
|
|
|
}
|
|
|
|
# Load the spec file
|
|
|
|
specs = SpecFile.from_file(f, metadata)
|
|
|
|
# find all errors during parsing
|
|
|
|
specs.errors.each do |err|
|
2015-08-02 23:39:43 -07:00
|
|
|
invalid.(:error, err)
|
2015-06-20 01:36:27 +02:00
|
|
|
end
|
|
|
|
# detect missing metadata
|
|
|
|
meta = specs.metadata
|
|
|
|
if meta['title'].nil?
|
2015-09-05 16:07:54 +02:00
|
|
|
invalid.(:warn, 'Missing title in spec file')
|
2015-06-07 21:41:54 +02:00
|
|
|
end
|
|
|
|
if meta['copyright'].nil?
|
2015-09-05 16:07:54 +02:00
|
|
|
invalid.(:warn, 'Missing copyright in spec file')
|
2015-06-07 21:41:54 +02:00
|
|
|
end
|
2015-06-20 01:36:27 +02:00
|
|
|
# detect empty rules
|
2015-06-16 23:20:24 +02:00
|
|
|
unless meta['rules'][''].nil?
|
2015-09-05 16:07:54 +02:00
|
|
|
invalid.(:error, 'Please configure IDs for all rules.')
|
2015-06-07 21:41:54 +02:00
|
|
|
end
|
|
|
|
|
2015-06-16 23:20:24 +02:00
|
|
|
meta['rules'].each do |k,v|
|
2015-06-10 17:36:30 +02:00
|
|
|
if v['impact'].nil?
|
2015-07-27 17:31:08 +02:00
|
|
|
invalid.(:error, "Missing impact for rule #{k}")
|
2015-06-10 17:36:30 +02:00
|
|
|
else
|
2015-07-27 17:31:08 +02:00
|
|
|
invalid.(:error, "Impact cannot be larger than 1.0 for rule #{k}") if v['impact'] > 1.0
|
|
|
|
invalid.(:error, "Impact cannot be less than 0.0 for rule #{k}") if v['impact'] < 0.0
|
2015-06-10 17:36:30 +02:00
|
|
|
end
|
2015-07-27 17:31:08 +02:00
|
|
|
invalid.(:warn, "Missing title for rule #{k}") if v['title'].nil?
|
|
|
|
invalid.(:warn, "Missing description for rule #{k}") if v['desc'].nil?
|
2015-06-07 21:41:54 +02:00
|
|
|
end
|
|
|
|
|
2015-06-16 23:33:27 +02:00
|
|
|
if valid && specs.instance_variable_get(:@invalid_calls).empty?
|
|
|
|
@log.ok "Valid spec file in #{f} with #{meta['rules'].length} rules"
|
|
|
|
end
|
2015-06-07 21:41:54 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2015-09-03 20:43:58 +02:00
|
|
|
def add_specs_in_folder(path)
|
2015-06-16 23:20:24 +02:00
|
|
|
allrules = {}
|
2015-06-25 17:45:46 +02:00
|
|
|
meta = Metadata.for_path(path, @profile_id, @log)
|
2015-06-07 21:41:54 +02:00
|
|
|
|
|
|
|
Dir["#{path}/spec/*_spec.rb"].each do |specfile|
|
2015-09-05 16:07:54 +02:00
|
|
|
rel_path = specfile.sub(File.join(path, ''), '')
|
2015-06-20 01:41:48 +02:00
|
|
|
specs = SpecFile.from_file(specfile, meta)
|
|
|
|
allrules[rel_path] = sanitize_specfile_json(specs.metadata)
|
2015-06-07 21:41:54 +02:00
|
|
|
end
|
|
|
|
|
2015-06-20 01:41:48 +02:00
|
|
|
meta.dict['rules'] = allrules
|
|
|
|
@profiles = meta.dict
|
2015-06-07 21:41:54 +02:00
|
|
|
end
|
|
|
|
|
2015-09-03 20:43:58 +02:00
|
|
|
def sanitize_specfile_json(j)
|
2015-09-05 16:07:54 +02:00
|
|
|
j['rules'].each do |k, v|
|
2015-06-10 17:21:03 +02:00
|
|
|
v['title'] = k if v['title'].nil?
|
2015-09-05 16:07:54 +02:00
|
|
|
v['desc'] = '' if v['desc'].nil?
|
2015-08-12 23:05:26 -07:00
|
|
|
v['impact'] = 0.5 if v['impact'].nil?
|
2015-06-10 17:21:03 +02:00
|
|
|
end
|
2015-06-12 12:32:10 +02:00
|
|
|
j
|
2015-06-10 17:21:03 +02:00
|
|
|
end
|
2015-06-07 21:41:54 +02:00
|
|
|
end
|
|
|
|
end
|