diff --git a/spec/unit/type/yumrepo_spec.rb b/spec/unit/type/yumrepo_spec.rb index 25dd8d833..012ad8210 100644 --- a/spec/unit/type/yumrepo_spec.rb +++ b/spec/unit/type/yumrepo_spec.rb @@ -1,78 +1,238 @@ require 'spec_helper' require 'puppet' -describe Puppet::Type.type(:yumrepo) do - let(:yumrepo) { - Puppet::Type.type(:yumrepo).new( - :name => "puppetlabs" - ) - } +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 +end - describe "When validating attributes" do - it "should have a 'name' parameter'" do - yumrepo[:name].should == "puppetlabs" - end +shared_examples_for "a yumrepo parameter that expects a boolean parameter" do |param| + valid_values = %w[True False 0 1 No Yes] - [:baseurl, :cost, :descr, :enabled, :enablegroups, :exclude, :failovermethod, - :gpgcheck, :repo_gpgcheck, :gpgkey, :http_caching, :include, :includepkgs, :keepalive, - :metadata_expire, :mirrorlist, :priority, :protect, :proxy, :proxy_username, - :proxy_password, :timeout, :sslcacert, :sslverify, :sslclientcert, - :sslclientkey, :s3_enabled, :metalink].each do |param| - it "should have a '#{param}' parameter" do - Puppet::Type.type(:yumrepo).attrtype(param).should == :property - end + valid_values.each do |value| + it "accepts a valid value of #{value}" do + described_class.new(:name => 'puppetlabs', param => value) end end - describe "When validating attribute values" do - [:cost, :enabled, :enablegroups, :failovermethod, :gpgcheck, :repo_gpgcheck, :http_caching, - :keepalive, :metadata_expire, :priority, :protect, :timeout].each do |param| - it "should support :absent as a value to '#{param}' parameter" do - Puppet::Type.type(:yumrepo).new(:name => 'puppetlabs', param => :absent) + 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 + +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 - [:cost, :enabled, :enablegroups, :gpgcheck, :repo_gpgcheck, :keepalive, :metadata_expire, - :priority, :protect, :timeout].each do |param| - it "should fail if '#{param}' is not true/false, 0/1, or yes/no" do - expect { Puppet::Type.type(:yumrepo).new(:name => "puppetlabs", param => "notanumber") }.to raise_error - end + describe "target" do + it_behaves_like "a yumrepo parameter that can be absent", :target end - [:enabled, :enabledgroups, :gpgcheck, :repo_gpgcheck, :keepalive, :protect, :s3_enabled].each do |param| - it "should fail if '#{param}' does not have one of the following values (0|1)" do - expect { Puppet::Type.type(:yumrepo).new(:name => "puppetlabs", param => "2") }.to raise_error - 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 - it "should fail if 'failovermethod' does not have one of the following values (roundrobin|priority)" do - expect { Puppet::Type.type(:yumrepo).new(:name => "puppetlabs", :failovermethod => "notavalidvalue") }.to raise_error + 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 - it "should fail if 'http_caching' does not have one of the following values (packages|all|none)" do - expect { Puppet::Type.type(:yumrepo).new(:name => "puppetlabs", :http_caching => "notavalidvalue") }.to raise_error + 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 - it "should fail if 'sslverify' does not have one of the following values (True|False)" do - expect { Puppet::Type.type(:yumrepo).new(:name => "puppetlabs", :sslverify => "notavalidvalue") }.to raise_error + describe "exclude" do + it_behaves_like "a yumrepo parameter that can be absent", :exclude end - it "should succeed if 'sslverify' has one of the following values (True|False)" do - Puppet::Type.type(:yumrepo).new(:name => "puppetlabs", :sslverify => "True")[:sslverify].should == "True" - Puppet::Type.type(:yumrepo).new(:name => "puppetlabs", :sslverify => "False")[:sslverify].should == "False" + describe "includepkgs" do + it_behaves_like "a yumrepo parameter that can be absent", :includepkgs end - [:mirrorlist, :baseurl, :gpgkey, :include, :proxy, :metalink].each do |param| - it "should succeed if '#{param}' uses one of the following protocols (file|http|https|ftp)" do - Puppet::Type.type(:yumrepo).new(:name => "puppetlabs", param => "file:///srv/example/")[param].should =~ %r{\Afile://} - Puppet::Type.type(:yumrepo).new(:name => "puppetlabs", param => "http://example.com/")[param].should =~ %r{\Ahttp://} - Puppet::Type.type(:yumrepo).new(:name => "puppetlabs", param => "https://example.com/")[param].should =~ %r{\Ahttps://} - Puppet::Type.type(:yumrepo).new(:name => "puppetlabs", param => "ftp://example.com/")[param].should =~ %r{\Aftp://} + 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 + 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 "should fail if '#{param}' does not use one of the following protocols (file|http|https|ftp)" do - expect { Puppet::Type.type(:yumrepo).new(:name => "puppetlabs", param => "gopher://example.com/") }.to raise_error + 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 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 + end + + describe "metadata_expire" do + it_behaves_like "a yumrepo parameter that can be absent", :metadata_expire + 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_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 "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 end end