2019-07-25 01:21:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-04-06 12:20:27 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-10-07 11:08:40 -04:00
|
|
|
RSpec.describe Gitlab::Ci::Trace, :clean_gitlab_redis_shared_state, factory_default: :keep do
|
2021-02-18 01:09:43 -05:00
|
|
|
let_it_be(:project) { create_default(:project).freeze }
|
2020-10-07 11:08:40 -04:00
|
|
|
let_it_be_with_reload(:build) { create(:ci_build) }
|
2017-04-06 12:20:27 -04:00
|
|
|
let(:trace) { described_class.new(build) }
|
|
|
|
|
|
|
|
describe "associations" do
|
|
|
|
it { expect(trace).to respond_to(:job) }
|
|
|
|
it { expect(trace).to delegate_method(:old_trace).to(:job) }
|
|
|
|
end
|
|
|
|
|
2020-08-24 14:10:19 -04:00
|
|
|
context 'when trace is migrated to object storage' do
|
|
|
|
let!(:job) { create(:ci_build, :trace_artifact) }
|
|
|
|
let!(:artifact1) { job.job_artifacts_trace }
|
|
|
|
let!(:artifact2) { job.reload.job_artifacts_trace }
|
|
|
|
let(:test_data) { "hello world" }
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_artifacts_object_storage
|
|
|
|
|
|
|
|
artifact1.file.migrate!(ObjectStorage::Store::REMOTE)
|
|
|
|
end
|
|
|
|
|
2020-08-27 11:10:21 -04:00
|
|
|
it 'reloads the trace after is it migrated' do
|
|
|
|
stub_const('Gitlab::HttpIO::BUFFER_SIZE', test_data.length)
|
2020-08-24 14:10:19 -04:00
|
|
|
|
2020-08-27 11:10:21 -04:00
|
|
|
expect_next_instance_of(Gitlab::HttpIO) do |http_io|
|
|
|
|
expect(http_io).to receive(:get_chunk).and_return(test_data, "")
|
2020-08-24 14:10:19 -04:00
|
|
|
end
|
|
|
|
|
2020-08-27 11:10:21 -04:00
|
|
|
expect(artifact2.job.trace.raw).to eq(test_data)
|
2020-08-24 14:10:19 -04:00
|
|
|
end
|
2020-10-13 11:08:53 -04:00
|
|
|
|
|
|
|
it 'reloads the trace in case of a chunk error' do
|
|
|
|
chunk_error = described_class::ChunkedIO::FailedToGetChunkError
|
|
|
|
|
|
|
|
allow_any_instance_of(described_class::Stream)
|
|
|
|
.to receive(:raw).and_raise(chunk_error)
|
|
|
|
|
|
|
|
expect(build).to receive(:reset).and_return(build)
|
|
|
|
expect { trace.raw }.to raise_error(chunk_error)
|
|
|
|
end
|
2020-08-24 14:10:19 -04:00
|
|
|
end
|
|
|
|
|
2018-05-04 06:49:26 -04:00
|
|
|
context 'when live trace feature is disabled' do
|
2017-04-06 12:20:27 -04:00
|
|
|
before do
|
2018-05-04 06:49:26 -04:00
|
|
|
stub_feature_flags(ci_enable_live_trace: false)
|
2017-04-06 12:20:27 -04:00
|
|
|
end
|
2018-04-05 11:42:25 -04:00
|
|
|
|
2018-05-04 06:49:26 -04:00
|
|
|
it_behaves_like 'trace with disabled live trace feature'
|
2017-04-06 12:20:27 -04:00
|
|
|
end
|
2018-02-27 13:44:13 -05:00
|
|
|
|
2018-05-04 06:49:26 -04:00
|
|
|
context 'when live trace feature is enabled' do
|
2018-04-02 12:22:46 -04:00
|
|
|
before do
|
2018-05-04 06:49:26 -04:00
|
|
|
stub_feature_flags(ci_enable_live_trace: true)
|
2018-04-02 12:22:46 -04:00
|
|
|
end
|
|
|
|
|
2018-05-04 06:49:26 -04:00
|
|
|
it_behaves_like 'trace with enabled live trace feature'
|
2018-02-27 13:44:13 -05:00
|
|
|
end
|
2020-01-13 04:08:03 -05:00
|
|
|
|
|
|
|
describe '#update_interval' do
|
|
|
|
context 'it is not being watched' do
|
2021-03-23 08:09:33 -04:00
|
|
|
it { expect(trace.update_interval).to eq(60.seconds) }
|
2020-01-13 04:08:03 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'it is being watched' do
|
|
|
|
before do
|
|
|
|
trace.being_watched!
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns 3 seconds' do
|
|
|
|
expect(trace.update_interval).to eq(3.seconds)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#being_watched!' do
|
|
|
|
let(:cache_key) { "gitlab:ci:trace:#{build.id}:watched" }
|
|
|
|
|
|
|
|
it 'sets gitlab:ci:trace:<job.id>:watched in redis' do
|
|
|
|
trace.being_watched!
|
|
|
|
|
|
|
|
result = Gitlab::Redis::SharedState.with do |redis|
|
|
|
|
redis.exists(cache_key)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(result).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates the expiry of gitlab:ci:trace:<job.id>:watched in redis', :clean_gitlab_redis_shared_state do
|
|
|
|
Gitlab::Redis::SharedState.with do |redis|
|
|
|
|
redis.set(cache_key, true, ex: 4.seconds)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect do
|
|
|
|
trace.being_watched!
|
|
|
|
end.to change { Gitlab::Redis::SharedState.with { |redis| redis.pttl(cache_key) } }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#being_watched?' do
|
|
|
|
context 'gitlab:ci:trace:<job.id>:watched in redis is set', :clean_gitlab_redis_shared_state do
|
|
|
|
before do
|
|
|
|
Gitlab::Redis::SharedState.with do |redis|
|
|
|
|
redis.set("gitlab:ci:trace:#{build.id}:watched", true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true' do
|
|
|
|
expect(trace.being_watched?).to be(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'gitlab:ci:trace:<job.id>:watched in redis is not set' do
|
|
|
|
it 'returns false' do
|
|
|
|
expect(trace.being_watched?).to be(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-09-28 11:09:44 -04:00
|
|
|
|
|
|
|
describe '#lock' do
|
|
|
|
it 'acquires an exclusive lease on the trace' do
|
|
|
|
trace.lock do
|
|
|
|
expect { trace.lock }
|
|
|
|
.to raise_error described_class::LockedError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-04-06 12:20:27 -04:00
|
|
|
end
|