Test-Kitchen Workflow With Docker For OSX

Test-Kitchen Workflow With Docker For OSX

Script Code

Everyone knows that automation is key to being successful in getting your job done and done correctly. One thing that people complain about over and over is the time it takes to set up a fast feedback loop to properly validation your configuration management code be it puppet, ansible, chef, or saltstack.   Our Managed Services team exclusively uses Test-Kitchen to streamline this workflow for the shortest possible feedback loop for this work.   So let’s dive into a few of these points and get your baseline setup working!

What Gains You’ll Make From This Article:

  • Full local development setup for configuration management
  • A repeatable process to scale out to your DevOps team.
  • A look into immutable infrastructure
  • Lighter weight base test infrastructure
  • Very short feedback loop

Getting The Pre-Reqs Installed For OSX:

Here is the list of software that you will need installed prior to getting going with this:

  • Homebrew
  • Terminal Software (we suggest iTerm2 + zsh)
  • ruby 2.x
  • bundler (gem install bundler)
  • Coffee / Beer (depending on the time of the day you’re cranking through this)

Installing Docker Edge

We’re going to use Docker as the main provider that we’re going to work within this tutorial and we want all of the features available to us as we’re going to be working with them over the course of the week.

https://docs.docker.com/docker-for-mac/install/#download-docker-for-mac

  • Grab the Edge dmg file and get it installed by following the documentation
  • Make sure that docker service is running by opening your terminal and run the following commands:
    • docker ps

Installing Chef Development Kit (ChefDK)

Regardless of what configuration management solution that you are using you will want to install the full chef development kit to make sure that you have test-kitchen installed correctly and properly.

https://downloads.chef.io/chefdk

  • Grab the latest dmg and install it

Now we’re ready to rock and get to the interesting stuff!!!

Bootstrapping Your Project

We’re going to do a fairly simple project and just get your project bootstrapped where we will pull down an Ubuntu 16.04 docker image and prep it for installing apache with Ansible, which is RLT’s preferred configuration management solution of choice.

In your terminal run these commands

mkdir rlt-kitchen
cd rlt-kitchen
git init && kitchen init

➜ rlt-kitchen git:(master) ✗ ls -lah
total 24
drwxr-xr-x 7 efouarge staff 224B Jan 8 13:13 .
drwxr-xr-x 9 efouarge staff 288B Jan 8 13:12 ..
drwxr-xr-x 9 efouarge staff 288B Jan 8 13:13 .git
-rw-r--r-- 1 efouarge staff 29B Jan 8 13:13 .gitignore
-rw-r--r-- 1 efouarge staff 173B Jan 8 13:13 .kitchen.yml
-rw-r--r-- 1 efouarge staff 9B Jan 8 13:13 chefignore
drwxr-xr-x 3 efouarge staff 96B Jan 8 13:13 test

mkdir -p roles/apache/tasks
mkdir -p roles/apache/handlers
mkdir -p roles/core/tasks
mkdir inventories
mkdir group_vars

Your project is now bootstrapped and you should be ready to start getting your project going

Setting Up Test-Kitchen With Docker Support

Now to get Docker rolling with test-kitchen we’re going to need to make a few quick edits and couple new files.

Edit the .kitchen.yml

---
driver:
 name: docker
 privileged: true
 use_sudo: true

provisioner:
 name: ansible_playbook
 roles_path: roles
 require_ansible_repo: true
 ansible_verbose: true
 ansible_version: latest
 hosts: webservers
 playbook: site.yml
 ansible_inventory: inventories/development

platforms:
 - name: ubuntu-16.04

suites:
 - name: default

Create file Gemfile with this content:

# RLT test-kitchen sample Gemfile
source "https://rubygems.org"

gem "test-kitchen"
gem "kitchen-docker"
gem "kitchen-ansible"
gem "net-ssh"
gem "serverspec"
gem "kitchen-verifier-serverspec"

Install the new ruby gems with bundler:

bundle install
kitchen list
## Expected output
Instance Driver Provisioner Verifier Transport Last Action Last Error
default-ubuntu-1604 Docker AnsiblePlaybook Busser Ssh <Not Created> <None>

Great work!! Now we’re ready to push on to getting the Ansible code in place

Integrating Ansible Into The Workflow

We’ve already created the base directory structure, now we just need to get the base ansible code in to get our Apache server up and going.

Create file site.yml

---
- name: Setup webserver
 hosts: webservers
 roles:
 - core
 - apache

Create file ansible.cfg

[defaults]
# uncomment this to disable SSH key host checking
host_key_checking = False

Create file inventories/development

[webservers]
localhost

Create file groups_vars/dev

---
serverMode: DEV

Create file roles/core/tasks/main.yml

---
- name: Debug {{ serverMode }}
 debug: 
 msg="Server Environment - {{ serverMode }}"

- name: Update cache
 apt: 
 update_cache=yes cache_valid_time=3600
 sudo: 
 yes

Create file roles/apache/tasks/main.yml

---
- name: Install Apache2
 apt: 
 name=apache2 state=installed
 sudo: 
 yes
 tags:
 - apache2

- name: Disable OS default site config
 command: 
 a2dissite 000-default
 sudo: 
 yes
 register: 
 result
 changed_when: 
 "'already disabled' not in result.stdout"
 notify:
 - restart-apache2
 tags:
 - apache2

Create file roles/apache/handlers/main.yaml

---
- name: restart-apache2
 service: 
 name=apache2 state=restarted
 sudo: 
 yes
 tags:
 - apache2

Now the ansible code is completed!  Let’s validate it is working!!

Validating Ansible Code With Test-Kitchen

This is the core of the workflow and the meat that you are looking for as a DevOps Engineer.

Create your instances

kitchen create

Apply the Ansible code

kitchen converge
kitchen login default-ubuntu-1604

Tear down process

kitchen destroy

What’s Next?

This is just a part of larger series of posts that we will be publishing around streamlining your DevOps workflow. Reach out to us here if you need a larger project delivered or an assessment done on your current workflow!

Coming down the pipeline here are the upcoming articles!! Don’t see something you’d want us to cover let us know in the comments.

  • Serverspec integration
  • bats integration
  • Inspec integration
  • Dealing with Windows config management testing
  • Puppet
  • Jenkins Integration
  • Complex application config management testing
No Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.