Skip to content

Commit d05f003

Browse files
committed
Added Docker Registry View tests
1 parent d5d8e76 commit d05f003

File tree

10 files changed

+87
-27
lines changed

10 files changed

+87
-27
lines changed

app/controllers/jwt_controller.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class JwtController < ApplicationController
33
skip_before_action :verify_authenticity_token
44

55
SERVICES = {
6-
'container_registry' => Jwt::ContainerRegistryAuthenticationService,
6+
Jwt::ContainerRegistryAuthenticationService::AUDIENCE => Jwt::ContainerRegistryAuthenticationService,
77
}
88

99
def auth

app/services/jwt/container_registry_authentication_service.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module Jwt
22
class ContainerRegistryAuthenticationService < BaseService
3+
AUDIENCE = 'container_registry'
4+
35
def execute
46
if params[:offline_token]
57
return error('forbidden', 403) unless current_user
@@ -14,7 +16,7 @@ def self.full_access_token(*names)
1416
registry = Gitlab.config.registry
1517
token = ::Jwt::RSAToken.new(registry.key)
1618
token.issuer = registry.issuer
17-
token.audience = 'docker'
19+
token.audience = AUDIENCE
1820
token[:access] = names.map do |name|
1921
{ type: 'repository', name: name, actions: %w(pull push) }
2022
end
@@ -26,7 +28,7 @@ def self.full_access_token(*names)
2628
def authorized_token(access)
2729
token = ::Jwt::RSAToken.new(registry.key)
2830
token.issuer = registry.issuer
29-
token.audience = params[:service]
31+
token.audience = AUDIENCE
3032
token.subject = current_user.try(:username)
3133
token[:access] = access
3234
token

app/services/projects/destroy_service.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def execute
2727
project.destroy!
2828

2929
unless remove_registry_tags
30-
raise_error('Failed to remove project image registry. Please try again or contact administrator')
30+
raise_error('Failed to remove project container registry. Please try again or contact administrator')
3131
end
3232

3333
unless remove_repository(repo_path)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
%tr.tag
2+
%td
3+
= escape_once(tag.name)
4+
= clipboard_button(clipboard_text: "docker pull #{tag.path}")
5+
%td
6+
- if layer = tag.layers.first
7+
%span.has-tooltip(title="#{layer.revision}")
8+
= layer.short_revision
9+
- else
10+
\-
11+
%td
12+
= number_to_human_size(tag.total_size)
13+
&middot;
14+
= pluralize(tag.layers.size, "layer")
15+
%td
16+
= time_ago_in_words(tag.created_at)
17+
%td.content
18+
.controls.hidden-xs.pull-right
19+
= link_to namespace_project_container_registry_path(@project.namespace, @project, tag.name), class: 'btn btn-remove has-tooltip', title: "Remove", data: { confirm: "Are you sure?" }, method: :delete do
20+
= icon("trash cred")

app/views/projects/container_registry/index.html.haml

+2-21
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
- else
2828
.table-holder
29-
%table.table.builds
29+
%table.table.tags
3030
%thead
3131
%tr
3232
%th Name
@@ -36,23 +36,4 @@
3636
%th
3737

3838
- @tags.each do |tag|
39-
%tr
40-
%td
41-
= escape_once(tag.name)
42-
= clipboard_button(clipboard_text: "docker pull #{tag.path}")
43-
%td
44-
- if layer = tag.layers.first
45-
%span.has-tooltip(title="#{layer.revision}")
46-
= layer.short_revision
47-
- else
48-
\-
49-
%td
50-
= number_to_human_size(tag.total_size)
51-
&middot;
52-
= pluralize(tag.layers.size, "layer")
53-
%td
54-
= time_ago_in_words(tag.created_at)
55-
%td.content
56-
.controls.hidden-xs.pull-right
57-
= link_to namespace_project_container_registry_path(@project.namespace, @project, tag.name), class: 'btn btn-remove has-tooltip', title: "Remove", data: { confirm: "Are you sure?" }, method: :delete do
58-
= icon("trash cred")
39+
= render 'tag', tag: tag

lib/container_registry/client.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Client
1010
def initialize(base_uri, options = {})
1111
@base_uri = base_uri
1212
@faraday = Faraday.new(@base_uri) do |conn|
13-
initialize_connection(conn)
13+
initialize_connection(conn, options)
1414
end
1515
end
1616

@@ -51,7 +51,7 @@ def delete_blob(name, digest)
5151

