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

job_trace_type_spec.rb « ci « types « graphql « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6d1214eafe653cd5e827e3baf61d451f3eeec7a5 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe GitlabSchema.types['CiJobTrace'], feature_category: :continuous_integration do
  include GraphqlHelpers

  let_it_be(:job) { create(:ci_build) }

  it 'has the correct fields' do
    expected_fields = [:html_summary]

    expect(described_class).to have_graphql_fields(*expected_fields)
  end

  describe 'htmlSummary' do
    subject(:resolved_field) { resolve_field(:html_summary, job.trace, args: args) }

    context 'when trace contains few lines' do
      before do
        job.trace.set('BUILD TRACE')
      end

      context 'when last_lines is set to 10' do
        let(:args) { { last_lines: 10 } }

        it 'shows the correct trace contents' do
          expect_next_instance_of(Gitlab::Ci::Trace) do |trace|
            expect(trace).to receive(:html).with(last_lines: 10, max_size: 16384).and_call_original
          end

          is_expected.to eq('<span>BUILD TRACE</span>')
        end
      end
    end

    context 'when trace contains many lines' do
      before do
        job.trace.set((1..200).map { |i| "Line #{i}" }.join("\n"))
      end

      def expected_html_trace_contents(line_count)
        "<span>#{((200 - (line_count - 1))..200).map { |i| "Line #{i}" }.join('<br/>')}</span>"
      end

      context 'when last_lines is not set' do
        let(:args) { {} }

        it 'shows the last 10 lines of trace contents' do
          expect_next_instance_of(Gitlab::Ci::Trace) do |trace|
            expect(trace).to receive(:html).with(last_lines: 10, max_size: 16384).and_call_original
          end

          is_expected.to eq expected_html_trace_contents(10)
        end
      end

      context 'when last_lines is set to a negative number' do
        let(:args) { { last_lines: -10 } }

        it 'shows the last line of trace contents' do
          expect_next_instance_of(Gitlab::Ci::Trace) do |trace|
            expect(trace).to receive(:html).with(last_lines: 1, max_size: 16384).and_call_original
          end

          is_expected.to eq expected_html_trace_contents(1)
        end
      end

      context 'when last_lines is set to 10' do
        let(:args) { { last_lines: 10 } }

        it 'shows the correct trace contents' do
          expect_next_instance_of(Gitlab::Ci::Trace) do |trace|
            expect(trace).to receive(:html).with(last_lines: 10, max_size: 16384).and_call_original
          end

          is_expected.to eq expected_html_trace_contents(10)
        end
      end

      context 'when last_lines is set to 150' do
        let(:args) { { last_lines: 150 } }

        it 'shows the last 100 lines of trace contents' do
          expect_next_instance_of(Gitlab::Ci::Trace) do |trace|
            expect(trace).to receive(:html).with(last_lines: 100, max_size: 16384).and_call_original
          end

          is_expected.to eq expected_html_trace_contents(100)
        end
      end
    end

    context 'when trace contains long lines' do
      before do
        # Creates lines of "aaaaaaaa...aaaaaaaa"
        job.trace.set((1..20).map { (1..1024).map { "a" }.join("") }.join("\n"))
      end

      context 'when last_lines is lower than 16KB' do
        let(:args) { {} }

        it 'shows the whole lines' do
          expect_next_instance_of(Gitlab::Ci::Trace) do |trace|
            expect(trace).to receive(:html).with(last_lines: 10, max_size: 16384).and_call_original
          end

          is_expected.to eq "<span>#{(1..10).map { (1..1024).map { 'a' }.join('') }.join('<br/>')}</span>"
        end
      end

      context 'when last_lines is higher than 16KB' do
        let(:args) { { last_lines: 20 } }

        it 'shows only the latest byte' do
          expect_next_instance_of(Gitlab::Ci::Trace) do |trace|
            expect(trace).to receive(:html).with(last_lines: 20, max_size: 16384).and_call_original
          end

          is_expected.to eq "<span>#{(1..1009).map { 'a' }.join('')}<br/>" \
                            "#{(1..15).map { (1..1024).map { 'a' }.join('') }.join('<br/>')}</span>"
        end
      end

      context 'when trace is cut in middle of a line' do
        let(:args) { {} }

        before do
          stub_const('Types::Ci::JobTraceType::MAX_SIZE_B', 1536)
        end

        it 'shows only the latest byte' do
          expect_next_instance_of(Gitlab::Ci::Trace) do |trace|
            expect(trace).to receive(:html).with(last_lines: 10, max_size: 1536).and_call_original
          end

          is_expected.to eq "<span>#{(1..511).map { 'a' }.join('')}<br/>#{(1..1024).map { 'a' }.join('')}</span>"
        end
      end

      context 'when trace is cut at end of a line' do
        let(:args) { {} }

        before do
          stub_const('Types::Ci::JobTraceType::MAX_SIZE_B', 2050)
        end

        it 'shows only the latest byte' do
          expect_next_instance_of(Gitlab::Ci::Trace) do |trace|
            expect(trace).to receive(:html).with(last_lines: 10, max_size: 2050).and_call_original
          end

          is_expected.to eq "<span><br/>#{(1..2).map { (1..1024).map { 'a' }.join('') }.join('<br/>')}</span>"
        end
      end
    end

    context 'when trace contains multi-bytes UTF-8' do
      before do
        # Creates lines of 4 pound symbol, pound symbol is 2 byte wise in UTF-8
        # Append an "a" (1 byte character) at the end to cut in the middle of UTF-8
        job.trace.set((1..20).map { (1..4).map { "£" }.join("") }.join("\n"))
      end

      context 'when cut in the middle of a codepoint' do
        before do
          stub_const('Types::Ci::JobTraceType::MAX_SIZE_B', 5)
        end

        let(:args) { {} }

        it 'shows a single "invalid utf-8" symbol' do
          expect_next_instance_of(Gitlab::Ci::Trace) do |trace|
            expect(trace).to receive(:html).with(last_lines: 10, max_size: 5).and_call_original
          end

          is_expected.to eq "<span>�££</span>"
        end
      end
    end
  end
end