(#12094) Replace chained .with_* with a hash

The hash passing to the with method is cleaner and closer to puppet code, so
all of the with_$param have been replaced with with($hash). This also
includes two minor whitspace changes to unstable.pp and source.pp.
This also replaces the ternary switch on param_set with a hash merge,
which is cleaner and will support more use cases.
This commit is contained in:
Matthaus Litteken 2012-02-03 17:24:09 -08:00
parent 8cf1bd0289
commit d522877cdd
12 changed files with 134 additions and 102 deletions

View file

@ -15,7 +15,7 @@ describe 'apt', :type => :class do
].each do |param_set| ].each do |param_set|
describe "when #{param_set == {} ? "using default" : "specifying"} class parameters" do describe "when #{param_set == {} ? "using default" : "specifying"} class parameters" do
let :param_hash do let :param_hash do
param_set == {} ? default_params : params default_params.merge(param_set)
end end
let :params do let :params do
@ -35,38 +35,43 @@ describe 'apt', :type => :class do
it { should contain_package("python-software-properties") } it { should contain_package("python-software-properties") }
it { it {
should create_file("sources.list")\ should contain_file("sources.list").with({
.with_path("/etc/apt/sources.list")\ 'path' => "/etc/apt/sources.list",
.with_ensure("present")\ 'ensure' => "present",
.with_owner("root")\ 'owner' => "root",
.with_group("root")\ 'group' => "root",
.with_mode(644) 'mode' => 644
})
} }
it { it {
should create_file("sources.list.d")\ should create_file("sources.list.d").with({
.with_path("/etc/apt/sources.list.d")\ "path" => "/etc/apt/sources.list.d",
.with_ensure("directory")\ "ensure" => "directory",
.with_owner("root")\ "owner" => "root",
.with_group("root") "group" => "root"
})
} }
it { it {
should create_exec("apt_update")\ should contain_exec("apt_update").with({
.with_command("/usr/bin/apt-get update")\ 'command' => "/usr/bin/apt-get update",
.with_subscribe(["File[sources.list]", "File[sources.list.d]"])\ 'subscribe' => ["File[sources.list]", "File[sources.list.d]"],
.with_refreshonly(refresh_only_apt_update) 'refreshonly' => refresh_only_apt_update
})
} }
it { it {
if param_hash[:disable_keys] if param_hash[:disable_keys]
should create_exec("make-apt-insecure")\ should contain_exec("make-apt-insecure").with({
.with_command('/bin/echo "APT::Get::AllowUnauthenticated 1;" >> /etc/apt/apt.conf.d/99unauth')\ 'command' => '/bin/echo "APT::Get::AllowUnauthenticated 1;" >> /etc/apt/apt.conf.d/99unauth',
.with_creates('/etc/apt/apt.conf.d/99unauth') 'creates' => '/etc/apt/apt.conf.d/99unauth'
})
else else
should_not create_exec("make-apt-insecure")\ should_not contain_exec("make-apt-insecure").with({
.with_command('/bin/echo "APT::Get::AllowUnauthenticated 1;" >> /etc/apt/apt.conf.d/99unauth')\ 'command' => '/bin/echo "APT::Get::AllowUnauthenticated 1;" >> /etc/apt/apt.conf.d/99unauth',
.with_creates('/etc/apt/apt.conf.d/99unauth') 'creates' => '/etc/apt/apt.conf.d/99unauth'
})
end end
} }
end end

View file

@ -1,13 +1,14 @@
require 'spec_helper' require 'spec_helper'
describe 'apt::debian::testing', :type => :class do describe 'apt::debian::testing', :type => :class do
it { it {
should create_resource("Apt::source", "debian_testing")\ should contain_apt__source("debian_testing").with({
.with_param("location", "http://debian.mirror.iweb.ca/debian/")\ "location" => "http://debian.mirror.iweb.ca/debian/",
.with_param("release", "testing")\ "release" => "testing",
.with_param("repos", "main contrib non-free")\ "repos" => "main contrib non-free",
.with_param("required_packages", "debian-keyring debian-archive-keyring")\ "required_packages" => "debian-keyring debian-archive-keyring",
.with_param("key", "55BE302B")\ "key" => "55BE302B",
.with_param("key_server", "subkeys.pgp.net")\ "key_server" => "subkeys.pgp.net",
.with_param("pin", "-10") "pin" => "-10"
})
} }
end end

