~ubuntu-branches/ubuntu/wily/puppet/wily

« back to all changes in this revision

Viewing changes to spec/unit/type/yumrepo_spec.rb

  • Committer: Package Import Robot
  • Author(s): Stig Sandbeck Mathisen
  • Date: 2014-04-17 14:50:28 UTC
  • mfrom: (3.1.59 sid)
  • Revision ID: package-import@ubuntu.com-20140417145028-j3p3dwvp8ggpzvaf
Tags: 3.5.1-1
ImportedĀ upstreamĀ releaseĀ 3.5.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#! /usr/bin/env ruby
2
 
 
3
1
require 'spec_helper'
 
2
require 'puppet'
 
3
 
 
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)
 
7
  end
 
8
end
 
9
 
 
10
shared_examples_for "a yumrepo parameter that expects a boolean parameter" do |param|
 
11
  valid_values = %w[True False 0 1 No Yes]
 
12
 
 
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
 
17
    end
 
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
 
21
    end
 
22
  end
 
23
 
 
24
  it "rejects invalid boolean values" do
 
25
    expect {
 
26
      described_class.new(:name => 'puppetlabs', param => 'flase')
 
27
    }.to raise_error(Puppet::ResourceError, /Parameter #{param} failed/)
 
28
  end
 
29
end
 
30
 
 
31
shared_examples_for "a yumrepo parameter that accepts a single URL" do |param|
 
32
  it "can accept a single URL" do
 
33
    described_class.new(
 
34
      :name => 'puppetlabs',
 
35
      param => 'http://localhost/yumrepos'
 
36
    )
 
37
  end
 
38
 
 
39
  it "fails if an invalid URL is provided" do
 
40
    expect {
 
41
      described_class.new(
 
42
        :name => 'puppetlabs',
 
43
        param => "that's no URL!"
 
44
      )
 
45
    }.to raise_error(Puppet::ResourceError, /Parameter #{param} failed/)
 
46
  end
 
47
 
 
48
  it "fails if a valid URL uses an invalid URI scheme" do
 
49
    expect {
 
50
      described_class.new(
 
51
        :name => 'puppetlabs',
 
52
        param => 'ldap://localhost/yumrepos'
 
53
      )
 
54
    }.to raise_error(Puppet::ResourceError, /Parameter #{param} failed/)
 
55
  end
 
56
end
 
57
 
 
58
shared_examples_for "a yumrepo parameter that accepts multiple URLs" do |param|
 
59
  it "can accept multiple URLs" do
 
60
    described_class.new(
 
61
      :name => 'puppetlabs',
 
62
      param => 'http://localhost/yumrepos http://localhost/more-yumrepos'
 
63
    )
 
64
  end
 
65
 
 
66
  it "fails if multiple URLs are given and one is invalid" do
 
67
    expect {
 
68
      described_class.new(
 
69
        :name => 'puppetlabs',
 
70
        param => "http://localhost/yumrepos That's no URL!"
 
71
      )
 
72
    }.to raise_error(Puppet::ResourceError, /Parameter #{param} failed/)
 
73
  end
 
74
end
4
75
 
5
76
describe Puppet::Type.type(:yumrepo) do
6
 
  include PuppetSpec::Files
7
 
 
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"
11
 
    end
12
 
 
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
18
 
      end
19
 
    end
20
 
  end
21
 
 
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)
26
 
      end
27
 
    end
28
 
 
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
32
 
      end
33
 
    end
34
 
 
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
38
 
      end
39
 
    end
40
 
 
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
43
 
    end
44
 
 
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
47
 
    end
48
 
 
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
51
 
    end
52
 
 
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"
56
 
    end
57
 
  end
58
 
 
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={})
62
 
      hash[:name] = name
63
 
      Puppet::Type.type(:yumrepo).new(hash)
64
 
    end
65
 
 
66
 
    def all_sections(inifile)
67
 
      sections = []
68
 
      inifile.each_section { |section| sections << section.name }
69
 
      sections.sort
70
 
    end
71
 
 
72
 
    def create_data_files()
73
 
      File.open(File.join(@yumdir, "fedora.repo"), "w") do |f|
74
 
        f.print(FEDORA_REPO_FILE)
75
 
      end
76
 
 
77
 
      File.open(File.join(@yumdir, "fedora-devel.repo"), "w") do |f|
78
 
        f.print(FEDORA_DEVEL_REPO_FILE)
79
 
      end
80
 
    end
81
 
 
82
 
    before(:each) do
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"
87
 
      end
88
 
      Puppet::Type.type(:yumrepo).yumconf = @yumconf
89
 
 
90
 
      # It needs to be reset each time, otherwise the cache is used.
91
 
      Puppet::Type.type(:yumrepo).inifile = nil
92
 
    end
93
 
 
94
 
    it "should be able to create a valid config file" do
95
 
      values = {
96
 
          :descr => "Fedora Core $releasever - $basearch - Base",
97
 
          :baseurl => "http://example.com/yum/$releasever/$basearch/os/",
98
 
          :enabled => "1",
99
 
          :gpgcheck => "1",
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"
105
 
      }
106
 
      repo = make_repo("base", values)
107
 
 
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)
112
 
      catalog.apply
113
 
 
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
119
 
    end
120
 
 
121
 
    # Modify one existing section
122
 
    it "should be able to modify an existing config file" do
123
 
      create_data_files
124
 
 
125
 
      devel = make_repo("development", { :descr => "New description" })
126
 
      current_values = devel.retrieve
127
 
 
128
 
      devel[:name].should == "development"
129
 
      current_values[devel.property(:descr)].should == 'Fedora Core $releasever - Development Tree'
130
 
      devel[:descr].should == 'New description'
131
 
 
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)
136
 
      catalog.apply
