blob: c059ca53ad7a8d5dc1e1ada8c9a3fcbb5f73ea53 [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 Michalec993e3fa2016-07-15 15:56:34 +02005# curl -sL "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 Michalec993e3fa2016-07-15 15:56:34 +020014export formula=${formula:-$(awk -f: '/name/{gsub(/[\ \"]/,"");print $2}' metadata.yml)}
15export 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 || \
47envtpl < <(curl -sL "${SOURCE_REPO_URI}/.kitchen.yaml" -- | sed 's/cookiecutter\.kitchen_//g') > .kitchen.yml
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020048
49[[ "$DRIVER" != "docker" ]] && {
50 test -e .kitchen.docker.yml || \
Petr Michalec993e3fa2016-07-15 15:56:34 +020051 envtpl < <(curl -sL "${SOURCE_REPO_URI}/.kitchen.yaml" -- | sed 's/cookiecutter\.kitchen_//g' | head -n12 ) > .kitchen.docker.yml
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020052}
53
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020054test -e .kitchen.openstack.yml || \
Petr Michalec993e3fa2016-07-15 15:56:34 +020055envtpl < <(curl -sL "${SOURCE_REPO_URI}/.kitchen.openstack.yaml" -- | sed 's/cookiecutter\.kitchen_//g') > .kitchen.openstack.yml
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020056
57
58
Petr Michalec993e3fa2016-07-15 15:56:34 +020059# UPDATE README, etc...
60###################################
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020061
Petr Michalec993e3fa2016-07-15 15:56:34 +020062grep -Eoq 'Development and testing' README.* || {
Petr Michalecd1ff7bd2016-07-14 10:43:13 +020063
64KITCHEN_LIST=$(kitchen list|tail -n+2)
65cat >> README.* <<-\EOF
66
67 Development and testing
68 =======================
69
70 Development and test workflow with `Test Kitchen <http://kitchen.ci>`_ and
71 `kitchen-salt <https://github.com/simonmcc/kitchen-salt>`_ provisioner plugin.
72
73 Test Kitchen is a test harness tool to execute your configured code on one or more platforms in isolation.
74 There is a ``.kitchen.yml`` in main directory that defines *platforms* to be tested and *suites* to execute on them.
75
76 Kitchen CI can spin instances locally or remote, based on used *driver*.
77 For local development ``.kitchen.yml`` defines a `vagrant <https://github.com/test-kitchen/kitchen-vagrant>`_ or
78 `docker <https://github.com/test-kitchen/kitchen-docker>`_ driver.
79
80 To use backend drivers or implement your CI follow the section `INTEGRATION.rst#Continuous Integration`__.
81
82 A listing of scenarios to be executed:
83
84 .. code-block:: shell
85
86 $ kitchen list
87
88 Instance Driver Provisioner Verifier Transport Last Action
89
90EOF
91
92echo "$KITCHEN_LIST" | sed 's/^/ /' >> README.*
93
94cat >> README.* <<-\EOF
95
96 The `Busser <https://github.com/test-kitchen/busser>`_ *Verifier* is used to setup and run tests
97 implementated in `<repo>/test/integration`. It installs the particular driver to tested instance
98 (`Serverspec <https://github.com/neillturner/kitchen-verifier-serverspec>`_,
99 `InSpec <https://github.com/chef/kitchen-inspec>`_, Shell, Bats, ...) prior the verification is executed.
100
101
102 Usage:
103
104 .. code-block:: shell
105
Petr Michalec993e3fa2016-07-15 15:56:34 +0200106 # list instances and status
107 kitchen list
Petr Michalecd1ff7bd2016-07-14 10:43:13 +0200108
Petr Michalec993e3fa2016-07-15 15:56:34 +0200109 # manually execute integration tests
110 kitchen [test || [create|converge|verify|exec|login|destroy|...]] [instance] -t tests/integration
111
112 # use with provided Makefile (ie: within CI pipeline)
113 make kitchen
Petr Michalecd1ff7bd2016-07-14 10:43:13 +0200114
115EOF
Petr Michalec993e3fa2016-07-15 15:56:34 +0200116}
Petr Michalecd1ff7bd2016-07-14 10:43:13 +0200117
Petr Michalec993e3fa2016-07-15 15:56:34 +0200118test -e INTEGRATION.rst || \
119curl -sL "${SOURCE_REPO_URI}/INTEGRATION.rst" -o INTEGRATION.rst
120
121
122# ADD CHANGES TO GIT
123###################################
124
125# update Makefile, but do not auto-add to git
126curl -sL "${SOURCE_REPO_URI}/Makefile" -o Makefile
127
128git add \
129 .gitignore \
130 .kitchen*yml \
131 INTEGRATION.rst \
132 README.rst
133
Petr Michalecd1ff7bd2016-07-14 10:43:13 +0200134git status
135