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 | |
Alena Holanova | 1433eb8 | 2016-07-19 13:37:26 +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 | 5364af3 | 2016-07-19 13:42:55 +0200 | [diff] [blame^] | 47 | envtpl < <(curl -skL "${SOURCE_REPO_URI}/.kitchen.yaml" -- | sed 's/cookiecutter\.kitchen_//g') > .kitchen.yml |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 48 | |
| 49 | [[ "$DRIVER" != "docker" ]] && { |
| 50 | test -e .kitchen.docker.yml || \ |
Alena Holanova | 5364af3 | 2016-07-19 13:42:55 +0200 | [diff] [blame^] | 51 | envtpl < <(curl -skL "${SOURCE_REPO_URI}/.kitchen.yaml" -- | sed 's/cookiecutter\.kitchen_//g' | head -n12 ) > .kitchen.docker.yml |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 52 | } |
| 53 | |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 54 | test -e .kitchen.openstack.yml || \ |
Alena Holanova | 5364af3 | 2016-07-19 13:42:55 +0200 | [diff] [blame^] | 55 | envtpl < <(curl -skL "${SOURCE_REPO_URI}/.kitchen.openstack.yaml" -- | sed 's/cookiecutter\.kitchen_//g') > .kitchen.openstack.yml |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 56 | |
| 57 | |
| 58 | |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 59 | # UPDATE README, etc... |
| 60 | ################################### |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 61 | |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 62 | grep -Eoq 'Development and testing' README.* || { |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 63 | |
| 64 | KITCHEN_LIST=$(kitchen list|tail -n+2) |
| 65 | cat >> 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 | |
| 90 | EOF |
| 91 | |
| 92 | echo "$KITCHEN_LIST" | sed 's/^/ /' >> README.* |
| 93 | |
| 94 | cat >> 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 Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 106 | # list instances and status |
| 107 | kitchen list |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 108 | |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 109 | # 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 Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 114 | |
| 115 | EOF |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 116 | } |
Petr Michalec | d1ff7bd | 2016-07-14 10:43:13 +0200 | [diff] [blame] | 117 | |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 118 | test -e INTEGRATION.rst || \ |
Alena Holanova | 5364af3 | 2016-07-19 13:42:55 +0200 | [diff] [blame^] | 119 | curl -skL "${SOURCE_REPO_URI}/INTEGRATION.rst" -o INTEGRATION.rst |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 120 | |
| 121 | |
| 122 | # ADD CHANGES TO GIT |
| 123 | ################################### |
| 124 | |
| 125 | # update Makefile, but do not auto-add to git |
Alena Holanova | 5364af3 | 2016-07-19 13:42:55 +0200 | [diff] [blame^] | 126 | curl -skL "${SOURCE_REPO_URI}/Makefile" -o Makefile |
Petr Michalec | 993e3fa | 2016-07-15 15:56:34 +0200 | [diff] [blame] | 127 | |
| 128 | git add \ |
| 129 | .gitignore \ |
| 130 | .kitchen*yml \ |
| 131 | INTEGRATION.rst \ |
| 132 | README.rst |
| 133 | |
Alena Holanova | 1433eb8 | 2016-07-19 13:37:26 +0200 | [diff] [blame] | 134 | git status |