Skip to content

Commit

Permalink
Adding unit test middleware
Browse files Browse the repository at this point in the history
Signed-off-by: Navin Shrinivas <karupal2002@gmail.com>
  • Loading branch information
NavinShrinivas committed Feb 1, 2024
1 parent b99756d commit ac50454
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 2 deletions.
50 changes: 50 additions & 0 deletions packages/jaeger-ui/src/middlewares/fixtures/otlp2jaeger-in.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"resourceSpans": [
{
"resource": {
"attributes": [
{
"key": "service.name",
"value": {
"stringValue": "telemetrygen"
}
}
]
},
"scopeSpans": [
{
"scope": {
"name": "telemetrygen"
},
"spans": [
{
"traceId": "83a9efd15c1c98a977e0711cc93ee28b",
"spanId": "e127af99e3b3e074",
"parentSpanId": "909541b92cf05311",
"name": "okey-dokey-0",
"kind": 2,
"startTimeUnixNano": "1706678909209712000",
"endTimeUnixNano": "1706678909209835000",
"attributes": [
{
"key": "net.peer.ip",
"value": {
"stringValue": "1.2.3.4"
}
},
{
"key": "peer.service",
"value": {
"stringValue": "telemetrygen-client"
}
}
],
"status": {}
}
]
}
],
"schemaUrl": "https://opentelemetry.io/schemas/1.4.0"
}
]
}
59 changes: 59 additions & 0 deletions packages/jaeger-ui/src/middlewares/fixtures/otlp2jaeger-out.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"data": [
{
"traceID": "83a9efd15c1c98a977e0711cc93ee28b",
"spans": [
{
"traceID": "83a9efd15c1c98a977e0711cc93ee28b",
"spanID": "e127af99e3b3e074",
"operationName": "okey-dokey-0",
"references": [
{
"refType": "CHILD_OF",
"traceID": "83a9efd15c1c98a977e0711cc93ee28b",
"spanID": "909541b92cf05311"
}
],
"startTime": 1706678909209712,
"duration": 123,
"tags": [
{
"key": "otel.library.name",
"type": "string",
"value": "telemetrygen"
},
{
"key": "net.peer.ip",
"type": "string",
"value": "1.2.3.4"
},
{
"key": "peer.service",
"type": "string",
"value": "telemetrygen-client"
},
{
"key": "span.kind",
"type": "string",
"value": "server"
}
],
"logs": [],
"processID": "p1",
"warnings": null
}
],
"processes": {
"p1": {
"serviceName": "telemetrygen",
"tags": []
}
},
"warnings": null
}
],
"total": 0,
"limit": 0,
"offset": 0,
"errors": null
}
2 changes: 1 addition & 1 deletion packages/jaeger-ui/src/middlewares/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const historyUpdateMiddleware = store => next => action => {
return next(action);
};

export const transformOTLPMiddleware = store => next => action => {
export const loadJsonTracesMiddleware = store => next => action => {
if (action.type === String([`${loadJsonTraces}_FULFILLED`])) {
// Check if action.payload is OTLP and make API call if so
// We are allowed to change the action.payload here
Expand Down
44 changes: 43 additions & 1 deletion packages/jaeger-ui/src/middlewares/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,22 @@ jest.mock(
);

import { change } from 'redux-form';

import fs from 'fs';
import _ from 'lodash';
import * as jaegerMiddlewares from './index';
import { fetchServiceOperations } from '../actions/jaeger-api';
import { loadJsonTraces } from '../actions/file-reader-api';
import JaegerAPI from '../api/jaeger';

jest.spyOn(JaegerAPI, 'transformOTLP').mockImplementation(APICallRequest => {
const OTLPTrace = JSON.parse(fs.readFileSync('src/middlewares/fixtures/otlp2jaeger-in.json', 'utf-8'));
const jaegerTrace = JSON.parse(fs.readFileSync('src/middlewares/fixtures/otlp2jaeger-out.json', 'utf-8'));
if (_.isEqual(APICallRequest, OTLPTrace)) {
return Promise.resolve(jaegerTrace);
}
// This defines case where API call errors out even after detecting a `resourceSpan` in the request:
return Promise.reject();
});

it('jaegerMiddlewares should contain the promise middleware', () => {
expect(typeof jaegerMiddlewares.promise).toBe('function');
Expand All @@ -41,3 +54,32 @@ it('loadOperationsForServiceMiddleware fetches operations for services', () => {
expect(dispatch).toHaveBeenCalledWith(fetchServiceOperations('yo'));
expect(dispatch).toHaveBeenCalledWith(change('searchSideBar', 'operation', 'all'));
});

it('loadJsonTracesMiddleware transformes traces from OTLP to jaeger by making an API call', async () => {
// Testing 3 paths in all :
// OTLP traces
// Jaeger traces
// Ivalid traces
const { loadJsonTracesMiddleware } = jaegerMiddlewares;
const dispatch = jest.fn();
const next = jest.fn();

const OTLPFileAction = {
type: String([`${loadJsonTraces}_FULFILLED`]),
payload: JSON.parse(fs.readFileSync('src/middlewares/fixtures/otlp2jaeger-in.json', 'utf-8')),
};

const JaegerAction = {
type: String([`${loadJsonTraces}_FULFILLED`]),
payload: JSON.parse(fs.readFileSync('src/middlewares/fixtures/otlp2jaeger-out.json', 'utf-8')),
};

await loadJsonTracesMiddleware({ dispatch })(next)(OTLPFileAction);
expect(JaegerAPI.transformOTLP).toHaveBeenCalledWith(OTLPFileAction.payload);
expect(next).toHaveBeenCalledWith(JaegerAction);
jest.clearAllMocks();
await loadJsonTracesMiddleware({ dispatch })(next)(JaegerAction);
expect(JaegerAPI.transformOTLP).not.toHaveBeenCalled();
expect(next).toHaveBeenCalledWith(JaegerAction);
// Errored JSON are caught before any redux action is invoked
});

0 comments on commit ac50454

Please sign in to comment.