summaryrefslogtreecommitdiff
path: root/qa/qa/specs/features/browser_ui/5_package/package_registry/maven_gradle_repository_spec.rb
blob: 57e1aa6a0871cd660f27fb7c997801f9a61ab7e0 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# frozen_string_literal: true

module QA
  RSpec.describe 'Package', :orchestrated, :packages, :object_storage do
    describe 'Maven Repository with Gradle' do
      using RSpec::Parameterized::TableSyntax
      include Runtime::Fixtures
      include_context 'packages registry qa scenario'

      let(:group_id) { 'com.gitlab.qa' }
      let(:artifact_id) { "maven_gradle-#{SecureRandom.hex(8)}" }
      let(:package_name) { "#{group_id}/#{artifact_id}".tr('.', '/') }
      let(:package_version) { '1.3.7' }
      let(:package_type) { 'maven_gradle' }

      let(:package_gitlab_ci_file) do
        {
          file_path: '.gitlab-ci.yml',
          content:
              <<~YAML
                deploy:
                  image: gradle:6.5-jdk11
                  script:
                  - 'gradle publish'
                  only:
                  - "#{package_project.default_branch}"
                  tags:
                  - "runner-for-#{package_project.group.name}"
              YAML
        }
      end

      let(:package_build_gradle_file) do
        {
          file_path: 'build.gradle',
          content:
              <<~EOF
                plugins {
                    id 'java'
                    id 'maven-publish'
                }

                publishing {
                    publications {
                        library(MavenPublication) {
                            groupId '#{group_id}'
                            artifactId '#{artifact_id}'
                            version '#{package_version}'
                            from components.java
                        }
                    }
                    repositories {
                        maven {
                            url "#{gitlab_address_with_port}/api/v4/projects/#{package_project.id}/packages/maven"
                            credentials(HttpHeaderCredentials) {
                                name = "Private-Token"
                                value = "#{personal_access_token}"
                            }
                            authentication {
                                header(HttpHeaderAuthentication)
                            }
                        }
                    }
                }
              EOF
        }
      end

      let(:client_gitlab_ci_file) do
        {
          file_path: '.gitlab-ci.yml',
          content:
              <<~YAML
                build:
                  image: gradle:6.5-jdk11
                  script:
                  - 'gradle build'
                  only:
                  - "#{client_project.default_branch}"
                  tags:
                  - "runner-for-#{client_project.group.name}"
              YAML
        }
      end

      where(:authentication_token_type, :maven_header_name) do
        :personal_access_token | 'Private-Token'
        :ci_job_token          | 'Job-Token'
        :project_deploy_token  | 'Deploy-Token'
      end

      with_them do
        let(:token) do
          case authentication_token_type
          when :personal_access_token
            "\"#{personal_access_token}\""
          when :ci_job_token
            'System.getenv("CI_JOB_TOKEN")'
          when :project_deploy_token
            "\"#{project_deploy_token.token}\""
          end
        end

        let(:client_build_gradle_file) do
          {
            file_path: 'build.gradle',
            content:
                <<~EOF
                  plugins {
                      id 'java'
                      id 'application'
                  }

                  repositories {
                      jcenter()
                      maven {
                          url "#{gitlab_address_with_port}/api/v4/projects/#{package_project.id}/packages/maven"
                          name "GitLab"
                          credentials(HttpHeaderCredentials) {
                              name = '#{maven_header_name}'
                              value = #{token}
                          }
                          authentication {
                              header(HttpHeaderAuthentication)
                          }
                      }
                  }

                  dependencies {
                      implementation group: '#{group_id}', name: '#{artifact_id}', version: '#{package_version}'
                      testImplementation 'junit:junit:4.12'
                  }

                  application {
                    mainClassName = 'gradle_maven_app.App'
                  }
                EOF
          }
        end

        it "pushes and pulls a maven package via gradle using #{params[:authentication_token_type]}" do
          Support::Retrier.retry_on_exception(max_attempts: 3, sleep_interval: 2) do
            Resource::Repository::Commit.fabricate_via_api! do |commit|
              commit.project = package_project
              commit.commit_message = 'Add .gitlab-ci.yml'
              commit.add_files([package_gitlab_ci_file, package_build_gradle_file])
            end
          end

          package_project.visit!

          Flow::Pipeline.visit_latest_pipeline

          Page::Project::Pipeline::Show.perform do |pipeline|
            pipeline.click_job('deploy')
          end

          Page::Project::Job::Show.perform do |job|
            expect(job).to be_successful(timeout: 800)
          end

          Page::Project::Menu.perform(&:click_packages_link)

          Page::Project::Packages::Index.perform do |index|
            expect(index).to have_package(package_name)

            index.click_package(package_name)
          end

          Page::Project::Packages::Show.perform do |show|
            expect(show).to have_package_info(package_name, package_version)
          end

          Support::Retrier.retry_on_exception(max_attempts: 3, sleep_interval: 2) do
            Resource::Repository::Commit.fabricate_via_api! do |commit|
              commit.project = client_project
              commit.commit_message = 'Add .gitlab-ci.yml'
              commit.add_files([client_gitlab_ci_file, client_build_gradle_file])
            end
          end

          client_project.visit!

          Flow::Pipeline.visit_latest_pipeline

          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: 800)
          end
        end
      end
    end
  end
end