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