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