Merge pull request #216 from genome-vendor/master
[puppet-modules/puppetlabs-apt.git] / spec / classes / apt_spec.rb
1 require 'spec_helper'
2 describe 'apt', :type => :class do
3   let(:facts) { { :lsbdistid => 'Debian' } }
4   let :default_params do
5     {
6       :disable_keys => :undef,
7       :always_apt_update => false,
8       :purge_sources_list => false,
9       :purge_sources_list_d => false,
10     }
11   end
12
13   [{},
14     {
15       :disable_keys => true,
16       :always_apt_update => true,
17       :proxy_host => true,
18       :proxy_port => '3128',
19       :purge_sources_list => true,
20       :purge_sources_list_d => true,
21     },
22     {
23       :purge_preferences   => true,
24       :purge_preferences_d => true,
25     },
26     {
27       :disable_keys => false
28     }
29   ].each do |param_set|
30     describe "when #{param_set == {} ? "using default" : "specifying"} class parameters" do
31       let :param_hash do
32         default_params.merge(param_set)
33       end
34
35       let :params do
36         param_set
37       end
38
39       let :refresh_only_apt_update do
40         if param_hash[:always_apt_update]
41           false
42         else
43           true
44         end
45       end
46
47       it { should contain_class("apt::params") }
48
49       it {
50         if param_hash[:purge_sources_list]
51         should contain_file("sources.list").with({
52             'path'    => "/etc/apt/sources.list",
53             'ensure'  => "present",
54             'owner'   => "root",
55             'group'   => "root",
56             'mode'    => "0644",
57             "content" => "# Repos managed by puppet.\n"
58           })
59         else
60         should contain_file("sources.list").with({
61             'path'    => "/etc/apt/sources.list",
62             'ensure'  => "present",
63             'owner'   => "root",
64             'group'   => "root",
65             'mode'    => "0644",
66             'content' => nil
67           })
68         end
69       }
70       it {
71         if param_hash[:purge_sources_list_d]
72           should create_file("sources.list.d").with({
73             'path'    => "/etc/apt/sources.list.d",
74             'ensure'  => "directory",
75             'owner'   => "root",
76             'group'   => "root",
77             'purge'   => true,
78             'recurse' => true,
79             'notify'  => 'Exec[apt_update]'
80           })
81         else
82           should create_file("sources.list.d").with({
83             'path'    => "/etc/apt/sources.list.d",
84             'ensure'  => "directory",
85             'owner'   => "root",
86             'group'   => "root",
87             'purge'   => false,
88             'recurse' => false,
89             'notify'  => 'Exec[apt_update]'
90           })
91         end
92       }
93       it {
94         if param_hash[:purge_preferences]
95           should create_file('apt-preferences').with({
96             :ensure  => 'present',
97             :path    => '/etc/apt/preferences',
98             :owner   => 'root',
99             :group   => 'root',
100             :mode    => '0644',
101             :content => /Explanation/,
102           })
103         else
104           should create_file('apt-preferences').with({
105             :ensure  => 'present',
106             :path    => '/etc/apt/preferences',
107             :owner   => 'root',
108             :group   => 'root',
109             :mode    => '0644',
110             :content => nil,
111           })
112         end
113       }
114
115       it {
116         if param_hash[:purge_preferences_d]
117           should create_file("preferences.d").with({
118             'path'    => "/etc/apt/preferences.d",
119             'ensure'  => "directory",
120             'owner'   => "root",
121             'group'   => "root",
122             'purge'   => true,
123             'recurse' => true,
124           })
125         else
126           should create_file("preferences.d").with({
127             'path'    => "/etc/apt/preferences.d",
128             'ensure'  => "directory",
129             'owner'   => "root",
130             'group'   => "root",
131             'purge'   => false,
132             'recurse' => false,
133           })
134         end
135       }
136
137       it {
138         should contain_exec("apt_update").with({
139           'command'     => "/usr/bin/apt-get update",
140           'refreshonly' => refresh_only_apt_update
141         })
142       }
143
144       it {
145         if param_hash[:disable_keys] == true
146           should create_file("99unauth").with({
147             'content' => "APT::Get::AllowUnauthenticated 1;\n",
148             'ensure'  => "present",
149             'path'    => "/etc/apt/apt.conf.d/99unauth"
150           })
151         elsif param_hash[:disable_keys] == false
152           should create_file("99unauth").with({
153             'ensure' => "absent",
154             'path'   => "/etc/apt/apt.conf.d/99unauth"
155           })
156         elsif param_hash[:disable_keys] != :undef
157           should_not create_file("99unauth").with({
158             'path'   => "/etc/apt/apt.conf.d/99unauth"
159           })
160         end
161       }
162       describe 'when setting a proxy' do
163         it {
164           if param_hash[:proxy_host]
165             should contain_file('configure-apt-proxy').with(
166               'path'    => '/etc/apt/apt.conf.d/proxy',
167               'content' => "Acquire::http::Proxy \"http://#{param_hash[:proxy_host]}:#{param_hash[:proxy_port]}\";",
168               'notify'  => "Exec[apt_update]"
169             )
170           else
171             should contain_file('configure-apt-proxy').with(
172               'path'    => '/etc/apt/apt.conf.d/proxy',
173               'notify'  => 'Exec[apt_update]',
174               'ensure'  => 'absent'
175             )
176           end
177         }
178       end
179     end
180   end
181 end