View file

@ -1,13 +1,14 @@
require 'spec_helper' require 'spec_helper'
describe 'apt::debian::unstable', :type => :class do describe 'apt::debian::unstable', :type => :class do
it { it {
should create_resource("Apt::source", "debian_unstable")\ should contain_apt__source("debian_unstable").with({
.with_param("location", "http://debian.mirror.iweb.ca/debian/")\ "location" => "http://debian.mirror.iweb.ca/debian/",
.with_param("release", "unstable")\ "release" => "unstable",
.with_param("repos", "main contrib non-free")\ "repos" => "main contrib non-free",
.with_param("required_packages", "debian-keyring debian-archive-keyring")\ "required_packages" => "debian-keyring debian-archive-keyring",
.with_param("key", "55BE302B")\ "key" => "55BE302B",
.with_param("key_server", "subkeys.pgp.net")\ "key_server" => "subkeys.pgp.net",
.with_param("pin", "-10") "pin" => "-10"
})
} }
end end

View file

@ -2,7 +2,7 @@ require 'spec_helper'
describe 'apt::params', :type => :class do describe 'apt::params', :type => :class do
let (:title) { 'my_package' } let (:title) { 'my_package' }
it { should create_class("apt::params") } it { should contain_apt__params }
# There are 4 resources in this class currently # There are 4 resources in this class currently
# there should not be any more resources because it is a params class # there should not be any more resources because it is a params class

View file

@ -11,11 +11,12 @@ describe 'apt::release', :type => :class do
it { should include_class("apt::params") } it { should include_class("apt::params") }
it { it {
should contain_file("/etc/apt/apt.conf.d/01release")\ should contain_file("/etc/apt/apt.conf.d/01release").with({
.with_owner("root")\ "owner" => "root",
.with_group("root")\ "group" => "root",
.with_mode(644)\ "mode" => 644,
.with_content("APT::Default-Release \"#{param_set[:release_id]}\";") "content" => "APT::Default-Release \"#{param_set[:release_id]}\";"
})
} }
end end

View file

@ -6,12 +6,19 @@ describe 'apt::builddep', :type => :define do
describe "should succeed with a Class['apt']" do describe "should succeed with a Class['apt']" do
let(:pre_condition) { 'class {"apt": } ' } let(:pre_condition) { 'class {"apt": } ' }
it { should contain_exec("apt-update-#{title}").with_command("/usr/bin/apt-get update").with_refreshonly(true) } it { should contain_exec("apt-update-#{title}").with({
'command' => "/usr/bin/apt-get update",
'refreshonly' => true
})
}
end end
describe "should fail without Class['apt']" do describe "should fail without Class['apt']" do
it { expect {should contain_exec("apt-update-#{title}").with_command("/usr/bin/apt-get update").with_refreshonly(true) }\ it { expect {should contain_exec("apt-update-#{title}").with({
.to raise_error(Puppet::Error) 'command' => "/usr/bin/apt-get update",
'refreshonly' => true
}).to raise_error(Puppet::Error)
}
} }
end end

View file

@ -19,7 +19,7 @@ describe 'apt::force', :type => :define do
].each do |param_set| ].each do |param_set|
describe "when #{param_set == {} ? "using default" : "specifying"} define parameters" do describe "when #{param_set == {} ? "using default" : "specifying"} define parameters" do
let :param_hash do let :param_hash do
param_set == {} ? default_params : params default_params.merge(param_set)
end end
let :params do let :params do

View file

