I have put off learning how test-kitchen works with salt formulas for awhile. Mainly due to the multi-step setup process that can be slightly confusing if you are not accustomed to working with developer tools day to day. It became clear though that learning this workflow would be really beneficial for developing salt-formulas and other projects because of the increased testing speed and the ability to quickly iterate changes without building bulky VM infrastructure.
While the above approach is great, I have found some oddities when using docker for testing salt-formulas. As an example, something that works without issue in a VM, doesn’t always work the same way when using containers. The complex relationships between the build OS and the container OS can add the need for interesting workarounds. Nevertheless, if this sounds interesting, then use this quick setup guide for getting started.
There are several technologies used to create the underlying build system so we can take advantage of the benefits of the proposed solution such as; Ruby, Docker, KitchenSalt, InSpec, and a CI tool like Travis. Below are steps to install each major sub-component.
Install docker
Install docker by following the steps located here https://docs.docker.com/install/
Once completed, make sure your user is setup to use docker.
sudo usermod -aG docker <username>
You may have to reload your profile.
source /etc/profile
Check if you can run docker ps from the cli as your user.
Install rbenv, ruby, and bundler
Install the dependencies for building ruby on CentOS:
sudo yum install -y git openssl-devel readline-devel zlib-devel bzip2 make gcc gcc-c++
Clone the rbenv repos and setup your path:
sudo git clone git://github.com/rbenv/rbenv.git /usr/local/rbenv sudo mkdir /usr/local/rbenv/plugins sudo git clone git://github.com/rbenv/ruby-build.git /usr/local/rbenv/plugins/ruby-build sudo tee /etc/profile.d/rbenv.sh <<< 'export PATH="/usr/local/rbenv/plugins/ruby-build/bin:/usr/local/rbenv/bin:$PATH"' sudo tee -a /etc/profile.d/rbenv.sh <<< 'source <(rbenv init -)'
Install ruby with rbenv and set the version to use by default:
Versions of ruby to install https://www.ruby-lang.org/en/downloads/releases/
rbenv install 2.6.3 rbenv global 2.6.3
Install bundler, as it provides a consistent deployment by installing the defined ruby gems within individual project environments.
gem install bundler
Install KitchenSalt
Create a Gemfile in your working directory and add; kitchen-salt, kitchen-docker, and kitchen-sync entries.
vi Gemfile
insert the below contents:
# frozen_string_literal: true source 'https://rubygems.org' gem 'kitchen-docker' gem 'kitchen-salt' gem 'kitchen-sync'
Now run bundle install
Validate everything works
A quick test is to clone the salt-formulas template-formula to make sure everything works.
git clone https://github.com/saltstack-formulas/template-formula
cd into the repo and configure things
cd template-formula bundle install
Run your test
bin/kitchen test centos-7-2019-2-py3
You can look in the .travis.yml file for additional platforms to test if you are curious.
REF:https://s.saltstack.com/testing-your-salt-states-with-kitchen-salt/
REF:https://kitchen.saltstack.com/docs/file/docs/jenkins.md
REF:https://medium.com/linux-academy/infrastructure-testing-for-beginners-getting-started-with-test-kitchen-and-salt-fb31942c6cbf
REF:https://thepracticalsysadmin.com/test-kitchen-style-testing-for-salt/
REF:https://github.com/test-kitchen/kitchen-docker
REF:https://github.com/saltstack/kitchen-salt
REF:https://www.linode.com/docs/applications/configuration-management/test-salt-locally-with-kitchen-salt/
REF:https://www.inspec.io/docs/reference/dsl_inspec/
REF:https://learn.chef.io/modules/explore-inspec-resources#/