Welcome to mirror list, hosted at ThFree Co, Russian Federation.

append_build_trace_service_spec.rb « ci « services « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 20f7967d1f17d7f56bc51cece6b42126ea2f695d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Ci::AppendBuildTraceService do
  let_it_be(:project) { create(:project) }
  let_it_be(:pipeline) { create(:ci_pipeline, project: project) }
  let_it_be_with_reload(:build) { create(:ci_build, :running, pipeline: pipeline) }

  before do
    stub_feature_flags(ci_enable_live_trace: true)
  end

  context 'build trace append is successful' do
    it 'returns a correct stream size and status code' do
      stream_size = 192.kilobytes
      body_data = 'x' * stream_size
      content_range = "0-#{stream_size}"

      result = described_class
        .new(build, content_range: content_range)
        .execute(body_data)

      expect(result.status).to eq 202
      expect(result.stream_size).to eq stream_size
      expect(build.trace_chunks.count).to eq 2
    end
  end

  context 'when could not correctly append to a trace' do
    it 'responds with content range violation and data stored' do
      allow(build).to receive_message_chain(:trace, :append) { 16 }

      result = described_class
        .new(build, content_range: '0-128')
        .execute('x' * 128)

      expect(result.status).to eq 416
      expect(result.stream_size).to eq 16
    end

    it 'logs exception if build has live trace' do
      build.trace.append('abcd', 0)

      expect(::Gitlab::ErrorTracking)
        .to receive(:log_exception)
        .with(anything, hash_including(chunk_index: 0, chunk_store: 'redis_trace_chunks'))

      result = described_class
        .new(build, content_range: '0-128')
        .execute('x' * 128)

      expect(result.status).to eq 416
      expect(result.stream_size).to eq 4
    end
  end

  context 'when the trace size is exceeded' do
    before do
      project.actual_limits.update!(ci_jobs_trace_size_limit: 1)
    end

    it 'returns 403 status code' do
      stream_size = 1.25.megabytes
      body_data = 'x' * stream_size
      content_range = "0-#{stream_size}"

      result = described_class
        .new(build, content_range: content_range)
        .execute(body_data)

      expect(result.status).to eq 403
      expect(result.stream_size).to be_nil
      expect(build.trace_chunks.count).to eq 0
      expect(build.reload).to be_failed
      expect(build.failure_reason).to eq 'trace_size_exceeded'
    end
  end

  context 'when debug_trace param is provided' do
    let(:metadata) { Ci::BuildMetadata.find_by(build_id: build) }
    let(:stream_size) { 192.kilobytes }
    let(:body_data) { 'x' * stream_size }
    let(:content_range) { "#{body_start}-#{stream_size}" }

    context 'when sending the first trace' do
      let(:body_start) { 0 }

      it 'updates build metadata debug_trace_enabled' do
        described_class
          .new(build, content_range: content_range, debug_trace: true)
          .execute(body_data)

        expect(metadata.debug_trace_enabled).to be(true)
      end
    end

    context 'when sending the second trace' do
      let(:body_start) { 1 }

      it 'does not update build metadata debug_trace_enabled', :aggregate_failures do
        query_recorder = ActiveRecord::QueryRecorder.new do
          described_class.new(build, content_range: content_range, debug_trace: true).execute(body_data)
        end

        expect(metadata.debug_trace_enabled).to be(false)
        expect(query_recorder.log).not_to include(/p_ci_builds_metadata/)
      end
    end
  end
end