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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
# frozen_string_literal: true
RSpec.shared_examples 'when regex matching everything is specified' do
|delete_expectations:, service_response_extra: {}, supports_caching: false|
let(:params) do
{ 'name_regex_delete' => '.*' }
end
it_behaves_like 'removing the expected tags',
service_response_extra: service_response_extra,
supports_caching: supports_caching,
delete_expectations: delete_expectations
context 'with deprecated name_regex param' do
let(:params) do
{ 'name_regex' => '.*' }
end
it_behaves_like 'removing the expected tags',
service_response_extra: service_response_extra,
supports_caching: supports_caching,
delete_expectations: delete_expectations
end
end
RSpec.shared_examples 'when regex matching everything is specified and latest is not kept' do
|delete_expectations:, service_response_extra: {}, supports_caching: false|
let(:params) do
{ 'name_regex_delete' => '.*', 'keep_latest' => false }
end
it_behaves_like 'removing the expected tags',
service_response_extra: service_response_extra,
supports_caching: supports_caching,
delete_expectations: delete_expectations
end
RSpec.shared_examples 'when delete regex matching specific tags is used' do
|service_response_extra: {}, supports_caching: false|
let(:params) do
{ 'name_regex_delete' => 'C|D' }
end
it_behaves_like 'removing the expected tags',
service_response_extra: service_response_extra,
supports_caching: supports_caching,
delete_expectations: [%w[C D]]
end
RSpec.shared_examples 'when delete regex matching specific tags is used with overriding allow regex' do
|service_response_extra: {}, supports_caching: false|
let(:params) do
{
'name_regex_delete' => 'C|D',
'name_regex_keep' => 'C'
}
end
it_behaves_like 'removing the expected tags',
service_response_extra: service_response_extra,
supports_caching: supports_caching,
delete_expectations: [%w[D]]
context 'with name_regex_delete overriding deprecated name_regex' do
let(:params) do
{
'name_regex' => 'C|D',
'name_regex_delete' => 'D'
}
end
it_behaves_like 'removing the expected tags',
service_response_extra: service_response_extra,
supports_caching: supports_caching,
delete_expectations: [%w[D]]
end
end
RSpec.shared_examples 'with allow regex value' do
|delete_expectations:, service_response_extra: {}, supports_caching: false|
let(:params) do
{
'name_regex_delete' => '.*',
'name_regex_keep' => 'B.*'
}
end
it_behaves_like 'removing the expected tags',
service_response_extra: service_response_extra,
supports_caching: supports_caching,
delete_expectations: delete_expectations
end
RSpec.shared_examples 'when keeping only N tags' do
|delete_expectations:, service_response_extra: {}, supports_caching: false|
let(:params) do
{
'name_regex' => 'A|B.*|C',
'keep_n' => 1
}
end
it 'sorts tags by date' do
delete_expectations.each { |expectation| expect_delete(expectation) }
expect_no_caching unless supports_caching
expect(service).to receive(:order_by_date_desc).at_least(:once).and_call_original
is_expected.to eq(expected_service_response(deleted: delete_expectations.flatten).merge(service_response_extra))
end
end
RSpec.shared_examples 'when not keeping N tags' do
|delete_expectations:, service_response_extra: {}, supports_caching: false|
let(:params) do
{ 'name_regex' => 'A|B.*|C' }
end
it 'does not sort tags by date' do
delete_expectations.each { |expectation| expect_delete(expectation) }
expect_no_caching unless supports_caching
expect(service).not_to receive(:order_by_date_desc)
is_expected.to eq(expected_service_response(deleted: delete_expectations.flatten).merge(service_response_extra))
end
end
RSpec.shared_examples 'when removing keeping only 3' do
|delete_expectations:, service_response_extra: {}, supports_caching: false|
let(:params) do
{ 'name_regex_delete' => '.*',
'keep_n' => 3 }
end
it_behaves_like 'removing the expected tags',
service_response_extra: service_response_extra,
supports_caching: supports_caching,
delete_expectations: delete_expectations
end
RSpec.shared_examples 'when removing older than 1 day' do
|delete_expectations:, service_response_extra: {}, supports_caching: false|
let(:params) do
{
'name_regex_delete' => '.*',
'older_than' => '1 day'
}
end
it_behaves_like 'removing the expected tags',
service_response_extra: service_response_extra,
supports_caching: supports_caching,
delete_expectations: delete_expectations
context 'when update_chronic_duration is disabled' do
before do
stub_feature_flags(update_chronic_duration: false)
end
it_behaves_like 'removing the expected tags',
service_response_extra: service_response_extra,
supports_caching: supports_caching,
delete_expectations: delete_expectations
end
end
RSpec.shared_examples 'when combining all parameters' do
|delete_expectations:, service_response_extra: {}, supports_caching: false|
let(:params) do
{
'name_regex_delete' => '.*',
'keep_n' => 1,
'older_than' => '1 day'
}
end
it_behaves_like 'removing the expected tags',
service_response_extra: service_response_extra,
supports_caching: supports_caching,
delete_expectations: delete_expectations
end
RSpec.shared_examples 'when running a container_expiration_policy' do
|delete_expectations:, service_response_extra: {}, supports_caching: false|
let(:user) { nil }
context 'with valid container_expiration_policy param' do
let(:params) do
{
'name_regex_delete' => '.*',
'keep_n' => 1,
'older_than' => '1 day',
'container_expiration_policy' => true
}
end
it 'removes the expected tags' do
delete_expectations.each { |expectation| expect_delete(expectation, container_expiration_policy: true) }
expect_no_caching unless supports_caching
is_expected.to eq(expected_service_response(deleted: delete_expectations.flatten).merge(service_response_extra))
end
end
end
RSpec.shared_examples 'not removing anything' do |service_response_extra: {}, supports_caching: false|
it 'does not remove anything' do
expect(Projects::ContainerRepository::DeleteTagsService).not_to receive(:new)
expect_no_caching unless supports_caching
is_expected.to eq(expected_service_response(deleted: []).merge(service_response_extra))
end
end
RSpec.shared_examples 'removing the expected tags' do
|delete_expectations:, service_response_extra: {}, supports_caching: false|
it 'removes the expected tags' do
delete_expectations.each { |expectation| expect_delete(expectation) }
expect_no_caching unless supports_caching
is_expected.to eq(expected_service_response(deleted: delete_expectations.flatten).merge(service_response_extra))
end
end
|