Testing Your Automation Code (Docker Version)
-
Upload
mischa-taylor -
Category
Technology
-
view
1.408 -
download
3
description
Transcript of Testing Your Automation Code (Docker Version)
![Page 1: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/1.jpg)
v0.0.1
Testing YourAutomation CodeMischa Taylor (@misheska)[email protected] (C) 2014 Chef Software, Inc.
1
Wednesday, April 30, 14
![Page 2: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/2.jpg)
v2.0.0
Why Are You Here?
2
Wednesday, April 30, 14
![Page 3: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/3.jpg)
3
You’ve used and want to
bulletproofyour so that
you are with
Spec
Wednesday, April 30, 14
![Page 4: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/4.jpg)
In this class• We’ll add tests to the apache cookbook from the
Fundamentals Course• We’ll show you how to run cookbooks in a sandbox
environment mirroring production with Test Kitchen
• We’ll show you how to detect suspicious cookbook code with Foodcritic & RuboCop
• We’ll show you how to produce runnable documentation with ChefSpec
4
Wednesday, April 30, 14
![Page 5: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/5.jpg)
Using Chef is half the battle
5
“Chef is like....tests for your infrastructure”
-Ezra Zygmuntowicz, Co-Founder Engine Yard
http://www.akitaonrails.com/2008/6/5/railsconf-2008-brazil-rails-podcast-special-edition#.U0HfiF7Ed-8
Wednesday, April 30, 14
![Page 6: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/6.jpg)
Chef makes things more testable• Chef automates infrastructure in a repeatable
fashion
6
Wednesday, April 30, 14
![Page 7: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/7.jpg)
What’s the other half of the battle?
7
“Have a plan”-Adam Jacob, Co-Founder Chef
Wednesday, April 30, 14
![Page 8: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/8.jpg)
There’s no more magic to testing
8http://www.flickr.com/photos/dkeats/4128747046/sizes/s/in/photostream/
Wednesday, April 30, 14
![Page 9: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/9.jpg)
You Are The Testing A-Team When...
9
Wednesday, April 30, 14
![Page 10: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/10.jpg)
Recommended plan• Build in quality and robustness up front
10
https://flic.kr/p/8W67ZC
Wednesday, April 30, 14
![Page 11: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/11.jpg)
Otherwise you could...• Verify and validate just before going to production
until time runs out. But time always runs out
11http://mrg.bz/iEr1oj
Wednesday, April 30, 14
![Page 12: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/12.jpg)
Waiting to test when it’s “done”
12
Intention:
Reality:
Build Test Deploy
Build Tes
Deploy
We’re lateno timeto test!
Wednesday, April 30, 14
![Page 13: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/13.jpg)
13
Wednesday, April 30, 14
![Page 14: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/14.jpg)
14
Wednesday, April 30, 14
![Page 15: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/15.jpg)
Bake testing in earlier
15
Shorter cycles,to start testing early as possible:
Build Test Deploy Build Test Deploy Build Test Deploy
Wednesday, April 30, 14
![Page 16: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/16.jpg)
Penny saved with testing
16
Up Front Testing Saves Money
https://www.flickr.com/people/68751915@N05/
Wednesday, April 30, 14
![Page 17: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/17.jpg)
Cost of change
17
Wednesday, April 30, 14
![Page 18: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/18.jpg)
Test arrangement• Arrange tests to get feedback fast - at the earliest
possible time
18
seconds
minutes
hours
Foodcritic/Rubocop
ChefSpec
Serverspec
Wednesday, April 30, 14
![Page 19: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/19.jpg)
Reason for multiple tools• Finding a bug in something that you can’t execute
is freaking hard!• While fixing bugs before writing code is cheap,
finding them is expensive
19
Wednesday, April 30, 14
![Page 20: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/20.jpg)
The Tools• Each tool is specialized to give feedback as early
as possible during the cookbook authoring process
20
Wednesday, April 30, 14
![Page 21: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/21.jpg)
What each tool does• In your text editor when you type in cookbook code:
• Foodcritic analyzes your Chef style• RuboCop analyzes your Ruby coding technique
• Before you deploy to a test node:• ChefSpec helps you document and organize your
code• After you deploy to a test node:
• Serverspec verifies a cookbook behaves as intended
21
Wednesday, April 30, 14
![Page 22: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/22.jpg)
v2.0.0
Legend
22
Wednesday, April 30, 14
![Page 23: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/23.jpg)
Legend: Do I run that command on my workstation?
$ whoamii-am-a-workstation
This is an example of a command to run on your workstation
user@hostname:~$ whoamii-am-a-chef-node
This is an example of a command to run on your target node via SSH.
23
Wednesday, April 30, 14
![Page 24: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/24.jpg)
$ ifconfig
Legend: Example Terminal Command and Output
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384! options=3<RXCSUM,TXCSUM>! inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1! inet 127.0.0.1 netmask 0xff000000! inet6 ::1 prefixlen 128gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280stf0: flags=0<> mtu 1280en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500! ether 28:cf:e9:1f:79:a3! inet6 fe80::2acf:e9ff:fe1f:79a3%en0 prefixlen 64 scopeid 0x4! inet 10.100.0.84 netmask 0xffffff00 broadcast 10.100.0.255! media: autoselect! status: activep2p0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 2304! ether 0a:cf:e9:1f:79:a3! media: autoselect! status: inactive
24
Wednesday, April 30, 14
![Page 25: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/25.jpg)
OPEN IN EDITOR:
SAVE FILE!
~/hello_world
Hi!
I am a friendly file.
Legend: Example of editing a file on your workstation
25
Wednesday, April 30, 14
![Page 26: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/26.jpg)
v2.0.0
Training Environment (CloudShare)
26
Wednesday, April 30, 14
![Page 27: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/27.jpg)
CloudShare Node• Register and login to CloudShare (see invite)• Start Using This Environment
27
Wednesday, April 30, 14
![Page 28: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/28.jpg)
Two Virtual Machines
28
Wednesday, April 30, 14
![Page 29: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/29.jpg)
Two Virtual Machines
29
We’ll just use the Ubuntuw/Docker image
Wednesday, April 30, 14
![Page 30: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/30.jpg)
30
The Ubuntu w/Docker VM
will be your Chef Development
Workstationfor this class
Wednesday, April 30, 14
![Page 31: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/31.jpg)
CloudShare Development Workstation
31
Wednesday, April 30, 14
![Page 32: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/32.jpg)
CloudShare Node
32
Wednesday, April 30, 14
![Page 33: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/33.jpg)
$ ssh sysadmin@<EXTERNAL_ADDRESS>
Lab - Login
The authenticity of host 'uvo1qrwls0jdgs3blvt.vm.cld.sr (69.195.232.110)' can't be established.RSA key fingerprint is d9:95:a3:b9:02:27:e9:cd:74:e4:a2:34:23:f5:a6:8b.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added 'uvo1qrwls0jdgs3blvt.vm.cld.sr,69.195.232.110' (RSA) to the list of known [email protected]'s password: Last login: Mon Jan 6 16:26:24 2014 from host86-145-117-53.range86-145.btcentralplus.com[chef@CentOS63 ~]$
33
Wednesday, April 30, 14
![Page 34: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/34.jpg)
Checkpoint • At this point you should have
• One virtual machine (VM) or server that you’ll use for the lab exercises
• The IP address or public hostname• An application for establishing an ssh connection• 'sudo' or 'root' permissions on the VM
34
Wednesday, April 30, 14
![Page 35: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/35.jpg)
v2.0.0
Workstation Setup - Linux (Docker)Getting started
35
Wednesday, April 30, 14
![Page 36: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/36.jpg)
Base OS Install• Ubuntu and Enterprise Linux support the kernel
extensions needed for Docker (64-bit only).• Ubuntu is recommended if you are using a VM. The
Enterprise Linux package uses the 3.14 kernel which has issues working with the VMware and VirtualBox guest tools.
36
Wednesday, April 30, 14
![Page 37: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/37.jpg)
Base OS Install• Recommended Packer templates for building the
base OS with Docker:• Ubuntu Server:
https://github.com/misheska/basebox-packer/blob/master/template/ubuntu/ubuntu1204-docker.json
• Vagrant Cloud: vagrant init learningchef/ubuntu1204-‐docker
37
Wednesday, April 30, 14
![Page 38: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/38.jpg)
Install Chef• Install Chef (if not already installed)
http://www.getchef.com/chef/install
38
Wednesday, April 30, 14
![Page 39: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/39.jpg)
Install Chef Client
39
Wednesday, April 30, 14
![Page 40: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/40.jpg)
Install on Linux
40
Wednesday, April 30, 14
![Page 41: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/41.jpg)
$ curl -L http://www.getchef.com/chef/install.sh | sudo bash
Workstation Setup - Linux
% Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed100 14398 100 14398 0 0 56535 0 --:--:-- --:--:-- --:--:-- 78250
Downloading Chef for ubuntu...Installing Chef installing with dpkg...Selecting previously unselected package chef.(Reading database ... 78644 files and directories currently installed.)Unpacking chef (from .../chef_11.12.2-1_amd64.deb) ...Setting up chef (11.12.2-1) ...Thank you for installing Chef!
41
Wednesday, April 30, 14
![Page 42: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/42.jpg)
What just happened?• Chef and all of its dependencies installed via an
operating system-specific package ("omnibus installer")• Installation includes
• The Ruby language - used by Chef• knife - Command line tool for administrators• chef-client - Client application• ohai - System profiler• ...and more
42
Wednesday, April 30, 14
![Page 43: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/43.jpg)
OPEN IN EDITOR: $HOME/.bash_profile
export PATH="/opt/chef/embedded/bin:$PATH"
Add Chef Client to PATH
43
Wednesday, April 30, 14
![Page 44: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/44.jpg)
$ source $HOME/.bash_profile
Make the profile active
44
Wednesday, April 30, 14
![Page 45: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/45.jpg)
$ which ruby
Check the PATH setting
/opt/chef/embedded/bin/ruby
45
Wednesday, April 30, 14
![Page 46: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/46.jpg)
$ chef-client --version
Verify Install
Chef: 11.12.2
46
Wednesday, April 30, 14
![Page 47: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/47.jpg)
Install Developer Tools• Install Developer Tools to build native extensions
47
http://patshaughnessy.net/2011/10/31/dont-be-terrified-of-building-native-extensions
Wednesday, April 30, 14
![Page 48: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/48.jpg)
Install Developer Tools• Use build-essential cookbook:
https://github.com/opscode-cookbooks/build-essential
48
Wednesday, April 30, 14
![Page 49: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/49.jpg)
$ mkdir /tmp/cookbooks
Temporary area for cookbook downloads
49
Wednesday, April 30, 14
![Page 50: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/50.jpg)
$ cd /tmp/cookbooks
Easiest to go to cookbooks dir
50
Wednesday, April 30, 14
![Page 51: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/51.jpg)
$ knife cookbook site download build-essential$ tar xvf build-essential-*.tar.gz
Download build-essential cookbook
51
Wednesday, April 30, 14
![Page 52: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/52.jpg)
$ cd /tmp$ sudo chef-client -z -o build-essential
Use local mode to run cookbook
52
Wednesday, April 30, 14
![Page 53: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/53.jpg)
$ gcc --version
Verify Install
gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
53
Wednesday, April 30, 14
![Page 54: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/54.jpg)
$ cd /tmp/cookbooks
Easiest to go to cookbooks dir
54
Wednesday, April 30, 14
![Page 55: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/55.jpg)
$ knife cookbook site download git$ knife cookbook site download dmg$ knife cookbook site download windows$ knife cookbook site download runit$ knife cookbook site download yum$ knife cookbook site download yum-epel$ knife cookbook site download chef_handler
Download git cookbook + dependencies
55
Wednesday, April 30, 14
![Page 56: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/56.jpg)
$ tar xvf git*.tar.gz$ tar xvf dmg*.tar.gz$ tar xvf windows*.tar.gz$ tar xvf runit*.tar.gz$ tar xvf yum-3*.tar.gz$ tar xvf yum-epel*.tar.gz$ tar xvf chef_handler*.tar.gz
Extract cookbook archives
56
Wednesday, April 30, 14
![Page 57: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/57.jpg)
$ cd /tmp$ sudo chef-client -z -o git
Use local mode to run cookbook
57
Wednesday, April 30, 14
![Page 58: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/58.jpg)
$ git --version
Verify Install
git version 1.7.9.5
58
Wednesday, April 30, 14
![Page 59: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/59.jpg)
$ rm -rf /tmp/cookbooks
Keep it neat
59
Wednesday, April 30, 14
![Page 60: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/60.jpg)
v2.0.0
Our Source
60
Wednesday, April 30, 14
![Page 61: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/61.jpg)
chef-fundamentals-repo• We’re going to build on the chef-fundamentals-
repo created in the Chef Fundamentals training, adding tests:
https://github.com/learnchef/chef-fundamentals-repo/tree/master/cookbooks/apache
61
Wednesday, April 30, 14
![Page 62: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/62.jpg)
$ cd $HOME
Home directory - great place for source
62
$ cd %USERPROFILE%
Wednesday, April 30, 14
![Page 63: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/63.jpg)
$ git clone https://github.com/learnchef/chef-fundamentals-repo.git
Grab the chef-fundamentals repo from GitHub
Cloning into 'chef-fundamentals-repo'...remote: Reusing existing pack: 247, done.remote: Total 247 (delta 0), reused 0 (delta 0)Receiving objects: 100% (247/247), 149.52 KiB | 184.00 KiB/s, done.Resolving deltas: 100% (45/45), done.Checking connectivity... done.
63
Wednesday, April 30, 14
![Page 64: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/64.jpg)
$ cd chef-fundamentals-repo
chef-fundamentals-repo
64
!"" Berksfile!"" cookbooks/!"" data_bags/!"" environments/!"" .git/!"" README.md!"" roles/#"" Vagrantfile
Wednesday, April 30, 14
![Page 65: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/65.jpg)
v2.0.0
Reviewing Cookbooks with Test Kitchen (Docker Version)
65
Wednesday, April 30, 14
![Page 66: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/66.jpg)
Apache Clowns & Bears• We’ll be focusing on the Apache Clowns and
Bears cookbook in:
chef-‐fundamentals-‐repo/cookbooks/apache
66
Wednesday, April 30, 14
![Page 67: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/67.jpg)
Cookbook Review with Test Kitchen• Test Kitchen is a great environment in which to
develop and review cookbooks• Written by Fletcher Nichol
67
Wednesday, April 30, 14
![Page 68: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/68.jpg)
Back in my day...• Running a cookbook
involved a lot of setup:• Configure workstation• Configure Chef Server• Bootstrap node
68
Wednesday, April 30, 14
![Page 69: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/69.jpg)
No longer a chore• Test Kitchen lets you set up sandbox environments
in which to run cookbooks right on your Chef development workstation
69
Wednesday, April 30, 14
![Page 70: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/70.jpg)
Supported Environments• Test Kitchen supports:
• Virtual Machines
• Cloud Instances
• Metal - Physical Servers
• Containers (Docker, LXC, etc.)70
Wednesday, April 30, 14
![Page 71: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/71.jpg)
Sandbox Benefits• A sandbox environment:
• Is a safe place to make mistakes• Easily reset to a clean config• Can simulate production
71
Wednesday, April 30, 14
![Page 72: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/72.jpg)
Virtual Machines vs. Containers
72
Hypervisor/Host OS
Guest OS
Core OS
Guest OS Guest OS
App App App
App App App
Virtual Machines Containers
Wednesday, April 30, 14
![Page 73: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/73.jpg)
73
Test Kitchen is packaged as a
Ruby Gem
Wednesday, April 30, 14
![Page 74: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/74.jpg)
Ruby Gem• A gem is a application or supporting library
written in ruby, installable with:
74
gem install
Wednesday, April 30, 14
![Page 75: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/75.jpg)
$ sudo gem install test-kitchen --no-ri --no-rdoc
Install Test Kitchen
75
$ sudo env "PATH=$PATH" gem install test-kitchen --no-ri --no-rdoc
Run As Administrator
> gem install test-kitchen --no-ri --no-rdoc
Wednesday, April 30, 14
![Page 76: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/76.jpg)
Install Test Kitchen
76
Fetching: net-scp-1.1.2.gem (100%)Fetching: safe_yaml-1.0.2.gem (100%)Fetching: thor-0.19.1.gem (100%)Fetching: test-kitchen-1.2.1.gem (100%)Successfully installed net-scp-1.1.2Successfully installed safe_yaml-1.0.2Successfully installed thor-0.19.1Successfully installed test-kitchen-1.2.14 gems installed
Text
Output:
Wednesday, April 30, 14
![Page 77: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/77.jpg)
OPEN IN EDITOR: $HOME/.gemrc
gem: --no-ri --no-rdoc
Automatically add --no-ri & --no-rdoc
77
Wednesday, April 30, 14
![Page 78: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/78.jpg)
$ cd $HOME/chef-fundamentals-repo/cookbooks/apache
Go to Apache cookbook dir
78
Wednesday, April 30, 14
![Page 79: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/79.jpg)
79
We’re going to need a lot of gems
Wednesday, April 30, 14
![Page 80: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/80.jpg)
Gemfile• A Gemfile can be used to list all the gems you need
for cookbook testing
80
Wednesday, April 30, 14
![Page 81: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/81.jpg)
Gemfile template• Test Kitchen can generate a Gemfile template
(among other things)
81
Wednesday, April 30, 14
![Page 82: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/82.jpg)
$ kitchen init --driver=kitchen-docker --create-gemfile
create .kitchen.yml create test/integration/default create Gemfile append Gemfile append GemfileYou must run ‘bundle install’ to fetch any new gems.
Create Gemfile template
82
Wednesday, April 30, 14
![Page 83: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/83.jpg)
chef-fundamentals-repo/cookbooks/apache/Gemfile
source 'https://rubygems.org'
gem 'test-kitchen'gem 'kitchen-docker'
83
Wednesday, April 30, 14
![Page 84: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/84.jpg)
$ kitchen init --driver=kitchen-docker --create-gemfile
create .kitchen.yml create test/integration/default create Gemfile append Gemfile append GemfileYou must run ‘bundle install’ to fetch any new gems.
Create Gemfile template
84
Wednesday, April 30, 14
![Page 85: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/85.jpg)
85
Wednesday, April 30, 14
![Page 86: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/86.jpg)
86
Gemfile is read by:
bundle install
to install gems
Wednesday, April 30, 14
![Page 87: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/87.jpg)
87
No need to install bundler• Chef install includes the bundler gem• Outside the Chef install, bundler can be installed via:•gem install bundler
Wednesday, April 30, 14
![Page 88: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/88.jpg)
$ bundle install --path vendor/bundle
Do what Test Kitchen tells you
Fetching gem metadata from https://rubygems.org/..........Fetching gem metadata from https://rubygems.org/..Installing mixlib-shellout (1.3.0)Installing net-ssh (2.8.0)Installing net-scp (1.1.2)Installing safe_yaml (1.0.2)Installing thor (0.19.1)Installing test-kitchen (1.2.1)Installing kitchen-docker (1.0.0)Using bundler (1.1.5)Your bundle is complete! It was installed into ./vendor/bundle
88
Wednesday, April 30, 14
![Page 89: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/89.jpg)
89
Vendor Everything• Gems change frequently and sometimes conflict• -‐-‐path option passed to bundle install
overrides system gems by installing Gemfile gems locally
• Bundler docs recommend using vendor/bundle
Wednesday, April 30, 14
![Page 90: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/90.jpg)
90
If you vendor gems use:
bundle exec
to read gems invendor/bunde
Wednesday, April 30, 14
![Page 91: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/91.jpg)
91
Test Kitchen commands• Let’s cover a few essential Test Kitchen commands
Wednesday, April 30, 14
![Page 92: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/92.jpg)
92
kitchen list• kitchen list prints out a list of sandbox instances
defined in .kitchen.yml
Wednesday, April 30, 14
![Page 93: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/93.jpg)
$ bundle exec kitchen list
kitchen list
Instance Driver Provisioner Last Actiondefault-ubuntu-1204 Docker ChefSolo <Not Created>default-centos-64 Docker ChefSolo <Not Created>
93
Wednesday, April 30, 14
![Page 94: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/94.jpg)
94
kitchen create• kitchen create <instance_name> spins up a
sandbox instance
Wednesday, April 30, 14
![Page 95: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/95.jpg)
$ bundle exec kitchen create default-centos-64
kitchen create
-----> Starting Kitchen (v1.2.1)-----> Creating <default-centos-64>... Step 0 : FROM centos:6.4 ---> 539c0211cd76 Step 1 : RUN yum clean all ---> Using cache ---> 9a4da62cec5e... "Links": null, "PublishAllPorts": false } }] Finished creating <default-centos-64> (0m4.55s).-----> Kitchen is finished. (0m4.66s)
95
Wednesday, April 30, 14
![Page 96: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/96.jpg)
96
kitchen login• kitchen login <instance_name> uses ssh to
login to the sandbox instance
Wednesday, April 30, 14
![Page 97: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/97.jpg)
$ bundle exec kitchen login default-centos-64
kitchen login
kitchen@localhost's password:[kitchen@fc9834984e9d ~]$ cat /etc/redhat-releaseCentOS release 6.4 (Final)[kitchen@fc9834984e9d ~]$ exitlogoutConnection to localhost closed.
97
Default password is kitchen
Wednesday, April 30, 14
![Page 98: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/98.jpg)
98
kitchen destroy• kitchen destroy <instance_name> shuts down a
sandbox instance and destroys and virtual resources allocated
Wednesday, April 30, 14
![Page 99: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/99.jpg)
$ bundle exec kitchen destroy default-centos-64
kitchen destroy
-----> Starting Kitchen (v1.2.1)-----> Destroying <default-centos-64>... 3d71e7241e317d374f42ac0fc79c0752cb77eb86b4bfd76c068beebdb151dda3 3d71e7241e317d374f42ac0fc79c0752cb77eb86b4bfd76c068beebdb151dda3 Finished destroying <default-centos-64> (0m2.90s).-----> Kitchen is finished. (0m3.07s)
99
Wednesday, April 30, 14
![Page 100: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/100.jpg)
100
kitchen converge
performs a Chef runin the sandbox instance
Wednesday, April 30, 14
![Page 101: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/101.jpg)
$ bundle exec kitchen converge default-centos-64
Perform Chef run
-----> Starting Kitchen (v1.2.1)-----> Creating <default-centos-64>... Step 0 : FROM centos:6.4 ---> 539c0211cd76 Step 1 : RUN yum clean all ---> Using cache ---> 735ba0db794e....
101
Wednesday, April 30, 14
![Page 102: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/102.jpg)
$ bundle exec kitchen converge default-centos-64
Perform Chef run
-----> Starting Kitchen (v1.2.1)-----> Creating <default-centos-64>... Step 0 : FROM centos:6.4 ---> 539c0211cd76 Step 1 : RUN yum clean all ---> Using cache ---> 735ba0db794e....[2014-04-07T02:32:50-04:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)>>>>>> Converge failed on instance <default-centos-64>.>>>>>> Please see .kitchen/logs/default-centos-64.log for more details>>>>>> ------Exception------->>>>>> Class: Kitchen::ActionFailed>>>>>> Message: SSH exited (1) for command: [sudo -E chef-solo --config /tmp/kitchen/solo.rb --json-attributes /tmp/kitchen/dna.json --log_level info]>>>>>> ----------------------
102
FAILWednesday, April 30, 14
![Page 103: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/103.jpg)
103
Our first bit of feedback!
Wednesday, April 30, 14
![Page 104: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/104.jpg)
104
Wednesday, April 30, 14
![Page 105: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/105.jpg)
105
Undefined attribute• When you see undefined method ‘[]’ for nil:NilClass it often means you have an undefined attribute
• Let’s see if node["motd"]["company"] is being set
Wednesday, April 30, 14
![Page 106: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/106.jpg)
cookbooks/apache/attributes/default.rb
106
default["apache"]["indexfile"] = "index1.html"
default["apache"]["sites"]["clowns"] = { "port" => 80 }
default["apache"]["sites"]["bears"] = { "port" => 81 }
NOPEWednesday, April 30, 14
![Page 107: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/107.jpg)
107
Undefined attribute• How is ["motd"]["company"] set in the motd
cookbook?
Wednesday, April 30, 14
![Page 108: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/108.jpg)
cookbooks/motd/attributes/default.rb
108
default["motd"]["company"] = "Chef"
Wednesday, April 30, 14
![Page 109: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/109.jpg)
Good cookbooks• Good cookbooks can be used in isolation• They set reasonable defaults for all attributes used• Test Kitchen is designed to run a cookbook in
isolation to give you feedback on attribute use
109
Wednesday, April 30, 14
![Page 110: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/110.jpg)
Test attributes• We won’t fix the motd cookbook in this class• Test Kitchen supports injection of test attributes• We’ll supply the correct attribute in
the .kitchen.yml configuration file
110
Wednesday, April 30, 14
![Page 111: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/111.jpg)
Default .kitchen.yml
111
---driver: name: docker
provisioner: name: chef_solo
platforms: - name: ubuntu-12.04 - name: centos-6.4
suites: - name: default run_list: - recipe[apache::default] attributes:
Wednesday, April 30, 14
![Page 112: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/112.jpg)
.kitchen.yml is a tab-free zone• Spacing matters in a .kitchen.yml file.• .kitchen.yml is a tab-free zone - you will get errors
if you try to use tabs. Set your editor accordingly.
112
Wednesday, April 30, 14
![Page 113: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/113.jpg)
---driver: name: docker
provisioner: name: chef_solo
platforms: - name: ubuntu-12.04 - name: centos-6.4
suites: - name: default run_list: - recipe[apache::default] attributes: motd: {company: Chef}
cookbooks/apache/.kitchen.yml
Set node[“motd”][“company”] (docker)
113
OPEN IN EDITOR:
Wednesday, April 30, 14
![Page 114: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/114.jpg)
$ bundle exec kitchen converge default-centos-64
Perform Chef run
-----> Starting Kitchen (v1.2.1)-----> Converging <default-centos-64>... Preparing files for transfer Preparing current project directory as a cookbook Removing non-cookbook files before transfer Transfering files to <default-centos-64>[2014-04-07T02:40:01-04:00] INFO: Forking chef instance to converge...Starting Chef Client, version 11.10.4....
114
Wednesday, April 30, 14
![Page 115: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/115.jpg)
$ bundle exec kitchen converge default-centos-64
Perform Chef run
-----> Starting Kitchen (v1.2.1)-----> Converging <default-centos-64>... Preparing files for transfer Preparing current project directory as a cookbook Removing non-cookbook files before transfer Transfering files to <default-centos-64>[2014-04-07T02:40:01-04:00] INFO: Forking chef instance to converge...Starting Chef Client, version 11.10.4....[2014-04-07T02:40:03-04:00] INFO: Report handlers completeChef Client finished, 5/10 resources updated in 2.52587913 seconds Finished converging <default-centos-64> (0m4.15s).-----> Kitchen is finished. (0m4.22s)
115
WINWednesday, April 30, 14
![Page 116: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/116.jpg)
Where to go next• Learning Chef book excerpt
was sent to you are part of the class registration.
• Chapter 1 covers Test Kitchen and .kitchen.yml format in more detail.
• Appendix provides sample .kitchen.yml configs
116
Wednesday, April 30, 14
![Page 117: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/117.jpg)
Where to go next• Vagrant boxes for training image:
• VirtualBox 4.3.10https://s3-us-west-2.amazonaws.com/misheska/vagrant/virtualbox4.3.10/ubuntu1204-docker-chef11.12.2.box
• VMware Fusion 6/Workstation 10https://s3-us-west-2.amazonaws.com/misheska/vagrant/vmware9.6.1/ubuntu1204-docker-chef11.12.2.box
• Packer Templatehttps://github.com/misheska/basebox-packer/blob/master/template/ubuntu/ubuntu1204-docker.json
117
Wednesday, April 30, 14
![Page 118: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/118.jpg)
Gemfile
118
source 'https://rubygems.org'
gem 'test-kitchen'gem 'kitchen-vagrant'
Wednesday, April 30, 14
![Page 119: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/119.jpg)
VirtualBox - .kitchen.yml
119
---driver: name: vagrant
provisioner: name: chef_solo
platforms: - name: ubuntu1204-docker driver: box: ubuntu1204-docker box_url: https://s3-us-west-2.amazonaws.com/misheska/vagrant/vmware9.6.1/ubuntu1204-docker-chef11.12.2.box customize: memory: 1024
suites: - name: default
Wednesday, April 30, 14
![Page 120: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/120.jpg)
VMware Fusion 6 - .kitchen.yml
120
---driver: name: vagrant
provisioner: name: chef_solo
platforms: - name: ubuntu1204-docker driver: provider: vmware_fusion box: ubuntu1204-docker box_url: https://s3-us-west-2.amazonaws.com/misheska/vagrant/vmware9.6.1/ubuntu1204-docker-chef11.12.2.box customize: memory: 1024
suites: - name: default
Wednesday, April 30, 14
![Page 121: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/121.jpg)
VMware Workstation 10 - .kitchen.yml
121
---driver: name: vagrant
provisioner: name: chef_solo
platforms: - name: ubuntu1204-docker driver: provider: vmware_workstation box: ubuntu1204-docker box_url: https://s3-us-west-2.amazonaws.com/misheska/vagrant/vmware9.6.1/ubuntu1204-docker-chef11.12.2.box customize: memory: 1024
suites: - name: default
Wednesday, April 30, 14
![Page 122: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/122.jpg)
v2.0.0
Automated Verification in Test Kitchen with Serverspec (Docker Version)
122
Wednesday, April 30, 14
![Page 123: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/123.jpg)
123
Let’s verify that the Apache cookbookactually works by
configuring Test Kitchen to allowweb browser access by
the Chef Development workstation
Wednesday, April 30, 14
![Page 124: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/124.jpg)
OPEN IN EDITOR: cookbooks/apache/.kitchen.yml
---driver: name: docker
provisioner: name: chef_solo
platforms: - name: centos-6.4 driver_config: forward: - 80:80
suites: - name: default run_list: - recipe[apache::default] attributes: motd: {company: Chef}
Network config - clowns
124
Wednesday, April 30, 14
![Page 125: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/125.jpg)
$ bundle exec kitchen destroy default-centos-64
Network reconfig requires destroy
-----> Starting Kitchen (v1.2.1)-----> Destroying <default-centos-64>... 5b88b1806d613be02bfcd14f359c8d9bb1d77f06bbce7e423c0e3100fee73177 5b88b1806d613be02bfcd14f359c8d9bb1d77f06bbce7e423c0e3100fee73177 Finished destroying <default-centos-64> (0m0.47s).-----> Kitchen is finished. (0m0.53s)
125
Wednesday, April 30, 14
![Page 126: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/126.jpg)
$ bundle exec kitchen converge default-centos-64
Perform Chef run
-----> Starting Kitchen (v1.2.1)-----> Creating <default-centos-64>... Step 0 : FROM centos:6.4 ---> 539c0211cd76 Step 1 : RUN yum clean all ---> Using cache ---> 735ba0db794e Step 2 : RUN yum install -y sudo openssh-server openssh-clients curl ---> Using cache ---> 37c081ac2580....
126
Wednesday, April 30, 14
![Page 127: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/127.jpg)
Port now forwarded
127
Wednesday, April 30, 14
![Page 128: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/128.jpg)
$ curl localhost:80
Does it blend?
<html> <body> <h1>Welcome to Chef</h1> <h2>We love clowns</h2> 172.17.0.2:80 </body></html>
128
Wednesday, April 30, 14
![Page 129: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/129.jpg)
$ curl localhost:80
Does it blend?
<html> <body> <h1>Welcome to Chef</h1> <h2>We love clowns</h2> 172.17.0.2:80 </body></html>
129
WINWednesday, April 30, 14
![Page 130: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/130.jpg)
Clowns in your web browser
130
•You can access the clowns web site using the external address for your CloudShare workstation
Wednesday, April 30, 14
![Page 131: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/131.jpg)
CloudShare Node
131
Wednesday, April 30, 14
![Page 132: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/132.jpg)
CloudShare Node
132
Wednesday, April 30, 14
![Page 133: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/133.jpg)
http://<EXTERNAL_ADDRESS>
133
Wednesday, April 30, 14
![Page 134: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/134.jpg)
OPEN IN EDITOR: cookbooks/apache/.kitchen.yml---driver: name: docker
provisioner: name: chef_solo
platforms: - name: centos-6.4 driver_config: forward: - 80:80 - 81:81
suites: - name: default run_list: - recipe[apache::default] attributes: motd: {company: Chef}
Network config - bears
134
Wednesday, April 30, 14
![Page 135: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/135.jpg)
$ bundle exec kitchen destroy default-centos-64
Network reconfig requires destroy
-----> Starting Kitchen (v1.2.1)-----> Destroying <default-centos-64>... 5b88b1806d613be02bfcd14f359c8d9bb1d77f06bbce7e423c0e3100fee73177 5b88b1806d613be02bfcd14f359c8d9bb1d77f06bbce7e423c0e3100fee73177 Finished destroying <default-centos-64> (0m0.47s).-----> Kitchen is finished. (0m0.53s)
135
Wednesday, April 30, 14
![Page 136: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/136.jpg)
$ bundle exec kitchen converge default-centos-64
Perform Chef run
-----> Starting Kitchen (v1.2.1)-----> Creating <default-centos-64>... Step 0 : FROM centos:6.4 ---> 539c0211cd76 Step 1 : RUN yum clean all ---> Using cache ---> 735ba0db794e Step 2 : RUN yum install -y sudo openssh-server openssh-clients curl ---> Using cache ---> 37c081ac2580....[2014-04-07T03:06:36-04:00] INFO: Report handlers completeChef Client finished, 11/11 resources updated in 24.32934282 seconds Finished converging <default-centos-64> (0m38.09s).-----> Kitchen is finished. (0m38.33s)
136
Wednesday, April 30, 14
![Page 137: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/137.jpg)
Both ports forwarded
137
Wednesday, April 30, 14
![Page 138: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/138.jpg)
$ curl localhost:81
Does it blend?
<html> <body> <h1>Welcome to Chef</h1> <h2>We love bears</h2> 172.17.0.2:81 </body></html>
138
Wednesday, April 30, 14
![Page 139: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/139.jpg)
Very nice!
139
Wednesday, April 30, 14
![Page 140: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/140.jpg)
140
Wednesday, April 30, 14
![Page 141: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/141.jpg)
141
Wednesday, April 30, 14
![Page 142: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/142.jpg)
Serverspec author• Written by Gosuke Miyashita
142
Wednesday, April 30, 14
![Page 143: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/143.jpg)
OPEN IN EDITOR: cookbooks/apache/Gemfile
source 'https://rubygems.org'
gem 'test-kitchen'gem 'kitchen-vagrant'
gem 'serverspec', '~> 1.1'
Add serverspec to Gemfile
143
Wednesday, April 30, 14
![Page 144: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/144.jpg)
OPEN IN EDITOR: cookbooks/apache/Gemfile
source 'https://rubygems.org'
gem 'test-kitchen'gem 'kitchen-vagrant'
gem 'serverspec', '~> 1.1'
Add serverspec to Gemfile
144
Pessimistic VersionConstraint
Wednesday, April 30, 14
![Page 145: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/145.jpg)
Version Constraint
145
• If a gem properly follows semantic versioning with its versioning scheme. You can take advantage of this to choose a version constraint to lock down the gem in your application.
http://guides.rubygems.org/patterns/#declaring_dependencies
Wednesday, April 30, 14
![Page 146: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/146.jpg)
Semantic Versioning
146
Given a version number MAJOR.MINOR.PATCH, increment the:1.MAJOR version when you make incompatible API
changes,2.MINOR version when you add functionality in a backwards-
compatible manner, and3.PATCH version when you make backwards-compatible bug
fixes.Additional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR.PATCH format.
http://guides.rubygems.org/patterns/#semantic_versioning
Wednesday, April 30, 14
![Page 147: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/147.jpg)
Versioning Example
147
Let’s say the following releases of a gem exist:■Version 2.1.0 — Baseline■Version 2.2.0 — Introduced some new (backward
compatible) features.■Version 2.2.1 — Removed some bugs■Version 2.2.2 — Streamlined your code■Version 2.3.0 — More new features (but still backwards
compatible).■Version 3.0.0 — Reworked the interface. Code written to
version 2.x might not work.http://guides.rubygems.org/patterns/#semantic_versioning
Wednesday, April 30, 14
![Page 148: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/148.jpg)
Version Constraint
148
gem 'library', '= 2.2.0'
Only use version 2.2.0
Wednesday, April 30, 14
![Page 149: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/149.jpg)
Optimistic Version Constraint
149
gem 'library', '>= 2.2.0'
Assume all changes from 2.x on will work, including 3.0.0 and higher
Wednesday, April 30, 14
![Page 150: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/150.jpg)
Pessimistic Version Constraint
150
gem 'library', '>= 2.2.0', ‘< 3.0’
Explicitly exclude any versions that might break your code
Wednesday, April 30, 14
![Page 151: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/151.jpg)
Pessimistic Version Constraint
151
gem 'library', '>= 2.2.0', ‘< 3.0’
Shorthand for:
Using the twiddle-wakka:gem 'library', '~> 2.2'
Wednesday, April 30, 14
![Page 152: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/152.jpg)
$ bundle install
Install Serverspec
152
$ CONFIGURE_ARGS="--with-ldflags=\'-Wno-error=unused-command-line-argument-hard-error-in-future'" bundle install
clang 5.1 Workaround
Wednesday, April 30, 14
![Page 153: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/153.jpg)
v2.0.0
RSpec Syntax
153
Wednesday, April 30, 14
![Page 154: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/154.jpg)
Documentation form
154
describe ‘<entity>’ do <descriptions here>end
Wednesday, April 30, 14
![Page 155: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/155.jpg)
Documentation example
155
describe ‘clowns site’ do ...end
Wednesday, April 30, 14
![Page 156: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/156.jpg)
Description form
156
describe ‘<entity>’ do it ‘<description>’ do ... endend
Wednesday, April 30, 14
![Page 157: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/157.jpg)
Description example
157
describe ‘clowns site’ do it ‘responds on port 80’ do ... endend
Wednesday, April 30, 14
![Page 158: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/158.jpg)
Expectation form
158
describe ‘<entity>’ do it ‘<description>’ do expect(thing).to eq result endend
Wednesday, April 30, 14
![Page 159: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/159.jpg)
Expectation form
159
describe ‘<entity>’ do it ‘<description>’ do expect(thing).to eq result endend
Matcher
Wednesday, April 30, 14
![Page 160: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/160.jpg)
Expectation example
160
describe ‘clowns site’ do it ‘responds on port 80’ do expect(port 80).to be_listening ‘tcp’ endend
Wednesday, April 30, 14
![Page 161: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/161.jpg)
Expectation form
161
describe ‘<entity>’ do it ‘<description>’ do expect(thing).to eq result endend
This is new syntax
Wednesday, April 30, 14
![Page 162: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/162.jpg)
Should vs Expect
162
describe ‘clowns site’ do it ‘responds on port 80’ do expect(port 80).to be_listening ‘tcp’ endend
Expect Form
One-Liner Should Formdescribe ‘clowns site’ do describe port(80) do it { should be_listening.with(‘tcp’) } endend
Wednesday, April 30, 14
![Page 163: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/163.jpg)
Expect vs. Should
163
Debate on whether or not to use expect vs. should is epic:http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax...and pointless. Use whatever makes the most sense to you. There are some technical limitations to the ‘should’ form, but if you stick to the “one-liner should” syntax, they are essentially interchangeable.
Wednesday, April 30, 14
![Page 164: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/164.jpg)
We Use Expect Form
164
Because all the ChefSpec examples are in the expect form
Wednesday, April 30, 14
![Page 165: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/165.jpg)
165
Let’s write some serverspectests!
Wednesday, April 30, 14
![Page 166: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/166.jpg)
166
Default location for tests• By default, Test Kitchen will look in the test/integration directory for test-related files
• For convenience, Test Kitchen creates this directory when you run kitchen init
Wednesday, April 30, 14
![Page 167: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/167.jpg)
chef-fundamentals-repo
167
.!"" attributes/!"" .bundle/!"" CHANGELOG.md!"" files/!"" Gemfile!"" Gemfile.lock!"" .kitchen/!"" .kitchen.yml!"" metadata.rb!"" README.md!"" recipes/!"" templates/!"" test/$ #"" integration/#"" vendor/
Wednesday, April 30, 14
![Page 168: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/168.jpg)
168
Suite subdirectory• Test Kitchen requires a few more directories underneath
test/integration• First directory name underneath test/integration should
match the suite name:
└── test/ └── integration/ └── <suite_name>/
Wednesday, April 30, 14
![Page 169: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/169.jpg)
OPEN IN EDITOR: cookbooks/apache/.kitchen.yml---driver: name: docker
provisioner: name: chef_solo
platforms: - name: centos-6.4 driver_config: forward: - 80:80 - 81:81
suites: - name: default run_list: - recipe[apache::default] attributes: motd: {company: Chef}
Network configuration
169
Suite name
Wednesday, April 30, 14
![Page 170: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/170.jpg)
170
Suite subdirectory• Our suite name is default
└── test/ └── integration/ └── default/
Wednesday, April 30, 14
![Page 171: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/171.jpg)
171
Busser directory• The next directory level denotes the test plugin, as
Test Kitchen many different kinds of test plugins. A testplugin is called a busser. We’ll be using the busserdirectory called serverspec.
└── test/ └── integration/ └── default/ └── serverspec/
Wednesday, April 30, 14
![Page 172: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/172.jpg)
172
Hostname directory• Serverspec supports testing via SSH, so it requires yet
another directory level to denote the hostname. Wewon’t be using this capability, so it should be localhost
└── test/ └── integration/ └── default/ └── serverspec/ └── localhost/
Wednesday, April 30, 14
![Page 173: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/173.jpg)
173
Hostname directory
• NOTE: Use of the localhost directory is optional
Wednesday, April 30, 14
![Page 174: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/174.jpg)
$ cd $HOME/chef-fundamentals-repos/cookbooks/apache
Make sure you are in the apache cookbook root
174
> cd %USERPROFILE%/chef-fundamentals-repos/cookbooks/apache
Wednesday, April 30, 14
![Page 175: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/175.jpg)
$ mkdir -p test/integration/default/serverspec
Create test directory structure
175
> mkdir test\integration\default\serverspec
Wednesday, April 30, 14
![Page 176: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/176.jpg)
176
*_spec.rb files• By convention, Test Kitchen expects files with tests
to end in _spec.rb
Wednesday, April 30, 14
![Page 177: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/177.jpg)
Serverspec expectation form• Every specialized RSpec-based testing library like
serverspec has their own special twist on the basic RSpec expectation form
177
Wednesday, April 30, 14
![Page 178: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/178.jpg)
Generic Expectation Form
178
describe ‘<entity>’ do it ‘<description>’ do expect(thing).to eq result endend
Wednesday, April 30, 14
![Page 179: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/179.jpg)
Serverspec Command
179
describe ‘<entity>’ do it ‘<description>’ do expect(command).to eq result endend
thing to expectis called a commandin serverspec
Wednesday, April 30, 14
![Page 180: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/180.jpg)
Serverspec commands & matchers• Serverspec has provides a wide variety of matchers
for each command• Serverspec commands are well-documented: http://
serverspec.org/resource_types.html
180
Wednesday, April 30, 14
![Page 181: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/181.jpg)
Serverspec resources
181
Wednesday, April 30, 14
![Page 182: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/182.jpg)
Port resource/be_listening matcher
182
Wednesday, April 30, 14
![Page 183: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/183.jpg)
183
Writing your first test• Let’s create a serverspec test checking to make sure
the clowns web site is active on port 80• Let’s use the port resource and the be_listening
matcher
Wednesday, April 30, 14
![Page 184: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/184.jpg)
Spec for clowns
184
require 'serverspec'
include Serverspec::Helper::Exec
describe 'clowns site' do it 'responds on port 80' do expect(port 80).to be_listening 'tcp' endend
OPEN IN EDITOR:apache/test/integration/default/serverspec/clown_spec.rb
Wednesday, April 30, 14
![Page 185: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/185.jpg)
185
kitchen setup• Before running tests you need to run kitchen setup
• kitchen setup loads and configures the file necessary to run test plugins on the node
• The component that manages Test Kitchen plugins is called Busser
Wednesday, April 30, 14
![Page 186: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/186.jpg)
$ bundle exec kitchen setup default-centos-64
kitchen setup
-----> Starting Kitchen (v1.2.1)-----> Setting up <default-centos-64>...Fetching: thor-0.19.0.gem (100%)Fetching: busser-0.6.2.gem (100%)Successfully installed thor-0.19.0Successfully installed busser-0.6.22 gems installed-----> Setting up Busser Creating BUSSER_ROOT in /tmp/busser Creating busser binstub Plugin serverspec installed (version 0.2.6)-----> Running postinstall for serverspec plugin Finished setting up <default-centos-64> (0m34.28s).-----> Kitchen is finished. (0m34.37s)
186
Wednesday, April 30, 14
![Page 187: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/187.jpg)
187
kitchen verify• The kitchen verify command will run the tests in
your *_spec.rb files in the test/integration tree
Wednesday, April 30, 14
![Page 188: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/188.jpg)
$ bundle exec kitchen verify default-centos-64
kitchen verify
-----> Starting Kitchen (v1.2.1)-----> Verifying <default-centos-64>... Removing /tmp/busser/suites/serverspecUploading /tmp/busser/suites/serverspec/localhost/clown_spec.rb (mode=0664)-----> Running serverspec test suite/opt/chef/embedded/bin/ruby -I/tmp/busser/suites/serverspec -S /opt/chef/embedded/bin/rspec /tmp/busser/suites/serverspec/localhost/clown_spec.rb --color --format documentation
clowns site responds on port 80
Finished in 0.00713 seconds1 example, 0 failures Finished verifying <default-centos-64> (0m0.90s).-----> Kitchen is finished. (0m0.97s)
188
Wednesday, April 30, 14
![Page 189: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/189.jpg)
189
Verifying that the tests work• Did the test actually do anything? Let’s verify this
by changing the port to a known incorrect value.
Wednesday, April 30, 14
![Page 190: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/190.jpg)
Replace port 80 with 85
190
require 'serverspec'
include Serverspec::Helper::Exec
describe 'clowns site' do it 'responds on port 85' do expect(port 85).to be_listening 'tcp' endend
OPEN IN EDITOR:apache/test/integration/default/serverspec/clown_spec.rb
Wednesday, April 30, 14
![Page 191: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/191.jpg)
$ bundle exec kitchen verify default-centos-64
This should fail
-----> Running serverspec test suite/opt/chef/embedded/bin/ruby -I/tmp/busser/suites/serverspec -S /opt/chef/embedded/bin/rspec /tmp/busser/suites/serverspec/localhost/clown_spec.rb --color --format documentation
clowns site responds on port 85 (FAILED - 1)
Failures:
1) clowns site responds on port 85 Failure/Error: expect(port 85).to be_listening 'tcp' netstat -tunl | grep -- :85\ expected Port "85" to be listening "tcp" # /tmp/busser/suites/serverspec/localhost/clown_spec.rb:7:in `block (2 levels) in <top (required)>'
Finished in 0.00718 seconds1 example, 1 failure...
191
Wednesday, April 30, 14
![Page 192: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/192.jpg)
$ bundle exec kitchen verify default-centos-64
This should fail
-----> Running serverspec test suite/opt/chef/embedded/bin/ruby -I/tmp/busser/suites/serverspec -S /opt/chef/embedded/bin/rspec /tmp/busser/suites/serverspec/localhost/clown_spec.rb --color --format documentation
clowns site responds on port 85 (FAILED - 1)
Failures:
1) clowns site responds on port 85 Failure/Error: expect(port 85).to be_listening 'tcp' netstat -tunl | grep -- :85\ expected Port "85" to be listening "tcp" # /tmp/busser/suites/serverspec/localhost/clown_spec.rb:7:in `block (2 levels) in <top (required)>'
Finished in 0.00718 seconds1 example, 1 failure...
192
WIN FAILWednesday, April 30, 14
![Page 193: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/193.jpg)
193
Back to success• Remember to reset the tests back to the original port
value so they succeed again!
Wednesday, April 30, 14
![Page 194: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/194.jpg)
Reset back to port 80
194
require 'serverspec'
include Serverspec::Helper::Exec
describe 'clowns site' do it 'responds on port 80' do expect(port 80).to be_listening 'tcp' endend
OPEN IN EDITOR:apache/test/integration/default/serverspec/clown_spec.rb
Wednesday, April 30, 14
![Page 195: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/195.jpg)
$ bundle exec kitchen verify default-centos-64
kitchen verify
-----> Starting Kitchen (v1.2.1)-----> Verifying <default-centos-64>... Removing /tmp/busser/suites/serverspecUploading /tmp/busser/suites/serverspec/localhost/clown_spec.rb (mode=0664)-----> Running serverspec test suite/opt/chef/embedded/bin/ruby -I/tmp/busser/suites/serverspec -S /opt/chef/embedded/bin/rspec /tmp/busser/suites/serverspec/localhost/clown_spec.rb --color --format documentation
clowns site responds on port 80
Finished in 0.00713 seconds1 example, 0 failures Finished verifying <default-centos-64> (0m0.90s).-----> Kitchen is finished. (0m0.97s)
195
Wednesday, April 30, 14
![Page 196: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/196.jpg)
196
Testing bears• Let’s add another test to do a similar check for the
bears port
Wednesday, April 30, 14
![Page 197: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/197.jpg)
Spec for bears
197
require 'serverspec'
include Serverspec::Helper::Exec
describe 'bears site' do it 'responds on port 81' do expect(port 81).to be_listening 'tcp' endend
OPEN IN EDITOR:apache/test/integration/default/serverspec/bear_spec.rb
Wednesday, April 30, 14
![Page 198: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/198.jpg)
198
No need to run kitchen setup• You only need to run kitchen setup once per
node. (Though it doesn’t hurt to run it more than once).
Wednesday, April 30, 14
![Page 199: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/199.jpg)
$ bundle exec kitchen verify default-centos-64
Verifying clowns & bears
...----> Running serverspec test suite /opt/chef/embedded/bin/ruby -I/tmp/busser/suites/serverspec -S /opt/chef/embedded/bin/rspec /tmp/busser/suites/serverspec/localhost/bear_spec.rb /tmp/busser/suites/serverspec/localhost/clown_spec.rb --color --format documentation
bears site responds on port 81
clowns site responds on port 80
Finished in 0.00889 seconds 2 examples, 0 failures...
199
Wednesday, April 30, 14
![Page 200: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/200.jpg)
200
Code cleanup• Common code can be moved to a file called spec_helper.rb in test/integration/default/serverspec
• Test Kitchen automatically looks for spec_helper.rb in test/integration/default/serverspec
Wednesday, April 30, 14
![Page 201: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/201.jpg)
201
Code cleanup• Let’s move common code between clowns & bears
to spec_helper.rb
Wednesday, April 30, 14
![Page 202: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/202.jpg)
spec_helper.rb
202
require 'serverspec'
include Serverspec::Helper::Exec
OPEN IN EDITOR:apache/test/integration/default/serverspec/spec_helper.rb
Wednesday, April 30, 14
![Page 203: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/203.jpg)
require spec_helper clowns
203
require 'spec_helper'
describe 'clowns site' do it 'responds on port 80' do expect(port 80).to be_listening 'tcp' endend
OPEN IN EDITOR:apache/test/integration/default/serverspec/clown_spec.rb
Wednesday, April 30, 14
![Page 204: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/204.jpg)
require spec_helper bears
204
require 'spec_helper'
describe 'bears site' do it 'responds on port 81' do expect(port 81).to be_listening 'tcp' endend
OPEN IN EDITOR:apache/test/integration/default/serverspec/bear_spec.rb
Wednesday, April 30, 14
![Page 205: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/205.jpg)
$ bundle exec kitchen verify default-centos-64
Testing clowns and bears w/spec_helper.rb
...----> Running serverspec test suite /opt/chef/embedded/bin/ruby -I/tmp/busser/suites/serverspec -S /opt/chef/embedded/bin/rspec /tmp/busser/suites/serverspec/localhost/bear_spec.rb /tmp/busser/suites/serverspec/localhost/clown_spec.rb --color --format documentation
bears site response on port 81
clowns site responds on port 80
Finished in 0.00889 seconds 2 examples, 0 failures...
205
Wednesday, April 30, 14
![Page 206: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/206.jpg)
$ bundle exec kitchen verify default-centos-64
Testing clowns and bears w/spec_helper.rb
...----> Running serverspec test suite /opt/chef/embedded/bin/ruby -I/tmp/busser/suites/serverspec -S /opt/chef/embedded/bin/rspec /tmp/busser/suites/serverspec/localhost/bear_spec.rb /tmp/busser/suites/serverspec/localhost/clown_spec.rb --color --format documentation
bears site response on port 81
clowns site responds on port 80
Finished in 0.00889 seconds 2 examples, 0 failures...
206
WINWednesday, April 30, 14
![Page 207: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/207.jpg)
207
Are the web sites really working?• While we’ve added checks to verify that the test
node is listening on ports 80 and 81, we haven’t verified that users see the right content when they visit these sites.
• Let’s use the command resource with the return_stdout matcher to do a simple check with curl to verify that port 80 is clowns and port 81 is bears.
Wednesday, April 30, 14
![Page 208: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/208.jpg)
command resource/return_stdout matcher
208
Wednesday, April 30, 14
![Page 209: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/209.jpg)
Check clown content
209
require 'spec_helper'
describe 'clowns site' do it 'responds on port 80' do expect(port 80).to be_listening 'tcp' end
it 'returns clowns in the HTML body' do expect(command 'curl localhost:80').to \ return_stdout(/clowns/) endend
OPEN IN EDITOR:apache/test/integration/default/serverspec/clown_spec.rb
Wednesday, April 30, 14
![Page 210: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/210.jpg)
210
Wednesday, April 30, 14
![Page 211: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/211.jpg)
Check bear content
211
require 'spec_helper'
describe 'bears site' do it 'responds on port 81' do expect(port 81).to be_listening 'tcp' end
it 'returns bears in the HTML body' do expect(command 'curl localhost:81').to \ return_stdout(/bears/) endend
OPEN IN EDITOR:apache/test/integration/default/serverspec/bear_spec.rb
Wednesday, April 30, 14
![Page 212: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/212.jpg)
$ bundle exec kitchen verify default-centos-64
Testing for content
...-----> Running serverspec test suite/opt/chef/embedded/bin/ruby -I/tmp/busser/suites/serverspec -S /opt/chef/embedded/bin/rspec /tmp/busser/suites/serverspec/localhost/bear_spec.rb /tmp/busser/suites/serverspec/localhost/clown_spec.rb --color --format documentation
bears site responds on port 81 returns bears in the HTML body
clowns site responds on port 80 returns clowns in the HTML body
Finished in 0.0293 seconds4 examples, 0 failures Finished verifying <default-centos-64> (0m1.73s).-----> Kitchen is finished. (0m1.79s)...
212
WINWednesday, April 30, 14
![Page 213: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/213.jpg)
213
Detecting the target OS• Many of the resources require that Serverspec
detect the OS so it can run the correct command for your platform expect(package 'httpd').to be_installed
• You’ll need to add an extra Helper to spec_helper.rb
Wednesday, April 30, 14
![Page 214: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/214.jpg)
spec_helper.rb
214
require 'serverspec'
include Serverspec::Helper::Execinclude Serverspec::Helper::DetectOS
OPEN IN EDITOR:apache/test/integration/default/serverspec/spec_helper.rb
Wednesday, April 30, 14
![Page 215: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/215.jpg)
Check httpd package
215
require 'spec_helper'
describe 'server' do it 'has apache installed' do expect(package 'httpd').to be_installed endend
OPEN IN EDITOR:apache/test/integration/default/serverspec/default_spec.rb
Wednesday, April 30, 14
![Page 216: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/216.jpg)
$ bundle exec kitchen verify default-centos-64
Testing for httpd
...-----> Running serverspec test suite/opt/chef/embedded/bin/ruby -I/tmp/busser/suites/serverspec -S /opt/chef/embedded/bin/rspec /tmp/busser/suites/serverspec/localhost/bear_spec.rb /tmp/busser/suites/serverspec/localhost/clown_spec.rb --color --format documentation
bears site responds on port 81 returns bears in the HTML body
clowns site responds on port 80 returns clowns in the HTML body
Finished in 0.0293 seconds4 examples, 0 failures Finished verifying <default-centos-64> (0m1.73s).-----> Kitchen is finished. (0m1.79s)...
216
WINWednesday, April 30, 14
![Page 217: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/217.jpg)
217
kitchen test• The kitchen test command will automate all the
previous actions you’ve learned so far into one command. It runs the following commands in sequence:• kitchen destroy (if necessary)•kitchen create•kitchen converge•kitchen setup•kitchen verify•kitchen destroy
Wednesday, April 30, 14
![Page 218: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/218.jpg)
218
kitchen test• The kitchen test command is intended to be used
as a final check on a fresh image before committing changes to source control and/or to be used in a Continuous Integration environment like Jenkins.
Wednesday, April 30, 14
![Page 219: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/219.jpg)
$ bundle exec kitchen test default-centos-64
kitchen test
-----> Starting Kitchen (v1.2.1)-----> Cleaning up any prior instances of <default-centos-64>-----> Destroying <default-centos-64>... 2c46b1a4609dc6a2beaf44e1134638b0a8ac47c9c5a02baee0bdb3df64e7bcdf 2c46b1a4609dc6a2beaf44e1134638b0a8ac47c9c5a02baee0bdb3df64e7bcdf Finished destroying <default-centos-64> (0m0.60s).-----> Testing <default-centos-64>-----> Creating <default-centos-64>......Finished in 0.0311 seconds4 examples, 0 failures Finished verifying <default-centos-64> (0m1.71s).-----> Destroying <default-centos-64>... d22a8c4db8505f89f7f7e65bca26492f58d5637f9a88763d5eb919d860dade4e d22a8c4db8505f89f7f7e65bca26492f58d5637f9a88763d5eb919d860dade4e Finished destroying <default-centos-64> (0m0.47s). Finished testing <default-centos-64> (0m39.78s).-----> Kitchen is finished. (0m39.84s)
219
Wednesday, April 30, 14
![Page 220: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/220.jpg)
Where to go next• Jenkins cookbook is chock full of advanced
Serverspec techniques:
https://github.com/opscode-cookbooks/jenkins
220
Wednesday, April 30, 14
![Page 221: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/221.jpg)
Where to go next• jenkins/test/shared/support contains
examples for implementing custom Serverspec matchers used in jenkins/test/integration:•describe jenkins_job('my-‐project') do it { should be_a_jenkins_job }end
221
Wednesday, April 30, 14
![Page 222: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/222.jpg)
Where to go next• test/fixtures contains mini-cookbooks to
exercise resource providers• Aliased in Berksfile:•cookbook 'smoke', path: 'test/fixtures/cookbooks/smoke'
• Run via Rakefile
222
Wednesday, April 30, 14
![Page 223: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/223.jpg)
Where to go next• Uses data/path directive in .kitchen.yml to share
test data between serverspec suites• Directory specified in data/path is copied to /tmp/kitchen/data on guest
• Reason for weird require_relative directive in tests that use custom Serverspec matchers:require_relative '../../../kitchen/data/spec_helper'
223
Wednesday, April 30, 14
![Page 224: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/224.jpg)
RECAP: Why Test?• It’s important to find bugs fast
224
Wednesday, April 30, 14
![Page 225: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/225.jpg)
Better, Faster, Stronger• Test Kitchen is an invaluable tool for managing
sandbox environments and truly verifying that a cookbook produces the intended results
• But it does require spinning up an instance and performing a full Chef converge, which can take a long time
• Use Test Kitchen judiciously. The other tools can provide more limited forms of feedback faster.
225
Wednesday, April 30, 14
![Page 226: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/226.jpg)
v2.0.0
Detect Suspicious Cookbook Code with Foodcritic
226
Wednesday, April 30, 14
![Page 227: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/227.jpg)
Feedback on Chef Coding Style• Foodcritic provides feedback on your Chef coding
style• It is designed to be used as you are writing Chef
code - how’s that for freaking fast!• Written by Andrew Crump
http://acrmp.github.com/footcritic
227
Wednesday, April 30, 14
![Page 228: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/228.jpg)
Feedback on Chef Coding Style• Let’s install Foodcritic on your development
workstation so you can give it a spin• Add Foodcritic to your Gemfile• Install the app with bundle install
228
Wednesday, April 30, 14
![Page 229: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/229.jpg)
OPEN IN EDITOR: cookbooks/apache/Gemfile
source 'https://rubygems.org'
gem 'test-kitchen'gem 'kitchen-vagrant'
gem 'serverspec', '~> 1.1'gem 'foodcritic', '~> 3.0'
Add foodcritic to Gemfile
229
Wednesday, April 30, 14
![Page 230: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/230.jpg)
$ sudo yum install -y libxslt-devel libxml2-devel
Install Prerequisites
230
$ sudo apt-get install -y libxslt-dev libxml2-dev
Wednesday, April 30, 14
![Page 231: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/231.jpg)
$ bundle install
Install Foodcritic
231
$ CONFIGURE_ARGS="--with-ldflags=\'-Wno-error=unused-command-line-argument-hard-error-in-future'" bundle install
clang 5.1 Workaround
Wednesday, April 30, 14
![Page 232: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/232.jpg)
$ bundle exec foodcritic --version
Verify install
foodcritic 3.0.3
232
Wednesday, April 30, 14
![Page 233: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/233.jpg)
$ bundle exec foodcritic .
Run Foodcritic on your cookbook
233
FC003: Check whether you are running with chef server before using server-specific features: cookbooks/apache/recipes/ip-logger.rb:1FC008: Generated cookbook metadata needs updating: cookbooks/apache/metadata.rb:2FC008: Generated cookbook metadata needs updating: cookbooks/apache/metadata.rb:3
Wednesday, April 30, 14
![Page 234: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/234.jpg)
234
More feedback!
Wednesday, April 30, 14
![Page 235: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/235.jpg)
235
Fast feedback!
Wednesday, April 30, 14
![Page 236: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/236.jpg)
Feedback on Chef Coding Style• Foodcritic comes with a set of checks called rules• Foodcritic rules are documented at http://
acrmp.github.io/foodcritic/• The default rules are a good start, and you can add
new rules of your own easily
236
Wednesday, April 30, 14
![Page 237: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/237.jpg)
Default Rules
237
Wednesday, April 30, 14
![Page 238: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/238.jpg)
238
What about theFC003 & FC008 issues
in the apache cookbook?
Wednesday, April 30, 14
![Page 239: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/239.jpg)
FC008 - Generated cookbook metadata needs updating
239
Wednesday, April 30, 14
![Page 240: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/240.jpg)
240
Let’s fix!
Wednesday, April 30, 14
![Page 241: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/241.jpg)
cookbooks/apache/metadata.rb
241
name 'apache'maintainer 'YOUR_COMPANY_NAME'maintainer_email 'YOUR_EMAIL'license 'All rights reserved'description 'Installs/Configures apache'long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))version '0.2.0'
Wednesday, April 30, 14
![Page 242: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/242.jpg)
OPEN IN EDITOR: cookbooks/apache/metadata.rb
name 'apache'maintainer 'Mischa Taylor'maintainer_email '[email protected]'license 'All rights reserved'description 'Installs/Configures apache'long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))version '0.2.0'
242
Addressing FC008
Wednesday, April 30, 14
![Page 243: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/243.jpg)
$ bundle exec foodcritic .
Rerun foodcritic
243
FC003: Check whether you are running with chef server before using server-specific features: ./recipes/ip-logger.rb:1
Wednesday, April 30, 14
![Page 244: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/244.jpg)
244
Two down,one to go!
Wednesday, April 30, 14
![Page 245: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/245.jpg)
FC003 - Check for chef server before using server-specific features
245
Wednesday, April 30, 14
![Page 246: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/246.jpg)
246
Ignoring FC003• Let’s say, for now, we don’t want to fix ip-‐logger.rb, and we’d like to squelch the FC003 check
Wednesday, April 30, 14
![Page 247: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/247.jpg)
247
--tags parameter• The -‐-‐tags <TAGS> parameter can be used to
specify a list of rules for foodcritic to use•foodcritic -‐-‐tags FC001,FC002,FC008
• The tilde (~) modifier can be used to ignore specific rules•foodcritic -‐-‐tags ~FC003
Wednesday, April 30, 14
![Page 248: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/248.jpg)
$ bundle exec foodcritic --tags ~FC003 .
Ignore FC003
248
Wednesday, April 30, 14
![Page 249: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/249.jpg)
$ bundle exec foodcritic --tags ~FC003 .
Ignore FC003
249
WINWednesday, April 30, 14
![Page 250: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/250.jpg)
250
Custom rules• Etsy created some custom Foodcritic rules to check
for issues that caused production outages/performance degradation.
• Good example for how to create your own custom rules
• Documented here:https://github.com/etsy/foodcritic-rules
Wednesday, April 30, 14
![Page 251: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/251.jpg)
Etsy Foodcritic Rules• ETSY001 - Package or yum_package resource used
with :upgrade action• ETSY002 - Execute resource used to run git commands• ETSY003 - Execute resource used to run curl or wget commands• ETSY004 - Execute resource defined without conditional or
action :nothing• ETSY005 - Action :restart sent to a core service• ETSY006 - Execute resource used to run chef-provided command• ETSY007 - Package or yum_package resource used to install
core package without specific version number
251
Wednesday, April 30, 14
![Page 252: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/252.jpg)
$ git clone https://github.com/etsy/foodcritic-rules ../../foodcritic/etsy$ rm -rf ../../foodcritic/etsy/.git
Installing new rules
252
Wednesday, April 30, 14
![Page 253: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/253.jpg)
253
--include parameter• The -‐-‐include <PATH> parameter species
additional paths to load rules (shortened with -I)
Wednesday, April 30, 14
![Page 254: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/254.jpg)
$ bundle exec foodcritic -t ~FC003 -I ../../foodcritic .
Including Custom Rules
ETSY005: Action :restart sent to a core service: ./recipes/default.rb:19ETSY005: Action :restart sent to a core service: ./recipes/default.rb:32ETSY007: Package or yum_package resource used to install core package without specific version number: ./recipes/default.rb:10
254
Wednesday, April 30, 14
![Page 255: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/255.jpg)
255
Editor support• Many popular editors can be configured to run
Foodcritic inside the editor (including Vim, GNU Emacs and Sublime Text). So you can get feedback even faster.
Wednesday, April 30, 14
![Page 256: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/256.jpg)
v2.0.0
Detect Suspicious Ruby Code with RuboCop
256
Wednesday, April 30, 14
![Page 257: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/257.jpg)
RuboCop - Feedback on Ruby Style• Many people new to Ruby would like some guidance
on how to write idiomatic Ruby• Get the same kind of feedback for Ruby using
RuboCop that you get for Chef Code using Foodcritic (Chef code is Ruby)
257
Wednesday, April 30, 14
![Page 258: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/258.jpg)
RuboCop Author• Written by Bozhidar Batsov:
https://github.com/bbatsov/rubocop
258
Wednesday, April 30, 14
![Page 259: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/259.jpg)
RuboCop - Feedback on Ruby Style• Follows community-driven style guide:
https://github.com/bbatsov/ruby-style-guide
• Looks at cookbooks for Ruby best practices, not the Chef DSL - that’s Foodcritic
259
Wednesday, April 30, 14
![Page 260: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/260.jpg)
RuboCop - Feedback on Ruby Style• Let’s install RuboCop on your development
workstation so you can give it a spin• Add RuboCop to your Gemfile• Install the app with bundle install
260
Wednesday, April 30, 14
![Page 261: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/261.jpg)
OPEN IN EDITOR: cookbooks/apache/Gemfile
source 'https://rubygems.org'
gem 'test-kitchen'gem 'kitchen-vagrant'
gem 'serverspec', '~> 1.1'gem 'foodcritic', '~> 3.0'gem 'rubocop', '~> 0.20'
Add rubocop to Gemfile
261
Wednesday, April 30, 14
![Page 262: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/262.jpg)
$ bundle install
Install RuboCop
262
$ CONFIGURE_ARGS="--with-ldflags=\'-Wno-error=unused-command-line-argument-hard-error-in-future'" bundle install
clang 5.1 Workaround
Wednesday, April 30, 14
![Page 263: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/263.jpg)
$ bundle exec rubocop --version
Verify Install
0.20.1
263
Wednesday, April 30, 14
![Page 264: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/264.jpg)
Running RuboCop• Just run the rubocop command - it recursively
checks all the *.rb files in all subdirectories underneath the current directory (excluding vendor/)
264
Wednesday, April 30, 14
![Page 265: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/265.jpg)
$ bundle exec rubocop
Run RuboCop on your cookbook
attributes/default.rb:3:19: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.default["apache"]["sites"]["bears"] = { "port" => 81 } ^^^^^^^attributes/default.rb:3:28: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.default["apache"]["sites"]["bears"] = { "port" => 81 } ^^^^^^^attributes/default.rb:3:41: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.default["apache"]["sites"]["bears"] = { "port" => 81 } ^^^^^^
7 files inspected, 52 offenses detected
265
Wednesday, April 30, 14
![Page 266: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/266.jpg)
rubocop-todo.yml via --auto-gen-config• rubocop-todo.yml will help generate TODOs for each
item on the offense list• It also shows you what config setting can be used to
mask each offense, which we’ll need to do for some of these, because Chef code conventions vary slightly from the Rubocop community standards
266
Wednesday, April 30, 14
![Page 267: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/267.jpg)
$ bundle exec rubocop --auto-gen-config
Generate rubocop-todo.yml
attributes/default.rb:3:28: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.default["apache"]["sites"]["bears"] = { "port" => 81 } ^^^^^^^attributes/default.rb:3:41: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.default["apache"]["sites"]["bears"] = { "port" => 81 } ^^^^^^
7 files inspected, 52 offenses detectedCreated rubocop-todo.yml.Run `rubocop --config rubocop-todo.yml`, oradd inherit_from: rubocop-todo.yml in a .rubocop.yml file.
267
Wednesday, April 30, 14
![Page 268: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/268.jpg)
.rubocop.yml Configures RuboCop• .rubocop.yml can be used to configure RuboCop
(similar to .kitchen.yml in Test Kitchen)• We’ll add a settings to ignore things, similar to what
we did for Foodcritic, that don’t make as much sense for Chef.
• Settings are documented in the RuboCop README: https://github.com/bbatsov/rubocop/blob/master/README.md
• Cop is the RuboCop equivalent of a rule268
Wednesday, April 30, 14
![Page 269: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/269.jpg)
OPEN IN EDITOR: cookbooks/apache/.rubocop.yml
inherit_from: rubocop-‐todo.yml
Include rubocop-todo.yml
269
Wednesday, April 30, 14
![Page 270: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/270.jpg)
$ bundle exec rubocop
Run RuboCop on your cookbook
Inspecting 7 files.......
7 files inspected, no offenses detected
270
Wednesday, April 30, 14
![Page 271: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/271.jpg)
Easy peasy• Wow, now more offenses...not really
271
Wednesday, April 30, 14
![Page 272: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/272.jpg)
Match Chef community standards• First, we’ll move some of the Cops from rubocop-
todo.yml to .rubocop.yml for things that match Chef community standards (as opposed to the Ruby community standards)
272
Wednesday, April 30, 14
![Page 273: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/273.jpg)
Missing utf-8 encoding comment
273
Wednesday, April 30, 14
![Page 274: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/274.jpg)
OPEN IN EDITOR: cookbooks/apache/.rubocop.yml
inherit_from: rubocop-‐todo.yml
Encoding: Enabled: false
Chef does not (yet) support encoding comment
274
Wednesday, April 30, 14
![Page 275: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/275.jpg)
Line is too long
275
Wednesday, April 30, 14
![Page 276: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/276.jpg)
OPEN IN EDITOR: cookbooks/apache/.rubocop.yml
inherit_from: rubocop-‐todo.yml
Encoding: Enabled: false
LineLength: Max: 200
Relax line limit
276
Wednesday, April 30, 14
![Page 277: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/277.jpg)
Use the new Ruby 1.9 hash syntax
277
Wednesday, April 30, 14
![Page 278: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/278.jpg)
OPEN IN EDITOR: cookbooks/apache/.rubocop.yml
inherit_from: rubocop-‐todo.yml
Encoding: Enabled: false
LineLength: Max: 200
HashSyntax: EnforcedStyle: hash_rockets
Some cookbooks try to be Ruby 1.8 compatible
278
Wednesday, April 30, 14
![Page 279: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/279.jpg)
Prefer single-quoted strings
279
Wednesday, April 30, 14
![Page 280: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/280.jpg)
OPEN IN EDITOR: cookbooks/apache/.rubocop.yml
inherit_from: rubocop-‐todo.yml
Encoding: Enabled: false
LineLength: Max: 200
HashSyntax: EnforcedStyle: hash_rockets
StringLiterals: Enabled: false
Conflicts w/decision to relax FC002
280
Wednesday, April 30, 14
![Page 281: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/281.jpg)
$ bundle exec rubocop --auto-gen-config
Regenerate rubocop-todo.yml
metadata.rb:2:11: C: Put one space between the method name and the first argument.maintainer 'Mischa Taylor' ^^^^^^^metadata.rb:4:8: C: Put one space between the method name and the first argument.license 'All rights reserved' ^^^^^^^^^^metadata.rb:5:12: C: Put one space between the method name and the first argument.description 'Installs/Configures apache' ^^^^^^metadata.rb:7:8: C: Put one space between the method name and the first argument.version '0.2.0' ^^^^^^^^^^
7 files inspected, 11 offenses detected
281
Wednesday, April 30, 14
![Page 282: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/282.jpg)
Rubocop Workflow• Uncomment lines in rubocop-todo.yml• Fix offenses• git commit• Repeat
282
Wednesday, April 30, 14
![Page 283: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/283.jpg)
Only really critical issue
283
Wednesday, April 30, 14
![Page 284: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/284.jpg)
Trailing whitespace & Git• Whitespace differences make diffs longer and
diverts focus from more important changes• Even with Git, trailing whitespace can make merge
conflicts more difficult to resolve
284
Wednesday, April 30, 14
![Page 285: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/285.jpg)
285
Editor support• Many popular editors can be configured to run
RuboCop inside the editor (including Vim, GNU Emacs and Sublime Text). So you can get feedback even faster.
• RuboCop includes great docs on editor configuration (which work for Foodcritic as well): https://github.com/bbatsov/rubocop#editor-integration
Wednesday, April 30, 14
![Page 286: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/286.jpg)
v2.0.0
Runnable documentation with ChefSpec
286
Wednesday, April 30, 14
![Page 287: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/287.jpg)
RECAP: Why Not Begin With Testing?• Finding a bug in something that you can’t execute
is freaking hard!• While fixing bugs before writing code is cheap,
finding them is expensive
287
Wednesday, April 30, 14
![Page 288: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/288.jpg)
What is ChefSpec?• ChefSpec helps produce runnable documentation.
Its primary purpose is to help document and organize your code.• As a side effect, you’ll end up with a set of tests
which can also be used to uncover bugs when changes are made.
• Plus, your cookbook code will be improved when it is guided by tests.
288
Wednesday, April 30, 14
![Page 289: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/289.jpg)
ChefSpec Authors• Written by Andrew Crump and Seth Vargo
289
Wednesday, April 30, 14
![Page 290: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/290.jpg)
ChefSpec - Runnable Documentation• Let’s install ChefSpec on your development
workstation so you can give it a spin• Add ChefSpec to your Gemfile• Install the app with bundle install
290
Wednesday, April 30, 14
![Page 291: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/291.jpg)
OPEN IN EDITOR: chef-fundamentals-repo/Gemfile
source 'https://rubygems.org'
gem 'test-kitchen'gem 'kitchen-vagrant'
gem 'serverspec', '~> 1.1'gem 'foodcritic', '~> 3.0'gem 'rubocop', '~> 0.20'gem 'chefspec', '~> 3.4'
Gemfile
291
Wednesday, April 30, 14
![Page 292: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/292.jpg)
$ bundle install
Install ChefSpec
292
$ CONFIGURE_ARGS="--with-ldflags=\'-Wno-error=unused-command-line-argument-hard-error-in-future'" bundle install
clang 5.1 Workaround
Wednesday, April 30, 14
![Page 293: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/293.jpg)
ChefSpec builds on RSpec• ChefSpec uses the RSpec description form to create
runnable documentation (in a similar vein to serverspec)
293
Wednesday, April 30, 14
![Page 294: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/294.jpg)
Documentation form
294
describe ‘<recipe_name>’ do <perform in-‐memory Chef run> <descriptions here>end
Wednesday, April 30, 14
![Page 295: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/295.jpg)
Documentation example
295
describe ‘apache::default’ do ...end
Wednesday, April 30, 14
![Page 296: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/296.jpg)
In-Memory Chef Run Form
296
require ‘chefspec’
describe ‘<recipe_name>’ do chef_run = ChefSpec::Runner.new.converge(<recipe_name>) <descriptions here>end
Wednesday, April 30, 14
![Page 297: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/297.jpg)
In-Memory Chef Run Example
297
require ‘chefspec’
describe 'apache::default' do chef_run = ChefSpec::Runner.new.converge('apache::default')
<descriptions here>end
Wednesday, April 30, 14
![Page 298: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/298.jpg)
Expectation form
298
describe ‘<recipe_name>’ do <perform in-‐memory Chef run> it ‘<description>’ do expect(<chef_run>).to eq result endend
Wednesday, April 30, 14
![Page 299: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/299.jpg)
Expectation form
299
describe ‘<recipe_name>’ do <perform in-‐memory Chef run> it ‘<description>’ do expect(<chef_run>).to eq result endend
MatcherWednesday, April 30, 14
![Page 300: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/300.jpg)
Expectation Example
300
require ‘chefspec’
describe 'apache::default' do chef_run = ChefSpec::Runner.new.converge('apache::default')
it ‘installs apache2’ do expect(chef_run).to install_package(‘httpd’) endend
Wednesday, April 30, 14
![Page 301: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/301.jpg)
Runnable Documentation• expect statement does not actually perform the
httpd package installation• It just verifies the cookbook syntax that it instructs
Chef to install the package• Good enough for well-tested primitives like the package resource
301
Wednesday, April 30, 14
![Page 302: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/302.jpg)
302
Let’s write some ChefSpectests!
Wednesday, April 30, 14
![Page 303: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/303.jpg)
303
Default location for tests• By default, ChefSpec will look in the spec/directory
for ChefSpec test-related files
Wednesday, April 30, 14
![Page 304: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/304.jpg)
chef-fundamentals-repo
304
.!"" .bundle/!"" .kitchen/!"" .kitchen.yml!"" CHANGELOG.md!"" Gemfile!"" Gemfile.lock!"" README.md!"" attributes/!"" files/!"" metadata.rb!"" recipes/!"" spec/!"" templates/!"" test/$ #"" integration/#"" vendor/ #"" bundle/
Wednesday, April 30, 14
![Page 305: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/305.jpg)
$ mkdir spec
Create spec directory in cookbooks/apache
305
> mkdir spec
Wednesday, April 30, 14
![Page 306: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/306.jpg)
306
*_spec.rb files• By convention, ChefSpec expects files with tests to
end in _spec.rb
Wednesday, April 30, 14
![Page 307: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/307.jpg)
ChefSpec Matcher Documentation
307
http://rubydoc.info/github/acrmp/chefspec/frames
Wednesday, April 30, 14
![Page 308: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/308.jpg)
ChefSpec > API
308
Wednesday, April 30, 14
![Page 309: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/309.jpg)
Each API has an example
309
Wednesday, April 30, 14
![Page 310: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/310.jpg)
Let’s use the install_package matcher
310
Wednesday, April 30, 14
![Page 311: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/311.jpg)
install_package Example
311
Wednesday, April 30, 14
![Page 312: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/312.jpg)
OPEN IN EDITOR: spec/default_spec.rb
require 'chefspec'
describe 'apache::default' do chef_run = ChefSpec::Runner.new.converge('apache::default')
it 'installs apache2' do expect(chef_run).to install_package('httpd') endend
Test apache::default recipe
312
Wednesday, April 30, 14
![Page 313: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/313.jpg)
313
Rspec runs ChefSpec• There’s no separate chefspec command.• Just run rspec to run ChefSpec tests.
Wednesday, April 30, 14
![Page 314: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/314.jpg)
$ bundle exec rspec --color
Run ChefSpec on your cookbok
.
Finished in 0.0006 seconds1 example, 0 failures
314
Wednesday, April 30, 14
![Page 315: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/315.jpg)
OPEN IN EDITOR: spec/default_spec.rb
require 'chefspec'
describe 'apache::default' do chef_run = ChefSpec::Runner.new.converge('apache::default')
it 'installs apache2' do expect(chef_run).to install_package('badhttpd') endend
Did it really check anything?
315
Wednesday, April 30, 14
![Page 316: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/316.jpg)
$ bundle exec rspec
Run ChefSpec on your cookbok
F
Failures:
1) apache::default installs apache2 Failure/Error: expect(chef_run).to install_package('badhttpd') expected "package[badhttpd]" with action :install to be in Chef run. Other package resources:
package[httpd]
# ./spec/default_spec.rb:7:in `block (2 levels) in <top (required)>'
Finished in 0.00044 seconds1 example, 1 failure
316
Wednesday, April 30, 14
![Page 317: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/317.jpg)
OPEN IN EDITOR: spec/default_spec.rb
require 'chefspec'
describe 'apache::default' do chef_run = ChefSpec::Runner.new.converge('apache::default')
it 'installs apache2' do expect(chef_run).to install_package('httpd') endend
Restore back to working
317
Wednesday, April 30, 14
![Page 318: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/318.jpg)
$ bundle exec rspec --color
Run ChefSpec on your cookbok
.
Finished in 0.0006 seconds1 example, 0 failures
318
Wednesday, April 30, 14
![Page 319: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/319.jpg)
Lazy evaluation with let
319
require 'chefspec'
describe 'apache::default' do let(:chef_run) \ { ChefSpec::Runner.new.converge(described_recipe) }
it 'installs apache2' do expect(chef_run).to install_package('httpd') endend
Lazy evaluation
Wednesday, April 30, 14
![Page 320: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/320.jpg)
320
described_recipe• let blocks aren’t evaluated until the first time they
are called• Also allows ChefSpec to run the described_recipe
macro to evaluate the recipe name
Wednesday, April 30, 14
![Page 321: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/321.jpg)
OPEN IN EDITOR: spec/default_spec.rb
require 'chefspec'
describe 'apache::default' do let(:chef_run) \ { ChefSpec::Runner.new.converge(described_recipe) }
it 'installs apache2' do expect(chef_run).to install_package('httpd') endend
Lazy evaluation
321
Wednesday, April 30, 14
![Page 322: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/322.jpg)
$ bundle exec rspec --color
Run ChefSpec on your cookbok
.
Finished in 0.0006 seconds1 example, 0 failures
322
Wednesday, April 30, 14
![Page 323: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/323.jpg)
323
ChefSpec Resource Report• ChefSpec Resource Report can help guide writing
your tests
Wednesday, April 30, 14
![Page 324: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/324.jpg)
OPEN IN EDITOR: spec/default_spec.rb
require 'chefspec'
at_exit { ChefSpec::Coverage.report! }
describe 'apache::default' do let (:chef_run) \ { ChefSpec::Runner.new.converge(described_recipe) }
it 'installs apache2' do expect(chef_run).to install_package('httpd') endend
Adding resource report
324
Wednesday, April 30, 14
![Page 325: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/325.jpg)
$ bundle exec rspec --color
Run ChefSpec on your cookbok
.
Finished in 0.01106 seconds1 example, 0 failures
ChefSpec Coverage report generated...
Total Resources: 9 Touched Resources: 1 Touch Coverage: 11.11%
Untouched Resources:
service[httpd] /recipes/default.rb:14 execute[mv /etc/httpd/conf.d/welcome.conf /etc/httpd/conf.d/welcome.conf.disabled] /recipes/default.rb:19 template[/etc/httpd/conf.d/clowns.conf] /recipes/default.rb:32 directory[/srv/apache/clowns] /recipes/default.rb:43 template[/srv/apache/clowns/index.html] /recipes/default.rb:49 template[/etc/httpd/conf.d/bears.conf] /recipes/default.rb:32 directory[/srv/apache/bears] /recipes/default.rb:43 template[/srv/apache/bears/index.html] /recipes/default.rb:49
325
Wednesday, April 30, 14
![Page 326: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/326.jpg)
326
create_file matcher• Let’s verify that the clowns.conf file gets created
with the create_file matcher
Wednesday, April 30, 14
![Page 327: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/327.jpg)
327
create_file matcher
Wednesday, April 30, 14
![Page 328: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/328.jpg)
OPEN IN EDITOR: spec/default_spec.rb
require 'chefspec'
at_exit { ChefSpec::Coverage.report! }
describe 'apache::default' do let(:chef_run) \ { ChefSpec::Runner.new.converge(described_recipe) } ...
it 'creates clowns.conf' do expect(chef_run).to \ create_file('/etc/httpd/conf.d/clowns.conf') endend
Checking clowns.conf files
328
Wednesday, April 30, 14
![Page 329: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/329.jpg)
$ bundle exec rspec --color
Run ChefSpec on your cookbok
F
Failures:
1) apache::default creates clowns.conf Failure/Error: expect(chef_run).to create_file('/srv/apache/clowns') expected "file[/srv/apache/clowns]" with action :create to be in Chef run. Other file resources:
# ./spec/default_spec.rb:13:in `block (2 levels) in <top (required)>'
Finished in 0.01903 seconds2 examples, 1 failure
Failed examples:
rspec ./spec/default_spec.rb:12 # apache::default creates clowns.conf
329
Wednesday, April 30, 14
![Page 330: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/330.jpg)
$ bundle exec rspec --color
Run ChefSpec on your cookbok
F
Failures:
1) apache::default creates clowns.conf Failure/Error: expect(chef_run).to create_file('/srv/apache/clowns') expected "file[/srv/apache/clowns]" with action :create to be in Chef run. Other file resources:
# ./spec/default_spec.rb:13:in `block (2 levels) in <top (required)>'
Finished in 0.01903 seconds2 examples, 1 failure
Failed examples:
rspec ./spec/default_spec.rb:12 # apache::default creates clowns.conf
330
FAILWednesday, April 30, 14
![Page 331: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/331.jpg)
331
ChefSpec == Runnable Documentation• Remember: ChefSpec is just runnable
documentation• It isn’t actually performing a Chef run to verify that
clowns.conf was created• Instead it is just verifying that you told Chef to
create the clowns.conf via the file resource, which you never did - you used the template resource
Wednesday, April 30, 14
![Page 332: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/332.jpg)
332
template matcher
Wednesday, April 30, 14
![Page 333: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/333.jpg)
OPEN IN EDITOR: spec/default_spec.rb
require 'chefspec'
at_exit { ChefSpec::Coverage.report! }
describe 'apache::default' do let(:chef_run) { ChefSpec::Runner.new.converge(described_recipe) } ...
it 'creates clowns.conf' do expect(chef_run).to \ create_template('/etc/httpd/conf.d/clowns.conf') endend
Checking clowns.conf file
333
Wednesday, April 30, 14
![Page 334: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/334.jpg)
$ bundle exec rspec --color
Run ChefSpec on your cookbok
.
Finished in 0.01955 seconds2 examples, 0 failures
334
Wednesday, April 30, 14
![Page 335: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/335.jpg)
$ bundle exec rspec --color
Run ChefSpec on your cookbok
.
Finished in 0.01955 seconds2 examples, 0 failures
335
WINWednesday, April 30, 14
![Page 336: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/336.jpg)
336
spec_helper.rb• Similar to Serverspec, common code can be moved
to a file called spec_helper.rb with ChefSpec
Wednesday, April 30, 14
![Page 337: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/337.jpg)
OPEN IN EDITOR: spec/spec_helper.rb
require 'chefspec'
at_exit { ChefSpec::Coverage.report! }
Checking clowns.conf file
337
Wednesday, April 30, 14
![Page 338: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/338.jpg)
338
RSpec recurses through spec/*• RSpec recurses through the spec/ subtree, looking
for tests, so you can create any directory structure you like underneath
• We’ll move default_spec.rb to spec/recipes
Wednesday, April 30, 14
![Page 339: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/339.jpg)
$ mkdir spec/recipes$ mv spec/default_spec.rb spec/recipes
Move default_spec.rb
339
Wednesday, April 30, 14
![Page 340: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/340.jpg)
OPEN IN EDITOR: spec/recipes/default_spec.rb
require 'spec_helper'
describe 'apache::default' do let (:chef_run) \ { ChefSpec::Runner.new.converge(described_recipe) }
it 'installs apache2' do expect(chef_run).to install_package('httpd') end
it 'creates clowns.conf' do expect(chef_run).to \ create_template('/etc/httpd/conf.d/clowns.conf') endend
Checking clowns.conf file
340
Wednesday, April 30, 14
![Page 341: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/341.jpg)
$ bundle exec rspec --color
Run ChefSpec on your cookbok
.
Finished in 0.01955 seconds2 examples, 0 failures
341
Wednesday, April 30, 14
![Page 342: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/342.jpg)
$ bundle exec rspec --color
Run ChefSpec on your cookbok
.
Finished in 0.01955 seconds2 examples, 0 failures
342
WINWednesday, April 30, 14
![Page 343: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/343.jpg)
343
Where to go next• There’s a lot of ChefSpec written for the community
cookbooks. Check out the spec/ directory your favorites.
Wednesday, April 30, 14
![Page 344: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/344.jpg)
v2.0.0
Continuous Validation with Guard
344
Wednesday, April 30, 14
![Page 345: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/345.jpg)
What is Guard?• A tool that monitors for filesystem changes and
performs actions (like launching rake tasks)• Written by Thibaud Guillaume-Gentil
345
Wednesday, April 30, 14
![Page 346: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/346.jpg)
Guard install• Let’s install Guard on your development
workstation so you can give it a spin• Add guard to your Gemfile• Install the app with bundle install
346
Wednesday, April 30, 14
![Page 347: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/347.jpg)
OPEN IN EDITOR: cookbooks/apache/Gemfile
source 'https://rubygems.org'
gem 'test-kitchen'gem 'kitchen-vagrant'
gem 'serverspec', '~> 1.1'gem 'foodcritic', '~> 3.0'gem 'rubocop', '~> 0.20'gem 'chefspec', '~> 3.4'gem 'guard', '~> 2.6'
Gemfile
347
Wednesday, April 30, 14
![Page 348: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/348.jpg)
$ bundle install
Install Guard
348
$ CONFIGURE_ARGS="--with-ldflags=\'-Wno-error=unused-command-line-argument-hard-error-in-future'" bundle install
clang 5.1 Workaround
Wednesday, April 30, 14
![Page 349: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/349.jpg)
OPEN IN EDITOR: cookbooks/apache/Gemfile
source 'https://rubygems.org'
gem 'test-kitchen'gem 'kitchen-vagrant'
gem 'serverspec', '~> 1.1'gem 'foodcritic', '~> 3.0'gem 'rubocop', '~> 0.20'gem 'chefspec', '~> 3.4'gem 'guard', '~> 2.6'gem 'guard-rubocop', '~> 1.1'
Gemfile
349
Wednesday, April 30, 14
![Page 350: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/350.jpg)
$ bundle install
Install guard-rubocop
350
$ CONFIGURE_ARGS="--with-ldflags=\'-Wno-error=unused-command-line-argument-hard-error-in-future'" bundle install
clang 5.1 Workaround
Wednesday, April 30, 14
![Page 351: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/351.jpg)
$ bundle exec guard init
Create Guardfile
02:39:58 - INFO - Writing new Guardfile to /home/vagrant/chef-fundamentals-repo/cookbooks/apache/Guardfile02:45:32 - INFO - rubocop guard added to Guardfile, feel free to edit it
351
Wednesday, April 30, 14
![Page 352: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/352.jpg)
cookbooks/apache/Guardfile
352
# A sample Guardfile
# More info at https://github.com/guard/guard#readme
guard :rubocop do
watch(%r{.+\.rb$})
watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
end
Wednesday, April 30, 14
![Page 353: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/353.jpg)
$ bundle exec guard
Run Guard
02:48:54 - INFO - Guard is now watching at '/home/vagrant/chef-fundamentals-repo/cookbooks/apache'[1] guard(main)>
353
Wednesday, April 30, 14
![Page 354: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/354.jpg)
CloudShare Node
354
Wednesday, April 30, 14
![Page 355: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/355.jpg)
CloudShare Node
355
Wednesday, April 30, 14
![Page 356: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/356.jpg)
$ cd $HOME/chef-fundamentals-repo/cookbooks/apache
In another session
356
And edit some .rb file - upon save, rubocop is launched!
Wednesday, April 30, 14
![Page 357: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/357.jpg)
Stopping guard[1] guard(main)> quit
19:23:42 - INFO - Bye bye...
357
Wednesday, April 30, 14
![Page 358: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/358.jpg)
358
Where to go nextMichael Goetz blog posts:
https://micgo.net/check-yo-self-before-you-wreck-yo-self-with-foodcritic-chefspec/
Foodcritic and Guard:
Serverspec and Guard:https://micgo.net/serverspec-guard-and-test-kitchen-testing-
servers-like-a-boss/
Wednesday, April 30, 14
![Page 359: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/359.jpg)
359
Where to go nextMichael Goetz blog posts:
ChefSpec and Guard:https://micgo.net/continuous-chefspec-validation-with-guard/
Wednesday, April 30, 14
![Page 360: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/360.jpg)
v2.0.0
Repeating Test Steps with Rake
360
Wednesday, April 30, 14
![Page 361: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/361.jpg)
What is Rake?• Rake includes a language for expressing the
command line steps needed to create an app• Perfect for capturing all the commands you’ve
learned in this class so others can run them easily, or in your continuous integration system (Jenkins, Bamboo, TeamCity, etc.)
361
Wednesday, April 30, 14
![Page 362: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/362.jpg)
Rake Author• Written by Jim Weirich:
http://rake.rubyforge.org/
362
Wednesday, April 30, 14
![Page 363: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/363.jpg)
Rake - Repeatable Test Commands• Let’s install Rake on your development workstation
so you can give it a spin• Add rake to your Gemfile• Install the app with bundle install
363
Wednesday, April 30, 14
![Page 364: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/364.jpg)
OPEN IN EDITOR: chef-fundamentals-repo/Gemfile
source 'https://rubygems.org'
gem 'test-kitchen'gem 'kitchen-vagrant'
gem 'rake'gem 'serverspec', '~> 1.1'gem 'foodcritic', '~> 3.0'gem 'rubocop', '~> 0.20'gem 'chefspec', '~> 3.4'gem 'guard', '~> 2.6'gem 'guard-rubocop', '~> 1.1'
Gemfile
364
Wednesday, April 30, 14
![Page 365: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/365.jpg)
$ bundle install
Install Rake
365
$ CONFIGURE_ARGS="--with-ldflags=\'-Wno-error=unused-command-line-argument-hard-error-in-future'" bundle install
clang 5.1 Workaround
Wednesday, April 30, 14
![Page 366: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/366.jpg)
Rake - Repeatable Tasks• Task - expresses command line actions to perform
366
Wednesday, April 30, 14
![Page 367: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/367.jpg)
Rake Task Form
367
task :<task_name> do <action> <action>end
Wednesday, April 30, 14
![Page 368: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/368.jpg)
Rake - Repeatable Tasks• Configuration file for rake is a Rakefile
368
Wednesday, April 30, 14
![Page 369: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/369.jpg)
Rake - Actions• Actions are expressed in Ruby syntax• sh “<command>” runs a shell command:
sh “bundle exec rspec”
369
Wednesday, April 30, 14
![Page 370: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/370.jpg)
OPEN IN EDITOR: cookbooks/apache/Rakefile
task :rubocop do sh 'bundle exec rubocop'end
Rubocop Task
370
Wednesday, April 30, 14
![Page 371: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/371.jpg)
$ bundle exec rake rubocop
Execute Rake Task
Inspecting 9 files.......
9 files inspected, no offenses detected
371
Wednesday, April 30, 14
![Page 372: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/372.jpg)
Task Description• Every task should have a description which
documents what the task does• rake -‐-‐tasks prints out tasks with descriptions
372
Wednesday, April 30, 14
![Page 373: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/373.jpg)
OPEN IN EDITOR: cookbooks/apache/Rakefile
desc 'Run Ruby style checks with Rubocop'task :rubocop do sh 'bundle exec rubocop'end
Rubocop Task
373
Wednesday, April 30, 14
![Page 374: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/374.jpg)
$ bundle exec rake --tasks
Execute Rake Task
rake rubocop # Run Ruby style checks with Rubocop
374
Wednesday, April 30, 14
![Page 375: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/375.jpg)
Adding Foodcritic• Let’s add a task for Foocritic next
375
Wednesday, April 30, 14
![Page 376: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/376.jpg)
OPEN IN EDITOR: cookbooks/apache/Rakefile
desc 'Run Ruby style checks with Rubocop'task :rubocop do sh 'bundle exec rubocop'end
desc 'Run Chef style checks with Foodcritic'task :foodcritic do sh 'bundle exec foodcritic -t ~FC003 .'end
Foodcritic Task
376
Wednesday, April 30, 14
![Page 377: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/377.jpg)
$ bundle exec rake foodcritic
Execute Rake Task
bundle exec foodcritic -t ~FC003 .FC011: Missing README in markdown format: spec/README.md:1FC031: Cookbook without metadata file: spec/metadata.rb:1FC045: Consider setting cookbook name in metadata: spec/metadata.rb:1
377
Wednesday, April 30, 14
![Page 378: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/378.jpg)
$ bundle exec rake foodcritic
Execute Rake Task
bundle exec foodcritic -t ~FC003 .FC011: Missing README in markdown format: spec/README.md:1FC031: Cookbook without metadata file: spec/metadata.rb:1FC045: Consider setting cookbook name in metadata: spec/metadata.rb:1
378
WAT?Wednesday, April 30, 14
![Page 379: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/379.jpg)
Foodcritic 3.0.3 issue• Foodcritic is checking spec/ subtree when it
shouldn’t• Does not expose command line option to exclude
directories:https://github.com/acrmp/foodcritic/issues/148
• When fixed, this should work:bundle exec foodcritic -‐X spec -‐t ~FC003 .
379
Wednesday, April 30, 14
![Page 380: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/380.jpg)
OPEN IN EDITOR: cookbooks/apache/Rakefile
desc 'Run Ruby style checks with Rubocop'task :rubocop do sh 'bundle exec rubocop'end
require 'foodcritic'desc 'Run Chef style checks with Foodcritic'FoodCritic::Rake::LintTask.new(:foodcritic) do |t| t.options = { tags: ['~FC003'], excludes: ['test', 'spec', 'features'] }end
Workaround - Use Ruby
380
Wednesday, April 30, 14
![Page 381: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/381.jpg)
Default task• Rake supports a special task name called default• default runs when no parameters are supplied to
rake• default (as well as any other task) can point to a
list of other task names to execute task :default => [:foodcritic]
381
Wednesday, April 30, 14
![Page 382: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/382.jpg)
OPEN IN EDITOR: cookbooks/apache/Rakefile
task :default => [:rubocop, :foodcritic]
desc 'Run Ruby style checks with Rubocop'task :rubocop do sh 'bundle exec rubocop'end
require 'foodcritic'desc 'Run Chef style checks with Foodcritic'FoodCritic::Rake::LintTask.new(:foodcritic) do |t| t.options = { tags: ['~FC003'], excludes: ['test', 'spec', 'features' ] }end
Foodcritic Task
382
Wednesday, April 30, 14
![Page 383: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/383.jpg)
$ bundle exec rake
Execute Rake Task
bundle exec rubocopInspecting 9 files.......
7 files inspected, no offenses detected
383
Wednesday, April 30, 14
![Page 384: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/384.jpg)
384
Where to go next
Rake Boot Camp
http://cloud.github.com/downloads/jimweirich/RakePresentations/PowerRake.key.pdf
http://www.confreaks.com/videos/899-railsconf2012-basic-rake
Go to http://confreaks.comSearch for “Basic Rake”
Wednesday, April 30, 14
![Page 385: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/385.jpg)
385
Where to go next
Rake Tasks can have testshttp://blog.jayfields.com/2006/11/ruby-testing-rake-tasks.html
Wednesday, April 30, 14
![Page 386: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/386.jpg)
v2.0.0
Jenkins
386
Wednesday, April 30, 14
![Page 387: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/387.jpg)
What is Jenkins?• Jenkins is a commonly used, open source
continuous integration system used to build early and often
• Written by Kohsuke Kawaguchi
387
Wednesday, April 30, 14
![Page 388: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/388.jpg)
$ sudo yum install -y libxslt-devel libxml2-devel
Install Prerequisites
388
$ sudo apt-get install -y libxslt-dev libxml2-dev
Wednesday, April 30, 14
![Page 389: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/389.jpg)
$ cd $HOME
Home directory - great place for source
389
$ cd %USERPROFILE%
Wednesday, April 30, 14
![Page 390: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/390.jpg)
Jenkins cookbook• Jenkins cookbook - https://github.com/opscode-
cookbooks/jenkins• Jenkins cookbook is library cookbook
390
Wednesday, April 30, 14
![Page 391: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/391.jpg)
Library cookbook• Popularized by Bryan Berry’s blog post How to Write
Resuable Chef Cookbooks, Gangnam Style
391
Wednesday, April 30, 14
![Page 392: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/392.jpg)
Jenkins wrapper cookbook• Start of our wrapper cookbook:https://github.com/misheska/test-class-jenkins
392
Wednesday, April 30, 14
![Page 393: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/393.jpg)
$ git clone https://github.com/misheska/test-class-jenkins
Grab test-class-jenkins from Github
Cloning into 'test-class-jenkins'...remote: Counting objects: 19, done.remote: Compressing objects: 100% (16/16), done.remote: Total 19 (delta 0), reused 19 (delta 0)Unpacking objects: 100% (19/19), done.
393
Wednesday, April 30, 14
![Page 394: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/394.jpg)
$ cd test-class-jenkins
test-class-jenkins
394
Wednesday, April 30, 14
![Page 395: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/395.jpg)
$ bundle install --path vendor/bundle
Install gems vendored
Fetching gem metadata from https://rubygems.org/.......Fetching additional metadata from https://rubygems.org/..Resolving dependencies...Installing rake (10.2.2)Installing addressable (2.3.6)Installing ast (1.1.0)...Installing powerpack (0.0.9)Installing rainbow (2.0.0)Installing ruby-progressbar (1.4.2)Installing rubocop (0.20.1)Using bundler (1.5.3)Your bundle is complete!It was installed into ./vendor/bundle
395
Wednesday, April 30, 14
![Page 396: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/396.jpg)
Jenkins PSA
396
Wednesday, April 30, 14
![Page 397: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/397.jpg)
397
Wednesday, April 30, 14
![Page 398: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/398.jpg)
test-class-jenkins/recipes/default.rb
398
include_recipe 'jenkins::java'include_recipe 'jenkins::master'
Wednesday, April 30, 14
![Page 399: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/399.jpg)
test-class-jenkins/attributes/default.rb
399
include_attribute 'jenkins::master'
Wednesday, April 30, 14
![Page 400: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/400.jpg)
test-class-jenkins/Gemfile
400
source 'https://rubygems.org'
gem 'test-kitchen'gem 'kitchen-docker'
gem 'rake'gem 'berkshelf', '~> 3.0.0.rc'gem 'rubocop', '~> 0.20'gem 'foodcritic', '~> 3.0'
Wednesday, April 30, 14
![Page 401: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/401.jpg)
test-class-jenkins/.kitchen.yml
401
---driver: name: docker
provisioner: name: chef_solo
platforms: - name: centos-6.4 driver_config: forward: - 8080:8080
suites: - name: default run_list: - recipe[test-class-jenkins::default]
Wednesday, April 30, 14
![Page 402: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/402.jpg)
$ bundle exec kitchen converge
Perform Chef run of Jenkins wrapper
-----> Starting Kitchen (v1.2.1)-----> Creating <default-centos-64>... Step 0 : FROM centos:6.4...----> Converging <default-centos-64>... Preparing files for transfer Resolving cookbook dependencies with Berkshelf 3.0.0.rc1......
402
Wednesday, April 30, 14
![Page 403: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/403.jpg)
http://<EXTERNAL_ADDRESS>:8080
403
Wednesday, April 30, 14
![Page 404: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/404.jpg)
Check plugins
404
Wednesday, April 30, 14
![Page 405: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/405.jpg)
Just defaults
405
Wednesday, April 30, 14
![Page 406: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/406.jpg)
jenkins_plugin resource
406
Wednesday, April 30, 14
![Page 407: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/407.jpg)
OPEN IN EDITOR: test-class-jenkins/recipes/default.rb
include_recipe 'jenkins::java'include_recipe 'jenkins::master'
# Install version 1.13 of the greenballs pluginjenkins_plugin 'greenballs' do version '1.13'end
Install greenballs plugin
407
Wednesday, April 30, 14
![Page 408: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/408.jpg)
$ bundle exec kitchen converge
Perform Chef run of Jenkins wrapper
-----> Starting Kitchen (v1.2.1)-----> Creating <default-centos-64>... Step 0 : FROM centos:6.4...----> Converging <default-centos-64>... Preparing files for transfer Resolving cookbook dependencies with Berkshelf 3.0.0.rc1......
408
Wednesday, April 30, 14
![Page 409: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/409.jpg)
Greenballs installed
409
Wednesday, April 30, 14
![Page 410: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/410.jpg)
jenkins_script resource
410
Wednesday, April 30, 14
![Page 411: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/411.jpg)
OPEN IN EDITOR: test-class-jenkins/recipes/default.rb
...
jenkins_script 'configure-‐mailer' do command <<-‐GROOVY.gsub(/^ {4}/, '') jenkins = jenkins.model.Jenkins.getInstance() mailer = jenkins.getDescriptorByType(hudson.tasks.Mailer.DescriptorImpl) mailer.setSmtpHost("smtp.gmail.com") mailer.setUseSsl(true) mailer.setSmtpAuth("smtp", "password") mailer.setReplyToAddress("[email protected]") mailer.save() GROOVYend
Configure E-mail Notification
411
Wednesday, April 30, 14
![Page 412: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/412.jpg)
412
Wednesday, April 30, 14
![Page 413: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/413.jpg)
$ bundle exec kitchen converge
Perform Chef run of Jenkins wrapper
-----> Starting Kitchen (v1.2.1)-----> Creating <default-centos-64>... Step 0 : FROM centos:6.4...----> Converging <default-centos-64>... Preparing files for transfer Resolving cookbook dependencies with Berkshelf 3.0.0.rc1......
413
Wednesday, April 30, 14
![Page 414: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/414.jpg)
414
Wednesday, April 30, 14
![Page 415: Testing Your Automation Code (Docker Version)](https://reader037.fdocuments.in/reader037/viewer/2022103016/554f6cfbb4c905bb178b4f1a/html5/thumbnails/415.jpg)
jenkins_job resource
415
Wednesday, April 30, 14