Skip to content

Commit 1b2d594

Browse files
Merge pull request #95 from alanconway/run-unit-tests
test: Add more unit tests, run tests from Makefile.
2 parents 468092d + 705c0cf commit 1b2d594

8 files changed

Lines changed: 85 additions & 22 deletions

File tree

Makefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
.PHONY: test-frontend
2+
test-frontend: lint-frontend
3+
cd web && npm run test:unit
4+
15
.PHONY: install-frontend
26
install-frontend:
37
cd web && npm install
@@ -39,7 +43,7 @@ start-backend:
3943
go run ./cmd/plugin-backend.go -port='9002' -config-path='./web/dist' -static-path='./web/dist' -plugin-config-path='ct.yaml'
4044

4145
.PHONY: build-image
42-
build-image:
46+
build-image: test-frontend
4347
./scripts/build-image.sh
4448

4549
.PHONY: start-forward
@@ -51,7 +55,7 @@ export TAG?=latest
5155
IMAGE=quay.io/${REGISTRY_ORG}/troubleshooting-panel-console-plugin:${TAG}
5256

5357
.PHONY: deploy
54-
deploy: lint-frontend ## Build and push image, reinstall on cluster using helm.
58+
deploy: test-frontend ## Build and push image, reinstall on cluster using helm.
5559
helm uninstall troubleshooting-panel-console-plugin -n troubleshooting-panel-console-plugin || true
5660
PUSH=1 scripts/build-image.sh
5761
helm install troubleshooting-panel-console-plugin charts/openshift-console-plugin -n troubleshooting-panel-console-plugin --create-namespace --set plugin.image=$(IMAGE)

