debian-mirror-gitlab/spec/lib/gitlab/ci/build/cache_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

81 lines
2.8 KiB
Ruby
Raw Normal View History

2021-04-17 20:07:23 +05:30
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Ci::Build::Cache do
2023-06-20 00:43:36 +05:30
let(:cache_config) { [] }
let(:pipeline) { double(::Ci::Pipeline) }
let(:cache_seed_a) { double(Gitlab::Ci::Pipeline::Seed::Build::Cache) }
let(:cache_seed_b) { double(Gitlab::Ci::Pipeline::Seed::Build::Cache) }
subject(:cache) { described_class.new(cache_config, pipeline) }
2021-04-17 20:07:23 +05:30
describe '.initialize' do
2021-06-08 01:23:25 +05:30
context 'when the cache is an array' do
2023-06-20 00:43:36 +05:30
let(:cache_config) { [{ key: 'key-a' }, { key: 'key-b' }] }
2021-06-08 01:23:25 +05:30
it 'instantiates an array of cache seeds' do
allow(Gitlab::Ci::Pipeline::Seed::Build::Cache).to receive(:new).and_return(cache_seed_a, cache_seed_b)
2023-06-20 00:43:36 +05:30
cache
2021-06-08 01:23:25 +05:30
2023-03-04 22:38:38 +05:30
expect(Gitlab::Ci::Pipeline::Seed::Build::Cache).to have_received(:new).with(pipeline, { key: 'key-a' }, 0)
expect(Gitlab::Ci::Pipeline::Seed::Build::Cache).to have_received(:new).with(pipeline, { key: 'key-b' }, 1)
2021-06-08 01:23:25 +05:30
expect(cache.instance_variable_get(:@cache)).to eq([cache_seed_a, cache_seed_b])
2021-04-17 20:07:23 +05:30
end
2021-06-08 01:23:25 +05:30
end
2021-04-17 20:07:23 +05:30
2021-06-08 01:23:25 +05:30
context 'when the cache is a hash' do
2023-06-20 00:43:36 +05:30
let(:cache_config) { { key: 'key-a' } }
2021-04-17 20:07:23 +05:30
it 'instantiates a cache seed' do
2023-06-20 00:43:36 +05:30
allow(Gitlab::Ci::Pipeline::Seed::Build::Cache).to receive(:new).and_return(cache_seed_a)
2021-04-17 20:07:23 +05:30
2023-06-20 00:43:36 +05:30
cache
2021-04-17 20:07:23 +05:30
2023-03-04 22:38:38 +05:30
expect(Gitlab::Ci::Pipeline::Seed::Build::Cache).to have_received(:new).with(pipeline, cache_config, 0)
2023-06-20 00:43:36 +05:30
expect(cache.instance_variable_get(:@cache)).to eq([cache_seed_a])
end
end
context 'when the cache is an array with files inside hashes' do
let(:cache_config) { [{ key: { files: ['file1.json'] } }, { key: { files: ['file1.json', 'file2.json'] } }] }
it 'instantiates a cache seed' do
allow(Gitlab::Ci::Pipeline::Seed::Build::Cache).to receive(:new).and_return(cache_seed_a, cache_seed_b)
cache
expect(Gitlab::Ci::Pipeline::Seed::Build::Cache).to have_received(:new)
.with(pipeline, cache_config.first, '0_file1')
expect(Gitlab::Ci::Pipeline::Seed::Build::Cache).to have_received(:new)
.with(pipeline, cache_config.second, '1_file1_file2')
expect(cache.instance_variable_get(:@cache)).to match_array([cache_seed_a, cache_seed_b])
2021-04-17 20:07:23 +05:30
end
end
end
describe '#cache_attributes' do
2021-06-08 01:23:25 +05:30
context 'when there are no caches' do
it 'returns an empty hash' do
2021-04-17 20:07:23 +05:30
attributes = cache.cache_attributes
2021-06-08 01:23:25 +05:30
expect(attributes).to eq({})
2021-04-17 20:07:23 +05:30
end
end
2021-06-08 01:23:25 +05:30
context 'when there are caches' do
it 'returns the structured attributes for the caches' do
cache_config = [{ key: 'key-a' }, { key: 'key-b' }]
cache = described_class.new(cache_config, pipeline)
2021-04-17 20:07:23 +05:30
2021-06-08 01:23:25 +05:30
attributes = cache.cache_attributes
2021-04-17 20:07:23 +05:30
2021-06-08 01:23:25 +05:30
expect(attributes).to eq({
options: { cache: cache_config }
})
2021-04-17 20:07:23 +05:30
end
end
end
end