Merge pull request #52 from nanliu/tb/14308
[puppet-modules/puppetlabs-apt.git] / spec / defines / source_spec.rb
1 require 'spec_helper'
2 describe 'apt::source', :type => :define do
3   let :title do
4     'my_source'
5   end
6
7   let :default_params do
8     {
9       :ensure             => 'present',
10       :location           => '',
11       :release            => 'karmic',
12       :repos              => 'main',
13       :include_src        => true,
14       :required_packages  => false,
15       :key                => false,
16       :key_server         => 'keyserver.ubuntu.com',
17       :key_content        => false,
18       :key_source         => false,
19       :pin                => false
20     }
21   end
22
23   [{},
24    {
25       :location           => 'somewhere',
26       :release            => 'precise',
27       :repos              => 'security',
28       :include_src        => false,
29       :required_packages  => 'apache',
30       :key                => 'key_name',
31       :key_server         => 'keyserver.debian.com',
32       :pin                => '600',
33       :key_content        => 'ABCD1234'
34     },
35     {
36       :key                => 'key_name',
37       :key_server         => 'keyserver.debian.com',
38       :key_content        => false,
39     },
40     {
41       :ensure             => 'absent',
42       :location           => 'somewhere',
43       :release            => 'precise',
44       :repos              => 'security',
45     }
46   ].each do |param_set|
47     describe "when #{param_set == {} ? "using default" : "specifying"} class parameters" do
48       let :param_hash do
49         default_params.merge(param_set)
50       end
51
52       let :facts do
53         {:lsbdistcodename => 'karmic'}
54       end
55
56       let :params do
57         param_set
58       end
59
60       let :filename do
61         "/etc/apt/sources.list.d/#{title}.list"
62       end
63
64       let :content do
65         content = "# #{title}"
66         content << "\ndeb #{param_hash[:location]} #{param_hash[:release]} #{param_hash[:repos]}\n"
67         if param_hash[:include_src]
68           content << "deb-src #{param_hash[:location]} #{param_hash[:release]} #{param_hash[:repos]}\n"
69         end
70         content
71       end
72
73       it { should contain_apt__params }
74
75       it { should contain_file("#{title}.list").with({
76           'ensure'    => param_hash[:ensure],
77           'path'      => filename,
78           'owner'     => 'root',
79           'group'     => 'root',
80           'mode'      => '0644',
81           'content'   => content,
82         })
83       }
84
85       it {
86         if param_hash[:pin]
87           should contain_apt__pin(param_hash[:release]).with({
88             "priority"  => param_hash[:pin],
89             "before"    => "File[#{title}.list]"
90           })
91         else
92           should_not contain_apt__pin(param_hash[:release]).with({
93             "priority"  => param_hash[:pin],
94             "before"    => "File[#{title}.list]"
95           })
96         end
97       }
98
99       it {
100         should contain_exec("#{title} apt update").with({
101           "command"     => "/usr/bin/apt-get update",
102           "subscribe"   => "File[#{title}.list]",
103           "refreshonly" => true
104         })
105       }
106
107       it {
108         if param_hash[:required_packages]
109           should contain_exec("Required packages: '#{param_hash[:required_packages]}' for #{title}").with({
110             "command" => "/usr/bin/apt-get -y install #{param_hash[:required_packages]}",
111             "subscribe"   => "File[#{title}.list]",
112             "refreshonly" => true
113           })
114         else
115           should_not contain_exec("Required packages: '#{param_hash[:required_packages]}' for #{title}").with({
116             "command"     => "/usr/bin/apt-get -y install #{param_hash[:required_packages]}",
117             "subscribe"   => "File[#{title}.list]",
118             "refreshonly" => true
119           })
120         end
121       }
122
123       it {
124         if param_hash[:key]
125           should contain_apt__key("Add key: #{param_hash[:key]} from Apt::Source #{title}").with({
126             "key"         => param_hash[:key],
127             "ensure"      => :present,
128             "key_server"  => param_hash[:key_server],
129             "key_content" => param_hash[:key_content],
130             "key_source"  => param_hash[:key_source],
131             "before"      => "File[#{title}.list]"
132           })
133         else
134           should_not contain_apt__key("Add key: #{param_hash[:key]} from Apt::Source #{title}").with({
135             "key"         => param_hash[:key],
136             "ensure"      => :present,
137             "key_server"  => param_hash[:key_server],
138             "key_content" => param_hash[:key_content],
139             "key_source"  => param_hash[:key_source],
140             "before"      => "File[#{title}.list]"
141           })
142         end
143       }
144     end
145   end
146   describe "without release should raise a Puppet::Error" do
147     let(:default_params) { Hash.new }
148     let(:facts) { Hash.new }
149     it { expect { should raise_error(Puppet::Error) } }
150     let(:facts) { { :lsbdistcodename => 'lucid' } }
151     it { should contain_apt__source(title) }
152   end
153 end