Improve offline-installer-review.sh logic to only run relevant tests 06/100406/1
authorBartek Grzybowski <b.grzybowski@partner.samsung.com>
Fri, 17 Jan 2020 08:29:15 +0000 (09:29 +0100)
committerBartek Grzybowski <b.grzybowski@partner.samsung.com>
Fri, 17 Jan 2020 11:34:48 +0000 (12:34 +0100)
Main CI wrapper script refactored so that it runs tests in a more
granulated manner, i.e. matching between changed files and relevant
tests to run was optimised.

Change-Id: I09086099d7d16c52e29da2fe19440bcfd79ad295
Signed-off-by: Bartek Grzybowski <b.grzybowski@partner.samsung.com>
Issue-ID: OOM-1853

offline-installer-review.sh

index 3530d8f..3e011ea 100755 (executable)
@@ -1,7 +1,7 @@
-#! /usr/bin/env bash
+#!/usr/bin/env bash
 #   COPYRIGHT NOTICE STARTS HERE
 #
-#   Copyright 2018 © Samsung Electronics Co., Ltd.
+#   Copyright 2018-2020 © Samsung Electronics Co., Ltd.
 #
 #   Licensed under the Apache License, Version 2.0 (the "License");
 #   you may not use this file except in compliance with the License.
@@ -50,25 +50,67 @@ function run_molecule() {
 #                           MAIN                                      #$
 #######################################################################$
 FAILED_ROLES=()
+ALL_PLAYBOOKS=(`ls -d ansible/test/play-*`) # enumerate all playbook tests for later usage
 
-#if ansible role was changed$$
-ROLE_CHANGES=(`git diff  HEAD^ HEAD --name-only | grep "ansible/role" | cut -f 1-3 -d "/" | sort -u`)
+# Check for changes in Ansible roles
+ROLE_CHANGES=(`git diff HEAD^ HEAD --name-only ansible/roles | cut -f 1-3 -d "/" | sort -u`)
 if [ -z "${ROLE_CHANGES}" ];  then
   echo "NO ANSIBLE ROLE TESTS REQUIRED"
 else
   run_molecule "${ROLE_CHANGES[@]}"
 fi
 
-#if ansible was changed
+# Check for changes in Molecule tests
+if ! $(git diff HEAD^ HEAD --exit-code --quiet ansible/test); then
+  # If there are any changes in ansible/test area
+  MOLECULE_CHANGES=(`git diff HEAD^ HEAD --name-only ansible/test | grep -v "ansible/test/play-.*/"`)
+  if [ ${#MOLECULE_CHANGES[@]} -gt 0 ]; then
+    # If detected changes that affect all playbook tests - run all
+    run_molecule "${ALL_PLAYBOOKS[@]}"
+    # memorize already tested playbooks
+    TESTED_PLAYBOOKS=${ALL_PLAYBOOKS[@]}
+  else
+    # Changes only in ansible/test/play-* area - run tests only for changed playbook tests
+    PLAYBOOKS=(`git diff HEAD^ HEAD --name-only ansible/test | cut -f 1-3 -d "/" | sort -u`)
+    run_molecule "${PLAYBOOKS[@]}"
+    # memorize already tested playbooks
+    TESTED_PLAYBOOKS=${PLAYBOOKS[@]}
+  fi
+fi
 
-if `git diff  HEAD^ HEAD --name-only | grep -q "ansible/test"`; then
-  PLAYBOOKS=(`find ansible/test -name "play-*"`)
-  run_molecule "${PLAYBOOKS[@]}"
-else
-  echo "NO FULL ANSIBLE TEST REQUIRED";
+# Check for changes in Ansible playbooks
+PLAYBOOK_CHANGES=(`git diff HEAD^ HEAD --name-only --relative=ansible ansible/*yml | cut -f 1 -d "."`)
+if [ ${#PLAYBOOK_CHANGES[@]} -gt 0 ]; then
+  for playbook in ${PLAYBOOK_CHANGES[@]};
+  do
+    if [ -d ansible/test/play-${playbook} ]; then
+      # If tests for this playbook are defined
+      if [[ ! ${TESTED_PLAYBOOKS[*]} =~ ${playbook} ]]; then
+        # AND weren't already run
+        run_molecule "ansible/test/play-${playbook}"
+      fi
+    else
+      # Warn that no tests are defined for this playbook
+      echo "[WARNING] ---------- THERE ARE NO TESTS DEFINED FOR ${playbook}.yml PLAYBOOK ----------"
+    fi
+  done
 fi
 
-#if build was changed
+# Check for changes in Ansible group_vars or libraries
+if ! $(git diff HEAD^ HEAD --exit-code --quiet --relative=ansible/group_vars) || \
+   ! $(git diff HEAD^ HEAD --exit-code --quiet --relative=ansible/library); then
+  # If there are any changes in ansible/{group_vars,libraries}
+  # then run all playbook tests except those that've been
+  # already run
+  for playbook in ${ALL_PLAYBOOKS[@]};
+  do
+    if [[ ! ${TESTED_PLAYBOOKS[*]} =~ ${playbook} ]]; then
+      run_molecule "${playbook}"
+    fi
+  done
+fi
+
+# if build was changed
 
 if `git diff  HEAD^ HEAD --name-only | grep -q "build"`; then
   echo "TO DO: BUILD TEST" ;
@@ -76,7 +118,7 @@ else
   echo "NO BUILD TEST REQUIRED"
 fi
 
-#if documentation was changed
+# if documentation was changed
 
 if `git diff  HEAD^ HEAD --name-only | grep -q "docs"`; then
   echo "TO DO: DOC TEST";
@@ -84,7 +126,7 @@ else
   echo "NO DOC TEST REQUIRED"
 fi
 
-#SUMMARY RESULTS
+# SUMMARY RESULTS
 
 if [ -z ${FAILED_ROLES}  ]; then
   echo "All verification steps passed"
@@ -92,4 +134,3 @@ else
   echo "Verification failed for ${FAILED_ROLES[*]}"
   exit 1
 fi
-