Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 3 | # Script to add Kitchen configuration to existing formulas. |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 4 | # usage: |
Petr Michalec | 71b9c88 | 2016-09-23 10:32:38 +0200 | [diff] [blame] | 5 | # curl -skL "https://raw.githubusercontent.com/tcpcloud/cookiecutter-salt-formula/master/kitchen-init.sh" | bash -s -- |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 6 | |
Petr Michalec | a7af122 | 2016-09-23 09:47:24 +0200 | [diff] [blame] | 7 | # source gist: |
| 8 | # https://gist.github.com/epcim/b0368794e69e6807635b0c7268e5ceec |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 9 | |
| 10 | # CONFIG |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 11 | ################################### |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 12 | |
Petr Michalec | 3f949f6 | 2016-11-15 23:21:56 +0100 | [diff] [blame^] | 13 | export driver=${driver:-docker} # vagrant, dokken, openstack, ... |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 14 | export verifier=${verifier:-inspec} # serverspec, pester |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 15 | |
Petr Michalec | a347c80 | 2016-07-20 21:49:52 +0200 | [diff] [blame] | 16 | export formula=${formula:-$(awk -F: '/^name/{gsub(/[\ \"]/,"");print $2}' metadata.yml)} |
Petr Michalec | 60b1b21 | 2016-07-26 07:37:39 +0200 | [diff] [blame] | 17 | export suites=$(ls tests/pillar|xargs -I{} basename {} .sls) |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 18 | |
Petr Michalec | 71b9c88 | 2016-09-23 10:32:38 +0200 | [diff] [blame] | 19 | export SOURCE_REPO_URI="https://raw.githubusercontent.com/tcpcloud/cookiecutter-salt-formula/master/%7B%7Bcookiecutter.project_name%7D%7D" |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 20 | |
Petr Michalec | 1eac0ef | 2016-07-26 20:56:55 +0200 | [diff] [blame] | 21 | which envtpl &> /dev/null || { |
Petr Michalec | 3f949f6 | 2016-11-15 23:21:56 +0100 | [diff] [blame^] | 22 | echo "ERROR: missing prerequisite, install 'envtpl' first : sudo pip install envtpl" |
Petr Michalec | 1eac0ef | 2016-07-26 20:56:55 +0200 | [diff] [blame] | 23 | exit 1 |
| 24 | } |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 25 | |
| 26 | # INIT |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 27 | ################################### |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 28 | test ! -e .kitchen.yml || { |
Petr Michalec | 3f949f6 | 2016-11-15 23:21:56 +0100 | [diff] [blame^] | 29 | kitchen init -D kitchen-docker -P kitchen-salt --no-create-gemfile |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 30 | echo .kitchen >> .gitignore |
| 31 | rm -rf test |
| 32 | rm -f .kitchen.yml |
| 33 | rm -f chefignore |
| 34 | } |
| 35 | |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 36 | |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 37 | # CONFIGURE & SCAFFOLD TEST DIR |
| 38 | ################################### |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 39 | test -d tests/integration || { |
Petr Michalec | 3f949f6 | 2016-11-15 23:21:56 +0100 | [diff] [blame^] | 40 | mkdir -p tests/integration |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 41 | } |
Petr Michalec | 3f949f6 | 2016-11-15 23:21:56 +0100 | [diff] [blame^] | 42 | # 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 Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 49 | |
| 50 | |
| 51 | # .KITCHEN.YML |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 52 | ################################### |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 53 | |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 54 | test -e .kitchen.yml || \ |
Petr Michalec | 3f949f6 | 2016-11-15 23:21:56 +0100 | [diff] [blame^] | 55 | envtpl < <(curl -skL "${SOURCE_REPO_URI}/.kitchen.${driver}.yml" -- | sed 's/cookiecutter\.kitchen_//g' ) > .kitchen.yml |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 56 | |
Petr Michalec | 221b232 | 2016-07-26 07:33:42 +0200 | [diff] [blame] | 57 | [[ "$driver" != "docker" ]] && { |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 58 | test -e .kitchen.docker.yml || \ |
Petr Michalec | 3f949f6 | 2016-11-15 23:21:56 +0100 | [diff] [blame^] | 59 | envtpl < <(curl -skL "${SOURCE_REPO_URI}/.kitchen.docker.yml" -- | sed 's/cookiecutter\.kitchen_//g') > .kitchen.docker.yml |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 60 | } |
| 61 | |
Petr Michalec | 3f949f6 | 2016-11-15 23:21:56 +0100 | [diff] [blame^] | 62 | [[ "$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 Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 71 | |
| 72 | |
| 73 | |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 74 | # UPDATE README, etc... |
| 75 | ################################### |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 76 | |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 77 | grep -Eoq 'Development and testing' README.* || { |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 78 | |
| 79 | KITCHEN_LIST=$(kitchen list|tail -n+2) |
| 80 | cat >> README.* <<-\EOF |
| 81 | |
| 82 | Development and testing |
| 83 | ======================= |
| 84 | |
| 85 | Development and test workflow with `Test Kitchen <http://kitchen.ci>`_ and |
| 86 | `kitchen-salt <https://github.com/simonmcc/kitchen-salt>`_ provisioner plugin. |
| 87 | |
| 88 | Test Kitchen is a test harness tool to execute your configured code on one or more platforms in isolation. |
| 89 | There is a ``.kitchen.yml`` in main directory that defines *platforms* to be tested and *suites* to execute on them. |
| 90 | |
| 91 | Kitchen CI can spin instances locally or remote, based on used *driver*. |
| 92 | For local development ``.kitchen.yml`` defines a `vagrant <https://github.com/test-kitchen/kitchen-vagrant>`_ or |
| 93 | `docker <https://github.com/test-kitchen/kitchen-docker>`_ driver. |
| 94 | |
| 95 | To use backend drivers or implement your CI follow the section `INTEGRATION.rst#Continuous Integration`__. |
| 96 | |
| 97 | A listing of scenarios to be executed: |
| 98 | |
| 99 | .. code-block:: shell |
| 100 | |
| 101 | $ kitchen list |
| 102 | |
| 103 | Instance Driver Provisioner Verifier Transport Last Action |
| 104 | |
| 105 | EOF |
| 106 | |
| 107 | echo "$KITCHEN_LIST" | sed 's/^/ /' >> README.* |
| 108 | |
| 109 | cat >> README.* <<-\EOF |
| 110 | |
| 111 | The `Busser <https://github.com/test-kitchen/busser>`_ *Verifier* is used to setup and run tests |
| 112 | implementated in `<repo>/test/integration`. It installs the particular driver to tested instance |
| 113 | (`Serverspec <https://github.com/neillturner/kitchen-verifier-serverspec>`_, |
| 114 | `InSpec <https://github.com/chef/kitchen-inspec>`_, Shell, Bats, ...) prior the verification is executed. |
| 115 | |
| 116 | |
| 117 | Usage: |
| 118 | |
| 119 | .. code-block:: shell |
| 120 | |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 121 | # list instances and status |
| 122 | kitchen list |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 123 | |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 124 | # manually execute integration tests |
| 125 | kitchen [test || [create|converge|verify|exec|login|destroy|...]] [instance] -t tests/integration |
| 126 | |
| 127 | # use with provided Makefile (ie: within CI pipeline) |
| 128 | make kitchen |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 129 | |
| 130 | EOF |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 131 | } |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 132 | |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 133 | test -e INTEGRATION.rst || \ |
Alena Holanova | 5364af3 | 2016-07-19 13:42:55 +0200 | [diff] [blame] | 134 | curl -skL "${SOURCE_REPO_URI}/INTEGRATION.rst" -o INTEGRATION.rst |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 135 | |
| 136 | |
| 137 | # ADD CHANGES TO GIT |
| 138 | ################################### |
| 139 | |
| 140 | # update Makefile, but do not auto-add to git |
Alena Holanova | 5364af3 | 2016-07-19 13:42:55 +0200 | [diff] [blame] | 141 | curl -skL "${SOURCE_REPO_URI}/Makefile" -o Makefile |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 142 | |
| 143 | git add \ |
| 144 | .gitignore \ |
| 145 | .kitchen*yml \ |
| 146 | INTEGRATION.rst \ |
| 147 | README.rst |
| 148 | |
Petr Michalec | a7af122 | 2016-09-23 09:47:24 +0200 | [diff] [blame] | 149 | git status |