chunked_io_spec.rb 11.4 KB
Newer Older
1 2
# frozen_string_literal: true

Shinya Maeda's avatar
Shinya Maeda committed
3 4
require 'spec_helper'

5
RSpec.describe Gitlab::Ci::Trace::ChunkedIO, :clean_gitlab_redis_cache do
Shinya Maeda's avatar
Shinya Maeda committed
6 7
  include ChunkedIOHelpers

8
  let_it_be(:build) { create(:ci_build, :running) }
9

10
  let(:chunked_io) { described_class.new(build) }
Shinya Maeda's avatar
Shinya Maeda committed
11 12

  before do
13
    stub_feature_flags(ci_enable_live_trace: true)
Shinya Maeda's avatar
Shinya Maeda committed
14 15
  end

Ryan Cobb's avatar
Ryan Cobb committed
16
  describe "#initialize" do
Shinya Maeda's avatar
Shinya Maeda committed
17 18
    context 'when a chunk exists' do
      before do
19
        build.trace.set('ABC')
Shinya Maeda's avatar
Shinya Maeda committed
20 21 22 23 24 25 26 27
      end

      it { expect(chunked_io.size).to eq(3) }
    end

    context 'when two chunks exist' do
      before do
        stub_buffer_size(4)
28
        build.trace.set('ABCDEF')
Shinya Maeda's avatar
Shinya Maeda committed
29 30 31 32 33 34 35 36 37 38
      end

      it { expect(chunked_io.size).to eq(6) }
    end

    context 'when no chunks exists' do
      it { expect(chunked_io.size).to eq(0) }
    end
  end

Ryan Cobb's avatar
Ryan Cobb committed
39
  describe "#seek" do
Shinya Maeda's avatar
Shinya Maeda committed
40 41 42
    subject { chunked_io.seek(pos, where) }

    before do
43
      build.trace.set(sample_trace_raw)
Shinya Maeda's avatar
Shinya Maeda committed
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
    end

    context 'when moves pos to end of the file' do
      let(:pos) { 0 }
      let(:where) { IO::SEEK_END }

      it { is_expected.to eq(sample_trace_raw.bytesize) }
    end

    context 'when moves pos to middle of the file' do
      let(:pos) { sample_trace_raw.bytesize / 2 }
      let(:where) { IO::SEEK_SET }

      it { is_expected.to eq(pos) }
    end

    context 'when moves pos around' do
      it 'matches the result' do
        expect(chunked_io.seek(0)).to eq(0)
        expect(chunked_io.seek(100, IO::SEEK_CUR)).to eq(100)
        expect { chunked_io.seek(sample_trace_raw.bytesize + 1, IO::SEEK_CUR) }
          .to raise_error('new position is outside of file')
      end
    end
  end

Ryan Cobb's avatar
Ryan Cobb committed
70
  describe "#eof?" do
Shinya Maeda's avatar
Shinya Maeda committed
71 72 73
    subject { chunked_io.eof? }

    before do
74
      build.trace.set(sample_trace_raw)
Shinya Maeda's avatar
Shinya Maeda committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    end

    context 'when current pos is at end of the file' do
      before do
        chunked_io.seek(sample_trace_raw.bytesize, IO::SEEK_SET)
      end

      it { is_expected.to be_truthy }
    end

    context 'when current pos is not at end of the file' do
      before do
        chunked_io.seek(0, IO::SEEK_SET)
      end

      it { is_expected.to be_falsey }
    end
  end

Ryan Cobb's avatar
Ryan Cobb committed
94
  describe "#each_line" do
Shinya Maeda's avatar
Shinya Maeda committed
95 96 97 98 99
    let(:string_io) { StringIO.new(sample_trace_raw) }

    context 'when buffer size is smaller than file size' do
      before do
        stub_buffer_size(sample_trace_raw.bytesize / 2)
100
        build.trace.set(sample_trace_raw)
