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 | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 13 | export driver=${driver:-vagrant} # vagrant, dokken, openstack, ... |
| 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 || { |
| 22 | echo "ERROR: missing prerequisite, install 'envtpl' first : pip install envtpl" |
| 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 | 1eac0ef | 2016-07-26 20:56:55 +0200 | [diff] [blame] | 29 | kitchen init -D kitchen-vagrant -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 | 59566fb | 2016-07-26 22:39:50 +0200 | [diff] [blame] | 40 | for suite in $(echo $suites|xargs); do |
Petr Michalec | 3e89e1b | 2016-07-26 21:43:17 +0200 | [diff] [blame] | 41 | mkdir -p tests/integration/$suite/$verifier |
| 42 | touch tests/integration/$suite/$verifier/default_spec.rb |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 43 | done |
Petr Michalec | 3e89e1b | 2016-07-26 21:43:17 +0200 | [diff] [blame] | 44 | mkdir -p tests/integration/helpers/$verifier/ |
Petr Michalec | 59566fb | 2016-07-26 22:39:50 +0200 | [diff] [blame] | 45 | touch tests/integration/helpers/$verifier/spec_helper.rb |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | |
| 49 | # .KITCHEN.YML |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 50 | ################################### |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 51 | |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 52 | test -e .kitchen.yml || \ |
Alena Holanova | 58406c1 | 2016-07-19 13:49:41 +0200 | [diff] [blame] | 53 | envtpl < <(curl -skL "${SOURCE_REPO_URI}/.kitchen.yml" -- | sed 's/cookiecutter\.kitchen_//g') > .kitchen.yml |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 54 | |
Petr Michalec | 221b232 | 2016-07-26 07:33:42 +0200 | [diff] [blame] | 55 | [[ "$driver" != "docker" ]] && { |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 56 | test -e .kitchen.docker.yml || \ |
Petr Michalec | 1eac0ef | 2016-07-26 20:56:55 +0200 | [diff] [blame] | 57 | 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] | 58 | } |
| 59 | |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 60 | test -e .kitchen.openstack.yml || \ |
Alena Holanova | 58406c1 | 2016-07-19 13:49:41 +0200 | [diff] [blame] | 61 | envtpl < <(curl -skL "${SOURCE_REPO_URI}/.kitchen.openstack.yml" -- | sed 's/cookiecutter\.kitchen_//g') > .kitchen.openstack.yml |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 62 | |
| 63 | |
| 64 | |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 65 | # UPDATE README, etc... |
| 66 | ################################### |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 67 | |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 68 | grep -Eoq 'Development and testing' README.* || { |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 69 | |
| 70 | KITCHEN_LIST=$(kitchen list|tail -n+2) |
| 71 | cat >> README.* <<-\EOF |
| 72 | |
| 73 | Development and testing |
| 74 | ======================= |
| 75 | |
| 76 | Development and test workflow with `Test Kitchen <http://kitchen.ci>`_ and |
| 77 | `kitchen-salt <https://github.com/simonmcc/kitchen-salt>`_ provisioner plugin. |
| 78 | |
| 79 | Test Kitchen is a test harness tool to execute your configured code on one or more platforms in isolation. |
| 80 | There is a ``.kitchen.yml`` in main directory that defines *platforms* to be tested and *suites* to execute on them. |
| 81 | |
| 82 | Kitchen CI can spin instances locally or remote, based on used *driver*. |
| 83 | For local development ``.kitchen.yml`` defines a `vagrant <https://github.com/test-kitchen/kitchen-vagrant>`_ or |
| 84 | `docker <https://github.com/test-kitchen/kitchen-docker>`_ driver. |
| 85 | |
| 86 | To use backend drivers or implement your CI follow the section `INTEGRATION.rst#Continuous Integration`__. |
| 87 | |
| 88 | A listing of scenarios to be executed: |
| 89 | |
| 90 | .. code-block:: shell |
| 91 | |
| 92 | $ kitchen list |
| 93 | |
| 94 | Instance Driver Provisioner Verifier Transport Last Action |
| 95 | |
| 96 | EOF |
| 97 | |
| 98 | echo "$KITCHEN_LIST" | sed 's/^/ /' >> README.* |
| 99 | |
| 100 | cat >> README.* <<-\EOF |
| 101 | |
| 102 | The `Busser <https://github.com/test-kitchen/busser>`_ *Verifier* is used to setup and run tests |
| 103 | implementated in `<repo>/test/integration`. It installs the particular driver to tested instance |
| 104 | (`Serverspec <https://github.com/neillturner/kitchen-verifier-serverspec>`_, |
| 105 | `InSpec <https://github.com/chef/kitchen-inspec>`_, Shell, Bats, ...) prior the verification is executed. |
| 106 | |
| 107 | |
| 108 | Usage: |
| 109 | |
| 110 | .. code-block:: shell |
| 111 | |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 112 | # list instances and status |
| 113 | kitchen list |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 114 | |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 115 | # manually execute integration tests |
| 116 | kitchen [test || [create|converge|verify|exec|login|destroy|...]] [instance] -t tests/integration |
| 117 | |
| 118 | # use with provided Makefile (ie: within CI pipeline) |
| 119 | make kitchen |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 120 | |
| 121 | EOF |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 122 | } |
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 | test -e INTEGRATION.rst || \ |
Alena Holanova | 5364af3 | 2016-07-19 13:42:55 +0200 | [diff] [blame] | 125 | curl -skL "${SOURCE_REPO_URI}/INTEGRATION.rst" -o INTEGRATION.rst |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 126 | |
| 127 | |
| 128 | # ADD CHANGES TO GIT |
| 129 | ################################### |
| 130 | |
| 131 | # update Makefile, but do not auto-add to git |
Alena Holanova | 5364af3 | 2016-07-19 13:42:55 +0200 | [diff] [blame] | 132 | curl -skL "${SOURCE_REPO_URI}/Makefile" -o Makefile |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 133 | |
| 134 | git add \ |
| 135 | .gitignore \ |
| 136 | .kitchen*yml \ |
| 137 | INTEGRATION.rst \ |
| 138 | README.rst |
| 139 | |
Petr Michalec | a7af122 | 2016-09-23 09:47:24 +0200 | [diff] [blame^] | 140 | git status |