Project

General

Profile

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

foreman_docker / test / units / registry_api_test.rb @ 27a7d2c4

1
require 'test_plugin_helper'
2

    
3
class RegistryApiTest < ActiveSupport::TestCase
4
  let(:url) { 'http://dockerregistry.com:5000' }
5
  subject { Service::RegistryApi.new(url: url) }
6

    
7
  describe '#connection' do
8
    test 'returns a Docker::Connection' do
9
      assert_equal Docker::Connection, subject.connection.class
10
    end
11

    
12
    test 'the connection has the same url' do
13
      assert_equal url, subject.connection.url
14
    end
15

    
16
    context 'authentication is set' do
17
      let(:user) { 'username' }
18
      let(:password) { 'secretpassword' }
19

    
20
      subject do
21
        Service::RegistryApi.new(
22
          :url => url,
23
          :password => password,
24
          :user => user
25
        )
26
      end
27

    
28
      test 'it sets the same user and password' do
29
        assert_equal user, subject.connection.options[:user]
30
        assert_equal password, subject.connection.options[:password]
31
      end
32
    end
33
  end
34

    
35
  describe '#get' do
36
    let(:path) { '/v1/search' }
37
    let(:json) { '{}' }
38

    
39
    test 'calls get on #connection' do
40
      subject.connection
41
             .expects(:get).at_least_once
42
             .returns(json)
43

    
44
      subject.get(path)
45
    end
46

    
47
    test 'returns a parsed json' do
48
      subject.connection.stubs(:get).returns(json)
49
      assert_equal JSON.parse(json), subject.get(path)
50
    end
51

    
52
    # Docker::Connection is used and meant for the Docker,
53
    # not the Registry API therefore it is required
54
    # to override the path via options
55
    test 'sets the path as an option not param for Docker::Connection#get' do
56
      subject.connection.stubs(:get) do |path_param, _, options|
57
        refute_equal path, path_param
58
        assert_equal path, options[:path]
59
      end.returns(json)
60

    
61
      subject.get(path)
62
    end
63

    
64
    # Docker Hub will return a 503 when a 'Host' header includes a port.
65
    # Omitting default ports (an Excon option) solves the issue
66
    test 'sets omit_default_port to true' do
67
      subject.connection.stubs(:get) do |_, _, options|
68
        assert options[:omit_default_port]
69
      end.returns(json)
70

    
71
      subject.get(path)
72
    end
73

    
74
    test 'returns the response raw body if it is not JSON' do
75
      response = 'This is not JSON'
76
      subject.connection.stubs(:get)
77
             .returns(response)
78
      assert_equal response, subject.get('/v1/')
79
    end
80
  end
81

    
82
  describe '#search' do
83
    let(:path) { '/v1/search' }
84
    let(:query) { 'centos' }
85

    
86
    test "calls #get with path and query" do
87
      subject.expects(:get).with(path, q: query) do |path_param, params|
88
        assert_equal path, path_param
89
        assert_equal query, params[:q]
90
      end.returns({})
91

    
92
      subject.search(query)
93
    end
94

    
95
    test "falls back to #catalog if #get fails" do
96
      subject.expects(:catalog).with(query)
97

    
98
      subject.expects(:get).with(path, q: query)
99
             .raises('Error')
100

    
101
      subject.search(query)
102
    end
103
  end
104

    
105
  describe '#catalog' do
106
    let(:path) { '/v2/_catalog' }
107
    let(:query) { 'centos' }
108
    let(:catalog) { { 'repositories' => ['centos', 'fedora'] } }
109

    
110
    setup do
111
      subject.stubs(:get).returns(catalog)
112
    end
113

    
114
    test "calls #get with path" do
115
      subject.expects(:get).with(path)
116
             .returns(catalog)
117

    
118
      subject.catalog(query)
119
    end
120

    
121
    test 'returns {"name" => value} pairs' do
122
      result = subject.catalog(query)
123
      assert_equal({ "name" => query }, result.first)
124
    end
125

    
126
    test 'only give back matching results' do
127
      result = subject.catalog('fedora')
128
      assert_match(/^fedora/, result.first['name'])
129
    end
130
  end
131

    
132
  describe '#tags' do
133
    let(:query) { 'alpine' }
134
    let(:path) { "/v1/repositories/#{query}/tags" }
135

    
136
    test "calls #get with path" do
137
      subject.expects(:get).with(path)
138
      subject.tags(query)
139
    end
140

    
141
    test "falls back to #tags_v2 if #get fails" do
142
      subject.expects(:get).with(path)
143
             .raises('Error')
144

    
145
      subject.expects(:tags_v2).with(query)
146
      subject.tags(query)
147
    end
148

    
149
    # https://registry.access.redhat.com returns a hash not an array
150
    test 'handles a hash response correctly' do
151
      tags_hash = {
152
        "7.0-21" => "e1f5733f050b2488a17b7630cb038bfbea8b7bdfa9bdfb99e63a33117e28d02f",
153
        "7.0-23" => "bef54b8f8a2fdd221734f1da404d4c0a7d07ee9169b1443a338ab54236c8c91a",
154
        "7.0-27" => "8e6704f39a3d4a0c82ec7262ad683a9d1d9a281e3c1ebbb64c045b9af39b3940"
155
      }
156
      subject.expects(:get).with(path)
157
             .returns(tags_hash)
158
      assert_equal '7.0-21', subject.tags(query).first['name']
159
    end
160
  end
161

    
162
  describe '#tags for API v2' do
163
    let(:query) { 'debian' }
164
    let(:v1_path) { "/v1/repositories/#{query}/tags" }
165
    let(:path) { "/v2/#{query}/tags/list" }
166
    let(:tags) { { 'tags' => ['jessy', 'woody'] } }
167

    
168
    setup do
169
      subject.stubs(:get).with(v1_path)
170
             .raises('404 Not found')
171
    end
172

    
173
    test 'calls #get with path' do
174
      subject.expects(:get).with(path)
175
             .returns(tags)
176
      subject.tags(query)
177
    end
178

    
179
    test 'returns {"name" => value } pairs ' do
180
      subject.stubs(:get).with(path).returns(tags)
181
      result = subject.tags(query)
182
      assert_equal tags['tags'].first, result.first['name']
183
    end
184
  end
185

    
186
  describe '#ok?' do
187
    test 'calls the API via #get with /v1/' do
188
      subject.connection.expects(:get)
189
             .with('/', nil, Service::RegistryApi::DEFAULTS[:connection].merge(path: '/v1/'))
190
             .returns('Docker Registry API')
191
      assert subject.ok?
192
    end
193

    
194
    test 'calls #get with /v2/ if /v1/fails' do
195
      subject.stubs(:get).with('/v1/')
196
             .raises('404 page not found')
197
      subject.expects(:get).with('/v2/')
198
             .returns({})
199
      assert subject.ok?
200
    end
201
  end
202

    
203
  describe '.docker_hub' do
204
    subject { Service::RegistryApi }
205

    
206
    test 'returns an instance for Docker Hub' do
207
      result = subject.docker_hub
208
      assert_equal Service::RegistryApi::DOCKER_HUB, result.url
209
    end
210
  end
211
end