2019-03-30 03:23:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-06-25 06:46:38 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Ci::BuildTraceChunks::Fog do
|
2018-06-25 06:46:38 -04:00
|
|
|
let(:data_store) { described_class.new }
|
2020-10-28 14:08:52 -04:00
|
|
|
let(:bucket) { 'artifacts' }
|
|
|
|
let(:connection_params) { Gitlab.config.artifacts.object_store.connection.symbolize_keys }
|
|
|
|
let(:connection) { ::Fog::Storage.new(connection_params) }
|
2018-06-25 06:46:38 -04:00
|
|
|
|
|
|
|
before do
|
2020-10-28 14:08:52 -04:00
|
|
|
stub_object_storage(connection_params: connection_params, remote_directory: bucket)
|
2018-06-25 06:46:38 -04:00
|
|
|
stub_artifacts_object_storage
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#available?' do
|
|
|
|
subject { data_store.available? }
|
|
|
|
|
|
|
|
context 'when object storage is enabled' do
|
|
|
|
it { is_expected.to be_truthy }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when object storage is disabled' do
|
|
|
|
before do
|
|
|
|
stub_artifacts_object_storage(enabled: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be_falsy }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#data' do
|
|
|
|
subject { data_store.data(model) }
|
|
|
|
|
|
|
|
context 'when data exists' do
|
|
|
|
let(:model) { create(:ci_build_trace_chunk, :fog_with_data, initial_data: 'sample data in fog') }
|
|
|
|
|
|
|
|
it 'returns the data' do
|
|
|
|
is_expected.to eq('sample data in fog')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when data does not exist' do
|
|
|
|
let(:model) { create(:ci_build_trace_chunk, :fog_without_data) }
|
|
|
|
|
|
|
|
it 'returns nil' do
|
2020-07-22 11:09:28 -04:00
|
|
|
expect(data_store.data(model)).to be_nil
|
2018-06-25 06:46:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#set_data' do
|
2020-08-27 20:10:34 -04:00
|
|
|
let(:new_data) { 'abc123' }
|
2018-06-25 06:46:38 -04:00
|
|
|
|
|
|
|
context 'when data exists' do
|
|
|
|
let(:model) { create(:ci_build_trace_chunk, :fog_with_data, initial_data: 'sample data in fog') }
|
|
|
|
|
|
|
|
it 'overwrites data' do
|
|
|
|
expect(data_store.data(model)).to eq('sample data in fog')
|
|
|
|
|
2020-08-27 20:10:34 -04:00
|
|
|
data_store.set_data(model, new_data)
|
2018-06-25 06:46:38 -04:00
|
|
|
|
2020-08-27 20:10:34 -04:00
|
|
|
expect(data_store.data(model)).to eq new_data
|
2018-06-25 06:46:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when data does not exist' do
|
|
|
|
let(:model) { create(:ci_build_trace_chunk, :fog_without_data) }
|
|
|
|
|
|
|
|
it 'sets new data' do
|
2020-07-22 11:09:28 -04:00
|
|
|
expect(data_store.data(model)).to be_nil
|
2018-06-25 06:46:38 -04:00
|
|
|
|
2020-08-27 20:10:34 -04:00
|
|
|
data_store.set_data(model, new_data)
|
2018-06-25 06:46:38 -04:00
|
|
|
|
2020-08-27 20:10:34 -04:00
|
|
|
expect(data_store.data(model)).to eq new_data
|
2018-06-25 06:46:38 -04:00
|
|
|
end
|
2020-11-19 04:09:38 -05:00
|
|
|
|
|
|
|
context 'when S3 server side encryption is enabled' do
|
|
|
|
before do
|
|
|
|
config = Gitlab.config.artifacts.object_store.to_h
|
|
|
|
config[:storage_options] = { server_side_encryption: 'AES256' }
|
|
|
|
allow(data_store).to receive(:object_store_raw_config).and_return(config)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a file with attributes' do
|
|
|
|
expect_next_instance_of(Fog::AWS::Storage::Files) do |files|
|
|
|
|
expect(files).to receive(:create).with(
|
|
|
|
hash_including(
|
|
|
|
key: anything,
|
|
|
|
body: new_data,
|
|
|
|
'x-amz-server-side-encryption' => 'AES256')
|
|
|
|
).and_call_original
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(data_store.data(model)).to be_nil
|
|
|
|
|
|
|
|
data_store.set_data(model, new_data)
|
|
|
|
|
|
|
|
expect(data_store.data(model)).to eq new_data
|
|
|
|
end
|
|
|
|
end
|
2018-06-25 06:46:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-17 11:10:03 -04:00
|
|
|
describe '#append_data' do
|
2021-06-24 08:08:07 -04:00
|
|
|
let(:initial_data) { (+'😺').force_encoding(Encoding::ASCII_8BIT) }
|
|
|
|
let(:model) { create(:ci_build_trace_chunk, :fog_with_data, initial_data: initial_data) }
|
2021-06-17 11:10:03 -04:00
|
|
|
let(:data) { data_store.data(model) }
|
|
|
|
|
2021-06-24 08:08:07 -04:00
|
|
|
context 'when ci_job_trace_force_encode is enabled' do
|
|
|
|
it 'appends ASCII data' do
|
|
|
|
data_store.append_data(model, +'hello world', 4)
|
2021-06-17 11:10:03 -04:00
|
|
|
|
2021-06-24 08:08:07 -04:00
|
|
|
expect(data.encoding).to eq(Encoding::ASCII_8BIT)
|
|
|
|
expect(data.force_encoding(Encoding::UTF_8)).to eq('😺hello world')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'appends UTF-8 data' do
|
|
|
|
data_store.append_data(model, +'Résumé', 4)
|
|
|
|
|
|
|
|
expect(data.encoding).to eq(Encoding::ASCII_8BIT)
|
|
|
|
expect(data.force_encoding(Encoding::UTF_8)).to eq("😺Résumé")
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when initial data is UTF-8' do
|
|
|
|
let(:initial_data) { +'😺' }
|
|
|
|
|
|
|
|
it 'appends ASCII data' do
|
|
|
|
data_store.append_data(model, +'hello world', 4)
|
|
|
|
|
|
|
|
expect(data.encoding).to eq(Encoding::ASCII_8BIT)
|
|
|
|
expect(data.force_encoding(Encoding::UTF_8)).to eq('😺hello world')
|
|
|
|
end
|
|
|
|
end
|
2021-06-17 11:10:03 -04:00
|
|
|
end
|
|
|
|
|
2021-06-24 08:08:07 -04:00
|
|
|
context 'when ci_job_trace_force_encode is disabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(ci_job_trace_force_encode: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'appends ASCII data' do
|
|
|
|
data_store.append_data(model, +'hello world', 4)
|
|
|
|
|
|
|
|
expect(data.encoding).to eq(Encoding::ASCII_8BIT)
|
|
|
|
expect(data.force_encoding(Encoding::UTF_8)).to eq('😺hello world')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'throws an exception when appending UTF-8 data' do
|
|
|
|
expect(Gitlab::ErrorTracking).to receive(:track_and_raise_exception).and_call_original
|
|
|
|
expect { data_store.append_data(model, +'Résumé', 4) }.to raise_exception(Encoding::CompatibilityError)
|
|
|
|
end
|
2021-06-17 11:10:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-25 06:46:38 -04:00
|
|
|
describe '#delete_data' do
|
|
|
|
subject { data_store.delete_data(model) }
|
|
|
|
|
|
|
|
context 'when data exists' do
|
|
|
|
let(:model) { create(:ci_build_trace_chunk, :fog_with_data, initial_data: 'sample data in fog') }
|
|
|
|
|
|
|
|
it 'deletes data' do
|
|
|
|
expect(data_store.data(model)).to eq('sample data in fog')
|
|
|
|
|
|
|
|
subject
|
|
|
|
|
2020-07-22 11:09:28 -04:00
|
|
|
expect(data_store.data(model)).to be_nil
|
2018-06-25 06:46:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when data does not exist' do
|
|
|
|
let(:model) { create(:ci_build_trace_chunk, :fog_without_data) }
|
|
|
|
|
|
|
|
it 'does nothing' do
|
2020-07-22 11:09:28 -04:00
|
|
|
expect(data_store.data(model)).to be_nil
|
2018-06-25 06:46:38 -04:00
|
|
|
|
|
|
|
subject
|
|
|
|
|
2020-07-22 11:09:28 -04:00
|
|
|
expect(data_store.data(model)).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#size' do
|
|
|
|
context 'when data exists' do
|
|
|
|
let(:model) { create(:ci_build_trace_chunk, :fog_with_data, initial_data: 'üabcd') }
|
|
|
|
|
|
|
|
it 'returns data bytesize correctly' do
|
|
|
|
expect(data_store.size(model)).to eq 6
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when data does not exist' do
|
|
|
|
let(:model) { create(:ci_build_trace_chunk, :fog_without_data) }
|
|
|
|
|
|
|
|
it 'returns zero' do
|
|
|
|
expect(data_store.size(model)).to be_zero
|
2018-06-25 06:46:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#keys' do
|
|
|
|
subject { data_store.keys(relation) }
|
|
|
|
|
|
|
|
let(:build) { create(:ci_build) }
|
|
|
|
let(:relation) { build.trace_chunks }
|
|
|
|
|
|
|
|
before do
|
|
|
|
create(:ci_build_trace_chunk, :fog_with_data, chunk_index: 0, build: build)
|
|
|
|
create(:ci_build_trace_chunk, :fog_with_data, chunk_index: 1, build: build)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns keys' do
|
|
|
|
is_expected.to eq([[build.id, 0], [build.id, 1]])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#delete_keys' do
|
|
|
|
subject { data_store.delete_keys(keys) }
|
|
|
|
|
|
|
|
let(:build) { create(:ci_build) }
|
|
|
|
let(:relation) { build.trace_chunks }
|
|
|
|
let(:keys) { data_store.keys(relation) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
create(:ci_build_trace_chunk, :fog_with_data, chunk_index: 0, build: build)
|
|
|
|
create(:ci_build_trace_chunk, :fog_with_data, chunk_index: 1, build: build)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'deletes multiple data' do
|
2020-10-28 14:08:52 -04:00
|
|
|
files = connection.directories.new(key: bucket).files
|
|
|
|
|
|
|
|
expect(files.count).to eq(2)
|
|
|
|
expect(files[0].body).to be_present
|
|
|
|
expect(files[1].body).to be_present
|
2018-06-25 06:46:38 -04:00
|
|
|
|
|
|
|
subject
|
|
|
|
|
2020-10-28 14:08:52 -04:00
|
|
|
files.reload
|
|
|
|
|
|
|
|
expect(files.count).to eq(0)
|
2018-06-25 06:46:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|