environments_spec.rb 11.0 KB
Newer Older
1 2
require 'spec_helper'

3
feature 'Environments page', :js do
4
  given(:project) { create(:project) }
5 6
  given(:user) { create(:user) }
  given(:role) { :developer }
7

8
  background do
9
    project.add_role(user, role)
10
    sign_in(user)
11 12
  end

13
  describe 'page tabs' do
14 15 16
    it 'shows "Available" and "Stopped" tab with links' do
      visit_environments(project)

F
Filipa Lacerda 已提交
17 18 19 20
      expect(page).to have_selector('.js-environments-tab-available')
      expect(page).to have_content('Available')
      expect(page).to have_selector('.js-environments-tab-stopped')
      expect(page).to have_content('Stopped')
21
    end
F
Filipa Lacerda 已提交
22 23

    describe 'with one available environment' do
24 25 26
      before do
        create(:environment, project: project, state: :available)
      end
F
Filipa Lacerda 已提交
27 28 29

      describe 'in available tab page' do
        it 'should show one environment' do
30 31
          visit_environments(project, scope: 'available')

32
          expect(page).to have_css('.environments-container')
33
          expect(page.all('.environment-name').length).to eq(1)
F
Filipa Lacerda 已提交
34 35 36 37 38
        end
      end

      describe 'in stopped tab page' do
        it 'should show no environments' do
39 40
          visit_environments(project, scope: 'stopped')

41
          expect(page).to have_css('.environments-container')
F
Filipa Lacerda 已提交
42 43 44
          expect(page).to have_content('You don\'t have any environments right now')
        end
      end
45 46 47 48 49 50

      context 'when cluster is not reachable' do
        let!(:cluster) { create(:cluster, :provided_by_gcp, projects: [project]) }
        let!(:application_prometheus) { create(:clusters_applications_prometheus, :installed, cluster: cluster) }

        before do
51
          allow_any_instance_of(Kubeclient::Client).to receive(:proxy_url).and_raise(Kubeclient::HttpError.new(401, 'Unauthorized', nil))
52 53 54 55 56 57 58 59 60
        end

        it 'should show one environment without error' do
          visit_environments(project, scope: 'available')

          expect(page).to have_css('.environments-container')
          expect(page.all('.environment-name').length).to eq(1)
        end
      end
F
Filipa Lacerda 已提交
61 62 63
    end

    describe 'with one stopped environment' do
64 65 66
      before do
        create(:environment, project: project, state: :stopped)
      end
F
Filipa Lacerda 已提交
67 68 69

      describe 'in available tab page' do
        it 'should show no environments' do
70 71
          visit_environments(project, scope: 'available')

72
          expect(page).to have_css('.environments-container')
F
Filipa Lacerda 已提交
73 74 75 76 77 78
          expect(page).to have_content('You don\'t have any environments right now')
        end
      end

      describe 'in stopped tab page' do
        it 'should show one environment' do
79 80
          visit_environments(project, scope: 'stopped')

81
          expect(page).to have_css('.environments-container')
82
          expect(page.all('.environment-name').length).to eq(1)
F
Filipa Lacerda 已提交
83 84 85
        end
      end
    end
86
  end
87

88
  context 'without environments' do
89 90
    before do
      visit_environments(project)
91 92
    end

93 94 95
    it 'does not show environments and counters are set to zero' do
      expect(page).to have_content('You don\'t have any environments right now.')

F
Filipa Lacerda 已提交
96 97
      expect(page.find('.js-environments-tab-available .badge').text).to eq('0')
      expect(page.find('.js-environments-tab-stopped .badge').text).to eq('0')
98 99 100
    end
  end

101 102 103
  describe 'environments table' do
    given!(:environment) do
      create(:environment, project: project, state: :available)
104
    end
105

106 107 108
    context 'when there are no deployments' do
      before do
        visit_environments(project)
109
      end
K
Kamil Trzcinski 已提交
110

111 112
      it 'shows environments names and counters' do
        expect(page).to have_link(environment.name)
