blob: b5737ba2afe1ae70f18b9485c204539cffbb6caf [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 Michalec993e3fa2016-07-15 15:56:34 +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
19which envtpl &> /dev/null|| pip3 install envtpl
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020020
21# INIT
Petr Michalec993e3fa2016-07-15 15:56:34 +020022###################################
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020023test ! -e .kitchen.yml || {
24 kitchen init -D kitchen-docker -P kitchen-salt --no-create-gemfile
25 echo .kitchen >> .gitignore
26 rm -rf test
27 rm -f .kitchen.yml
28 rm -f chefignore
29}
30
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020031
Petr Michalec993e3fa2016-07-15 15:56:34 +020032# CONFIGURE & SCAFFOLD TEST DIR
33###################################
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020034test -d tests/integration || {
35 for suite in $SUITES; do
36 mkdir -p tests/integration/$suite/$VERIFIER
37 done
38 mkdir -p tests/integration/helpers/$VERIFIER/
39 touch $_/spec_helper.rb
40}
41
42
43# .KITCHEN.YML
Petr Michalec993e3fa2016-07-15 15:56:34 +020044###################################
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020045
Petr Michalec993e3fa2016-07-15 15:56:34 +020046test -e .kitchen.yml || \
Alena Holanova58406c12016-07-19 13:49:41 +020047envtpl < <(curl -skL "${SOURCE_REPO_URI}/.kitchen.yml" -- | sed 's/cookiecutter\.kitchen_//g') > .kitchen.yml
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020048
Petr Michalec221b2322016-07-26 07:33:42 +020049[[ "$driver" != "docker" ]] && {
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020050 test -e .kitchen.docker.yml || \
Petr Michalec221b2322016-07-26 07:33:42 +020051 driver=docker \
52 envtpl < <(curl -skL "${SOURCE_REPO_URI}/.kitchen.yml" -- | sed 's/cookiecutter\.kitchen_//g' | head -n12 ) > .kitchen.docker.yml
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020053}
54
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020055test -e .kitchen.openstack.yml || \
Alena Holanova58406c12016-07-19 13:49:41 +020056envtpl < <(curl -skL "${SOURCE_REPO_URI}/.kitchen.openstack.yml" -- | sed 's/cookiecutter\.kitchen_//g') > .kitchen.openstack.yml
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020057
58
59
Petr Michalec993e3fa2016-07-15 15:56:34 +020060# UPDATE README, etc...
61###################################
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020062
Petr Michalec993e3fa2016-07-15 15:56:34 +020063grep -Eoq 'Development and testing' README.* || {
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020064
65KITCHEN_LIST=$(kitchen list|tail -n+2)
66cat >> README.* <<-\EOF
67
68 Development and testing
69 =======================
70
71 Development and test workflow with `Test Kitchen <http://kitchen.ci>`_ and
72 `kitchen-salt <https://github.com/simonmcc/kitchen-salt>`_ provisioner plugin.
73
74 Test Kitchen is a test harness tool to execute your configured code on one or more platforms in isolation.
75 There is a ``.kitchen.yml`` in main directory that defines *platforms* to be tested and *suites* to execute on them.
76
77 Kitchen CI can spin instances locally or remote, based on used *driver*.
78 For local development ``.kitchen.yml`` defines a `vagrant <https://github.com/test-kitchen/kitchen-vagrant>`_ or
79 `docker <https://github.com/test-kitchen/kitchen-docker>`_ driver.
80
81 To use backend drivers or implement your CI follow the section `INTEGRATION.rst#Continuous Integration`__.
82
83 A listing of scenarios to be executed:
84
85 .. code-block:: shell
86
87 $ kitchen list
88
89 Instance Driver Provisioner Verifier Transport Last Action
90
91EOF
92
93echo "$KITCHEN_LIST" | sed 's/^/ /' >> README.*
94
95cat >> README.* <<-\EOF
96
97 The `Busser <https://github.com/test-kitchen/busser>`_ *Verifier* is used to setup and run tests
98 implementated in `<repo>/test/integration`. It installs the particular driver to tested instance
99 (`Serverspec <https://github.com/neillturner/kitchen-verifier-serverspec>`_,
100 `InSpec <https://github.com/chef/kitchen-inspec>`_, Shell, Bats, ...) prior the verification is executed.
101
102
103 Usage:
104
105 .. code-block:: shell
106
Petr Michalec993e3fa2016-07-15 15:56:34 +0200107 # list instances and status
108 kitchen list
Petr Michalecd1ff7bd2016-07-14 10:43:13 +0200109
Petr Michalec993e3fa2016-07-15 15:56:34 +0200110 # manually execute integration tests
111 kitchen [test || [create|converge|verify|exec|login|destroy|...]] [instance] -t tests/integration
112
113 # use with provided Makefile (ie: within CI pipeline)
114 make kitchen
Petr Michalecd1ff7bd2016-07-14 10:43:13 +0200115
116EOF
Petr Michalec993e3fa2016-07-15 15:56:34 +0200117}
Petr Michalecd1ff7bd2016-07-14 10:43:13 +0200118
Petr Michalec993e3fa2016-07-15 15:56:34 +0200119test -e INTEGRATION.rst || \
Alena Holanova5364af32016-07-19 13:42:55 +0200120curl -skL "${SOURCE_REPO_URI}/INTEGRATION.rst" -o INTEGRATION.rst
Petr Michalec993e3fa2016-07-15 15:56:34 +0200121
122
123# ADD CHANGES TO GIT
124###################################
125
126# update Makefile, but do not auto-add to git
Alena Holanova5364af32016-07-19 13:42:55 +0200127curl -skL "${SOURCE_REPO_URI}/Makefile" -o Makefile
Petr Michalec993e3fa2016-07-15 15:56:34 +0200128
129git add \
130 .gitignore \
131 .kitchen*yml \
132 INTEGRATION.rst \
133 README.rst
134
Petr Michaleca347c802016-07-20 21:49:52 +0200135git status