Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tknerr/docker-seminar-tdi
Demo code for my "Test-Driven Infrastructure with Docker, ServerSpec and KitchenCI" talk here http://slides.com/tknerr/tdi-with-kitchenci-and-docker/live#/
https://github.com/tknerr/docker-seminar-tdi
Last synced: about 2 months ago
JSON representation
Demo code for my "Test-Driven Infrastructure with Docker, ServerSpec and KitchenCI" talk here http://slides.com/tknerr/tdi-with-kitchenci-and-docker/live#/
- Host: GitHub
- URL: https://github.com/tknerr/docker-seminar-tdi
- Owner: tknerr
- Created: 2015-05-06T09:58:41.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2015-05-06T13:02:45.000Z (over 9 years ago)
- Last Synced: 2023-03-23T00:22:24.365Z (almost 2 years ago)
- Language: Ruby
- Size: 125 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Sample Repo for the Docker Seminar
Code repository for my HL docker seminar talk about [Test-Driven Infrastructure with Docker, ServerSpec and KitchenCI](http://slides.com/tknerr/tdi-with-kitchenci-and-docker/live#/)
Below are the steps from the "Demo Time!" part for you to follow. For the demo I used the [Bill's Kitchen DevPack](https://github.com/tknerr/bills-kitchen) which includes Docker 1.6.0 (with boot2docker) and KitchenCI 1.4.0.
## Generate the motd Cookbook
Let's create a sample "motd" cookbook:
```
chef generate cookbook motd
cd motd
```## Run the (not yet existing) Tests
Without further ado, we can already run some tests:
* `foodcritic .` - to run chef linting checks
* `rspec` - to run ChefSpec unit tests
* `kitchen test` - to run integration testsRunning `kitchen test` with VirtualBox is quite slow:
```
W:\repo\docker-seminar-tdi\motd>kitchen test 1204
-----> Starting Kitchen (v1.4.0)
-----> Cleaning up any prior instances of
-----> Destroying ...
Finished destroying (0m0.00s).
-----> Testing
-----> Creating ...
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'opscode-ubuntu-12.04'......
Finished destroying (0m8.01s).
Finished testing (1m31.10s).
-----> Kitchen is finished. (1m33.65s)
```## Let's get faster by using Docker!
Since running `kitchen test` with VirtualBox is sloooooooooooow, so let's fix that
by creating a `.kitchen.docker.yml` file with the necessary overrides for the
docker provisioner:```yml
---
driver:
name: dockerdriver_config:
provision_command:
- curl -L https://www.opscode.com/chef/install.sh | bash -s -- -v 12.3.0
require_chef_omnibus: false
use_sudo: false
```Note that we installed the omnibus chef client via `provision_command`, which
basically adds a `RUN` entry in the Dockerfile and thus we get it cached in
a docker layer :-)In order to use it, you have to `set KITCHEN_LOCAL_YAML=.kitchen.docker.yml`
and then run `kitchen test` again.Weeeeeeeeee, much faster, and we can even run it in parallel via `kitchen test -c` now! :-)
```
W:\repo\docker-seminar-tdi\motd>kitchen test -c
-----> Starting Kitchen (v1.4.0)
-----> Cleaning up any prior instances of
-----> Cleaning up any prior instances of
-----> Destroying ...
-----> Destroying ...
Finished destroying (0m0.00s).
Finished destroying (0m0.00s).
-----> Testing
-----> Testing
-----> Creating ...
-----> Creating ......
Finished testing (0m17.68s).
Finished testing (0m23.27s).
-----> Kitchen is finished. (0m25.49s)
```## Describe what we expect in a Test
Hah, let's approach it in a test-first manner :-)
So we can describe the expected state in `test/integration/default/serverspec/default_spec.rb`:
```ruby
require 'spec_helper'describe 'motd::default' do
# Serverspec examples can be found at
# http://serverspec.org/resource_types.htmldescribe file('/var/run/motd') do
it { should be_file }
it { should be_mode 644 }
it { should contain "hellooooo" }
end
end
```
## Run and see it failingNow run `kitchen verify 1204` to converge the node and see the tests failing:
```
W:\repo\docker-seminar-tdi\motd>kitchen verify ubuntu
-----> Starting Kitchen (v1.4.0)
-----> Verifying ...
$$$$$$ Running legacy verify for 'Docker' Driver
Preparing files for transfer
Removing /tmp/verifier/suites/serverspec
Transferring files to
-----> Running serverspec test suite
/opt/chef/embedded/bin/ruby -I/tmp/verifier/suites/serverspec -I/tmp/verifier/gems/gems/rspec-support-3.2.2/lib:/tmp/verifier/gems/gems/rspec-core-3.2.3/lib /opt/chef/embedded/bin/rspec --pattern /tmp/verifier/suites/serverspec/\*\*/\*_spec.rb --color --format documentation --default-path /tmp/verifier/suites/serverspecmotd::default
File "/var/run/motd"
should be file
should be mode 644
should contain "hellooooo" (FAILED - 1)Failures:
1) motd::default File "/var/run/motd" should contain "hellooooo"
Failure/Error: it { should contain "hellooooo" }
expected File "/var/run/motd" to contain "hellooooo"
/bin/sh -c grep\ -qs\ --\ hellooooo\ /var/run/motd\ \|\|\ grep\ -qFs\ --\ hellooooo\ /var/run/motd# /tmp/verifier/suites/serverspec/default_spec.rb:11:in `block (3 levels) in '
Finished in 0.12986 seconds (files took 0.29833 seconds to load)
3 examples, 1 failureFailed examples:
rspec /tmp/verifier/suites/serverspec/default_spec.rb:11 # motd::default File "/var/run/motd" should contain "hellooooo"
```## Fix the test by placing the motd file
Ok, so let's edit `recipes/default.rb` and add a simple motd file:
```ruby
#
# Cookbook Name:: motd
# Recipe:: default
#
# Copyright (c) 2015 The Authors, All Rights Reserved.file '/var/run/motd' do
content "hellooooooooooo"
mode "0644"
action :create
end
```Ok, lets see it!
```
W:\repo\docker-seminar-tdi\motd>kitchen verify ubuntu
-----> Starting Kitchen (v1.4.0)
-----> Verifying ...
$$$$$$ Running legacy verify for 'Docker' Driver
Preparing files for transfer
Removing /tmp/verifier/suites/serverspec
Transferring files to
-----> Running serverspec test suite
/opt/chef/embedded/bin/ruby -I/tmp/verifier/suites/serverspec -I/tmp/verifier/gems/gems/rspec-support-3.2.2/lib:/tmp/verifier/gems/gems/rspec-core-3.2.3/lib /opt/chef/embedded/bin/rspec --pattern /tmp/verifier/suites/serverspec/\*\*/\*_spec.rb --color --format documentation --default-path /tmp/verifier/suites/serverspecmotd::default
File "/var/run/motd"
should be file
should be mode 644
should contain "hellooooo"Finished in 0.1236 seconds (files took 0.30404 seconds to load)
3 examples, 0 failuresFinished verifying (0m1.43s).
-----> Kitchen is finished. (0m3.66s)
```Weeeeee, green tests! :-)