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