summaryrefslogtreecommitdiff
path: root/spec/services/ci/create_trace_artifact_service_spec.rb
blob: dc22165c9e202f2fe94ddad4496316478bbe4886 (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
require 'spec_helper'

describe Ci::CreateTraceArtifactService do
  describe '#execute' do
    subject { described_class.new(nil, nil).execute(job) }

    context 'when the job does not have trace artifact' do
      context 'when the job has a trace file' do
        let!(:job) { create(:ci_build, :trace_live) }
        let!(:legacy_path) { job.trace.read { |stream| return stream.path } }
        let!(:legacy_checksum) { Digest::SHA256.file(legacy_path).hexdigest }
        let(:new_path) { job.job_artifacts_trace.file.path }
        let(:new_checksum) { Digest::SHA256.file(new_path).hexdigest }

        it { expect(File.exist?(legacy_path)).to be_truthy }

        it 'creates trace artifact' do
          expect { subject }.to change { Ci::JobArtifact.count }.by(1)

          expect(File.exist?(legacy_path)).to be_falsy
          expect(File.exist?(new_path)).to be_truthy
          expect(new_checksum).to eq(legacy_checksum)
          expect(job.job_artifacts_trace.file.exists?).to be_truthy
          expect(job.job_artifacts_trace.file.filename).to eq('job.log')
        end

        context 'when failed to create trace artifact record' do
          before do
            # When ActiveRecord error happens
            allow_any_instance_of(Ci::JobArtifact).to receive(:save).and_return(false)
            allow_any_instance_of(Ci::JobArtifact).to receive_message_chain(:errors, :full_messages)
              .and_return("Error")

            subject rescue nil

            job.reload
          end

          it 'keeps legacy trace and removes trace artifact' do
            expect(File.exist?(legacy_path)).to be_truthy
            expect(job.job_artifacts_trace).to be_nil
          end
        end

        context 'when migrated trace artifact file is not found' do
          before do
            allow_any_instance_of(CarrierWave::SanitizedFile).to receive(:exists?) { false }
          end

          it 'raises an error' do
            expect { subject }.to raise_error('Trace artifact not found')

            expect(File.exist?(legacy_path)).to be_truthy
          end
        end
      end

      context 'when the job does not have a trace file' do
        let!(:job) { create(:ci_build) }

        it 'does not create trace artifact' do
          expect { subject }.not_to change { Ci::JobArtifact.count }
        end
      end
    end

    context 'when the job has already had trace artifact' do
      let!(:job) { create(:ci_build, :trace_artifact) }

      it 'does not create trace artifact' do
        expect { subject }.not_to change { Ci::JobArtifact.count }
      end
    end
  end
end