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: |
Alena Holanova | 5364af3 | 2016-07-19 13:42:55 +0200 | [diff] [blame] | 5 | # curl -skL "https://git.tcpcloud.eu/cookiecutter-templates/cookiecutter-salt-formula/raw/master/kitchen-init.sh" | bash -s -- |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 6 | |
| 7 | |
| 8 | # CONFIG |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 9 | ################################### |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 10 | |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 11 | export driver=${driver:-vagrant} # vagrant, dokken, openstack, ... |
| 12 | export verifier=${verifier:-inspec} # serverspec, pester |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 13 | |
Petr Michalec | a347c80 | 2016-07-20 21:49:52 +0200 | [diff] [blame] | 14 | export formula=${formula:-$(awk -F: '/^name/{gsub(/[\ \"]/,"");print $2}' metadata.yml)} |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 15 | export suites=$(ls tests/pillar|xargs -i{} basename {} .sls) |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 16 | |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 17 | export SOURCE_REPO_URI="https://git.tcpcloud.eu/cookiecutter-templates/cookiecutter-salt-formula/raw/master/%7B%7Bcookiecutter.project_name%7D%7D" |
| 18 | |
| 19 | which envtpl &> /dev/null|| pip3 install envtpl |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 20 | |
| 21 | # INIT |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 22 | ################################### |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 23 | test ! -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 Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 31 | |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 32 | # CONFIGURE & SCAFFOLD TEST DIR |
| 33 | ################################### |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 34 | test -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 Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 44 | ################################### |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 45 | |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 46 | test -e .kitchen.yml || \ |
Alena Holanova | 58406c1 | 2016-07-19 13:49:41 +0200 | [diff] [blame] | 47 | 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] | 48 | |
Petr Michalec | 221b232 | 2016-07-26 07:33:42 +0200 | [diff] [blame^] | 49 | [[ "$driver" != "docker" ]] && { |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 50 | test -e .kitchen.docker.yml || \ |
Petr Michalec | 221b232 | 2016-07-26 07:33:42 +0200 | [diff] [blame^] | 51 | driver=docker \ |
| 52 | envtpl < <(curl -skL "${SOURCE_REPO_URI}/.kitchen.yml" -- | sed 's/cookiecutter\.kitchen_//g' | head -n12 ) > .kitchen.docker.yml |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 53 | } |
| 54 | |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 55 | test -e .kitchen.openstack.yml || \ |
Alena Holanova | 58406c1 | 2016-07-19 13:49:41 +0200 | [diff] [blame] | 56 | 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] | 57 | |
| 58 | |
| 59 | |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 60 | # UPDATE README, etc... |
| 61 | ################################### |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 62 | |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 63 | grep -Eoq 'Development and testing' README.* || { |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 64 | |
| 65 | KITCHEN_LIST=$(kitchen list|tail -n+2) |
| 66 | cat >> 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 | |
| 91 | EOF |
| 92 | |
| 93 | echo "$KITCHEN_LIST" | sed 's/^/ /' >> README.* |
| 94 | |
| 95 | cat >> 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 Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 107 | # list instances and status |
| 108 | kitchen list |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 109 | |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 110 | # 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 Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 115 | |
| 116 | EOF |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 117 | } |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 118 | |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 119 | test -e INTEGRATION.rst || \ |
Alena Holanova | 5364af3 | 2016-07-19 13:42:55 +0200 | [diff] [blame] | 120 | curl -skL "${SOURCE_REPO_URI}/INTEGRATION.rst" -o INTEGRATION.rst |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 121 | |
| 122 | |
| 123 | # ADD CHANGES TO GIT |
| 124 | ################################### |
| 125 | |
| 126 | # update Makefile, but do not auto-add to git |
Alena Holanova | 5364af3 | 2016-07-19 13:42:55 +0200 | [diff] [blame] | 127 | curl -skL "${SOURCE_REPO_URI}/Makefile" -o Makefile |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 128 | |
| 129 | git add \ |
| 130 | .gitignore \ |
| 131 | .kitchen*yml \ |
| 132 | INTEGRATION.rst \ |
| 133 | README.rst |
| 134 | |
Petr Michalec | a347c80 | 2016-07-20 21:49:52 +0200 | [diff] [blame] | 135 | git status |