gcp_spec.rb 6.92 KB
Newer Older
Kamil Trzcinski's avatar
Kamil Trzcinski committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
require 'spec_helper'

feature 'Gcp Cluster', :js do
  include GoogleApi::CloudPlatformHelpers

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

  before do
    project.add_master(user)
    gitlab_sign_in(user)
    allow(Projects::ClustersController).to receive(:STATUS_POLLING_INTERVAL) { 100 }
  end

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

Kamil Trzcinski's avatar
Kamil Trzcinski committed
18
    before do
Dennis Tang's avatar
Dennis Tang committed
19
      allow_any_instance_of(Projects::ClustersController)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
20
        .to receive(:token_in_session).and_return('token')
Dennis Tang's avatar
Dennis Tang committed
21
      allow_any_instance_of(Projects::ClustersController)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
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
Kamil Trzcinski's avatar
Kamil Trzcinski committed
26
      before do
27 28 29
        visit project_clusters_path(project)

        click_link 'Add Kubernetes cluster'
Dennis Tang's avatar
Dennis Tang committed
30
        click_link 'Create new Cluster on GKE'
Kamil Trzcinski's avatar
Kamil Trzcinski committed
31 32
      end

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

Kamil Trzcinski's avatar
Kamil Trzcinski committed
36
        before do
37 38 39 40 41 42
          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'
            )
Kamil Trzcinski's avatar
Kamil Trzcinski committed
43 44
          end

45
          allow(WaitForClusterCreationWorker).to receive(:perform_in).and_return(nil)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
46

Dennis Tang's avatar
Dennis Tang committed
47 48 49 50 51 52 53 54 55 56
          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'
57 58 59 60
        end

        it 'users sees a form with the GCP token' do
          expect(page).to have_selector(:css, 'form[data-token="token"]')
61
        end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
62

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

66
          expect(page).to have_content('Kubernetes cluster is being created on Google Kubernetes Engine...')
Kamil Trzcinski's avatar
Kamil Trzcinski committed
67

68
          Clusters::Cluster.last.provider.make_created!
69

70
          expect(page).to have_content('Kubernetes cluster was successfully created on Google Kubernetes Engine')
Kamil Trzcinski's avatar
Kamil Trzcinski committed
71 72
        end

Dennis Tang's avatar
Dennis Tang committed
73
        it 'user sees a error if something wrong during creation' do
74 75
          subject

76
          expect(page).to have_content('Kubernetes cluster is being created on Google Kubernetes Engine...')
Kamil Trzcinski's avatar
Kamil Trzcinski committed
77

78
          Clusters::Cluster.last.provider.make_errored!('Something wrong!')
Kamil Trzcinski's avatar
Kamil Trzcinski committed
79

80
          expect(page).to have_content('Something wrong!')
81
        end
82
      end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
83

84 85
      context 'when user filled form with invalid parameters' do
        before do
Dennis Tang's avatar
Dennis Tang committed
86
          execute_script('document.querySelector(".js-gke-cluster-creation-submit").removeAttribute("disabled")')
87
          click_button 'Create Kubernetes cluster'
Kamil Trzcinski's avatar
Kamil Trzcinski committed
88
        end
89

90 91
        it 'user sees a validation error' do
          expect(page).to have_css('#error_explanation')
Kamil Trzcinski's avatar
Kamil Trzcinski committed
92 93
        end
      end
94
    end
95

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

99 100
      before do
        visit project_cluster_path(project, cluster)
101 102
      end

103 104 105
      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)
106 107
      end

108 109 110 111 112
      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
113

114 115 116 117
        it 'user sees the successful message' do
          expect(page).to have_content('Kubernetes cluster was successfully updated.')
        end
      end
118

119 120 121 122 123
      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
124

125 126 127 128
        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
Kamil Trzcinski's avatar
Kamil Trzcinski committed
129
      end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
130

131 132 133 134 135 136 137 138 139 140 141
      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
Kamil Trzcinski's avatar
Kamil Trzcinski committed
142 143 144 145 146 147 148 149
      end
    end
  end

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

150
      click_link 'Add Kubernetes cluster'
Dennis Tang's avatar
Dennis Tang committed
151
      click_link 'Create new Cluster on GKE'
Kamil Trzcinski's avatar
Kamil Trzcinski committed
152 153 154 155
    end

    it 'user sees a login page' do
      expect(page).to have_css('.signin-with-google')
156
      expect(page).to have_link('Google account')
Kamil Trzcinski's avatar
Kamil Trzcinski committed
157 158
    end
  end
159

160 161 162 163 164 165 166 167 168 169 170 171 172
  context 'when a user cannot edit the environment scope' do
    before do
      visit project_clusters_path(project)

      click_link 'Add Kubernetes cluster'
      click_link 'Add an existing Kubernetes cluster'
    end

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

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
  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'
Dennis Tang's avatar
Dennis Tang committed
190
      click_link 'Create new Cluster on GKE'
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211

      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
Kamil Trzcinski's avatar
Kamil Trzcinski committed
212
end