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