@ -17,7 +17,7 @@ describe 'apt::pin', :type => :define do
].each do |param_set| ].each do |param_set|
describe "when #{param_set == {} ? "using default" : "specifying"} define parameters" do describe "when #{param_set == {} ? "using default" : "specifying"} define parameters" do
let :param_hash do let :param_hash do
param_set == {} ? default_params : params default_params.merge(param_set)
end end
let :params do let :params do
@ -26,13 +26,14 @@ describe 'apt::pin', :type => :define do
it { should include_class("apt::params") } it { should include_class("apt::params") }
it { should create_file("#{title}.pref")\ it { should contain_file("#{title}.pref").with({
.with_path("/etc/apt/preferences.d/#{title}")\ 'path' => "/etc/apt/preferences.d/#{title}",
.with_ensure("file")\ 'ensure' => "file",
.with_owner("root")\ 'owner' => "root",
.with_group("root")\ 'group' => "root",
.with_mode("644")\ 'mode' => "644",
.with_content("# #{title}\nPackage: #{param_hash[:packages]}\nPin: release a=#{title}\nPin-Priority: #{param_hash[:priority]}") 'content' => "# #{title}\nPackage: #{param_hash[:packages]}\nPin: release a=#{title}\nPin-Priority: #{param_hash[:priority]}"
})
} }
end end
end end

View file

