From 2d688f4cdc121da434db873f818dd95977385a46 Mon Sep 17 00:00:00 2001 From: Matthaus Litteken Date: Mon, 23 Jan 2012 13:08:00 -0800 Subject: [PATCH] (#12094) Add rspec-puppet tests for apt This commit adds full coverage for the apt module as it currently exists. It adds rspec-puppet tests for the defines (apt::builddep, apt::force, apt::pin, apt::ppa, apt::source) and classes (apt, debian::testing, debian::unstable, apt::params, apt::release). --- spec/classes/apt_spec.rb | 74 ++++++++++++++++ spec/classes/debian_testing_spec.rb | 13 +++ spec/classes/debian_unstable_spec.rb | 13 +++ spec/classes/params_spec.rb | 13 +++ spec/classes/release_spec.rb | 21 +++++ spec/defines/builddep_spec.rb | 18 ++++ spec/defines/force_spec.rb | 31 +++++-- spec/defines/pin_spec.rb | 39 ++++++++ spec/defines/ppa_spec.rb | 5 ++ spec/defines/source_spec.rb | 128 +++++++++++++++++++++++++++ 10 files changed, 348 insertions(+), 7 deletions(-) create mode 100644 spec/classes/apt_spec.rb create mode 100644 spec/classes/debian_testing_spec.rb create mode 100644 spec/classes/debian_unstable_spec.rb create mode 100644 spec/classes/params_spec.rb create mode 100644 spec/classes/release_spec.rb create mode 100644 spec/defines/builddep_spec.rb create mode 100644 spec/defines/pin_spec.rb create mode 100644 spec/defines/source_spec.rb diff --git a/spec/classes/apt_spec.rb b/spec/classes/apt_spec.rb new file mode 100644 index 0000000..a858180 --- /dev/null +++ b/spec/classes/apt_spec.rb @@ -0,0 +1,74 @@ +require 'spec_helper' +describe 'apt', :type => :class do + let :default_params do + { + :disable_keys => false, + :always_apt_update => false + } + end + + [{}, + { + :disable_keys => true, + :always_apt_update => true + } + ].each do |param_set| + describe "when #{param_set == {} ? "using default" : "specifying"} class parameters" do + let :param_hash do + param_set == {} ? default_params : params + end + + let :params do + param_set + end + + let :refresh_only_apt_update do + if param_hash[:always_apt_update] + false + else + true + end + end + + it { should include_class("apt::params") } + + it { should contain_package("python-software-properties") } + + it { + should create_file("sources.list")\ + .with_path("/etc/apt/sources.list")\ + .with_ensure("present")\ + .with_owner("root")\ + .with_group("root")\ + .with_mode(644) + } + + it { + should create_file("sources.list.d")\ + .with_path("/etc/apt/sources.list.d")\ + .with_ensure("directory")\ + .with_owner("root")\ + .with_group("root") + } + + it { + should create_exec("apt_update")\ + .with_command("/usr/bin/apt-get update")\ + .with_subscribe(["File[sources.list]", "File[sources.list.d]"])\ + .with_refreshonly(refresh_only_apt_update) + } + + it { + if param_hash[:disable_keys] + should create_exec("make-apt-insecure")\ + .with_command('/bin/echo "APT::Get::AllowUnauthenticated 1;" >> /etc/apt/apt.conf.d/99unauth')\ + .with_creates('/etc/apt/apt.conf.d/99unauth') + else + should_not create_exec("make-apt-insecure")\ + .with_command('/bin/echo "APT::Get::AllowUnauthenticated 1;" >> /etc/apt/apt.conf.d/99unauth')\ + .with_creates('/etc/apt/apt.conf.d/99unauth') + end + } + end + end +end diff --git a/spec/classes/debian_testing_spec.rb b/spec/classes/debian_testing_spec.rb new file mode 100644 index 0000000..0ee1b2e --- /dev/null +++ b/spec/classes/debian_testing_spec.rb @@ -0,0 +1,13 @@ +require 'spec_helper' +describe 'apt::debian::testing', :type => :class do + it { + should create_resource("Apt::source", "debian_testing")\ + .with_param("location", "http://debian.mirror.iweb.ca/debian/")\ + .with_param("release", "testing")\ + .with_param("repos", "main contrib non-free")\ + .with_param("required_packages", "debian-keyring debian-archive-keyring")\ + .with_param("key", "55BE302B")\ + .with_param("key_server", "subkeys.pgp.net")\ + .with_param("pin", "-10") + } +end diff --git a/spec/classes/debian_unstable_spec.rb b/spec/classes/debian_unstable_spec.rb new file mode 100644 index 0000000..5673b78 --- /dev/null +++ b/spec/classes/debian_unstable_spec.rb @@ -0,0 +1,13 @@ +require 'spec_helper' +describe 'apt::debian::unstable', :type => :class do + it { + should create_resource("Apt::source", "debian_unstable")\ + .with_param("location", "http://debian.mirror.iweb.ca/debian/")\ + .with_param("release", "unstable")\ + .with_param("repos", "main contrib non-free")\ + .with_param("required_packages", "debian-keyring debian-archive-keyring")\ + .with_param("key", "55BE302B")\ + .with_param("key_server", "subkeys.pgp.net")\ + .with_param("pin", "-10") + } +end diff --git a/spec/classes/params_spec.rb b/spec/classes/params_spec.rb new file mode 100644 index 0000000..a0ec08c --- /dev/null +++ b/spec/classes/params_spec.rb @@ -0,0 +1,13 @@ +require 'spec_helper' +describe 'apt::params', :type => :class do + let (:title) { 'my_package' } + + it { should create_class("apt::params") } + + # There are 4 resources in this class currently + # there should not be any more resources because it is a params class + # The resources are class[apt::params], class[main], class[settings], stage[main] + it "Should not contain any resources" do + subject.resources.size.should == 4 + end +end diff --git a/spec/classes/release_spec.rb b/spec/classes/release_spec.rb new file mode 100644 index 0000000..a8b1448 --- /dev/null +++ b/spec/classes/release_spec.rb @@ -0,0 +1,21 @@ +require 'spec_helper' +describe 'apt::release', :type => :class do + let (:title) { 'my_package' } + + let :param_set do + { :release_id => 'precise' } + end + + let (:params) { param_set } + + it { should include_class("apt::params") } + + it { + should contain_file("/etc/apt/apt.conf.d/01release")\ + .with_owner("root")\ + .with_group("root")\ + .with_mode(644)\ + .with_content("APT::Default-Release \"#{param_set[:release_id]}\";") + } +end + diff --git a/spec/defines/builddep_spec.rb b/spec/defines/builddep_spec.rb new file mode 100644 index 0000000..d60a330 --- /dev/null +++ b/spec/defines/builddep_spec.rb @@ -0,0 +1,18 @@ +require 'spec_helper' +describe 'apt::builddep', :type => :define do + + let(:title) { 'my_package' } + + describe "should succeed with a Class['apt']" do + let(:pre_condition) { 'class {"apt": } ' } + + it { should contain_exec("apt-update-#{title}").with_command("/usr/bin/apt-get update").with_refreshonly(true) } + end + + 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) }\ + .to raise_error(Puppet::Error) + } + end + +end diff --git a/spec/defines/force_spec.rb b/spec/defines/force_spec.rb index 8477a08..11b82e3 100644 --- a/spec/defines/force_spec.rb +++ b/spec/defines/force_spec.rb @@ -1,22 +1,39 @@ require 'spec_helper' describe 'apt::force', :type => :define do - let :title do 'my_package' end - [false, '1'].each do |version| - describe "with version: #{version}" do + let :default_params do + { + :release => 'testing', + :version => false + } + end + + [{}, + { + :release => 'stable', + :version => '1' + } + ].each do |param_set| + describe "when #{param_set == {} ? "using default" : "specifying"} define parameters" do + let :param_hash do + param_set == {} ? default_params : params + end + let :params do - {:version => version, :release => 'testing'} + param_set end + let :unless_query do base_command = "/usr/bin/dpkg -s #{title} | grep -q " - base_command + (version ? "'Version: #{params[:version]}'" : "'Status: install'") + base_command + (params[:version] ? "'Version: #{params[:version]}'" : "'Status: install'") end + let :exec_title do - base_exec = "/usr/bin/aptitude -y -t #{params[:release]} install #{title}" - base_exec + (version ? "=#{version}" : "") + base_exec = "/usr/bin/aptitude -y -t #{param_hash[:release]} install #{title}" + base_exec + (params[:version] ? "=#{params[:version]}" : "") end it { should contain_exec(exec_title).with_unless(unless_query) } end diff --git a/spec/defines/pin_spec.rb b/spec/defines/pin_spec.rb new file mode 100644 index 0000000..ac61864 --- /dev/null +++ b/spec/defines/pin_spec.rb @@ -0,0 +1,39 @@ +require 'spec_helper' +describe 'apt::pin', :type => :define do + let(:title) { 'my_pin' } + + let :default_params do + { + :packages => '*', + :priority => '0' + } + end + + [{}, + { + :packages => 'apache', + :priority => '1' + } + ].each do |param_set| + describe "when #{param_set == {} ? "using default" : "specifying"} define parameters" do + let :param_hash do + param_set == {} ? default_params : params + end + + let :params do + param_set + end + + it { should include_class("apt::params") } + + it { should create_file("#{title}.pref")\ + .with_path("/etc/apt/preferences.d/#{title}")\ + .with_ensure("file")\ + .with_owner("root")\ + .with_group("root")\ + .with_mode("644")\ + .with_content("# #{title}\nPackage: #{param_hash[:packages]}\nPin: release a=#{title}\nPin-Priority: #{param_hash[:priority]}") + } + end + end +end diff --git a/spec/defines/ppa_spec.rb b/spec/defines/ppa_spec.rb index 6532654..3d786e1 100644 --- a/spec/defines/ppa_spec.rb +++ b/spec/defines/ppa_spec.rb @@ -29,4 +29,9 @@ describe 'apt::ppa', :type => :define do it { should contain_exec("apt-update-#{t}").without_unless } end end + + describe "without Class[apt] should raise a Puppet::Error" do + let(:title) { "ppa" } + it { expect { should create_resource("apt::ppa", title) }.to raise_error(Puppet::Error) } + end end diff --git a/spec/defines/source_spec.rb b/spec/defines/source_spec.rb new file mode 100644 index 0000000..333d534 --- /dev/null +++ b/spec/defines/source_spec.rb @@ -0,0 +1,128 @@ +require 'spec_helper' +describe 'apt::source', :type => :define do + let :title do + 'my_source' + end + + let :default_params do + { + :location => '', + :release => 'karmic', + :repos => 'main', + :include_src => true, + :required_packages => false, + :key => false, + :key_server => 'keyserver.ubuntu.com', + :pin => false, + :key_content => false + } + end + + [{}, + { + :location => 'somewhere', + :release => 'precise', + :repos => 'security', + :include_src => false, + :required_packages => 'apache', + :key => 'key_name', + :key_server => 'keyserver.debian.com', + :pin => '600', + :key_content => 'ABCD1234' + } + ].each do |param_set| + describe "when #{param_set == {} ? "using default" : "specifying"} class parameters" do + let :param_hash do + param_set == {} ? default_params : params + end + + let :params do + param_set + end + + let :filename do + "/etc/apt/sources.list.d/#{title}.list" + end + + let :content do + content = "# #{title}" + content << "\ndeb #{param_hash[:location]} #{param_hash[:release]} #{param_hash[:repos]}\n" + if param_hash[:include_src] + content << "deb-src #{param_hash[:location]} #{param_hash[:release]} #{param_hash[:repos]}\n" + end + content + end + + it { should contain_class("apt::params") } + + it { should contain_file("#{title}.list")\ + .with_path(filename)\ + .with_ensure("file")\ + .with_owner("root")\ + .with_group("root")\ + .with_mode(644)\ + .with_content(content) + } + + it { + if param_hash[:pin] + should create_resource("apt::pin", param_hash[:release]).with_param("priority", param_hash[:pin]).with_param("before", "File[#{title}.list]") + else + should_not create_resource("apt::pin", param_hash[:release]).with_param("priority", param_hash[:pin]).with_param("before", "File[#{title}.list]") + end + } + + it { + should contain_exec("#{title} apt update")\ + .with_command("/usr/bin/apt-get update")\ + .with_subscribe("File[#{title}.list]")\ + .with_refreshonly(true) + } + + it { + if param_hash[:required_packages] + should contain_exec("/usr/bin/apt-get -y install #{param_hash[:required_packages]}")\ + .with_subscribe("File[#{title}.list]")\ + .with_refreshonly(true) + else + should_not contain_exec("/usr/bin/apt-get -y install #{param_hash[:required_packages]}")\ + .with_subscribe("File[#{title}.list]")\ + .with_refreshonly(true) + end + } + + it { + if param_hash[:key] + if param_hash[:key_content] + should contain_exec("Add key: #{param_hash[:key]} from content")\ + .with_command("/bin/echo '#{param_hash[:key_content]}' | /usr/bin/apt-key add -")\ + .with_unless("/usr/bin/apt-key list | /bin/grep '#{param_hash[:key]}'")\ + .with_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]}")\ + .with_before("File[#{title}.list]") + + else + should 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]}")\ + .with_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 -")\ + .with_unless("/usr/bin/apt-key list | /bin/grep '#{param_hash[:key]}'")\ + .with_before("File[#{title}.list]") + end + else + should_not contain_exec("Add key: #{param_hash[:key]} from content")\ + .with_command("/bin/echo '#{param_hash[:key_content]}' | /usr/bin/apt-key add -")\ + .with_unless("/usr/bin/apt-key list | /bin/grep '#{param_hash[:key]}'")\ + .with_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]}")\ + .with_before("File[#{title}.list]") + + end + } + end + end +end + -- 2.32.3