gcp_spec.rb 7.3 KB
Newer Older
K
Kamil Trzcinski 已提交
1 2
require 'spec_helper'

3
describe 'Gcp Cluster', :js do
K
Kamil Trzcinski 已提交
4 5 6 7 8 9
  include GoogleApi::CloudPlatformHelpers

  let(:project) { create(:project) }
  let(:user) { create(:user) }

  before do
10
    project.add_maintainer(user)
K
Kamil Trzcinski 已提交
11
    gitlab_sign_in(user)
T
Thong Kuah 已提交
12
    allow(Projects::ClustersController).to receive(:STATUS_POLLING_INTERVAL) { 100 }
K
Kamil Trzcinski 已提交
13 14 15
  end

  context 'when user has signed with Google' do
16 17
    let(:project_id) { 'test-project-1234' }

K
Kamil Trzcinski 已提交
18
    before do
T
Thong Kuah 已提交
19
      allow_any_instance_of(Projects::ClustersController)
K
Kamil Trzcinski 已提交
20
        .to receive(:token_in_session).and_return('token')
T
Thong Kuah 已提交
21
      allow_any_instance_of(Projects::ClustersController)
K
Kamil Trzcinski 已提交
22 23 24
        .to receive(:expires_at_in_session).and_return(1.hour.since.to_i.to_s)
    end

25
    context 'when user does not have a cluster and visits cluster index page' do
K
Kamil Trzcinski 已提交
26
      before do
27 28 29
        visit project_clusters_path(project)

        click_link 'Add Kubernetes cluster'
D
Dennis Tang 已提交
30
        click_link 'Create new Cluster on GKE'
K
Kamil Trzcinski 已提交
31 32
      end

33
      context 'when user filled form with valid parameters' do
34 35
        subject { click_button 'Create Kubernetes cluster' }

T
Thong Kuah 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
        shared_examples 'valid cluster gcp form' do
          it 'users sees a form with the GCP token' do
            expect(page).to have_selector(:css, 'form[data-token="token"]')
          end

          it 'user sees a cluster details page and creation status' do
            subject

            expect(page).to have_content('Kubernetes cluster is being created on Google Kubernetes Engine...')

            Clusters::Cluster.last.provider.make_created!

            expect(page).to have_content('Kubernetes cluster was successfully created on Google Kubernetes Engine')
          end

          it 'user sees a error if something wrong during creation' do
            subject

            expect(page).to have_content('Kubernetes cluster is being created on Google Kubernetes Engine...')

            Clusters::Cluster.last.provider.make_errored!('Something wrong!')

            expect(page).to have_content('Something wrong!')
          end
        end

K
Kamil Trzcinski 已提交
62
        before do
63 64 65 66 67 68
          allow_any_instance_of(GoogleApi::CloudPlatform::Client)
            .to receive(:projects_zones_clusters_create) do
            OpenStruct.new(
              self_link: 'projects/gcp-project-12345/zones/us-central1-a/operations/ope-123',
              status: 'RUNNING'
            )
K
Kamil Trzcinski 已提交
69 70
          end

71
          allow(WaitForClusterCreationWorker).to receive(:perform_in).and_return(nil)
K
Kamil Trzcinski 已提交
72

D
Dennis Tang 已提交
73 74 75 76 77 78 79 80 81 82
          execute_script('document.querySelector(".js-gke-cluster-creation-submit").removeAttribute("disabled")')
          sleep 2 # wait for ajax
          execute_script('document.querySelector(".js-gcp-project-id-dropdown input").setAttribute("type", "text")')
          execute_script('document.querySelector(".js-gcp-zone-dropdown input").setAttribute("type", "text")')
          execute_script('document.querySelector(".js-gcp-machine-type-dropdown input").setAttribute("type", "text")')

          fill_in 'cluster[name]', with: 'dev-cluster'
          fill_in 'cluster[provider_gcp_attributes][gcp_project_id]', with: 'gcp-project-123'
          fill_in 'cluster[provider_gcp_attributes][zone]', with: 'us-central1-a'
          fill_in 'cluster[provider_gcp_attributes][machine_type]', with: 'n1-standard-2'
83 84
        end

T
Thong Kuah 已提交
85
        it_behaves_like 'valid cluster gcp form'
86

T
Thong Kuah 已提交
87
        context 'RBAC is enabled for the cluster' do
T
Thong Kuah 已提交
88 89 90
          before do
            check 'cluster_provider_gcp_attributes_legacy_abac'
          end
K
Kamil Trzcinski 已提交
91

T
Thong Kuah 已提交
92
          it_behaves_like 'valid cluster gcp form'