Shinya Maeda's avatar
Shinya Maeda committed
101 102 103 104 105 106 107 108 109 110 111
      end

      it 'yields lines' do
        expect { |b| chunked_io.each_line(&b) }
          .to yield_successive_args(*string_io.each_line.to_a)
      end
    end

    context 'when buffer size is larger than file size' do
      before do
        stub_buffer_size(sample_trace_raw.bytesize * 2)
112
        build.trace.set(sample_trace_raw)
Shinya Maeda's avatar
Shinya Maeda committed
113 114 115
      end

      it 'calls get_chunk only once' do
116 117 118
        expect_next_instance_of(Gitlab::Ci::Trace::ChunkedIO) do |instance|
          expect(instance).to receive(:current_chunk).once.and_call_original
        end
Shinya Maeda's avatar
Shinya Maeda committed
119 120 121 122

        chunked_io.each_line { |line| }
      end
    end
123 124 125 126 127 128 129 130 131 132 133 134 135

    context 'when buffer consist of many empty lines' do
      let(:sample_trace_raw) { Array.new(10, "   ").join("\n") }

      before do
        build.trace.set(sample_trace_raw)
      end

      it 'yields lines' do
        expect { |b| chunked_io.each_line(&b) }
          .to yield_successive_args(*string_io.each_line.to_a)
      end
    end
Shinya Maeda's avatar
Shinya Maeda committed
136 137
  end

Ryan Cobb's avatar
Ryan Cobb committed
138
  describe "#read" do
Shinya Maeda's avatar
Shinya Maeda committed
139 140 141 142 143 144 145 146
    subject { chunked_io.read(length) }

    context 'when read the whole size' do
      let(:length) { nil }

      context 'when buffer size is smaller than file size' do
        before do
          stub_buffer_size(sample_trace_raw.bytesize / 2)
147
          build.trace.set(sample_trace_raw)
Shinya Maeda's avatar
Shinya Maeda committed
148 149 150 151 152 153 154 155
        end

        it { is_expected.to eq(sample_trace_raw) }
      end

      context 'when buffer size is larger than file size' do
        before do
          stub_buffer_size(sample_trace_raw.bytesize * 2)
156
          build.trace.set(sample_trace_raw)
Shinya Maeda's avatar
Shinya Maeda committed
157 158 159 160 161 162
        end

        it { is_expected.to eq(sample_trace_raw) }
      end
    end

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    context 'when chunk is missing data' do
      let(:length) { nil }

      before do
        stub_buffer_size(1024)
        build.trace.set(sample_trace_raw)

        # make second chunk to not have data
        build.trace_chunks.second.append('', 0)
      end

      it 'raises an error' do
        expect { subject }.to raise_error described_class::FailedToGetChunkError
      end
    end

Shinya Maeda's avatar
Shinya Maeda committed
179 180 181 182 183 184
    context 'when read only first 100 bytes' do
      let(:length) { 100 }

      context 'when buffer size is smaller than file size' do
        before do
          stub_buffer_size(sample_trace_raw.bytesize / 2)
185
          build.trace.set(sample_trace_raw)
Shinya Maeda's avatar
Shinya Maeda committed
186 187 188 189 190 191 192 193 194 195
        end

        it 'reads a trace' do
          is_expected.to eq(sample_trace_raw.byteslice(0, length))
        end
      end

      context 'when buffer size is larger than file size' do
        before do
          stub_buffer_size(sample_trace_raw.bytesize * 2)
196
          build.trace.set(sample_trace_raw)
Shinya Maeda's avatar
Shinya Maeda committed
197 198 199 200 201 202 203 204 205 206 207 208 209 210
        end

        it 'reads a trace' do
          is_expected.to eq(sample_trace_raw.byteslice(0, length))
        end
      end
    end

    context 'when tries to read oversize' do
      let(:length) { sample_trace_raw.bytesize + 1000 }

      context 'when buffer size is smaller than file size' do
        before do
          stub_buffer_size(sample_trace_raw.bytesize / 2)
211
          build.trace.set(sample_trace_raw)
Shinya Maeda's avatar
Shinya Maeda committed
212 213 214 215 216 217 218 219 220 221
        end

        it 'reads a trace' do
          is_expected.to eq(sample_trace_raw)
        end
      end

      context 'when buffer size is larger than file size' do
        before do
          stub_buffer_size(sample_trace_raw.bytesize * 2)
