From c8f94e90ed94b0b133e66db7d1814b12221f1415 Mon Sep 17 00:00:00 2001 From: carlad Date: Fri, 5 Aug 2016 16:31:53 +0200 Subject: [PATCH] add webmock, update spec --- Gemfile | 1 + Gemfile.lock | 9 +++++++++ lib/travis/api/v3/queries/log.rb | 9 ++------- spec/spec_helper.rb | 1 + spec/v3/services/log/find_spec.rb | 25 +++++++++++++++++++------ 5 files changed, 32 insertions(+), 13 deletions(-) diff --git a/Gemfile b/Gemfile index bee58fa5..185b97a1 100644 --- a/Gemfile +++ b/Gemfile @@ -47,6 +47,7 @@ group :test do gem 'mocha', '~> 0.12' gem 'database_cleaner', '~> 0.8.0' gem 'timecop', '~> 0.8.0' + gem 'webmock' end group :development do diff --git a/Gemfile.lock b/Gemfile.lock index 82e45d07..76f7f419 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -146,6 +146,8 @@ GEM activerecord (~> 3.2.0, >= 3.2.9) concurrent-ruby (1.0.2) connection_pool (2.2.0) + crack (0.4.3) + safe_yaml (~> 1.0.0) customerio (1.0.0) multi_json (~> 1.0) dalli (2.7.6) @@ -189,6 +191,7 @@ GEM multi_json (~> 1.11) os (~> 0.9) signet (~> 0.7) + hashdiff (0.3.0) hashr (0.0.22) hike (1.2.3) hitimes (1.2.4) @@ -288,6 +291,7 @@ GEM rspec-expectations (>= 2.99.0.beta1) rspec-mocks (2.99.4) ruby_dep (1.3.1) + safe_yaml (1.0.4) sentry-raven (1.0.0) faraday (>= 0.7.6) sidekiq (4.1.2) @@ -345,6 +349,10 @@ GEM coercible (~> 1.0) descendants_tracker (~> 0.0, >= 0.0.3) equalizer (~> 0.0, >= 0.0.9) + webmock (2.1.0) + addressable (>= 2.3.6) + crack (>= 0.3.2) + hashdiff yard (0.8.7.6) PLATFORMS @@ -392,6 +400,7 @@ DEPENDENCIES travis-support! travis-yaml! unicorn + webmock yard-sinatra! BUNDLED WITH diff --git a/lib/travis/api/v3/queries/log.rb b/lib/travis/api/v3/queries/log.rb index 75607f26..293f357a 100644 --- a/lib/travis/api/v3/queries/log.rb +++ b/lib/travis/api/v3/queries/log.rb @@ -8,16 +8,11 @@ module Travis::API::V3 log = Models::Log.find_by_job_id(job.id) raise EntityMissing, 'log not found'.freeze if log.nil? - - p log.archived_at - #if the log has been archived, go to s3 if log.archived_at ## if it's not there then fetch it from S3, and return it wrapped as a compatible log_parts object with a hard coded #number (log_parts have a number) and the parts chunked (not sure how to do this) - archived_log_path = archive_url("/jobs/#{params[:job.id]}/log.txt") - p archived_log_path - content = open(Net::HTTP.get(URI.parse(archived_log_path))) - p content + archived_log_path = archive_url("/jobs/#{job.id}/log.txt") + content = Net::HTTP.get(URI.parse(archived_log_path)) log_parts = [] content.each_line.with_index do |line, number| diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 74fcf50e..e0e72ef1 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -10,6 +10,7 @@ require 'gh' require 'multi_json' require 'pry' require 'stackprof' +require 'webmock/rspec' require 'travis/api/app' require 'travis/testing' diff --git a/spec/v3/services/log/find_spec.rb b/spec/v3/services/log/find_spec.rb index ba2a816f..fd20ac04 100644 --- a/spec/v3/services/log/find_spec.rb +++ b/spec/v3/services/log/find_spec.rb @@ -37,13 +37,14 @@ describe Travis::API::V3::Services::Log::Find, set_app: true do context 'when log not found in db but stored on S3' do describe 'returns log with an array of Log Parts' do + before do + stub_request(:get, "https://s3.amazonaws.com/archive.travis-ci.org/jobs/#{s3job.id}/log.txt"). + with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Host'=>'s3.amazonaws.com', 'User-Agent'=>'Ruby'}). + to_return(:status => 200, :body => "$ git clean -fdx\nRemoving Gemfile.lock\n$ git fetch", :headers => {}) + end example do - s3log.update_attribute(:archived_at, Time.now) + s3log.update_attributes(archived_at: Time.now) get("/v3/job/#{s3job.id}/log", {}, headers) - p s3job - p s3job.id - p s3log - p s3log.archived_at expect(parsed_body).to eq( '@href' => "/v3/job/#{s3job.id}/log", @@ -51,7 +52,19 @@ describe Travis::API::V3::Services::Log::Find, set_app: true do '@type' => 'log', 'content' => 'minimal log 1', 'id' => s3log.id, - 'log_parts' => []) + 'log_parts' => [{ + "@type"=>"log_part", + "@representation"=>"minimal", + "content"=>"$ git clean -fdx", + "number"=>0}, { + "@type"=>"log_part", + "@representation"=>"minimal", + "content"=>"Removing Gemfile.lock", + "number"=>1}, { + "@type"=>"log_part", + "@representation"=>"minimal", + "content"=>"$ git fetch", + "number"=>2}]) end end describe 'returns log as plain text'