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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Ci::Artifactable do
let(:ci_job_artifact) { build(:ci_job_artifact) }
describe 'artifact properties are included' do
context 'when enum is defined' do
subject { ci_job_artifact }
it { is_expected.to define_enum_for(:file_format).with_values(raw: 1, zip: 2, gzip: 3).with_suffix }
end
context 'when const is defined' do
subject { ci_job_artifact.class }
it { is_expected.to be_const_defined(:FILE_FORMAT_ADAPTERS) }
end
end
describe '#each_blob' do
context 'when file format is gzip' do
context 'when gzip file contains one file' do
let(:artifact) { build(:ci_job_artifact, :junit) }
it 'iterates blob once' do
expect { |b| artifact.each_blob(&b) }.to yield_control.once
end
end
context 'when gzip file contains three files' do
let(:artifact) { build(:ci_job_artifact, :junit_with_three_testsuites) }
it 'iterates blob three times' do
expect { |b| artifact.each_blob(&b) }.to yield_control.exactly(3).times
end
end
end
context 'when file format is raw' do
let(:artifact) { build(:ci_job_artifact, :codequality, file_format: :raw) }
it 'iterates blob once' do
expect { |b| artifact.each_blob(&b) }.to yield_control.once
end
end
context 'when file format is zip' do
context 'when artifact contains one file' do
let(:artifact) { build(:ci_job_artifact, :zip_with_single_file) }
it 'iterates blob once' do
expect { |b| artifact.each_blob(&b) }.to yield_control.once
end
end
context 'when artifact contains two files' do
let(:artifact) { build(:ci_job_artifact, :zip_with_multiple_files) }
it 'iterates blob two times' do
expect { |b| artifact.each_blob(&b) }.to yield_control.exactly(2).times
end
end
end
context 'when there are no adapters for the file format' do
let(:artifact) { build(:ci_job_artifact, :junit) }
before do
allow(artifact).to receive(:file_format).and_return(:unknown)
end
it 'raises an error' do
expect { |b| artifact.each_blob(&b) }.to raise_error(described_class::NotSupportedAdapterError)
end
end
context 'pushes artifact_size to application context' do
let(:artifact) { create(:ci_job_artifact, :junit) }
it 'logs artifact size', :aggregate_failures do
expect { |b| artifact.each_blob(&b) }.to yield_control.once
expect(Gitlab::ApplicationContext.current).to include("meta.artifact_size" => artifact.size)
end
end
end
context 'ActiveRecord scopes' do
let_it_be(:recently_expired_artifact) { create(:ci_job_artifact, expire_at: 1.day.ago) }
let_it_be(:later_expired_artifact) { create(:ci_job_artifact, expire_at: 2.days.ago) }
let_it_be(:not_expired_artifact) { create(:ci_job_artifact, expire_at: 1.day.from_now) }
describe '.expired_before' do
it 'returns expired artifacts' do
expect(Ci::JobArtifact.expired_before(1.hour.ago))
.to match_array([recently_expired_artifact, later_expired_artifact])
end
end
describe '.expired' do
it 'returns all expired artifacts' do
expect(Ci::JobArtifact.expired).to contain_exactly(recently_expired_artifact, later_expired_artifact)
end
end
describe '.with_files_stored_locally' do
it 'returns artifacts stored locally' do
expect(Ci::JobArtifact.with_files_stored_locally).to contain_exactly(recently_expired_artifact, later_expired_artifact, not_expired_artifact)
end
end
describe '.with_files_stored_remotely' do
let(:remote_artifact) { create(:ci_job_artifact, :remote_store) }
before do
stub_artifacts_object_storage
end
it 'returns artifacts stored remotely' do
expect(Ci::JobArtifact.with_files_stored_remotely).to contain_exactly(remote_artifact)
end
end
describe '.project_id_in' do
context 'when artifacts belongs to projects' do
let(:project_ids) { [recently_expired_artifact.project.id, not_expired_artifact.project.id, non_existing_record_id] }
it 'returns artifacts belonging to projects' do
expect(Ci::JobArtifact.project_id_in(project_ids)).to contain_exactly(recently_expired_artifact, not_expired_artifact)
end
end
end
end
end
|