blob: e8e241d14543bb466fd4fbc52a2a2a46fcb269e7 [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
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 Michalec71b9c882016-09-23 10:32:38 +020017export 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 +020018
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 || {
Petr Michalec59566fb2016-07-26 22:39:50 +020038 for suite in $(echo $suites|xargs); do
Petr Michalec3e89e1b2016-07-26 21:43:17 +020039 mkdir -p tests/integration/$suite/$verifier
40 touch tests/integration/$suite/$verifier/default_spec.rb
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020041 done
Petr Michalec3e89e1b2016-07-26 21:43:17 +020042 mkdir -p tests/integration/helpers/$verifier/
Petr Michalec59566fb2016-07-26 22:39:50 +020043 touch tests/integration/helpers/$verifier/spec_helper.rb
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020044}
45
46
47# .KITCHEN.YML
Petr Michalec993e3fa2016-07-15 15:56:34 +020048###################################
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020049
Petr Michalec993e3fa2016-07-15 15:56:34 +020050test -e .kitchen.yml || \
Alena Holanova58406c12016-07-19 13:49:41 +020051envtpl < <(curl -skL "${SOURCE_REPO_URI}/.kitchen.yml" -- | sed 's/cookiecutter\.kitchen_//g') > .kitchen.yml
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020052
Petr Michalec221b2322016-07-26 07:33:42 +020053[[ "$driver" != "docker" ]] && {
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020054 test -e .kitchen.docker.yml || \
Petr Michalec1eac0ef2016-07-26 20:56:55 +020055 envtpl < <(curl -skL "${SOURCE_REPO_URI}/.kitchen.docker.yml" -- | sed 's/cookiecutter\.kitchen_//g' ) > .kitchen.docker.yml
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020056}
57
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020058test -e .kitchen.openstack.yml || \
Alena Holanova58406c12016-07-19 13:49:41 +020059envtpl < <(curl -skL "${SOURCE_REPO_URI}/.kitchen.openstack.yml" -- | sed 's/cookiecutter\.kitchen_//g') > .kitchen.openstack.yml
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020060
61
62
Petr Michalec993e3fa2016-07-15 15:56:34 +020063# UPDATE README, etc...
64###################################
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020065
Petr Michalec993e3fa2016-07-15 15:56:34 +020066grep -Eoq 'Development and testing' README.* || {
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020067
68KITCHEN_LIST=$(kitchen list|tail -n+2)
69cat >> README.* <<-\EOF
70
71 Development and testing
72 =======================
73
74 Development and test workflow with `Test Kitchen <http://kitchen.ci>`_ and
75 `kitchen-salt <https://github.com/simonmcc/kitchen-salt>`_ provisioner plugin.
76
77 Test Kitchen is a test harness tool to execute your configured code on one or more platforms in isolation.
78 There is a ``.kitchen.yml`` in main directory that defines *platforms* to be tested and *suites* to execute on them.
79
80 Kitchen CI can spin instances locally or remote, based on used *driver*.
81 For local development ``.kitchen.yml`` defines a `vagrant <https://github.com/test-kitchen/kitchen-vagrant>`_ or
82 `docker <https://github.com/test-kitchen/kitchen-docker>`_ driver.
83
84 To use backend drivers or implement your CI follow the section `INTEGRATION.rst#Continuous Integration`__.
85
86 A listing of scenarios to be executed:
87
88 .. code-block:: shell
89
90 $ kitchen list
91
92 Instance Driver Provisioner Verifier Transport Last Action
93
94EOF
95
96echo "$KITCHEN_LIST" | sed 's/^/ /' >> README.*
97
98cat >> README.* <<-\EOF
99
100 The `Busser <https://github.com/test-kitchen/busser>`_ *Verifier* is used to setup and run tests
101 implementated in `<repo>/test/integration`. It installs the particular driver to tested instance
102 (`Serverspec <https://github.com/neillturner/kitchen-verifier-serverspec>`_,
103 `InSpec <https://github.com/chef/kitchen-inspec>`_, Shell, Bats, ...) prior the verification is executed.
104
105
106 Usage:
107
108 .. code-block:: shell
109
Petr Michalec993e3fa2016-07-15 15:56:34 +0200110 # list instances and status
111 kitchen list
Petr Michalecd1ff7bd2016-07-14 10:43:13 +0200112
Petr Michalec993e3fa2016-07-15 15:56:34 +0200113 # manually execute integration tests
114 kitchen [test || [create|converge|verify|exec|login|destroy|...]] [instance] -t tests/integration
115
116 # use with provided Makefile (ie: within CI pipeline)
117 make kitchen
Petr Michalecd1ff7bd2016-07-14 10:43:13 +0200118
119EOF
Petr Michalec993e3fa2016-07-15 15:56:34 +0200120}
Petr Michalecd1ff7bd2016-07-14 10:43:13 +0200121
Petr Michalec993e3fa2016-07-15 15:56:34 +0200122test -e INTEGRATION.rst || \
Alena Holanova5364af32016-07-19 13:42:55 +0200123curl -skL "${SOURCE_REPO_URI}/INTEGRATION.rst" -o INTEGRATION.rst
Petr Michalec993e3fa2016-07-15 15:56:34 +0200124
125
126# ADD CHANGES TO GIT
127###################################
128
129# update Makefile, but do not auto-add to git
Alena Holanova5364af32016-07-19 13:42:55 +0200130curl -skL "${SOURCE_REPO_URI}/Makefile" -o Makefile
Petr Michalec993e3fa2016-07-15 15:56:34 +0200131
132git add \
133 .gitignore \
134 .kitchen*yml \
135 INTEGRATION.rst \
136 README.rst
137
Petr Michaleca347c802016-07-20 21:49:52 +0200138git status