2019-03-30 03:23:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-07-05 09:55:10 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Ci::BuildRunnerSession, model: true do
|
2018-07-05 09:55:10 -04:00
|
|
|
let!(:build) { create(:ci_build, :with_runner_session) }
|
2019-11-29 07:10:06 -05:00
|
|
|
let(:url) { 'https://new.example.com' }
|
2018-07-05 09:55:10 -04:00
|
|
|
|
|
|
|
subject { build.runner_session }
|
|
|
|
|
|
|
|
it { is_expected.to belong_to(:build) }
|
|
|
|
|
|
|
|
it { is_expected.to validate_presence_of(:build) }
|
|
|
|
it { is_expected.to validate_presence_of(:url).with_message('must be a valid URL') }
|
|
|
|
|
2019-11-29 07:10:06 -05:00
|
|
|
context 'nested attribute assignment' do
|
|
|
|
it 'creates a new session' do
|
|
|
|
simple_build = create(:ci_build)
|
|
|
|
simple_build.runner_session_attributes = { url: url }
|
|
|
|
simple_build.save!
|
|
|
|
|
|
|
|
session = simple_build.reload.runner_session
|
|
|
|
expect(session).to be_a(Ci::BuildRunnerSession)
|
|
|
|
expect(session.url).to eq(url)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates session with new attributes' do
|
|
|
|
build.runner_session_attributes = { url: url }
|
|
|
|
build.save!
|
|
|
|
|
|
|
|
expect(build.reload.runner_session.url).to eq(url)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-05 09:55:10 -04:00
|
|
|
describe '#terminal_specification' do
|
2019-04-04 14:32:02 -04:00
|
|
|
let(:specification) { subject.terminal_specification }
|
|
|
|
|
|
|
|
it 'returns terminal.gitlab.com protocol' do
|
|
|
|
expect(specification[:subprotocols]).to eq ['terminal.gitlab.com']
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a wss url' do
|
|
|
|
expect(specification[:url]).to start_with('wss://')
|
|
|
|
end
|
2018-07-05 09:55:10 -04:00
|
|
|
|
|
|
|
it 'returns empty hash if no url' do
|
|
|
|
subject.url = ''
|
|
|
|
|
2019-04-04 14:32:02 -04:00
|
|
|
expect(specification).to be_empty
|
2018-07-05 09:55:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when url is present' do
|
|
|
|
it 'returns ca_pem nil if empty certificate' do
|
|
|
|
subject.certificate = ''
|
|
|
|
|
2019-04-04 14:32:02 -04:00
|
|
|
expect(specification[:ca_pem]).to be_nil
|
2018-07-05 09:55:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds Authorization header if authorization is present' do
|
|
|
|
subject.authorization = 'whatever'
|
|
|
|
|
2019-04-04 14:32:02 -04:00
|
|
|
expect(specification[:headers]).to include(Authorization: ['whatever'])
|
2018-07-05 09:55:10 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-05-25 11:07:58 -04:00
|
|
|
|
|
|
|
describe '#service_specification' do
|
2022-08-03 05:12:13 -04:00
|
|
|
let(:service) { 'foo' }
|
2020-05-25 11:07:58 -04:00
|
|
|
let(:port) { 80 }
|
|
|
|
let(:path) { 'path' }
|
|
|
|
let(:subprotocols) { nil }
|
|
|
|
let(:specification) { subject.service_specification(service: service, port: port, path: path, subprotocols: subprotocols) }
|
|
|
|
|
|
|
|
it 'returns service proxy url' do
|
|
|
|
expect(specification[:url]).to eq "https://localhost/proxy/#{service}/#{port}/#{path}"
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns default service proxy websocket subprotocol' do
|
|
|
|
expect(specification[:subprotocols]).to eq %w[terminal.gitlab.com]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns empty hash if no url' do
|
|
|
|
subject.url = ''
|
|
|
|
|
|
|
|
expect(specification).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when port is not present' do
|
|
|
|
let(:port) { nil }
|
|
|
|
|
|
|
|
it 'uses the default port name' do
|
|
|
|
expect(specification[:url]).to eq "https://localhost/proxy/#{service}/default_port/#{path}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the service is not present' do
|
|
|
|
let(:service) { '' }
|
|
|
|
|
|
|
|
it 'uses the service name "build" as default' do
|
|
|
|
expect(specification[:url]).to eq "https://localhost/proxy/build/#{port}/#{path}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when url is present' do
|
|
|
|
it 'returns ca_pem nil if empty certificate' do
|
|
|
|
subject.certificate = ''
|
|
|
|
|
|
|
|
expect(specification[:ca_pem]).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds Authorization header if authorization is present' do
|
|
|
|
subject.authorization = 'foobar'
|
|
|
|
|
|
|
|
expect(specification[:headers]).to include(Authorization: ['foobar'])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when subprotocol is present' do
|
|
|
|
let(:subprotocols) { 'foobar' }
|
|
|
|
|
|
|
|
it 'returns the new subprotocol' do
|
|
|
|
expect(specification[:subprotocols]).to eq [subprotocols]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-07-05 09:55:10 -04:00
|
|
|
end
|