require_relative 'container/version' require 'yaml' require 'ostruct' require 'json' require 'rake' require 'colorize' module Henry # Henry Container class Container # Nothing to be done here yet... def initialize(opts) @opts = opts end # Executes the loaded tasks and stores their results. def execute self.before_execution self.tasks_results = self.tasks.collect do |task| task.execute(self.task_params(task.name)) end self.update_results self.dump_results end protected # Initial state the conteiner execution results. # # @return [Hash] the results template. RESULTS_TEMPLATE = { summary: { total: 0, breakdownByStatusCode: { 'OK' => 0, 'ERROR' => 0, 'WARNING' => 0 }, tasks: [] } } # Returns the input file path based on the given options. # # @return [String] the input file path. def input_file_path "#{@opts[:"in-dir"]}/#{@opts[:in]}" end # Returns the output file path based on the given options. # # @return [String] the output file path. def output_file_path "#{@opts[:"out-dir"]}/#{@opts[:out]}" end # Executes required validations before execution. def before_execution unless File.exists?(self.input_file_path) abort "#{"'#{self.input_file_path}' file does not exist.".red}\n #{'*'.red} If running isolated make sure to create it first.\n #{'*'.red} If running from Henry contact us at henry@despegar.it." end unless File.exists?('henry-context.yml') abort "'henry-context.yml' file does not exist. You need it in order to execute a Henry compatible test.".red end end # Loads and parses the @opts[:in] params. # # @return [{String => String}] the execution params. def params @params ||= JSON.parse(File.read(self.input_file_path)) end # Returns the default params. # @note Custom task_params will overwrite the defaults. # # @return [Hash] the default params. def default_params @default_params ||= self.params['all'] end # Returns the custom params for the given task. # @note default_params will be used for undefined keys. # # @param [String] task_name the target Task name. # @return [Hash] the Task custom params. def task_params(task_name) self.default_params.merge(self.params[task_name] || {}) end # Load and parses the henry-context.yml params. # # @return [{String => String}] the execution context. def context @context ||= OpenStruct.new(YAML.load_file('henry-context.yml')) end # Returns the tasks to be executed based on the context. # # @return [] tasks to be executed. def tasks @tasks ||= self.context.tasks.collect { |task_data| Task.create(task_data["name"], task_data) } end # Returns the execution's current results. # # @return [Hash] the execution's current resutls. def results @results ||= RESULTS_TEMPLATE end # Returns the tasks_results node from the results hash. # # @return [Array] def tasks_results self.results[:summary][:tasks] end # Serts the tasks_results node of the results hash. # # @param [Array] results the tasks_results collection. def tasks_results=(results) self.results[:summary][:tasks] = results end # Updates the results attributes based on the task_results. def update_results self.tasks_results.each do |task_results| self.results[:summary][:total] += 1 self.results[:summary][:breakdownByStatusCode][task_results[:code]] += 1 end end # Writes into @opts[:out] the execution results. def dump_results File.open(self.output_file_path, 'w') { |f| f.write(results.to_json) } end end end