blob: 2c2a84ec1d91a62fe2068026c7f41ce43a1e3a7a [file] [log] [blame]
Petr Michalecd1ff7bd2016-07-14 10:43:13 +02001#!/bin/bash
2
Petr Michalec993e3fa2016-07-15 15:56:34 +02003# Script to add Kitchen configuration to existing formulas.
Petr Michalecd1ff7bd2016-07-14 10:43:13 +02004# usage:
Petr Michalec71b9c882016-09-23 10:32:38 +02005# curl -skL "https://raw.githubusercontent.com/tcpcloud/cookiecutter-salt-formula/master/kitchen-init.sh" | bash -s --
Petr Michalecd1ff7bd2016-07-14 10:43:13 +02006
Petr Michaleca7af1222016-09-23 09:47:24 +02007# source gist:
8# https://gist.github.com/epcim/b0368794e69e6807635b0c7268e5ceec
Petr Michalecd1ff7bd2016-07-14 10:43:13 +02009
10# CONFIG
Petr Michalec993e3fa2016-07-15 15:56:34 +020011###################################
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020012
Petr Michalec993e3fa2016-07-15 15:56:34 +020013export driver=${driver:-vagrant} # vagrant, dokken, openstack, ...
14export verifier=${verifier:-inspec} # serverspec, pester
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020015
Petr Michaleca347c802016-07-20 21:49:52 +020016export formula=${formula:-$(awk -F: '/^name/{gsub(/[\ \"]/,"");print $2}' metadata.yml)}
Petr Michalec60b1b212016-07-26 07:37:39 +020017export suites=$(ls tests/pillar|xargs -I{} basename {} .sls)
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020018
Petr Michalec71b9c882016-09-23 10:32:38 +020019export SOURCE_REPO_URI="https://raw.githubusercontent.com/tcpcloud/cookiecutter-salt-formula/master/%7B%7Bcookiecutter.project_name%7D%7D"
Petr Michalec993e3fa2016-07-15 15:56:34 +020020
Petr Michalec1eac0ef2016-07-26 20:56:55 +020021which envtpl &> /dev/null || {
22 echo "ERROR: missing prerequisite, install 'envtpl' first : pip install envtpl"
23 exit 1
24}
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020025
26# INIT
Petr Michalec993e3fa2016-07-15 15:56:34 +020027###################################
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020028test ! -e .kitchen.yml || {
Petr Michalec1eac0ef2016-07-26 20:56:55 +020029 kitchen init -D kitchen-vagrant -P kitchen-salt --no-create-gemfile
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020030 echo .kitchen >> .gitignore
31 rm -rf test
32 rm -f .kitchen.yml
33 rm -f chefignore
34}
35
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020036
Petr Michalec993e3fa2016-07-15 15:56:34 +020037# CONFIGURE & SCAFFOLD TEST DIR
38###################################
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020039test -d tests/integration || {
Petr Michalec59566fb2016-07-26 22:39:50 +020040 for suite in $(echo $suites|xargs); do
Petr Michalec3e89e1b2016-07-26 21:43:17 +020041 mkdir -p tests/integration/$suite/$verifier
42 touch tests/integration/$suite/$verifier/default_spec.rb
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020043 done
Petr Michalec3e89e1b2016-07-26 21:43:17 +020044 mkdir -p tests/integration/helpers/$verifier/
Petr Michalec59566fb2016-07-26 22:39:50 +020045 touch tests/integration/helpers/$verifier/spec_helper.rb
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020046}
47
48
49# .KITCHEN.YML
Petr Michalec993e3fa2016-07-15 15:56:34 +020050###################################
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020051
Petr Michalec993e3fa2016-07-15 15:56:34 +020052test -e .kitchen.yml || \
Alena Holanova58406c12016-07-19 13:49:41 +020053envtpl < <(curl -skL "${SOURCE_REPO_URI}/.kitchen.yml" -- | sed 's/cookiecutter\.kitchen_//g') > .kitchen.yml
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020054
Petr Michalec221b2322016-07-26 07:33:42 +020055[[ "$driver" != "docker" ]] && {
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020056 test -e .kitchen.docker.yml || \
Petr Michalec1eac0ef2016-07-26 20:56:55 +020057 envtpl < <(curl -skL "${SOURCE_REPO_URI}/.kitchen.docker.yml" -- | sed 's/cookiecutter\.kitchen_//g' ) > .kitchen.docker.yml
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020058}
59
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020060test -e .kitchen.openstack.yml || \
Alena Holanova58406c12016-07-19 13:49:41 +020061envtpl < <(curl -skL "${SOURCE_REPO_URI}/.kitchen.openstack.yml" -- | sed 's/cookiecutter\.kitchen_//g') > .kitchen.openstack.yml
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020062
63
64
Petr Michalec993e3fa2016-07-15 15:56:34 +020065# UPDATE README, etc...
66###################################
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020067
Petr Michalec993e3fa2016-07-15 15:56:34 +020068grep -Eoq 'Development and testing' README.* || {
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020069
70KITCHEN_LIST=$(kitchen list|tail -n+2)
71cat >> README.* <<-\EOF
72
73 Development and testing
74 =======================
75
76 Development and test workflow with `Test Kitchen <http://kitchen.ci>`_ and
77 `kitchen-salt <https://github.com/simonmcc/kitchen-salt>`_ provisioner plugin.
78
79 Test Kitchen is a test harness tool to execute your configured code on one or more platforms in isolation.
80 There is a ``.kitchen.yml`` in main directory that defines *platforms* to be tested and *suites* to execute on them.
81
82 Kitchen CI can spin instances locally or remote, based on used *driver*.
83 For local development ``.kitchen.yml`` defines a `vagrant <https://github.com/test-kitchen/kitchen-vagrant>`_ or
84 `docker <https://github.com/test-kitchen/kitchen-docker>`_ driver.
85
86 To use backend drivers or implement your CI follow the section `INTEGRATION.rst#Continuous Integration`__.
87
88 A listing of scenarios to be executed:
89
90 .. code-block:: shell
91
92 $ kitchen list
93
94 Instance Driver Provisioner Verifier Transport Last Action
95
96EOF
97
98echo "$KITCHEN_LIST" | sed 's/^/ /' >> README.*
99
100cat >> README.* <<-\EOF
101
102 The `Busser <https://github.com/test-kitchen/busser>`_ *Verifier* is used to setup and run tests
103 implementated in `<repo>/test/integration`. It installs the particular driver to tested instance
104 (`Serverspec <https://github.com/neillturner/kitchen-verifier-serverspec>`_,
105 `InSpec <https://github.com/chef/kitchen-inspec>`_, Shell, Bats, ...) prior the verification is executed.
106
107
108 Usage:
109
110 .. code-block:: shell
111
Petr Michalec993e3fa2016-07-15 15:56:34 +0200112 # list instances and status
113 kitchen list
Petr Michalecd1ff7bd2016-07-14 10:43:13 +0200114
Petr Michalec993e3fa2016-07-15 15:56:34 +0200115 # manually execute integration tests
116 kitchen [test || [create|converge|verify|exec|login|destroy|...]] [instance] -t tests/integration
117
118 # use with provided Makefile (ie: within CI pipeline)
119 make kitchen
Petr Michalecd1ff7bd2016-07-14 10:43:13 +0200120
121EOF
Petr Michalec993e3fa2016-07-15 15:56:34 +0200122}
Petr Michalecd1ff7bd2016-07-14 10:43:13 +0200123
Petr Michalec993e3fa2016-07-15 15:56:34 +0200124test -e INTEGRATION.rst || \
Alena Holanova5364af32016-07-19 13:42:55 +0200125curl -skL "${SOURCE_REPO_URI}/INTEGRATION.rst" -o INTEGRATION.rst
Petr Michalec993e3fa2016-07-15 15:56:34 +0200126
127
128# ADD CHANGES TO GIT
129###################################
130
131# update Makefile, but do not auto-add to git
Alena Holanova5364af32016-07-19 13:42:55 +0200132curl -skL "${SOURCE_REPO_URI}/Makefile" -o Makefile
Petr Michalec993e3fa2016-07-15 15:56:34 +0200133
134git add \
135 .gitignore \
136 .kitchen*yml \
137 INTEGRATION.rst \
138 README.rst
139
Petr Michaleca7af1222016-09-23 09:47:24 +0200140git status