Module: Inferno::Utils::ExecutionScriptRunner

Defined in:
lib/inferno/utils/execution_script_runner.rb

Class Method Summary collapse

Class Method Details

.determine_known_error_result(config, output) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/inferno/utils/execution_script_runner.rb', line 107

def self.determine_known_error_result(config, output)
  expected_file = File.join(File.dirname(config), "#{File.basename(config, '.yaml')}_expected.json")
  if (output.include?('"errors"') || output.include?('skipping comparison')) && !File.exist?(expected_file)
    puts '=> PASS (known-error script exited with 3 due to expected error before comparison)'
    :pass
  elsif output.include?('Actual results matched expected results? true')
    puts '=> PASS (known-error script exited with 3 and results matched expected)'
    :pass
  else
    puts '=> FAIL (exited with 3 but results did not match expected)'
    :fail
  end
end

.determine_result(config, return_code, output, allow_known_errors) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/inferno/utils/execution_script_runner.rb', line 98

def self.determine_result(config, return_code, output, allow_known_errors)
  known_error = allow_known_errors && File.basename(config, '.yaml').end_with?('_error')
  return determine_known_error_result(config, output) if known_error && !return_code.zero?
  return (:pass.tap { puts '=> PASS' }) if return_code.zero?

  puts "=> FAIL (exit code #{return_code})"
  :fail
end


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/inferno/utils/execution_script_runner.rb', line 121

def self.print_summary(passed, failed)
  puts '=' * 60
  puts "Results: #{passed.length} passed, #{failed.length} failed"
  puts '=' * 60

  if passed.any?
    puts 'Passed:'
    passed.each { |s| puts "  #{s}" }
  end

  return unless failed.any?

  puts 'Failed:'
  failed.each { |s| puts "  #{s}" }
  exit 1
end

.run_all(pattern: 'execution_scripts/**/*.yaml', inferno_base_url: nil, allow_known_errors: false, allow_commands: false, compare_messages: true, compare_result_message: true, poll_interval: 3, default_poll_timeout: 120, only_different_messages: true) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/inferno/utils/execution_script_runner.rb', line 6

def self.run_all(pattern: 'execution_scripts/**/*.yaml', inferno_base_url: nil,
                 allow_known_errors: false, allow_commands: false,
                 compare_messages: true, compare_result_message: true,
                 poll_interval: 3, default_poll_timeout: 120,
                 only_different_messages: true)
  scripts = Dir.glob(pattern)

  if scripts.empty?
    warn "No scripts found matching: #{pattern}"
    exit 1
  end

  puts "Found #{scripts.length} script(s) to run.\n\n"

  passed = []
  failed = []

  scripts.each do |config|
    unless config.end_with?('.yaml', '.yml')
      warn "Skipping non-YAML file: #{config}"
      next
    end

    result = run_script(config, inferno_base_url:, allow_known_errors:, allow_commands:,
                                compare_messages:, compare_result_message:,
                                poll_interval:, default_poll_timeout:,
                                only_different_messages:)
    (result == :pass ? passed : failed) << config

    puts
  end

  print_summary(passed, failed)
end

.run_script(config, inferno_base_url:, allow_known_errors:, allow_commands: false, compare_messages: true, compare_result_message: true, poll_interval: 3, default_poll_timeout: 120, only_different_messages: true) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/inferno/utils/execution_script_runner.rb', line 41

def self.run_script(config, inferno_base_url:, allow_known_errors:, allow_commands: false,
                    compare_messages: true, compare_result_message: true,
                    poll_interval: 3, default_poll_timeout: 120,
                    only_different_messages: true)
  puts '=' * 60
  puts "Running: #{config}"
  puts '=' * 60

  allow_commands ||= File.basename(config, '.yaml').include?('_with_commands')
  cmd = build_command(config, inferno_base_url:, allow_commands:, compare_messages:,
                              compare_result_message:, poll_interval:, default_poll_timeout:,
                              only_different_messages:)
  output, exitstatus = stream_command(*cmd)

  determine_result(config, exitstatus, output, allow_known_errors)
end

.stream_command(*cmd) ⇒ Object

Runs cmd as a subprocess, echoing its combined stdout/stderr to the console line-by-line as it's produced (rather than buffering until the process exits), while still returning the full captured output for result determination.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/inferno/utils/execution_script_runner.rb', line 82

def self.stream_command(*cmd)
  output = +''
  exitstatus = nil

  Open3.popen2e(*cmd) do |stdin, stdout_and_stderr, wait_thread|
    stdin.close
    stdout_and_stderr.each_line do |line|
      puts line
      output << line
    end
    exitstatus = wait_thread.value.exitstatus
  end

  [output, exitstatus]
end