Project

General

Profile

Revision f02a9ce2

Added by Sebastian Gräßl over 5 years ago

Fixes #18733 - Prevent fallback to Docker Hub on registry search

When searching for an external registry the search would show
results from Docker Hub due to it searching via the compute
resource.

By using a registry parameter to indicate which search tab is
active this gets prevented on the backend as well as on the
client by not searching when no registry is selected.

View differences:

test/functionals/image_search_controller_test.rb
1 1
require 'test_plugin_helper'
2 2

  
3 3
class ImageSearchControllerTest < ActionController::TestCase
4
  let(:image) { 'centos' }
5
  let(:tags) { ['latest', '5', '4.3'].map { |tag| "#{term}:#{tag}" } }
6
  let(:term) { 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

  
4 13
  setup do
5
    @container = FactoryGirl.create(:docker_cr)
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
6 64
  end
7 65

  
8 66
  describe '#auto_complete_image_tag' do
9
    let(:tags) { ['latest', '5', '4.3'] }
67
    let(:tag_fragment) { 'lat' }
68
    let(:term) { "#{image}:#{tag_fragment}"}
10 69

  
11 70
    test 'returns an array of { label:, value: } hashes' do
12
      ForemanDocker::Docker.any_instance.expects(:tags)
13
        .with('test')
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' })
14 75
        .returns(tags)
15
      get :auto_complete_image_tag,
16
          { search: "test", id: @container.id, format: :js }, set_session_user
76
      xhr :get, :auto_complete_image_tag,
77
        { registry: search_type, search: term,
78
          id: compute_resource }, set_session_user
17 79
      assert_equal tags.first, JSON.parse(response.body).first['value']
18 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(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(image).never
103
        docker_hub.expects(:tags).never
104
        registry.api.expects(:tags).with(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(image).never
146
        docker_hub.expects(:search).never
147
        registry.api.expects(:search).with(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
19 155
  end
20 156

  
21 157
  [Docker::Error::DockerError, Excon::Errors::Error, Errno::ECONNREFUSED].each do |error|
22 158
    test 'auto_complete_repository_name catches exceptions on network errors' do
23
      ForemanDocker::Docker.any_instance.expects(:exist?).raises(error)
24
      get :auto_complete_repository_name, { :search => "test", :id => @container.id },
25
          set_session_user
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
26 163
      assert_response_is_expected
27 164
    end
28 165

  
29 166
    test 'auto_complete_image_tag catch exceptions on network errors' do
30
      ForemanDocker::Docker.any_instance.expects(:tags).raises(error)
31
      get :auto_complete_image_tag, { :search => "test", :id => @container.id }, set_session_user
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
32 170
      assert_response_is_expected
33 171
    end
34 172

  
35 173
    test 'search_repository catch exceptions on network errors' do
36
      ForemanDocker::Docker.any_instance.expects(:search).raises(error)
37
      get :search_repository, { :search => "test", :id => @container.id }, set_session_user
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
38 177
      assert_response_is_expected
39 178
    end
40 179
  end
......
49 188
                   "name" =>  repo_full_name,
50 189
                   "star_count" => 0
51 190
                }]
52
    ForemanDocker::Docker.any_instance.expects(:search).returns(expected).at_least_once
53
    get :search_repository, { :search => "centos", :id => @container.id }, set_session_user
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
54 194
    assert_response :success
55 195
    refute response.body.include?(repo_full_name)
56 196
    assert response.body.include?(repository)
......
66 206
                  "name" =>  repo_full_name,
67 207
                  "star_count" => 0
68 208
                }]
69
    ForemanDocker::Docker.any_instance.expects(:search).returns(expected).at_least_once
70
    get :search_repository, { :search => "centos", :id => @container.id }, set_session_user
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
71 212
    assert_response :success
72 213
    assert response.body.include?(repo_full_name)
73 214
    assert response.body.include?(repository)

Also available in: Unified diff