iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >python实现hive自动化测试
  • 790
分享到

python实现hive自动化测试

测试pythonhive 2023-01-31 05:01:10 790人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

本程序主要实现Hive权限测试。系统中有管理员用户single和测试用户test。在路径/home/test/下,将用例和预期结果写在xml文件中。        执 行过程:kinit single用户,beeline -u -e登录并执

本程序主要实现Hive权限测试。系统中有管理员用户single和测试用户test。在路径/home/test/下,将用例和预期结果写在xml文件中。

        执 行过程:kinit single用户,beeline -u -e登录并执行对test用户对应角色回收和赋予权限的语句;kinit test用户,beeline -u -e去执行测试语句并保存执行结果到tmp文件中;在tmp文件中查找预期关键字,得出该测试pass还是fail的结果,统计测试结果。


#!/usr/bin/python
#coding:utf-8
#by cvv54

import sys
import os
import re

try:
    import xml.etree.cElementTree as ET
except ImportError:
    import xml.etree.ElementTree as ET

try:
    tree = ET.parse("/home/test/case/usecase.xml")
    #root = ET.fromstring(country_string)
    root = tree.getroot()
except Exception,e:
    print "Error:cannot parse file:usecase.xml."
    sys.exit(1)
print root.tag,"---",root.attrib
for child in root:
    print child.tag,"---",child.attrib

passed=0
failed=0    

for case in root.findall('case'):
    pre = case.find('pre').text
    perfORM = case.find('perform').text
    expect = case.find('expect').text.strip('\n')
    id = case.get("id")

    print "key Words are :"    
    print expect

    for each in pre.split(';'):
#        print each
        if not each.strip()=='':
            command = each.strip('\n')
            os.environ['command']=str(command)
            print command
        
            os.system("kdestroy")
            os.system("kinit -kt /etc/security/keytabs/single.keytab single")
            os.system('beeline -u "jdbc:hive2://gateway.xxx.xxx:10000/;principal=single" -e "$command;" &>>log')

    for each in perform.split(';'):
#        print each
        if not each.strip()=='':
            command = each.strip('\n')
            os.environ['command']=str(command)
            print command
        
            os.system("kdestroy")
            os.system("kinit -kt /etc/security/keytabs/test.keytab test")
            os.system('beeline -u "jdbc:hive2://gateway.xxx.xxx:10000/;principal=single" -e "$command;" &>tmp')
   
    f=open('tmp')
    flag=0
    for line in f:
#        print "line is :"
#        print line
        match=re.findall(expect,line)
        if match != []:
            passed+=1
            flag=1
        
    if flag == 0:
        failed+=1            
        print(id)
    
    os.system("cat tmp>>log")
    os.system("rm -f tmp")

print "passed:"
print passed
print "failed:"
print failed

os.system('mv log `date "+%Y-%m-%d~%H-%M-%S"`')



我的xml是这样写的:

<?xml version="1.0" encoding="utf-8"?>

<test>



<case id="001" name="SHOW TABLES">

<pre>

REVOKE ALL ON SERVER server1 FROM ROLE test_role; GRANT ALL ON URI TO ROLE test_role;

</pre>

<perform>

SHOW TABLES;

</perform>

<expect>

tab_name

</expect>

</case>



<case id="002" name="SHOW TABLES">

<pre>

REVOKE ALL ON SERVER server1 FROM ROLE test_role; 

</pre>

<perform>

SHOW TABLES;

</perform>

<expect>

FAILED: SemanticException No valid privileges

</expect>

</case>



<case id="003" name="SHOW CREATE TABLE" privilege="SELECT">

<pre>

CREATE TABLE IF NOT EXISTS test_table1  (id INT,name STRING,salary FLOAT,street STRING,city STRING,state STRING,zip INT) PARTITIONED BY (address STRING) row format delimited fields terminated by ' ';

REVOKE ALL ON SERVER server1 FROM ROLE test_role;GRANT SELECT ON TABLE test_table1 TO ROLE test_role;

</pre>

<perform>

SHOW CREATE TABLE test_table1;

</perform>

<expect>

createtab_stmt 

</expect>

</case>



<case id="004" name="SHOW CREATE TABLE" privilege="INSERT">

<pre>

REVOKE ALL ON SERVER server1 FROM ROLE test_role;GRANT INSERT ON TABLE test_table1 TO ROLE test_role;

</pre>

<perform>

SHOW CREATE TABLE test_table1;

</perform>

<expect>

createtab_stmt 

</expect>

</case>



<case id="005" name="SHOW CREATE TABLE" >

<pre>

REVOKE ALL ON SERVER server1 FROM ROLE test_role;

</pre>

<perform>

SHOW CREATE TABLE test_table1;

</perform>

<expect>

FAILED: SemanticException No valid privileges

</expect>

</case>



</test>


--结束END--

本文标题: python实现hive自动化测试

本文链接: https://www.lsjlt.com/news/188795.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作