提交log用例示例和log关键字(Python文件)

This commit is contained in:
lyf
2021-03-26 10:50:43 +08:00
parent e987c67721
commit bf2def3ecd
3 changed files with 212 additions and 61 deletions

View File

@@ -0,0 +1,56 @@
*** Settings ***
Resource ../../../03-Variable/BifangApiVariable.txt
Library ../../../04-CustomLibrary/Custometest/Schema.py
Library DateTime
Library json
Library OperatingSystem
Library Selenium2Library
Library RequestsLibrary
Library Collections
Library String
*** Variables ***
${url} /${version}/report/url/recommend
*** Test Cases ***
LogApiVerifyCase-001
[Documentation] 验证api接口
${logurl} Set Variable http://${host}:${port}/v1/log/list
${logtype} Set Variable security_event_log
${schemaurl} Set Variable http://${host}:${port}/v1/log/schema?logType=${logtype}
${EndTime} get current date
${EndTime1} Get Substring ${EndTime} \ -4
${StartTime} add time to date ${EndTime} -01:00:00
${StartTime1} Get Substring ${StartTime} \ -4
logapiverify ${logurl} ${schemaurl} ${token} ${StartTime1} ${EndTime1} ${logtype}
LogApiVerifyCase-002
[Documentation] 验证api接口
${Logurl} Set Variable http://${host}:${port}/v1/log/list
${Logtype} Set Variable security_event_log
${Schemaurl} Set Variable http://${host}:${port}/v1/log/schema?logType=${logtype}
${EndTime} get current date
${EndTime1} Get Substring ${EndTime} \ -4
${StartTime} add time to date ${EndTime} -01:00:00
${StartTime1} Get Substring ${StartTime} \ -4
${filter} Set Variable common_recv_time = 45535
${responsebody} loglistverify ${logurl} ${schemaurl} ${token} ${StartTime1} ${EndTime1} ${logtype} ${filter}
log ${responsebody}

View File

