]> review.fuel-infra Code Review - puppet-modules/puppetlabs-apt.git/blob - spec/defines/source_spec.rb
333d53460f033b35ab4f69ca7c15982c08f93c55
[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       :pin                => false,
17       :key_content        => false
18     }
19   end
20
21   [{},
22    {
23       :location           => 'somewhere',
24       :release            => 'precise',
25       :repos              => 'security',
26       :include_src        => false,
27       :required_packages  => 'apache',
28       :key                => 'key_name',
29       :key_server         => 'keyserver.debian.com',
30       :pin                => '600',
31       :key_content        => 'ABCD1234'
32     }
33   ].each do |param_set|
34     describe "when #{param_set == {} ? "using default" : "specifying"} class parameters" do
35       let :param_hash do
36         param_set == {} ? default_params : params
37       end
38
39       let :params do
40         param_set
41       end
42
43       let :filename do
44         "/etc/apt/sources.list.d/#{title}.list"
45       end
46
47       let :content do
48         content = "# #{title}"
49         content << "\ndeb #{param_hash[:location]} #{param_hash[:release]} #{param_hash[:repos]}\n"
50         if param_hash[:include_src]
51           content << "deb-src #{param_hash[:location]} #{param_hash[:release]} #{param_hash[:repos]}\n"
52         end
53         content
54       end
55
56       it { should contain_class("apt::params") }
57
58       it { should contain_file("#{title}.list")\
59         .with_path(filename)\
60         .with_ensure("file")\
61         .with_owner("root")\
62         .with_group("root")\
63         .with_mode(644)\
64         .with_content(content)
65       }
66
67       it {
68         if param_hash[:pin]
69           should create_resource("apt::pin", param_hash[:release]).with_param("priority", param_hash[:pin]).with_param("before", "File[#{title}.list]")
70         else
71           should_not create_resource("apt::pin", param_hash[:release]).with_param("priority", param_hash[:pin]).with_param("before", "File[#{title}.list]")
72         end
73       }
74
75       it {
76         should contain_exec("#{title} apt update")\
77           .with_command("/usr/bin/apt-get update")\
78           .with_subscribe("File[#{title}.list]")\
79           .with_refreshonly(true)
80       }
81
82       it {
83         if param_hash[:required_packages]
84           should contain_exec("/usr/bin/apt-get -y install #{param_hash[:required_packages]}")\
85             .with_subscribe("File[#{title}.list]")\
86             .with_refreshonly(true)
87         else
88           should_not contain_exec("/usr/bin/apt-get -y install #{param_hash[:required_packages]}")\
89             .with_subscribe("File[#{title}.list]")\
90             .with_refreshonly(true)
91         end
92       }
93
94       it {
95         if param_hash[:key]
96           if param_hash[:key_content]
97             should contain_exec("Add key: #{param_hash[:key]} from content")\
98               .with_command("/bin/echo '#{param_hash[:key_content]}' | /usr/bin/apt-key add -")\
99               .with_unless("/usr/bin/apt-key list | /bin/grep '#{param_hash[:key]}'")\
100               .with_before("File[#{title}.list]")
101             should_not contain_exec("/usr/bin/apt-key adv --keyserver #{param_hash[:key_server]} --recv-keys #{param_hash[:key]}")\
102               .with_unless("/usr/bin/apt-key list | /bin/grep #{param_hash[:key]}")\
103               .with_before("File[#{title}.list]")
104
105           else
106             should contain_exec("/usr/bin/apt-key adv --keyserver #{param_hash[:key_server]} --recv-keys #{param_hash[:key]}")\
107               .with_unless("/usr/bin/apt-key list | /bin/grep #{param_hash[:key]}")\
108               .with_before("File[#{title}.list]")
109             should_not contain_exec("Add key: #{param_hash[:key]} from content")\
110               .with_command("/bin/echo '#{param_hash[:key_content]}' | /usr/bin/apt-key add -")\
111               .with_unless("/usr/bin/apt-key list | /bin/grep '#{param_hash[:key]}'")\
112               .with_before("File[#{title}.list]")
113           end
114         else
115           should_not contain_exec("Add key: #{param_hash[:key]} from content")\
116             .with_command("/bin/echo '#{param_hash[:key_content]}' | /usr/bin/apt-key add -")\
117             .with_unless("/usr/bin/apt-key list | /bin/grep '#{param_hash[:key]}'")\
118             .with_before("File[#{title}.list]")
119           should_not contain_exec("/usr/bin/apt-key adv --keyserver #{param_hash[:key_server]} --recv-keys #{param_hash[:key]}")\
120               .with_unless("/usr/bin/apt-key list | /bin/grep #{param_hash[:key]}")\
121               .with_before("File[#{title}.list]")
122
123         end
124       }
125     end
126   end
127 end
128