Project

General

Profile

Download (2.97 KB) Statistics
| Branch: | Tag: | Revision:
d4c3f2c2 Ivan Nečas
module ForemanMaintain
class Scenario
include Concerns::Logger
include Concerns::SystemHelpers
include Concerns::Metadata
d22bc3a1 Ivan Nečas
include Concerns::Finders
d4c3f2c2 Ivan Nečas
attr_reader :steps

276f9729 Ivan Nečas
class FilteredScenario < Scenario
644df26c Ivan Nečas
metadata { manual_detection }
163e3fff Ivan Nečas
276f9729 Ivan Nečas
attr_reader :filter_label, :filter_tags
f608b3cb Ivan Nečas
276f9729 Ivan Nečas
def initialize(filter)
@filter_tags = filter[:tags]
@filter_label = filter[:label]
163e3fff Ivan Nečas
@steps = ForemanMaintain.available_checks(filter).map(&:ensure_instance)
c418295c Swapnil Abnave
@steps = DependencyGraph.sort(@steps)
f608b3cb Ivan Nečas
end

def description
276f9729 Ivan Nečas
if @filter_label
"check with label [#{dashize(@filter_label)}]"
else
"checks with tags #{tag_string(@filter_tags)}"
end
f608b3cb Ivan Nečas
end

private

def tag_string(tags)
276f9729 Ivan Nečas
tags.map { |tag| dashize("[#{tag}]") }.join(' ')
end

def dashize(string)
string.to_s.tr('_', '-')
f608b3cb Ivan Nečas
end
end

644df26c Ivan Nečas
class PreparationScenario < Scenario
734dd76a Ivan Nečas
metadata do
manual_detection
3b966152 Ivan Nečas
description 'preparation steps required to run the next scenarios'
734dd76a Ivan Nečas
end
644df26c Ivan Nečas
attr_reader :main_scenario

def initialize(main_scenario)
@main_scenario = main_scenario
end

def steps
@steps ||= main_scenario.preparation_steps.find_all(&:necessary?)
end
end

d4c3f2c2 Ivan Nečas
def initialize
@steps = []
compose
end

# Override to compose steps for the scenario
ed2c683b Ivan Nečas
def compose; end
ab1bcec2 Ivan Nečas
644df26c Ivan Nečas
def preparation_steps
# we first take the preparation steps defined for the scenario + collect
# preparation steps for the steps inside the scenario
steps.inject(super.dup) do |results, step|
results.concat(step.preparation_steps)
end.uniq
end

181810b0 Ivan Nečas
def steps_with_error(options = {})
filter_whitelisted(steps.find_all(&:fail?), options)
ea60b444 Ivan Nečas
end

181810b0 Ivan Nečas
def steps_with_warning(options = {})
filter_whitelisted(steps.find_all(&:warning?), options)
end

def filter_whitelisted(steps, options)
options.validate_options!(:whitelisted)
if options.key?(:whitelisted)
steps.select do |step|
options[:whitelisted] ? step.whitelisted? : !step.whitelisted?
end
else
steps
end
ea60b444 Ivan Nečas
end

def passed?
181810b0 Ivan Nečas
(steps_with_error(:whitelisted => false) + steps_with_warning(:whitelisted => false)).empty?
ea60b444 Ivan Nečas
end

def failed?
!passed?
end

644df26c Ivan Nečas
# scenarios to be run before this scenario
def before_scenarios
scenarios = []
preparation_scenario = PreparationScenario.new(self)
scenarios << [preparation_scenario] unless preparation_scenario.steps.empty?
scenarios
end

163e3fff Ivan Nečas
def add_steps(steps)
db17af15 Swapnil Abnave
steps.each do |step|
self.steps << step.ensure_instance
end
163e3fff Ivan Nečas
end

def add_step(step)
add_steps([step])
end

ab1bcec2 Ivan Nečas
def self.inspect
"Scenario Class #{metadata[:description]}<#{name}>"
end

def inspect
"#{self.class.metadata[:description]}<#{self.class.name}>"
end
d4c3f2c2 Ivan Nečas
end
end