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
|
# frozen_string_literal: true
require 'airborne'
module QA
RSpec.describe 'Package', only: { subdomain: %i[staging staging-canary pre] },
product_group: :container_registry do
include Support::API
include Support::Helpers::MaskToken
describe 'SaaS Container Registry API' do
let(:api_client) { Runtime::API::Client.new(:gitlab) }
let(:project) do
Resource::Project.fabricate_via_api! do |project|
project.name = 'project-with-registry-api'
project.template_name = 'express'
project.api_client = api_client
end
end
let!(:runner) do
Resource::ProjectRunner.fabricate! do |runner|
runner.project = project
runner.name = "runner-for-#{project.name}"
runner.tags = ["runner-for-#{project.name}"]
end
end
let!(:project_access_token) do
QA::Resource::ProjectAccessToken.fabricate_via_api! do |pat|
pat.project = project
end
end
let(:masked_token) do
use_ci_variable(name: 'PAT', value: project_access_token.token, project: project)
end
let(:gitlab_ci_yaml) do
<<~YAML
stages:
- build
- test
build:
image: docker:24.0.1
stage: build
services:
- docker:24.0.1-dind
variables:
IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
DOCKER_HOST: tcp://docker:2376
DOCKER_TLS_CERTDIR: "/certs"
DOCKER_TLS_VERIFY: 1
DOCKER_CERT_PATH: "$DOCKER_TLS_CERTDIR/client"
before_script:
- until docker info; do sleep 1; done
script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
- docker build -t $IMAGE_TAG .
- docker push $IMAGE_TAG
- docker pull $IMAGE_TAG
tags:
- "runner-for-#{project.name}"
test:
image: dwdraju/alpine-curl-jq:latest
stage: test
script:
- 'id=$(curl --header "PRIVATE-TOKEN: #{masked_token}" "https://${CI_SERVER_HOST}/api/v4/projects/#{project.id}/registry/repositories" | jq ".[0].id")'
- echo $id
- 'tag_count=$(curl --header "PRIVATE-TOKEN: #{masked_token}" "https://${CI_SERVER_HOST}/api/v4/projects/#{project.id}/registry/repositories/$id/tags" | jq ". | length")'
- if [ $tag_count -ne 1 ]; then exit 1; fi;
- 'status_code=$(curl --request DELETE --head --output /dev/null --write-out "%{http_code}\n" --header "PRIVATE-TOKEN: #{masked_token}" "https://${CI_SERVER_HOST}/api/v4/projects/#{project.id}/registry/repositories/$id/tags/master")'
- if [ $status_code -ne 200 ]; then exit 1; fi;
- 'status_code=$(curl --head --output /dev/null --write-out "%{http_code}\n" --header "PRIVATE-TOKEN: #{masked_token}" "https://${CI_SERVER_HOST}/api/v4/projects/#{project.id}/registry/repositories/$id/tags/master")'
- if [ $status_code -ne 404 ]; then exit 1; fi;
tags:
- "runner-for-#{project.name}"
YAML
end
after do
runner.remove_via_api!
end
it 'pushes, pulls image to the registry and deletes tag',
testcase: 'https://gitlab.com/gitlab-org/gitlab/-/quality/test_cases/348001' do
Support::Retrier.retry_on_exception(max_attempts: 3, sleep_interval: 2, message: "Commit push") do
Resource::Repository::Commit.fabricate_via_api! do |commit|
commit.api_client = api_client
commit.commit_message = 'Add .gitlab-ci.yml'
commit.project = project
commit.add_files([{
file_path: '.gitlab-ci.yml',
content: gitlab_ci_yaml
}])
end
end
Support::Retrier.retry_until(
max_duration: 10,
sleep_interval: 1,
message: "Waiting for pipeline to start"
) do
pipeline_is_triggered?
end
Support::Retrier.retry_until(
max_duration: 300,
sleep_interval: 5,
message: "Waiting for pipeline to succeed"
) do
latest_pipeline_succeed?
end
end
private
def pipeline_is_triggered?
!project.pipelines.empty?
end
def latest_pipeline_succeed?
latest_pipeline = project.pipelines.first
latest_pipeline[:status] == 'success'
end
end
end
end
|