K
Kamil Trzcinski 已提交
113

F
Filipa Lacerda 已提交
114 115
        expect(page.find('.js-environments-tab-available .badge').text).to eq('1')
        expect(page.find('.js-environments-tab-stopped .badge').text).to eq('0')
K
Kamil Trzcinski 已提交
116 117
      end

118 119 120
      it 'does not show deployments' do
        expect(page).to have_content('No deployments yet')
      end
K
Kamil Trzcinski 已提交
121

122 123
      it 'does not show stip button when environment is not stoppable' do
        expect(page).not_to have_selector('.stop-env-link')
K
Kamil Trzcinski 已提交
124
      end
125 126
    end

127
    context 'when there are deployments' do
128
      given(:project) { create(:project, :repository) }
129

130
      given!(:deployment) do
131 132
        create(:deployment, environment: environment,
                            sha: project.commit.id)
133
      end
134

135 136
      it 'shows deployment SHA and internal ID' do
        visit_environments(project)
137

138
        expect(page).to have_link(deployment.short_sha)
139
        expect(page).to have_content(deployment.iid)
140 141
      end

142 143 144
      context 'when builds and manual actions are present' do
        given!(:pipeline) { create(:ci_pipeline, project: project) }
        given!(:build) { create(:ci_build, pipeline: pipeline) }
145

146
        given!(:action) do
147
          create(:ci_build, :manual, pipeline: pipeline, name: 'deploy to production')
148 149
        end

150
        given!(:deployment) do
151
          create(:deployment, environment: environment,
152
                              deployable: build,
153
                              sha: project.commit.id)
154
        end
155

156 157 158 159 160
        before do
          visit_environments(project)
        end

        it 'shows a play button' do
F
Filipa Lacerda 已提交
161
          find('.js-dropdown-play-icon-container').click
162

163
          expect(page).to have_content(action.name.humanize)
K
Kamil Trzcinski 已提交
164 165
        end

166
        it 'allows to play a manual action', :js do
167
          expect(action).to be_manual
168

F
Filipa Lacerda 已提交
169
          find('.js-dropdown-play-icon-container').click
170
          expect(page).to have_content(action.name.humanize)
171

172
          expect { find('.js-manual-action-link').click }
173 174
            .not_to change { Ci::Pipeline.count }
        end
175

176
        it 'shows build name and id' do
177 178
          expect(page).to have_link("#{build.name} ##{build.id}")
        end
179

180
        it 'shows a stop button' do
181 182
          expect(page).not_to have_selector('.stop-env-link')
        end
183

184
        it 'does not show external link button' do
185 186
          expect(page).not_to have_css('external-url')
        end
187

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

192 193 194 195
        context 'with external_url' do
          given(:environment) { create(:environment, project: project, external_url: 'https://git.gitlab.com') }
          given(:build) { create(:ci_build, pipeline: pipeline) }
          given(:deployment) { create(:deployment, environment: environment, deployable: build) }
196

197
          it 'shows an external link button' do
198
            expect(page).to have_link(nil, href: environment.external_url)
199
          end
200
        end
201

202
        context 'with stop action' do
203 204 205 206 207 208 209 210 211
          given(:action) do
            create(:ci_build, :manual, pipeline: pipeline, name: 'close_app')
          end

          given(:deployment) do
            create(:deployment, environment: environment,
                                deployable: build,
                                on_stop: 'close_app')
          end
212

213
          it 'shows a stop button' do
214
            expect(page).to have_selector('.stop-env-link')
215
          end
K
Kamil Trzcinski 已提交
216

217
          context 'when user is a reporter' do
218
            let(:role) { :reporter }
K
Kamil Trzcinski 已提交
219

220
            it 'does not show stop button' do
221
              expect(page).not_to have_selector('.stop-env-link')
K
Kamil Trzcinski 已提交
222
            end
F
Filipa Lacerda 已提交
223
          end
224
        end
K
Kamil Trzcinski 已提交
225

226
        context 'when kubernetes terminal is available' do
