Skip to content

Commit

Permalink
Merge pull request #8791 from code-dot-org/cucumber-logs-on-s3
Browse files Browse the repository at this point in the history
Upload cucumber logs to S3
  • Loading branch information
islemaster committed Jun 7, 2016
2 parents c373dff + 5d82aee commit 0ae0e16
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 7 deletions.
37 changes: 30 additions & 7 deletions dashboard/test/ui/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
require 'bundler'
require 'bundler/setup'

require 'cdo/aws/s3'
require 'cdo/git_utils'
require 'cdo/rake_utils'
require 'cdo/test_flakiness'
require 'cdo/hip_chat'
Expand All @@ -25,6 +27,26 @@

ENV['BUILD'] = `git rev-parse --short HEAD`

S3_LOGS_BUCKET = 'cucumber-logs'
S3_LOGS_PREFIX = GitUtils.current_branch
log_uploader = AWS::S3::PublicVersionedLogUploader.new(S3_LOGS_BUCKET, S3_LOGS_PREFIX)

#
# Upload the given filename (of a cucumber log) to the logs s3 bucket.
# Returns a public URL to the uploaded log.
#
def upload_log_and_get_public_url(log_uploader, filename)
return nil unless $options.html
log_uploader.upload_log(filename)
rescue Exception => msg
HipChat.log "Uploading log to S3 failed: #{msg}"
return nil
end

def log_link_from_url(url)
url ? " <a href='#{url}'>☁ Log on S3</a>" : ''
end

$options = OpenStruct.new
$options.config = nil
$options.browser = nil
Expand Down Expand Up @@ -363,9 +385,12 @@ def how_many_reruns?(test_run_string)
while !succeeded && (reruns < max_reruns)
reruns += 1

# Upload the failure log to S3, so we can examine it at our leisure.
log_url = upload_log_and_get_public_url(log_uploader, html_output_filename)

HipChat.log "<pre>#{output_synopsis(output_stdout)}</pre>"
# Since output_stderr is empty, we do not log it to HipChat.
HipChat.log "<b>dashboard</b> UI tests failed with <b>#{test_run_string}</b> (#{RakeUtils.format_duration(test_duration)}), retrying (#{reruns}/#{max_reruns}, flakiness: #{TestFlakiness.test_flakiness[test_run_string] || "?"})..."
HipChat.log "<b>dashboard</b> UI tests failed with <b>#{test_run_string}</b> (#{RakeUtils.format_duration(test_duration)})#{log_link_from_url log_url}, retrying (#{reruns}/#{max_reruns}, flakiness: #{TestFlakiness.test_flakiness[test_run_string] || "?"})..."

rerun_arguments = File.exist?(rerun_filename) ? " @#{rerun_filename}" : ''

Expand Down Expand Up @@ -404,14 +429,12 @@ def how_many_reruns?(test_run_string)
# Don't log individual successes because we hit HipChat rate limits
# HipChat.log "<b>dashboard</b> UI tests passed with <b>#{test_run_string}</b> (#{RakeUtils.format_duration(test_duration)}#{scenario_info})"
else
# Upload the failure log to S3, so we can examine it at our leisure.
log_url = upload_log_and_get_public_url(log_uploader, html_output_filename)

HipChat.log "<pre>#{output_synopsis(output_stdout)}</pre>"
HipChat.log "<pre>#{output_stderr}</pre>"
message = "<b>dashboard</b> UI tests failed with <b>#{test_run_string}</b> (#{RakeUtils.format_duration(test_duration)}#{scenario_info}#{rerun_info})"

if $options.html
link = "https://#{CDO.dashboard_hostname}/ui_test/" + html_output_filename
message += " <a href='#{link}'>☁ html output</a>"
end
message = "<b>dashboard</b> UI tests failed with <b>#{test_run_string}</b> (#{RakeUtils.format_duration(test_duration)}#{scenario_info}#{rerun_info})#{log_link_from_url log_url}"
short_message = message

message += "<br/><i>rerun: bundle exec ./runner.rb -c #{browser_name} -f #{feature} #{'--eyes' if $options.run_eyes_tests} --html</i>"
Expand Down
26 changes: 26 additions & 0 deletions lib/cdo/aws/s3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,31 @@ def self.random
def self.public_url(bucket, filename)
Aws::S3::Object.new(bucket, filename, region: CDO.aws_region).public_url
end

class PublicVersionedLogUploader
def initialize(bucket, prefix)
@bucket = bucket
@prefix = prefix
end

#
# Uploads the file with the given filename to S3 in the preconfigured
# bucket and prefix, and returns a public URL to the uploaded log file.
# May raise an exception if the file cannot be opened or the S3 upload fails.
#
def upload_log(filename)
File.open(filename, 'rb') do |file|
result = AWS::S3.create_client.put_object(
bucket: @bucket,
key: "#{@prefix}/#{filename}",
body: file,
acl: 'public-read'
)
log_url = "https://s3.amazonaws.com/#{S3_LOGS_BUCKET}/#{S3_LOGS_PREFIX}/#{filename}"
log_url += "?versionId=#{result[:version_id]}" unless result[:version_id].nil?
return log_url
end
end
end
end
end

0 comments on commit 0ae0e16

Please sign in to comment.