environments_spec.rb 10.3 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 45 46 47
          expect(page).to have_content('You don\'t have any environments right now')
        end
      end
    end

    describe 'with one stopped environment' do
48 49 50
      before do
        create(:environment, project: project, state: :stopped)
      end
F
Filipa Lacerda 已提交
51 52 53

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

56
          expect(page).to have_css('.environments-container')
F
Filipa Lacerda 已提交
57 58 59 60 61 62
          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
63 64
          visit_environments(project, scope: 'stopped')

65
          expect(page).to have_css('.environments-container')
66
          expect(page.all('.environment-name').length).to eq(1)
F
Filipa Lacerda 已提交
67 68 69
        end
      end
    end
70
  end
71

72
  context 'without environments' do
73 74
    before do
      visit_environments(project)
75 76
    end

77 78 79
    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 已提交
80 81
      expect(page.find('.js-environments-tab-available .badge').text).to eq('0')
      expect(page.find('.js-environments-tab-stopped .badge').text).to eq('0')
82 83 84
    end
  end

85 86 87
  describe 'environments table' do
    given!(:environment) do
      create(:environment, project: project, state: :available)
88
    end
89

90 91 92
    context 'when there are no deployments' do
      before do
        visit_environments(project)
93
      end
K
Kamil Trzcinski 已提交
94

95 96
      it 'shows environments names and counters' do
        expect(page).to have_link(environment.name)
K
Kamil Trzcinski 已提交
97

F
Filipa Lacerda 已提交
98 99
        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 已提交
100 101
      end

102 103 104
      it 'does not show deployments' do
        expect(page).to have_content('No deployments yet')
      end
K
Kamil Trzcinski 已提交
105

106 107
      it 'does not show stip button when environment is not stoppable' do
        expect(page).not_to have_selector('.stop-env-link')
K
Kamil Trzcinski 已提交
108
      end
109 110
    end

111
    context 'when there are deployments' do
112
      given(:project) { create(:project, :repository) }
113

114
      given!(:deployment) do
115 116
        create(:deployment, environment: environment,
                            sha: project.commit.id)
117
      end
118

119 120
      it 'shows deployment SHA and internal ID' do
        visit_environments(project)
121

122
        expect(page).to have_link(deployment.short_sha)
123
        expect(page).to have_content(deployment.iid)
124 125
      end

126 127 128
      context 'when builds and manual actions are present' do
        given!(:pipeline) { create(:ci_pipeline, project: project) }
        given!(:build) { create(:ci_build, pipeline: pipeline) }
129

130
        given!(:action) do
131
          create(:ci_build, :manual, pipeline: pipeline, name: 'deploy to production')
132 133
        end

134
        given!(:deployment) do
135
          create(:deployment, environment: environment,
136
                              deployable: build,
137
                              sha: project.commit.id)
138
        end
139

140 141 142 143 144
        before do
          visit_environments(project)
        end

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

147
          expect(page).to have_content(action.name.humanize)
K
Kamil Trzcinski 已提交
148 149
        end

150
        it 'allows to play a manual action', :js do
151
          expect(action).to be_manual
152

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

156
          expect { find('.js-manual-action-link').click }
157 158
            .not_to change { Ci::Pipeline.count }
        end
159

160
        it 'shows build name and id' do
161 162
          expect(page).to have_link("#{build.name} ##{build.id}")
        end
163

164
        it 'shows a stop button' do
165 166
          expect(page).not_to have_selector('.stop-env-link')
        end
167

168
        it 'does not show external link button' do
169 170
          expect(page).not_to have_css('external-url')
        end
171

172
        it 'does not show terminal button' do
173 174 175
          expect(page).not_to have_terminal_button
        end

176 177 178 179
        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) }
180

181
          it 'shows an external link button' do
182
            expect(page).to have_link(nil, href: environment.external_url)
183
          end
184
        end
185

186
        context 'with stop action' do
187 188 189 190 191 192 193 194 195
          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
196

197
          it 'shows a stop button' do
198
            expect(page).to have_selector('.stop-env-link')
199
          end
K
Kamil Trzcinski 已提交
200

201
          context 'when user is a reporter' do
202
            let(:role) { :reporter }
K
Kamil Trzcinski 已提交
203

204
            it 'does not show stop button' do
205
              expect(page).not_to have_selector('.stop-env-link')
K
Kamil Trzcinski 已提交
206
            end
F
Filipa Lacerda 已提交
207
          end
208
        end
K
Kamil Trzcinski 已提交
209

210
        context 'when kubernetes terminal is available' do
211
          shared_examples 'same behavior between KubernetesService and Platform::Kubernetes' do
212 213
            context 'for project master' do
              let(:role) { :master }
214

215 216 217 218 219 220 221
              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 已提交
222

223 224 225
              it 'does not show terminal button' do
                expect(page).not_to have_terminal_button
              end
K
Kamil Trzcinski 已提交
226
            end
227
          end
S
Shinya Maeda 已提交
228

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

232
            it_behaves_like 'same behavior between KubernetesService and Platform::Kubernetes'
233
          end
K
Kamil Trzcinski 已提交
234

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

239
            it_behaves_like 'same behavior between KubernetesService and Platform::Kubernetes'
F
Filipa Lacerda 已提交
240
          end
241
        end
242 243 244 245
      end
    end
  end

246 247 248
  it 'does have a new environment button' do
    visit_environments(project)

249
    expect(page).to have_link('New environment')
250
  end
251

252
  describe 'creating a new environment' do
253
    before do
254
      visit_environments(project)
255
    end
256

257 258
    context 'user is a developer' do
      given(:role) { :developer }
259

260 261 262 263
      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'
264

265
        expect(page).to have_content('production')
266 267
      end

268 269 270 271
      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'
272

273
        expect(page).to have_content('Name can contain only letters')
274 275 276
      end
    end

277
    context 'user is a reporter' do
278
      given(:role) { :reporter }
279

280
      scenario 'reporters tries to create a new environment' do
281 282 283 284
        expect(page).not_to have_link('New environment')
      end
    end
  end
285

286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
  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 已提交
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
  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

332
  def have_terminal_button
333
    have_link(nil, href: terminal_project_environment_path(project, environment))
334 335
  end

336 337
  def visit_environments(project, **opts)
    visit project_environments_path(project, **opts)
F
Filipa Lacerda 已提交
338
    wait_for_requests
339
  end
340
end