227
          shared_examples 'same behavior between KubernetesService and Platform::Kubernetes' do
228 229
            context 'for project master' do
              let(:role) { :master }
230

231 232 233 234 235 236 237
              it 'shows the terminal button' do
                expect(page).to have_terminal_button
              end
            end

            context 'when user is a developer' do
              let(:role) { :developer }
K
Kamil Trzcinski 已提交
238

239 240 241
              it 'does not show terminal button' do
                expect(page).not_to have_terminal_button
              end
K
Kamil Trzcinski 已提交
242
            end
243
          end
S
Shinya Maeda 已提交
244

245 246
          context 'when user configured kubernetes from Integration > Kubernetes' do
            let(:project) { create(:kubernetes_project, :test_repo) }
K
Kamil Trzcinski 已提交
247

248
            it_behaves_like 'same behavior between KubernetesService and Platform::Kubernetes'
249
          end
K
Kamil Trzcinski 已提交
250

251
          context 'when user configured kubernetes from CI/CD > Clusters' do
S
Shinya Maeda 已提交
252
            let(:cluster) { create(:cluster, :provided_by_gcp, projects: [create(:project, :repository)]) }
253 254
            let(:project) { cluster.project }

255
            it_behaves_like 'same behavior between KubernetesService and Platform::Kubernetes'
F
Filipa Lacerda 已提交
256
          end
257
        end
258 259 260 261
      end
    end
  end

262 263 264
  it 'does have a new environment button' do
    visit_environments(project)

265
    expect(page).to have_link('New environment')
266
  end
267

268
  describe 'creating a new environment' do
269
    before do
270
      visit_environments(project)
271
    end
272

273 274
    context 'user is a developer' do
      given(:role) { :developer }
275

276 277 278 279
      scenario 'developer creates a new environment with a valid name' do
        within(".top-area") { click_link 'New environment' }
        fill_in('Name', with: 'production')
        click_on 'Save'
280

281
        expect(page).to have_content('production')
282 283
      end

284 285 286 287
      scenario 'developer creates a new environmetn with invalid name' do
        within(".top-area") { click_link 'New environment' }
        fill_in('Name', with: 'name,with,commas')
        click_on 'Save'
288

289
        expect(page).to have_content('Name can contain only letters')
290 291 292
      end
    end

293
    context 'user is a reporter' do
294
      given(:role) { :reporter }
295

296
      scenario 'reporters tries to create a new environment' do
297 298 299 300
        expect(page).not_to have_link('New environment')
      end
    end
  end
301

302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
  describe 'environments folders' do
    before do
      create(:environment, project: project,
                           name: 'staging/review-1',
                           state: :available)
      create(:environment, project: project,
                           name: 'staging/review-2',
                           state: :available)
    end

    scenario 'users unfurls an environment folder' do
      visit_environments(project)

      expect(page).not_to have_content 'review-1'
      expect(page).not_to have_content 'review-2'
      expect(page).to have_content 'staging 2'

      within('.folder-row') do
        find('.folder-name', text: 'staging').click
      end

      expect(page).to have_content 'review-1'
      expect(page).to have_content 'review-2'
    end
  end

F
Filipa Lacerda 已提交
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
  describe 'environments folders view' do
    before do
      create(:environment, project: project,
                           name: 'staging.review/review-1',
                           state: :available)
      create(:environment, project: project,
                           name: 'staging.review/review-2',
                           state: :available)
    end

    scenario 'user opens folder view' do
      visit folder_project_environments_path(project, 'staging.review')
      wait_for_requests

      expect(page).to have_content('Environments / staging.review')
      expect(page).to have_content('review-1')
      expect(page).to have_content('review-2')
    end
  end

348
  def have_terminal_button
349
    have_link(nil, href: terminal_project_environment_path(project, environment))
350 351
  end

352 353
  def visit_environments(project, **opts)
    visit project_environments_path(project, **opts)
F
Filipa Lacerda 已提交
354
    wait_for_requests
355
  end
356
end