create_trace_artifact_service_spec.rb 2.2 KB
Newer Older
S
Shinya Maeda 已提交
1 2 3 4 5 6 7 8
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
9 10
        let!(:job) { create(:ci_build, :trace_live) }
        let!(:legacy_path) { job.trace.read { |stream| return stream.path } }
11 12 13
        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 }
S
Shinya Maeda 已提交
14

S
Shinya Maeda 已提交
15
        it { expect(File.exist?(legacy_path)).to be_truthy }
S
Shinya Maeda 已提交
16 17 18 19

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

S
Shinya Maeda 已提交
20 21
          expect(File.exist?(legacy_path)).to be_falsy
          expect(File.exist?(new_path)).to be_truthy
22 23
          expect(new_checksum).to eq(legacy_checksum)
          expect(job.job_artifacts_trace.file.exists?).to be_truthy
24
          expect(job.job_artifacts_trace.file.filename).to eq('job.log')
S
Shinya Maeda 已提交
25 26
        end

27
        context 'when failed to create trace artifact record' do
S
Shinya Maeda 已提交
28
          before do
29 30 31 32 33 34 35 36
            # 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
S
Shinya Maeda 已提交
37 38
          end

39
          it 'keeps legacy trace and removes trace artifact' do
S
Shinya Maeda 已提交
40
            expect(File.exist?(legacy_path)).to be_truthy
41
            expect(job.job_artifacts_trace).to be_nil
S
Shinya Maeda 已提交
42 43 44 45 46
          end
        end
      end

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

S
Shinya Maeda 已提交
49 50 51 52 53
        it 'does not create trace artifact' do
          expect { subject }.not_to change { Ci::JobArtifact.count }
        end
      end
    end
54 55 56 57 58 59 60 61

    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
S
Shinya Maeda 已提交
62 63
  end
end