2019-03-30 07:23:56 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-08-18 10:54:51 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2021-05-25 18:10:42 +00:00
|
|
|
RSpec.describe Integrations::Buildkite, :use_clean_rails_memory_store_caching do
|
2017-01-12 22:31:02 +00:00
|
|
|
include ReactiveCachingHelpers
|
2019-04-21 10:03:26 +00:00
|
|
|
include StubRequests
|
2017-01-12 22:31:02 +00:00
|
|
|
|
2017-08-02 19:55:11 +00:00
|
|
|
let(:project) { create(:project) }
|
2017-01-12 22:31:02 +00:00
|
|
|
|
2021-07-01 18:07:29 +00:00
|
|
|
subject(:integration) do
|
2020-09-04 15:08:46 +00:00
|
|
|
described_class.create!(
|
2017-01-12 22:31:02 +00:00
|
|
|
project: project,
|
|
|
|
properties: {
|
2020-07-23 03:09:42 +00:00
|
|
|
project_url: 'https://buildkite.com/organization-name/example-pipeline',
|
2017-01-12 22:31:02 +00:00
|
|
|
token: 'secret-sauce-webhook-token:secret-sauce-status-token'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-07-14 18:08:31 +00:00
|
|
|
it_behaves_like Integrations::HasWebHook do
|
|
|
|
let(:hook_url) { 'https://webhook.buildkite.com/deliver/secret-sauce-webhook-token' }
|
2014-08-18 10:54:51 +00:00
|
|
|
end
|
|
|
|
|
2016-04-21 15:13:14 +00:00
|
|
|
describe 'Validations' do
|
2021-07-01 18:07:29 +00:00
|
|
|
context 'when integration is active' do
|
2017-06-14 18:18:56 +00:00
|
|
|
before do
|
|
|
|
subject.active = true
|
|
|
|
end
|
2016-04-21 15:13:14 +00:00
|
|
|
|
|
|
|
it { is_expected.to validate_presence_of(:project_url) }
|
|
|
|
it { is_expected.to validate_presence_of(:token) }
|
2021-07-01 18:07:29 +00:00
|
|
|
it_behaves_like 'issue tracker integration URL attribute', :project_url
|
2016-04-21 15:13:14 +00:00
|
|
|
end
|
|
|
|
|
2021-07-01 18:07:29 +00:00
|
|
|
context 'when integration is inactive' do
|
2017-06-14 18:18:56 +00:00
|
|
|
before do
|
|
|
|
subject.active = false
|
|
|
|
end
|
2016-04-21 15:13:14 +00:00
|
|
|
|
|
|
|
it { is_expected.not_to validate_presence_of(:project_url) }
|
|
|
|
it { is_expected.not_to validate_presence_of(:token) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-23 03:09:42 +00:00
|
|
|
describe '.supported_events' do
|
|
|
|
it 'supports push, merge_request, and tag_push events' do
|
2021-07-01 18:07:29 +00:00
|
|
|
expect(integration.supported_events).to eq %w(push merge_request tag_push)
|
2020-07-23 03:09:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-18 10:54:51 +00:00
|
|
|
describe 'commits methods' do
|
|
|
|
before do
|
2017-01-12 22:31:02 +00:00
|
|
|
allow(project).to receive(:default_branch).and_return('default-brancho')
|
2014-08-18 10:54:51 +00:00
|
|
|
end
|
|
|
|
|
2020-07-23 03:09:42 +00:00
|
|
|
it 'always activates SSL verification after saved' do
|
2021-07-01 18:07:29 +00:00
|
|
|
integration.create_service_hook(enable_ssl_verification: false)
|
2020-07-23 03:09:42 +00:00
|
|
|
|
2021-07-01 18:07:29 +00:00
|
|
|
integration.enable_ssl_verification = false
|
|
|
|
integration.active = true
|
2020-07-23 03:09:42 +00:00
|
|
|
|
2021-07-01 18:07:29 +00:00
|
|
|
expect { integration.save! }
|
|
|
|
.to change { integration.service_hook.enable_ssl_verification }.from(false).to(true)
|
2020-07-23 03:09:42 +00:00
|
|
|
end
|
|
|
|
|
2021-07-14 18:08:31 +00:00
|
|
|
describe '#hook_url' do
|
2014-08-18 10:54:51 +00:00
|
|
|
it 'returns the webhook url' do
|
2021-07-14 18:08:31 +00:00
|
|
|
expect(integration.hook_url).to eq(
|
2015-03-26 11:40:43 +00:00
|
|
|
'https://webhook.buildkite.com/deliver/secret-sauce-webhook-token'
|
2015-02-12 18:17:35 +00:00
|
|
|
)
|
2014-08-18 10:54:51 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-11 22:12:31 +00:00
|
|
|
describe '#commit_status_path' do
|
2014-08-18 10:54:51 +00:00
|
|
|
it 'returns the correct status page' do
|
2021-07-01 18:07:29 +00:00
|
|
|
expect(integration.commit_status_path('2ab7834c')).to eq(
|
2015-03-26 11:40:43 +00:00
|
|
|
'https://gitlab.buildkite.com/status/secret-sauce-status-token.json?commit=2ab7834c'
|
2015-02-12 18:17:35 +00:00
|
|
|
)
|
2014-08-18 10:54:51 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-11 22:12:31 +00:00
|
|
|
describe '#build_page' do
|
2014-08-18 10:54:51 +00:00
|
|
|
it 'returns the correct build page' do
|
2021-07-01 18:07:29 +00:00
|
|
|
expect(integration.build_page('2ab7834c', nil)).to eq(
|
2020-07-23 03:09:42 +00:00
|
|
|
'https://buildkite.com/organization-name/example-pipeline/builds?commit=2ab7834c'
|
2015-02-12 18:17:35 +00:00
|
|
|
)
|
2014-08-18 10:54:51 +00:00
|
|
|
end
|
|
|
|
end
|
2017-01-12 22:31:02 +00:00
|
|
|
|
|
|
|
describe '#commit_status' do
|
|
|
|
it 'returns the contents of the reactive cache' do
|
2021-07-01 18:07:29 +00:00
|
|
|
stub_reactive_cache(integration, { commit_status: 'foo' }, 'sha', 'ref')
|
2017-01-12 22:31:02 +00:00
|
|
|
|
2021-07-01 18:07:29 +00:00
|
|
|
expect(integration.commit_status('sha', 'ref')).to eq('foo')
|
2017-01-12 22:31:02 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#calculate_reactive_cache' do
|
2020-02-06 21:08:48 +00:00
|
|
|
describe '#commit_status' do
|
2020-03-25 12:08:19 +00:00
|
|
|
let(:buildkite_full_url) do
|
|
|
|
'https://gitlab.buildkite.com/status/secret-sauce-status-token.json?commit=123'
|
|
|
|
end
|
|
|
|
|
2021-07-01 18:07:29 +00:00
|
|
|
subject { integration.calculate_reactive_cache('123', 'unused')[:commit_status] }
|
2017-01-12 22:31:02 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
it 'passes through build status untouched when status is 200' do
|
2017-02-22 22:39:43 +00:00
|
|
|
stub_request(body: %q({"status":"Great Success"}))
|
2017-01-12 22:31:02 +00:00
|
|
|
|
|
|
|
is_expected.to eq('Great Success')
|
|
|
|
end
|
2020-03-25 12:08:19 +00:00
|
|
|
|
|
|
|
Gitlab::HTTP::HTTP_ERRORS.each do |http_error|
|
|
|
|
it "sets commit status to :error with a #{http_error.name} error" do
|
|
|
|
WebMock.stub_request(:get, buildkite_full_url)
|
|
|
|
.to_raise(http_error)
|
|
|
|
|
|
|
|
expect(Gitlab::ErrorTracking)
|
|
|
|
.to receive(:log_exception)
|
|
|
|
.with(instance_of(http_error), project_id: project.id)
|
|
|
|
|
|
|
|
is_expected.to eq(:error)
|
|
|
|
end
|
|
|
|
end
|
2017-01-12 22:31:02 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def stub_request(status: 200, body: nil)
|
2017-02-22 22:39:43 +00:00
|
|
|
body ||= %q({"status":"success"})
|
2017-01-12 22:31:02 +00:00
|
|
|
|
2019-04-21 10:03:26 +00:00
|
|
|
stub_full_request(buildkite_full_url)
|
|
|
|
.to_return(status: status,
|
|
|
|
headers: { 'Content-Type' => 'application/json' },
|
|
|
|
body: body)
|
2014-08-18 10:54:51 +00:00
|
|
|
end
|
|
|
|
end
|