2016-01-05 02:18:23 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Ci::Build, models: true do
|
|
|
|
let(:build) { create(:ci_build) }
|
|
|
|
let(:test_trace) { 'This is a test' }
|
|
|
|
|
2016-11-16 18:23:05 -05:00
|
|
|
it { is_expected.to belong_to(:runner) }
|
|
|
|
it { is_expected.to belong_to(:trigger_request) }
|
|
|
|
it { is_expected.to belong_to(:erased_by) }
|
|
|
|
|
|
|
|
it { is_expected.to have_many(:deployments) }
|
2016-11-17 06:08:28 -05:00
|
|
|
|
2016-01-05 02:18:23 -05:00
|
|
|
describe '#trace' do
|
|
|
|
it 'obfuscates project runners token' do
|
|
|
|
allow(build).to receive(:raw_trace).and_return("Test: #{build.project.runners_token}")
|
|
|
|
|
2016-09-19 07:29:48 -04:00
|
|
|
expect(build.trace).to eq("Test: xxxxxxxxxxxxxxxxxxxx")
|
2016-01-05 02:18:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'empty project runners token' do
|
|
|
|
allow(build).to receive(:raw_trace).and_return(test_trace)
|
|
|
|
# runners_token can't normally be set to nil
|
|
|
|
allow(build.project).to receive(:runners_token).and_return(nil)
|
|
|
|
|
|
|
|
expect(build.trace).to eq(test_trace)
|
|
|
|
end
|
|
|
|
end
|
2016-08-29 08:10:14 -04:00
|
|
|
|
|
|
|
describe '#has_trace_file?' do
|
|
|
|
context 'when there is no trace' do
|
|
|
|
it { expect(build.has_trace_file?).to be_falsey }
|
|
|
|
it { expect(build.trace).to be_nil }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there is a trace' do
|
|
|
|
context 'when trace is stored in file' do
|
2016-08-30 06:47:31 -04:00
|
|
|
let(:build_with_trace) { create(:ci_build, :trace) }
|
2016-08-29 08:10:14 -04:00
|
|
|
|
2016-08-30 06:47:31 -04:00
|
|
|
it { expect(build_with_trace.has_trace_file?).to be_truthy }
|
|
|
|
it { expect(build_with_trace.trace).to eq('BUILD TRACE') }
|
2016-08-29 08:10:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when trace is stored in old file' do
|
|
|
|
before do
|
2016-08-30 06:47:31 -04:00
|
|
|
allow(build.project).to receive(:ci_id).and_return(999)
|
|
|
|
allow(File).to receive(:exist?).with(build.path_to_trace).and_return(false)
|
|
|
|
allow(File).to receive(:exist?).with(build.old_path_to_trace).and_return(true)
|
|
|
|
allow(File).to receive(:read).with(build.old_path_to_trace).and_return(test_trace)
|
2016-08-29 08:10:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(build.has_trace_file?).to be_truthy }
|
|
|
|
it { expect(build.trace).to eq(test_trace) }
|
|
|
|
end
|
|
|
|
|
2016-08-30 06:47:31 -04:00
|
|
|
context 'when trace is stored in DB' do
|
2016-08-29 08:10:14 -04:00
|
|
|
before do
|
2016-08-30 06:47:31 -04:00
|
|
|
allow(build.project).to receive(:ci_id).and_return(nil)
|
|
|
|
allow(build).to receive(:read_attribute).with(:trace).and_return(test_trace)
|
|
|
|
allow(File).to receive(:exist?).with(build.path_to_trace).and_return(false)
|
|
|
|
allow(File).to receive(:exist?).with(build.old_path_to_trace).and_return(false)
|
2016-08-29 08:10:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(build.has_trace_file?).to be_falsey }
|
|
|
|
it { expect(build.trace).to eq(test_trace) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-08-30 06:47:31 -04:00
|
|
|
|
|
|
|
describe '#trace_file_path' do
|
|
|
|
context 'when trace is stored in file' do
|
|
|
|
before do
|
|
|
|
allow(build).to receive(:has_trace_file?).and_return(true)
|
|
|
|
allow(build).to receive(:has_old_trace_file?).and_return(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(build.trace_file_path).to eq(build.path_to_trace) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when trace is stored in old file' do
|
|
|
|
before do
|
|
|
|
allow(build).to receive(:has_trace_file?).and_return(true)
|
|
|
|
allow(build).to receive(:has_old_trace_file?).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(build.trace_file_path).to eq(build.old_path_to_trace) }
|
|
|
|
end
|
|
|
|
end
|
2016-11-22 11:58:10 -05:00
|
|
|
|
|
|
|
describe '#update_project_statistics' do
|
|
|
|
let!(:build) { create(:ci_build, artifacts_size: 23) }
|
|
|
|
|
|
|
|
it 'updates project statistics when the artifact size changes' do
|
|
|
|
expect(ProjectCacheWorker).to receive(:perform_async)
|
|
|
|
.with(build.project_id, [], [:build_artifacts_size])
|
|
|
|
|
|
|
|
build.artifacts_size = 42
|
|
|
|
build.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not update project statistics when the artifact size stays the same' do
|
|
|
|
expect(ProjectCacheWorker).not_to receive(:perform_async)
|
|
|
|
|
|
|
|
build.name = 'changed'
|
|
|
|
build.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates project statistics when the build is destroyed' do
|
|
|
|
expect(ProjectCacheWorker).to receive(:perform_async)
|
|
|
|
.with(build.project_id, [], [:build_artifacts_size])
|
|
|
|
|
|
|
|
build.destroy
|
|
|
|
end
|
|
|
|
end
|
2016-01-05 02:18:23 -05:00
|
|
|
end
|