debian-mirror-gitlab/spec/finders/environments_finder_spec.rb

177 lines
6.5 KiB
Ruby
Raw Normal View History

2019-12-21 20:55:43 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
require 'spec_helper'
2020-06-23 00:09:42 +05:30
RSpec.describe EnvironmentsFinder do
2020-04-22 19:07:51 +05:30
let(:project) { create(:project, :repository) }
let(:user) { project.creator }
let(:environment) { create(:environment, :available, project: project) }
2017-08-17 22:00:37 +05:30
2020-04-22 19:07:51 +05:30
before do
project.add_maintainer(user)
end
2017-08-17 22:00:37 +05:30
2020-04-22 19:07:51 +05:30
describe '#execute' do
2017-08-17 22:00:37 +05:30
context 'tagged deployment' do
2020-03-13 15:44:24 +05:30
let(:environment_two) { create(:environment, project: project) }
# Environments need to include commits, so rewind two commits to fit
let(:commit) { project.commit('HEAD~2') }
2017-08-17 22:00:37 +05:30
before do
2020-03-13 15:44:24 +05:30
create(:deployment, :success, environment: environment, ref: 'v1.0.0', tag: true, sha: project.commit.id)
create(:deployment, :success, environment: environment_two, ref: 'v1.1.0', tag: true, sha: project.commit('HEAD~1').id)
2017-08-17 22:00:37 +05:30
end
it 'returns environment when with_tags is set' do
2020-03-13 15:44:24 +05:30
expect(described_class.new(project, user, ref: 'master', commit: commit, with_tags: true).execute)
.to contain_exactly(environment, environment_two)
2017-08-17 22:00:37 +05:30
end
it 'does not return environment when no with_tags is set' do
2020-03-13 15:44:24 +05:30
expect(described_class.new(project, user, ref: 'master', commit: commit).execute)
2017-08-17 22:00:37 +05:30
.to be_empty
end
it 'does not return environment when commit is not part of deployment' do
expect(described_class.new(project, user, ref: 'master', commit: project.commit('feature')).execute)
.to be_empty
end
2020-03-13 15:44:24 +05:30
# We expect two Gitaly calls: FindCommit, CommitIsAncestor
# This tests to ensure we don't call one CommitIsAncestor per environment
it 'only calls Gitaly twice when multiple environments are present', :request_store do
expect do
result = described_class.new(project, user, ref: 'master', commit: commit, with_tags: true, find_latest: true).execute
expect(result).to contain_exactly(environment_two)
end.to change { Gitlab::GitalyClient.get_request_count }.by(2)
end
2017-08-17 22:00:37 +05:30
end
context 'branch deployment' do
before do
2018-12-13 13:39:08 +05:30
create(:deployment, :success, environment: environment, ref: 'master', sha: project.commit.id)
2017-08-17 22:00:37 +05:30
end
it 'returns environment when ref is set' do
expect(described_class.new(project, user, ref: 'master', commit: project.commit).execute)
.to contain_exactly(environment)
end
it 'does not environment when ref is different' do
expect(described_class.new(project, user, ref: 'feature', commit: project.commit).execute)
.to be_empty
end
it 'does not return environment when commit is not part of deployment' do
expect(described_class.new(project, user, ref: 'master', commit: project.commit('feature')).execute)
.to be_empty
end
it 'returns environment when commit constraint is not set' do
expect(described_class.new(project, user, ref: 'master').execute)
.to contain_exactly(environment)
end
end
context 'commit deployment' do
before do
2018-12-13 13:39:08 +05:30
create(:deployment, :success, environment: environment, ref: 'master', sha: project.commit.id)
2017-08-17 22:00:37 +05:30
end
it 'returns environment' do
expect(described_class.new(project, user, commit: project.commit).execute)
.to contain_exactly(environment)
end
end
context 'recently updated' do
context 'when last deployment to environment is the most recent one' do
before do
2018-12-13 13:39:08 +05:30
create(:deployment, :success, environment: environment, ref: 'feature')
2017-08-17 22:00:37 +05:30
end
it 'finds recently updated environment' do
expect(described_class.new(project, user, ref: 'feature', recently_updated: true).execute)
.to contain_exactly(environment)
end
end
context 'when last deployment to environment is not the most recent' do
before do
2018-12-13 13:39:08 +05:30
create(:deployment, :success, environment: environment, ref: 'feature')
create(:deployment, :success, environment: environment, ref: 'master')
2017-08-17 22:00:37 +05:30
end
it 'does not find environment' do
expect(described_class.new(project, user, ref: 'feature', recently_updated: true).execute)
.to be_empty
end
end
context 'when there are two environments that deploy to the same branch' do
let(:second_environment) { create(:environment, project: project) }
before do
2018-12-13 13:39:08 +05:30
create(:deployment, :success, environment: environment, ref: 'feature')
create(:deployment, :success, environment: second_environment, ref: 'feature')
2017-08-17 22:00:37 +05:30
end
it 'finds both environments' do
expect(described_class.new(project, user, ref: 'feature', recently_updated: true).execute)
.to contain_exactly(environment, second_environment)
end
end
end
end
2020-04-22 19:07:51 +05:30
describe '#find' do
context 'with states parameter' do
let(:stopped_environment) { create(:environment, :stopped, project: project) }
it 'returns environments with the requested state' do
result = described_class.new(project, user, states: 'available').find
expect(result).to contain_exactly(environment)
end
it 'returns environments with any of the requested states' do
result = described_class.new(project, user, states: %w(available stopped)).find
expect(result).to contain_exactly(environment, stopped_environment)
end
it 'raises exception when requested state is invalid' do
expect { described_class.new(project, user, states: %w(invalid stopped)).find }.to(
raise_error(described_class::InvalidStatesError, 'Requested states are invalid')
)
end
context 'works with symbols' do
it 'returns environments with the requested state' do
result = described_class.new(project, user, states: :available).find
expect(result).to contain_exactly(environment)
end
it 'returns environments with any of the requested states' do
result = described_class.new(project, user, states: [:available, :stopped]).find
expect(result).to contain_exactly(environment, stopped_environment)
end
end
end
context 'with search and states' do
let(:environment2) { create(:environment, :stopped, name: 'test2', project: project) }
let(:environment3) { create(:environment, :available, name: 'test3', project: project) }
it 'searches environments by name and state' do
result = described_class.new(project, user, search: 'test', states: :available).find
expect(result).to contain_exactly(environment3)
end
end
end
2017-08-17 22:00:37 +05:30
end