3
1
require 'spec_helper'
4
shared_examples_for "a yumrepo parameter that can be absent" do |param|
5
it "can be set as :absent" do
6
described_class.new(:name => 'puppetlabs', param => :absent)
10
shared_examples_for "a yumrepo parameter that expects a boolean parameter" do |param|
11
valid_values = %w[True False 0 1 No Yes]
13
valid_values.each do |value|
14
it "accepts a valid value of #{value}" do
15
instance = described_class.new(:name => 'puppetlabs', param => value)
16
expect(instance[param]).to eq value
18
it "accepts #{value} downcased to #{value.downcase}" do
19
instance = described_class.new(:name => 'puppetlabs', param => value.downcase)
20
expect(instance[param]).to eq value.downcase
24
it "rejects invalid boolean values" do
26
described_class.new(:name => 'puppetlabs', param => 'flase')
27
}.to raise_error(Puppet::ResourceError, /Parameter #{param} failed/)
31
shared_examples_for "a yumrepo parameter that accepts a single URL" do |param|
32
it "can accept a single URL" do
34
:name => 'puppetlabs',
35
param => 'http://localhost/yumrepos'
39
it "fails if an invalid URL is provided" do
42
:name => 'puppetlabs',
43
param => "that's no URL!"
45
}.to raise_error(Puppet::ResourceError, /Parameter #{param} failed/)
48
it "fails if a valid URL uses an invalid URI scheme" do
51
:name => 'puppetlabs',
52
param => 'ldap://localhost/yumrepos'
54
}.to raise_error(Puppet::ResourceError, /Parameter #{param} failed/)
58
shared_examples_for "a yumrepo parameter that accepts multiple URLs" do |param|
59
it "can accept multiple URLs" do
61
:name => 'puppetlabs',
62
param => 'http://localhost/yumrepos http://localhost/more-yumrepos'
66
it "fails if multiple URLs are given and one is invalid" do
69
:name => 'puppetlabs',
70
param => "http://localhost/yumrepos That's no URL!"
72
}.to raise_error(Puppet::ResourceError, /Parameter #{param} failed/)
5
76
describe Puppet::Type.type(:yumrepo) do
6
include PuppetSpec::Files
8
describe "When validating attributes" do
9
it "should have a 'name' parameter'" do
10
Puppet::Type.type(:yumrepo).new(:name => "puppetlabs")[:name].should == "puppetlabs"
13
[:baseurl, :cost, :descr, :enabled, :enablegroups, :exclude, :failovermethod, :gpgcheck, :gpgkey, :http_caching,
14
:include, :includepkgs, :keepalive, :metadata_expire, :mirrorlist, :priority, :protect, :proxy, :proxy_username, :proxy_password, :timeout,
15
:sslcacert, :sslverify, :sslclientcert, :sslclientkey, :s3_enabled].each do |param|
16
it "should have a '#{param}' parameter" do
17
Puppet::Type.type(:yumrepo).attrtype(param).should == :property
22
describe "When validating attribute values" do
23
[:cost, :enabled, :enablegroups, :failovermethod, :gpgcheck, :http_caching, :keepalive, :metadata_expire, :priority, :protect, :timeout].each do |param|
24
it "should support :absent as a value to '#{param}' parameter" do
25
Puppet::Type.type(:yumrepo).new(:name => "puppetlabs.repo", param => :absent)
29
[:cost, :enabled, :enablegroups, :gpgcheck, :keepalive, :metadata_expire, :priority, :protect, :timeout].each do |param|
30
it "should fail if '#{param}' is not a number" do
31
lambda { Puppet::Type.type(:yumrepo).new(:name => "puppetlabs", param => "notanumber") }.should raise_error
35
[:enabled, :enabledgroups, :gpgcheck, :keepalive, :protect, :s3_enabled].each do |param|
36
it "should fail if '#{param}' does not have one of the following values (0|1)" do
37
lambda { Puppet::Type.type(:yumrepo).new(:name => "puppetlabs", param => "2") }.should raise_error
41
it "should fail if 'failovermethod' does not have one of the following values (roundrobin|priority)" do
42
lambda { Puppet::Type.type(:yumrepo).new(:name => "puppetlabs", :failovermethod => "notavalidvalue") }.should raise_error
45
it "should fail if 'http_caching' does not have one of the following values (packages|all|none)" do
46
lambda { Puppet::Type.type(:yumrepo).new(:name => "puppetlabs", :http_caching => "notavalidvalue") }.should raise_error
49
it "should fail if 'sslverify' does not have one of the following values (True|False)" do
50
lambda { Puppet::Type.type(:yumrepo).new(:name => "puppetlabs", :sslverify => "notavalidvalue") }.should raise_error
53
it "should succeed if 'sslverify' has one of the following values (True|False)" do
54
Puppet::Type.type(:yumrepo).new(:name => "puppetlabs", :sslverify => "True")[:sslverify].should == "True"
55
Puppet::Type.type(:yumrepo).new(:name => "puppetlabs", :sslverify => "False")[:sslverify].should == "False"
59
# these tests were ported from the old spec/unit/type/yumrepo_spec.rb, pretty much verbatim
60
describe "When manipulating config file" do
61
def make_repo(name, hash={})
63
Puppet::Type.type(:yumrepo).new(hash)
66
def all_sections(inifile)
68
inifile.each_section { |section| sections << section.name }
72
def create_data_files()
73
File.open(File.join(@yumdir, "fedora.repo"), "w") do |f|
74
f.print(FEDORA_REPO_FILE)
77
File.open(File.join(@yumdir, "fedora-devel.repo"), "w") do |f|
78
f.print(FEDORA_DEVEL_REPO_FILE)
83
@yumdir = tmpdir("yumrepo_spec_tmpdir")
84
@yumconf = File.join(@yumdir, "yum.conf")
85
File.open(@yumconf, "w") do |f|
86
f.print "[main]\nreposdir=#{@yumdir} /no/such/dir\n"
88
Puppet::Type.type(:yumrepo).yumconf = @yumconf
90
# It needs to be reset each time, otherwise the cache is used.
91
Puppet::Type.type(:yumrepo).inifile = nil
94
it "should be able to create a valid config file" do
96
:descr => "Fedora Core $releasever - $basearch - Base",
97
:baseurl => "http://example.com/yum/$releasever/$basearch/os/",
100
:includepkgs => "absent",
101
:gpgkey => "file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora",
102
:proxy => "http://proxy.example.com:80/",
103
:proxy_username => "username",
104
:proxy_password => "password"
106
repo = make_repo("base", values)
108
catalog = Puppet::Resource::Catalog.new
109
# Stop Puppet from doing a bunch of magic; might want to think about a util for specs that handles this
110
catalog.host_config = false
111
catalog.add_resource(repo)
114
inifile = Puppet::Type.type(:yumrepo).read
115
sections = all_sections(inifile)
116
sections.should == ['base', 'main']
117
text = inifile["base"].format
118
text.should == EXPECTED_CONTENTS_FOR_CREATED_FILE
121
# Modify one existing section
122
it "should be able to modify an existing config file" do
125
devel = make_repo("development", { :descr => "New description" })
126
current_values = devel.retrieve
128
devel[:name].should == "development"
129
current_values[devel.property(:descr)].should == 'Fedora Core $releasever - Development Tree'
130
devel[:descr].should == 'New description'
132
catalog = Puppet::Resource::Catalog.new
133
# Stop Puppet from doing a bunch of magic; might want to think about a util for specs that handles this
134
catalog.host_config = false
135
catalog.add_resource(devel)
138
inifile = Puppet::Type.type(:yumrepo).read
139
inifile['development']['name'].should == 'New description'
140
inifile['base']['name'].should == 'Fedora Core $releasever - $basearch - Base'
141
inifile['base']['exclude'].should == "foo\n bar\n baz"
142
all_sections(inifile).should == ['base', 'development', 'main']
145
# Delete mirrorlist by setting it to :absent and enable baseurl
146
it "should support 'absent' value" do
149
baseurl = 'http://example.com/'
153
{ :mirrorlist => 'absent',
155
:baseurl => baseurl })
158
catalog = Puppet::Resource::Catalog.new
159
# Stop Puppet from doing a bunch of magic; might want to think about a util for specs that handles this
160
catalog.host_config = false
161
catalog.add_resource(devel)
164
inifile = Puppet::Type.type(:yumrepo).read
165
sec = inifile["development"]
166
sec["mirrorlist"].should == nil
167
sec["baseurl"].should == baseurl
77
it "has :name as its namevar" do
78
expect(described_class.key_attributes).to eq [:name]
81
describe "validating" do
84
it "is a valid parameter" do
85
instance = described_class.new(:name => 'puppetlabs')
86
expect(instance.name).to eq 'puppetlabs'
91
it_behaves_like "a yumrepo parameter that can be absent", :target
95
it_behaves_like "a yumrepo parameter that can be absent", :descr
98
describe "mirrorlist" do
99
it_behaves_like "a yumrepo parameter that accepts a single URL", :mirrorlist
100
it_behaves_like "a yumrepo parameter that can be absent", :mirrorlist
103
describe "baseurl" do
104
it_behaves_like "a yumrepo parameter that can be absent", :baseurl
105
it_behaves_like "a yumrepo parameter that accepts a single URL", :baseurl
106
it_behaves_like "a yumrepo parameter that accepts multiple URLs", :baseurl
109
describe "enabled" do
110
it_behaves_like "a yumrepo parameter that expects a boolean parameter", :enabled
111
it_behaves_like "a yumrepo parameter that can be absent", :enabled
114
describe "gpgcheck" do
115
it_behaves_like "a yumrepo parameter that expects a boolean parameter", :gpgcheck
116
it_behaves_like "a yumrepo parameter that can be absent", :gpgcheck
119
describe "repo_gpgcheck" do
120
it_behaves_like "a yumrepo parameter that expects a boolean parameter", :repo_gpgcheck
121
it_behaves_like "a yumrepo parameter that can be absent", :repo_gpgcheck
125
it_behaves_like "a yumrepo parameter that can be absent", :gpgkey
126
it_behaves_like "a yumrepo parameter that accepts a single URL", :gpgkey
127
it_behaves_like "a yumrepo parameter that accepts multiple URLs", :gpgkey
130
describe "include" do
131
it_behaves_like "a yumrepo parameter that can be absent", :include
132
it_behaves_like "a yumrepo parameter that accepts a single URL", :include
135
describe "exclude" do
136
it_behaves_like "a yumrepo parameter that can be absent", :exclude
139
describe "includepkgs" do
140
it_behaves_like "a yumrepo parameter that can be absent", :includepkgs
143
describe "enablegroups" do
144
it_behaves_like "a yumrepo parameter that expects a boolean parameter", :enablegroups
145
it_behaves_like "a yumrepo parameter that can be absent", :enablegroups
148
describe "failovermethod" do
150
%w[roundrobin priority].each do |value|
151
it "accepts a value of #{value}" do
152
described_class.new(:name => "puppetlabs", :failovermethod => value)
156
it "raises an error if an invalid value is given" do
158
described_class.new(:name => "puppetlabs", :failovermethod => "notavalidvalue")
159
}.to raise_error(Puppet::ResourceError, /Parameter failovermethod failed/)
162
it_behaves_like "a yumrepo parameter that can be absent", :failovermethod
165
describe "keepalive" do
166
it_behaves_like "a yumrepo parameter that expects a boolean parameter", :keepalive
167
it_behaves_like "a yumrepo parameter that can be absent", :keepalive
170
describe "http_caching" do
171
%w[packages all none].each do |value|
172
it "accepts a valid value of #{value}" do
173
described_class.new(:name => 'puppetlabs', :http_caching => value)
177
it "rejects invalid values" do
179
described_class.new(:name => 'puppetlabs', :http_caching => 'yes')
180
}.to raise_error(Puppet::ResourceError, /Parameter http_caching failed/)
183
it_behaves_like "a yumrepo parameter that can be absent", :http_caching
186
describe "timeout" do
187
it_behaves_like "a yumrepo parameter that can be absent", :timeout
190
describe "metadata_expire" do
191
it_behaves_like "a yumrepo parameter that can be absent", :metadata_expire
194
describe "protect" do
195
it_behaves_like "a yumrepo parameter that expects a boolean parameter", :protect
196
it_behaves_like "a yumrepo parameter that can be absent", :protect
199
describe "priority" do
200
it_behaves_like "a yumrepo parameter that can be absent", :priority
204
it_behaves_like "a yumrepo parameter that can be absent", :proxy
205
it_behaves_like "a yumrepo parameter that accepts a single URL", :proxy
208
describe "proxy_username" do
209
it_behaves_like "a yumrepo parameter that can be absent", :proxy_username
212
describe "proxy_password" do
213
it_behaves_like "a yumrepo parameter that can be absent", :proxy_password
216
describe "s3_enabled" do
217
it_behaves_like "a yumrepo parameter that expects a boolean parameter", :s3_enabled
218
it_behaves_like "a yumrepo parameter that can be absent", :s3_enabled
221
describe "sslcacert" do
222
it_behaves_like "a yumrepo parameter that can be absent", :sslcacert
225
describe "sslverify" do
226
it_behaves_like "a yumrepo parameter that expects a boolean parameter", :sslverify
227
it_behaves_like "a yumrepo parameter that can be absent", :sslverify
230
describe "sslclientcert" do
231
it_behaves_like "a yumrepo parameter that can be absent", :sslclientcert
234
describe "sslclientkey" do
235
it_behaves_like "a yumrepo parameter that can be absent", :sslclientkey
238
describe "metalink" do
239
it_behaves_like "a yumrepo parameter that can be absent", :metalink
240
it_behaves_like "a yumrepo parameter that accepts a single URL", :metalink
172
EXPECTED_CONTENTS_FOR_CREATED_FILE = <<'EOF'
174
name=Fedora Core $releasever - $basearch - Base
175
baseurl=http://example.com/yum/$releasever/$basearch/os/
178
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora
179
proxy=http://proxy.example.com:80/
180
proxy_username=username
181
proxy_password=password
184
FEDORA_REPO_FILE = <<END
186
name=Fedora Core $releasever - $basearch - Base
187
mirrorlist=http://fedora.redhat.com/download/mirrors/fedora-core-$releasever
190
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora
196
FEDORA_DEVEL_REPO_FILE = <<END
198
# These packages are untested and still under development. This
199
# repository is used for updates to test releases, and for
200
# development of new releases.
202
# This repository can see significant daily turn over and can see major
203
# functionality changes which cause unexpected problems with other
204
# development packages. Please use these packages if you want to work
205
# with the Fedora developers by testing these new development packages.
207
# fedora-test-list@redhat.com is available as a discussion forum for
208
# testing and troubleshooting for development packages in conjunction
209
# with new test releases.
211
# fedora-devel-list@redhat.com is available as a discussion forum for
212
# testing and troubleshooting for development packages in conjunction
213
# with developing new releases.
215
# Reportable issues should be filed at bugzilla.redhat.com
216
# Product: Fedora Core
218
name=Fedora Core $releasever - Development Tree
219
#baseurl=http://download.fedora.redhat.com/pub/fedora/linux/core/development/$basearch/
220
mirrorlist=http://fedora.redhat.com/download/mirrors/fedora-core-rawhide