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 folder
for 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
    

done

echo `date` >>/home/test/log/result.txt
echo "$pass passed" >>/home/test/log/result.txt
echo "$fail failed">>/home/test/log/result.txt
echo "$block blocked">>/home/test/log/result.txt
#rename log.txt with current time
cat /home/test/log/result.txt >> /home/test/log/$log
rm -f /home/test/log/result.txt
时间: 2024-08-05 23:38:46

shell实现hive自动化测试的相关文章

python实现hive自动化测试

本程序主要实现hive权限测试.系统中有管理员用户single和测试用户test.在路径/home/test/下,将用例和预期结果写在xml文件中. 执 行过程:kinit single用户,beeline -u -e登录并执行对test用户对应角色回收和赋予权限的语句:kinit test用户,beeline -u -e去执行测试语句并保存执行结果到tmp文件中:在tmp文件中查找预期关键字,得出该测试pass还是fail的结果,统计测试结果. #!/usr/bin/python #codin

学习Hive和Impala必看经典解析

Hive和Impala作为数据查询工具,它们是怎样来查询数据的呢?与Impala和Hive进行交互,我们有哪些工具可以使用呢? 我们首先明确Hive和Impala分别提供了对应查询的接口: (1)命令行shell: 1. Impala:impala shell 2. Hive:beeline(早期hive的命令行版本是hive shell,现在基本不使用) (2)Hue Web UI: 1.Hue里面提供了 Hive查询编辑器 2.Hue里面提供了Impala查询编辑器 3.Hue里面提供了元数

Hive-1.2.0学习笔记(三)Hive用户接口

鲁春利的工作笔记,谁说程序员不能有文艺范? Hive对外提供了三种服务模式,即CLI(command line interface).Hive Web和Hive Client(如JavaApi方式). 1.Hive命令行模式(CLI) 启动Hive命令行模式有两种方式 bin/hive 或 bin/hive --service cli hive命令选项 [[email protected] hive1.2.0]$ bin/hive --help Usage ./hive <parameters>

【原】hive 操作笔记

1.建表: hive> CREATE TABLE pokes (foo INT, bar STRING);hive> CREATE TABLE invites (foo INT, bar STRING) PARTITIONED BY (ds STRING);由于很多数据在hadoop平台,当从hadoop平台的数据迁移到hive目录下时,由于hive默认的分隔符是/u0001,为了平滑迁移,需要在创建表格时指定数据的分割符号,语法如下:create table ooo(uid string,n

Hive安装和基础使用

1.安装JDK并设置环境变量 2.上传安装包 3.解压 4.设置环境变量# vi ~/.bash_profile或vi /etc/profile5.进入hive shell# hive shell或# hive 6.常见操作 查看数据库清单hive> show databses; 查看表清单hive> show tables;查看表结构hive> desc table_name;创建数据库,location为hdfs中的路径为hdfs中的路径,不存在的目录会自动创建.hive> c

hive 的判断条件(if、coalesce、case)

CONDITIONAL FUNCTIONS IN HIVE Hive supports three types of conditional functions. These functions are listed below: IF( Test Condition, True Value, False Value ) The IF condition evaluates the "Test Condition" and if the "Test Condition&quo

Hive的Security配置

为了更好地使用好Hive,我将<Programming Hive>的Security章节取出来,翻译了一下. Hive还是支持相当多的权限管理功能,满足一般数据仓库的使用. Hive由一个默认的设置来配置新建文件的默认权限. Xml代码   <property> <name>hive.files.umask.value</name> <value>0002</value> <description>The dfs.umas

hadoop+hive使用中遇到的问题汇总

问题排查方式 一般的错误,查看错误输出,按照关键字google 异常错误(如namenode.datanode莫名其妙挂了):查看hadoop($HADOOP_HOME/logs)或hive日志 hadoop错误1.datanode无法正常启动添加datanode后,datanode无法正常启动,进程一会莫名其妙挂掉,查看namenode日志显示如下: Text代码 2013-06-21 18:53:39,182 FATAL org.apache.hadoop.hdfs.StateChange:

Hive的安装与配置

1.因为我使用MySQL做为Hive的元数据库,所以先安装MySQL. 参考:http://www.cnblogs.com/hunttown/p/5452205.html 登录命令:mysql -h主机地址 -u用户名 -p用户密码 mysql –u root #初始登录没有密码 修改密码 格式:mysqladmin -u用户名 -p旧密码 password 新密码 mysql>mysqladmin -uroot –password 123456 注:因为开始时root没有密码,所以-p旧密码一