Consider the following 3 files:
1. shellTestFramework.sh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | #!/bin/bash # Copyright (c) Ronald Pringadi # Before each Test function setUpTest(){ #"Please overwrite this function on your unit test. Something that need to be done before each test" return 0; } function tearDownTest(){ #"Please overwrite this function on your unit test. Something that need to be done after each test" return 0; } # Helping assertion function assertEqual(){ if [ "$1" == "$2" ]; then echo "`caller 0`. PASS" else echo "`caller 0`. FAIL. $1!=$2" fi } # Helping assertion function assertNotEqual(){ if [ "$1" != "$2" ]; then echo "`caller 0`. PASS" else echo "`caller 0`. FAIL. $1==$2" fi } # To run all test. # Will run 'setUpTest()' before each test. # Will run 'tearDownTest()' after each test. function runAllTest(){ array=($(compgen -A function |grep -i test)) for i in "${array[@]}" do if [ "$i" != "setUpTest" ] && [ "$i" != "runAllTest" ] && [ "$i" != "tearDownTest" ]; then echo "`setUpTest`" echo "`$i`" echo "`tearDownTest`" fi done } |
2. osdetector.sh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | #!/bin/bash # Copyright (c) Ronald Pringadi # Returns String ("ubuntu", "censtos", "redhat", "unknown") function detectAnyOS(){ OS="unknown" DETECT="ubuntu" if [ `detectSpecificOS "$DETECT"` -eq 1 ] ; then echo $DETECT return 0 fi DETECT="centos" if [ `detectSpecificOS "$DETECT"` -eq 1 ] ; then echo $DETECT return 0 fi DETECT="Red Hat" if [ `detectSpecificOS "$DETECT"` -eq 1 ] ; then echo "redhat" return 0 fi DETECT="RHEL" if [ `detectSpecificOS "$DETECT"` -eq 1 ] ; then echo "redhat" return 0 fi } # Input String ("ubuntu", "Centos", "RedHat") # Returns String "1"=found, "0"=not found function detectSpecificOS(){ DETECT=$1 IS_FOUND=`cat /etc/*-release |grep -i "$DETECT"|wc -l` if [ $IS_FOUND -gt 0 ]; then echo "1" else echo "0" fi } # Returns String ("6", "5.5", "4") function detectOSVersion(){ # Works for ubuntu VERSION=`cat /etc/*-release |grep 'DISTRIB_RELEASE='|grep -o '[0-9|.]\+'|head -1` if [ "$VERSION" != "" ]; then echo $VERSION return 0 fi # Works for Centos VERSION=`cat /etc/*-release |grep ' release '|grep -o '[0-9|.]\+'|head -1` if [ "$VERSION" != "" ]; then echo $VERSION return 0 fi # Final resort VERSION=`cat /etc/*-release |grep -i 'ubuntu\|red hat\|centos'| grep -o '\ [0-9|.]\+'|head -1` if [ "$VERSION" != "" ]; then echo $VERSION return 0 fi } # Returns String ("6", "5", "4") function detectOSMajorVersion(){ VERSION=`detectOSVersion` MAJOR_VERSION=`echo "$VERSION" |grep -o '[0-9]\+'|head -1` echo "$MAJOR_VERSION" } # Input String OS ("ubuntu", "Centos", "RedHat") # Input String OSVersion name # Returns String "1"=supported, "0"=not supported function isOSSupported(){ SUPPORTED="0" OS=`echo "$1"| tr '[:upper:]' '[:lower:]'` VERSION="$2" MAJOR_VERSION=`echo "$VERSION" |grep -o '[0-9]\+'|head -1` if [ "$OS" == "ubuntu" ]; then if [ "$MAJOR_VERSION" == "12" ] || [ "$MAJOR_VERSION" == "13" ] || [ "$MAJOR_VERSION" == "14" ]; then SUPPORTED="1" fi fi if [ "$OS" == "red hat" ] || [ "$OS" == "redhat" ] || [ "$OS" == "centos" ]; then if [ "$MAJOR_VERSION" == "6" ]; then SUPPORTED="1" fi fi echo $SUPPORTED } |
3. osdetector.test.sh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | #!/bin/bash # Copyright (c) Ronald Pringadi my_dir="$(dirname "$0")" source "$my_dir/osdetector.sh" source "$my_dir/shellTestFramework.sh" # Before each Test function setupTest(){ RELEASE_CONTENT="Ubuntu 14.04.3 LTS" echo "$RELEASE_CONTENT">/etc/test-release } function adetectSpecificOSTest(){ DETECT="Ubuntu" RESULT=`detectSpecificOS $DETECT` assertEqual "$RESULT" "1" DETECT="Centos" RESULT=`detectSpecificOS $DETECT` assertEqual "$RESULT" "0" } function detectAnyOSTest(){ RESULT=`detectAnyOS` assertEqual "$RESULT" "ubuntu" assertNotEqual "$RESULT" "centos" } function detectOSAndOsVersionTest(){ RELEASE_CONTENT="Ubuntu 14.04.3 LTS" echo "$RELEASE_CONTENT" > /etc/test-release RESULT=`detectOSVersion` assertEqual "$RESULT" "14.04.3" RESULT=`detectOSMajorVersion` assertEqual "$RESULT" "14" RESULT=`detectAnyOS` assertEqual "$RESULT" "ubuntu" RELEASE_CONTENT="CentOS release 6.6 (Final)" echo "$RELEASE_CONTENT" > /etc/test-release RESULT=`detectOSVersion` assertEqual "$RESULT" "6.6" RESULT=`detectOSMajorVersion` assertEqual "$RESULT" "6" RESULT=`detectAnyOS` assertEqual "$RESULT" "centos" RELEASE_CONTENT="CentOS Linux release 7.1.1503 (Core)" echo "$RELEASE_CONTENT" > /etc/test-release RESULT=`detectOSVersion` assertEqual "$RESULT" "7.1.1503" RESULT=`detectOSMajorVersion` assertEqual "$RESULT" "7" RESULT=`detectAnyOS` assertEqual "$RESULT" "centos" RELEASE_CONTENT="Red Hat Enterprise Linux Server release 6.3 (Santiago)" echo "$RELEASE_CONTENT" > /etc/test-release echo "$RELEASE_CONTENT" >> /etc/test-release RESULT=`detectOSVersion` assertEqual "$RESULT" "6.3" RESULT=`detectOSMajorVersion` assertEqual "$RESULT" "6" RESULT=`detectAnyOS` assertEqual "$RESULT" "redhat" } function isOSSupportedTest(){ RESULT=`isOSSupported "ubuntu" "11"` assertEqual "$RESULT" "0" RESULT=`isOSSupported "ubuntu" "12"` assertEqual "$RESULT" "1" RESULT=`isOSSupported "ubuntu" "13"` assertEqual "$RESULT" "1" RESULT=`isOSSupported "ubuntu" "14"` assertEqual "$RESULT" "1" RESULT=`isOSSupported "ubuntu" "15"` assertEqual "$RESULT" "0" RESULT=`isOSSupported "redhat" "5"` assertEqual "$RESULT" "0" RESULT=`isOSSupported "redhat" "6"` assertEqual "$RESULT" "1" RESULT=`isOSSupported "redhat" "7"` assertEqual "$RESULT" "0" RESULT=`isOSSupported "red hat" "5"` assertEqual "$RESULT" "0" RESULT=`isOSSupported "red hat" "6"` assertEqual "$RESULT" "1" RESULT=`isOSSupported "red hat" "7"` assertEqual "$RESULT" "0" RESULT=`isOSSupported "centos" "5"` assertEqual "$RESULT" "0" RESULT=`isOSSupported "centos" "6"` assertEqual "$RESULT" "1" RESULT=`isOSSupported "centos" "7"` assertEqual "$RESULT" "0" RESULT=`isOSSupported "Fedora" "50"` assertEqual "$RESULT" "0" } runAllTest |