summaryrefslogtreecommitdiff
path: root/qa/qa/specs/features/browser_ui/7_configure/auto_devops/create_project_with_auto_devops_spec.rb
blob: d6446c9725d11418b6664eea9cb2b6690e9a0b53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# frozen_string_literal: true

module QA
  RSpec.describe 'Configure',
                 quarantine: { issue: 'https://gitlab.com/gitlab-org/gitlab/-/issues/381454', type: :flaky },
                 only: { subdomain: %i[staging staging-canary] }, product_group: :configure do
    describe 'Auto DevOps with a Kubernetes Agent' do
      let!(:app_project) do
        Resource::Project.fabricate_via_api! do |project|
          project.name = 'autodevops-app-project'
          project.template_name = 'express'
          project.auto_devops_enabled = true
        end
      end

      let!(:cluster) { Service::KubernetesCluster.new(provider_class: Service::ClusterProvider::Gcloud).create! }

      let!(:kubernetes_agent) do
        Resource::Clusters::Agent.fabricate_via_api! do |agent|
          agent.name = 'agent1'
          agent.project = app_project
        end
      end

      let!(:agent_token) do
        Resource::Clusters::AgentToken.fabricate_via_api! do |token|
          token.agent = kubernetes_agent
        end
      end

      before do
        cluster.install_kubernetes_agent(agent_token.token)
        upload_agent_config(app_project, kubernetes_agent.name)

        set_kube_ingress_base_domain(app_project)
        set_kube_context(app_project)
        disable_optional_jobs(app_project)
      end

      after do
        cluster&.remove!
      end

      it 'runs auto devops', testcase: 'https://gitlab.com/gitlab-org/gitlab/-/quality/test_cases/348061' do
        Flow::Login.sign_in

        app_project.visit!

        Page::Project::Menu.perform(&:click_ci_cd_pipelines)
        Page::Project::Pipeline::Index.perform(&:click_run_pipeline_button)
        Page::Project::Pipeline::New.perform(&:click_run_pipeline_button)

        Page::Project::Pipeline::Show.perform do |pipeline|
          pipeline.click_job('build')
        end
        Page::Project::Job::Show.perform do |job|
          expect(job).to be_successful(timeout: 600)

          job.click_element(:pipeline_path)
        end

        Page::Project::Pipeline::Show.perform do |pipeline|
          pipeline.click_job('production')
        end
        Page::Project::Job::Show.perform do |job|
          expect(job).to be_successful(timeout: 600)
        end
      end
    end

    private

    def set_kube_ingress_base_domain(project)
      Resource::CiVariable.fabricate_via_api! do |resource|
        resource.project = project
        resource.key = 'KUBE_INGRESS_BASE_DOMAIN'
        resource.value = 'example.com'
        resource.masked = false
      end
    end

    def set_kube_context(project)
      Resource::CiVariable.fabricate_via_api! do |resource|
        resource.project = project
        resource.key = 'KUBE_CONTEXT'
        resource.value = "#{project.path_with_namespace}:#{kubernetes_agent.name}"
        resource.masked = false
      end
    end

    def upload_agent_config(project, agent)
      Support::Retrier.retry_on_exception(max_attempts: 3, sleep_interval: 2) do
        Resource::Repository::Commit.fabricate_via_api! do |commit|
          commit.project = project
          commit.commit_message = 'Add kubernetes agent configuration'
          commit.add_files(
            [
              {
                file_path: ".gitlab/agents/#{agent}/config.yaml",
                content: <<~YAML
                  ci_access:
                    projects:
                      - id: #{project.path_with_namespace}
                YAML
              }
            ]
          )
        end
      end
    end

    def disable_optional_jobs(project)
      %w[
        TEST_DISABLED CODE_QUALITY_DISABLED LICENSE_MANAGEMENT_DISABLED
        BROWSER_PERFORMANCE_DISABLED LOAD_PERFORMANCE_DISABLED
        SAST_DISABLED SECRET_DETECTION_DISABLED DEPENDENCY_SCANNING_DISABLED
        CONTAINER_SCANNING_DISABLED DAST_DISABLED REVIEW_DISABLED
        CODE_INTELLIGENCE_DISABLED CLUSTER_IMAGE_SCANNING_DISABLED
      ].each do |key|
        Resource::CiVariable.fabricate_via_api! do |resource|
          resource.project = project
          resource.key = key
          resource.value = '1'
          resource.masked = false
        end
      end
    end
  end
end