python實現hive自動化測試
- 2020 年 1 月 7 日
- 筆記
本程式主要實現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>