Skip to content

Commit 3a90419

Browse files
committed
Add spec test for add-apt-repository
This commit adds spec tests that validate the changes made in the previous commit.
1 parent c26ad2a commit 3a90419

File tree

2 files changed

+83
-56
lines changed

2 files changed

+83
-56
lines changed

spec/defines/ppa_spec.rb

Lines changed: 63 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
# frozen_string_literal: true
22

33
require 'spec_helper'
4+
5+
def ppa_exec_params(user, repo, distro = 'trusty', environment = [])
6+
[
7+
environment: environment,
8+
command: "/opt/puppetlabs/puppet/cache/add-apt-repository-#{user}-ubuntu-#{repo}-#{distro}.sh",
9+
logoutput: 'on_failure',
10+
]
11+
end
12+
413
describe 'apt::ppa' do
514
let :pre_condition do
615
'class { "apt": }'
@@ -24,15 +33,13 @@
2433
}
2534
end
2635

27-
let(:title) { 'ppa:needs/such.substitution/wow+type' }
36+
let(:title) { 'ppa:needs/substitution' }
2837

2938
it { is_expected.not_to contain_package('python-software-properties') }
3039
it {
31-
is_expected.to contain_exec('add-apt-repository-ppa:needs/such.substitution/wow+type').that_notifies('Class[Apt::Update]').with(environment: [],
32-
command: '/usr/bin/add-apt-repository -y ppa:needs/such.substitution/wow+type || (rm /etc/apt/sources.list.d/needs-ubuntu-such_substitution-wow_type-trusty.list && false)', # rubocop:disable Layout/LineLength
33-
unless: '/usr/bin/test -f /etc/apt/sources.list.d/needs-ubuntu-such_substitution-wow_type-trusty.list && /usr/bin/test -f /etc/apt/trusted.gpg.d/needs_ubuntu_such_substitution-wow_type.gpg', # rubocop:disable Layout/LineLength
34-
user: 'root',
35-
logoutput: 'on_failure')
40+
is_expected.to contain_exec('add-apt-repository-ppa:needs/substitution')
41+
.that_notifies('Class[Apt::Update]')
42+
.with(*ppa_exec_params('needs', 'substitution'))
3643
}
3744
end
3845

@@ -57,24 +64,24 @@
5764
let(:title) { 'ppa:user/foo' }
5865

5966
it {
60-
is_expected.to contain_exec('add-apt-repository-ppa:user/foo').that_notifies('Class[Apt::Update]').with(environment: [],
61-
command: '/usr/bin/add-apt-repository -y ppa:user/foo || (rm /etc/apt/sources.list.d/user-ubuntu-foo-wily.list && false)', # rubocop:disable Layout/LineLength
62-
unless: '/usr/bin/test -f /etc/apt/sources.list.d/user-ubuntu-foo-wily.list && /usr/bin/test -f /etc/apt/trusted.gpg.d/user_ubuntu_foo.gpg', # rubocop:disable Layout/LineLength
63-
user: 'root',
64-
logoutput: 'on_failure')
67+
is_expected.to contain_exec('add-apt-repository-ppa:user/foo')
68+
.that_notifies('Class[Apt::Update]')
69+
.with(*ppa_exec_params('user', 'foo', 'wily'))
6570
}
6671
end
6772