web/jest.config.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
/* eslint-disable no-undef */
22
module.exports = {
33
preset: 'ts-jest',
4-
testEnvironment: 'node',
4+
testEnvironment: 'jsdom', // Needed for tests that use localStorage
55
transform: {
6-
'\\.[jt]sx?$': 'ts-jest',
6+
// Supress annoying test warning about "esModuleInterop", we don't want to set this true.
7+
'\\.[jt]sx?$': ['ts-jest', { diagnostics: { ignoreCodes: ['TS151001'] } }],
78
},
89
transformIgnorePatterns: ['<rootDir>/node_modules/(?!(@openshift-console|@patternfly))'],
910
coverageDirectory: '<rootDir>/coverage/cov-jest',

web/src/__tests__/k8s-node.spec.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
/**
2-
* @jest-environment jsdom
3-
*/
4-
51
import { K8sNode } from '../korrel8r/k8s';
62

73
beforeAll(() => {

web/src/__tests__/metric-node.spec.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { WrongDomainError } from '../korrel8r/korrel8r.types';
12
import { MetricNode } from '../korrel8r/metric';
23

34
/**
@@ -26,26 +27,26 @@ describe('Test MetricNode Parsing', () => {
2627
expect(MetricNode.fromQuery(expectedQuery)?.toURL()).toEqual(url);
2728
});
2829

29-
it('Test url to query parsing with expected errors', () => {
30+
describe('Test url to query parsing with expected errors', () => {
3031
[
3132
{
32-
url: 'monitoring/query-browse',
33-
expected: 'Expected metric URL: monitoring/query-browse',
33+
url: 'foobar',
34+
expected: new WrongDomainError('Expected metric URL: foobar'),
3435
},
3536
{
3637
url: 'monitoring/query-browser',
37-
expected: 'Expected query parameters in metric URL:',
38+
expected: 'Invalid metric URL',
3839
},
3940
{
4041
url: 'monitoring/query-browser?',
41-
expected: 'Expected query parameters in metric URL:',
42+
expected: 'Invalid metric URL',
4243
},
4344
{
4445
url: 'monitoring/query-browser?query1=wrong_query',
45-
expected: 'Expected to find query0',
46+
expected: 'Invalid metric URL',
4647
},
4748
].forEach(({ url, expected }) => {
48-
expect(() => MetricNode.fromURL(url)).toThrow(expected);
49+
it(`converts from ${url}`, () => expect(() => MetricNode.fromURL(url)).toThrow(expected));
4950
});
5051
});
5152

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { Korrel8rNodeFactory } from '../korrel8r/node-factory';
2+
3+
beforeAll(() => {
4+
// Mock API discovery resources.
5+
const resources = {
6+
consoleVersion: 'x.y.z',
7+
models: [
8+
{
9+
kind: 'Pod',
10+
apiVersion: 'v1',
11+
path: 'pods',
12+
verbs: ['watch'],
13+
},
14+
],
15+
};
16+
localStorage.setItem('bridge/api-discovery-resources', JSON.stringify(resources));
17+
window['SERVER_FLAGS'] = { consoleVersion: 'x.y.z' };
18+
});
19+
20+
const testdata = [
21+
{
22+
query:
23+
'alert:alert:{"alertname":"KubePodCrashLooping","container":"bad-deployment","namespace":"default","pod":"bad-deployment"}',
24+
url: 'monitoring/alerts?alerts=alertname%3DKubePodCrashLooping%2Ccontainer%3Dbad-deployment%2Cnamespace%3Ddefault%2Cpod%3Dbad-deployment',
25+
},
26+
{
27+
query: 'k8s:Pod.v1.:{"namespace":"default","name":"bad-deployment-000000000-00000"}',
28+
url: 'k8s/ns/default/pods/bad-deployment-000000000-00000',
29+
},
30+
{
31+
query: 'netflow:network:{SrcK8S_Type="Pod",SrcK8S_Namespace="myNamespace"}',
32+
url: `netflow-traffic?tenant=network&filters=${encodeURIComponent(
33+
'src_kind=Pod;src_namespace=myNamespace',
34+
)}`,
35+
},
36+
{
37+
url:
38+
`observe/traces?name=platform&namespace=openshift-tracing&tenant=platform&` +
39+
`q=${encodeURIComponent('{resource.service.name = "article-service"}')}`,
40+
query: `trace:trace:{resource.service.name = "article-service"}`,
41+
},
42+
{
43+
url: `monitoring/logs?q=${encodeURIComponent(
44+
'{kubernetes_namespace_name="default",log_type="infrastructure"}|json',
45+
)}&tenant=infrastructure`,
46+
query:
47+
'log:infrastructure:{kubernetes_namespace_name="default",log_type="infrastructure"}|json',
48+
},
49+
];
50+
51+
describe('Korrel8rNodeFactory.fromURL', () => {
52+
it.each(testdata)('converts $url', ({ url, query }) =>
53+
expect(Korrel8rNodeFactory.fromURL(url).toQuery()).toEqual(query),
54+
);
55+
});
56+
57+
describe('Korrel8rNodeFactory.fromQuery', () => {
58+
it.each(testdata)('converts $query', ({ url, query }) => {
59+
expect(Korrel8rNodeFactory.fromQuery(query).toURL()).toEqual(url);
60+
});
61+
});

web/src/__tests__/trace-node.spec.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import { TraceNode } from '../korrel8r/trace';
22

33
const roundtrip = [
44
{
5-
url: `observe/traces?name=platform&namespace=openshift-tracing&tenant=platform&
6-
q=${encodeURIComponent('{resource.service.name = "article-service"}')}`,
5+
url:
6+
`observe/traces?name=platform&namespace=openshift-tracing&tenant=platform&` +
7+
`q=${encodeURIComponent('{resource.service.name = "article-service"}')}`,
78
query: `trace:trace:{resource.service.name = "article-service"}`,
89
},
910
];
@@ -22,7 +23,8 @@ describe('TraceNode.fromQuery', () => {
2223
it('Query => URL => Query', () => {
2324
const query = 'trace:trace:{resource.service.name="shop-backend"}';
2425
const expectedKorrel8rURL =
25-
'observe/traces?name=platform&namespace=openshift-tracing&tenant=platform&q=%7Bresource.service.name%3D%22shop-backend%22%7D';
26+
'observe/traces?name=platform&namespace=openshift-tracing&tenant=platform&' +
27+
'q=%7Bresource.service.name%3D%22shop-backend%22%7D';
2628
const actualKorrel8rURL = TraceNode.fromQuery(query)?.toURL();
2729
expect(actualKorrel8rURL).toEqual(expectedKorrel8rURL);
2830
expect(TraceNode.fromURL(actualKorrel8rURL)?.toQuery()).toEqual(query);

web/src/korrel8r/metric.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ export class MetricNode extends Korrel8rNode {
1313

1414
static fromURL(url: string): Korrel8rNode {
1515
const [, params] = parseURL('metric', 'monitoring/query-browser', url);
16-
if (!params || params.size == 0)
17-
throw new NodeError(`Expected query parameters in metric URL: ${url}`);
1816
const promqlQuery = params.get('query0');
19-
if (!promqlQuery) throw new NodeError('Expected to find query0');
17+
if (!promqlQuery) throw new NodeError('Invalid metric URL: ${url}');
2018
const query = `metric:metric:${promqlQuery}`;
2119

2220
return new MetricNode(url, query);

web/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@
1313
"include": [
1414
"src"
1515
]
16-
}
16+
}

0 commit comments

Comments
 (0)