@ -20,7 +20,7 @@ describe 'apt::ppa', :type => :define do
'notify' => "Exec[apt-update-#{t}]" 'notify' => "Exec[apt-update-#{t}]"
) )
} }
it { should contain_exec("add-apt-repository-#{t}").with_unless(unless_statement) } it { should contain_exec("add-apt-repository-#{t}").with({"unless" => unless_statement}) }
it { should contain_exec("apt-update-#{t}").with( it { should contain_exec("apt-update-#{t}").with(
'command' => '/usr/bin/aptitude update', 'command' => '/usr/bin/aptitude update',
'refreshonly' => true 'refreshonly' => true
@ -32,6 +32,6 @@ describe 'apt::ppa', :type => :define do
describe "without Class[apt] should raise a Puppet::Error" do describe "without Class[apt] should raise a Puppet::Error" do
let(:title) { "ppa" } let(:title) { "ppa" }
it { expect { should create_resource("apt::ppa", title) }.to raise_error(Puppet::Error) } it { expect { should contain_apt__ppa(title) }.to raise_error(Puppet::Error) }
end end
end end

View file

@ -33,7 +33,7 @@ describe 'apt::source', :type => :define do
].each do |param_set| ].each do |param_set|
describe "when #{param_set == {} ? "using default" : "specifying"} class parameters" do describe "when #{param_set == {} ? "using default" : "specifying"} class parameters" do
let :param_hash do let :param_hash do
param_set == {} ? default_params : params default_params.merge(param_set)
end end
let :params do let :params do
@ -53,72 +53,88 @@ describe 'apt::source', :type => :define do
content content
end end
it { should contain_class("apt::params") } it { should contain_apt__params }
it { should contain_file("#{title}.list")\ it { should contain_file("#{title}.list").with({
.with_path(filename)\ 'path' => filename,
.with_ensure("file")\ 'ensure' => "file",
.with_owner("root")\ 'owner' => "root",
.with_group("root")\ 'group' => "root",
.with_mode(644)\ 'mode' => 644,
.with_content(content) 'content' => content
})
} }
it { it {
if param_hash[:pin] if param_hash[:pin]
should create_resource("apt::pin", param_hash[:release]).with_param("priority", param_hash[:pin]).with_param("before", "File[#{title}.list]") should contain_apt__pin(param_hash[:release]).with({
"priority" => param_hash[:pin],
"before" => "File[#{title}.list]"
})
else else
should_not create_resource("apt::pin", param_hash[:release]).with_param("priority", param_hash[:pin]).with_param("before", "File[#{title}.list]") should_not contain_apt__pin(param_hash[:release]).with({
"priority" => param_hash[:pin],
"before" => "File[#{title}.list]"
})
end end
} }
it { it {
should contain_exec("#{title} apt update")\ should contain_exec("#{title} apt update").with({
.with_command("/usr/bin/apt-get update")\ "command" => "/usr/bin/apt-get update",
.with_subscribe("File[#{title}.list]")\ "subscribe" => "File[#{title}.list]",
.with_refreshonly(true) "refreshonly" => true
})
} }
it { it {
if param_hash[:required_packages] if param_hash[:required_packages]
should contain_exec("/usr/bin/apt-get -y install #{param_hash[:required_packages]}")\ should contain_exec("/usr/bin/apt-get -y install #{param_hash[:required_packages]}").with({
.with_subscribe("File[#{title}.list]")\ "subscribe" => "File[#{title}.list]",
.with_refreshonly(true) "refreshonly" => true
})
else else
should_not contain_exec("/usr/bin/apt-get -y install #{param_hash[:required_packages]}")\ should_not contain_exec("/usr/bin/apt-get -y install #{param_hash[:required_packages]}").with({
.with_subscribe("File[#{title}.list]")\ "subscribe" => "File[#{title}.list]",
.with_refreshonly(true) "refreshonly" => true
})
end end
} }
it { it {
if param_hash[:key] if param_hash[:key]
if param_hash[:key_content] if param_hash[:key_content]
should contain_exec("Add key: #{param_hash[:key]} from content")\ should contain_exec("Add key: #{param_hash[:key]} from content").with({
.with_command("/bin/echo '#{param_hash[:key_content]}' | /usr/bin/apt-key add -")\ "command" => "/bin/echo '#{param_hash[:key_content]}' | /usr/bin/apt-key add -",
.with_unless("/usr/bin/apt-key list | /bin/grep '#{param_hash[:key]}'")\ "unless" => "/usr/bin/apt-key list | /bin/grep '#{param_hash[:key]}'",
.with_before("File[#{title}.list]") "before" => "File[#{title}.list]"
should_not contain_exec("/usr/bin/apt-key adv --keyserver #{param_hash[:key_server]} --recv-keys #{param_hash[:key]}")\ })
.with_unless("/usr/bin/apt-key list | /bin/grep #{param_hash[:key]}")\ should_not contain_exec("/usr/bin/apt-key adv --keyserver #{param_hash[:key_server]} --recv-keys #{param_hash[:key]}").with({
.with_before("File[#{title}.list]") "unless" => "/usr/bin/apt-key list | /bin/grep #{param_hash[:key]}",
"before" => "File[#{title}.list]"
})
else else
should contain_exec("/usr/bin/apt-key adv --keyserver #{param_hash[:key_server]} --recv-keys #{param_hash[:key]}")\ should contain_exec("/usr/bin/apt-key adv --keyserver #{param_hash[:key_server]} --recv-keys #{param_hash[:key]}").with({
.with_unless("/usr/bin/apt-key list | /bin/grep #{param_hash[:key]}")\ "unless" => "/usr/bin/apt-key list | /bin/grep #{param_hash[:key]}",
.with_before("File[#{title}.list]") "before" => "File[#{title}.list]"
should_not contain_exec("Add key: #{param_hash[:key]} from content")\ })
.with_command("/bin/echo '#{param_hash[:key_content]}' | /usr/bin/apt-key add -")\ should_not contain_exec("Add key: #{param_hash[:key]} from content").with({
.with_unless("/usr/bin/apt-key list | /bin/grep '#{param_hash[:key]}'")\ "command" => "/bin/echo '#{param_hash[:key_content]}' | /usr/bin/apt-key add -",
.with_before("File[#{title}.list]") "unless" => "/usr/bin/apt-key list | /bin/grep '#{param_hash[:key]}'",
"before" => "File[#{title}.list]"
})
end end
else else
should_not contain_exec("Add key: #{param_hash[:key]} from content")\ should_not contain_exec("Add key: #{param_hash[:key]} from content").with({
.with_command("/bin/echo '#{param_hash[:key_content]}' | /usr/bin/apt-key add -")\ "command" => "/bin/echo '#{param_hash[:key_content]}' | /usr/bin/apt-key add -",
.with_unless("/usr/bin/apt-key list | /bin/grep '#{param_hash[:key]}'")\ "unless" => "/usr/bin/apt-key list | /bin/grep '#{param_hash[:key]}'",
.with_before("File[#{title}.list]") "before" => "File[#{title}.list]"
should_not contain_exec("/usr/bin/apt-key adv --keyserver #{param_hash[:key_server]} --recv-keys #{param_hash[:key]}")\ })
.with_unless("/usr/bin/apt-key list | /bin/grep #{param_hash[:key]}")\ should_not contain_exec("/usr/bin/apt-key adv --keyserver #{param_hash[:key_server]} --recv-keys #{param_hash[:key]}").with({
.with_before("File[#{title}.list]") "unless" => "/usr/bin/apt-key list | /bin/grep #{param_hash[:key]}",
"before" => "File[#{title}.list]"
})
end end
} }