yapi-plugin-pl-auto-test
Version:
YAPI自动化测试插件,支持在YAPI设置测试计划,历史测试结果存入ES,界面显示测试结果,自定义通知。
123 lines (116 loc) • 3.13 kB
JavaScript
const Config = require('./utils/config');
const yapi = require('yapi.js');
const { Client } = require('@elastic/elasticsearch')
const ops = Config.instance;
const logType = 'pl-auto-test';
class elasticsearch {
constructor(ctx) {
this.ctx = ctx;
const esUsername = `${ops.esUsername}`;
if (esUsername === 'undefined' || esUsername === "") {
yapi.commons.log("will use es with no login", logType);
this.client = new Client({ node: `http://${ops.esHost}` });
} else {
this.client = new Client({
node: `http://${ops.esHost}`, auth: {
username: `${ops.esUsername}`,
password: `${ops.esPwd}`
}
});
}
}
indicesExists (indexName) {
const promise = new Promise((resolve, reject) => {
this.client.indices.exists({
index: indexName
}, (err, result) => {
if (err) {
yapi.commons.log(`error:${JSON.stringify(err)}`, logType);
reject(false);
} else {
// yapi.commons.log(`indice exists:${result.body}`, logType);
resolve(eval(`${result.body}`));
}
});
})
return promise
}
indexCreate (indexName) {
const promise = new Promise((resolve, reject) => {
this.client.indices.create({
index: indexName,
body: {
mappings: {
_doc: {
properties: {
message: {
type: "object",
dynamic: true
},
list: {
properties: {
res_body: {
type: "test",
dynamic: false
}
}
}
}
}
}
}
}, (err, result) => {
if (err) {
// const detail = JSON.stringify(err.body.error)
yapi.commons.log(`error:${JSON.stringify(err)}`, logType);
reject(false);
} else {
resolve(true);
}
})
})
return promise
}
insertData (indexName, data) {
const promise = new Promise((resolve, reject) => {
this.client.index({
index: indexName,
type: "doc",
refresh: 'true',
body: data
}, (err, result) => {
if (err) {
let errStr = JSON.stringify(err);
const length = errStr.length;
if (length > 400) {
errStr = errStr.substring(0, 400);
}
yapi.commons.log(`indexName:${indexName},error:${errStr}`, logType);
reject(false);
} else {
resolve(true);
}
})
})
return promise
}
//no use ,just example
queryES (indexName) {
// callback API
this.client.search({
index: indexName,
body: {
query: {
match: { group_id: 23 }
}
}
}, (err, result) => {
if (err) {
yapi.commons.log(`error:${JSON.stringify(err)}`, logType);
} else {
yapi.commons.log(`result:${result.body.hits.total}`, logType);
}
})
}
}
module.exports = elasticsearch;