222
          build.trace.set(sample_trace_raw)
Shinya Maeda's avatar
Shinya Maeda committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236
        end

        it 'reads a trace' do
          is_expected.to eq(sample_trace_raw)
        end
      end
    end

    context 'when tries to read 0 bytes' do
      let(:length) { 0 }

      context 'when buffer size is smaller than file size' do
        before do
          stub_buffer_size(sample_trace_raw.bytesize / 2)
237
          build.trace.set(sample_trace_raw)
Shinya Maeda's avatar
Shinya Maeda committed
238 239 240 241 242 243 244 245 246 247
        end

        it 'reads a trace' do
          is_expected.to be_empty
        end
      end

      context 'when buffer size is larger than file size' do
        before do
          stub_buffer_size(sample_trace_raw.bytesize * 2)
248
          build.trace.set(sample_trace_raw)
Shinya Maeda's avatar
Shinya Maeda committed
249 250 251 252 253 254 255 256 257
        end

        it 'reads a trace' do
          is_expected.to be_empty
        end
      end
    end
  end

Ryan Cobb's avatar
Ryan Cobb committed
258
  describe "#readline" do
Shinya Maeda's avatar
Shinya Maeda committed
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
    subject { chunked_io.readline }

    let(:string_io) { StringIO.new(sample_trace_raw) }

    shared_examples 'all line matching' do
      it do
        (0...sample_trace_raw.lines.count).each do
          expect(chunked_io.readline).to eq(string_io.readline)
        end
      end
    end

    context 'when buffer size is smaller than file size' do
      before do
        stub_buffer_size(sample_trace_raw.bytesize / 2)
274
        build.trace.set(sample_trace_raw)
Shinya Maeda's avatar
Shinya Maeda committed
275 276 277 278 279 280 281 282
      end

      it_behaves_like 'all line matching'
    end

    context 'when buffer size is larger than file size' do
      before do
        stub_buffer_size(sample_trace_raw.bytesize * 2)
283
        build.trace.set(sample_trace_raw)
Shinya Maeda's avatar
Shinya Maeda committed
284 285 286 287 288 289 290 291
      end

      it_behaves_like 'all line matching'
    end

    context 'when pos is at middle of the file' do
      before do
        stub_buffer_size(sample_trace_raw.bytesize / 2)
292
        build.trace.set(sample_trace_raw)
Shinya Maeda's avatar
Shinya Maeda committed
293 294 295 296 297 298 299 300 301

        chunked_io.seek(chunked_io.size / 2)
        string_io.seek(string_io.size / 2)
      end

      it 'reads from pos' do
        expect(chunked_io.readline).to eq(string_io.readline)
      end
    end
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318

    context 'when chunk is missing data' do
      let(:length) { nil }

      before do
        build.trace.set(sample_trace_raw)

        # make first chunk to have invalid data
        build.trace_chunks.first.append('data', 0)
      end

      it 'raises an error' do
        expect { subject }.to raise_error described_class::FailedToGetChunkError
      end
    end

    context 'when utf-8 is being used' do
319
      let(:sample_trace_raw) { sample_trace_raw_utf8.dup.force_encoding(Encoding::BINARY) }
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
      let(:sample_trace_raw_utf8) { "😺\n😺\n😺\n😺" }

      before do
        stub_buffer_size(3) # the utf-8 character has 4 bytes

        build.trace.set(sample_trace_raw_utf8)
      end

      it 'has known length' do
        expect(sample_trace_raw_utf8.bytesize).to eq(4 * 4 + 3 * 1)
        expect(sample_trace_raw.bytesize).to eq(4 * 4 + 3 * 1)
        expect(chunked_io.size).to eq(4 * 4 + 3 * 1)
      end

      it_behaves_like 'all line matching'
    end
Shinya Maeda's avatar
Shinya Maeda committed
336 337
  end

Ryan Cobb's avatar
Ryan Cobb committed
338
  describe "#write" do
