environment_spec.rb 10.6 KB
Newer Older
F
Filipa Lacerda 已提交
1 2
require 'spec_helper'

3 4 5 6
describe 'Environment' do
  let(:project) { create(:project) }
  let(:user) { create(:user) }
  let(:role) { :developer }
F
Filipa Lacerda 已提交
7

8
  before do
9
    sign_in(user)
10
    project.add_role(user, role)
F
Filipa Lacerda 已提交
11 12
  end

13 14 15 16 17
  describe 'environment details page' do
    let!(:environment) { create(:environment, project: project) }
    let!(:permissions) { }
    let!(:deployment) { }
    let!(:action) { }
F
Filipa Lacerda 已提交
18 19

    before do
20
      visit_environment(environment)
F
Filipa Lacerda 已提交
21 22
    end

23
    it 'shows environment name' do
24 25 26
      expect(page).to have_content(environment.name)
    end

F
Filipa Lacerda 已提交
27
    context 'without deployments' do
28
      it 'does show no deployments' do
F
Filipa Lacerda 已提交
29 30 31 32 33
        expect(page).to have_content('You don\'t have any deployments right now.')
      end
    end

    context 'with deployments' do
34
      context 'when there is no related deployable' do
35
        let(:deployment) do
S
Shinya Maeda 已提交
36
          create(:deployment, :success, environment: environment, deployable: nil)
37
        end
F
Filipa Lacerda 已提交
38

39
        it 'does show deployment SHA' do
40 41
          expect(page).to have_link(deployment.short_sha)
          expect(page).not_to have_link('Re-deploy')
42 43
          expect(page).not_to have_terminal_button
        end
F
Filipa Lacerda 已提交
44 45
      end

S
Shinya Maeda 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
      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

        it 'does show no deployments' do
          expect(page).to have_content('You don\'t have any deployments right now.')
        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

        it 'does show no deployments' do
          expect(page).to have_content('You don\'t have any deployments right now.')
        end
      end

85
      context 'with related deployable present' do
86 87
        let(:pipeline) { create(:ci_pipeline, project: project) }
        let(:build) { create(:ci_build, pipeline: pipeline) }
88

89
        let(:deployment) do
S
Shinya Maeda 已提交
90
          create(:deployment, :success, environment: environment, deployable: build)
91
        end
F
Filipa Lacerda 已提交
92

93
        it 'does show build name' do
F
Filipa Lacerda 已提交
94
          expect(page).to have_link("#{build.name} (##{build.id})")
95
          expect(page).not_to have_link('Re-deploy')
96 97 98
          expect(page).not_to have_terminal_button
        end

99 100 101 102 103 104 105 106 107 108 109
        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

F
Filipa Lacerda 已提交
110
        context 'with manual action' do
111
          let(:action) do
112
            create(:ci_build, :manual, pipeline: pipeline,
113
                                       name: 'deploy to production', environment: environment.name)
114
          end
F
Filipa Lacerda 已提交
115

116
          context 'when user has ability to trigger deployment' do
117
            let(:permissions) do
118 119 120
              create(:protected_branch, :developers_can_merge,
                     name: action.ref, project: project)
            end
121

122 123 124 125
            it 'does show a play button' do
              expect(page).to have_link(action.name.humanize)
            end

126
            it 'does allow to play manual action', :js do
127
              expect(action).to be_manual
F
Filipa Lacerda 已提交
128

129 130
              find('button.dropdown').click

131 132
              expect { click_link(action.name.humanize) }
                .not_to change { Ci::Pipeline.count }
133

134 135
              wait_for_all_requests

136 137 138 139
              expect(page).to have_content(action.name)
              expect(action.reload).to be_pending
            end
          end
140

141 142 143 144
          context 'when user has no ability to trigger a deployment' do
            it 'does not show a play button' do
              expect(page).not_to have_link(action.name.humanize)
            end
F
Filipa Lacerda 已提交
145 146 147
          end

          context 'with external_url' do
148 149
            let(:environment) { create(:environment, project: project, external_url: 'https://git.gitlab.com') }
            let(:build) { create(:ci_build, pipeline: pipeline) }
S
Shinya Maeda 已提交
150
            let(:deployment) { create(:deployment, :success, environment: environment, deployable: build) }
F
Filipa Lacerda 已提交
151

152
            it 'does show an external link button' do
F
Filipa Lacerda 已提交
153 154 155 156
              expect(page).to have_link(nil, href: environment.external_url)
            end
          end

157
          context 'with terminal' do
158
            shared_examples 'same behavior between KubernetesService and Platform::Kubernetes' do
159 160
              context 'for project maintainer' do
                let(:role) { :maintainer }
161

162
                it 'it shows the terminal button' do
163 164
                  expect(page).to have_terminal_button
                end
165