5252
private
5353

54-
def initialize_connection(conn)
54+
def initialize_connection(conn, options)
5555
conn.request :json
5656
conn.headers['Accept'] = MANIFEST_VERSION
5757

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
require 'spec_helper'
2+
3+
describe "Container Registry" do
4+
let(:project) { create(:empty_project) }
5+
let(:repository) { project.container_registry_repository }
6+
let(:tag_name) { 'latest' }
7+
let(:tags) { [tag_name] }
8+
9+
before do
10+
end
11+
12+
before do
13+
login_as(:user)
14+
project.team << [@user, :developer]
15+
stub_container_registry(*tags)
16+
end
17+
18+
describe 'GET /:project/container_registry' do
19+
before do
20+
visit namespace_project_container_registry_index_path(project.namespace, project)
21+
end
22+
23+
context 'when no tags' do
24+
let(:tags) { [] }
25+
it { expect(page).to have_content('No images in Container Registry for this project') }
26+
end
27+
28+
context 'when there are tags' do
29+
it { expect(page).to have_content(tag_name)}
30+
end
31+
end
32+
33+
describe 'DELETE /:project/container_registry/tag' do
34+
before do
35+
visit namespace_project_container_registry_index_path(project.namespace, project)
36+
end
37+
38+
it do
39+
expect_any_instance_of(::ContainerRegistry::Tag).to receive(:delete).and_return(true)
40+
click_on 'Remove'
41+
end
42+
end
43+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"architecture":"amd64","config":{"Hostname":"b14cd8298755","Domainname":"","User":"","AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"Tty":false,"OpenStdin":false,"StdinOnce":false,"Env":null,"Cmd":null,"Image":"","Volumes":null,"WorkingDir":"","Entrypoint":null,"OnBuild":null,"Labels":null},"container":"b14cd82987550b01af9a666a2f4c996280a6152e66873134fae5a0f223dc5976","container_config":{"Hostname":"b14cd8298755","Domainname":"","User":"","AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"Tty":false,"OpenStdin":false,"StdinOnce":false,"Env":null,"Cmd":["/bin/sh","-c","#(nop) ADD file:033ab063740d9ff4dcfb1c69eccf25f91d88729f57cd5a73050e014e3e094aa0 in /"],"Image":"","Volumes":null,"WorkingDir":"","Entrypoint":null,"OnBuild":null,"Labels":null},"created":"2016-04-01T20:53:00.160300546Z","docker_version":"1.9.1","history":[{"created":"2016-04-01T20:53:00.160300546Z","created_by":"/bin/sh -c #(nop) ADD file:033ab063740d9ff4dcfb1c69eccf25f91d88729f57cd5a73050e014e3e094aa0 in /"}],"os":"linux","rootfs":{"type":"layers","diff_ids":["sha256:c56b7dabbc7aa730eeab07668bdcbd7e3d40855047ca9a0cc1bfed23a2486111"]}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"schemaVersion":2,"mediaType":"application/vnd.docker.distribution.manifest.v2+json","config":{"mediaType":"application/octet-stream","size":1145,"digest":"sha256:d7a513a663c1a6dcdba9ed832ca53c02ac2af0c333322cd6ca92936d1d9917ac"},"layers":[{"mediaType":"application/vnd.docker.image.rootfs.diff.tar.gzip","size":2319870,"digest":"sha256:420890c9e918b6668faaedd9000e220190f2493b0693ee563ebd7b4cc754a57d"}]}

spec/support/stub_gitlab_calls.rb

+12
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ def stub_ci_builds_disabled
2525
allow_any_instance_of(Project).to receive(:builds_enabled?).and_return(false)
2626
end
2727

28+
def stub_container_registry(*tags)
29+
allow_any_instance_of(ContainerRegistry::Client).to receive(:repository_tags).and_return(
30+
{ "tags" => tags }
31+
)
32+
allow_any_instance_of(ContainerRegistry::Client).to receive(:repository_manifest).and_return(
33+
JSON.load(File.read(Rails.root + 'spec/fixtures/container_registry/tag_manifest.json'))
34+
)
35+
allow_any_instance_of(ContainerRegistry::Client).to receive(:blob).and_return(
36+
File.read(Rails.root + 'spec/fixtures/container_registry/config_blob.json')
37+
)
38+
end
39+
2840
private
2941

3042
def gitlab_url

0 commit comments

Comments
 (0)