Class: Testbot::Requester::Requester

Inherits:
Object
  • Object
show all
Defined in:
lib/requester/requester.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Requester

Returns a new instance of Requester.



23
24
25
26
27
28
29
30
# File 'lib/requester/requester.rb', line 23

def initialize(config = {})
  config = config.symbolize_keys_without_active_support
  config[:rsync_path]             ||= Testbot::DEFAULT_SERVER_PATH
  config[:project]                ||= Testbot::DEFAULT_PROJECT
  config[:server_user]            ||= Testbot::DEFAULT_USER
  config[:available_runner_usage] ||= Testbot::DEFAULT_RUNNER_USAGE
  @config = OpenStruct.new(config)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



21
22
23
# File 'lib/requester/requester.rb', line 21

def config
  @config
end

Class Method Details

.create_by_config(path) ⇒ Object



133
134
135
# File 'lib/requester/requester.rb', line 133

def self.create_by_config(path)
  Requester.new(YAML.load(ERB.new(File.open(path).read).result))
end

Instance Method Details

#run_tests(adapter, dir) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/requester/requester.rb', line 32

def run_tests(adapter, dir)
  puts if config.simple_output || config.logging

  if config.ssh_tunnel
    log "Setting up ssh tunnel" do
      SSHTunnel.new(config.server_host, config.server_user, adapter.requester_port).open
    end
    server_uri = "http://127.0.0.1:#{adapter.requester_port}"
  else
    server_uri = "http://#{config.server_host}:#{Testbot::SERVER_PORT}"
  end

  log "Syncing files" do
    rsync_ignores = config.rsync_ignores.to_s.split.map { |pattern| "--exclude='#{pattern}'" }.join(' ')
    system("rsync -az --delete --delete-excluded -e ssh #{rsync_ignores} . #{rsync_uri}")

    exitstatus = $?.exitstatus
    unless exitstatus == 0
      puts "rsync failed with exit code #{exitstatus}"
      exit 1
    end
  end

  files = adapter.test_files(dir)
  sizes = adapter.get_sizes(files)

  build_id = nil
  log "Requesting run" do
    response = HTTParty.post("#{server_uri}/builds", :body => { :root => root,
                             :type => adapter.type.to_s,
                             :project => config.project,
                             :available_runner_usage => config.available_runner_usage,
                             :files => files.join(' '),
                             :sizes => sizes.join(' '),
                             :jruby => jruby? }).response

    if response.code == "503"
      puts "No runners available. If you just started a runner, try again. It usually takes a few seconds before they're available."
      return false
    elsif response.code != "200"
      puts "Could not create build, #{response.code}: #{response.body}"
      return false
    else
      build_id = response.body
    end
  end

  at_exit do
    unless ENV['IN_TEST'] || @done
      log "Notifying server we want to stop the run" do
        HTTParty.delete("#{server_uri}/builds/#{build_id}")
      end
    end
  end

  puts if config.logging

  last_results_size = 0
  success = true
  error_count = 0
  while true
    sleep 0.5

    begin
      @build = HTTParty.get("#{server_uri}/builds/#{build_id}", :format => :json)
      next unless @build
    rescue Exception => ex
      error_count += 1
      if error_count > 4
        puts "Failed to get status: #{ex.message}"
        error_count = 0
      end
      next
    end

    results = @build['results'][last_results_size..-1]
    unless results == ''
      if config.simple_output
        print results.gsub(/[^\.F]|Finished/, '')
        STDOUT.flush
      else
        print results
        STDOUT.flush
      end
    end

    last_results_size = @build['results'].size

    break if @build['done']
  end

  puts if config.simple_output

  if adapter.respond_to?(:sum_results)
    puts "\n" + adapter.sum_results(@build['results'])
  end

  @done = true
  @build["success"]
end