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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

211 lines
6 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
2015-09-25 12:07:36 +05:30
require 'spec_helper'
2021-09-04 01:27:46 +05:30
RSpec.describe Integrations::DroneCi, :use_clean_rails_memory_store_caching do
2017-08-17 22:00:37 +05:30
include ReactiveCachingHelpers
2022-02-05 19:09:49 +05:30
subject(:integration) { described_class.new }
2015-09-25 12:07:36 +05:30
describe 'validations' do
context 'active' do
2017-09-10 17:25:29 +05:30
before do
subject.active = true
end
2015-09-25 12:07:36 +05:30
it { is_expected.to validate_presence_of(:token) }
it { is_expected.to validate_presence_of(:drone_url) }
2021-09-30 23:02:18 +05:30
it_behaves_like 'issue tracker integration URL attribute', :drone_url
2015-09-25 12:07:36 +05:30
end
context 'inactive' do
2017-09-10 17:25:29 +05:30
before do
subject.active = false
end
2015-09-25 12:07:36 +05:30
it { is_expected.not_to validate_presence_of(:token) }
it { is_expected.not_to validate_presence_of(:drone_url) }
end
end
2021-09-04 01:27:46 +05:30
shared_context :drone_ci_integration do
2021-09-30 23:02:18 +05:30
subject(:drone) do
described_class.new(
project: project,
active: true,
drone_url: drone_url,
token: token
)
end
2017-08-17 22:00:37 +05:30
let(:project) { create(:project, :repository, name: 'project') }
let(:path) { project.full_path }
2015-09-25 12:07:36 +05:30
let(:drone_url) { 'http://drone.example.com' }
let(:sha) { '2ab7834c' }
let(:branch) { 'dev' }
let(:token) { 'secret' }
let(:iid) { rand(1..9999) }
2021-09-04 01:27:46 +05:30
# URLs
2017-08-17 22:00:37 +05:30
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}" }
def stub_request(status: 200, body: nil)
body ||= %q({"status":"success"})
WebMock.stub_request(:get, commit_status_path).to_return(
status: status,
headers: { 'Content-Type' => 'application/json' },
body: body
)
end
2015-09-25 12:07:36 +05:30
end
2022-02-05 19:09:49 +05:30
include_context Integrations::EnableSslVerification do
describe '#enable_ssl_verification' do
before do
allow(integration).to receive(:new_record?).and_return(false)
end
it 'returns true for a known hostname' do
integration.drone_url = 'https://cloud.drone.io'
expect(integration.enable_ssl_verification).to be(true)
end
it 'returns true for new records' do
allow(integration).to receive(:new_record?).and_return(true)
integration.drone_url = 'http://example.com'
expect(integration.enable_ssl_verification).to be(true)
end
it 'returns false for an unknown hostname' do
integration.drone_url = 'https://example.com'
expect(integration.enable_ssl_verification).to be(false)
end
it 'returns false for a HTTP URL' do
integration.drone_url = 'http://cloud.drone.io'
expect(integration.enable_ssl_verification).to be(false)
end
it 'returns false for an invalid URL' do
integration.drone_url = 'https://example.com:foo'
expect(integration.enable_ssl_verification).to be(false)
end
it 'returns the persisted value if present' do
integration.drone_url = 'https://cloud.drone.io'
integration.enable_ssl_verification = false
expect(integration.enable_ssl_verification).to be(false)
end
end
end
2021-09-30 23:02:18 +05:30
it_behaves_like Integrations::HasWebHook do
include_context :drone_ci_integration
let(:integration) { drone }
let(:hook_url) { "#{drone_url}/hook?owner=#{project.namespace.full_path}&name=#{project.path}&access_token=#{token}" }
it 'does not create a hook if project is not present' do
integration.project = nil
integration.instance = true
expect { integration.save! }.not_to change(ServiceHook, :count)
end
end
describe "integration page/path methods" do
2021-09-04 01:27:46 +05:30
include_context :drone_ci_integration
2015-09-25 12:07:36 +05:30
2017-08-17 22:00:37 +05:30
it { expect(drone.build_page(sha, branch)).to eq(build_page) }
2015-09-25 12:07:36 +05:30
it { expect(drone.commit_status_path(sha, branch)).to eq(commit_status_path) }
2017-08-17 22:00:37 +05:30
end
describe '#commit_status' do
2021-09-04 01:27:46 +05:30
include_context :drone_ci_integration
2017-08-17 22:00:37 +05:30
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
2021-09-04 01:27:46 +05:30
include_context :drone_ci_integration
2017-08-17 22:00:37 +05:30
2020-03-13 15:44:24 +05:30
describe '#commit_status' do
2017-08-17 22:00:37 +05:30
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
2019-09-30 21:07:59 +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, commit_status_path)
.to_raise(http_error)
2020-04-22 19:07:51 +05:30
expect(Gitlab::ErrorTracking)
.to receive(:log_exception)
2022-07-16 23:28:13 +05:30
.with(instance_of(http_error), { project_id: project.id })
2020-04-22 19:07:51 +05:30
2019-09-30 21:07:59 +05:30
is_expected.to eq(:error)
end
end
2017-08-17 22:00:37 +05:30
{
"killed" => :canceled,
"failure" => :failed,
"error" => :failed,
"success" => "success"
}.each do |drone_status, our_status|
it "sets commit status to #{our_status.inspect} when returned status is #{drone_status.inspect}" do
stub_request(body: %Q({"status":"#{drone_status}"}))
is_expected.to eq(our_status)
end
end
end
2015-09-25 12:07:36 +05:30
end
describe "execute" do
2021-09-04 01:27:46 +05:30
include_context :drone_ci_integration
2015-09-25 12:07:36 +05:30
2019-03-02 22:35:43 +05:30
let(:user) { create(:user, username: 'username') }
2016-09-13 17:45:13 +05:30
let(:push_sample_data) do
Gitlab::DataBuilder::Push.build_sample(project, user)
end
2015-09-25 12:07:36 +05:30
2021-09-30 23:02:18 +05:30
it 'executes the webhook' do
expect(drone).to receive(:execute_web_hook!).with(push_sample_data)
drone.execute(push_sample_data)
end
it 'does not try to execute the webhook if the integration is not in a project' do
drone.project = nil
drone.instance = true
expect(drone).not_to receive(:execute_web_hook!)
2015-09-25 12:07:36 +05:30
drone.execute(push_sample_data)
end
end
end