93
        end
94
      end
K
Kamil Trzcinski 已提交
95

96 97
      context 'when user filled form with invalid parameters' do
        before do
D
Dennis Tang 已提交
98
          execute_script('document.querySelector(".js-gke-cluster-creation-submit").removeAttribute("disabled")')
99
          click_button 'Create Kubernetes cluster'
K
Kamil Trzcinski 已提交
100
        end
101

102 103
        it 'user sees a validation error' do
          expect(page).to have_css('#error_explanation')
K
Kamil Trzcinski 已提交
104 105
        end
      end
106
    end
107

108 109
    context 'when user does have a cluster and visits cluster page' do
      let(:cluster) { create(:cluster, :provided_by_gcp, projects: [project]) }
110

111 112
      before do
        visit project_cluster_path(project, cluster)
113 114
      end

115 116 117
      it 'user sees a cluster details page' do
        expect(page).to have_button('Save changes')
        expect(page.find(:css, '.cluster-name').value).to eq(cluster.name)
118 119
      end

120 121 122 123 124
      context 'when user disables the cluster' do
        before do
          page.find(:css, '.js-cluster-enable-toggle-area .js-project-feature-toggle').click
          page.within('#cluster-integration') { click_button 'Save changes' }
        end
125

126 127 128 129
        it 'user sees the successful message' do
          expect(page).to have_content('Kubernetes cluster was successfully updated.')
        end
      end
130

131 132 133 134 135
      context 'when user changes cluster parameters' do
        before do
          fill_in 'cluster_platform_kubernetes_attributes_namespace', with: 'my-namespace'
          page.within('#js-cluster-details') { click_button 'Save changes' }
        end
136

137 138 139 140
        it 'user sees the successful message' do
          expect(page).to have_content('Kubernetes cluster was successfully updated.')
          expect(cluster.reload.platform_kubernetes.namespace).to eq('my-namespace')
        end
K
Kamil Trzcinski 已提交
141
      end
K
Kamil Trzcinski 已提交
142

143 144 145 146 147 148 149 150 151 152 153
      context 'when user destroy the cluster' do
        before do
          page.accept_confirm do
            click_link 'Remove integration'
          end
        end

        it 'user sees creation form with the successful message' do
          expect(page).to have_content('Kubernetes cluster integration was successfully removed.')
          expect(page).to have_link('Add Kubernetes cluster')
        end
K
Kamil Trzcinski 已提交
154 155 156 157 158 159 160 161
      end
    end
  end

  context 'when user has not signed with Google' do
    before do
      visit project_clusters_path(project)

162
      click_link 'Add Kubernetes cluster'
D
Dennis Tang 已提交
163
      click_link 'Create new Cluster on GKE'
K
Kamil Trzcinski 已提交
164 165 166 167
    end

    it 'user sees a login page' do
      expect(page).to have_css('.signin-with-google')
M
Matija Čupić 已提交
168
      expect(page).to have_link('Google account')
K
Kamil Trzcinski 已提交
169 170
    end
  end
171

172 173 174 175 176
  context 'when a user cannot edit the environment scope' do
    before do
      visit project_clusters_path(project)

      click_link 'Add Kubernetes cluster'
D
Dennis Tang 已提交
177
      click_link 'Add existing cluster'
178 179 180 181 182 183 184
    end

    it 'user does not see the "Environment scope" field' do
      expect(page).not_to have_css('#cluster_environment_scope')
    end
  end

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
  context 'when user has not dismissed GCP signup offer' do
    before do
      visit project_clusters_path(project)
    end

    it 'user sees offer on cluster index page' do
      expect(page).to have_css('.gcp-signup-offer')
    end

    it 'user sees offer on cluster create page' do
      click_link 'Add Kubernetes cluster'

      expect(page).to have_css('.gcp-signup-offer')
    end

    it 'user sees offer on cluster GCP login page' do
      click_link 'Add Kubernetes cluster'
D
Dennis Tang 已提交
202
      click_link 'Create new Cluster on GKE'
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223

      expect(page).to have_css('.gcp-signup-offer')
    end
  end

  context 'when user has dismissed GCP signup offer' do
    before do
      visit project_clusters_path(project)
    end

    it 'user does not see offer after dismissing' do
      expect(page).to have_css('.gcp-signup-offer')

      find('.gcp-signup-offer .close').click
      wait_for_requests

      click_link 'Add Kubernetes cluster'

      expect(page).not_to have_css('.gcp-signup-offer')
    end
  end
K
Kamil Trzcinski 已提交
224
end