166 167 168 169 170 171 172 173 174 175 176 177
                context 'web terminal', :js do
                  before do
                    # Stub #terminals as it causes js-enabled feature specs to render the page incorrectly
                    allow_any_instance_of(Environment).to receive(:terminals) { nil }
                    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
178
              end
179

180 181
              context 'for developer' do
                let(:role) { :developer }
182

183
                it 'does not show terminal button' do
184
                  expect(page).not_to have_terminal_button
185 186
                end
              end
187 188
            end

189 190
            context 'when user configured kubernetes from Integration > Kubernetes' do
              let(:project) { create(:kubernetes_project, :test_repo) }
191

192
              it_behaves_like 'same behavior between KubernetesService and Platform::Kubernetes'
193 194 195 196 197 198
            end

            context 'when user configured kubernetes from CI/CD > Clusters' do
              let!(:cluster) { create(:cluster, :project, :provided_by_gcp) }
              let(:project) { cluster.project }

199
              it_behaves_like 'same behavior between KubernetesService and Platform::Kubernetes'
200 201 202
            end
          end

K
Kamil Trzcinski 已提交
203 204
          context 'when environment is available' do
            context 'with stop action' do
205
              let(:action) do
206 207 208 209
                create(:ci_build, :manual, pipeline: pipeline,
                                           name: 'close_app')
              end

210
              let(:deployment) do
S
Shinya Maeda 已提交
211 212
                create(:deployment, :success,
                                    environment: environment,
213 214 215
                                    deployable: build,
                                    on_stop: 'close_app')
              end
F
Filipa Lacerda 已提交
216

217
              context 'when user has ability to stop environment' do
218
                let(:permissions) do
219 220 221
                  create(:protected_branch, :developers_can_merge,
                         name: action.ref, project: project)
                end
222

223
                it 'allows to stop environment', :js do
224 225
                  click_button('Stop')
                  click_button('Stop environment') # confirm modal
226
                  wait_for_all_requests
227 228 229 230 231 232
                  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
233
                  expect(page).not_to have_button('Stop')
234
                end
K
Kamil Trzcinski 已提交
235
              end
F
Filipa Lacerda 已提交
236

K
Kamil Trzcinski 已提交
237 238
              context 'for reporter' do
                let(:role) { :reporter }
F
Filipa Lacerda 已提交
239

240
                it 'does not show stop button' do
241
                  expect(page).not_to have_button('Stop')
K
Kamil Trzcinski 已提交
242
                end
F
Filipa Lacerda 已提交
243 244
              end
            end
K
Kamil Trzcinski 已提交
245 246 247
          end

          context 'when environment is stopped' do
248
            let(:environment) { create(:environment, project: project, state: :stopped) }
K
Kamil Trzcinski 已提交
249

250
            it 'does not show stop button' do
251
              expect(page).not_to have_button('Stop')
K
Kamil Trzcinski 已提交
252
            end
F
Filipa Lacerda 已提交
253 254 255 256 257
          end
        end
      end
    end
  end
258

259
  describe 'environment folders', :js do
260 261 262 263 264 265 266 267
    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
M
Mike Greiling 已提交
268 269 270 271 272
        reqs = inspect_requests do
          visit folder_project_environments_path(project, id: 'staging-1.0')
        end

        expect(reqs.first.status_code).to eq(200)
273 274 275 276 277
        expect(page).to have_content('Environments / staging-1.0')
      end
    end
  end

278 279
  describe 'auto-close environment when branch is deleted' do
    let(:project) { create(:project, :repository) }
280

281
    let!(:environment) do
282 283 284 285
      create(:environment, :with_review_app, project: project,
                                             ref: 'feature')
    end

286
    it 'user visits environment page' do
287 288
      visit_environment(environment)

289
      expect(page).to have_button('Stop')
290 291
    end

292
    it 'user deletes the branch with running environment' do
293
      visit project_branches_filtered_path(project, state: 'all', search: 'feature')
294 295 296 297 298 299 300

      remove_branch_with_hooks(project, user, 'feature') do
        page.within('.js-branch-feature') { find('a.btn-remove').click }
      end

      visit_environment(environment)

301
      expect(page).not_to have_button('Stop')
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
    end

    ##
    # This is a workaround for problem described in #24543
    #
    def remove_branch_with_hooks(project, user, branch)
      params = {
        oldrev: project.commit(branch).id,
        newrev: Gitlab::Git::BLANK_SHA,
        ref: "refs/heads/#{branch}"
      }

      yield

      GitPushService.new(project, user, params).execute
    end
  end

  def visit_environment(environment)
321
    visit project_environment_path(environment.project, environment)
322
  end
323 324

  def have_terminal_button
325
    have_link(nil, href: terminal_project_environment_path(project, environment))
326
  end
F
Filipa Lacerda 已提交
327
end