Class: TestRunner

Inherits:
Object show all
Defined in:
lib/crazy_ivan/test_runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_path, report_assembler) ⇒ TestRunner

Returns a new instance of TestRunner.



2
3
4
5
6
7
8
9
10
# File 'lib/crazy_ivan/test_runner.rb', line 2

def initialize(project_path, report_assembler)
  @project_path = project_path
  @results = {:project_name => File.basename(@project_path),
              :version => {:output => '', :error => '', :exit_status => ''},
              :update  => {:output => '', :error => '', :exit_status => ''},
              :test    => {:output => '', :error => '', :exit_status => ''},
              :timestamp => {:start => nil, :finish => nil}}
  @report_assembler = report_assembler
end

Instance Attribute Details

#resultsObject (readonly)

Returns the value of attribute results.



12
13
14
# File 'lib/crazy_ivan/test_runner.rb', line 12

def results
  @results
end

Instance Method Details

#check_for_valid_scriptsObject



18
19
20
21
22
23
# File 'lib/crazy_ivan/test_runner.rb', line 18

def check_for_valid_scripts
  check_script('update')
  check_script('version')
  check_script('test')
  check_script('conclusion')
end

#check_script(name) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/crazy_ivan/test_runner.rb', line 29

def check_script(name)
  script_path = script_path(name)
  
  Dir.chdir(@project_path) do
    if File.exists?(script_path)
      if !File.stat(script_path).executable?
        msg = "#{@project_path}/.ci/#{name} script not executable"
        Syslog.warning msg
        abort msg
      elsif File.open(script_path).read.empty?
        msg = "#{@project_path}/.ci/#{name} script empty"
        Syslog.warning msg
        abort msg
      end
    else
      msg = "#{@project_path}/.ci/#{name} script missing"
      Syslog.warning msg
      abort msg
    end
  end
end

#finished?Boolean

Returns:

  • (Boolean)


163
164
165
# File 'lib/crazy_ivan/test_runner.rb', line 163

def finished?
  @results[:timestamp][:finish]
end

#project_nameObject



14
15
16
# File 'lib/crazy_ivan/test_runner.rb', line 14

def project_name
  @results[:project_name]
end

#run_conclusion_scriptObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/crazy_ivan/test_runner.rb', line 111

def run_conclusion_script
  # REFACTOR do this asynchronously so the next tests don't wait on running the conclusion
  
  Dir.chdir(@project_path) do
    Syslog.debug "Passing report to conclusion script at #{script_path('conclusion')}"
    errors = ''
    status = Open4.popen4(script_path('conclusion')) do |pid, stdin, stdout, stderr|
      stdin.puts @results.to_json
      stdin.close
      errors = stderr.read
    end
    
    Syslog.err(errors) if status.exitstatus != '0'
    Syslog.debug "Finished executing conclusion script"
  end
  
rescue Errno::EPIPE
  Syslog.err "Unknown issue in writing to conclusion script."
end

#run_script(name, options = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/crazy_ivan/test_runner.rb', line 51

def run_script(name, options = {})
  output = ''
  error = ''
  exit_status = ''
  
  Dir.chdir(@project_path) do
    Syslog.debug "Opening up the pipe to #{script_path(name)}"
    
    status = Open4::popen4(script_path(name)) do |pid, stdin, stdout, stderr|
      stdin.close  # Close to prevent hanging if the script wants input
      
      until stdout.eof? && stderr.eof? do
        ready_io_streams = select( [stdout], nil, [stderr], 3600 )
        
        script_output = ready_io_streams[0].pop
        script_error = ready_io_streams[2].pop
        
        if script_output && !script_output.eof?
          o = script_output.readpartial(4096)
          print o
          output << o
          
          if options[:stream_test_results?]
            @results[:test][:output] = output
            @report_assembler.update_currently_building(self)
          end
        end
        
        if script_error && !script_error.eof?
          e = script_error.readpartial(4096)
          print e
          error << e
          
          if options[:stream_test_results?]
            @results[:test][:error] = error
            @report_assembler.update_currently_building(self)
          end
        end
        
        # FIXME - this feels like I'm using IO.select wrong
        if script_output.eof? && script_error.nil?
          # there's no more output to SDOUT, and there aren't any errors
          e = stderr.read
          error << e
          print e
          
          if options[:stream_test_results?]
            @results[:test][:error] = error
            @report_assembler.update_currently_building(self)
          end
        end
      end
    end
    
    exit_status = status.exitstatus
  end
  
  return output.chomp, error.chomp, exit_status.to_s
end

#script_path(name) ⇒ Object



25
26
27
# File 'lib/crazy_ivan/test_runner.rb', line 25

def script_path(name)
  script_path = File.join('.ci', name)
end

#start!Object



131
132
133
134
135
136
137
# File 'lib/crazy_ivan/test_runner.rb', line 131

def start!
  # REFACTOR to just report whichever scripts are invalid
  check_for_valid_scripts
  
  @results[:timestamp][:start] = Time.now
  Syslog.info "Starting CI for #{project_name}"
end

#still_building?Boolean

Returns:

  • (Boolean)


167
168
169
# File 'lib/crazy_ivan/test_runner.rb', line 167

def still_building?
  !finished?
end

#test!Object



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/crazy_ivan/test_runner.rb', line 151

def test!
  if @results[:version][:exit_status] == '0'
    Syslog.debug "Testing #{@results[:project_name]} build #{@results[:version][:output]}"
    @results[:test][:output], @results[:test][:error], @results[:test][:exit_status] = run_script('test', :stream_test_results? => true)
  else
    Syslog.debug "Failed to test #{project_name}; version exit status was #{@results[:version][:exit_status]}"
  end
  
  @results[:timestamp][:finish] = Time.now
  run_conclusion_script
end

#update!Object



139
140
141
142
# File 'lib/crazy_ivan/test_runner.rb', line 139

def update!
  Syslog.debug "Updating #{project_name}"
  @results[:update][:output], @results[:update][:error], @results[:update][:exit_status] = run_script('update')
end

#version!Object



144
145
146
147
148
149
# File 'lib/crazy_ivan/test_runner.rb', line 144

def version!
  if @results[:update][:exit_status] == '0'
    Syslog.debug "Acquiring build version for #{project_name}"
    @results[:version][:output], @results[:version][:error], @results[:version][:exit_status] = run_script('version')
  end
end