Run env setup only once
[oom/offline-installer.git] / offline-installer-review.sh
1 #!/usr/bin/env bash
2 #   COPYRIGHT NOTICE STARTS HERE
3 #
4 #   Copyright 2018-2020 © Samsung Electronics Co., Ltd.
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
19 ###############################################################################
20 # This script performs Jenkins Change Verification for ONAP Offline Installer #
21 # No parameters are expected                                                  #
22 ###############################################################################
23
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   local roles=("$@")
35   local MOLECULE_RC
36   for role in ${roles[@]}
37     do
38       if [ -f ${role}/molecule/default/molecule.yml ]; then
39         ./ansible/test/bin/ci-molecule.sh ${role}
40         MOLECULE_RC=$?
41         if [ ${MOLECULE_RC} -ne "0" ]; then FAILED_ROLES+=(${role}); fi
42       else
43         echo "[WARNING] ---------- THERE ARE NO TESTS DEFINED FOR  ${role} ----------"
44       fi
45   done
46 }
47
48 #######################################################################$
49 #                           MAIN                                      #$
50 #######################################################################$
51 FAILED_ROLES=()
52 ALL_PLAYBOOKS=(`ls -d ansible/test/play-*`) # enumerate all playbook tests for later usage
53 # Setup environment
54 prep_ubuntu_16_04_for_molecule
55
56 # Check for changes in Ansible roles
57 ROLE_CHANGES=(`git diff HEAD^ HEAD --name-only ansible/roles | cut -f 1-3 -d "/" | sort -u`)
58 if [ -z "${ROLE_CHANGES}" ];  then
59   echo "NO ANSIBLE ROLE TESTS REQUIRED"
60 else
61   run_molecule "${ROLE_CHANGES[@]}"
62 fi
63
64 # Check for changes in Molecule tests
65 if ! $(git diff HEAD^ HEAD --exit-code --quiet ansible/test); then
66   # If there are any changes in ansible/test area
67   MOLECULE_CHANGES=(`git diff HEAD^ HEAD --name-only ansible/test | grep -v "ansible/test/play-.*/"`)
68   if [ ${#MOLECULE_CHANGES[@]} -gt 0 ]; then
69     # If detected changes that affect all playbook tests - run all
70     run_molecule "${ALL_PLAYBOOKS[@]}"
71     # memorize already tested playbooks
72     TESTED_PLAYBOOKS=${ALL_PLAYBOOKS[@]}
73   else
74     # Changes only in ansible/test/play-* area - run tests only for changed playbook tests
75     PLAYBOOKS=(`git diff HEAD^ HEAD --name-only ansible/test | cut -f 1-3 -d "/" | sort -u`)
76     run_molecule "${PLAYBOOKS[@]}"
77     # memorize already tested playbooks
78     TESTED_PLAYBOOKS=${PLAYBOOKS[@]}
79   fi
80 fi
81
82 # Check for changes in Ansible playbooks
83 PLAYBOOK_CHANGES=(`git diff HEAD^ HEAD --name-only --relative=ansible ansible/*yml | cut -f 1 -d "."`)
84 if [ ${#PLAYBOOK_CHANGES[@]} -gt 0 ]; then
85   for playbook in ${PLAYBOOK_CHANGES[@]};
86   do
87     if [ -d ansible/test/play-${playbook} ]; then
88       # If tests for this playbook are defined
89       if [[ ! ${TESTED_PLAYBOOKS[*]} =~ ${playbook} ]]; then
90         # AND weren't already run
91         run_molecule "ansible/test/play-${playbook}"
92       fi
93     else
94       # Warn that no tests are defined for this playbook
95       echo "[WARNING] ---------- THERE ARE NO TESTS DEFINED FOR ${playbook}.yml PLAYBOOK ----------"
96     fi
97   done
98 fi
99
100 # Check for changes in Ansible group_vars or libraries
101 if ! $(git diff HEAD^ HEAD --exit-code --quiet --relative=ansible/group_vars) || \
102    ! $(git diff HEAD^ HEAD --exit-code --quiet --relative=ansible/library); then
103   # If there are any changes in ansible/{group_vars,libraries}
104   # then run all playbook tests except those that've been
105   # already run
106   for playbook in ${ALL_PLAYBOOKS[@]};
107   do
108     if [[ ! ${TESTED_PLAYBOOKS[*]} =~ ${playbook} ]]; then
109       run_molecule "${playbook}"
110     fi
111   done
112 fi
113
114 # if build was changed
115
116 if `git diff  HEAD^ HEAD --name-only | grep -q "build"`; then
117   echo "TO DO: BUILD TEST" ;
118 else
119   echo "NO BUILD TEST REQUIRED"
120 fi
121
122 # if documentation was changed
123
124 if `git diff  HEAD^ HEAD --name-only | grep -q "docs"`; then
125   echo "TO DO: DOC TEST";
126 else
127   echo "NO DOC TEST REQUIRED"
128 fi
129
130 # SUMMARY RESULTS
131
132 if [ -z ${FAILED_ROLES}  ]; then
133   echo "All verification steps passed"
134 else
135   echo "Verification failed for ${FAILED_ROLES[*]}"
136   exit 1
137 fi