debian-mirror-gitlab/spec/features/projects/environments/environment_spec.rb

390 lines
13 KiB
Ruby
Raw Normal View History

2019-10-12 21:52:04 +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 'Environment' do
2020-03-13 15:44:24 +05:30
let(:project) { create(:project, :repository) }
2018-11-08 19:23:39 +05:30
let(:user) { create(:user) }
let(:role) { :developer }
2017-08-17 22:00:37 +05:30
2018-11-08 19:23:39 +05:30
before do
2017-09-10 17:25:29 +05:30
sign_in(user)
2018-03-17 18:26:18 +05:30
project.add_role(user, role)
2017-08-17 22:00:37 +05:30
end
2020-03-13 15:44:24 +05:30
def auto_stop_button_selector
%q{button[title="Prevent environment from auto-stopping"]}
end
2018-11-08 19:23:39 +05:30
describe 'environment details page' do
let!(:environment) { create(:environment, project: project) }
2022-08-27 11:52:29 +05:30
let!(:permissions) {}
let!(:deployment) {}
let!(:action) {}
let!(:cluster) {}
2017-08-17 22:00:37 +05:30
2020-03-13 15:44:24 +05:30
context 'with auto-stop' do
let!(:environment) { create(:environment, :will_auto_stop, name: 'staging', project: project) }
before do
visit_environment(environment)
end
2021-10-27 15:23:28 +05:30
it 'shows auto stop info', :js do
2020-03-13 15:44:24 +05:30
expect(page).to have_content('Auto stops')
end
2021-10-27 15:23:28 +05:30
it 'shows auto stop button', :js do
2020-03-13 15:44:24 +05:30
expect(page).to have_selector(auto_stop_button_selector)
expect(page.find(auto_stop_button_selector).find(:xpath, '..')['action']).to have_content(cancel_auto_stop_project_environment_path(environment.project, environment))
end
it 'allows user to cancel auto stop', :js do
page.find(auto_stop_button_selector).click
wait_for_all_requests
expect(page).to have_content('Auto stop successfully canceled.')
expect(page).not_to have_selector(auto_stop_button_selector)
end
end
2017-08-17 22:00:37 +05:30
context 'without deployments' do
2021-12-11 22:18:48 +05:30
before do
visit_environment(environment)
end
2018-12-13 13:39:08 +05:30
it 'does not show deployments' do
2017-08-17 22:00:37 +05:30
expect(page).to have_content('You don\'t have any deployments right now.')
end
end
context 'with deployments' do
2021-12-11 22:18:48 +05:30
before do
visit_environment(environment)
end
2017-08-17 22:00:37 +05:30
context 'when there is no related deployable' do
2018-11-08 19:23:39 +05:30
let(:deployment) do
2018-12-13 13:39:08 +05:30
create(:deployment, :success, environment: environment, deployable: nil)
2017-08-17 22:00:37 +05:30
end
2018-11-08 19:23:39 +05:30
it 'does show deployment SHA' do
2017-08-17 22:00:37 +05:30
expect(page).to have_link(deployment.short_sha)
expect(page).not_to have_link('Re-deploy')
end
end
2018-12-13 13:39:08 +05:30
context 'when there is a successful deployment' do
let(:pipeline) { create(:ci_pipeline, project: project) }
let(:build) { create(:ci_build, :success, pipeline: pipeline) }
let(:deployment) do
create(:deployment, :success, environment: environment, deployable: build)
end
it 'does show deployments' do
expect(page).to have_link("#{build.name} (##{build.id})")
end
end
context 'when there is a running deployment' do
let(:pipeline) { create(:ci_pipeline, project: project) }
let(:build) { create(:ci_build, pipeline: pipeline) }
let(:deployment) do
create(:deployment, :running, environment: environment, deployable: build)
end
2019-12-21 20:55:43 +05:30
it 'does show deployments' do
expect(page).to have_link("#{build.name} (##{build.id})")
2018-12-13 13:39:08 +05:30
end
end
context 'when there is a failed deployment' do
let(:pipeline) { create(:ci_pipeline, project: project) }
let(:build) { create(:ci_build, pipeline: pipeline) }
let(:deployment) do
create(:deployment, :failed, environment: environment, deployable: build)
end
2019-12-21 20:55:43 +05:30
it 'does show deployments' do
expect(page).to have_link("#{build.name} (##{build.id})")
2018-12-13 13:39:08 +05:30
end
end
2021-12-11 22:18:48 +05:30
context 'with many deployments' do
let(:pipeline) { create(:ci_pipeline, project: project) }
let(:build) { create(:ci_build, pipeline: pipeline) }
let!(:second) { create(:deployment, environment: environment, deployable: build, status: :success, finished_at: Time.current) }
let!(:first) { create(:deployment, environment: environment, deployable: build, status: :running) }
let!(:last) { create(:deployment, environment: environment, deployable: build, status: :success, finished_at: 2.days.ago) }
let!(:third) { create(:deployment, environment: environment, deployable: build, status: :canceled, finished_at: 1.day.ago) }
before do
visit_environment(environment)
end
it 'shows all of them in ordered way' do
ids = find_all('[data-testid="deployment-id"]').map { |e| e.text }
expected_ordered_ids = [first, second, third, last].map { |d| "##{d.iid}" }
expect(ids).to eq(expected_ordered_ids)
end
end
2022-04-04 11:22:00 +05:30
context 'with upcoming deployments' do
let(:pipeline) { create(:ci_pipeline, project: project) }
let(:build) { create(:ci_build, pipeline: pipeline) }
let!(:runnind_deployment_1) { create(:deployment, environment: environment, deployable: build, status: :running) }
let!(:runnind_deployment_2) { create(:deployment, environment: environment, deployable: build, status: :running) }
# Success deployments must have present `finished_at`. We'll backfill in the future.
# See https://gitlab.com/gitlab-org/gitlab/-/issues/350618 for more information.
let!(:success_without_finished_at) { create(:deployment, environment: environment, deployable: build, status: :success, finished_at: nil) }
before do
visit_environment(environment)
end
# This ordering is unexpected and to be fixed.
# See https://gitlab.com/gitlab-org/gitlab/-/issues/350618 for more information.
it 'shows upcoming deployments in unordered way' do
displayed_ids = find_all('[data-testid="deployment-id"]').map { |e| e.text }
internal_ids = [runnind_deployment_1, runnind_deployment_2, success_without_finished_at].map { |d| "##{d.iid}" }
expect(displayed_ids).to match_array(internal_ids)
end
end
2017-08-17 22:00:37 +05:30
context 'with related deployable present' do
2018-11-08 19:23:39 +05:30
let(:pipeline) { create(:ci_pipeline, project: project) }
2022-07-23 23:45:48 +05:30
let(:build) { create(:ci_build, pipeline: pipeline, environment: environment.name) }
2017-08-17 22:00:37 +05:30
2018-11-08 19:23:39 +05:30
let(:deployment) do
2018-12-13 13:39:08 +05:30
create(:deployment, :success, environment: environment, deployable: build)
2017-08-17 22:00:37 +05:30
end
2021-12-11 22:18:48 +05:30
before do
visit_environment(environment)
end
2018-11-08 19:23:39 +05:30
it 'does show build name' do
2017-08-17 22:00:37 +05:30
expect(page).to have_link("#{build.name} (##{build.id})")
end
2020-03-13 15:44:24 +05:30
it 'shows the re-deploy button' do
expect(page).to have_button('Re-deploy to environment')
2018-12-13 13:39:08 +05:30
end
2017-08-17 22:00:37 +05:30
context 'with manual action' do
2018-11-08 19:23:39 +05:30
let(:action) do
2017-08-17 22:00:37 +05:30
create(:ci_build, :manual, pipeline: pipeline,
2018-12-05 23:21:45 +05:30
name: 'deploy to production', environment: environment.name)
2017-08-17 22:00:37 +05:30
end
context 'when user has ability to trigger deployment' do
2018-11-08 19:23:39 +05:30
let(:permissions) do
2017-08-17 22:00:37 +05:30
create(:protected_branch, :developers_can_merge,
name: action.ref, project: project)
end
it 'does show a play button' do
2019-07-07 11:18:12 +05:30
expect(page).to have_link(action.name)
2017-08-17 22:00:37 +05:30
end
2018-12-05 23:21:45 +05:30
it 'does allow to play manual action', :js do
2017-08-17 22:00:37 +05:30
expect(action).to be_manual
2018-12-05 23:21:45 +05:30
find('button.dropdown').click
2019-07-07 11:18:12 +05:30
expect { click_link(action.name) }
2017-08-17 22:00:37 +05:30
.not_to change { Ci::Pipeline.count }
2018-12-05 23:21:45 +05:30
wait_for_all_requests
2017-08-17 22:00:37 +05:30
expect(page).to have_content(action.name)
expect(action.reload).to be_pending
end
end
context 'when user has no ability to trigger a deployment' do
2020-03-13 15:44:24 +05:30
let(:permissions) do
create(:protected_branch, :no_one_can_merge,
name: action.ref, project: project)
end
2017-08-17 22:00:37 +05:30
it 'does not show a play button' do
2019-07-07 11:18:12 +05:30
expect(page).not_to have_link(action.name)
2017-08-17 22:00:37 +05:30
end
end
context 'with external_url' do
2018-11-08 19:23:39 +05:30
let(:environment) { create(:environment, project: project, external_url: 'https://git.gitlab.com') }
let(:build) { create(:ci_build, pipeline: pipeline) }
2018-12-13 13:39:08 +05:30
let(:deployment) { create(:deployment, :success, environment: environment, deployable: build) }
2017-08-17 22:00:37 +05:30
2021-10-27 15:23:28 +05:30
it 'does show an external link button', :js do
2017-08-17 22:00:37 +05:30
expect(page).to have_link(nil, href: environment.external_url)
end
end
context 'with terminal' do
2019-09-04 21:01:54 +05:30
context 'when user configured kubernetes from CI/CD > Clusters' do
2020-03-13 15:44:24 +05:30
let!(:cluster) do
create(:cluster, :project, :provided_by_gcp, projects: [project])
end
2019-09-04 21:01:54 +05:30
2018-11-18 11:00:15 +05:30
context 'for project maintainer' do
let(:role) { :maintainer }
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
context 'web terminal', :js do
before do
2019-02-15 15:39:39 +05:30
# Stub #terminals as it causes js-enabled feature specs to
# render the page incorrectly
#
# In EE we have to stub EE::Environment since it overwrites
# the "terminals" method.
2019-12-26 22:10:19 +05:30
allow_next_instance_of(Gitlab.ee? ? EE::Environment : Environment) do |instance|
allow(instance).to receive(:terminals) { nil }
end
2019-02-15 15:39:39 +05:30
2018-03-17 18:26:18 +05:30
visit terminal_project_environment_path(project, environment)
end
it 'displays a web terminal' do
expect(page).to have_selector('#terminal')
expect(page).to have_link(nil, href: environment.external_url)
end
end
2017-08-17 22:00:37 +05:30
end
end
end
context 'when environment is available' do
context 'with stop action' do
2022-07-23 23:45:48 +05:30
let(:build) { create(:ci_build, :success, pipeline: pipeline, environment: environment.name) }
2018-11-08 19:23:39 +05:30
let(:action) do
2017-08-17 22:00:37 +05:30
create(:ci_build, :manual, pipeline: pipeline,
2022-07-23 23:45:48 +05:30
name: 'close_app',
environment: environment.name)
2017-08-17 22:00:37 +05:30
end
2018-11-08 19:23:39 +05:30
let(:deployment) do
2018-12-13 13:39:08 +05:30
create(:deployment, :success,
environment: environment,
2017-08-17 22:00:37 +05:30
deployable: build,
on_stop: 'close_app')
end
context 'when user has ability to stop environment' do
2018-11-08 19:23:39 +05:30
let(:permissions) do
2017-08-17 22:00:37 +05:30
create(:protected_branch, :developers_can_merge,
name: action.ref, project: project)
end
2018-12-05 23:21:45 +05:30
it 'allows to stop environment', :js do
2018-11-18 11:00:15 +05:30
click_button('Stop')
click_button('Stop environment') # confirm modal
2018-12-05 23:21:45 +05:30
wait_for_all_requests
2017-08-17 22:00:37 +05:30
end
end
context 'when user has no ability to stop environment' do
2020-03-13 15:44:24 +05:30
let(:permissions) do
create(:protected_branch, :no_one_can_merge,
name: action.ref, project: project)
end
2021-10-27 15:23:28 +05:30
it 'does not allow to stop environment', :js do
2018-11-18 11:00:15 +05:30
expect(page).not_to have_button('Stop')
2017-08-17 22:00:37 +05:30
end
end
context 'for reporter' do
let(:role) { :reporter }
2021-10-27 15:23:28 +05:30
it 'does not show stop button', :js do
2018-11-18 11:00:15 +05:30
expect(page).not_to have_button('Stop')
2017-08-17 22:00:37 +05:30
end
end
end
end
context 'when environment is stopped' do
2018-11-08 19:23:39 +05:30
let(:environment) { create(:environment, project: project, state: :stopped) }
2017-08-17 22:00:37 +05:30
2021-10-27 15:23:28 +05:30
it 'does not show stop button', :js do
2018-11-18 11:00:15 +05:30
expect(page).not_to have_button('Stop')
2017-08-17 22:00:37 +05:30
end
end
end
end
end
end
2018-11-08 19:23:39 +05:30
describe 'environment folders', :js do
2017-08-17 22:00:37 +05:30
context 'when folder name contains special charaters' do
before do
create(:environment, project: project,
name: 'staging-1.0/review',
state: :available)
end
it 'renders a correct environment folder' do
2018-03-17 18:26:18 +05:30
reqs = inspect_requests do
visit folder_project_environments_path(project, id: 'staging-1.0')
end
expect(reqs.first.status_code).to eq(200)
2017-08-17 22:00:37 +05:30
expect(page).to have_content('Environments / staging-1.0')
end
end
end
2018-11-08 19:23:39 +05:30
describe 'auto-close environment when branch is deleted' do
let(:project) { create(:project, :repository) }
2017-08-17 22:00:37 +05:30
2018-11-08 19:23:39 +05:30
let!(:environment) do
2017-08-17 22:00:37 +05:30
create(:environment, :with_review_app, project: project,
ref: 'feature')
end
2021-10-27 15:23:28 +05:30
it 'user visits environment page', :js do
2017-08-17 22:00:37 +05:30
visit_environment(environment)
2018-11-18 11:00:15 +05:30
expect(page).to have_button('Stop')
2017-08-17 22:00:37 +05:30
end
2021-09-04 01:27:46 +05:30
it 'user deletes the branch with running environment', :js do
2018-03-27 19:54:05 +05:30
visit project_branches_filtered_path(project, state: 'all', search: 'feature')
2017-08-17 22:00:37 +05:30
remove_branch_with_hooks(project, user, 'feature') do
2021-09-04 01:27:46 +05:30
page.within('.js-branch-feature') { find('.js-delete-branch-button').click }
2017-08-17 22:00:37 +05:30
end
visit_environment(environment)
end
##
# This is a workaround for problem described in #24543
#
def remove_branch_with_hooks(project, user, branch)
params = {
2019-12-21 20:55:43 +05:30
change: {
oldrev: project.commit(branch).id,
newrev: Gitlab::Git::BLANK_SHA,
ref: "refs/heads/#{branch}"
}
2017-08-17 22:00:37 +05:30
}
yield
2019-07-07 11:18:12 +05:30
Git::BranchPushService.new(project, user, params).execute
2017-08-17 22:00:37 +05:30
end
end
def visit_environment(environment)
2017-09-10 17:25:29 +05:30
visit project_environment_path(environment.project, environment)
2017-08-17 22:00:37 +05:30
end
end