Categories
Testing

Infrastructure testing with Kitchen CI

Requirements

gem install kitchen-ansible
gem install kitchen-vagrant
gem install kitchen-inspec

Commands

$ kitchen create

$ kitchen converge

$ kitchen setup

$ kitchen verify

$ kitchen destroy

Initiate Kitchen

Inside a directory

kitchen init --driver=vagrant --provisioner=ansible_playbook --create-gemfile

Ansible Playbook

playbook.yml

---

- name: Test Role
  hosts: test-kitchen
  user: vagrant
  roles:
    - nginx

roles/nginx/tasks/main.yml

---
- name: install Nginx
  yum: pkg={{ item }} state=installed
  with_items:
    - nginx

- name: ensure nginx is running automatically at boot time
  service: name=nginx state=restarted enabled=yes
$ cat hosts
[test-kitchen]
localhost

Kitchen CI

Kitchen.yml

---
driver:
  name: vagrant

provisioner:
  name: ansible_playbook
  hosts: test-kitchen
  roles_path: roles
  require_ansible_repo: true
  ansible_verbose: true
  ansible_version: latest
  require_chef_for_busser: false
  playbook: playbook.yml

platforms:
  - name: centos-7.2

verifier:
  name: inspec
  sudo_path: true

suites:
  - name: default
    verifier:
      inspec_tests:
        - test/integration/default

Inspec

test/integration/default/default_spec.rb

describe package('nginx') do
  it { should be_installed }
end

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

describe port(80) do
  it { should be_listening }
end
kitchen list
kitchen create
kitchen converge
kitchen verify

To Destroy

kitchen destroy