2015-08-26 19:58:49 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-01-12 17:31:02 -05:00
|
|
|
describe DroneCiService, models: true, caching: true do
|
|
|
|
include ReactiveCachingHelpers
|
|
|
|
|
2015-08-26 19:58:49 -04:00
|
|
|
describe 'associations' do
|
|
|
|
it { is_expected.to belong_to(:project) }
|
|
|
|
it { is_expected.to have_one(:service_hook) }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'validations' do
|
|
|
|
context 'active' do
|
2016-04-21 11:13:14 -04:00
|
|
|
before { subject.active = true }
|
2015-08-26 19:58:49 -04:00
|
|
|
|
|
|
|
it { is_expected.to validate_presence_of(:token) }
|
|
|
|
it { is_expected.to validate_presence_of(:drone_url) }
|
2016-04-21 11:13:14 -04:00
|
|
|
it_behaves_like 'issue tracker service URL attribute', :drone_url
|
2015-08-26 19:58:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'inactive' do
|
2016-04-21 11:13:14 -04:00
|
|
|
before { subject.active = false }
|
2015-08-26 19:58:49 -04:00
|
|
|
|
|
|
|
it { is_expected.not_to validate_presence_of(:token) }
|
|
|
|
it { is_expected.not_to validate_presence_of(:drone_url) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_context :drone_ci_service do
|
|
|
|
let(:drone) { DroneCiService.new }
|
2017-01-26 17:44:58 -05:00
|
|
|
let(:project) { create(:project, :repository, name: 'project') }
|
2017-02-23 18:55:01 -05:00
|
|
|
let(:path) { project.full_path }
|
2015-08-26 19:58:49 -04:00
|
|
|
let(:drone_url) { 'http://drone.example.com' }
|
|
|
|
let(:sha) { '2ab7834c' }
|
|
|
|
let(:branch) { 'dev' }
|
|
|
|
let(:token) { 'secret' }
|
|
|
|
let(:iid) { rand(1..9999) }
|
|
|
|
|
2017-01-12 17:31:02 -05:00
|
|
|
# URL's
|
|
|
|
let(:build_page) { "#{drone_url}/gitlab/#{path}/redirect/commits/#{sha}?branch=#{branch}" }
|
|
|
|
let(:commit_status_path) { "#{drone_url}/gitlab/#{path}/commits/#{sha}?branch=#{branch}&access_token=#{token}" }
|
|
|
|
|
2015-08-26 19:58:49 -04:00
|
|
|
before(:each) do
|
|
|
|
allow(drone).to receive_messages(
|
|
|
|
project_id: project.id,
|
|
|
|
project: project,
|
|
|
|
active: true,
|
|
|
|
drone_url: drone_url,
|
|
|
|
token: token
|
|
|
|
)
|
|
|
|
end
|
2017-01-12 17:31:02 -05:00
|
|
|
|
|
|
|
def stub_request(status: 200, body: nil)
|
2017-02-22 17:39:43 -05:00
|
|
|
body ||= %q({"status":"success"})
|
2017-01-12 17:31:02 -05:00
|
|
|
|
|
|
|
WebMock.stub_request(:get, commit_status_path).to_return(
|
|
|
|
status: status,
|
|
|
|
headers: { 'Content-Type' => 'application/json' },
|
|
|
|
body: body
|
|
|
|
)
|
|
|
|
end
|
2015-08-26 19:58:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "service page/path methods" do
|
|
|
|
include_context :drone_ci_service
|
|
|
|
|
2017-01-12 17:31:02 -05:00
|
|
|
it { expect(drone.build_page(sha, branch)).to eq(build_page) }
|
2015-08-26 19:58:49 -04:00
|
|
|
it { expect(drone.commit_status_path(sha, branch)).to eq(commit_status_path) }
|
2017-01-12 17:31:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#commit_status' do
|
|
|
|
include_context :drone_ci_service
|
|
|
|
|
|
|
|
it 'returns the contents of the reactive cache' do
|
|
|
|
stub_reactive_cache(drone, { commit_status: 'foo' }, 'sha', 'ref')
|
|
|
|
|
|
|
|
expect(drone.commit_status('sha', 'ref')).to eq('foo')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#calculate_reactive_cache' do
|
|
|
|
include_context :drone_ci_service
|
|
|
|
|
|
|
|
context '#commit_status' do
|
|
|
|
subject { drone.calculate_reactive_cache(sha, branch)[:commit_status] }
|
|
|
|
|
|
|
|
it 'sets commit status to :error when status is 500' do
|
|
|
|
stub_request(status: 500)
|
|
|
|
|
|
|
|
is_expected.to eq(:error)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets commit status to :error when status is 404' do
|
|
|
|
stub_request(status: 404)
|
|
|
|
|
|
|
|
is_expected.to eq(:error)
|
|
|
|
end
|
|
|
|
|
2017-02-22 12:44:44 -05:00
|
|
|
{
|
|
|
|
"killed" => :canceled,
|
2017-01-12 17:31:02 -05:00
|
|
|
"failure" => :failed,
|
|
|
|
"error" => :failed,
|
2017-02-22 12:44:44 -05:00
|
|
|
"success" => "success"
|
2017-01-12 17:31:02 -05:00
|
|
|
}.each do |drone_status, our_status|
|
|
|
|
it "sets commit status to #{our_status.inspect} when returned status is #{drone_status.inspect}" do
|
2017-02-22 17:39:43 -05:00
|
|
|
stub_request(body: %Q({"status":"#{drone_status}"}))
|
2017-01-12 17:31:02 -05:00
|
|
|
|
|
|
|
is_expected.to eq(our_status)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-08-26 19:58:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "execute" do
|
|
|
|
include_context :drone_ci_service
|
|
|
|
|
|
|
|
let(:user) { create(:user, username: 'username') }
|
2016-08-04 11:44:27 -04:00
|
|
|
let(:push_sample_data) do
|
2016-08-12 04:09:29 -04:00
|
|
|
Gitlab::DataBuilder::Push.build_sample(project, user)
|
2016-08-04 11:44:27 -04:00
|
|
|
end
|
2015-08-26 19:58:49 -04:00
|
|
|
|
|
|
|
it do
|
|
|
|
service_hook = double
|
|
|
|
expect(service_hook).to receive(:execute)
|
|
|
|
expect(drone).to receive(:service_hook).and_return(service_hook)
|
|
|
|
|
|
|
|
drone.execute(push_sample_data)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|