6873
describe 'package_name => software-properties-common' do
6974
let :pre_condition do
7075
'class { "apt": }'
7176
end
77+
7278
let :params do
7379
{
7480
package_name: 'software-properties-common',
7581
package_manage: true,
7682
}
7783
end
84+
7885
let :facts do
7986
{
8087
os: {
@@ -92,26 +99,21 @@
9299
}
93100
end
94101

95-
let(:title) { 'ppa:needs/such.substitution/wow' }
102+
let(:title) { 'ppa:needs/substitution' }
96103

97104
it { is_expected.to contain_package('software-properties-common') }
98105
it {
99-
is_expected.to contain_exec('add-apt-repository-ppa:needs/such.substitution/wow').that_notifies('Class[Apt::Update]').with('environment' => [],
100-
'command' => '/usr/bin/add-apt-repository -y ppa:needs/such.substitution/wow || (rm /etc/apt/sources.list.d/needs-ubuntu-such_substitution-wow-trusty.list && false)', # rubocop:disable Layout/LineLength
101-
'unless' => '/usr/bin/test -f /etc/apt/sources.list.d/needs-ubuntu-such_substitution-wow-trusty.list && /usr/bin/test -f /etc/apt/trusted.gpg.d/needs_ubuntu_such_substitution-wow.gpg', # rubocop:disable Layout/LineLength
102-
'user' => 'root',
103-
'logoutput' => 'on_failure')
104-
}
105-
106-
it {
107-
is_expected.to contain_file('/etc/apt/sources.list.d/needs-ubuntu-such_substitution-wow-trusty.list').that_requires('Exec[add-apt-repository-ppa:needs/such.substitution/wow]').with('ensure' => 'file') # rubocop:disable Layout/LineLength
106+
is_expected.to contain_exec('add-apt-repository-ppa:needs/substitution')
107+
.that_notifies('Class[Apt::Update]')
108+
.with(*ppa_exec_params('needs', 'substitution'))
108109
}
109110
end
110111

111112
describe 'package_manage => false' do
112113
let :pre_condition do
113114
'class { "apt": }'
114115
end
116+
115117
let :facts do
116118
{
117119
os: {
@@ -128,25 +130,20 @@
128130
},
129131
}
130132
end
133+
131134
let :params do
132135
{
133136
package_manage: false,
134137
}
135138
end
136139

137-
let(:title) { 'ppa:needs/such.substitution/wow' }
140+
let(:title) { 'ppa:needs/substitution' }
138141

139142
it { is_expected.not_to contain_package('python-software-properties') }
140143
it {
141-
is_expected.to contain_exec('add-apt-repository-ppa:needs/such.substitution/wow').that_notifies('Class[Apt::Update]').with('environment' => [],
142-
'command' => '/usr/bin/add-apt-repository -y ppa:needs/such.substitution/wow || (rm /etc/apt/sources.list.d/needs-ubuntu-such_substitution-wow-trusty.list && false)', # rubocop:disable Layout/LineLength
143-
'unless' => '/usr/bin/test -f /etc/apt/sources.list.d/needs-ubuntu-such_substitution-wow-trusty.list && /usr/bin/test -f /etc/apt/trusted.gpg.d/needs_ubuntu_such_substitution-wow.gpg', # rubocop:disable Layout/LineLength
144-
'user' => 'root',
145-
'logoutput' => 'on_failure')
146-
}
147-
148-
it {
149-
is_expected.to contain_file('/etc/apt/sources.list.d/needs-ubuntu-such_substitution-wow-trusty.list').that_requires('Exec[add-apt-repository-ppa:needs/such.substitution/wow]').with('ensure' => 'file') # rubocop:disable Layout/LineLength
144+
is_expected.to contain_exec('add-apt-repository-ppa:needs/substitution')
145+
.that_notifies('Class[Apt::Update]')
146+
.with(*ppa_exec_params('needs', 'substitution'))
150147
}
151148
end
152149

@@ -156,6 +153,7 @@
156153
apt::ppa { "ppa:user/foo2": }
157154
'
158155
end
156+
159157
let :facts do
160158
{
161159
os: {
@@ -172,23 +170,22 @@
172170
},
173171
}
174172
end
173+
175174
let :params do
176175
{
177-
options: '',
178176
package_manage: true,
179177
require: 'Apt::Ppa[ppa:user/foo2]',
180178
}
181179
end
180+
182181
let(:title) { 'ppa:user/foo' }
183182

184183
it { is_expected.to compile.with_all_deps }
185184
it { is_expected.to contain_package('software-properties-common') }
186185
it {
187-
is_expected.to contain_exec('add-apt-repository-ppa:user/foo').that_notifies('Class[Apt::Update]').with(environment: [],
188-
command: '/usr/bin/add-apt-repository ppa:user/foo || (rm /etc/apt/sources.list.d/user-ubuntu-foo-trusty.list && false)', # rubocop:disable Layout/LineLength
189-
unless: '/usr/bin/test -f /etc/apt/sources.list.d/user-ubuntu-foo-trusty.list && /usr/bin/test -f /etc/apt/trusted.gpg.d/user_ubuntu_foo.gpg', # rubocop:disable Layout/LineLength
190-
user: 'root',
191-
logoutput: 'on_failure')
186+
is_expected.to contain_exec('add-apt-repository-ppa:user/foo')
187+
.that_notifies('Class[Apt::Update]')
188+
.with(*ppa_exec_params('user', 'foo'))
192189
}
193190
end
194191

@@ -198,6 +195,7 @@
198195
proxy => { "host" => "localhost" },
199196
}'
200197
end
198+
201199
let :facts do
202200
{
203201
os: {
@@ -214,21 +212,20 @@
214212
},
215213
}
216214
end
215+
217216
let :params do
218217
{
219-
'options' => '',
220218
'package_manage' => true,
221219
}
222220
end
221+
223222
let(:title) { 'ppa:user/foo' }
224223

225224
it { is_expected.to contain_package('software-properties-common') }
226225
it {
227-
is_expected.to contain_exec('add-apt-repository-ppa:user/foo').that_notifies('Class[Apt::Update]').with(environment: ['http_proxy=http://localhost:8080'],
228-
command: '/usr/bin/add-apt-repository ppa:user/foo || (rm /etc/apt/sources.list.d/user-ubuntu-foo-trusty.list && false)', # rubocop:disable Layout/LineLength
229-
unless: '/usr/bin/test -f /etc/apt/sources.list.d/user-ubuntu-foo-trusty.list && /usr/bin/test -f /etc/apt/trusted.gpg.d/user_ubuntu_foo.gpg', # rubocop:disable Layout/LineLength
230-
user: 'root',
231-
logoutput: 'on_failure')
226+
is_expected.to contain_exec('add-apt-repository-ppa:user/foo')
227+
.that_notifies('Class[Apt::Update]')
228+
.with(*ppa_exec_params('user', 'foo', 'trusty', ['http_proxy=http://localhost:8080']))
232229
}
233230
end
234231

@@ -238,6 +235,7 @@
238235
proxy => { "host" => "localhost", "port" => 8180 },
239236
}'
240237
end
238+
241239
let :facts do
242240
{
243241
os: {
@@ -254,21 +252,20 @@
254252
},
255253
}
256254
end
255+
257256
let :params do
258257
{
259-
options: '',
260258
package_manage: true,
261259
}
262260
end
261+
263262
let(:title) { 'ppa:user/foo' }
264263

265264
it { is_expected.to contain_package('software-properties-common') }
266265
it {
267-
is_expected.to contain_exec('add-apt-repository-ppa:user/foo').that_notifies('Class[Apt::Update]').with(environment: ['http_proxy=http://localhost:8180'],
268-
command: '/usr/bin/add-apt-repository ppa:user/foo || (rm /etc/apt/sources.list.d/user-ubuntu-foo-trusty.list && false)', # rubocop:disable Layout/LineLength
269-
unless: '/usr/bin/test -f /etc/apt/sources.list.d/user-ubuntu-foo-trusty.list && /usr/bin/test -f /etc/apt/trusted.gpg.d/user_ubuntu_foo.gpg', # rubocop:disable Layout/LineLength
270-
user: 'root',
271-
logoutput: 'on_failure')
266+
is_expected.to contain_exec('add-apt-repository-ppa:user/foo')
267+
.that_notifies('Class[Apt::Update]')
268+
.with(*ppa_exec_params('user', 'foo', 'trusty', ['http_proxy=http://localhost:8180']))
272269
}
273270
end
274271

@@ -278,6 +275,7 @@
278275
proxy => { "host" => "localhost", "port" => 8180, "https" => true },
279276
}'
280277
end
278+
281279
let :facts do
282280
{
283281
os: {
@@ -294,28 +292,28 @@
294292
},
295293
}
296294
end
295+
297296
let :params do
298297
{
299-
options: '',
300298
package_manage: true,
301299
}
302300
end
301+
303302
let(:title) { 'ppa:user/foo' }
304303

305304
it { is_expected.to contain_package('software-properties-common') }
306305
it {
307-
is_expected.to contain_exec('add-apt-repository-ppa:user/foo').that_notifies('Class[Apt::Update]').with(environment: ['http_proxy=http://localhost:8180', 'https_proxy=https://localhost:8180'],
308-
command: '/usr/bin/add-apt-repository ppa:user/foo || (rm /etc/apt/sources.list.d/user-ubuntu-foo-trusty.list && false)', # rubocop:disable Layout/LineLength
309-
unless: '/usr/bin/test -f /etc/apt/sources.list.d/user-ubuntu-foo-trusty.list && /usr/bin/test -f /etc/apt/trusted.gpg.d/user_ubuntu_foo.gpg', # rubocop:disable Layout/LineLength
310-
user: 'root',
311-
logoutput: 'on_failure')
306+
is_expected.to contain_exec('add-apt-repository-ppa:user/foo')
307+
.that_notifies('Class[Apt::Update]')
308+
.with(*ppa_exec_params('user', 'foo', 'trusty', ['http_proxy=http://localhost:8180', 'https_proxy=https://localhost:8180']))
312309
}
313310
end
314311

315312
describe 'ensure absent' do
316313
let :pre_condition do
317314
'class { "apt": }'
318315
end
316+
319317
let :facts do
320318
{
321319
os: {
@@ -332,15 +330,22 @@
332330
},
333331
}
334332
end
333+
335334
let(:title) { 'ppa:user/foo' }
335+
336336
let :params do
337337
{
338338
ensure: 'absent',
339339
}
340340
end
341341

342342
it {
343-
is_expected.to contain_file('/etc/apt/sources.list.d/user-ubuntu-foo-trusty.list').that_notifies('Class[Apt::Update]').with(ensure: 'absent')
343+
is_expected.to contain_tidy("remove-apt-repository-script-#{title}")
344+
.with('path' => '/opt/puppetlabs/puppet/cache/add-apt-repository-user-ubuntu-foo-trusty.sh')
345+
346+
is_expected.to contain_tidy("remove-apt-repository-#{title}")
347+
.with('path' => '/etc/apt/sources.list.d/user-ubuntu-foo-trusty.list')
348+
.that_notifies('Class[Apt::Update]')
344349
}
345350
end
346351

@@ -362,6 +367,7 @@
362367
},
363368
}
364369
end
370+
365371
let(:title) { 'ppa:user/foo' }
366372

367373
it do
@@ -386,6 +392,7 @@
386392
},
387393
}
388394
end
395+
389396
let(:title) { 'ppa:user/foo' }
390397

391398
it do

spec/unit/facter/apt_sources_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'apt_sources fact' do
6+
subject { Facter.fact(:apt_sources).value }
7+
8+
before(:each) { Facter.clear }
9+
10+
describe 'returns a list of .list files' do
11+
let(:sources_raw) { ['/etc/apt/sources.list.d/puppet-tools.list', '/etc/apt/sources.list.d/some-cli.list'] }
12+
let(:sources_want) { ['sources.list', 'puppet-tools.list', 'some-cli.list'] }
13+
14+
before(:each) do
15+
allow(Dir).to receive(:glob).and_return(sources_raw)
16+
end
17+
18+
it { is_expected.to eq(sources_want) }
19+
end
20+
end

0 commit comments

Comments
 (0)