Welcome to mirror list, hosted at ThFree Co, Russian Federation.

run.rb « support « qa « qa - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 242293f9eefeee6c58409352977d991d8b2b7af5 (plain)
1
2
3
4
5
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
40
41
42
43
44
45
46
47
# frozen_string_literal: true

require 'open3'

module QA
  module Support
    module Run
      include QA::Support::Repeater

      CommandError = Class.new(StandardError)

      Result = Struct.new(:command, :exitstatus, :response) do
        alias_method :to_s, :response

        def success?
          exitstatus == 0 && !response.include?('Error encountered')
        end

        def to_i
          response.to_i
        end
      end

      def run(command_str, env: [], max_attempts: 1, log_prefix: '')
        command = [*env, command_str, '2>&1'].compact.join(' ')
        result = nil

        repeat_until(max_attempts: max_attempts, raise_on_failure: false) do
          Runtime::Logger.debug "#{log_prefix}pwd=[#{Dir.pwd}], command=[#{command}]"
          output, status = Open3.capture2e(command)
          output.chomp!
          Runtime::Logger.debug "#{log_prefix}output=[#{output}], exitstatus=[#{status.exitstatus}]"

          result = Result.new(command, status.exitstatus, output)

          result.success?
        end

        unless result.success?
          raise CommandError, "The command #{result.command} failed (#{result.exitstatus}) with the following output:\n#{result.response}"
        end

        result
      end
    end
  end
end