@@ -11,16 +11,25 @@ Resource ../tsg_bfapi/ApiRequest.robot
Resource Log_schema.robot
Resource ../tsg_bfapi/Common.robot
Library ../../04-CustomLibrary/Custometest/Schema.py
Resource ../../03-Variable/BifangApiVariable.txt
*** Test Cases ***
Apitext
${logurl} Set Variable http://192.168.44.72:8080/v1/log/list
${logtype} Set Variable security_event_log
${schemaurl} Set Variable http://192.168.44.72:8080/v1/log/schema?logType=${logtype}
${EndTime} get current date
${EndTime1} Get Substring ${EndTime} \ -4
${StartTime} add time to date ${EndTime} -01:00:00
${StartTime1} Get Substring ${StartTime} \ -4
log ${StartTime1}
log ${EndTime1}
LogApiSearch ${StartTime1} ${EndTime1} security_event_log v1/log/list
logapiverify ${logurl} ${schemaurl} ${token} ${StartTime1} ${EndTime1} ${logtype}
*** Keywords ***
log_search
@@ -47,10 +56,16 @@ LogApiSearch
log 11111111${type1}
${schemaresponsedata1} Get From Dictionary ${schemaresponsedata} data
${filds} Get From Dictionary ${schemaresponsedata1} fields
${filedsJson} json.dumps ${filds}
${type3} Evaluate "${filds}".replace(' ','')
#${type2} Evaluate ${type3}.replace(' ','')
${type2} Evaluate type(${type3})
log ${type2}
log ${type3}
#${filedsJson} json.dumps ${type3}
log ${type3}
${filter1} run keyword if "${filter}"=="null" Filter1 ${schemaresponsedata}
run keyword if "${filter}"=="null" ForFilter ${start-time} ${end-time} ${logtype} ${url}
... ${filedsJson} ${conditions} ${filter1}
... ${type3} ${conditions} ${filter1}
... ELSE LogSearchRequest ${start-time} ${end-time} ${logtype} ${url}
... ${conditions} ${filter}
# ${requestbody} Set Variable {"start_common_recv_time":"${start-time}","end_common_recv_time":"${end-time}","logType":"${type}","fields":${filedsJson},"conditions":${conditions},"filter":${filter},refuseCode":true}
@@ -61,10 +76,11 @@ LogSearchRequest
[Arguments] ${start-time} ${end-time} ${logtype} ${url} ${filedsJson} ${conditions} ${filter}
# ${type2} Evaluate type(${filedsJson})
# log 7777${type2}
${requestbody} Set Variable {"start_common_recv_time":"${start-time}","end_common_recv_time":"${end-time}","logType":"${logtype}","fields":${filedsJson},"conditions":${conditions},"filter":${filter},refuseCode":true}
${requestbody} Set Variable {"start_common_recv_time":"${start-time}","end_common_recv_time":"${end-time}","logType":"${logtype}","fields":${filedsJson},"conditions":${conditions},"filter":${filter},refuseCode":"true"}
log ${requestbody}
#${requestbody1} json.loads ${requestbody}
#${type1} Evaluate type(${requestbody})
#log 88888888${type1}
# ${type2} Evaluate type(${requestbody})
# log 88888888${type2}
${response} Post-Request ${url} ${requestbody}
${code} Get From Dictionary ${response} code
log 22222222${response}

View File

@@ -3,28 +3,24 @@
import requests
import random
import json
import allure
number = random.randint(0,100000)
str = random.choice('abcdefghijklmnopqrstuvwxyz!@#$%^&*()')
str = random.choice('abcdefghijklmnopqrstuvwxyz')
list = []
# def schema():
# url = "http://192.168.44.72:8080/v1/log/schema?logType=security_event_log"
# headers = {"Content-Type":"application/x-www-form-urlencoded","Authorization":"555b5dc2-d69f-44a4-8a00-171e81edd739&889&"}
# response = requests.get(url=url,headers=headers)
# return response.json()
#请求schema接口得到返回数据用于其他接口
def schema(schemauerl,token):
url = schemauerl #"http://192.168.44.72:8080/v1/log/schema?logType=security_event_log"
headers = {"Content-Type":"application/x-www-form-urlencoded","Authorization":token}
response = requests.get(url=url,headers=headers)
return response.json()
def Filter1(schema):
print("1234")
print(type(schema))
json_str = json.dumps(schema)
schema1 = json.loads(json_str)
fields = schema1["data"]["fields"]
#print(fields)
#print(type(fields))
operator = schema1["data"]["doc"]["schema_query"]["references"]["operator"]
#print(operator)
#根据schema接口返回数据得出所有属性所支持的比较类型的列表
def Filter1(schemauerl,token):
json_str = schema(schemauerl,token)
print(type(json_str))
fields = json_str["data"]["fields"]
operator = json_str["data"]["doc"]["schema_query"]["references"]["operator"]
for i in fields:
print(i)
name = i["name"]
doc = i["doc"]
if doc == None:
@@ -36,15 +32,26 @@ def Filter1(schema):
functions = j["functions"]
functions1 = functions.split(",")
for v in functions1:
str1 = name + " "+v + " "+ f"{value1}"
list.append(str1)
if v=="in" or v == "not in":
str1 = name + " "+v + " "+ "(" +f"{value1}" + ")"
list.append(str1)
else:
str1 = name + " " + v + " " + f"{value1}"
list.append(str1)
elif type1 == "string":
value1 = str
functions = j["functions"]
functions1 = functions.split(",")
for v in functions1:
str1 = name + " "+v + " "+ value1
list.append(str1)
if v == "not empty" or v == "empty":
str1 = v + "(" + " '" + name + " '" + ")"
list.append(str1)
elif v == "in" or v == "not in":
str1 = name + " " + v + " " + "(" + " '" + value1 + " '" + ")"
list.append(str1)
else:
str1 = name + " " + v + " " + " '"+ value1 + " '"
list.append(str1)
else:
if i["doc"]["constraints"]== None:
type1 = i["type"]
@@ -55,15 +62,26 @@ def Filter1(schema):
functions = j["functions"]
functions1 = functions.split(",")
for v in functions1:
str1 = name + " "+v + " "+ f"{value1}"
list.append(str1)
if v == "in" or v == "not in":
str1 = name + " " + v + " " + "(" + f"{value1}" + ")"
list.append(str1)
else:
str1 = name + " " + v + " " + f"{value1}"
list.append(str1)
elif type1=="string":
value1 = str
functions = j["functions"]
functions1 = functions.split(",")
for v in functions1:
str1=name + " "+v + " "+ value1
list.append(str1)
if v == "not empty" or v == "empty":
str1 = v + "(" + " '" + name + " '" + ")"
list.append(str1)
elif v == "in" or v == "not in":
str1 = name + " " + v + " " + "(" + " '" + value1 + " '" + ")"
list.append(str1)
else:
str1 = name + " " + v + " " + " '" + value1 + " '"
list.append(str1)
else:
if i["doc"]["constraints"]["operator_functions"]==None:
@@ -75,15 +93,26 @@ def Filter1(schema):
functions = j["functions"]
functions1 = functions.split(",")
for v in functions1:
str1 = name + " "+ v + " "+ f"{value1}"
list.append(str1)
if v == "in" or v == "not in" :
str1 = name + " " + v + " " + "(" + f"{value1}" + ")"
list.append(str1)
else:
str1 = name + " " + v + " " + f"{value1}"
list.append(str1)
elif type1 == "string":
value1 = str
functions = j["functions"]
functions1 = functions.split(",")
for v in functions1:
str1 = name + " "+ v + " "+ value1
list.append(str1)
if v == "not empty" or v == "empty":
str1 = v + "(" + " '" + name + " '" + ")"
list.append(str1)
elif v == "in" or v == "not in":
str1 = name + " " + v + " " + "(" + " '" + value1 + " '" + ")"
list.append(str1)
else:
str1 = name + " " + v + " " + " '" + value1 + " '"
list.append(str1)
else:
type1 = i["type"]
operator1 = i["doc"]["constraints"]["operator_functions"]
@@ -97,29 +126,79 @@ def Filter1(schema):
list.append(str1)
else:
for o in operator2:
str1 = name + " "+ o + " "+ code
str1 = name + " "+ o + " "+ " '" + code + " '"
list.append(str1)
print(list)
return list
# def api():
# filter2 = Filter1()
# a = schema()
# fields = a["data"]["fields"]
# for i in filter2:
# url = "http://192.168.32.59:8888/v1/log/list"
# headers = {"Content-Type": "application/json",
# "Authorization": "7dda2581-38ca-4c48-9103-98ab852a5be5&807&"}
# data = {
# "start_common_recv_time":'2021-03-20 16:36:41',
# "end_common_recv_time":'2021-03-21 17:36:41',
# "logType":"security_event_log",
# "fields":fields,
# "filter":i
# }
# print(data)
# response1 = requests.post(url=url, data=json.dumps(data),headers=headers)
# print(response1.json())
# return response1.json()
#
# if __name__ == '__main__':
# api()
#根据Filter1方法中的的数据写入log请求接口中来验证log请求接口
def logapiverify(logurl,schemauerl,token,starttime,endtime,logtype):
filter2 = Filter1(schemauerl,token)
a = schema(schemauerl,token)
fields = a["data"]["fields"]
print(fields)
str2 = ""
for i in filter2:
str2 = str2 + i + " " + "and" + " "
url = logurl #"http://192.168.44.72:8080/v1/log/list"
headers = {"Content-Type": "application/json",
"Authorization": token}
data = {
"start_common_recv_time":starttime,
"end_common_recv_time":endtime,
"logType":logtype,
"fields":fields,
"filter":i
}
print(data)
print(json.dumps(data))
response1 = requests.post(url=url, data=json.dumps(data),headers=headers)
code = response1.json()["code"]
assert code == 200
print(response1.json()["code"])
return response1.json()
print(str2)
str3 = str2[0:-4]
print(str3)
url = logurl # "http://192.168.44.72:8080/v1/log/list"
headers = {"Content-Type": "application/json",
"Authorization": token}
data = {
"start_common_recv_time": starttime,
"end_common_recv_time": endtime,
"logType": logtype,
"fields": fields,
"filter": str3
}
print(data)
print(json.dumps(data))
response1 = requests.post(url=url, data=json.dumps(data), headers=headers)
code = response1.json()["code"]
print(response1.json())
assert code == 200
print(response1.json()["code"])
#精确filter请求日志接口
def loglistverify(logurl,schemauerl,token,starttime,endtime,logtype,filtervalue):
a = schema(schemauerl,token)
fields = a["data"]["fields"]
print(fields)
url = logurl # "http://192.168.44.72:8080/v1/log/list"
headers = {"Content-Type": "application/json",
"Authorization": token}
data = {
"start_common_recv_time": starttime,
"end_common_recv_time": endtime,
"logType": logtype,
"fields": fields,
"filter": filtervalue
}
print(data)
print(json.dumps(data))
response1 = requests.post(url=url, data=json.dumps(data), headers=headers)
code = response1.json()["code"]
print(response1.json())
assert code == 200
print(response1.json()["code"])
return response1.json()
#if __name__ == '__main__':
# logapiverify("http://192.168.32.59:8080/v1/log/list","http://192.168.32.59:8080/v1/log/schema?logType=security_event_log","d475b20d-e2b8-4f24-87ee-d54af46e6aff&807&",'2021-03-20 16:36:41','2021-03-21 17:36:41',"security_event_log")