-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
455 lines (377 loc) · 12.5 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
<!doctype html>
<html lang="de">
<head>
<meta charset="utf-8">
<title>Beaker-RSpec</title>
<meta name="description" content="Puppet module acceptance testing">
<meta name="author" content="Leo Arnold">
<meta name="apple-mobile-web-app-capable" content="yes"/>
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="css/presentation.css">
<script src="js/presentation.js"></script>
</head>
<body class="auto-fragment">
<section>
<h1>Beaker-RSpec</h1>
<h3>Puppet module acceptance testing</h3>
<p style="padding-top:15%">
<a href="https://github.com/leoarnold/">Leo Arnold</a><br/>
Munich Puppet User Group<br/>
Google Munich, 2017-05-09
</p>
</section>
<section data-title="About me">
<section>
<h3>leoarnold-cups</h3>
<ul>
<li>written from scratch</li>
<li>300+ unit tests</li>
<li>200+ acceptance tests</li>
<li>independent of locale</li>
<li>features galore</li>
</ul>
</section>
<section>
<h3>Success story</h3>
<img src="img/Forge.png" width="100%"/>
</section>
<section>
<h3>Check it out</h3>
<p>
Puppet Forge:<br/>
<a href="https://forge.puppet.com/leoarnold/cups" target="_blank">https://forge.puppet.com/leoarnold/cups</a>
</p>
<p>
Source Code:<br/>
<a href="https://github.com/leoarnold/puppet-cups" target="_blank">https://github.com/leoarnold/puppet-cups</a>
</p>
</section>
</section>
<section data-title="Why acceptance tests?">
<section>
<h2><I>
"We already tested<p/>
<B>everything</B><p/>
with RSpec-Puppet ..."
</I></h2>
</section>
<section>
<h2><I>
"... what could possibly</p>
go wrong!?"
</I></h2>
</section>
<section>
<h2>Case: custom provider</h2>
</section>
<section>
<h3>Parsing output</h3>
<pre><code class="text" data-trim>
$ lpstat -l -p Office
</code></pre>
<pre><code class="text" data-trim>
printer Office disabled since Thu 30 Mar 2017 11:19:39 PM UTC -
reason unknown
Form mounted:
Content types: any
Printer types: unknown
Description: Office
Alerts: none
Location:
Connection: direct
Interface: /etc/cups/ppd/Office.ppd
[...]
</code></pre>
</section>
<section>
<h3>Gotcha: locale</h3>
<pre><code class="text" data-trim>
$ LANG="ja" lpstat -l -p Office
</code></pre>
<pre><code class="text" data-trim>
プリンター Office は Thu Mar 30 23:19:39 2017 から無効です -
未知の理由
設定されたフォーム:
コンテンツの種類: すべて
プリンターの種類: 不明
説明: Office
警告: paused
場所:
接続: 直結
インターフェイス: /etc/cups/ppd/Office.ppd
[...]
</code></pre>
</section>
<section>
<h2>Solution: Beaker</h2>
<h3>See your manifests fail in a VM,</h3>
<h3><b>not</b> in production</h3>
</section>
<section>
<h2><I>
"But I don't use<p/>
custom types or providers,<p/>
so I'm good, right?"
</I></h2>
</section>
<section>
<h2>Case: OS upgrade / change</h2>
</section>
<section>
<h3>Is <code>ipptool</code> in the <code>cups</code> package?</h3>
<table>
<thead>
<tr>
<th>Operating system</th>
<th>CUPS package</th>
<th>IPPtool package</th>
</tr>
</thead>
<tbody>
<tr>
<td>Fedora 25</td>
<td>cups</td>
<td>cups-ipptool</td>
</tr>
<tr>
<td>SLES 12</td>
<td>cups</td>
<td>---</td>
</tr>
<tr>
<td>Ubuntu ≤ 15.04</td>
<td>cups</td>
<td>---</td>
</tr>
<tr>
<td>Ubuntu ≥ 15.10</td>
<td>cups</td>
<td>cups-ipp-utils</td>
</tr>
</tbody>
</table>
</section>
<section>
<h2>Solution: ServerSpec</h2>
<pre><code class="ruby">describe service('cups') do
it { should be_running }
it { should be_enabled }
end
describe file('/usr/bin/ipptool') do
it { should be_file }
it { should be_executable }
end</code></pre>
Documentation: <a href="http://serverspec.org/resource_types.html" target="_blank">http://serverspec.org/resource_types.html</a>
</section>
</section>
<section data-title="Beaker">
<section>
<h3>Beaker</h3>
<ul>
<li>this one: <a href="https://github.com/puppetlabs/beaker" target="_blank">github.com/puppetlabs/beaker</a></li>
<li><b>NOT</b> that one: <a href="https://beaker-project.org" target="_blank">beaker-project.org</a></li>
<li>wrapper around hypervisors:
<ul>
<li>Vagrant (best practice)</li>
<li>Docker (beware)</li>
<li>vSphere</li>
<li>AWS</li>
<li>...</li>
</ul>
</li>
<li>define 1..n virtual machines in a <i>nodeset</i></li>
</ul>
</section>
<section>
<h3>Beaker nodeset</h3>
<pre><code class="yaml" data-trim># myNodes.yml
HOSTS:
MyPuppetMaster: # VM name
roles:
- master # master / agent / database / ...
platform: ubuntu-16.04-i386 # must match /^OSFAMILY-VERSION-ARCH.*$/
hypervisor: vagrant
box: bento/ubuntu-16.04-i386
MyPuppetAgent: # A second VM in the same nodeset
# ...
CONFIG:
type: foss # use Puppet community edition
log_level: debug # be *extremely* verbose</code></pre>
Documentation: <a href="https://github.com/puppetlabs/beaker/blob/master/docs/tutorials/creating_a_test_environment.md" target="_blank">creating_a_test_environment.md</a>
</section>
<section>
<h3>Beaker commands</h3>
<pre><code class="ruby" data-trim># shell - run commands via SSH
shell('/usr/games/cowsay "Hello Beaker!"')
shell('/bin/false', acceptable_exit_codes: [0, 1])
# apply_manifest
## fail when Puppet fails
apply_manifest('include cups')
## fail when Puppet fails OR succeeds without changes
apply_manifest('include cups', expect_changes: true)
## fail when Puppet fails OR applies changes successfully
apply_manifest('include cups', catch_changes: true)
# Nodeset with multiple VMs
shell_on('master', '/usr/games/cowsay "Hello Beaker!"')</ul>
apply_manifest_on('agent', 'include cups')</code></pre>
see <a href="http://www.rubydoc.info/github/puppetlabs/beaker/Beaker/DSL/Helpers" target="_blank">Beaker::DSL::Helpers</a> and
<a href="http://www.rubydoc.info/github/puppetlabs/beaker/Beaker/DSL/Helpers/PuppetHelpers" target="_blank">Beaker::DSL::Helpers::PuppetHelpers</a></li>
</section>
</section>
<section data-title="Beaker-RSpec">
<section>
<h3>Setting up Beaker-RSpec</h3>
<pre><code class="text">$ sudo apt-get install libxml2-dev libxslt1-dev g++ zlib1g-dev</code></pre>
<span class="fragment" data-fragment-index="1">
<pre><code class="ruby">#Gemfile
# ...
group :acceptance_testing do
gem 'beaker-rspec'
end</code></pre></span>
<span class="fragment" data-fragment-index="2"><pre><code class="ruby"># spec/spec_helper_acceptance.rb
require 'beaker-rspec'
RSpec.configure do |c|
project_root = File.expand_path(File.join(File.dirname(__FILE__), '..'))
c.before(:suite) do
hosts.each do |host|
install_puppet_agent_on(host)
copy_module_to(host, source: project_root, module_name: 'cups')
end
end
end</code></pre></span>
</section>
<section>
<h3>Default nodeset</h3>
<pre><code class="yaml" data-trim># spec/acceptance/nodesets/default.yml
HOSTS:
UbuntuXenial:
roles:
- master
platform: ubuntu-16.04-i386
hypervisor: vagrant
box: bento/ubuntu-16.04-i386
CONFIG:
type: foss
log_level: debug</code></pre>
</section>
<section>
<h3>Acceptance test skeleton</h3>
<pre><code class="ruby" data-trim># spec/acceptance/puppet/skeleton_spec.rb
# Run Beaker, set up VMs, ...
require 'spec_helper_acceptance'
# Start testing
describe 'My use case' do
# ...
end</code></pre>
<span class="fragment" data-fragment-index="1">
<pre><code class="text">$ [bundle exec] rspec spec/acceptance/puppet/skeleton_spec.rb</code></pre>
</span>
</section>
<section>
<h3>Environment Variables</h3>
<ul>
<li><code>BEAKER_set</code> - which nodeset should be used?</li>
<li><code>BEAKER_destroy</code>
<ul>
<li><code>"yes"</code> - destroy all VMs at teardown (default)</li>
<li><code>"no"</code> - preserve dirty VMs for analysis</li>
</ul>
</li>
<li><code>BEAKER_provision</code>
<ul>
<li><code>"yes"</code> - create a fresh VM before running tests (default)</li>
<li><code>"no"</code> - reuse existing VMs</li>
</ul>
</li>
</ul>
</section>
<section>
<h3>Rake tasks</h3>
<pre><code class="ruby" data-trim># Gemfile
# ...
group :development do
gem 'puppetlabs_spec_helper'
end</code></pre>
<span class="fragment" data-fragment-index="1"><pre><code class="ruby" data-trim># Rakefile
require 'puppetlabs_spec_helper/rake_tasks'
# ...</code></pre>
<ul>
<li><code>rake beaker</code> - run ALL acceptance tests in batch</li>
<li><code>rake beaker:ssh</code> - SSH into test system</li>
</ul></span>
</section>
</section>
<section data-title="Best practices">
<section>
<h2>Testing a class</h2>
</section>
<section>
<pre><code class="ruby" data-trim># spec/acceptance/classes/init_spec.rb
require 'spec_helper_acceptance'
describe 'Including class "cups"' do
context 'Default class inclusion' do
context 'when applying' do
it 'applies without error' do
apply_manifest('include cups')
end
it 'is idempotent' do
apply_manifest('include cups', catch_changes: true)
end
end
context 'after applying' do
describe service('cups') do
it { should be_running }
it { should be_enabled }
end
describe file('/usr/bin/ipptool') do
it { should be_file }
it { should be_executable }
end
end
end
end</code></pre>
</section>
<section>
<h2>Testing a custom provider</h2>
</section>
<section>
<pre><code class="ruby" data-trim># spec/acceptance/puppet/cups_queue_properties_spec.rb
require 'spec_helper_acceptance'
# ...
before(:all) do
shell("cupsreject -E TestPrinter")
end
manifest = <<-EOM
cups_queue { 'TestPrinter':
ensure => 'printer',
accepting => 'true'
}
EOM
it 'applies changes' do
apply_manifest(manifest, expect_changes: true)
end
it 'sets the correct value' do
expect(shell("lpoptions -p TestPrinter").stdout).to \
include('printer-is-accepting-jobs=true')
end
it 'is idempotent' do
apply_manifest(manifest, catch_changes: true)
end
# ...</code></pre>
</section>
</section>
<section>
<h1>Thank you!</h1>
<div style="height: 40%"> </div>
<p>
Slides:<br/>
<a href="https://leoarnold.github.io/talk-20170509-beaker-rspec">leoarnold.github.io/talk-20170509-beaker-rspec</a>
</p>
<p>
Source:<br/>
<a href="https://github.com/leoarnold/talk-20170509-beaker-rspec">github.com/leoarnold/talk-20170509-beaker-rspec</a>
</p>
</section>
</body>
</html>