summaryrefslogtreecommitdiff
path: root/spec/unit/type/yumrepo_spec.rb
blob: 2246b7274a0f358e52195901201803c79776452a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
require 'spec_helper'
require 'puppet'

shared_examples_for "a yumrepo parameter that can be absent" do |param|
  it "can be set as :absent" do
    described_class.new(:name => 'puppetlabs', param => :absent)
  end
  it "can be set as \"absent\"" do
    described_class.new(:name => 'puppetlabs', param => 'absent')
  end
end

shared_examples_for "a yumrepo parameter that expects a natural value" do |param|
  it "accepts a valid positive integer" do
    instance = described_class.new(:name => 'puppetlabs', param => '12')
    expect(instance[param]).to eq '12'
  end
  it "rejects invalid negative integer" do
    expect {
      described_class.new(
        :name => 'puppetlabs',
        param => '-12'
      )
    }.to raise_error(Puppet::ResourceError, /Parameter #{param} failed/)
  end
  it "rejects invalid non-integer" do
    expect {
      described_class.new(
        :name => 'puppetlabs',
        param => 'I\'m a six'
      )
    }.to raise_error(Puppet::ResourceError, /Parameter #{param} failed/)
  end
  it "rejects invalid string with integers inside" do
    expect {
      described_class.new(
        :name => 'puppetlabs',
        param => 'I\'m a 6'
      )
    }.to raise_error(Puppet::ResourceError, /Parameter #{param} failed/)
  end
end

shared_examples_for "a yumrepo parameter that expects a boolean parameter" do |param|
  valid_values = %w[True False 0 1 No Yes]

  valid_values.each do |value|
    it "accepts a valid value of #{value}" do
      instance = described_class.new(:name => 'puppetlabs', param => value)
      expect(instance[param]).to eq value
    end
    it "accepts #{value} downcased to #{value.downcase}" do
      instance = described_class.new(:name => 'puppetlabs', param => value.downcase)
      expect(instance[param]).to eq value.downcase
    end
    it "fails on valid value #{value} contained in another value" do
        expect {
          described_class.new(
            :name => 'puppetlabs',
            param => "bla#{value}bla"
          )
        }.to raise_error(Puppet::ResourceError, /Parameter #{param} failed/)
    end
  end

  it "rejects invalid boolean values" do
    expect {
      described_class.new(:name => 'puppetlabs', param => 'flase')
    }.to raise_error(Puppet::ResourceError, /Parameter #{param} failed/)
  end
end

shared_examples_for "a yumrepo parameter that accepts a single URL" do |param|
  it "can accept a single URL" do
    described_class.new(
      :name => 'puppetlabs',
      param => 'http://localhost/yumrepos'
    )
  end

  it "fails if an invalid URL is provided" do
    expect {
      described_class.new(
        :name => 'puppetlabs',
        param => "that's no URL!"
      )
    }.to raise_error(Puppet::ResourceError, /Parameter #{param} failed/)
  end

  it "fails if a valid URL uses an invalid URI scheme" do
    expect {
      described_class.new(
        :name => 'puppetlabs',
        param => 'ldap://localhost/yumrepos'
      )
    }.to raise_error(Puppet::ResourceError, /Parameter #{param} failed/)
  end
end

shared_examples_for "a yumrepo parameter that accepts multiple URLs" do |param|
  it "can accept multiple URLs" do
    described_class.new(
      :name => 'puppetlabs',
      param => 'http://localhost/yumrepos http://localhost/more-yumrepos'
    )
  end

  it "fails if multiple URLs are given and one is invalid" do
    expect {
      described_class.new(
        :name => 'puppetlabs',
        param => "http://localhost/yumrepos That's no URL!"
      )
    }.to raise_error(Puppet::ResourceError, /Parameter #{param} failed/)
  end
end

shared_examples_for "a yumrepo parameter that accepts kMG units" do |param|
  %w[k M G].each do |unit|
    it "can accept an integer with #{unit} units" do
      described_class.new(
        :name => 'puppetlabs',
        param => "123#{unit}"
      )
    end
  end

  it "fails if wrong unit passed" do
    expect {
      described_class.new(
        :name => 'puppetlabs',
        param => '123J'
      )
    }.to raise_error(Puppet::ResourceError, /Parameter #{param} failed/)
  end
end

describe Puppet::Type.type(:yumrepo) do
  it "has :name as its namevar" do
    expect(described_class.key_attributes).to eq [:name]
  end

  describe "validating" do

    describe "name" do
      it "is a valid parameter" do
        instance = described_class.new(:name => 'puppetlabs')
        expect(instance.name).to eq 'puppetlabs'
      end
    end

    describe "target" do
      it_behaves_like "a yumrepo parameter that can be absent", :target
    end

    describe "descr" do
      it_behaves_like "a yumrepo parameter that can be absent", :descr
    end

    describe "mirrorlist" do
      it_behaves_like "a yumrepo parameter that accepts a single URL", :mirrorlist
      it_behaves_like "a yumrepo parameter that can be absent", :mirrorlist
    end

    describe "baseurl" do
      it_behaves_like "a yumrepo parameter that can be absent", :baseurl
      it_behaves_like "a yumrepo parameter that accepts a single URL", :baseurl
      it_behaves_like "a yumrepo parameter that accepts multiple URLs", :baseurl
    end

    describe "enabled" do
      it_behaves_like "a yumrepo parameter that expects a boolean parameter", :enabled
      it_behaves_like "a yumrepo parameter that can be absent", :enabled
    end

    describe "gpgcheck" do
      it_behaves_like "a yumrepo parameter that expects a boolean parameter", :gpgcheck
      it_behaves_like "a yumrepo parameter that can be absent", :gpgcheck
    end

    describe "repo_gpgcheck" do
      it_behaves_like "a yumrepo parameter that expects a boolean parameter", :repo_gpgcheck
      it_behaves_like "a yumrepo parameter that can be absent", :repo_gpgcheck
    end

    describe "gpgkey" do
      it_behaves_like "a yumrepo parameter that can be absent", :gpgkey
      it_behaves_like "a yumrepo parameter that accepts a single URL", :gpgkey
      it_behaves_like "a yumrepo parameter that accepts multiple URLs", :gpgkey
    end

    describe "include" do
      it_behaves_like "a yumrepo parameter that can be absent", :include
      it_behaves_like "a yumrepo parameter that accepts a single URL", :include
    end

    describe "exclude" do
      it_behaves_like "a yumrepo parameter that can be absent", :exclude
    end

    describe "includepkgs" do
      it_behaves_like "a yumrepo parameter that can be absent", :includepkgs
    end

    describe "enablegroups" do
      it_behaves_like "a yumrepo parameter that expects a boolean parameter", :enablegroups
      it_behaves_like "a yumrepo parameter that can be absent", :enablegroups
    end

    describe "failovermethod" do

      %w[roundrobin priority].each do |value|
        it "accepts a value of #{value}" do
          described_class.new(:name => "puppetlabs", :failovermethod => value)
        end
        it "fails on valid value #{value} contained in another value" do
          expect {
            described_class.new(
              :name => 'puppetlabs',
              :failovermethod => "bla#{value}bla"
            )
          }.to raise_error(Puppet::ResourceError, /Parameter failovermethod failed/)
        end
      end

      it "raises an error if an invalid value is given" do
        expect {
          described_class.new(:name => "puppetlabs", :failovermethod => "notavalidvalue")
        }.to raise_error(Puppet::ResourceError, /Parameter failovermethod failed/)
      end

      it_behaves_like "a yumrepo parameter that can be absent", :failovermethod
    end

    describe "keepalive" do
      it_behaves_like "a yumrepo parameter that expects a boolean parameter", :keepalive
      it_behaves_like "a yumrepo parameter that can be absent", :keepalive
    end

    describe "http_caching" do
      %w[packages all none].each do |value|
        it "accepts a valid value of #{value}" do
          described_class.new(:name => 'puppetlabs', :http_caching => value)
        end
        it "fails on valid value #{value} contained in another value" do
          expect {
            described_class.new(
              :name => 'puppetlabs',
              :http_caching => "bla#{value}bla"
            )
          }.to raise_error(Puppet::ResourceError, /Parameter http_caching failed/)
        end
      end

      it "rejects invalid values" do
        expect {
          described_class.new(:name => 'puppetlabs', :http_caching => 'yes')
        }.to raise_error(Puppet::ResourceError, /Parameter http_caching failed/)
      end

      it_behaves_like "a yumrepo parameter that can be absent", :http_caching
    end

    describe "timeout" do
      it_behaves_like "a yumrepo parameter that can be absent", :timeout
      it_behaves_like "a yumrepo parameter that expects a natural value", :timeout
    end

    describe "metadata_expire" do
      it_behaves_like "a yumrepo parameter that can be absent", :metadata_expire
      it_behaves_like "a yumrepo parameter that expects a natural value", :metadata_expire

      it "accepts dhm units" do
        %W[d h m].each do |unit|
          described_class.new(
            :name            => 'puppetlabs',
            :metadata_expire => "123#{unit}"
          )
        end
      end

      it "accepts never as value" do
        described_class.new(:name => 'puppetlabs', :metadata_expire => 'never')
      end
    end

    describe "protect" do
      it_behaves_like "a yumrepo parameter that expects a boolean parameter", :protect
      it_behaves_like "a yumrepo parameter that can be absent", :protect
    end

    describe "priority" do
      it_behaves_like "a yumrepo parameter that can be absent", :priority
    end

    describe "proxy" do
      it_behaves_like "a yumrepo parameter that can be absent", :proxy
      it "accepts _none_" do
        described_class.new(
          :name  => 'puppetlabs',
          :proxy => "_none_"
        )
      end
      it_behaves_like "a yumrepo parameter that accepts a single URL", :proxy
    end

    describe "proxy_username" do
      it_behaves_like "a yumrepo parameter that can be absent", :proxy_username
    end

    describe "proxy_password" do
      it_behaves_like "a yumrepo parameter that can be absent", :proxy_password
    end

    describe "s3_enabled" do
      it_behaves_like "a yumrepo parameter that expects a boolean parameter", :s3_enabled
      it_behaves_like "a yumrepo parameter that can be absent", :s3_enabled
    end

    describe "skip_if_unavailable" do
      it_behaves_like "a yumrepo parameter that expects a boolean parameter", :skip_if_unavailable
      it_behaves_like "a yumrepo parameter that can be absent", :skip_if_unavailable
    end

    describe "sslcacert" do
      it_behaves_like "a yumrepo parameter that can be absent", :sslcacert
    end

    describe "sslverify" do
      it_behaves_like "a yumrepo parameter that expects a boolean parameter", :sslverify
      it_behaves_like "a yumrepo parameter that can be absent", :sslverify
    end

    describe "sslclientcert" do
      it_behaves_like "a yumrepo parameter that can be absent", :sslclientcert
    end

    describe "sslclientkey" do
      it_behaves_like "a yumrepo parameter that can be absent", :sslclientkey
    end

    describe "metalink" do
      it_behaves_like "a yumrepo parameter that can be absent", :metalink
      it_behaves_like "a yumrepo parameter that accepts a single URL", :metalink
    end


    describe "cost" do
      it_behaves_like "a yumrepo parameter that can be absent", :cost
      it_behaves_like "a yumrepo parameter that expects a natural value", :cost
    end

    describe "throttle" do
      it_behaves_like "a yumrepo parameter that can be absent", :throttle
      it_behaves_like "a yumrepo parameter that expects a natural value", :throttle
      it_behaves_like "a yumrepo parameter that accepts kMG units", :throttle

      it "accepts percentage as unit" do
        described_class.new(
          :name     => 'puppetlabs',
          :throttle => '123%'
        )
      end
    end

    describe "bandwidth" do
      it_behaves_like "a yumrepo parameter that can be absent", :bandwidth
      it_behaves_like "a yumrepo parameter that expects a natural value", :bandwidth
      it_behaves_like "a yumrepo parameter that accepts kMG units", :bandwidth
    end

    describe "gpgcakey" do
      it_behaves_like "a yumrepo parameter that can be absent", :gpgcakey
      it_behaves_like "a yumrepo parameter that accepts a single URL", :gpgcakey
    end

    describe "retries" do
      it_behaves_like "a yumrepo parameter that can be absent", :retries
      it_behaves_like "a yumrepo parameter that expects a natural value", :retries
    end

    describe "mirrorlist_expire" do
      it_behaves_like "a yumrepo parameter that can be absent", :mirrorlist_expire
      it_behaves_like "a yumrepo parameter that expects a natural value", :mirrorlist_expire
    end
  end
end