1
|
require 'test_plugin_helper'
|
2
|
|
3
|
class ImageSearchControllerTest < ActionController::TestCase
|
4
|
let(:docker_image) { 'centos' }
|
5
|
let(:tags) { ['latest', '5', '4.3'].map { |tag| "#{term}:#{tag}" } }
|
6
|
let(:term) { docker_image }
|
7
|
|
8
|
let(:docker_hub) { Service::RegistryApi.new(url: 'https://nothub.com') }
|
9
|
let(:compute_resource) { FactoryBot.create(:docker_cr) }
|
10
|
let(:registry) { FactoryBot.create(:docker_registry) }
|
11
|
let(:image_search_service) { ForemanDocker::ImageSearch.new }
|
12
|
|
13
|
setup do
|
14
|
Service::RegistryApi.stubs(:docker_hub).returns(docker_hub)
|
15
|
ComputeResource.const_get(:ActiveRecord_Relation).any_instance
|
16
|
.stubs(:find).returns(compute_resource)
|
17
|
DockerRegistry.const_get(:ActiveRecord_Relation).any_instance
|
18
|
.stubs(:find).returns(registry)
|
19
|
end
|
20
|
|
21
|
|
22
|
describe '#search_repository' do
|
23
|
let(:search_types) { ['hub', 'registry'] }
|
24
|
|
25
|
describe 'calls #search on image_search_service' do
|
26
|
setup do
|
27
|
subject.instance_variable_set(:@image_search_service, image_search_service)
|
28
|
end
|
29
|
|
30
|
test 'passes params search and tags' do
|
31
|
tags_enabled = ['true', 'false'].sample
|
32
|
image_search_service.expects(:search).with({ term: term, tags: tags_enabled })
|
33
|
.returns([])
|
34
|
get :search_repository,
|
35
|
params: { registry: search_types.sample, search: term, tags: tags_enabled,
|
36
|
id: compute_resource },
|
37
|
session: set_session_user,
|
38
|
xhr: true
|
39
|
end
|
40
|
|
41
|
test 'returns an array of { label:, value: } hashes' do
|
42
|
image_search_service.expects(:search).with({ term: term, tags: 'true' })
|
43
|
.returns(tags)
|
44
|
get :search_repository,
|
45
|
params: { registry: search_types.sample, search: term, tags: 'true',
|
46
|
id: compute_resource },
|
47
|
session: set_session_user,
|
48
|
xhr: true
|
49
|
|
50
|
assert_equal tags.first, JSON.parse(response.body).first['value']
|
51
|
end
|
52
|
|
53
|
test 'returns html with the found images' do
|
54
|
image_search_service.expects(:search)
|
55
|
.with({ term: term, tags: 'false' })
|
56
|
.returns([{ 'name' => term }])
|
57
|
get :search_repository, params:
|
58
|
{ registry: search_types.sample, search: term,
|
59
|
id: compute_resource, format: :html },
|
60
|
session: set_session_user, xhr: true
|
61
|
|
62
|
assert response.body.include?(term)
|
63
|
end
|
64
|
|
65
|
[Docker::Error::DockerError, Excon::Errors::Error, Errno::ECONNREFUSED].each do |error|
|
66
|
test "search_repository catch exceptions on network errors like #{error}" do
|
67
|
image_search_service.expects(:search)
|
68
|
.raises(error)
|
69
|
get :search_repository,
|
70
|
params: { registry: search_types.sample, search: term, id: compute_resource },
|
71
|
session: set_session_user, xhr: true
|
72
|
|
73
|
assert_response :error
|
74
|
assert response.body.include?('An error occured during repository search:')
|
75
|
end
|
76
|
end
|
77
|
|
78
|
test "centos 7 search responses are handled correctly" do
|
79
|
repository = "registry-fancycorp.rhcloud.com/fancydb-rhel7/fancydb"
|
80
|
repo_full_name = "redhat.com: #{repository}"
|
81
|
expected = [{ "description" => "Really fancy database app...",
|
82
|
"is_official" => true,
|
83
|
"is_trusted" => true,
|
84
|
"name" => repo_full_name,
|
85
|
"star_count" => 0
|
86
|
}]
|
87
|
image_search_service.expects(:search).returns(expected)
|
88
|
get :search_repository,
|
89
|
params: { registry: search_types.sample, search: 'centos', id: compute_resource, format: :html },
|
90
|
session: set_session_user,
|
91
|
xhr: true
|
92
|
|
93
|
assert_response :success
|
94
|
refute response.body.include?(repo_full_name)
|
95
|
assert response.body.include?(repository)
|
96
|
end
|
97
|
|
98
|
test "fedora search responses are handled correctly" do
|
99
|
repository = "registry-fancycorp.rhcloud.com/fancydb-rhel7/fancydb"
|
100
|
repo_full_name = repository
|
101
|
request.env["HTTP_ACCEPT"] = "application/javascript"
|
102
|
expected = [{ "description" => "Really fancy database app...",
|
103
|
"is_official" => true,
|
104
|
"is_trusted" => true,
|
105
|
"name" => repo_full_name,
|
106
|
"star_count" => 0
|
107
|
}]
|
108
|
image_search_service.expects(:search).returns(expected)
|
109
|
get :search_repository,
|
110
|
params: { registry: search_types.sample, search: term, id: compute_resource, format: :html },
|
111
|
session: set_session_user,
|
112
|
xhr: true
|
113
|
|
114
|
assert_response :success
|
115
|
assert response.body.include?(repo_full_name)
|
116
|
assert response.body.include?(repository)
|
117
|
end
|
118
|
end
|
119
|
|
120
|
describe 'for image names' do
|
121
|
context 'with a Docker Hub tab request' do
|
122
|
let(:search_type) { 'hub' }
|
123
|
|
124
|
test 'it searches Docker Hub and the ComputeResource' do
|
125
|
compute_resource.expects(:local_images)
|
126
|
.returns([OpenStruct.new(info: { 'RepoTags' => [term] })])
|
127
|
docker_hub.expects(:search).returns({})
|
128
|
|
129
|
get :search_repository,
|
130
|
params: { registry: search_type, search: term,
|
131
|
id: compute_resource },
|
132
|
session: set_session_user,
|
133
|
xhr: true
|
134
|
end
|
135
|
end
|
136
|
|
137
|
context 'with a External Registry tab request' do
|
138
|
let(:search_type) { 'registry' }
|
139
|
|
140
|
test 'it only queries the registry api' do
|
141
|
compute_resource.expects(:local_images).with(docker_image).never
|
142
|
docker_hub.expects(:search).never
|
143
|
registry.api.expects(:search).with(docker_image)
|
144
|
.returns({})
|
145
|
|
146
|
get :search_repository,
|
147
|
params: { registry: search_type, registry_id: registry,
|
148
|
search: term, id: compute_resource },
|
149
|
session: set_session_user,
|
150
|
xhr: true
|
151
|
end
|
152
|
end
|
153
|
end
|
154
|
|
155
|
describe 'for tags' do
|
156
|
let(:tag_fragment) { 'lat' }
|
157
|
let(:term) { "#{docker_image}:#{tag_fragment}"}
|
158
|
|
159
|
context 'with a Docker Hub tab request' do
|
160
|
let(:search_type) { 'hub' }
|
161
|
|
162
|
test 'it searches Docker Hub and the ComputeResource' do
|
163
|
compute_resource.expects(:image).with(docker_image)
|
164
|
.returns(term)
|
165
|
compute_resource.expects(:tags_for_local_image)
|
166
|
.returns(tags)
|
167
|
docker_hub.expects(:tags).returns([])
|
168
|
|
169
|
get :search_repository,
|
170
|
params: { registry: search_type, search: term,
|
171
|
tags: 'true', id: compute_resource },
|
172
|
session: set_session_user,
|
173
|
xhr: true
|
174
|
end
|
175
|
end
|
176
|
|
177
|
context 'with a External Registry tab request' do
|
178
|
let(:search_type) { 'registry' }
|
179
|
|
180
|
test 'it only queries the registry api' do
|
181
|
compute_resource.expects(:image).with(docker_image).never
|
182
|
docker_hub.expects(:tags).never
|
183
|
registry.api.expects(:tags).with(docker_image, tag_fragment)
|
184
|
.returns([])
|
185
|
|
186
|
get :search_repository,
|
187
|
params: { registry: search_type, registry_id: registry, tags: 'true',
|
188
|
search: term, id: compute_resource },
|
189
|
session: set_session_user,
|
190
|
xhr: true
|
191
|
end
|
192
|
end
|
193
|
end
|
194
|
end
|
195
|
end
|