Shinya Maeda's avatar
Shinya Maeda committed
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
    subject { chunked_io.write(data) }

    let(:data) { sample_trace_raw }

    context 'when data does not exist' do
      shared_examples 'writes a trace' do
        it do
          is_expected.to eq(data.bytesize)

          chunked_io.seek(0, IO::SEEK_SET)
          expect(chunked_io.read).to eq(data)
        end
      end

      context 'when buffer size is smaller than file size' do
        before do
          stub_buffer_size(data.bytesize / 2)
        end

        it_behaves_like 'writes a trace'
      end

      context 'when buffer size is larger than file size' do
        before do
          stub_buffer_size(data.bytesize * 2)
        end

        it_behaves_like 'writes a trace'
      end
    end

    context 'when data already exists' do
      let(:exist_data) { 'exist data' }

      shared_examples 'appends a trace' do
        it do
          chunked_io.seek(0, IO::SEEK_END)
          is_expected.to eq(data.bytesize)

          chunked_io.seek(0, IO::SEEK_SET)
          expect(chunked_io.read).to eq(exist_data + data)
        end
      end

      context 'when buffer size is smaller than file size' do
        before do
          stub_buffer_size(sample_trace_raw.bytesize / 2)
386
          build.trace.set(exist_data)
Shinya Maeda's avatar
Shinya Maeda committed
387 388 389 390 391 392 393 394
        end

        it_behaves_like 'appends a trace'
      end

      context 'when buffer size is larger than file size' do
        before do
          stub_buffer_size(sample_trace_raw.bytesize * 2)
395
          build.trace.set(exist_data)
Shinya Maeda's avatar
Shinya Maeda committed
396 397 398 399 400 401 402
        end

        it_behaves_like 'appends a trace'
      end
    end
  end

Ryan Cobb's avatar
Ryan Cobb committed
403
  describe "#truncate" do
Shinya Maeda's avatar
Shinya Maeda committed
404 405 406 407 408
    let(:offset) { 10 }

    context 'when data does not exist' do
      shared_examples 'truncates a trace' do
        it do
409
          chunked_io.truncate(offset)
Shinya Maeda's avatar
Shinya Maeda committed
410 411 412 413 414 415 416 417 418

          chunked_io.seek(0, IO::SEEK_SET)
          expect(chunked_io.read).to eq(sample_trace_raw.byteslice(0, offset))
        end
      end

      context 'when buffer size is smaller than file size' do
        before do
          stub_buffer_size(sample_trace_raw.bytesize / 2)
419
          build.trace.set(sample_trace_raw)
Shinya Maeda's avatar
Shinya Maeda committed
420 421 422 423 424 425 426 427
        end

        it_behaves_like 'truncates a trace'
      end

      context 'when buffer size is larger than file size' do
        before do
          stub_buffer_size(sample_trace_raw.bytesize * 2)
428
          build.trace.set(sample_trace_raw)
Shinya Maeda's avatar
Shinya Maeda committed
429 430 431 432 433 434 435
        end

        it_behaves_like 'truncates a trace'
      end
    end
  end

Ryan Cobb's avatar
Ryan Cobb committed
436
  describe "#destroy!" do
Shinya Maeda's avatar
Shinya Maeda committed
437 438 439
    subject { chunked_io.destroy! }

    before do
440
      build.trace.set(sample_trace_raw)
Shinya Maeda's avatar
Shinya Maeda committed
441 442 443 444 445 446
    end

    it 'deletes' do
      expect { subject }.to change { chunked_io.size }
        .from(sample_trace_raw.bytesize).to(0)

447
      expect(Ci::BuildTraceChunk.where(build: build).count).to eq(0)
Shinya Maeda's avatar
Shinya Maeda committed
448
    end
449 450 451 452 453 454 455 456 457 458

    context 'when the job does not have archived trace' do
      it 'leaves a message in sidekiq log' do
        expect(Sidekiq.logger).to receive(:warn).with(
          message: 'The job does not have archived trace but going to be destroyed.',
          job_id: build.id).and_call_original

        subject
      end
    end
Shinya Maeda's avatar
Shinya Maeda committed
459 460
  end
end