#!/bin/bash

group=$1
testName="KERN"
testSummary="running kernel matches newest installed kernel package"

source ../get_test_values --group=$group


function test()
{
    local system=$1
    timeout 10 ssh -T root@$system <<EOF
    function get_run_kernel() 
    { 
        uname -r  | sed -e "s/-default//"
    }
    function get_inst_kernel()
    {
       zypper se --installed --details --match-exact kernel-default 2>/dev/null |\
          tr -d ' ' | awk -F '|' '/kernel-default/ && cont { print \$4; cont=0} ' cont=1 |\
          sed -e "s/\.[^.]*$//"
    }
    a=\$(get_run_kernel)
    b=\$(get_inst_kernel)
    if [ \$a = \$b ]; then
        exit 0
    else
        exit 1
    fi
EOF
    return $?
}

for system in $allSystems; do
    test $system; rc=$?
    logger -t $LandscapeUseCase -s "Test $testName for $group on node $system rc:  $rc"
    case $rc in 
        124 | 255 ) # test timeout - set N/A
             rm -f $STATUS_BASE_DIR/.status/${group}_${testName}_OK
             rm -f $STATUS_BASE_DIR/.status/${group}_${testName}_FAIL
             res="NA";
             ;;
        0  )
             touch $STATUS_BASE_DIR/.status/${group}_${testName}_OK
             rm -f $STATUS_BASE_DIR/.status/${group}_${testName}_FAIL
             # no break
             ;;
        1 )  rm -f $STATUS_BASE_DIR/.status/${group}_${testName}_OK
             touch $STATUS_BASE_DIR/.status/${group}_${testName}_FAIL
             ;;
    esac
done
