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

329 lines
10 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'
2018-11-08 19:23:39 +05:30
describe 'Environment' do
let(:project) { create(:project) }
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
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) { }
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
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})")
2018-12-13 13:39:08 +05:30
expect(page).not_to have_link('Re-deploy')
2017-08-17 22:00:37 +05:30
expect(page).not_to have_terminal_button
end
2018-12-13 13:39:08 +05:30
context 'when user has ability to re-deploy' do
let(:permissions) do
create(:protected_branch, :developers_can_merge,
name: build.ref, project: project)
end
it 'does show re-deploy' do
expect(page).to have_link('Re-deploy')
end
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
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
let!(:cluster) { create(:cluster, :project, :provided_by_gcp) }
let(:project) { cluster.project }
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
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
2018-11-08 19:23:39 +05:30
it 'user deletes the branch with running environment' 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
page.within('.js-branch-feature') { find('a.btn-remove').click }
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
##
# 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