Skip to content

Commit

Permalink
Merge pull request #145 from test-results-reporter/feature/testbeats
Browse files Browse the repository at this point in the history
added beats
  • Loading branch information
ASaiAnudeep authored Jan 20, 2024
2 parents 2e292ae + 872e4f7 commit 823faec
Show file tree
Hide file tree
Showing 9 changed files with 326 additions and 21 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "test-results-reporter",
"version": "1.0.19",
"version": "1.1.0",
"description": "Publish test results to Microsoft Teams, Google Chat, Slack and InfluxDB",
"main": "src/index.js",
"types": "./src/index.d.ts",
Expand Down
82 changes: 82 additions & 0 deletions src/beats/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
const request = require('phin-retry');
const TestResult = require('test-results-parser/src/models/TestResult');

const BASE_URL = "http://localhost:9393";

/**
* @param {import('../index').PublishReport} config
* @param {TestResult} result
*/
async function run(config, result) {
if (config.project && config.build && config.api_key) {
const run_id = await publishTestResults(config, result);
if (run_id) {
attachTestBeatsReportHyperLink(config, run_id);
}
}
}

/**
* @param {import('../index').PublishReport} config
* @param {TestResult} result
*/
async function publishTestResults(config, result) {
try {
const response = await request.post({
url: `${BASE_URL}/api/core/v1/test-runs`,
headers: {
'x-api-key': config.api_key
},
body: {
project: config.project,
build: config.build,
...result
}
});
return response.id;
} catch (error) {
console.log("Unable to publish results to TestBeats");
console.log(error);
}
}

/**
* @param {import('../index').PublishReport} config
* @param {string} run_id
*/
function attachTestBeatsReportHyperLink(config, run_id) {
const hyperlink_to_test_beats = getTestBeatsReportHyperLink(run_id);
for (const target of config.targets) {
if (target.name === 'chat' || target.name === 'teams' || target.name === 'slack') {
target.extensions = target.extensions || [];
if (target.extensions.length > 0) {
if (target.extensions[0].name === 'hyperlinks' && target.extensions[0].inputs.links[0].name === 'Test Beats Report') {
target.extensions[0].inputs.links[0].url = `${BASE_URL}/reports/${run_id}`;
continue;
}
}
target.extensions = [hyperlink_to_test_beats, ...target.extensions];
}
}
}

/**
*
* @param {string} run_id
* @returns
*/
function getTestBeatsReportHyperLink(run_id) {
return {
"name": "hyperlinks",
"inputs": {
"links": [
{
"text": "Test Beats Report",
"url": `${BASE_URL}/reports/${run_id}`
}
]
}
}
}

module.exports = { run }
45 changes: 30 additions & 15 deletions src/commands/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const trp = require('test-results-parser');
const prp = require('performance-results-parser');

const { processData } = require('../helpers/helper');
const beats = require('../beats');
const target_manager = require('../targets');

/**
Expand All @@ -14,22 +15,36 @@ async function run(opts) {
opts.config = require(path.join(cwd, opts.config));
}
const config = processData(opts.config);
for (const report of config.reports) {
const parsed_results = [];
for (const result_options of report.results) {
if (result_options.type === 'custom') {
parsed_results.push(result_options.result);
} else if (result_options.type === 'jmeter') {
parsed_results.push(prp.parse(result_options));
} else {
parsed_results.push(trp.parse(result_options));
}
if (config.reports) {
for (const report of config.reports) {
await processReport(report);
}
for (let i = 0; i < parsed_results.length; i++) {
const result = parsed_results[i];
for (const target of report.targets) {
await target_manager.run(target, result);
}
} else {
await processReport(config);
}
}

/**
*
* @param {import('../index').PublishReport} report
*/
async function processReport(report) {
const parsed_results = [];
for (const result_options of report.results) {
if (result_options.type === 'custom') {
parsed_results.push(result_options.result);
} else if (result_options.type === 'jmeter') {
parsed_results.push(prp.parse(result_options));
} else {
parsed_results.push(trp.parse(result_options));
}
}

for (let i = 0; i < parsed_results.length; i++) {
const result = parsed_results[i];
await beats.run(report, result);
for (const target of report.targets) {
await target_manager.run(target, result);
}
}
}
Expand Down
14 changes: 11 additions & 3 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,20 @@ export interface CustomResultOptions {
}

export interface PublishReport {
targets: Target[];
results: ParseOptions[] | PerformanceParseOptions[] | CustomResultOptions[];
api_key?: string;
project?: string;
build?: string;
targets?: Target[];
results?: ParseOptions[] | PerformanceParseOptions[] | CustomResultOptions[];
}

export interface PublishConfig {
reports: PublishReport[];
api_key?: string;
project?: string;
build?: string;
targets?: Target[];
results?: ParseOptions[] | PerformanceParseOptions[] | CustomResultOptions[];
reports?: PublishReport[];
}

export interface PublishOptions {
Expand Down
96 changes: 96 additions & 0 deletions test/beats.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
const { mock } = require('pactum');
const assert = require('assert');
const { publish } = require("../src");

describe('TestBeats', () => {

it('should send results to beats', async () => {
const id1 = mock.addInteraction('post test results to testbeats');
const id2 = mock.addInteraction('post test-summary with testbeats to teams');
await publish({
config: {
api_key: 'api-key',
project: 'project-name',
build: 'build-name',
targets: [
{
name: 'teams',
inputs: {
url: 'http://localhost:9393/message'
}
}
],
results: [
{
type: 'testng',
files: [
'test/data/testng/single-suite.xml'
]
}
]
}
});
assert.equal(mock.getInteraction(id1).exercised, true);
assert.equal(mock.getInteraction(id2).exercised, true);
});

it('should send results to beats with extensions', async () => {
const id1 = mock.addInteraction('post test results to testbeats');
const id2 = mock.addInteraction('post test-summary with extensions and testbeats to teams');
await publish({
config: {
api_key: 'api-key',
project: 'project-name',
build: 'build-name',
targets: [
{
name: 'teams',
inputs: {
url: 'http://localhost:9393/message'
},
"extensions": [
{
"name": "metadata",
"inputs": {
"data": [
{
"key": "Browser",
"value": "Chrome"
},
{
"value": "1920*1080"
},
{
"value": "1920*1080",
"condition": "never"
},
{
"key": "Pipeline",
"value": "some-url",
"type": "hyperlink"
},
]
}
}
]
}
],
results: [
{
type: 'testng',
files: [
'test/data/testng/single-suite.xml'
]
}
]
}
});
assert.equal(mock.getInteraction(id1).exercised, true);
assert.equal(mock.getInteraction(id2).exercised, true);
});

afterEach(() => {
mock.clearInteractions();
});

});
17 changes: 17 additions & 0 deletions test/mocks/beats.mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const { addInteractionHandler } = require('pactum').handler;

addInteractionHandler('post test results to testbeats', () => {
return {
strict: false,
request: {
method: 'POST',
path: '/api/core/v1/test-runs'
},
response: {
status: 200,
body: {
id: 'test-run-id'
}
}
}
});
1 change: 1 addition & 0 deletions test/mocks/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require('./beats.mock');
require('./custom.mock');
require('./rp.mock');
require('./slack.mock');
Expand Down
Loading

0 comments on commit 823faec

Please sign in to comment.