137
 
 
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']
143
 
    end
144
 
 
145
 
    # Delete mirrorlist by setting it to :absent and enable baseurl
146
 
    it "should support 'absent' value" do
147
 
      create_data_files
148
 
 
149
 
      baseurl = 'http://example.com/'
150
 
 
151
 
      devel = make_repo(
152
 
          "development",
153
 
          { :mirrorlist => 'absent',
154
 
 
155
 
            :baseurl => baseurl })
156
 
      devel.retrieve
157
 
 
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)
162
 
      catalog.apply
163
 
 
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]
 
79
  end
 
80
 
 
81
  describe "validating" do
 
82
 
 
83
    describe "name" do
 
84
      it "is a valid parameter" do
 
85
        instance = described_class.new(:name => 'puppetlabs')
 
86
        expect(instance.name).to eq 'puppetlabs'
 
87
      end
 
88
    end
 
89
 
 
90
    describe "target" do
 
91
      it_behaves_like "a yumrepo parameter that can be absent", :target
 
92
    end
 
93
 
 
94
    describe "descr" do
 
95
      it_behaves_like "a yumrepo parameter that can be absent", :descr
 
96
    end
 
97
 
 
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
 
101
    end
 
102
 
 
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
 
107
    end
 
108
 
 
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
 
112
    end
 
113
 
 
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
 
117
    end
 
118
 
 
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
 
122
    end
 
123
 
 
124
    describe "gpgkey" do
 
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
 
128
    end
 
129
 
 
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
 
133
    end
 
134
 
 
135
    describe "exclude" do
 
136
      it_behaves_like "a yumrepo parameter that can be absent", :exclude
 
137
    end
 
138
 
 
139
    describe "includepkgs" do
 
140
      it_behaves_like "a yumrepo parameter that can be absent", :includepkgs
 
141
    end
 
142
 
 
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
 
146
    end
 
147
 
 
148
    describe "failovermethod" do
 
149
 
 
150
      %w[roundrobin priority].each do |value|
 
