Project

General

Profile

Download (7.17 KB) Statistics
| Branch: | Tag: | Revision:

foreman_docker / test / functionals / image_search_controller_test.rb @ 00bd7de4

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
  describe '#search_repository' do
22
    let(:search_types) { ['hub', 'registry'] }
23

    
24
    describe 'calls #search on image_search_service' do
25
      setup do
26
        subject.instance_variable_set(:@image_search_service, image_search_service)
27
      end
28

    
29
      test 'passes params search and tags' do
30
        tags_enabled = ['true', 'false'].sample
31
        image_search_service.expects(:search).with({ term: term, tags: tags_enabled })
32
          .returns([])
33
        get :search_repository,
34
            params: { registry: search_types.sample, search: term, tags: tags_enabled,
35
                      id: compute_resource },
36
            session: set_session_user,
37
            xhr: true
38
      end
39

    
40
      test 'returns an array of { label:, value: } hashes' do
41
        image_search_service.expects(:search).with({ term: term, tags: 'true' })
42
          .returns(tags)
43
        get :search_repository,
44
            params: { registry: search_types.sample, search: term, tags: 'true',
45
                      id: compute_resource },
46
            session: set_session_user,
47
            xhr: true
48

    
49
        assert_equal tags.first, JSON.parse(response.body).first['value']
50
      end
51

    
52
      test 'returns html with the found images' do
53
        image_search_service.expects(:search)
54
          .with({ term: term, tags: 'false' })
55
          .returns([{ 'name' => term }])
56
        get :search_repository, params:
57
          { registry: search_types.sample, search: term,
58
            id: compute_resource, format: :html },
59
            session: set_session_user, xhr: true
60

    
61
        assert response.body.include?(term)
62
      end
63

    
64
      [Docker::Error::DockerError, Excon::Errors::Error, Errno::ECONNREFUSED].each do |error|
65
        test "search_repository catch exceptions on network errors like #{error}" do
66
          image_search_service.expects(:search)
67
            .raises(error)
68
          get :search_repository,
69
              params: { registry: search_types.sample, search: term, id: compute_resource },
70
              session: set_session_user, xhr: true
71

    
72
          assert_response :error
73
          assert response.body.include?('An error occured during repository search:')
74
        end
75
      end
76

    
77
      test "centos 7 search responses are handled correctly" do
78
        repository = "registry-fancycorp.rhcloud.com/fancydb-rhel7/fancydb"
79
        repo_full_name = "redhat.com: #{repository}"
80
        expected = [{  "description" => "Really fancy database app...",
81
                       "is_official" => true,
82
                       "is_trusted" => true,
83
                       "name" =>  repo_full_name,
84
                       "star_count" => 0
85
                    }]
86
        image_search_service.expects(:search).returns(expected)
87
        get :search_repository,
88
            params: { registry: search_types.sample, search: 'centos', id: compute_resource, format: :html },
89
            session: set_session_user,
90
            xhr: true
91

    
92
        assert_response :success
93
        refute response.body.include?(repo_full_name)
94
        assert response.body.include?(repository)
95
      end
96

    
97
      test "fedora search responses are handled correctly" do
98
        repository = "registry-fancycorp.rhcloud.com/fancydb-rhel7/fancydb"
99
        repo_full_name = repository
100
        request.env["HTTP_ACCEPT"] = "application/javascript"
101
        expected = [{ "description" => "Really fancy database app...",
102
                      "is_official" => true,
103
                      "is_trusted" => true,
104
                      "name" =>  repo_full_name,
105
                      "star_count" => 0
106
                    }]
107
        image_search_service.expects(:search).returns(expected)
108
        get :search_repository,
109
            params: { registry: search_types.sample, search: term, id: compute_resource, format: :html },
110
            session: set_session_user,
111
            xhr: true
112

    
113
        assert_response :success
114
        assert response.body.include?(repo_full_name)
115
        assert response.body.include?(repository)
116
      end
117
    end
118

    
119
    describe 'for image names' do
120
      context 'with a Docker Hub tab request' do
121
        let(:search_type) { 'hub' }
122

    
123
        test 'it searches Docker Hub and the ComputeResource' do
124
          compute_resource.expects(:local_images)
125
            .returns([OpenStruct.new(info: { 'RepoTags' => [term] })])
126
          docker_hub.expects(:search).returns({})
127

    
128
          get :search_repository,
129
              params:  { registry: search_type, search: term,
130
                         id: compute_resource },
131
              session: set_session_user,
132
              xhr: true
133
        end
134
      end
135

    
136
      context 'with a External Registry tab request' do
137
        let(:search_type) { 'registry' }
138

    
139
        test 'it only queries the registry api' do
140
          compute_resource.expects(:local_images).with(docker_image).never
141
          docker_hub.expects(:search).never
142
          registry.api.expects(:search).with(docker_image)
143
            .returns({})
144

    
145
          get :search_repository,
146
              params: { registry: search_type, registry_id: registry,
147
              search: term, id: compute_resource },
148
              session: set_session_user,
149
              xhr: true
150
        end
151
      end
152
    end
153

    
154
    describe 'for tags' do
155
      let(:tag_fragment) { 'lat' }
156
      let(:term) { "#{docker_image}:#{tag_fragment}"}
157

    
158
      context 'with a Docker Hub tab request' do
159
        let(:search_type) { 'hub' }
160

    
161
        test 'it searches Docker Hub and the ComputeResource' do
162
          compute_resource.expects(:image).with(docker_image)
163
            .returns(term)
164
          compute_resource.expects(:tags_for_local_image)
165
            .returns(tags)
166
          docker_hub.expects(:tags).returns([])
167

    
168
          get :search_repository,
169
              params: { registry: search_type, search: term,
170
                        tags: 'true', id: compute_resource },
171
              session: set_session_user,
172
              xhr: true
173
        end
174
      end
175

    
176
      context 'with a External Registry tab request' do
177
        let(:search_type) { 'registry' }
178

    
179
        test 'it only queries the registry api' do
180
          compute_resource.expects(:image).with(docker_image).never
181
          docker_hub.expects(:tags).never
182
          registry.api.expects(:tags).with(docker_image, tag_fragment)
183
            .returns([])
184

    
185
          get :search_repository,
186
              params: { registry: search_type, registry_id: registry, tags: 'true',
187
                        search: term, id: compute_resource },
188
              session: set_session_user,
189
              xhr: true
190
        end
191
      end
192
    end
193
  end
194
end