trace_spec.rb 3.21 KB
Newer Older
1 2
# frozen_string_literal: true

3 4
require 'spec_helper'

5
RSpec.describe Gitlab::Ci::Trace, :clean_gitlab_redis_shared_state do
6 7 8 9 10 11 12 13
  let(:build) { create(:ci_build) }
  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

14 15 16 17 18 19 20 21 22 23 24 25
  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

26 27
    it 'reloads the trace after is it migrated' do
      stub_const('Gitlab::HttpIO::BUFFER_SIZE', test_data.length)
28

29 30
      expect_next_instance_of(Gitlab::HttpIO) do |http_io|
        expect(http_io).to receive(:get_chunk).and_return(test_data, "")
31 32
      end

33
      expect(artifact2.job.trace.raw).to eq(test_data)
34 35 36
    end
  end

37
  context 'when live trace feature is disabled' do
38
    before do
39
      stub_feature_flags(ci_enable_live_trace: false)
40
    end
Shinya Maeda's avatar
Shinya Maeda committed
41

42
    it_behaves_like 'trace with disabled live trace feature'
43
  end
44

45
  context 'when live trace feature is enabled' do
46
    before do
47
      stub_feature_flags(ci_enable_live_trace: true)
48 49
    end

50
    it_behaves_like 'trace with enabled live trace feature'
51
  end
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

  describe '#update_interval' do
    context 'it is not being watched' do
      it 'returns 30 seconds' do
        expect(trace.update_interval).to eq(30.seconds)
      end
    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
114 115 116 117 118

  describe '#lock' do
    it 'acquires an exclusive lease on the trace' do
      trace.lock do
        expect { trace.lock }
119
          .to raise_error described_class::LockedError
120 121 122
      end
    end
  end
123
end