2018-01-29 16:56:12 +00:00
|
|
|
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
|
2018-02-23 14:31:01 +00:00
|
|
|
let!(:job) { create(:ci_build, :trace_live) }
|
|
|
|
let!(:legacy_path) { job.trace.read { |stream| return stream.path } }
|
2018-02-23 14:58:58 +00:00
|
|
|
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 }
|
2018-01-29 16:56:12 +00:00
|
|
|
|
2018-02-23 15:02:13 +00:00
|
|
|
it { expect(File.exist?(legacy_path)).to be_truthy }
|
2018-01-29 16:56:12 +00:00
|
|
|
|
|
|
|
it 'creates trace artifact' do
|
|
|
|
expect { subject }.to change { Ci::JobArtifact.count }.by(1)
|
|
|
|
|
2018-02-23 15:02:13 +00:00
|
|
|
expect(File.exist?(legacy_path)).to be_falsy
|
|
|
|
expect(File.exist?(new_path)).to be_truthy
|
2018-02-23 14:58:58 +00:00
|
|
|
expect(new_checksum).to eq(legacy_checksum)
|
|
|
|
expect(job.job_artifacts_trace.file.exists?).to be_truthy
|
2018-02-23 14:31:01 +00:00
|
|
|
expect(job.job_artifacts_trace.file.filename).to eq('job.log')
|
2018-01-29 16:56:12 +00:00
|
|
|
end
|
|
|
|
|
2018-02-23 14:31:01 +00:00
|
|
|
context 'when failed to create trace artifact record' do
|
2018-01-29 16:56:12 +00:00
|
|
|
before do
|
2018-02-23 14:31:01 +00:00
|
|
|
# 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
|
2018-01-29 16:56:12 +00:00
|
|
|
end
|
|
|
|
|
2018-02-23 14:31:01 +00:00
|
|
|
it 'keeps legacy trace and removes trace artifact' do
|
2018-02-23 15:02:13 +00:00
|
|
|
expect(File.exist?(legacy_path)).to be_truthy
|
2018-02-23 14:31:01 +00:00
|
|
|
expect(job.job_artifacts_trace).to be_nil
|
2018-01-29 16:56:12 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the job does not have a trace file' do
|
2018-02-23 14:31:01 +00:00
|
|
|
let!(:job) { create(:ci_build) }
|
|
|
|
|
2018-01-29 16:56:12 +00:00
|
|
|
it 'does not create trace artifact' do
|
|
|
|
expect { subject }.not_to change { Ci::JobArtifact.count }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-02-23 14:31:01 +00:00
|
|
|
|
|
|
|
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
|
2018-01-29 16:56:12 +00:00
|
|
|
end
|
|
|
|
end
|