Cleaned up unit tests.
[puppet-modules/puppetlabs-apt.git] / spec / defines / ppa_spec.rb
1 require 'spec_helper'
2 describe 'apt::ppa', :type => :define do
3
4   describe 'defaults' do
5     let :pre_condition do
6       'class { "apt": }'
7     end
8     let :facts do
9       {
10         :lsbdistrelease  => '11.04',
11         :lsbdistcodename => 'natty',
12         :operatingsystem => 'Ubuntu',
13         :osfamily        => 'Debian',
14         :lsbdistid       => 'Ubuntu',
15       }
16     end
17
18     let(:title) { 'ppa:needs/such.substitution/wow' }
19     it { is_expected.to contain_package('python-software-properties') }
20     it { is_expected.to contain_exec('add-apt-repository-ppa:needs/such.substitution/wow').that_notifies('Exec[apt_update]').with({
21       'environment' => [],
22       'command'     => '/usr/bin/add-apt-repository -y ppa:needs/such.substitution/wow',
23       'unless'      => '/usr/bin/test -s /etc/apt/sources.list.d/needs-such_substitution-wow-natty.list',
24       'user'        => 'root',
25       'logoutput'   => 'on_failure',
26     })
27     }
28
29     it { is_expected.to contain_file('/etc/apt/sources.list.d/needs-such_substitution-wow-natty.list').that_requires('Exec[add-apt-repository-ppa:needs/such.substitution/wow]').with({
30       'ensure' => 'file',
31     })
32     }
33   end
34
35   describe 'apt included, no proxy' do
36     let :pre_condition do
37       'class { "apt": }'
38     end
39     let :facts do
40       {
41         :lsbdistrelease  => '14.04',
42         :lsbdistcodename => 'trusty',
43         :operatingsystem => 'Ubuntu',
44         :lsbdistid       => 'Ubuntu',
45         :osfamily        => 'Debian',
46       }
47     end
48     let :params do
49       {
50         'options' => '',
51       }
52     end
53     let(:title) { 'ppa:foo' }
54     it { is_expected.to contain_package('software-properties-common') }
55     it { is_expected.to contain_exec('add-apt-repository-ppa:foo').that_notifies('Exec[apt_update]').with({
56       'environment' => [],
57       'command'     => '/usr/bin/add-apt-repository  ppa:foo',
58       'unless'      => '/usr/bin/test -s /etc/apt/sources.list.d/foo-trusty.list',
59       'user'        => 'root',
60       'logoutput'   => 'on_failure',
61     })
62     }
63
64     it { is_expected.to contain_file('/etc/apt/sources.list.d/foo-trusty.list').that_requires('Exec[add-apt-repository-ppa:foo]').with({
65       'ensure' => 'file',
66     })
67     }
68   end
69
70   describe 'apt included, proxy' do
71     let :pre_condition do
72       'class { "apt": proxy_host => "example.com" }'
73     end
74     let :facts do
75       {
76         :lsbdistrelease  => '14.04',
77         :lsbdistcodename => 'trusty',
78         :operatingsystem => 'Ubuntu',
79         :lsbdistid       => 'Ubuntu',
80         :osfamily        => 'Debian',
81       }
82     end
83     let :params do
84       {
85         'release' => 'lucid',
86       }
87     end
88     let(:title) { 'ppa:foo' }
89     it { is_expected.to contain_package('software-properties-common') }
90     it { is_expected.to contain_exec('add-apt-repository-ppa:foo').that_notifies('Exec[apt_update]').with({
91       'environment' => ['http_proxy=http://example.com:8080', 'https_proxy=http://example.com:8080'],
92       'command'     => '/usr/bin/add-apt-repository -y ppa:foo',
93       'unless'      => '/usr/bin/test -s /etc/apt/sources.list.d/foo-lucid.list',
94       'user'        => 'root',
95       'logoutput'   => 'on_failure',
96     })
97     }
98
99     it { is_expected.to contain_file('/etc/apt/sources.list.d/foo-lucid.list').that_requires('Exec[add-apt-repository-ppa:foo]').with({
100       'ensure' => 'file',
101     })
102     }
103   end
104
105   describe 'ensure absent' do
106     let :facts do
107       {
108         :lsbdistrelease  => '14.04',
109         :lsbdistcodename => 'trusty',
110         :operatingsystem => 'Ubuntu',
111         :lsbdistid       => 'Ubuntu',
112         :osfamily        => 'Debian',
113       }
114     end
115     let(:title) { 'ppa:foo' }
116     let :params do
117       {
118         'ensure' => 'absent'
119       }
120     end
121     it { is_expected.to contain_file('/etc/apt/sources.list.d/foo-trusty.list').that_notifies('Exec[apt_update]').with({
122       'ensure' => 'absent',
123     })
124     }
125   end
126
127   context 'validation' do
128     describe 'no release' do
129       let :facts do
130         {
131           :lsbdistrelease  => '14.04',
132           :operatingsystem => 'Ubuntu',
133           :lsbdistid       => 'Ubuntu',
134           :osfamily        => 'Debian',
135         }
136       end
137       let(:title) { 'ppa:foo' }
138       it do
139         expect {
140           should compile
141         }.to raise_error(Puppet::Error, /lsbdistcodename fact not available: release parameter required/)
142       end
143     end
144
145     describe 'not ubuntu' do
146       let :facts do
147         {
148           :lsbdistrelease  => '14.04',
149           :lsbdistcodename => 'trusty',
150           :operatingsystem => 'Debian',
151           :lsbdistid       => 'Ubuntu',
152           :osfamily        => 'Debian',
153         }
154       end
155       let(:title) { 'ppa:foo' }
156       it do
157         expect {
158           should compile
159         }.to raise_error(Puppet::Error, /apt::ppa is currently supported on Ubuntu only./)
160       end
161     end
162   end
163 end