Merge pull request #54 from branan/python-software-properties-in-apt-ppa
[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 {
45         if param_hash[:purge_sources_list]
46         should contain_file("sources.list").with({
47             'path'    => "/etc/apt/sources.list",
48             'ensure'  => "present",
49             'owner'   => "root",
50             'group'   => "root",
51             'mode'    => "0644",
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'    => "0644",
61             'content' => nil
62           })
63         end
64       }
65       it {
66         if param_hash[:purge_sources_list_d]
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             'notify'  => 'Exec[apt_update]'
75           })
76         else
77           should create_file("sources.list.d").with({
78             'path'    => "/etc/apt/sources.list.d",
79             'ensure'  => "directory",
80             'owner'   => "root",
81             'group'   => "root",
82             'purge'   => false,
83             'recurse' => false,
84             'notify'  => 'Exec[apt_update]'
85           })
86         end
87       }
88
89       it {
90         should contain_exec("apt_update").with({
91           'command'     => "/usr/bin/apt-get update",
92           'refreshonly' => refresh_only_apt_update
93         })
94       }
95
96       it {
97         if param_hash[:disable_keys] == true
98           should create_file("99unauth").with({
99             'content' => "APT::Get::AllowUnauthenticated 1;\n",
100             'ensure'  => "present",
101             'path'    => "/etc/apt/apt.conf.d/99unauth"
102           })
103         elsif param_hash[:disable_keys] == false
104           should create_file("99unauth").with({
105             'ensure' => "absent",
106             'path'   => "/etc/apt/apt.conf.d/99unauth"
107           })
108         elsif param_hash[:disable_keys] != :undef
109           should_not create_file("99unauth").with({
110             'path'   => "/etc/apt/apt.conf.d/99unauth"
111           })
112         end
113       }
114       describe 'when setting a proxy' do
115         it {
116           if param_hash[:proxy_host]
117             should contain_file('configure-apt-proxy').with(
118               'path'    => '/etc/apt/apt.conf.d/proxy',
119               'content' => "Acquire::http::Proxy \"http://#{param_hash[:proxy_host]}:#{param_hash[:proxy_port]}\";",
120               'notify'  => "Exec[apt_update]"
121             )
122           else
123             should_not contain_file('configure_apt_proxy')
124           end
125         }
126       end
127     end
128   end
129 end