Project

General

Profile

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

foreman_docker / test / functionals / image_search_controller_test.rb @ 329cfcba

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 '#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
        xhr :get, :search_repository,
34
          { registry: search_types.sample, search: term, tags: tags_enabled,
35
            id: compute_resource }, set_session_user
36
      end
37

    
38
      test 'returns an array of { label:, value: } hashes' do
39
        image_search_service.expects(:search).with({ term: term, tags: 'true' })
40
          .returns(tags)
41
        xhr :get, :search_repository,
42
          { registry: search_types.sample, search: term, tags: 'true',
43
            id: compute_resource }, set_session_user
44
        assert_equal tags.first, JSON.parse(response.body).first['value']
45
      end
46

    
47
      test 'returns html with the found images' do
48
        image_search_service.expects(:search)
49
          .with({ term: term, tags: 'false' })
50
          .returns([{ 'name' => term }])
51
        xhr :get, :search_repository,
52
          { registry: search_types.sample, search: term,
53
            id: compute_resource, format: :html}, set_session_user
54
        assert response.body.include?(term)
55
      end
56

    
57
      [Docker::Error::DockerError, Excon::Errors::Error, Errno::ECONNREFUSED].each do |error|
58
        test "search_repository catch exceptions on network errors like #{error}" do
59
          image_search_service.expects(:search)
60
            .raises(error)
61
          xhr :get, :search_repository,
62
            { registry: search_types.sample, search: term, id: compute_resource }, set_session_user
63

    
64
          assert_response :error
65
          assert response.body.include?('An error occured during repository search:')
66
        end
67
      end
68

    
69
      test "centos 7 search responses are handled correctly" do
70
        repository = "registry-fancycorp.rhcloud.com/fancydb-rhel7/fancydb"
71
        repo_full_name = "redhat.com: #{repository}"
72
        expected = [{  "description" => "Really fancy database app...",
73
                       "is_official" => true,
74
                       "is_trusted" => true,
75
                       "name" =>  repo_full_name,
76
                       "star_count" => 0
77
                    }]
78
        image_search_service.expects(:search).returns(expected)
79
        xhr :get, :search_repository,
80
          { registry: search_types.sample, search: 'centos', id: compute_resource, format: :html }, set_session_user
81
        assert_response :success
82
        refute response.body.include?(repo_full_name)
83
        assert response.body.include?(repository)
84
      end
85

    
86
      test "fedora search responses are handled correctly" do
87
        repository = "registry-fancycorp.rhcloud.com/fancydb-rhel7/fancydb"
88
        repo_full_name = repository
89
        request.env["HTTP_ACCEPT"] = "application/javascript"
90
        expected = [{ "description" => "Really fancy database app...",
91
                      "is_official" => true,
92
                      "is_trusted" => true,
93
                      "name" =>  repo_full_name,
94
                      "star_count" => 0
95
                    }]
96
        image_search_service.expects(:search).returns(expected)
97
        xhr :get, :search_repository,
98
          { registry: search_types.sample, search: term, id: compute_resource, format: :html }, set_session_user
99
        assert_response :success
100
        assert response.body.include?(repo_full_name)
101
        assert response.body.include?(repository)
102
      end
103
    end
104

    
105
    describe 'for image names' do
106
      context 'with a Docker Hub tab request' do
107
        let(:search_type) { 'hub' }
108

    
109
        test 'it searches Docker Hub and the ComputeResource' do
110
          compute_resource.expects(:local_images)
111
            .returns([OpenStruct.new(info: { 'RepoTags' => [term] })])
112
          docker_hub.expects(:search).returns({})
113

    
114
          xhr :get, :search_repository,
115
            { registry: search_type, search: term,
116
              id: compute_resource }, set_session_user
117
        end
118
      end
119

    
120
      context 'with a External Registry tab request' do
121
        let(:search_type) { 'registry' }
122

    
123
        test 'it only queries the registry api' do
124
          compute_resource.expects(:local_images).with(docker_image).never
125
          docker_hub.expects(:search).never
126
          registry.api.expects(:search).with(docker_image)
127
            .returns({})
128

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

    
136
    describe 'for tags' do
137
      let(:tag_fragment) { 'lat' }
138
      let(:term) { "#{docker_image}:#{tag_fragment}"}
139

    
140
      context 'with a Docker Hub tab request' do
141
        let(:search_type) { 'hub' }
142

    
143
        test 'it searches Docker Hub and the ComputeResource' do
144
          compute_resource.expects(:image).with(docker_image)
145
            .returns(term)
146
          compute_resource.expects(:tags_for_local_image)
147
            .returns(tags)
148
          docker_hub.expects(:tags).returns([])
149

    
150
          xhr :get, :search_repository,
151
            { registry: search_type, search: term, tags: 'true',
152
              id: compute_resource }, set_session_user
153
        end
154
      end
155

    
156
      context 'with a External Registry tab request' do
157
        let(:search_type) { 'registry' }
158

    
159
        test 'it only queries the registry api' do
160
          compute_resource.expects(:image).with(docker_image).never
161
          docker_hub.expects(:tags).never
162
          registry.api.expects(:tags).with(docker_image, tag_fragment)
163
            .returns([])
164

    
165
          xhr :get, :search_repository,
166
            { registry: search_type, registry_id: registry, tags: 'true',
167
              search: term, id: compute_resource }, set_session_user
168
        end
169
      end
170
    end
171
  end
172
end