Class: Hatchet::TestRun

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token:, buildpacks:, app:, pipeline:, api_rate_limit:, timeout: 10, pause: 5, commit_sha: "sha", commit_branch: "master", commit_message: "commit", organization: nil) ⇒ TestRun

Hatchet::GitApp.new(“rails3_mri_193”).run_ci do |test_run|

assert :succeeded, test_run.status

end

TestRun.new(token: , buildpacks: , test_dir: )



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
# File 'lib/hatchet/test_run.rb', line 20

def initialize(
  token:,
  buildpacks:,
  app:,
  pipeline:,
  api_rate_limit:,
  timeout:        10,
  pause:          5,
  commit_sha:     "sha",
  commit_branch:  "master",
  commit_message: "commit",
  organization:    nil
)
  @pipeline        = pipeline || "#{Hatchet::APP_PREFIX}#{SecureRandom.hex(5)}"
  @timeout         = timeout
  @pause           = pause
  @organization    = organization
  @token           = token
  @commit_sha      = commit_sha
  @commit_branch   = commit_branch
  @commit_message  = commit_message
  @buildpacks      = Array(buildpacks)
  @app             = app
  @mutex           = Mutex.new
  @status          = false
  @api_rate_limit  = api_rate_limit
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



47
48
49
# File 'lib/hatchet/test_run.rb', line 47

def app
  @app
end

Instance Method Details

#create_test_runObject



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
# File 'lib/hatchet/test_run.rb', line 49

def create_test_run
  @mutex.synchronize do
    raise "Test is already running" if @status
    @status = :building

    body = {
      source_blob_url: source_blob_url,
      pipeline:        @pipeline,
      organization:    @organization,
      commit_sha:      @commit_sha,
      commit_branch:   @commit_branch,
      commit_message:  @commit_message,
    }

    # https://github.com/heroku/api/blob/master/schema/variants/3.ci/platform-api-reference.md#test-run-create
    attributes = excon_request(
      method:  :post,
      path:    "/test-runs",
      version: "3.ci",
      body:    body,
      expects: [201]
    )
    @test_run_id = attributes["id"]
  end
  info
end

#infoObject



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/hatchet/test_run.rb', line 76

def info
 # GET /test-runs/{test_run_id}
  response = excon_request(
    method:  :get,
    path:    "/test-runs/#{@test_run_id}",
    version: "3.ci",
    expects: [201, 200]
  )

  @status = response["status"].to_sym
end

#outputObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/hatchet/test_run.rb', line 92

def output
  test_nodes = excon_request(
    method:  :get,
    path:    "/test-runs/#{@test_run_id}/test-nodes",
    version: "3.ci",
    expects: [200]
  )

  node_output_urls  = []
  test_nodes.each do |test_node|
    node_output_urls << test_node.fetch("setup_stream_url")
    node_output_urls << test_node.fetch("output_stream_url")
  end

  output = String.new
  node_output_urls.each do |url|
    output << get_contents_or_whatever(url)
  end
  output
end

#source_blob_urlObject

Here’s where the magic happens folks

Set the buildpack

We take the current directory structure and see if it has an ‘app.json` This is how Heroku CI knows what buildpacks to use to run your tests Hatchet will inject whatever buildpack you pass to it, by default it uses the same buildpack you have specified in your Hatchet constants and uses the same branch your tests are using

Generate source blob url

The CI endpoint takes a url that has a ‘.tgz` file to execute your tests. We pull down the app you’re testing against, inject an ‘app.json` (or modify if it already exists). We the use the heroku “source” api to generate a url that we can put our newly generated `.tgz` file. It also returns a “get” url where those contents can be downloaded. We pass this url back to CI



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/hatchet/test_run.rb', line 156

def source_blob_url
  @app.in_directory do
    app_json = JSON.parse(File.read("app.json")) if File.exist?("app.json")
    app_json ||= {}
    app_json["environments"]                       ||= {}
    app_json["environments"]["test"]               ||= {}
    app_json["environments"]["test"]["buildpacks"] = @buildpacks.map {|b| { url: b } }
    File.open("app.json", "w") {|f| f.write(JSON.generate(app_json)) }

    `tar c . | gzip -9 > slug.tgz`

    source_put_url = @app.create_source
    Hatchet::RETRIES.times.retry do
      @api_rate_limit.call
      Excon.put(source_put_url,
                expects: [200],
                body:    File.read('slug.tgz'))
    end
  end
  return @app.source_get_url
end

#statusObject



88
89
90
# File 'lib/hatchet/test_run.rb', line 88

def status
  @status # :pending, :building, :creating, :succeeded, :failed, :errored
end

#wait!Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/hatchet/test_run.rb', line 113

def wait!
  Timeout::timeout(@timeout) do
    while true do
      info
      case @status
      when :succeeded
        yield self
        return self
      when :failed, :errored
        raise FailedTestError.new(self.app, self.output) unless app.allow_failure?
        yield self
        return self
      else
        # keep looping
      end
      sleep @pause
    end
  end
rescue Timeout::Error
  puts "Timed out status: #{@status}, timeout: #{@timeout}"
  raise FailedTestError.new(self.app, self.output) unless app.allow_failure?
  yield self
  return self
end