blob: 26c52196259a29a516d7c17fd97d2cbb7696dc2e [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:
Alena Holanova5364af32016-07-19 13:42:55 +02005# curl -skL "https://git.tcpcloud.eu/cookiecutter-templates/cookiecutter-salt-formula/raw/master/kitchen-init.sh" | bash -s --
Petr Michalecd1ff7bd2016-07-14 10:43:13 +02006
7
8# CONFIG
Petr Michalec993e3fa2016-07-15 15:56:34 +02009###################################
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020010
Petr Michalec993e3fa2016-07-15 15:56:34 +020011export driver=${driver:-vagrant} # vagrant, dokken, openstack, ...
12export verifier=${verifier:-inspec} # serverspec, pester
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020013
Petr Michaleca347c802016-07-20 21:49:52 +020014export formula=${formula:-$(awk -F: '/^name/{gsub(/[\ \"]/,"");print $2}' metadata.yml)}
Petr Michalec60b1b212016-07-26 07:37:39 +020015export suites=$(ls tests/pillar|xargs -I{} basename {} .sls)
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020016
Petr Michalec993e3fa2016-07-15 15:56:34 +020017export SOURCE_REPO_URI="https://git.tcpcloud.eu/cookiecutter-templates/cookiecutter-salt-formula/raw/master/%7B%7Bcookiecutter.project_name%7D%7D"
18
Petr Michalec1eac0ef2016-07-26 20:56:55 +020019which envtpl &> /dev/null || {
20 echo "ERROR: missing prerequisite, install 'envtpl' first : pip install envtpl"
21 exit 1
22}
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020023
24# INIT
Petr Michalec993e3fa2016-07-15 15:56:34 +020025###################################
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020026test ! -e .kitchen.yml || {
Petr Michalec1eac0ef2016-07-26 20:56:55 +020027 kitchen init -D kitchen-vagrant -P kitchen-salt --no-create-gemfile
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020028 echo .kitchen >> .gitignore
29 rm -rf test
30 rm -f .kitchen.yml
31 rm -f chefignore
32}
33
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020034
Petr Michalec993e3fa2016-07-15 15:56:34 +020035# CONFIGURE & SCAFFOLD TEST DIR
36###################################
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020037test -d tests/integration || {
38 for suite in $SUITES; do
39 mkdir -p tests/integration/$suite/$VERIFIER
40 done
41 mkdir -p tests/integration/helpers/$VERIFIER/
42 touch $_/spec_helper.rb
43}
44
45
46# .KITCHEN.YML
Petr Michalec993e3fa2016-07-15 15:56:34 +020047###################################
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020048
Petr Michalec993e3fa2016-07-15 15:56:34 +020049test -e .kitchen.yml || \
Alena Holanova58406c12016-07-19 13:49:41 +020050envtpl < <(curl -skL "${SOURCE_REPO_URI}/.kitchen.yml" -- | sed 's/cookiecutter\.kitchen_//g') > .kitchen.yml
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020051
Petr Michalec221b2322016-07-26 07:33:42 +020052[[ "$driver" != "docker" ]] && {
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020053 test -e .kitchen.docker.yml || \
Petr Michalec1eac0ef2016-07-26 20:56:55 +020054 envtpl < <(curl -skL "${SOURCE_REPO_URI}/.kitchen.docker.yml" -- | sed 's/cookiecutter\.kitchen_//g' ) > .kitchen.docker.yml
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020055}
56
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020057test -e .kitchen.openstack.yml || \
Alena Holanova58406c12016-07-19 13:49:41 +020058envtpl < <(curl -skL "${SOURCE_REPO_URI}/.kitchen.openstack.yml" -- | sed 's/cookiecutter\.kitchen_//g') > .kitchen.openstack.yml
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020059
60
61
Petr Michalec993e3fa2016-07-15 15:56:34 +020062# UPDATE README, etc...
63###################################
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020064
Petr Michalec993e3fa2016-07-15 15:56:34 +020065grep -Eoq 'Development and testing' README.* || {
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020066
67KITCHEN_LIST=$(kitchen list|tail -n+2)
68cat >> README.* <<-\EOF
69
70 Development and testing
71 =======================
72
73 Development and test workflow with `Test Kitchen <http://kitchen.ci>`_ and
74 `kitchen-salt <https://github.com/simonmcc/kitchen-salt>`_ provisioner plugin.
75
76 Test Kitchen is a test harness tool to execute your configured code on one or more platforms in isolation.
77 There is a ``.kitchen.yml`` in main directory that defines *platforms* to be tested and *suites* to execute on them.
78
79 Kitchen CI can spin instances locally or remote, based on used *driver*.
80 For local development ``.kitchen.yml`` defines a `vagrant <https://github.com/test-kitchen/kitchen-vagrant>`_ or
81 `docker <https://github.com/test-kitchen/kitchen-docker>`_ driver.
82
83 To use backend drivers or implement your CI follow the section `INTEGRATION.rst#Continuous Integration`__.
84
85 A listing of scenarios to be executed:
86
87 .. code-block:: shell
88
89 $ kitchen list
90
91 Instance Driver Provisioner Verifier Transport Last Action
92
93EOF
94
95echo "$KITCHEN_LIST" | sed 's/^/ /' >> README.*
96
97cat >> README.* <<-\EOF
98
99 The `Busser <https://github.com/test-kitchen/busser>`_ *Verifier* is used to setup and run tests
100 implementated in `<repo>/test/integration`. It installs the particular driver to tested instance
101 (`Serverspec <https://github.com/neillturner/kitchen-verifier-serverspec>`_,
102 `InSpec <https://github.com/chef/kitchen-inspec>`_, Shell, Bats, ...) prior the verification is executed.
103
104
105 Usage:
106
107 .. code-block:: shell
108
Petr Michalec993e3fa2016-07-15 15:56:34 +0200109 # list instances and status
110 kitchen list
Petr Michalecd1ff7bd2016-07-14 10:43:13 +0200111
Petr Michalec993e3fa2016-07-15 15:56:34 +0200112 # manually execute integration tests
113 kitchen [test || [create|converge|verify|exec|login|destroy|...]] [instance] -t tests/integration
114
115 # use with provided Makefile (ie: within CI pipeline)
116 make kitchen
Petr Michalecd1ff7bd2016-07-14 10:43:13 +0200117
118EOF
Petr Michalec993e3fa2016-07-15 15:56:34 +0200119}
Petr Michalecd1ff7bd2016-07-14 10:43:13 +0200120
Petr Michalec993e3fa2016-07-15 15:56:34 +0200121test -e INTEGRATION.rst || \
Alena Holanova5364af32016-07-19 13:42:55 +0200122curl -skL "${SOURCE_REPO_URI}/INTEGRATION.rst" -o INTEGRATION.rst
Petr Michalec993e3fa2016-07-15 15:56:34 +0200123
124
125# ADD CHANGES TO GIT
126###################################
127
128# update Makefile, but do not auto-add to git
Alena Holanova5364af32016-07-19 13:42:55 +0200129curl -skL "${SOURCE_REPO_URI}/Makefile" -o Makefile
Petr Michalec993e3fa2016-07-15 15:56:34 +0200130
131git add \
132 .gitignore \
133 .kitchen*yml \
134 INTEGRATION.rst \
135 README.rst
136
Petr Michaleca347c802016-07-20 21:49:52 +0200137git status