千家信息网

shell实现hive自动化测试

发表于:2025-01-31 作者:千家信息网编辑
千家信息网最后更新 2025年01月31日,本程序主要实现hive权限测试。系统中有管理员用户single和测试用户test。在路径/home/test/下,将用例的预置条件写在input文件夹内,每个用例对应一个input文件,命名为x-y~
千家信息网最后更新 2025年01月31日shell实现hive自动化测试

本程序主要实现hive权限测试。系统中有管理员用户single和测试用户test。在路径/home/test/下,将用例的预置条件写在input文件夹内,每个用例对应一个input文件,命名为x-y~z.q(其中xyz都是数字);将用例需要执行的语句放在case文件夹内,命名为case_x.q;将预期结果写入expect文件夹,命名为expect_x;执行结果输出到output文件夹中,命名为x-y~z.q

执行过程:kinit single用户,beeline -u -f登录并执行input文件中对test用户对应角色回收和赋予权限的语句;kinit test用户,beeline -u -f去执行case文件夹中的测试语句并保存执行结果到output文件夹中;根据expect中是否有文件及该测试为正向或逆向测试,和output内容作比较,得出该测试pass还是fail的结果,统计测试结果。

该程序能够实现hive的权限自动化测试,但每个用例对应一个input文件、一个case文件、一个expect文件、一个output文件,太过繁杂,不好用。下一篇出python优化版本。


#!/bin/bash#by cvv54        #rename existed log.txt#    if [ -f "/home/test/log/log.txt" ] ;then #        mv /home/test/log/log.txt /home/test/log/`date "+%Y-%m-%d~%H-%M-%S"`#    fi    #To create log file    date>>/home/test/log/`date "+%Y-%m-%d~%H-%M-%S"`    log=`ls /home/test/log/ -rt |tail -1`        pass=0;    fail=0;    block=0;    #To traversal files in input folderfor i in /home/test/input/*.q do    #To login system with user single to grant proper privileges to user test    #$i here is name of input file,it looks like this: /path/to/file/x-y~z.q    #(x:case number;y:item number;z:can be 1 or 2,means Positive testing or Negative Testing;end with .q:sql file)    kdestroy     kinit -kt /etc/security/keytabs/single.keytab single    beeline -u "jdbc:hive2://gateway.xxx.xxx:10000/;principal=single" -f $i &>>/home/test/log/$log#${var%%-*}#该命令的作用是去掉变量var从右边算起的最后一个'-'字符及其右边的内容,返回从右边算起的最后一个'/'(不含该字符)的左边的内容。#${var##*/}#该命令的作用是去掉变量var从左边算起的最后一个'/'字符及其左边的内容,返回从左边算起的最后一个'/'(不含该字符)的右边的内容。    #To drop path,only filename left    j=${i##*/}    #only for debug    echo "j=$j"        #To get last number in filename to distinguish  Positive testing from Negative Testing    k=${i##*~}    #only for debug    echo "k=$k"    #get case number    m=${j%%-*}    #only for debug    echo "m=$m"        #To login system with user test to execute query statement    #filename in folder case looks like this: case_x.q(x is varable)    #filename in folder output will be named like output_x-y~z.q(it is not a sql file...)    kdestroy    kinit -kt /etc/security/keytabs/test.keytab test    beeline -u "jdbc:hive2://gateway.xxx.xxx:10000/;principal=single" -f /home/test/case/case_$m.q  &>>/home/test/output/output_$j         #To check results    #filename in folder expect looks like this: expect_x(x is varable)    if [ $k = "1.q" ];then        if [ -f "/home/test/expect/expect_$m" ];then            eval $(/bin/grep ok /home/test/expect/expect_$m)            eval $(/bin/grep nok /home/test/expect/expect_$m)            if [ -n "`grep -w $ok /home/test/output/output_$j`"];then                echo "$i pass">>/home/test/log/result.txt                ((pass++))            elif [ -n "`grep -w $nok /home/test/output/output_$j`"];then                echo "$i failed">>/home/test/log/result.txt                ((fail++))            else                 echo "$i block">>/home/test/log/result.txt                ((block++))            fi        elif [ -n "`grep -w "seconds" /home/test/output/output_$j`" ];then            echo "$i pass">>/home/test/log/result.txt            ((pass++))        elif [ -n "`grep -w "FAILED: SemanticException No valid privileges" /home/test/output/output_$j`" ];then            echo "$i failed">>/home/test/log/result.txt            ((fail++))        else            echo "$i block">>/home/test/log/result.txt            ((block++))        fi    else         if [ -f "/home/test/expect/expect_$m" ];then            eval $(/bin/grep ok /home/test/expect/expect_$m)            eval $(/bin/grep nok /home/test/expect/expect_$m)            if [ -n "`grep -w $nok /home/test/output/output_$j`" ];then                echo "$i pass">>/home/test/log/result.txt                ((pass++))            elif [ -n "`grep -w $ok /home/test/output/output_$j`" ];then                echo "$i failed">>/home/test/log/result.txt                ((fail++))            else                 echo "$i block">>/home/test/log/result.txt                ((block++))            fi        elif [ -n "`grep -w "seconds" /home/test/output/output_$j`" ];then            echo "$i failed">>/home/test/log/result.txt            ((fail++))        elif [ -n "`grep -w "FAILED: SemanticException No valid privileges" /home/test/output/output_$j`" ];then            echo "$i pass">>/home/test/log/result.txt            ((pass++))                else            echo "$i block">>/home/test/log/result.txt            ((block++))        fi    fi    doneecho `date` >>/home/test/log/result.txtecho "$pass passed" >>/home/test/log/result.txtecho "$fail failed">>/home/test/log/result.txtecho "$block blocked">>/home/test/log/result.txt#rename log.txt with current timecat /home/test/log/result.txt >> /home/test/log/$logrm -f /home/test/log/result.txt
0