blob: c6ca3210ceb0225a8045e054e343a45353691c6e [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 Michalec3f949f62016-11-15 23:21:56 +010013export driver=${driver:-docker} # vagrant, dokken, openstack, ...
Petr Michalec993e3fa2016-07-15 15:56:34 +020014export 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 || {
Petr Michalec3f949f62016-11-15 23:21:56 +010022 echo "ERROR: missing prerequisite, install 'envtpl' first : sudo pip install envtpl"
Petr Michalec1eac0ef2016-07-26 20:56:55 +020023 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 Michalec3f949f62016-11-15 23:21:56 +010029 kitchen init -D kitchen-docker -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 Michalec3f949f62016-11-15 23:21:56 +010040 mkdir -p tests/integration
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020041}
Petr Michalec3f949f62016-11-15 23:21:56 +010042# for suite in $(echo $suites|xargs); do
43# mkdir -p tests/integration/$suite/$verifier
44# touch tests/integration/$suite/$verifier/default_spec.rb
45# done
46# mkdir -p tests/integration/helpers/$verifier/
47# touch tests/integration/helpers/$verifier/spec_helper.rb
48#}
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020049
50
51# .KITCHEN.YML
Petr Michalec993e3fa2016-07-15 15:56:34 +020052###################################
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020053
Petr Michalec993e3fa2016-07-15 15:56:34 +020054test -e .kitchen.yml || \
Petr Michalec3f949f62016-11-15 23:21:56 +010055 envtpl < <(curl -skL "${SOURCE_REPO_URI}/.kitchen.${driver}.yml" -- | sed 's/cookiecutter\.kitchen_//g' ) > .kitchen.yml
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020056
Petr Michalec221b2322016-07-26 07:33:42 +020057[[ "$driver" != "docker" ]] && {
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020058 test -e .kitchen.docker.yml || \
Petr Michalec3f949f62016-11-15 23:21:56 +010059 envtpl < <(curl -skL "${SOURCE_REPO_URI}/.kitchen.docker.yml" -- | sed 's/cookiecutter\.kitchen_//g') > .kitchen.docker.yml
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020060}
61
Petr Michalec3f949f62016-11-15 23:21:56 +010062[[ "$driver" != "vagrant" ]] && {
63 test -e .kitchen.vagrant.yml || \
64 envtpl < <(curl -skL "${SOURCE_REPO_URI}/.kitchen.vagrant.yml" -- | sed 's/cookiecutter\.kitchen_//g') > .kitchen.vagrant.yml
65}
66
67[[ "$driver" != "openstack" ]] && {
68 test -e .kitchen.openstack.yml || \
69 envtpl < <(curl -skL "${SOURCE_REPO_URI}/.kitchen.openstack.yml" -- | sed 's/cookiecutter\.kitchen_//g') > .kitchen.openstack.yml
70}
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020071
72
73
Petr Michalec993e3fa2016-07-15 15:56:34 +020074# UPDATE README, etc...
75###################################
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020076
Petr Michalec993e3fa2016-07-15 15:56:34 +020077grep -Eoq 'Development and testing' README.* || {
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020078
79KITCHEN_LIST=$(kitchen list|tail -n+2)
Petr Michalec36842122016-11-15 23:59:42 +010080README=$(ls README.*|head -n1)
81cat >> ${README} <<-\EOF
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020082
83 Development and testing
84 =======================
85
86 Development and test workflow with `Test Kitchen <http://kitchen.ci>`_ and
87 `kitchen-salt <https://github.com/simonmcc/kitchen-salt>`_ provisioner plugin.
88
89 Test Kitchen is a test harness tool to execute your configured code on one or more platforms in isolation.
90 There is a ``.kitchen.yml`` in main directory that defines *platforms* to be tested and *suites* to execute on them.
91
92 Kitchen CI can spin instances locally or remote, based on used *driver*.
93 For local development ``.kitchen.yml`` defines a `vagrant <https://github.com/test-kitchen/kitchen-vagrant>`_ or
94 `docker <https://github.com/test-kitchen/kitchen-docker>`_ driver.
95
96 To use backend drivers or implement your CI follow the section `INTEGRATION.rst#Continuous Integration`__.
97
98 A listing of scenarios to be executed:
99
100 .. code-block:: shell
101
102 $ kitchen list
103
104 Instance Driver Provisioner Verifier Transport Last Action
105
106EOF
107
108echo "$KITCHEN_LIST" | sed 's/^/ /' >> README.*
109
110cat >> README.* <<-\EOF
111
112 The `Busser <https://github.com/test-kitchen/busser>`_ *Verifier* is used to setup and run tests
113 implementated in `<repo>/test/integration`. It installs the particular driver to tested instance
114 (`Serverspec <https://github.com/neillturner/kitchen-verifier-serverspec>`_,
115 `InSpec <https://github.com/chef/kitchen-inspec>`_, Shell, Bats, ...) prior the verification is executed.
116
117
118 Usage:
119
120 .. code-block:: shell
121
Petr Michalec993e3fa2016-07-15 15:56:34 +0200122 # list instances and status
123 kitchen list
Petr Michalecd1ff7bd2016-07-14 10:43:13 +0200124
Petr Michalec993e3fa2016-07-15 15:56:34 +0200125 # manually execute integration tests
126 kitchen [test || [create|converge|verify|exec|login|destroy|...]] [instance] -t tests/integration
127
128 # use with provided Makefile (ie: within CI pipeline)
129 make kitchen
Petr Michalecd1ff7bd2016-07-14 10:43:13 +0200130
131EOF
Petr Michalec993e3fa2016-07-15 15:56:34 +0200132}
Petr Michalecd1ff7bd2016-07-14 10:43:13 +0200133
Petr Michalec993e3fa2016-07-15 15:56:34 +0200134test -e INTEGRATION.rst || \
Alena Holanova5364af32016-07-19 13:42:55 +0200135curl -skL "${SOURCE_REPO_URI}/INTEGRATION.rst" -o INTEGRATION.rst
Petr Michalec993e3fa2016-07-15 15:56:34 +0200136
137
138# ADD CHANGES TO GIT
139###################################
140
141# update Makefile, but do not auto-add to git
Alena Holanova5364af32016-07-19 13:42:55 +0200142curl -skL "${SOURCE_REPO_URI}/Makefile" -o Makefile
Petr Michalec993e3fa2016-07-15 15:56:34 +0200143
144git add \
145 .gitignore \
146 .kitchen*yml \
147 INTEGRATION.rst \
148 README.rst
149
Petr Michalec36842122016-11-15 23:59:42 +0100150git status