Commit 975be5c7 authored by Jan Provaznik's avatar Jan Provaznik

Merge branch 'lm-extract-seed-build-into-collection-class' into 'master'

Extracts cache initialization to collection class

See merge request gitlab-org/gitlab!56543
parents 9b101bf3 5192f606
# frozen_string_literal: true
module Gitlab
module Ci
module Build
class Cache
include ::Gitlab::Utils::StrongMemoize
def initialize(cache, pipeline)
if multiple_cache_per_job?
cache = Array.wrap(cache)
@cache = cache.map do |cache|
Gitlab::Ci::Pipeline::Seed::Build::Cache
.new(pipeline, cache)
end
else
@cache = Gitlab::Ci::Pipeline::Seed::Build::Cache
.new(pipeline, cache)
end
end
def cache_attributes
strong_memoize(:cache_attributes) do
if multiple_cache_per_job?
if @cache.empty?
{}
else
{ options: { cache: @cache.map(&:attributes) } }
end
else
@cache.build_attributes
end
end
end
private
def multiple_cache_per_job?
strong_memoize(:multiple_cache_per_job) do
::Gitlab::Ci::Features.multiple_cache_per_job?
end
end
end
end
end
end
...@@ -28,16 +28,8 @@ module Gitlab ...@@ -28,16 +28,8 @@ module Gitlab
.fabricate(attributes.delete(:except)) .fabricate(attributes.delete(:except))
@rules = Gitlab::Ci::Build::Rules @rules = Gitlab::Ci::Build::Rules
.new(attributes.delete(:rules), default_when: 'on_success') .new(attributes.delete(:rules), default_when: 'on_success')
@cache = Gitlab::Ci::Build::Cache
if multiple_cache_per_job? .new(attributes.delete(:cache), pipeline)
cache = Array.wrap(attributes.delete(:cache))
@cache = cache.map do |cache|
Seed::Build::Cache.new(pipeline, cache)
end
else
@cache = Seed::Build::Cache
.new(pipeline, attributes.delete(:cache))
end
end end
def name def name
...@@ -69,7 +61,7 @@ module Gitlab ...@@ -69,7 +61,7 @@ module Gitlab
.deep_merge(pipeline_attributes) .deep_merge(pipeline_attributes)
.deep_merge(rules_attributes) .deep_merge(rules_attributes)
.deep_merge(allow_failure_criteria_attributes) .deep_merge(allow_failure_criteria_attributes)
.deep_merge(cache_attributes) .deep_merge(@cache.cache_attributes)
end end
def bridge? def bridge?
...@@ -203,26 +195,6 @@ module Gitlab ...@@ -203,26 +195,6 @@ module Gitlab
end end
end end
def cache_attributes
strong_memoize(:cache_attributes) do
if multiple_cache_per_job?
if @cache.empty?
{}
else
{ options: { cache: @cache.map(&:attributes) } }
end
else
@cache.build_attributes
end
end
end
def multiple_cache_per_job?
strong_memoize(:multiple_cache_per_job) do
::Gitlab::Ci::Features.multiple_cache_per_job?
end
end
# If a job uses `allow_failure:exit_codes` and `rules:allow_failure` # If a job uses `allow_failure:exit_codes` and `rules:allow_failure`
# we need to prevent the exit codes from being persisted because they # we need to prevent the exit codes from being persisted because they
# would break the behavior defined by `rules:allow_failure`. # would break the behavior defined by `rules:allow_failure`.
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Ci::Build::Cache do
describe '.initialize' do
context 'when the multiple cache feature flag is disabled' do
before do
stub_feature_flags(multiple_cache_per_job: false)
end
it 'instantiates a cache seed' do
cache_config = { key: 'key-a' }
pipeline = double(::Ci::Pipeline)
cache_seed = double(Gitlab::Ci::Pipeline::Seed::Build::Cache)
allow(Gitlab::Ci::Pipeline::Seed::Build::Cache).to receive(:new).and_return(cache_seed)
cache = described_class.new(cache_config, pipeline)
expect(Gitlab::Ci::Pipeline::Seed::Build::Cache).to have_received(:new).with(pipeline, cache_config)
expect(cache.instance_variable_get(:@cache)).to eq(cache_seed)
end
end
context 'when the multiple cache feature flag is enabled' do
context 'when the cache is an array' do
it 'instantiates an array of cache seeds' do
cache_config = [{ key: 'key-a' }, { key: 'key-b' }]
pipeline = double(::Ci::Pipeline)
cache_seed_a = double(Gitlab::Ci::Pipeline::Seed::Build::Cache)
cache_seed_b = double(Gitlab::Ci::Pipeline::Seed::Build::Cache)
allow(Gitlab::Ci::Pipeline::Seed::Build::Cache).to receive(:new).and_return(cache_seed_a, cache_seed_b)
cache = described_class.new(cache_config, pipeline)
expect(Gitlab::Ci::Pipeline::Seed::Build::Cache).to have_received(:new).with(pipeline, { key: 'key-a' })
expect(Gitlab::Ci::Pipeline::Seed::Build::Cache).to have_received(:new).with(pipeline, { key: 'key-b' })
expect(cache.instance_variable_get(:@cache)).to eq([cache_seed_a, cache_seed_b])
end
end
context 'when the cache is a hash' do
it 'instantiates a cache seed' do
cache_config = { key: 'key-a' }
pipeline = double(::Ci::Pipeline)
cache_seed = double(Gitlab::Ci::Pipeline::Seed::Build::Cache)
allow(Gitlab::Ci::Pipeline::Seed::Build::Cache).to receive(:new).and_return(cache_seed)
cache = described_class.new(cache_config, pipeline)
expect(Gitlab::Ci::Pipeline::Seed::Build::Cache).to have_received(:new).with(pipeline, cache_config)
expect(cache.instance_variable_get(:@cache)).to eq([cache_seed])
end
end
end
end
describe '#cache_attributes' do
context 'when the multiple cache feature flag is disabled' do
before do
stub_feature_flags(multiple_cache_per_job: false)
end
it "returns the cache seed's build attributes" do
cache_config = { key: 'key-a' }
pipeline = double(::Ci::Pipeline)
cache = described_class.new(cache_config, pipeline)
attributes = cache.cache_attributes
expect(attributes).to eq({
options: { cache: { key: 'key-a' } }
})
end
end
context 'when the multiple cache feature flag is enabled' do
context 'when there are no caches' do
it 'returns an empty hash' do
cache_config = []
pipeline = double(::Ci::Pipeline)
cache = described_class.new(cache_config, pipeline)
attributes = cache.cache_attributes
expect(attributes).to eq({})
end
end
context 'when there are caches' do
it 'returns the structured attributes for the caches' do
cache_config = [{ key: 'key-a' }, { key: 'key-b' }]
pipeline = double(::Ci::Pipeline)
cache = described_class.new(cache_config, pipeline)
attributes = cache.cache_attributes
expect(attributes).to eq({
options: { cache: cache_config }
})
end
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment