environment_spec.rb 10.8 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
S
Shinya Maeda 已提交
28
      it 'does not show 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
      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

S
Shinya Maeda 已提交
67
        it 'does not show deployments' do
S
Shinya Maeda 已提交
68 69 70 71 72 73 74 75 76 77 78 79
          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

S
Shinya Maeda 已提交
80
        it 'does not show deployments' do
S
Shinya Maeda 已提交
81 82 83 84
          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
                context 'web terminal', :js do
                  before do
168 169 170 171 172 173 174 175
                    # 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.
                    allow_any_instance_of(defined?(EE) ? EE::Environment : Environment)
                      .to receive(:terminals) { nil }

176 177 178 179 180 181 182 183
                    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
184
              end
185

186 187
              context 'for developer' do
                let(:role) { :developer }
188

189
                it 'does not show terminal button' do
190
                  expect(page).not_to have_terminal_button
191 192
                end
              end
193 194
            end

195 196
            context 'when user configured kubernetes from Integration > Kubernetes' do
              let(:project) { create(:kubernetes_project, :test_repo) }
197

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

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

205
              it_behaves_like 'same behavior between KubernetesService and Platform::Kubernetes'
206 207 208
            end
          end

K
Kamil Trzcinski 已提交
209 210
          context 'when environment is available' do
            context 'with stop action' do
211
              let(:action) do
212 213 214 215
                create(:ci_build, :manual, pipeline: pipeline,
                                           name: 'close_app')
              end

216
              let(:deployment) do
S
Shinya Maeda 已提交
217 218
                create(:deployment, :success,
                                    environment: environment,
219 220 221
                                    deployable: build,
                                    on_stop: 'close_app')
              end
F
Filipa Lacerda 已提交
222

223
              context 'when user has ability to stop environment' do
224
                let(:permissions) do
225 226 227
                  create(:protected_branch, :developers_can_merge,
                         name: action.ref, project: project)
                end
228

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

K
Kamil Trzcinski 已提交
243 244
              context 'for reporter' do
                let(:role) { :reporter }
F
Filipa Lacerda 已提交
245

246
                it 'does not show stop button' do
247
                  expect(page).not_to have_button('Stop')
K
Kamil Trzcinski 已提交
248
                end
F
Filipa Lacerda 已提交
249 250
              end
            end
K
Kamil Trzcinski 已提交
251 252 253
          end

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

256
            it 'does not show stop button' do
257
              expect(page).not_to have_button('Stop')
K
Kamil Trzcinski 已提交
258
            end
F
Filipa Lacerda 已提交
259 260 261 262 263
          end
        end
      end
    end
  end
264

265
  describe 'environment folders', :js do
266 267 268 269 270 271 272 273
    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 已提交
274 275 276 277 278
        reqs = inspect_requests do
          visit folder_project_environments_path(project, id: 'staging-1.0')
        end

        expect(reqs.first.status_code).to eq(200)
279 280 281 282 283
        expect(page).to have_content('Environments / staging-1.0')
      end
    end
  end

284 285
  describe 'auto-close environment when branch is deleted' do
    let(:project) { create(:project, :repository) }
286

287
    let!(:environment) do
288 289 290 291
      create(:environment, :with_review_app, project: project,
                                             ref: 'feature')
    end

292
    it 'user visits environment page' do
293 294
      visit_environment(environment)

295
      expect(page).to have_button('Stop')
296 297
    end

298
    it 'user deletes the branch with running environment' do
299
      visit project_branches_filtered_path(project, state: 'all', search: 'feature')
300 301 302 303 304 305 306

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

      visit_environment(environment)

307
      expect(page).not_to have_button('Stop')
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
    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)
327
    visit project_environment_path(environment.project, environment)
328
  end
329 330

  def have_terminal_button
331
    have_link(nil, href: terminal_project_environment_path(project, environment))
332
  end
F
Filipa Lacerda 已提交
333
end