debian-mirror-gitlab/spec/models/integrations/buildkite_spec.rb

152 lines
4.3 KiB
Ruby
Raw Normal View History

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