151
        it "accepts a value of #{value}" do
 
152
          described_class.new(:name => "puppetlabs", :failovermethod => value)
 
153
        end
 
154
      end
 
155
 
 
156
      it "raises an error if an invalid value is given" do
 
157
        expect {
 
158
          described_class.new(:name => "puppetlabs", :failovermethod => "notavalidvalue")
 
159
        }.to raise_error(Puppet::ResourceError, /Parameter failovermethod failed/)
 
160
      end
 
161
 
 
162
      it_behaves_like "a yumrepo parameter that can be absent", :failovermethod
 
163
    end
 
164
 
 
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
 
168
    end
 
169
 
 
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)
 
174
        end
 
175
      end
 
176
 
 
177
      it "rejects invalid values" do
 
178
        expect {
 
179
          described_class.new(:name => 'puppetlabs', :http_caching => 'yes')
 
180
        }.to raise_error(Puppet::ResourceError, /Parameter http_caching failed/)
 
181
      end
 
182
 
 
183
      it_behaves_like "a yumrepo parameter that can be absent", :http_caching
 
184
    end
 
185
 
 
186
    describe "timeout" do
 
187
      it_behaves_like "a yumrepo parameter that can be absent", :timeout
 
188
    end
 
189
 
 
190
    describe "metadata_expire" do
 
191
      it_behaves_like "a yumrepo parameter that can be absent", :metadata_expire
 
192
    end
 
193
 
 
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
 
197
    end
 
198
 
 
199
    describe "priority" do
 
200
      it_behaves_like "a yumrepo parameter that can be absent", :priority
 
201
    end
 
202
 
 
203
    describe "proxy" do
 
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
 
206
    end
 
207
 
 
208
    describe "proxy_username" do
 
209
      it_behaves_like "a yumrepo parameter that can be absent", :proxy_username
 
210
    end
 
211
 
 
212
    describe "proxy_password" do
 
213
      it_behaves_like "a yumrepo parameter that can be absent", :proxy_password
 
214
    end
 
215
 
 
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
 
219
    end
 
220
 
 
221
    describe "sslcacert" do
 
222
      it_behaves_like "a yumrepo parameter that can be absent", :sslcacert
 
223
    end
 
224
 
 
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
 
228
    end
 
229
 
 
230
    describe "sslclientcert" do
 
231
      it_behaves_like "a yumrepo parameter that can be absent", :sslclientcert
 
232
    end
 
233
 
 
234
    describe "sslclientkey" do
 
235
      it_behaves_like "a yumrepo parameter that can be absent", :sslclientkey
 
236
    end
 
237
 
 
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
168
241
    end
169
242
  end
170
243
end
171
 
 
172
 
EXPECTED_CONTENTS_FOR_CREATED_FILE = <<'EOF'
173
 
[base]
174
 
name=Fedora Core $releasever - $basearch - Base
175
 
baseurl=http://example.com/yum/$releasever/$basearch/os/
176
 
enabled=1
177
 
gpgcheck=1
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
182
 
EOF
183
 
 
184
 
FEDORA_REPO_FILE = <<END
185
 
[base]
186
 
name=Fedora Core $releasever - $basearch - Base
187
 
mirrorlist=http://fedora.redhat.com/download/mirrors/fedora-core-$releasever
188
 
enabled=1
189
 
gpgcheck=1
190
 
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora
191
 
exclude=foo
192
 
  bar
193
 
  baz
194
 
END
195
 
 
196
 
FEDORA_DEVEL_REPO_FILE = <<END
197
 
[development]
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.
201
 
#
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.
206
 
#
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.
210
 
#
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.
214
 
#
215
 
# Reportable issues should be filed at bugzilla.redhat.com
216
 
# Product: Fedora Core
217
 
# Version: devel
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
221
 
enabled=0
222
 
gpgcheck=0
223
 
END