Commit d0b0cbc4 authored by Laura Montemayor's avatar Laura Montemayor Committed by Shinya Maeda

Adds multiple caches per build

* Adds an Entry::Caches to handle multiple caches
* Updates Build#cache
* Updates Entry::Root, Entry::Job, Entry::Default to use Entry::Caches
* Updates Seed::Build and Cache to accomodate multiple caches
* Updates specs to handle multiple cache
* Adds docs and a changelog
parent 0e14ece0
...@@ -812,14 +812,15 @@ module Ci ...@@ -812,14 +812,15 @@ module Ci
end end
def cache def cache
cache = options[:cache] cache = Array.wrap(options[:cache])
if cache && project.jobs_cache_index if project.jobs_cache_index
cache = cache.merge( cache = cache.map do |single_cache|
key: "#{cache[:key]}-#{project.jobs_cache_index}") single_cache.merge(key: "#{single_cache[:key]}-#{project.jobs_cache_index}")
end
end end
[cache] cache
end end
def credentials def credentials
......
---
title: Adds ability to have multiple cache per job
merge_request: 53410
author:
type: changed
---
name: multiple_cache_per_job
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/53410
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/321877
milestone: '13.10'
type: development
group: group::pipeline authoring
default_enabled: false
...@@ -2754,7 +2754,62 @@ URI-encoded `%2F`. A value made only of dots (`.`, `%2E`) is also forbidden. ...@@ -2754,7 +2754,62 @@ URI-encoded `%2F`. A value made only of dots (`.`, `%2E`) is also forbidden.
You can specify a [fallback cache key](#fallback-cache-key) to use if the specified `cache:key` is not found. You can specify a [fallback cache key](#fallback-cache-key) to use if the specified `cache:key` is not found.
##### Fallback cache key ##### Multiple caches
> - [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/32814) in GitLab 13.10.
> - It's [deployed behind a feature flag](../../user/feature_flags.md), disabled by default.
> - It's disabled on GitLab.com.
> - It's not recommended for production use.
> - To use it in GitLab self-managed instances, ask a GitLab administrator to [enable it](#enable-or-disable-multiple-caches). **(FREE SELF)**
WARNING:
This feature might not be available to you. Check the **version history** note above for details.
You can have a maximum of four caches:
```yaml
test-job:
stage: build
cache:
- key:
files:
- Gemfile.lock
paths:
- vendor/ruby
- key:
files:
- yarn.lock
paths:
- .yarn-cache/
script:
- bundle install --path=vendor
- yarn install --cache-folder .yarn-cache
- echo Run tests...
```
If multiple caches are combined with a [Fallback cache key](#fallback-cache-key),
the fallback is fetched multiple times if multiple caches are not found.
##### Enable or disable multiple caches **(FREE SELF)**
The multiple caches feature is under development and not ready for production use.
It is deployed behind a feature flag that is **disabled by default**.
[GitLab administrators with access to the GitLab Rails console](../../administration/feature_flags.md)
can enable it.
To enable it:
```ruby
Feature.enable(:multiple_cache_per_job)
```
To disable it:
```ruby
Feature.disable(:multiple_cache_per_job)
```
#### Fallback cache key
> [Introduced](https://gitlab.com/gitlab-org/gitlab-runner/-/merge_requests/1534) in GitLab Runner 13.4. > [Introduced](https://gitlab.com/gitlab-org/gitlab-runner/-/merge_requests/1534) in GitLab Runner 13.4.
......
...@@ -7,52 +7,90 @@ module Gitlab ...@@ -7,52 +7,90 @@ module Gitlab
## ##
# Entry that represents a cache configuration # Entry that represents a cache configuration
# #
class Cache < ::Gitlab::Config::Entry::Node class Cache < ::Gitlab::Config::Entry::Simplifiable
include ::Gitlab::Config::Entry::Configurable strategy :Caches, if: -> (config) { Feature.enabled?(:multiple_cache_per_job) }
include ::Gitlab::Config::Entry::Validatable strategy :Cache, if: -> (config) { Feature.disabled?(:multiple_cache_per_job) }
include ::Gitlab::Config::Entry::Attributable
class Caches < ::Gitlab::Config::Entry::ComposableArray
ALLOWED_KEYS = %i[key untracked paths when policy].freeze include ::Gitlab::Config::Entry::Validatable
ALLOWED_POLICY = %w[pull-push push pull].freeze
DEFAULT_POLICY = 'pull-push' MULTIPLE_CACHE_LIMIT = 4
ALLOWED_WHEN = %w[on_success on_failure always].freeze
DEFAULT_WHEN = 'on_success' validations do
validates :config, presence: true
validations do
validates :config, type: Hash, allowed_keys: ALLOWED_KEYS validate do
validates :policy, unless config.is_a?(Hash) || config.is_a?(Array)
inclusion: { in: ALLOWED_POLICY, message: 'should be pull-push, push, or pull' }, errors.add(:config, 'can only be a Hash or an Array')
allow_blank: true end
with_options allow_nil: true do if config.is_a?(Array) && config.count > MULTIPLE_CACHE_LIMIT
validates :when, errors.add(:config, "no more than #{MULTIPLE_CACHE_LIMIT} caches can be created")
inclusion: { end
in: ALLOWED_WHEN, end
message: 'should be on_success, on_failure or always' end
}
def initialize(*args)
super
@key = nil
end
def composable_class
Entry::Cache::Cache
end end
end end
entry :key, Entry::Key, class Cache < ::Gitlab::Config::Entry::Node
description: 'Cache key used to define a cache affinity.' include ::Gitlab::Config::Entry::Configurable
include ::Gitlab::Config::Entry::Validatable
include ::Gitlab::Config::Entry::Attributable
ALLOWED_KEYS = %i[key untracked paths when policy].freeze
ALLOWED_POLICY = %w[pull-push push pull].freeze
DEFAULT_POLICY = 'pull-push'
ALLOWED_WHEN = %w[on_success on_failure always].freeze
DEFAULT_WHEN = 'on_success'
validations do
validates :config, type: Hash, allowed_keys: ALLOWED_KEYS
validates :policy,
inclusion: { in: ALLOWED_POLICY, message: 'should be pull-push, push, or pull' },
allow_blank: true
with_options allow_nil: true do
validates :when,
inclusion: {
in: ALLOWED_WHEN,
message: 'should be on_success, on_failure or always'
}
end
end
entry :untracked, ::Gitlab::Config::Entry::Boolean, entry :key, Entry::Key,
description: 'Cache all untracked files.' description: 'Cache key used to define a cache affinity.'
entry :paths, Entry::Paths, entry :untracked, ::Gitlab::Config::Entry::Boolean,
description: 'Specify which paths should be cached across builds.' description: 'Cache all untracked files.'
attributes :policy, :when entry :paths, Entry::Paths,
description: 'Specify which paths should be cached across builds.'
def value attributes :policy, :when
result = super
result[:key] = key_value def value
result[:policy] = policy || DEFAULT_POLICY result = super
# Use self.when to avoid conflict with reserved word
result[:when] = self.when || DEFAULT_WHEN result[:key] = key_value
result[:policy] = policy || DEFAULT_POLICY
# Use self.when to avoid conflict with reserved word
result[:when] = self.when || DEFAULT_WHEN
result
end
end
result class UnknownStrategy < ::Gitlab::Config::Entry::Node
end end
end end
end end
......
...@@ -67,6 +67,10 @@ module Gitlab ...@@ -67,6 +67,10 @@ module Gitlab
def self.display_codequality_backend_comparison?(project) def self.display_codequality_backend_comparison?(project)
::Feature.enabled?(:codequality_backend_comparison, project, default_enabled: :yaml) ::Feature.enabled?(:codequality_backend_comparison, project, default_enabled: :yaml)
end end
def self.multiple_cache_per_job?
::Feature.enabled?(:multiple_cache_per_job, default_enabled: :yaml)
end
end end
end end
end end
...@@ -28,8 +28,16 @@ module Gitlab ...@@ -28,8 +28,16 @@ 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 = Seed::Build::Cache
.new(pipeline, attributes.delete(:cache)) if multiple_cache_per_job?
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
...@@ -197,7 +205,21 @@ module Gitlab ...@@ -197,7 +205,21 @@ module Gitlab
def cache_attributes def cache_attributes
strong_memoize(:cache_attributes) do strong_memoize(:cache_attributes) do
@cache.build_attributes 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
end end
......
...@@ -18,18 +18,18 @@ module Gitlab ...@@ -18,18 +18,18 @@ module Gitlab
raise ArgumentError, "unknown cache keys: #{local_cache.keys}" if local_cache.any? raise ArgumentError, "unknown cache keys: #{local_cache.keys}" if local_cache.any?
end end
def build_attributes def attributes
{ {
options: { key: key_string,
cache: { paths: @paths,
key: key_string, policy: @policy,
paths: @paths, untracked: @untracked,
policy: @policy, when: @when
untracked: @untracked, }.compact
when: @when end
}.compact.presence
}.compact def build_attributes
} { options: { cache: attributes.presence }.compact }
end end
private private
......
...@@ -7,225 +7,285 @@ RSpec.describe Gitlab::Ci::Config::Entry::Cache do ...@@ -7,225 +7,285 @@ RSpec.describe Gitlab::Ci::Config::Entry::Cache do
subject(:entry) { described_class.new(config) } subject(:entry) { described_class.new(config) }
describe 'validations' do context 'with multiple caches' do
before do before do
entry.compose! entry.compose!
end end
context 'when entry config value is correct' do describe '#valid?' do
let(:policy) { nil } context 'when configuration is valid with a single cache' do
let(:key) { 'some key' } let(:config) { { key: 'key', paths: ["logs/"], untracked: true } }
let(:when_config) { nil }
it 'is valid' do
let(:config) do expect(entry).to be_valid
{
key: key,
untracked: true,
paths: ['some/path/']
}.tap do |config|
config[:policy] = policy if policy
config[:when] = when_config if when_config
end end
end end
describe '#value' do context 'when configuration is valid with multiple caches' do
shared_examples 'hash key value' do let(:config) do
it 'returns hash value' do [
expect(entry.value).to eq(key: key, untracked: true, paths: ['some/path/'], policy: 'pull-push', when: 'on_success') { key: 'key', paths: ["logs/"], untracked: true },
end { key: 'key2', paths: ["logs/"], untracked: true },
{ key: 'key3', paths: ["logs/"], untracked: true }
]
end end
it_behaves_like 'hash key value' it 'is valid' do
expect(entry).to be_valid
end
end
context 'with files' do context 'when configuration is not a Hash or Array' do
let(:key) { { files: %w[a-file other-file] } } let(:config) { 'invalid' }
it_behaves_like 'hash key value' it 'is invalid' do
expect(entry).not_to be_valid
end end
end
context 'with files and prefix' do context 'when entry values contain more than four caches' do
let(:key) { { files: %w[a-file other-file], prefix: 'prefix-value' } } let(:config) do
[
{ key: 'key', paths: ["logs/"], untracked: true },
{ key: 'key2', paths: ["logs/"], untracked: true },
{ key: 'key3', paths: ["logs/"], untracked: true },
{ key: 'key4', paths: ["logs/"], untracked: true },
{ key: 'key5', paths: ["logs/"], untracked: true }
]
end
it_behaves_like 'hash key value' it 'is invalid' do
expect(entry.errors).to eq(["caches config no more than 4 caches can be created"])
expect(entry).not_to be_valid
end end
end
end
end
context 'with a single cache' do
before do
stub_feature_flags(multiple_cache_per_job: false)
end
describe 'validations' do
before do
entry.compose!
end
context 'with prefix' do context 'when entry config value is correct' do
let(:key) { { prefix: 'prefix-value' } } let(:policy) { nil }
let(:key) { 'some key' }
let(:when_config) { nil }
it 'key is nil' do let(:config) do
expect(entry.value).to match(a_hash_including(key: nil)) {
key: key,
untracked: true,
paths: ['some/path/']
}.tap do |config|
config[:policy] = policy if policy
config[:when] = when_config if when_config
end end
end end
context 'with `policy`' do describe '#value' do
where(:policy, :result) do shared_examples 'hash key value' do
'pull-push' | 'pull-push' it 'returns hash value' do
'push' | 'push' expect(entry.value).to eq(key: key, untracked: true, paths: ['some/path/'], policy: 'pull-push', when: 'on_success')
'pull' | 'pull' end
'unknown' | 'unknown' # invalid
end end
with_them do it_behaves_like 'hash key value'
it { expect(entry.value).to include(policy: result) }
context 'with files' do
let(:key) { { files: %w[a-file other-file] } }
it_behaves_like 'hash key value'
end end
end
context 'without `policy`' do context 'with files and prefix' do
it 'assigns policy to default' do let(:key) { { files: %w[a-file other-file], prefix: 'prefix-value' } }
expect(entry.value).to include(policy: 'pull-push')
it_behaves_like 'hash key value'
end end
end
context 'with `when`' do context 'with prefix' do
where(:when_config, :result) do let(:key) { { prefix: 'prefix-value' } }
'on_success' | 'on_success'
'on_failure' | 'on_failure' it 'key is nil' do
'always' | 'always' expect(entry.value).to match(a_hash_including(key: nil))
'unknown' | 'unknown' # invalid end
end end
with_them do context 'with `policy`' do
it { expect(entry.value).to include(when: result) } where(:policy, :result) do
'pull-push' | 'pull-push'
'push' | 'push'
'pull' | 'pull'
'unknown' | 'unknown' # invalid
end
with_them do
it { expect(entry.value).to include(policy: result) }
end
end end
end
context 'without `when`' do context 'without `policy`' do
it 'assigns when to default' do it 'assigns policy to default' do
expect(entry.value).to include(when: 'on_success') expect(entry.value).to include(policy: 'pull-push')
end
end end
end
end
describe '#valid?' do context 'with `when`' do
it { is_expected.to be_valid } where(:when_config, :result) do
'on_success' | 'on_success'
'on_failure' | 'on_failure'
'always' | 'always'
'unknown' | 'unknown' # invalid
end
context 'with files' do with_them do
let(:key) { { files: %w[a-file other-file] } } it { expect(entry.value).to include(when: result) }
end
end
it { is_expected.to be_valid } context 'without `when`' do
it 'assigns when to default' do
expect(entry.value).to include(when: 'on_success')
end
end
end end
end
context 'with `policy`' do describe '#valid?' do
where(:policy, :valid) do it { is_expected.to be_valid }
'pull-push' | true
'push' | true context 'with files' do
'pull' | true let(:key) { { files: %w[a-file other-file] } }
'unknown' | false
end
with_them do it { is_expected.to be_valid }
it 'returns expected validity' do
expect(entry.valid?).to eq(valid)
end end
end end
end
context 'with `when`' do context 'with `policy`' do
where(:when_config, :valid) do where(:policy, :valid) do
'on_success' | true 'pull-push' | true
'on_failure' | true 'push' | true
'always' | true 'pull' | true
'unknown' | false 'unknown' | false
end end
with_them do with_them do
it 'returns expected validity' do it 'returns expected validity' do
expect(entry.valid?).to eq(valid) expect(entry.valid?).to eq(valid)
end
end end
end end
end
context 'with key missing' do context 'with `when`' do
let(:config) do where(:when_config, :valid) do
{ untracked: true, 'on_success' | true
paths: ['some/path/'] } 'on_failure' | true
'always' | true
'unknown' | false
end
with_them do
it 'returns expected validity' do
expect(entry.valid?).to eq(valid)
end
end
end end
describe '#value' do context 'with key missing' do
it 'sets key with the default' do let(:config) do
expect(entry.value[:key]) { untracked: true,
.to eq(Gitlab::Ci::Config::Entry::Key.default) paths: ['some/path/'] }
end
describe '#value' do
it 'sets key with the default' do
expect(entry.value[:key])
.to eq(Gitlab::Ci::Config::Entry::Key.default)
end
end end
end end
end end
end
context 'when entry value is not correct' do context 'when entry value is not correct' do
describe '#errors' do describe '#errors' do
subject { entry.errors } subject { entry.errors }
context 'when is not a hash' do context 'when is not a hash' do
let(:config) { 'ls' } let(:config) { 'ls' }
it 'reports errors with config value' do it 'reports errors with config value' do
is_expected.to include 'cache config should be a hash' is_expected.to include 'cache config should be a hash'
end
end end
end
context 'when policy is unknown' do context 'when policy is unknown' do
let(:config) { { policy: 'unknown' } } let(:config) { { policy: 'unknown' } }
it 'reports error' do it 'reports error' do
is_expected.to include('cache policy should be pull-push, push, or pull') is_expected.to include('cache policy should be pull-push, push, or pull')
end
end end
end
context 'when `when` is unknown' do context 'when `when` is unknown' do
let(:config) { { when: 'unknown' } } let(:config) { { when: 'unknown' } }
it 'reports error' do it 'reports error' do
is_expected.to include('cache when should be on_success, on_failure or always') is_expected.to include('cache when should be on_success, on_failure or always')
end
end end
end
context 'when descendants are invalid' do context 'when descendants are invalid' do
context 'with invalid keys' do context 'with invalid keys' do
let(:config) { { key: 1 } } let(:config) { { key: 1 } }
it 'reports error with descendants' do it 'reports error with descendants' do
is_expected.to include 'key should be a hash, a string or a symbol' is_expected.to include 'key should be a hash, a string or a symbol'
end
end end
end
context 'with empty key' do context 'with empty key' do
let(:config) { { key: {} } } let(:config) { { key: {} } }
it 'reports error with descendants' do it 'reports error with descendants' do
is_expected.to include 'key config missing required keys: files' is_expected.to include 'key config missing required keys: files'
end
end end
end
context 'with invalid files' do context 'with invalid files' do
let(:config) { { key: { files: 'a-file' } } } let(:config) { { key: { files: 'a-file' } } }
it 'reports error with descendants' do it 'reports error with descendants' do
is_expected.to include 'key:files config should be an array of strings' is_expected.to include 'key:files config should be an array of strings'
end
end end
end
context 'with prefix without files' do context 'with prefix without files' do
let(:config) { { key: { prefix: 'a-prefix' } } } let(:config) { { key: { prefix: 'a-prefix' } } }
it 'reports error with descendants' do it 'reports error with descendants' do
is_expected.to include 'key config missing required keys: files' is_expected.to include 'key config missing required keys: files'
end
end end
end
context 'when there is an unknown key present' do context 'when there is an unknown key present' do
let(:config) { { key: { unknown: 'a-file' } } } let(:config) { { key: { unknown: 'a-file' } } }
it 'reports error with descendants' do it 'reports error with descendants' do
is_expected.to include 'key config contains unknown keys: unknown' is_expected.to include 'key config contains unknown keys: unknown'
end
end end
end end
end
context 'when there is an unknown key present' do context 'when there is an unknown key present' do
let(:config) { { invalid: true } } let(:config) { { invalid: true } }
it 'reports error with descendants' do it 'reports error with descendants' do
is_expected.to include 'cache config contains unknown keys: invalid' is_expected.to include 'cache config contains unknown keys: invalid'
end
end end
end end
end end
......
...@@ -537,7 +537,7 @@ RSpec.describe Gitlab::Ci::Config::Entry::Job do ...@@ -537,7 +537,7 @@ RSpec.describe Gitlab::Ci::Config::Entry::Job do
it 'overrides default config' do it 'overrides default config' do
expect(entry[:image].value).to eq(name: 'some_image') expect(entry[:image].value).to eq(name: 'some_image')
expect(entry[:cache].value).to eq(key: 'test', policy: 'pull-push', when: 'on_success') expect(entry[:cache].value).to eq([key: 'test', policy: 'pull-push', when: 'on_success'])
end end
end end
...@@ -552,7 +552,43 @@ RSpec.describe Gitlab::Ci::Config::Entry::Job do ...@@ -552,7 +552,43 @@ RSpec.describe Gitlab::Ci::Config::Entry::Job do
it 'uses config from default entry' do it 'uses config from default entry' do
expect(entry[:image].value).to eq 'specified' expect(entry[:image].value).to eq 'specified'
expect(entry[:cache].value).to eq(key: 'test', policy: 'pull-push', when: 'on_success') expect(entry[:cache].value).to eq([key: 'test', policy: 'pull-push', when: 'on_success'])
end
end
context 'with multiple_cache_per_job FF disabled' do
before do
stub_feature_flags(multiple_cache_per_job: false)
end
context 'when job config overrides default config' do
before do
entry.compose!(deps)
end
let(:config) do
{ script: 'rspec', image: 'some_image', cache: { key: 'test' } }
end
it 'overrides default config' do
expect(entry[:image].value).to eq(name: 'some_image')
expect(entry[:cache].value).to eq(key: 'test', policy: 'pull-push', when: 'on_success')
end
end
context 'when job config does not override default config' do
before do
allow(default).to receive('[]').with(:image).and_return(specified)
entry.compose!(deps)
end
let(:config) { { script: 'ls', cache: { key: 'test' } } }
it 'uses config from default entry' do
expect(entry[:image].value).to eq 'specified'
expect(entry[:cache].value).to eq(key: 'test', policy: 'pull-push', when: 'on_success')
end
end end
end end
......
...@@ -126,49 +126,105 @@ RSpec.describe Gitlab::Ci::Config::Entry::Root do ...@@ -126,49 +126,105 @@ RSpec.describe Gitlab::Ci::Config::Entry::Root do
expect(root.jobs_value.keys).to eq([:rspec, :spinach, :release]) expect(root.jobs_value.keys).to eq([:rspec, :spinach, :release])
expect(root.jobs_value[:rspec]).to eq( expect(root.jobs_value[:rspec]).to eq(
{ name: :rspec, { name: :rspec,
script: %w[rspec ls], script: %w[rspec ls],
before_script: %w(ls pwd), before_script: %w(ls pwd),
image: { name: 'ruby:2.7' }, image: { name: 'ruby:2.7' },
services: [{ name: 'postgres:9.1' }, { name: 'mysql:5.5' }], services: [{ name: 'postgres:9.1' }, { name: 'mysql:5.5' }],
stage: 'test', stage: 'test',
cache: { key: 'k', untracked: true, paths: ['public/'], policy: 'pull-push', when: 'on_success' }, cache: [{ key: 'k', untracked: true, paths: ['public/'], policy: 'pull-push', when: 'on_success' }],
variables: { 'VAR' => 'root', 'VAR2' => 'val 2' }, variables: { 'VAR' => 'root', 'VAR2' => 'val 2' },
ignore: false, ignore: false,
after_script: ['make clean'], after_script: ['make clean'],
only: { refs: %w[branches tags] }, only: { refs: %w[branches tags] },
scheduling_type: :stage } scheduling_type: :stage }
) )
expect(root.jobs_value[:spinach]).to eq( expect(root.jobs_value[:spinach]).to eq(
{ name: :spinach, { name: :spinach,
before_script: [], before_script: [],
script: %w[spinach], script: %w[spinach],
image: { name: 'ruby:2.7' }, image: { name: 'ruby:2.7' },
services: [{ name: 'postgres:9.1' }, { name: 'mysql:5.5' }], services: [{ name: 'postgres:9.1' }, { name: 'mysql:5.5' }],
stage: 'test', stage: 'test',
cache: { key: 'k', untracked: true, paths: ['public/'], policy: 'pull-push', when: 'on_success' }, cache: [{ key: 'k', untracked: true, paths: ['public/'], policy: 'pull-push', when: 'on_success' }],
variables: { 'VAR' => 'root', 'VAR2' => 'val 2' }, variables: { 'VAR' => 'root', 'VAR2' => 'val 2' },
ignore: false, ignore: false,
after_script: ['make clean'], after_script: ['make clean'],
only: { refs: %w[branches tags] }, only: { refs: %w[branches tags] },
scheduling_type: :stage } scheduling_type: :stage }
) )
expect(root.jobs_value[:release]).to eq( expect(root.jobs_value[:release]).to eq(
{ name: :release, { name: :release,
stage: 'release', stage: 'release',
before_script: [], before_script: [],
script: ["make changelog | tee release_changelog.txt"], script: ["make changelog | tee release_changelog.txt"],
release: { name: "Release $CI_TAG_NAME", tag_name: 'v0.06', description: "./release_changelog.txt" }, release: { name: "Release $CI_TAG_NAME", tag_name: 'v0.06', description: "./release_changelog.txt" },
image: { name: "ruby:2.7" }, image: { name: "ruby:2.7" },
services: [{ name: "postgres:9.1" }, { name: "mysql:5.5" }], services: [{ name: "postgres:9.1" }, { name: "mysql:5.5" }],
cache: { key: "k", untracked: true, paths: ["public/"], policy: "pull-push", when: 'on_success' }, cache: [{ key: "k", untracked: true, paths: ["public/"], policy: "pull-push", when: 'on_success' }],
only: { refs: %w(branches tags) }, only: { refs: %w(branches tags) },
variables: { 'VAR' => 'job', 'VAR2' => 'val 2' }, variables: { 'VAR' => 'job', 'VAR2' => 'val 2' },
after_script: [], after_script: [],
ignore: false, ignore: false,
scheduling_type: :stage } scheduling_type: :stage }
) )
end end
end end
context 'with multuple_cache_per_job FF disabled' do
before do
stub_feature_flags(multiple_cache_per_job: false)
root.compose!
end
describe '#jobs_value' do
it 'returns jobs configuration' do
expect(root.jobs_value.keys).to eq([:rspec, :spinach, :release])
expect(root.jobs_value[:rspec]).to eq(
{ name: :rspec,
script: %w[rspec ls],
before_script: %w(ls pwd),
image: { name: 'ruby:2.7' },
services: [{ name: 'postgres:9.1' }, { name: 'mysql:5.5' }],
stage: 'test',
cache: { key: 'k', untracked: true, paths: ['public/'], policy: 'pull-push', when: 'on_success' },
variables: { 'VAR' => 'root', 'VAR2' => 'val 2' },
ignore: false,
after_script: ['make clean'],
only: { refs: %w[branches tags] },
scheduling_type: :stage }
)
expect(root.jobs_value[:spinach]).to eq(
{ name: :spinach,
before_script: [],
script: %w[spinach],
image: { name: 'ruby:2.7' },
services: [{ name: 'postgres:9.1' }, { name: 'mysql:5.5' }],
stage: 'test',
cache: { key: 'k', untracked: true, paths: ['public/'], policy: 'pull-push', when: 'on_success' },
variables: { 'VAR' => 'root', 'VAR2' => 'val 2' },
ignore: false,
after_script: ['make clean'],
only: { refs: %w[branches tags] },
scheduling_type: :stage }
)
expect(root.jobs_value[:release]).to eq(
{ name: :release,
stage: 'release',
before_script: [],
script: ["make changelog | tee release_changelog.txt"],
release: { name: "Release $CI_TAG_NAME", tag_name: 'v0.06', description: "./release_changelog.txt" },
image: { name: "ruby:2.7" },
services: [{ name: "postgres:9.1" }, { name: "mysql:5.5" }],
cache: { key: "k", untracked: true, paths: ["public/"], policy: "pull-push", when: 'on_success' },
only: { refs: %w(branches tags) },
variables: { 'VAR' => 'job', 'VAR2' => 'val 2' },
after_script: [],
ignore: false,
scheduling_type: :stage }
)
end
end
end
end end
end end
...@@ -187,6 +243,52 @@ RSpec.describe Gitlab::Ci::Config::Entry::Root do ...@@ -187,6 +243,52 @@ RSpec.describe Gitlab::Ci::Config::Entry::Root do
spinach: { before_script: [], variables: { VAR: 'job' }, script: 'spinach' } } spinach: { before_script: [], variables: { VAR: 'job' }, script: 'spinach' } }
end end
context 'with multiple_cache_per_job FF disabled' do
context 'when composed' do
before do
stub_feature_flags(multiple_cache_per_job: false)
root.compose!
end
describe '#errors' do
it 'has no errors' do
expect(root.errors).to be_empty
end
end
describe '#jobs_value' do
it 'returns jobs configuration' do
expect(root.jobs_value).to eq(
rspec: { name: :rspec,
script: %w[rspec ls],
before_script: %w(ls pwd),
image: { name: 'ruby:2.7' },
services: [{ name: 'postgres:9.1' }, { name: 'mysql:5.5' }],
stage: 'test',
cache: { key: 'k', untracked: true, paths: ['public/'], policy: 'pull-push', when: 'on_success' },
variables: { 'VAR' => 'root' },
ignore: false,
after_script: ['make clean'],
only: { refs: %w[branches tags] },
scheduling_type: :stage },
spinach: { name: :spinach,
before_script: [],
script: %w[spinach],
image: { name: 'ruby:2.7' },
services: [{ name: 'postgres:9.1' }, { name: 'mysql:5.5' }],
stage: 'test',
cache: { key: 'k', untracked: true, paths: ['public/'], policy: 'pull-push', when: 'on_success' },
variables: { 'VAR' => 'job' },
ignore: false,
after_script: ['make clean'],
only: { refs: %w[branches tags] },
scheduling_type: :stage }
)
end
end
end
end
context 'when composed' do context 'when composed' do
before do before do
root.compose! root.compose!
...@@ -202,29 +304,29 @@ RSpec.describe Gitlab::Ci::Config::Entry::Root do ...@@ -202,29 +304,29 @@ RSpec.describe Gitlab::Ci::Config::Entry::Root do
it 'returns jobs configuration' do it 'returns jobs configuration' do
expect(root.jobs_value).to eq( expect(root.jobs_value).to eq(
rspec: { name: :rspec, rspec: { name: :rspec,
script: %w[rspec ls], script: %w[rspec ls],
before_script: %w(ls pwd), before_script: %w(ls pwd),
image: { name: 'ruby:2.7' }, image: { name: 'ruby:2.7' },
services: [{ name: 'postgres:9.1' }, { name: 'mysql:5.5' }], services: [{ name: 'postgres:9.1' }, { name: 'mysql:5.5' }],
stage: 'test', stage: 'test',
cache: { key: 'k', untracked: true, paths: ['public/'], policy: 'pull-push', when: 'on_success' }, cache: [{ key: 'k', untracked: true, paths: ['public/'], policy: 'pull-push', when: 'on_success' }],
variables: { 'VAR' => 'root' }, variables: { 'VAR' => 'root' },
ignore: false, ignore: false,
after_script: ['make clean'], after_script: ['make clean'],
only: { refs: %w[branches tags] }, only: { refs: %w[branches tags] },
scheduling_type: :stage }, scheduling_type: :stage },
spinach: { name: :spinach, spinach: { name: :spinach,
before_script: [], before_script: [],
script: %w[spinach], script: %w[spinach],
image: { name: 'ruby:2.7' }, image: { name: 'ruby:2.7' },
services: [{ name: 'postgres:9.1' }, { name: 'mysql:5.5' }], services: [{ name: 'postgres:9.1' }, { name: 'mysql:5.5' }],
stage: 'test', stage: 'test',
cache: { key: 'k', untracked: true, paths: ['public/'], policy: 'pull-push', when: 'on_success' }, cache: [{ key: 'k', untracked: true, paths: ['public/'], policy: 'pull-push', when: 'on_success' }],
variables: { 'VAR' => 'job' }, variables: { 'VAR' => 'job' },
ignore: false, ignore: false,
after_script: ['make clean'], after_script: ['make clean'],
only: { refs: %w[branches tags] }, only: { refs: %w[branches tags] },
scheduling_type: :stage } scheduling_type: :stage }
) )
end end
end end
...@@ -265,7 +367,20 @@ RSpec.describe Gitlab::Ci::Config::Entry::Root do ...@@ -265,7 +367,20 @@ RSpec.describe Gitlab::Ci::Config::Entry::Root do
describe '#cache_value' do describe '#cache_value' do
it 'returns correct cache definition' do it 'returns correct cache definition' do
expect(root.cache_value).to eq(key: 'a', policy: 'pull-push', when: 'on_success') expect(root.cache_value).to eq([key: 'a', policy: 'pull-push', when: 'on_success'])
end
end
context 'with multiple_cache_per_job FF disabled' do
before do
stub_feature_flags(multiple_cache_per_job: false)
root.compose!
end
describe '#cache_value' do
it 'returns correct cache definition' do
expect(root.cache_value).to eq(key: 'a', policy: 'pull-push', when: 'on_success')
end
end end
end end
end end
......
...@@ -9,8 +9,255 @@ RSpec.describe Gitlab::Ci::Pipeline::Seed::Build::Cache do ...@@ -9,8 +9,255 @@ RSpec.describe Gitlab::Ci::Pipeline::Seed::Build::Cache do
let(:processor) { described_class.new(pipeline, config) } let(:processor) { described_class.new(pipeline, config) }
describe '#build_attributes' do context 'with multiple_cache_per_job ff disabled' do
subject { processor.build_attributes } before do
stub_feature_flags(multiple_cache_per_job: false)
end
describe '#build_attributes' do
subject { processor.build_attributes }
context 'with cache:key' do
let(:config) do
{
key: 'a-key',
paths: ['vendor/ruby']
}
end
it { is_expected.to include(options: { cache: config }) }
end
context 'with cache:key as a symbol' do
let(:config) do
{
key: :a_key,
paths: ['vendor/ruby']
}
end
it { is_expected.to include(options: { cache: config.merge(key: "a_key") }) }
end
context 'with cache:key:files' do
shared_examples 'default key' do
let(:config) do
{ key: { files: files } }
end
it 'uses default key' do
expected = { options: { cache: { key: 'default' } } }
is_expected.to include(expected)
end
end
shared_examples 'version and gemfile files' do
let(:config) do
{
key: {
files: files
},
paths: ['vendor/ruby']
}
end
it 'builds a string key' do
expected = {
options: {
cache: {
key: '703ecc8fef1635427a1f86a8a1a308831c122392',
paths: ['vendor/ruby']
}
}
}
is_expected.to include(expected)
end
end
context 'with existing files' do
let(:files) { ['VERSION', 'Gemfile.zip'] }
it_behaves_like 'version and gemfile files'
end
context 'with files starting with ./' do
let(:files) { ['Gemfile.zip', './VERSION'] }
it_behaves_like 'version and gemfile files'
end
context 'with files ending with /' do
let(:files) { ['Gemfile.zip/'] }
it_behaves_like 'default key'
end
context 'with new line in filenames' do
let(:files) { ["Gemfile.zip\nVERSION"] }
it_behaves_like 'default key'
end
context 'with missing files' do
let(:files) { ['project-gemfile.lock', ''] }
it_behaves_like 'default key'
end
context 'with directories' do
shared_examples 'foo/bar directory key' do
let(:config) do
{
key: {
files: files
}
}
end
it 'builds a string key' do
expected = {
options: {
cache: { key: '74bf43fb1090f161bdd4e265802775dbda2f03d1' }
}
}
is_expected.to include(expected)
end
end
context 'with directory' do
let(:files) { ['foo/bar'] }
it_behaves_like 'foo/bar directory key'
end
context 'with directory ending in slash' do
let(:files) { ['foo/bar/'] }
it_behaves_like 'foo/bar directory key'
end
context 'with directories ending in slash star' do
let(:files) { ['foo/bar/*'] }
it_behaves_like 'foo/bar directory key'
end
end
end
context 'with cache:key:prefix' do
context 'without files' do
let(:config) do
{
key: {
prefix: 'a-prefix'
},
paths: ['vendor/ruby']
}
end
it 'adds prefix to default key' do
expected = {
options: {
cache: {
key: 'a-prefix-default',
paths: ['vendor/ruby']
}
}
}
is_expected.to include(expected)
end
end
context 'with existing files' do
let(:config) do
{
key: {
files: ['VERSION', 'Gemfile.zip'],
prefix: 'a-prefix'
},
paths: ['vendor/ruby']
}
end
it 'adds prefix key' do
expected = {
options: {
cache: {
key: 'a-prefix-703ecc8fef1635427a1f86a8a1a308831c122392',
paths: ['vendor/ruby']
}
}
}
is_expected.to include(expected)
end
end
context 'with missing files' do
let(:config) do
{
key: {
files: ['project-gemfile.lock', ''],
prefix: 'a-prefix'
},
paths: ['vendor/ruby']
}
end
it 'adds prefix to default key' do
expected = {
options: {
cache: {
key: 'a-prefix-default',
paths: ['vendor/ruby']
}
}
}
is_expected.to include(expected)
end
end
end
context 'with all cache option keys' do
let(:config) do
{
key: 'a-key',
paths: ['vendor/ruby'],
untracked: true,
policy: 'push',
when: 'on_success'
}
end
it { is_expected.to include(options: { cache: config }) }
end
context 'with unknown cache option keys' do
let(:config) do
{
key: 'a-key',
unknown_key: true
}
end
it { expect { subject }.to raise_error(ArgumentError, /unknown_key/) }
end
context 'with empty config' do
let(:config) { {} }
it { is_expected.to include(options: {}) }
end
end
end
describe '#attributes' do
subject { processor.attributes }
context 'with cache:key' do context 'with cache:key' do
let(:config) do let(:config) do
...@@ -20,7 +267,7 @@ RSpec.describe Gitlab::Ci::Pipeline::Seed::Build::Cache do ...@@ -20,7 +267,7 @@ RSpec.describe Gitlab::Ci::Pipeline::Seed::Build::Cache do
} }
end end
it { is_expected.to include(options: { cache: config }) } it { is_expected.to include(config) }
end end
context 'with cache:key as a symbol' do context 'with cache:key as a symbol' do
...@@ -31,7 +278,7 @@ RSpec.describe Gitlab::Ci::Pipeline::Seed::Build::Cache do ...@@ -31,7 +278,7 @@ RSpec.describe Gitlab::Ci::Pipeline::Seed::Build::Cache do
} }
end end
it { is_expected.to include(options: { cache: config.merge(key: "a_key") }) } it { is_expected.to include(config.merge(key: "a_key")) }
end end
context 'with cache:key:files' do context 'with cache:key:files' do
...@@ -41,7 +288,7 @@ RSpec.describe Gitlab::Ci::Pipeline::Seed::Build::Cache do ...@@ -41,7 +288,7 @@ RSpec.describe Gitlab::Ci::Pipeline::Seed::Build::Cache do
end end
it 'uses default key' do it 'uses default key' do
expected = { options: { cache: { key: 'default' } } } expected = { key: 'default' }
is_expected.to include(expected) is_expected.to include(expected)
end end
...@@ -59,13 +306,9 @@ RSpec.describe Gitlab::Ci::Pipeline::Seed::Build::Cache do ...@@ -59,13 +306,9 @@ RSpec.describe Gitlab::Ci::Pipeline::Seed::Build::Cache do
it 'builds a string key' do it 'builds a string key' do
expected = { expected = {
options: {
cache: {
key: '703ecc8fef1635427a1f86a8a1a308831c122392', key: '703ecc8fef1635427a1f86a8a1a308831c122392',
paths: ['vendor/ruby'] paths: ['vendor/ruby']
}
} }
}
is_expected.to include(expected) is_expected.to include(expected)
end end
...@@ -112,11 +355,7 @@ RSpec.describe Gitlab::Ci::Pipeline::Seed::Build::Cache do ...@@ -112,11 +355,7 @@ RSpec.describe Gitlab::Ci::Pipeline::Seed::Build::Cache do
end end
it 'builds a string key' do it 'builds a string key' do
expected = { expected = { key: '74bf43fb1090f161bdd4e265802775dbda2f03d1' }
options: {
cache: { key: '74bf43fb1090f161bdd4e265802775dbda2f03d1' }
}
}
is_expected.to include(expected) is_expected.to include(expected)
end end
...@@ -155,13 +394,9 @@ RSpec.describe Gitlab::Ci::Pipeline::Seed::Build::Cache do ...@@ -155,13 +394,9 @@ RSpec.describe Gitlab::Ci::Pipeline::Seed::Build::Cache do
it 'adds prefix to default key' do it 'adds prefix to default key' do
expected = { expected = {
options: {
cache: {
key: 'a-prefix-default', key: 'a-prefix-default',
paths: ['vendor/ruby'] paths: ['vendor/ruby']
} }
}
}
is_expected.to include(expected) is_expected.to include(expected)
end end
...@@ -180,13 +415,9 @@ RSpec.describe Gitlab::Ci::Pipeline::Seed::Build::Cache do ...@@ -180,13 +415,9 @@ RSpec.describe Gitlab::Ci::Pipeline::Seed::Build::Cache do
it 'adds prefix key' do it 'adds prefix key' do
expected = { expected = {
options: {
cache: {
key: 'a-prefix-703ecc8fef1635427a1f86a8a1a308831c122392', key: 'a-prefix-703ecc8fef1635427a1f86a8a1a308831c122392',
paths: ['vendor/ruby'] paths: ['vendor/ruby']
} }
}
}
is_expected.to include(expected) is_expected.to include(expected)
end end
...@@ -205,13 +436,9 @@ RSpec.describe Gitlab::Ci::Pipeline::Seed::Build::Cache do ...@@ -205,13 +436,9 @@ RSpec.describe Gitlab::Ci::Pipeline::Seed::Build::Cache do
it 'adds prefix to default key' do it 'adds prefix to default key' do
expected = { expected = {
options: {
cache: {
key: 'a-prefix-default', key: 'a-prefix-default',
paths: ['vendor/ruby'] paths: ['vendor/ruby']
} }
}
}
is_expected.to include(expected) is_expected.to include(expected)
end end
...@@ -229,7 +456,7 @@ RSpec.describe Gitlab::Ci::Pipeline::Seed::Build::Cache do ...@@ -229,7 +456,7 @@ RSpec.describe Gitlab::Ci::Pipeline::Seed::Build::Cache do
} }
end end
it { is_expected.to include(options: { cache: config }) } it { is_expected.to include(config) }
end end
context 'with unknown cache option keys' do context 'with unknown cache option keys' do
...@@ -242,11 +469,5 @@ RSpec.describe Gitlab::Ci::Pipeline::Seed::Build::Cache do ...@@ -242,11 +469,5 @@ RSpec.describe Gitlab::Ci::Pipeline::Seed::Build::Cache do
it { expect { subject }.to raise_error(ArgumentError, /unknown_key/) } it { expect { subject }.to raise_error(ArgumentError, /unknown_key/) }
end end
context 'with empty config' do
let(:config) { {} }
it { is_expected.to include(options: {}) }
end
end end
end end
...@@ -87,86 +87,167 @@ RSpec.describe Gitlab::Ci::Pipeline::Seed::Build do ...@@ -87,86 +87,167 @@ RSpec.describe Gitlab::Ci::Pipeline::Seed::Build do
end end
end end
context 'with cache:key' do context 'with multiple_cache_per_job FF disabled' do
let(:attributes) do before do
{ stub_feature_flags(multiple_cache_per_job: false)
name: 'rspec', end
ref: 'master',
cache: { context 'with cache:key' do
key: 'a-value' let(:attributes) do
{
name: 'rspec',
ref: 'master',
cache: {
key: 'a-value'
}
} }
} end
it { is_expected.to include(options: { cache: { key: 'a-value' } }) }
end end
it { is_expected.to include(options: { cache: { key: 'a-value' } }) } context 'with cache:key:files' do
end let(:attributes) do
{
name: 'rspec',
ref: 'master',
cache: {
key: {
files: ['VERSION']
}
}
}
end
context 'with cache:key:files' do it 'includes cache options' do
let(:attributes) do cache_options = {
{ options: {
name: 'rspec', cache: { key: 'f155568ad0933d8358f66b846133614f76dd0ca4' }
ref: 'master',
cache: {
key: {
files: ['VERSION']
} }
} }
}
is_expected.to include(cache_options)
end
end end
it 'includes cache options' do context 'with cache:key:prefix' do
cache_options = { let(:attributes) do
options: { {
name: 'rspec',
ref: 'master',
cache: { cache: {
key: 'f155568ad0933d8358f66b846133614f76dd0ca4' key: {
prefix: 'something'
}
} }
} }
} end
is_expected.to include(cache_options) it { is_expected.to include(options: { cache: { key: 'something-default' } }) }
end end
end
context 'with cache:key:prefix' do context 'with cache:key:files and prefix' do
let(:attributes) do let(:attributes) do
{ {
name: 'rspec', name: 'rspec',
ref: 'master', ref: 'master',
cache: { cache: {
key: { key: {
prefix: 'something' files: ['VERSION'],
prefix: 'something'
}
}
}
end
it 'includes cache options' do
cache_options = {
options: {
cache: { key: 'something-f155568ad0933d8358f66b846133614f76dd0ca4' }
} }
} }
}
end
it { is_expected.to include(options: { cache: { key: 'something-default' } }) } is_expected.to include(cache_options)
end
end
end end
context 'with cache:key:files and prefix' do context 'with cache:key' do
let(:attributes) do let(:attributes) do
{ {
name: 'rspec', name: 'rspec',
ref: 'master', ref: 'master',
cache: { cache: [{
key: { key: 'a-value'
files: ['VERSION'], }]
prefix: 'something' }
end
it { is_expected.to include(options: { cache: [a_hash_including(key: 'a-value')] }) }
context 'with cache:key:files' do
let(:attributes) do
{
name: 'rspec',
ref: 'master',
cache: [{
key: {
files: ['VERSION']
}
}]
}
end
it 'includes cache options' do
cache_options = {
options: {
cache: [a_hash_including(key: 'f155568ad0933d8358f66b846133614f76dd0ca4')]
} }
} }
}
is_expected.to include(cache_options)
end
end end
it 'includes cache options' do context 'with cache:key:prefix' do
cache_options = { let(:attributes) do
options: { {
cache: { name: 'rspec',
key: 'something-f155568ad0933d8358f66b846133614f76dd0ca4' ref: 'master',
cache: [{
key: {
prefix: 'something'
}
}]
}
end
it { is_expected.to include(options: { cache: [a_hash_including( key: 'something-default' )] }) }
end
context 'with cache:key:files and prefix' do
let(:attributes) do
{
name: 'rspec',
ref: 'master',
cache: [{
key: {
files: ['VERSION'],
prefix: 'something'
}
}]
}
end
it 'includes cache options' do
cache_options = {
options: {
cache: [a_hash_including(key: 'something-f155568ad0933d8358f66b846133614f76dd0ca4')]
} }
} }
}
is_expected.to include(cache_options) is_expected.to include(cache_options)
end
end end
end end
...@@ -179,7 +260,7 @@ RSpec.describe Gitlab::Ci::Pipeline::Seed::Build do ...@@ -179,7 +260,7 @@ RSpec.describe Gitlab::Ci::Pipeline::Seed::Build do
} }
end end
it { is_expected.to include(options: {}) } it { is_expected.to include({}) }
end end
context 'with allow_failure' do context 'with allow_failure' do
...@@ -296,7 +377,7 @@ RSpec.describe Gitlab::Ci::Pipeline::Seed::Build do ...@@ -296,7 +377,7 @@ RSpec.describe Gitlab::Ci::Pipeline::Seed::Build do
it 'does not have environment' do it 'does not have environment' do
expect(subject).not_to be_has_environment expect(subject).not_to be_has_environment
expect(subject.environment).to be_nil expect(subject.environment).to be_nil
expect(subject.metadata.expanded_environment_name).to be_nil expect(subject.metadata).to be_nil
expect(Environment.exists?(name: expected_environment_name)).to eq(false) expect(Environment.exists?(name: expected_environment_name)).to eq(false)
end end
end end
......
...@@ -1368,6 +1368,155 @@ module Gitlab ...@@ -1368,6 +1368,155 @@ module Gitlab
end end
end end
context 'with multiple_cache_per_job FF disabled' do
before do
stub_feature_flags(multiple_cache_per_job: false)
end
describe 'cache' do
context 'when cache definition has unknown keys' do
let(:config) do
YAML.dump(
{ cache: { untracked: true, invalid: 'key' },
rspec: { script: 'rspec' } })
end
it_behaves_like 'returns errors', 'cache config contains unknown keys: invalid'
end
it "returns cache when defined globally" do
config = YAML.dump({
cache: { paths: ["logs/", "binaries/"], untracked: true, key: 'key' },
rspec: {
script: "rspec"
}
})
config_processor = Gitlab::Ci::YamlProcessor.new(config).execute
expect(config_processor.stage_builds_attributes("test").size).to eq(1)
expect(config_processor.stage_builds_attributes("test").first[:cache]).to eq(
paths: ["logs/", "binaries/"],
untracked: true,
key: 'key',
policy: 'pull-push',
when: 'on_success'
)
end
it "returns cache when defined in default context" do
config = YAML.dump(
{
default: {
cache: { paths: ["logs/", "binaries/"], untracked: true, key: { files: ['file'] } }
},
rspec: {
script: "rspec"
}
})
config_processor = Gitlab::Ci::YamlProcessor.new(config).execute
expect(config_processor.stage_builds_attributes("test").size).to eq(1)
expect(config_processor.stage_builds_attributes("test").first[:cache]).to eq(
paths: ["logs/", "binaries/"],
untracked: true,
key: { files: ['file'] },
policy: 'pull-push',
when: 'on_success'
)
end
it 'returns cache key when defined in a job' do
config = YAML.dump({
rspec: {
cache: { paths: ['logs/', 'binaries/'], untracked: true, key: 'key' },
script: 'rspec'
}
})
config_processor = Gitlab::Ci::YamlProcessor.new(config).execute
expect(config_processor.stage_builds_attributes('test').size).to eq(1)
expect(config_processor.stage_builds_attributes('test').first[:cache]).to eq(
paths: ['logs/', 'binaries/'],
untracked: true,
key: 'key',
policy: 'pull-push',
when: 'on_success'
)
end
it 'returns cache files' do
config = YAML.dump(
rspec: {
cache: {
paths: ['logs/', 'binaries/'],
untracked: true,
key: { files: ['file'] }
},
script: 'rspec'
}
)
config_processor = Gitlab::Ci::YamlProcessor.new(config).execute
expect(config_processor.stage_builds_attributes('test').size).to eq(1)
expect(config_processor.stage_builds_attributes('test').first[:cache]).to eq(
paths: ['logs/', 'binaries/'],
untracked: true,
key: { files: ['file'] },
policy: 'pull-push',
when: 'on_success'
)
end
it 'returns cache files with prefix' do
config = YAML.dump(
rspec: {
cache: {
paths: ['logs/', 'binaries/'],
untracked: true,
key: { files: ['file'], prefix: 'prefix' }
},
script: 'rspec'
}
)
config_processor = Gitlab::Ci::YamlProcessor.new(config).execute
expect(config_processor.stage_builds_attributes('test').size).to eq(1)
expect(config_processor.stage_builds_attributes('test').first[:cache]).to eq(
paths: ['logs/', 'binaries/'],
untracked: true,
key: { files: ['file'], prefix: 'prefix' },
policy: 'pull-push',
when: 'on_success'
)
end
it "overwrite cache when defined for a job and globally" do
config = YAML.dump({
cache: { paths: ["logs/", "binaries/"], untracked: true, key: 'global' },
rspec: {
script: "rspec",
cache: { paths: ["test/"], untracked: false, key: 'local' }
}
})
config_processor = Gitlab::Ci::YamlProcessor.new(config).execute
expect(config_processor.stage_builds_attributes("test").size).to eq(1)
expect(config_processor.stage_builds_attributes("test").first[:cache]).to eq(
paths: ["test/"],
untracked: false,
key: 'local',
policy: 'pull-push',
when: 'on_success'
)
end
end
end
describe 'cache' do describe 'cache' do
context 'when cache definition has unknown keys' do context 'when cache definition has unknown keys' do
let(:config) do let(:config) do
...@@ -1381,22 +1530,22 @@ module Gitlab ...@@ -1381,22 +1530,22 @@ module Gitlab
it "returns cache when defined globally" do it "returns cache when defined globally" do
config = YAML.dump({ config = YAML.dump({
cache: { paths: ["logs/", "binaries/"], untracked: true, key: 'key' }, cache: { paths: ["logs/", "binaries/"], untracked: true, key: 'key' },
rspec: { rspec: {
script: "rspec" script: "rspec"
} }
}) })
config_processor = Gitlab::Ci::YamlProcessor.new(config).execute config_processor = Gitlab::Ci::YamlProcessor.new(config).execute
expect(config_processor.stage_builds_attributes("test").size).to eq(1) expect(config_processor.stage_builds_attributes("test").size).to eq(1)
expect(config_processor.stage_builds_attributes("test").first[:cache]).to eq( expect(config_processor.stage_builds_attributes("test").first[:cache]).to eq([
paths: ["logs/", "binaries/"], paths: ["logs/", "binaries/"],
untracked: true, untracked: true,
key: 'key', key: 'key',
policy: 'pull-push', policy: 'pull-push',
when: 'on_success' when: 'on_success'
) ])
end end
it "returns cache when defined in default context" do it "returns cache when defined in default context" do
...@@ -1413,32 +1562,46 @@ module Gitlab ...@@ -1413,32 +1562,46 @@ module Gitlab
config_processor = Gitlab::Ci::YamlProcessor.new(config).execute config_processor = Gitlab::Ci::YamlProcessor.new(config).execute
expect(config_processor.stage_builds_attributes("test").size).to eq(1) expect(config_processor.stage_builds_attributes("test").size).to eq(1)
expect(config_processor.stage_builds_attributes("test").first[:cache]).to eq( expect(config_processor.stage_builds_attributes("test").first[:cache]).to eq([
paths: ["logs/", "binaries/"], paths: ["logs/", "binaries/"],
untracked: true, untracked: true,
key: { files: ['file'] }, key: { files: ['file'] },
policy: 'pull-push', policy: 'pull-push',
when: 'on_success' when: 'on_success'
) ])
end end
it 'returns cache key when defined in a job' do it 'returns cache key/s when defined in a job' do
config = YAML.dump({ config = YAML.dump({
rspec: { rspec: {
cache: { paths: ['logs/', 'binaries/'], untracked: true, key: 'key' }, cache: [
script: 'rspec' { paths: ['binaries/'], untracked: true, key: 'keya' },
} { paths: ['logs/', 'binaries/'], untracked: true, key: 'key' }
}) ],
script: 'rspec'
}
})
config_processor = Gitlab::Ci::YamlProcessor.new(config).execute config_processor = Gitlab::Ci::YamlProcessor.new(config).execute
expect(config_processor.stage_builds_attributes('test').size).to eq(1) expect(config_processor.stage_builds_attributes('test').size).to eq(1)
expect(config_processor.stage_builds_attributes('test').first[:cache]).to eq( expect(config_processor.stage_builds_attributes('test').first[:cache]).to eq(
paths: ['logs/', 'binaries/'], [
untracked: true, {
key: 'key', paths: ['binaries/'],
policy: 'pull-push', untracked: true,
when: 'on_success' key: 'keya',
policy: 'pull-push',
when: 'on_success'
},
{
paths: ['logs/', 'binaries/'],
untracked: true,
key: 'key',
policy: 'pull-push',
when: 'on_success'
}
]
) )
end end
...@@ -1446,10 +1609,10 @@ module Gitlab ...@@ -1446,10 +1609,10 @@ module Gitlab
config = YAML.dump( config = YAML.dump(
rspec: { rspec: {
cache: { cache: {
paths: ['logs/', 'binaries/'], paths: ['binaries/'],
untracked: true, untracked: true,
key: { files: ['file'] } key: { files: ['file'] }
}, },
script: 'rspec' script: 'rspec'
} }
) )
...@@ -1457,13 +1620,13 @@ module Gitlab ...@@ -1457,13 +1620,13 @@ module Gitlab
config_processor = Gitlab::Ci::YamlProcessor.new(config).execute config_processor = Gitlab::Ci::YamlProcessor.new(config).execute
expect(config_processor.stage_builds_attributes('test').size).to eq(1) expect(config_processor.stage_builds_attributes('test').size).to eq(1)
expect(config_processor.stage_builds_attributes('test').first[:cache]).to eq( expect(config_processor.stage_builds_attributes('test').first[:cache]).to eq([
paths: ['logs/', 'binaries/'], paths: ['binaries/'],
untracked: true, untracked: true,
key: { files: ['file'] }, key: { files: ['file'] },
policy: 'pull-push', policy: 'pull-push',
when: 'on_success' when: 'on_success'
) ])
end end
it 'returns cache files with prefix' do it 'returns cache files with prefix' do
...@@ -1481,34 +1644,34 @@ module Gitlab ...@@ -1481,34 +1644,34 @@ module Gitlab
config_processor = Gitlab::Ci::YamlProcessor.new(config).execute config_processor = Gitlab::Ci::YamlProcessor.new(config).execute
expect(config_processor.stage_builds_attributes('test').size).to eq(1) expect(config_processor.stage_builds_attributes('test').size).to eq(1)
expect(config_processor.stage_builds_attributes('test').first[:cache]).to eq( expect(config_processor.stage_builds_attributes('test').first[:cache]).to eq([
paths: ['logs/', 'binaries/'], paths: ['logs/', 'binaries/'],
untracked: true, untracked: true,
key: { files: ['file'], prefix: 'prefix' }, key: { files: ['file'], prefix: 'prefix' },
policy: 'pull-push', policy: 'pull-push',
when: 'on_success' when: 'on_success'
) ])
end end
it "overwrite cache when defined for a job and globally" do it "overwrite cache when defined for a job and globally" do
config = YAML.dump({ config = YAML.dump({
cache: { paths: ["logs/", "binaries/"], untracked: true, key: 'global' }, cache: { paths: ["logs/", "binaries/"], untracked: true, key: 'global' },
rspec: { rspec: {
script: "rspec", script: "rspec",
cache: { paths: ["test/"], untracked: false, key: 'local' } cache: { paths: ["test/"], untracked: false, key: 'local' }
} }
}) })
config_processor = Gitlab::Ci::YamlProcessor.new(config).execute config_processor = Gitlab::Ci::YamlProcessor.new(config).execute
expect(config_processor.stage_builds_attributes("test").size).to eq(1) expect(config_processor.stage_builds_attributes("test").size).to eq(1)
expect(config_processor.stage_builds_attributes("test").first[:cache]).to eq( expect(config_processor.stage_builds_attributes("test").first[:cache]).to eq([
paths: ["test/"], paths: ["test/"],
untracked: false, untracked: false,
key: 'local', key: 'local',
policy: 'pull-push', policy: 'pull-push',
when: 'on_success' when: 'on_success'
) ])
end end
end end
......
...@@ -817,7 +817,48 @@ RSpec.describe Ci::Build do ...@@ -817,7 +817,48 @@ RSpec.describe Ci::Build do
end end
describe '#cache' do describe '#cache' do
let(:options) { { cache: { key: "key", paths: ["public"], policy: "pull-push" } } } let(:options) do
{ cache: [{ key: "key", paths: ["public"], policy: "pull-push" }] }
end
context 'with multiple_cache_per_job FF disabled' do
before do
stub_feature_flags(multiple_cache_per_job: false)
end
let(:options) { { cache: { key: "key", paths: ["public"], policy: "pull-push" } } }
subject { build.cache }
context 'when build has cache' do
before do
allow(build).to receive(:options).and_return(options)
end
context 'when project has jobs_cache_index' do
before do
allow_any_instance_of(Project).to receive(:jobs_cache_index).and_return(1)
end
it { is_expected.to be_an(Array).and all(include(key: "key-1")) }
end
context 'when project does not have jobs_cache_index' do
before do
allow_any_instance_of(Project).to receive(:jobs_cache_index).and_return(nil)
end
it { is_expected.to eq([options[:cache]]) }
end
end
context 'when build does not have cache' do
before do
allow(build).to receive(:options).and_return({})
end
it { is_expected.to eq([]) }
end
end
subject { build.cache } subject { build.cache }
...@@ -826,6 +867,21 @@ RSpec.describe Ci::Build do ...@@ -826,6 +867,21 @@ RSpec.describe Ci::Build do
allow(build).to receive(:options).and_return(options) allow(build).to receive(:options).and_return(options)
end end
context 'when build has multiple caches' do
let(:options) do
{ cache: [
{ key: "key", paths: ["public"], policy: "pull-push" },
{ key: "key2", paths: ["public"], policy: "pull-push" }
] }
end
before do
allow_any_instance_of(Project).to receive(:jobs_cache_index).and_return(1)
end
it { is_expected.to match([a_hash_including(key: "key-1"), a_hash_including(key: "key2-1")]) }
end
context 'when project has jobs_cache_index' do context 'when project has jobs_cache_index' do
before do before do
allow_any_instance_of(Project).to receive(:jobs_cache_index).and_return(1) allow_any_instance_of(Project).to receive(:jobs_cache_index).and_return(1)
...@@ -839,7 +895,7 @@ RSpec.describe Ci::Build do ...@@ -839,7 +895,7 @@ RSpec.describe Ci::Build do
allow_any_instance_of(Project).to receive(:jobs_cache_index).and_return(nil) allow_any_instance_of(Project).to receive(:jobs_cache_index).and_return(nil)
end end
it { is_expected.to eq([options[:cache]]) } it { is_expected.to eq(options[:cache]) }
end end
end end
...@@ -848,7 +904,7 @@ RSpec.describe Ci::Build do ...@@ -848,7 +904,7 @@ RSpec.describe Ci::Build do
allow(build).to receive(:options).and_return({}) allow(build).to receive(:options).and_return({})
end end
it { is_expected.to eq([nil]) } it { is_expected.to be_empty }
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