294892f3244b95983533d315bedd818a85c0ea8a
[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 => false,
6       :always_apt_update => false,
7       :purge => false
8     }
9   end
10
11   [{},
12    {
13       :disable_keys => true,
14       :always_apt_update => true,
15       :proxy_host => true,
16       :proxy_port => '3128',
17       :purge => true
18     }
19   ].each do |param_set|
20     describe "when #{param_set == {} ? "using default" : "specifying"} class parameters" do
21       let :param_hash do
22         default_params.merge(param_set)
23       end
24
25       let :params do
26         param_set
27       end
28
29       let :refresh_only_apt_update do
30         if param_hash[:always_apt_update]
31           false
32         else
33           true
34         end
35       end
36
37       it { should include_class("apt::params") }
38
39       it { should contain_package("python-software-properties") }
40
41       it {
42         if param_hash[:purge]
43         should contain_file("sources.list").with({
44             'path'    => "/etc/apt/sources.list",
45             'ensure'  => "present",
46             'owner'   => "root",
47             'group'   => "root",
48             'mode'    => 644,
49             "content" => "# Repos managed by puppet.\n"
50           })
51         else
52         should contain_file("sources.list").with({
53             'path'    => "/etc/apt/sources.list",
54             'ensure'  => "present",
55             'owner'   => "root",
56             'group'   => "root",
57             'mode'    => 644,
58             'content' => nil
59           })
60         end
61       }
62       it {
63         if param_hash[:purge]
64           should create_file("sources.list.d").with({
65             'path'    => "/etc/apt/sources.list.d",
66             'ensure'  => "directory",
67             'owner'   => "root",
68             'group'   => "root",
69             'purge'   => true,
70             'recurse' => true
71           })
72         else
73           should create_file("sources.list.d").with({
74             'path'    => "/etc/apt/sources.list.d",
75             'ensure'  => "directory",
76             'owner'   => "root",
77             'group'   => "root",
78             'purge'   => false,
79             'recurse' => false
80           })
81         end
82       }
83
84       it {
85         should contain_exec("apt_update").with({
86           'command'     => "/usr/bin/apt-get update",
87           'subscribe'   => ["File[sources.list]", "File[sources.list.d]"],
88           'refreshonly' => refresh_only_apt_update
89         })
90       }
91
92       it {
93         if param_hash[:disable_keys]
94           should contain_exec("make-apt-insecure").with({
95             'command'   => '/bin/echo "APT::Get::AllowUnauthenticated 1;" >> /etc/apt/apt.conf.d/99unauth',
96             'creates'   => '/etc/apt/apt.conf.d/99unauth'
97           })
98         else
99           should_not contain_exec("make-apt-insecure").with({
100             'command'   => '/bin/echo "APT::Get::AllowUnauthenticated 1;" >> /etc/apt/apt.conf.d/99unauth',
101             'creates'   => '/etc/apt/apt.conf.d/99unauth'
102           })
103         end
104       }
105       describe 'when setting a proxy' do
106         it {
107           if param_hash[:proxy_host]
108             should contain_file('configure-apt-proxy').with(
109               'path'    => '/etc/apt/apt.conf.d/proxy',
110               'content' => "Acquire::http::Proxy \"http://#{param_hash[:proxy_host]}:#{param_hash[:proxy_port]}\";"
111             )
112           else
113             should_not contain_file('configure_apt_proxy')
114           end
115         }
116       end
117     end
118   end
119 end