summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ci/trace/stream_spec.rb
blob: af519f4bae6a1638b8d356b7979d25a0897e1c5c (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
require 'spec_helper'

describe Gitlab::Ci::Trace::Stream, :clean_gitlab_redis_cache do
  set(:build) { create(:ci_build, :running) }

  before do
    stub_feature_flags(ci_enable_live_trace: true)
  end

  describe 'delegates' do
    subject { described_class.new { nil } }

    it { is_expected.to delegate_method(:close).to(:stream) }
    it { is_expected.to delegate_method(:tell).to(:stream) }
    it { is_expected.to delegate_method(:seek).to(:stream) }
    it { is_expected.to delegate_method(:size).to(:stream) }
    it { is_expected.to delegate_method(:path).to(:stream) }
    it { is_expected.to delegate_method(:truncate).to(:stream) }
    it { is_expected.to delegate_method(:valid?).to(:stream).as(:present?) }
  end

  describe '#limit' do
    shared_examples_for 'limits' do
      it 'if size is larger we start from beginning' do
        stream.limit(20)

        expect(stream.tell).to eq(0)
      end

      it 'if size is smaller we start from the end' do
        stream.limit(2)

        expect(stream.raw).to eq("8")
      end

      context 'when the trace contains ANSI sequence and Unicode' do
        let(:stream) do
          described_class.new do
            File.open(expand_fixture_path('trace/ansi-sequence-and-unicode'))
          end
        end

        it 'forwards to the next linefeed, case 1' do
          stream.limit(7)

          result = stream.raw

          expect(result).to eq('')
          expect(result.encoding).to eq(Encoding.default_external)
        end

        it 'forwards to the next linefeed, case 2' do
          stream.limit(29)

          result = stream.raw

          expect(result).to eq("\e[01;32m許功蓋\e[0m\n")
          expect(result.encoding).to eq(Encoding.default_external)
        end

        # See https://gitlab.com/gitlab-org/gitlab-ce/issues/30796
        it 'reads in binary, output as Encoding.default_external' do
          stream.limit(52)

          result = stream.html

          expect(result).to eq(
            "<span>ヾ(´༎ຶД༎ຶ`)ノ<br/></span>"\
            "<span class=\"term-fg-green\">許功蓋</span>"\
            "<span><br/></span>")
          expect(result.encoding).to eq(Encoding.default_external)
        end
      end
    end

    context 'when stream is StringIO' do
      let(:stream) do
        described_class.new do
          StringIO.new((1..8).to_a.join("\n"))
        end
      end

      it_behaves_like 'limits'
    end

    context 'when stream is ChunkedIO' do
      let(:stream) do
        described_class.new do
          Gitlab::Ci::Trace::ChunkedIO.new(build).tap do |chunked_io|
            chunked_io.write((1..8).to_a.join("\n"))
            chunked_io.seek(0, IO::SEEK_SET)
          end
        end
      end

      it_behaves_like 'limits'
    end
  end

  describe '#append' do
    shared_examples_for 'appends' do
      it "truncates and append content" do
        stream.append("89", 4)
        stream.seek(0)

        expect(stream.size).to eq(6)
        expect(stream.raw).to eq("123489")
      end

      it 'appends in binary mode' do
        '😺'.force_encoding('ASCII-8BIT').each_char.with_index do |byte, offset|
          stream.append(byte, offset)
        end

        stream.seek(0)

        expect(stream.size).to eq(4)
        expect(stream.raw).to eq('😺')
      end
    end

    context 'when stream is Tempfile' do
      let(:tempfile) { Tempfile.new }

      let(:stream) do
        described_class.new do
          tempfile.write("12345678")
          tempfile.rewind
          tempfile
        end
      end

      after do
        tempfile.unlink
      end

      it_behaves_like 'appends'
    end

    context 'when stream is ChunkedIO' do
      let(:stream) do
        described_class.new do
          Gitlab::Ci::Trace::ChunkedIO.new(build).tap do |chunked_io|
            chunked_io.write('12345678')
            chunked_io.seek(0, IO::SEEK_SET)
          end
        end
      end

      it_behaves_like 'appends'
    end
  end

  describe '#set' do
    shared_examples_for 'sets' do
      before do
        stream.set("8901")
      end

      it "overwrite content" do
        stream.seek(0)

        expect(stream.size).to eq(4)
        expect(stream.raw).to eq("8901")
      end
    end

    context 'when stream is StringIO' do
      let(:stream) do
        described_class.new do
          StringIO.new("12345678")
        end
      end

      it_behaves_like 'sets'
    end

    context 'when stream is ChunkedIO' do
      let(:stream) do
        described_class.new do
          Gitlab::Ci::Trace::ChunkedIO.new(build).tap do |chunked_io|
            chunked_io.write('12345678')
            chunked_io.seek(0, IO::SEEK_SET)
          end
        end
      end

      it_behaves_like 'sets'
    end
  end

  describe '#raw' do
    shared_examples_for 'sets' do
      it 'returns all contents if last_lines is not specified' do
        result = stream.raw

        expect(result).to eq(lines.join)
        expect(result.encoding).to eq(Encoding.default_external)
      end

      context 'limit max lines' do
        before do
          # specifying BUFFER_SIZE forces to seek backwards
          allow(described_class).to receive(:BUFFER_SIZE)
            .and_return(2)
        end

        it 'returns last few lines' do
          result = stream.raw(last_lines: 2)

          expect(result).to eq(lines.last(2).join)
          expect(result.encoding).to eq(Encoding.default_external)
        end

        it 'returns everything if trying to get too many lines' do
          result = stream.raw(last_lines: lines.size * 2)

          expect(result).to eq(lines.join)
          expect(result.encoding).to eq(Encoding.default_external)
        end
      end
    end

    let(:path) { __FILE__ }
    let(:lines) { File.readlines(path) }

    context 'when stream is File' do
      let(:stream) do
        described_class.new do
          File.open(path)
        end
      end

      it_behaves_like 'sets'
    end

    context 'when stream is ChunkedIO' do
      let(:stream) do
        described_class.new do
          Gitlab::Ci::Trace::ChunkedIO.new(build).tap do |chunked_io|
            chunked_io.write(File.binread(path))
            chunked_io.seek(0, IO::SEEK_SET)
          end
        end
      end

      it_behaves_like 'sets'
    end
  end

  describe '#html_with_state' do
    shared_examples_for 'html_with_states' do
      it 'returns html content with state' do
        result = stream.html_with_state

        expect(result.html).to eq("<span>1234</span>")
      end

      context 'follow-up state' do
        let!(:last_result) { stream.html_with_state }

        before do
          data_stream.seek(4, IO::SEEK_SET)
          data_stream.write("5678")
          stream.seek(0)
        end

        it "returns appended trace" do
          result = stream.html_with_state(last_result.state)

          expect(result.append).to be_truthy
          expect(result.html).to eq("<span>5678</span>")
        end
      end
    end

    context 'when stream is StringIO' do
      let(:data_stream) do
        StringIO.new("1234")
      end

      let(:stream) do
        described_class.new { data_stream }
      end

      it_behaves_like 'html_with_states'
    end

    context 'when stream is ChunkedIO' do
      let(:data_stream) do
        Gitlab::Ci::Trace::ChunkedIO.new(build).tap do |chunked_io|
          chunked_io.write("1234")
          chunked_io.seek(0, IO::SEEK_SET)
        end
      end

      let(:stream) do
        described_class.new { data_stream }
      end

      it_behaves_like 'html_with_states'
    end
  end

  describe '#html' do
    shared_examples_for 'htmls' do
      it "returns html" do
        expect(stream.html).to eq("<span>12<br/>34<br/>56</span>")
      end

      it "returns html for last line only" do
        expect(stream.html(last_lines: 1)).to eq("<span>56</span>")
      end
    end

    context 'when stream is StringIO' do
      let(:stream) do
        described_class.new do
          StringIO.new("12\n34\n56")
        end
      end

      it_behaves_like 'htmls'
    end

    context 'when stream is ChunkedIO' do
      let(:stream) do
        described_class.new do
          Gitlab::Ci::Trace::ChunkedIO.new(build).tap do |chunked_io|
            chunked_io.write("12\n34\n56")
            chunked_io.seek(0, IO::SEEK_SET)
          end
        end
      end

      it_behaves_like 'htmls'
    end
  end

  describe '#extract_coverage' do
    shared_examples_for 'extract_coverages' do
      context 'valid content & regex' do
        let(:data) { 'Coverage 1033 / 1051 LOC (98.29%) covered' }
        let(:regex) { '\(\d+.\d+\%\) covered' }

        it { is_expected.to eq("98.29") }
      end

      context 'valid content & bad regex' do
        let(:data) { 'Coverage 1033 / 1051 LOC (98.29%) covered\n' }
        let(:regex) { 'very covered' }

        it { is_expected.to be_nil }
      end

      context 'no coverage content & regex' do
        let(:data) { 'No coverage for today :sad:' }
        let(:regex) { '\(\d+.\d+\%\) covered' }

        it { is_expected.to be_nil }
      end

      context 'multiple results in content & regex' do
        let(:data) do
          <<~HEREDOC
            (98.39%) covered
            (98.29%) covered
          HEREDOC
        end

        let(:regex) { '\(\d+.\d+\%\) covered' }

        it 'returns the last matched coverage' do
          is_expected.to eq("98.29")
        end
      end

      context 'when BUFFER_SIZE is smaller than stream.size' do
        let(:data) { 'Coverage 1033 / 1051 LOC (98.29%) covered\n' }
        let(:regex) { '\(\d+.\d+\%\) covered' }

        before do
          stub_const('Gitlab::Ci::Trace::Stream::BUFFER_SIZE', 5)
        end

        it { is_expected.to eq("98.29") }
      end

      context 'when regex is multi-byte char' do
        let(:data) { '95.0 ゴッドファット\n' }
        let(:regex) { '\d+\.\d+ ゴッドファット' }

        before do
          stub_const('Gitlab::Ci::Trace::Stream::BUFFER_SIZE', 5)
        end

        it { is_expected.to eq('95.0') }
      end

      context 'when BUFFER_SIZE is equal to stream.size' do
        let(:data) { 'Coverage 1033 / 1051 LOC (98.29%) covered\n' }
        let(:regex) { '\(\d+.\d+\%\) covered' }

        before do
          stub_const('Gitlab::Ci::Trace::Stream::BUFFER_SIZE', data.length)
        end

        it { is_expected.to eq("98.29") }
      end

      context 'using a regex capture' do
        let(:data) { 'TOTAL      9926   3489    65%' }
        let(:regex) { 'TOTAL\s+\d+\s+\d+\s+(\d{1,3}\%)' }

        it { is_expected.to eq("65") }
      end

      context 'malicious regexp' do
        let(:data) { malicious_text }
        let(:regex) { malicious_regexp_re2 }

        include_examples 'malicious regexp'
      end

      context 'multi-line data with rooted regexp' do
        let(:data) { "\n65%\n" }
        let(:regex) { '^(\d+)\%$' }

        it { is_expected.to eq('65') }
      end

      context 'long line' do
        let(:data) { 'a' * 80000 + '100%' + 'a' * 80000 }
        let(:regex) { '\d+\%' }

        it { is_expected.to eq('100') }
      end

      context 'many lines' do
        let(:data) { "foo\n" * 80000 + "100%\n" + "foo\n" * 80000 }
        let(:regex) { '\d+\%' }

        it { is_expected.to eq('100') }
      end

      context 'empty regex' do
        let(:data) { 'foo' }
        let(:regex) { '' }

        it 'skips processing' do
          expect(stream).not_to receive(:read)

          is_expected.to be_nil
        end
      end

      context 'nil regex' do
        let(:data) { 'foo' }
        let(:regex) { nil }

        it 'skips processing' do
          expect(stream).not_to receive(:read)

          is_expected.to be_nil
        end
      end
    end

    subject { stream.extract_coverage(regex) }

    context 'when stream is StringIO' do
      let(:stream) do
        described_class.new do
          StringIO.new(data)
        end
      end

      it_behaves_like 'extract_coverages'
    end

    context 'when stream is ChunkedIO' do
      let(:stream) do
        described_class.new do
          Gitlab::Ci::Trace::ChunkedIO.new(build).tap do |chunked_io|
            chunked_io.write(data)
            chunked_io.seek(0, IO::SEEK_SET)
          end
        end
      end

      it_behaves_like 'extract_coverages'
    end
  end
end