Project

General

Profile

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

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

1
require 'test_plugin_helper'
2

    
3
class ImageSearchServiceTest < ActiveSupport::TestCase
4
  let(:compute_resource) { FactoryGirl.create(:docker_cr) }
5
  let(:registry) { FactoryGirl.create(:docker_registry).api }
6
  let(:term) { 'centos' }
7
  let(:query) { { term: term, tags: 'false' } }
8

    
9
  subject { ForemanDocker::ImageSearch.new(compute_resource, registry) }
10

    
11
  setup do
12
    stub_registry_api
13
  end
14

    
15
  describe '#add_source' do
16
    setup do
17
      subject.instance_variable_set(:@sources, {})
18
    end
19

    
20
    test 'adds a compute resource to @sources[:compute_resource]' do
21
      subject.add_source(compute_resource)
22
      assert_equal compute_resource,
23
                   subject.instance_variable_get(:@sources)[:compute_resource].first
24
    end
25

    
26
    test 'adds a registry to @sources[:registry]' do
27
      subject.add_source(registry)
28
      assert_equal registry,
29
                   subject.instance_variable_get(:@sources)[:registry].first
30
    end
31
  end
32

    
33
  describe '#remove_source' do
34
    test 'removes a registry source from @sources' do
35
      refute subject.instance_variable_get(:@sources)[:registry].empty?
36
      subject.remove_source(registry)
37
      assert subject.instance_variable_get(:@sources)[:registry].empty?
38
    end
39

    
40
    test 'removes a compute_resource source from @sources' do
41
      refute subject.instance_variable_get(:@sources)[:compute_resource].empty?
42
      subject.remove_source(compute_resource)
43
      assert subject.instance_variable_get(:@sources)[:compute_resource].empty?
44
    end
45
  end
46

    
47
  describe '#search' do
48
    test 'returns {"name" => value } pairs' do
49
      return_result = Hash.new
50
      return_result.stubs(:info).returns({ 'RepoTags' => ["#{term}:latest"]})
51
      compute_resource.stubs(:local_images).with(term)
52
        .returns([return_result])
53
      result = subject.search(query)
54
      assert_equal({"name" => term}, result.first)
55
    end
56

    
57
    context 'tags is false' do
58
      test 'calls #images with term as query' do
59
        subject.expects(:images).with(term)
60
          .returns([])
61
        subject.search(query)
62
      end
63
    end
64

    
65
    context 'tags is "true"' do
66
      setup do
67
        query[:tags] = 'true'
68
      end
69

    
70
      test 'calls #tags with term as query' do
71
        subject.expects(:tags).with(term)
72
          .returns([])
73
        subject.search(query)
74
      end
75
    end
76
  end
77

    
78
  describe '#images' do
79
    context 'a compute_resource set' do
80
      test 'calls #search_compute_resource with term as query' do
81
        subject.expects(:compute_resource_search).with(compute_resource, term)
82
          .returns([])
83
        subject.images(term)
84
      end
85
    end
86

    
87
    context 'no compute_resource is set' do
88
      setup do
89
        subject.remove_source(compute_resource)
90
      end
91

    
92
      test 'does not call #search_compute_resource' do
93
        subject.expects(:compute_resource_search).with(compute_resource, term)
94
          .never
95
        subject.images(term)
96
      end
97
    end
98

    
99
    context 'a registry is set' do
100
      test 'calls #search_registry' do
101
        subject.expects(:registry_search).with(registry, term)
102
          .returns([])
103
        subject.images(term)
104
      end
105
    end
106

    
107
    context 'no registry is set' do
108
      setup do
109
        subject.remove_source(registry)
110
      end
111

    
112
      test 'does not call #search_registry' do
113
        subject.expects(:registry_search).with(registry, term)
114
          .never
115
        subject.images(term)
116
      end
117
    end
118
  end
119

    
120
  describe '#tags' do
121
    let(:tag) { 'latest' }
122
    let(:query) { "#{term}:#{tag}" }
123

    
124
    context 'a compute_resource set' do
125
      test 'calls #compute_resource with image name and tag' do
126
        subject.expects(:compute_resource_tags).with(compute_resource, term, tag)
127
          .returns([])
128
        subject.tags(query)
129
      end
130
    end
131

    
132
    context 'no compute_resource is set' do
133
      setup do
134
        subject.remove_source(compute_resource)
135
      end
136

    
137
      test 'does not call #search_compute_resource' do
138
        subject.expects(:compute_resource_tags).with(compute_resource, term, tag)
139
          .never
140
        subject.tags(query)
141
      end
142
    end
143

    
144
    context 'a registry is set' do
145
      setup do
146
        subject.remove_source(compute_resource)
147
      end
148

    
149
      test 'calls #registry_tags with image name and tag' do
150
        subject.expects(:registry_tags).with(registry, term, tag)
151
          .returns([])
152
        subject.tags(query)
153
      end
154
    end
155

    
156
    context 'no registry is set' do
157
      setup do
158
        subject.remove_source(registry)
159
      end
160

    
161
      test 'does not call #registry_tags' do
162
        subject.expects(:registry_search).with(registry, term, tag)
163
          .never
164
        subject.images(query)
165
      end
166
    end
167
  end
168

    
169
  describe '#available?' do
170
    test 'calls #tags with query' do
171
      subject.expects(:tags).with(query).once
172
        .returns([])
173
      subject.available?(query)
174
    end
175

    
176
    test 'returns true if any matching image and tag is found' do
177
      subject.stubs(:tags).with(query)
178
        .returns([{ 'name' => "#{term}:latest" }])
179
      subject.available?(query)
180
    end
181

    
182
    test 'returns false if none are found' do
183
      subject.stubs(:tags).with(query)
184
        .returns([])
185
      subject.available?(query)
186
    end
187
  end
188
end