Skip to content

Instantly share code, notes, and snippets.

@cwebberOps
Created October 20, 2014 14:07
Show Gist options
  • Save cwebberOps/0c42002a0370a7cb9bbf to your computer and use it in GitHub Desktop.
Save cwebberOps/0c42002a0370a7cb9bbf to your computer and use it in GitHub Desktop.
ProStack v0.1 - Test Driven Infra

Pro Stack 0.1

Test Driven Infrastructure with Chef

Slides

  • Title
  • Intro
    • About Me
      • Community Software Engineer at Chef
      • Organizer of LA DevOps, HangOps, SysAdvent
      • Co Host of Ops All The Things
  • What is Chef?
  • Let's Talk About Testing
    • Types
      • Linting
      • Unit Testing (Pre-Convergence)
      • Convergence Testing (Does it compile)
      • Integration Testing (Post-Converge)
    • Linting
      • Rubocop for Ruby
      • Foodcritic for Chef
    • Convergence Testing
      • Kitchen Test Kitchen
      • I <3 Digital Ocean
    • Post Convergence Testing
      • Serveserverspec
      • BATS
  • Demo Time!
    • Build an app that does...

Code

Create Cookbook

chef generate cookbook awesomeapp
echo "Make Chris' Environment More Standard"
export KITCHEN_YAML=.kitchen.yml
cd awesomeapp
Setup Kitchen to Use Digtal Ocean
vi .kitchen.yml

Content

---
driver_config:
  digitalocean_client_id: <%= ENV['DIGITAL_OCEAN_CLIENT_ID'] %>
  digitalocean_api_key: <%= ENV['DIGITAL_OCEAN_API_KEY'] %>

provisioner:
  name: chef_zero
  require_chef_omnibus: latest

platforms:
- name: ubuntu-14.04
  driver_plugin: digital_ocean
  driver_config:
    size: 1gb
    image: ubuntu-14-04-x64
    region: <%= ENV['DIGITAL_OCEAN_REGION'] %>

suites:

- name: default
  run_list:
  - recipe[awesomeapp]
  attributes: {}
Write Some Tests
mkdir -p test/integration/default/serverspec
vi test/integration/default/serverspec/spec_helper.rb

Content

require 'serverspec'

set :backend, :exec

set :path, '/sbin:/usr/sbin:/usr/local/sbin:$PATH'

vi test/integration/default/serverspec/default_spec.rb

Content

require 'spec_helper'

describe process('nginx') do
  it { should be_running }
end

describe port('80') do
  it { should be_listening }
end
Converge and Run Tests
kitchen list
kitchen converge
kitchen verify
Make Some Tests Pass

Add nginx cookbook

vi metadata.rb

Add:

depends 'nginx'

vi recipes/default.rb

Add:

include_recipe 'nginx'

kitchen converge
kitchen verify
Setup A Site

Start with Tests

vi test/integration/default/serverspec/an_site_spec.rb

Content

require 'spec_helper'

describe command('curl http://localhost') do
  its(:stdout) { should match %r{An Site} }
end

Make 'em pass

vi recipes/default.rb

Add

directory '/var/www/nginx-default/' do
  action :create
  recursive true
  group 'www-data'
  owner 'www-data'
  mode '0755'
end

file '/var/www/nginx-default/index.html' do
  action :create
  content 'An Site'
  group 'www-data'
  owner 'www-data'
  mode '0644'
end

kitchen converge
kitchen verify
Add a vhost

Start with tests

vi test/integration/default/serverspec/teh_site_spec.rb

Content

require 'spec_helper'

describe command("curl -H 'Host: teh.site.com' http://localhost/") do
  its(:stdout) { should match %r{Teh Site} }
end

Make it Pass

vi recipes/default.rb

Add

template '/etc/nginx/sites-available/teh_site' do
  action :create
  source 'teh_site.erb'
  owner 'root'
  group 'root'
  mode '0644'
end

nginx_site 'teh_site'

directory '/var/www/teh_site/' do
  action :create
  recursive true
  group 'www-data'
  owner 'www-data'
  mode '0755'
end

file '/var/www/teh_site/index.html' do
  action :create
  content 'Teh Site'
  group 'www-data'
  owner 'www-data'
  mode '0644'
end

mkdir -p templates/default
vi templates/default/teh_site.erb

Content:

server {
  listen   80;
  root /var/www/teh_site;
  index index.html index.htm;
  server_name teh.site.com;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment