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