Bartek Grzybowski | 87d77c4 | 2020-01-17 09:29:15 +0100 | [diff] [blame] | 1 | #!/usr/bin/env bash |
Mateusz Pilat | 9c9be6f | 2019-05-06 17:12:36 +0200 | [diff] [blame] | 2 | # COPYRIGHT NOTICE STARTS HERE |
| 3 | # |
Bartek Grzybowski | 87d77c4 | 2020-01-17 09:29:15 +0100 | [diff] [blame] | 4 | # Copyright 2018-2020 © Samsung Electronics Co., Ltd. |
Mateusz Pilat | 9c9be6f | 2019-05-06 17:12:36 +0200 | [diff] [blame] | 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | # |
| 18 | # COPYRIGHT NOTICE ENDS HERE |
Mateusz Pilat | b0f546a | 2019-05-08 13:33:46 +0200 | [diff] [blame] | 19 | ############################################################################### |
| 20 | # This script performs Jenkins Change Verification for ONAP Offline Installer # |
| 21 | # No parameters are expected # |
| 22 | ############################################################################### |
Mateusz Pilat | 9c9be6f | 2019-05-06 17:12:36 +0200 | [diff] [blame] | 23 | |
Mateusz Pilat | b0f546a | 2019-05-08 13:33:46 +0200 | [diff] [blame] | 24 | function prep_ubuntu_16_04_for_molecule() { |
| 25 | sudo killall apt apt-get |
| 26 | sudo apt-get --assume-yes install software-properties-common |
| 27 | sudo add-apt-repository --yes ppa:deadsnakes/ppa |
| 28 | sudo apt update |
| 29 | sudo apt install --assume-yes python3.6 |
| 30 | sudo apt install --assume-yes python3.6-venv |
| 31 | } |
| 32 | |
| 33 | function run_molecule() { |
| 34 | prep_ubuntu_16_04_for_molecule |
| 35 | local roles=("$@") |
| 36 | local MOLECULE_RC |
| 37 | for role in ${roles[@]} |
| 38 | do |
Bartek Grzybowski | 0ce90bf | 2020-01-17 14:35:38 +0100 | [diff] [blame^] | 39 | if [ -f ${role}/molecule/default/molecule.yml ]; then |
Mateusz Pilat | b0f546a | 2019-05-08 13:33:46 +0200 | [diff] [blame] | 40 | ./ansible/test/bin/ci-molecule.sh ${role} |
| 41 | MOLECULE_RC=$? |
| 42 | if [ ${MOLECULE_RC} -ne "0" ]; then FAILED_ROLES+=(${role}); fi |
| 43 | else |
| 44 | echo "[WARNING] ---------- THERE ARE NO TESTS DEFINED FOR ${role} ----------" |
| 45 | fi |
| 46 | done |
| 47 | } |
| 48 | |
| 49 | #######################################################################$ |
| 50 | # MAIN #$ |
| 51 | #######################################################################$ |
| 52 | FAILED_ROLES=() |
Bartek Grzybowski | 87d77c4 | 2020-01-17 09:29:15 +0100 | [diff] [blame] | 53 | ALL_PLAYBOOKS=(`ls -d ansible/test/play-*`) # enumerate all playbook tests for later usage |
Mateusz Pilat | b0f546a | 2019-05-08 13:33:46 +0200 | [diff] [blame] | 54 | |
Bartek Grzybowski | 87d77c4 | 2020-01-17 09:29:15 +0100 | [diff] [blame] | 55 | # Check for changes in Ansible roles |
| 56 | ROLE_CHANGES=(`git diff HEAD^ HEAD --name-only ansible/roles | cut -f 1-3 -d "/" | sort -u`) |
Mateusz Pilat | b0f546a | 2019-05-08 13:33:46 +0200 | [diff] [blame] | 57 | if [ -z "${ROLE_CHANGES}" ]; then |
| 58 | echo "NO ANSIBLE ROLE TESTS REQUIRED" |
| 59 | else |
| 60 | run_molecule "${ROLE_CHANGES[@]}" |
| 61 | fi |
| 62 | |
Bartek Grzybowski | 87d77c4 | 2020-01-17 09:29:15 +0100 | [diff] [blame] | 63 | # Check for changes in Molecule tests |
| 64 | if ! $(git diff HEAD^ HEAD --exit-code --quiet ansible/test); then |
| 65 | # If there are any changes in ansible/test area |
| 66 | MOLECULE_CHANGES=(`git diff HEAD^ HEAD --name-only ansible/test | grep -v "ansible/test/play-.*/"`) |
| 67 | if [ ${#MOLECULE_CHANGES[@]} -gt 0 ]; then |
| 68 | # If detected changes that affect all playbook tests - run all |
| 69 | run_molecule "${ALL_PLAYBOOKS[@]}" |
| 70 | # memorize already tested playbooks |
| 71 | TESTED_PLAYBOOKS=${ALL_PLAYBOOKS[@]} |
| 72 | else |
| 73 | # Changes only in ansible/test/play-* area - run tests only for changed playbook tests |
| 74 | PLAYBOOKS=(`git diff HEAD^ HEAD --name-only ansible/test | cut -f 1-3 -d "/" | sort -u`) |
| 75 | run_molecule "${PLAYBOOKS[@]}" |
| 76 | # memorize already tested playbooks |
| 77 | TESTED_PLAYBOOKS=${PLAYBOOKS[@]} |
| 78 | fi |
Mateusz Pilat | b0f546a | 2019-05-08 13:33:46 +0200 | [diff] [blame] | 79 | fi |
Mateusz Pilat | 9c9be6f | 2019-05-06 17:12:36 +0200 | [diff] [blame] | 80 | |
Bartek Grzybowski | 87d77c4 | 2020-01-17 09:29:15 +0100 | [diff] [blame] | 81 | # Check for changes in Ansible playbooks |
| 82 | PLAYBOOK_CHANGES=(`git diff HEAD^ HEAD --name-only --relative=ansible ansible/*yml | cut -f 1 -d "."`) |
| 83 | if [ ${#PLAYBOOK_CHANGES[@]} -gt 0 ]; then |
| 84 | for playbook in ${PLAYBOOK_CHANGES[@]}; |
| 85 | do |
| 86 | if [ -d ansible/test/play-${playbook} ]; then |
| 87 | # If tests for this playbook are defined |
| 88 | if [[ ! ${TESTED_PLAYBOOKS[*]} =~ ${playbook} ]]; then |
| 89 | # AND weren't already run |
| 90 | run_molecule "ansible/test/play-${playbook}" |
| 91 | fi |
| 92 | else |
| 93 | # Warn that no tests are defined for this playbook |
| 94 | echo "[WARNING] ---------- THERE ARE NO TESTS DEFINED FOR ${playbook}.yml PLAYBOOK ----------" |
| 95 | fi |
| 96 | done |
| 97 | fi |
| 98 | |
| 99 | # Check for changes in Ansible group_vars or libraries |
| 100 | if ! $(git diff HEAD^ HEAD --exit-code --quiet --relative=ansible/group_vars) || \ |
| 101 | ! $(git diff HEAD^ HEAD --exit-code --quiet --relative=ansible/library); then |
| 102 | # If there are any changes in ansible/{group_vars,libraries} |
| 103 | # then run all playbook tests except those that've been |
| 104 | # already run |
| 105 | for playbook in ${ALL_PLAYBOOKS[@]}; |
| 106 | do |
| 107 | if [[ ! ${TESTED_PLAYBOOKS[*]} =~ ${playbook} ]]; then |
| 108 | run_molecule "${playbook}" |
| 109 | fi |
| 110 | done |
| 111 | fi |
| 112 | |
| 113 | # if build was changed |
Mateusz Pilat | 9c9be6f | 2019-05-06 17:12:36 +0200 | [diff] [blame] | 114 | |
Mateusz Pilat | b0f546a | 2019-05-08 13:33:46 +0200 | [diff] [blame] | 115 | if `git diff HEAD^ HEAD --name-only | grep -q "build"`; then |
| 116 | echo "TO DO: BUILD TEST" ; |
| 117 | else |
| 118 | echo "NO BUILD TEST REQUIRED" |
| 119 | fi |
Mateusz Pilat | 9c9be6f | 2019-05-06 17:12:36 +0200 | [diff] [blame] | 120 | |
Bartek Grzybowski | 87d77c4 | 2020-01-17 09:29:15 +0100 | [diff] [blame] | 121 | # if documentation was changed |
Mateusz Pilat | 9c9be6f | 2019-05-06 17:12:36 +0200 | [diff] [blame] | 122 | |
Mateusz Pilat | b0f546a | 2019-05-08 13:33:46 +0200 | [diff] [blame] | 123 | if `git diff HEAD^ HEAD --name-only | grep -q "docs"`; then |
| 124 | echo "TO DO: DOC TEST"; |
| 125 | else |
| 126 | echo "NO DOC TEST REQUIRED" |
| 127 | fi |
| 128 | |
Bartek Grzybowski | 87d77c4 | 2020-01-17 09:29:15 +0100 | [diff] [blame] | 129 | # SUMMARY RESULTS |
Mateusz Pilat | b0f546a | 2019-05-08 13:33:46 +0200 | [diff] [blame] | 130 | |
| 131 | if [ -z ${FAILED_ROLES} ]; then |
| 132 | echo "All verification steps passed" |
| 133 | else |
| 134 | echo "Verification failed for ${FAILED_ROLES[*]}" |
| 135 | exit 1 |
| 136 | fi |