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) }
|
|
|
|
let!(:permissions) { }
|
|
|
|
let!(:deployment) { }
|
|
|
|
let!(:action) { }
|
2020-03-13 15:44:24 +05:30
|
|
|
let!(:cluster) { }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
before do
|
|
|
|
visit_environment(environment)
|
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
it 'shows environment name' do
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(page).to have_content(environment.name)
|
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
context 'without auto-stop' do
|
|
|
|
it 'does not show auto-stop text' do
|
|
|
|
expect(page).not_to have_content('Auto stops')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not show auto-stop button' do
|
|
|
|
expect(page).not_to have_selector(auto_stop_button_selector)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with auto-stop' do
|
|
|
|
let!(:environment) { create(:environment, :will_auto_stop, name: 'staging', project: project) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
visit_environment(environment)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'shows auto stop info' do
|
|
|
|
expect(page).to have_content('Auto stops')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'shows auto stop button' do
|
|
|
|
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
|
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
|
|
|
|
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')
|
|
|
|
expect(page).not_to have_terminal_button
|
|
|
|
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
|
|
|
|
|
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) }
|
|
|
|
let(:build) { create(:ci_build, pipeline: pipeline) }
|
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
|
|
|
|
|
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
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
it 'does show an external link button' 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
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
it 'shows the terminal button' do
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(page).to have_terminal_button
|
|
|
|
end
|
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
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
context 'for developer' do
|
|
|
|
let(:role) { :developer }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
it 'does not show terminal button' do
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(page).not_to have_terminal_button
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when environment is available' do
|
|
|
|
context 'with stop 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,
|
|
|
|
name: 'close_app')
|
|
|
|
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
|
|
|
expect(page).to have_content('close_app')
|
|
|
|
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
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
it 'does not allow to stop environment' 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 }
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
it 'does not show stop button' 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
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
it 'does not show stop button' 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
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
it 'user visits environment page' 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)
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
expect(page).not_to have_button('Stop')
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
context 'when the feature flag :delete_branch_confirmation_modals is disabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(delete_branch_confirmation_modals: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'user deletes the branch with running environment' do
|
|
|
|
visit project_branches_filtered_path(project, state: 'all', search: 'feature')
|
|
|
|
|
|
|
|
remove_branch_with_hooks(project, user, 'feature') do
|
|
|
|
within('.js-branch-feature') { click_link(title: 'Delete branch') }
|
|
|
|
end
|
|
|
|
|
|
|
|
visit_environment(environment)
|
|
|
|
|
|
|
|
expect(page).not_to have_button('Stop')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
##
|
|
|
|
# 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
|
|
|
|
|
|
|
|
def have_terminal_button
|
2017-09-10 17:25:29 +05:30
|
|
|
have_link(nil, href: terminal_project_environment_path(project, environment))
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|