Merge branch 'fix_pin_quotes' into 1.4.x
[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       :disable_keys => false
24     }
25   ].each do |param_set|
26     describe "when #{param_set == {} ? "using default" : "specifying"} class parameters" do
27       let :param_hash do
28         default_params.merge(param_set)
29       end
30
31       let :params do
32         param_set
33       end
34
35       let :refresh_only_apt_update do
36         if param_hash[:always_apt_update]
37           false
38         else
39           true
40         end
41       end
42
43       it { should contain_class("apt::params") }
44
45       it {
46         if param_hash[:purge_sources_list]
47         should contain_file("sources.list").with({
48             'path'    => "/etc/apt/sources.list",
49             'ensure'  => "present",
50             'owner'   => "root",
51             'group'   => "root",
52             'mode'    => "0644",
53             "content" => "# Repos managed by puppet.\n"
54           })
55         else
56         should contain_file("sources.list").with({
57             'path'    => "/etc/apt/sources.list",
58             'ensure'  => "present",
59             'owner'   => "root",
60             'group'   => "root",
61             'mode'    => "0644",
62             'content' => nil
63           })
64         end
65       }
66       it {
67         if param_hash[:purge_sources_list_d]
68           should create_file("sources.list.d").with({
69             'path'    => "/etc/apt/sources.list.d",
70             'ensure'  => "directory",
71             'owner'   => "root",
72             'group'   => "root",
73             'purge'   => true,
74             'recurse' => true,
75             'notify'  => 'Exec[apt_update]'
76           })
77         else
78           should create_file("sources.list.d").with({
79             'path'    => "/etc/apt/sources.list.d",
80             'ensure'  => "directory",
81             'owner'   => "root",
82             'group'   => "root",
83             'purge'   => false,
84             'recurse' => false,
85             'notify'  => 'Exec[apt_update]'
86           })
87         end
88       }
89
90       it {
91         should contain_exec("apt_update").with({
92           'command'     => "/usr/bin/apt-get update",
93           'refreshonly' => refresh_only_apt_update
94         })
95       }
96
97       it {
98         if param_hash[:disable_keys] == true
99           should create_file("99unauth").with({
100             'content' => "APT::Get::AllowUnauthenticated 1;\n",
101             'ensure'  => "present",
102             'path'    => "/etc/apt/apt.conf.d/99unauth"
103           })
104         elsif param_hash[:disable_keys] == false
105           should create_file("99unauth").with({
106             'ensure' => "absent",
107             'path'   => "/etc/apt/apt.conf.d/99unauth"
108           })
109         elsif param_hash[:disable_keys] != :undef
110           should_not create_file("99unauth").with({
111             'path'   => "/etc/apt/apt.conf.d/99unauth"
112           })
113         end
114       }
115       describe 'when setting a proxy' do
116         it {
117           if param_hash[:proxy_host]
118             should contain_file('configure-apt-proxy').with(
119               'path'    => '/etc/apt/apt.conf.d/proxy',
120               'content' => "Acquire::http::Proxy \"http://#{param_hash[:proxy_host]}:#{param_hash[:proxy_port]}\";",
121               'notify'  => "Exec[apt_update]"
122             )
123           else
124             should contain_file('configure-apt-proxy').with(
125               'path'    => '/etc/apt/apt.conf.d/proxy',
126               'notify'  => 'Exec[apt_update]',
127               'ensure'  => 'absent'
128             )
129           end
130         }
131       end
132     end
133   end
134 end