diff options
author | Shinya Maeda <shinya@gitlab.com> | 2018-02-28 03:44:13 +0900 |
---|---|---|
committer | Shinya Maeda <shinya@gitlab.com> | 2018-03-06 21:43:19 +0900 |
commit | 011b849719cd0afc91c5cfbe6e7f46bce4611983 (patch) | |
tree | 347ee89821931f5bddd00948405efbbf0ff492bc /spec/lib | |
parent | c645d40a56a8291e61a597e956bb2a3659fe123a (diff) | |
download | gitlab-ce-011b849719cd0afc91c5cfbe6e7f46bce4611983.tar.gz |
Add soec for achevie! method. Fixed the method
Diffstat (limited to 'spec/lib')
-rw-r--r-- | spec/lib/gitlab/ci/trace_spec.rb | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/spec/lib/gitlab/ci/trace_spec.rb b/spec/lib/gitlab/ci/trace_spec.rb index 91c9625ba06..e52f5fc39d4 100644 --- a/spec/lib/gitlab/ci/trace_spec.rb +++ b/spec/lib/gitlab/ci/trace_spec.rb @@ -399,4 +399,129 @@ describe Gitlab::Ci::Trace do end end end + + describe '#archive!' do + subject { trace.archive! } + + shared_examples 'archive trace file' do + it do + expect { subject }.to change { Ci::JobArtifact.count }.by(1) + + build.reload + expect(build.trace.exist?).to be_truthy + expect(build.job_artifacts_trace.file.exists?).to be_truthy + expect(build.job_artifacts_trace.file.filename).to eq('job.log') + expect(File.exist?(src_path)).to be_falsy + expect(src_checksum) + .to eq(Digest::SHA256.file(build.job_artifacts_trace.file.path).digest) + end + end + + shared_examples 'source trace file stays intact' do |error:| + it do + expect { subject }.to raise_error(error) + + build.reload + expect(build.trace.exist?).to be_truthy + expect(build.job_artifacts_trace).to be_nil + expect(File.exist?(src_path)).to be_truthy + end + end + + shared_examples 'archive trace in database' do + it do + expect { subject }.to change { Ci::JobArtifact.count }.by(1) + + build.reload + expect(build.trace.exist?).to be_truthy + expect(build.job_artifacts_trace.file.exists?).to be_truthy + expect(build.job_artifacts_trace.file.filename).to eq('job.log') + expect(build.old_trace).to be_nil + expect(src_checksum) + .to eq(Digest::SHA256.file(build.job_artifacts_trace.file.path).digest) + end + end + + shared_examples 'source trace in database stays intact' do |error:| + it do + expect { subject }.to raise_error(error) + + build.reload + expect(build.trace.exist?).to be_truthy + expect(build.job_artifacts_trace).to be_nil + expect(build.old_trace).to eq(trace_content) + end + end + + context 'when job does not have trace artifact' do + context 'when trace file stored in default path' do + let!(:build) { create(:ci_build, :trace_live) } + let!(:src_path) { trace.read { |s| return s.path } } + let!(:src_checksum) { Digest::SHA256.file(src_path).digest } + + it_behaves_like 'archive trace file' + + context 'when failed to create clone file' do + before do + allow_any_instance_of(Gitlab::Ci::Trace) + .to receive(:clone_file!).and_raise('Not all saved') + end + + it_behaves_like 'source trace file stays intact', error: 'Not all saved' + end + + context 'when failed to create job artifact record' do + before do + 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", 'Error']) + end + + it_behaves_like 'source trace file stays intact', error: ActiveRecord::RecordInvalid + end + end + + context 'when trace stored in database' do + let(:trace_content) { IO.read(expand_fixture_path('trace/sample_trace')) } + let!(:src_checksum) { Digest::SHA256.digest(trace_content) } + + before do + build.update_column(:trace, trace_content) + end + + it_behaves_like 'archive trace in database' + + context 'when failed to create clone file' do + before do + allow_any_instance_of(Gitlab::Ci::Trace) + .to receive(:clone_file!).and_raise('Not all saved') + end + + it_behaves_like 'source trace in database stays intact', error: 'Not all saved' + end + + context 'when failed to create job artifact record' do + before do + 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", 'Error']) + end + + it_behaves_like 'source trace in database stays intact', error: ActiveRecord::RecordInvalid + end + end + end + + context 'when job has trace artifact' do + before do + create(:ci_job_artifact, :trace, job: build) + end + + it 'does not archive' do + expect_any_instance_of(Gitlab::Ci::Trace).not_to receive(:archive_stream!) + expect { subject }.not_to change { Ci::JobArtifact.count } + expect(build.job_artifacts_trace.file.exists?).to be_truthy + end + end + end end |