Skip to content

Commit c7ab1eb

Browse files
committed
Run some unit tests with new instrumentation
1 parent cb28dd8 commit c7ab1eb

File tree

9 files changed

+59
-14
lines changed

9 files changed

+59
-14
lines changed

.github/workflows/unit-test.yml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,19 @@ jobs:
6464
strategy:
6565
fail-fast: false
6666
matrix:
67-
node-version: [16.x, 18.x, 20.x, 22.x, 24.x]
67+
include:
68+
- node-version: 16.x
69+
new-instrumentation: false
70+
- node-version: 18.x
71+
new-instrumentation: false
72+
- node-version: 20.x
73+
new-instrumentation: false
74+
- node-version: 22.x
75+
new-instrumentation: false
76+
- node-version: 24.x
77+
new-instrumentation: false
78+
- node-version: 24.x
79+
new-instrumentation: true
6880
timeout-minutes: 10
6981
steps:
7082
- uses: actions/checkout@v4
@@ -93,6 +105,9 @@ jobs:
93105
- run: npm run install-lib-only
94106
- run: npm run build
95107
- run: npm run test:ci
108+
if: ${{ matrix.new-instrumentation == 'false' }}
109+
- run: npm run test:ci:new
110+
if: ${{ matrix.new-instrumentation == 'true' }}
96111
- name: "Upload coverage"
97112
uses: codecov/codecov-action@0565863a31f2c772f9f0395002a31e3f06189574 # v5
98113
with:

library/agent/Agent.test.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { Context } from "./Context";
1919
import { createTestAgent } from "../helpers/createTestAgent";
2020
import { setTimeout } from "node:timers/promises";
2121
import type { Response } from "./api/fetchBlockedLists";
22+
import { isNewInstrumentationUnitTest } from "../helpers/isNewInstrumentationUnitTest";
2223

2324
let shouldOnlyAllowSomeIPAddresses = false;
2425

@@ -123,11 +124,18 @@ t.test("it sends started event", async (t) => {
123124
},
124125
]);
125126

126-
t.same(logger.getMessages(), [
127-
"Starting agent v0.0.0...",
128-
"Found token, reporting enabled!",
129-
"mongodb@6.16.0 is supported!",
130-
]);
127+
if (!isNewInstrumentationUnitTest()) {
128+
t.same(logger.getMessages(), [
129+
"Starting agent v0.0.0...",
130+
"Found token, reporting enabled!",
131+
"mongodb@6.16.0 is supported!",
132+
]);
133+
} else {
134+
t.same(logger.getMessages(), [
135+
"Starting agent v0.0.0...",
136+
"Found token, reporting enabled!",
137+
]);
138+
}
131139
});
132140

133141
t.test("it throws error if already started", async () => {

library/agent/Agent.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ import { wrapInstalledPackages } from "./wrapInstalledPackages";
2525
import { Wrapper } from "./Wrapper";
2626
import { isAikidoCI } from "../helpers/isAikidoCI";
2727
import { AttackLogger } from "./AttackLogger";
28-
import { envToBool } from "../helpers/envToBool";
2928
import { Packages } from "./Packages";
3029
import { AIStatistics } from "./AIStatistics";
30+
import { isNewInstrumentationUnitTest } from "../helpers/isNewInstrumentationUnitTest";
3131

3232
type WrappedPackage = { version: string | null; supported: boolean };
3333

@@ -76,7 +76,7 @@ export class Agent {
7676
throw new Error("Serverless cannot be an empty string");
7777
}
7878

79-
if (envToBool(process.env.AIKIDO_TEST_NEW_INSTRUMENTATION)) {
79+
if (isNewInstrumentationUnitTest()) {
8080
this.newInstrumentation = true;
8181
}
8282
}

library/agent/hooks/instrumentation/codeTransformation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import type { PackageFileInstrumentationInstructionJSON } from "./types";
22
// eslint-disable-next-line camelcase
33
import { wasm_transform_code_str } from "./wasm/node_code_instrumentation";
44
import { getSourceType, PackageLoadFormat } from "./getSourceType";
5-
import { envToBool } from "../../../helpers/envToBool";
65
import { join } from "path";
6+
import { isNewInstrumentationUnitTest } from "../../../helpers/isNewInstrumentationUnitTest";
77

88
export function transformCode(
99
pkgName: string,
@@ -26,7 +26,7 @@ export function transformCode(
2626
}
2727

2828
// Rewrite import path for unit tests if environment variable is set to true
29-
if (envToBool(process.env.AIKIDO_TEST_NEW_INSTRUMENTATION)) {
29+
if (isNewInstrumentationUnitTest()) {
3030
return result.replace(
3131
"@aikidosec/firewall/instrument/internals",
3232
join(__dirname, "injectedFunctions.ts")
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { envToBool } from "./envToBool";
2+
3+
export function isNewInstrumentationUnitTest(): boolean {
4+
return envToBool(process.env.AIKIDO_TEST_NEW_INSTRUMENTATION);
5+
}

library/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ if (!isNewHookSystemUsed()) {
2424
console.warn(
2525
"AIKIDO: Your application seems to be running in ESM mode. You need to use the new hook system to enable AIKIDO. Please refer to the documentation for more information."
2626
);
27-
require("./agent/protect").protect();
2827
}
28+
require("./agent/protect").protect();
2929
}
3030
}
3131

library/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@
136136
"test": "node ../scripts/run-tap.js",
137137
"test:ci": "CI=true node ../scripts/run-tap.js",
138138
"test:new": "node ../scripts/run-tap.js --test-new-instrumentation",
139+
"test:ci:new": "node ../scripts/run-tap.js --ci --test-new-instrumentation",
139140
"build": "tsc -p tsconfig.build.json",
140141
"build:watch": "tsc --watch -p tsconfig.build.json",
141142
"lint": "npm run lint-eslint && npm run lint-tsc && npm run check-format",

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"test": "cd library && npm run test",
1515
"test:ci": "cd library && npm run test:ci",
1616
"test:new": "cd library && npm run test:new",
17+
"test:ci:new": "cd library && npm run test:ci:new",
1718
"lint": "cd library && npm run lint",
1819
"end2end": "cd end2end && npm run test",
1920
"format": "prettier --write .",

scripts/run-tap.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ const { execSync } = require("child_process");
22

33
const version = process.versions.node.split(".");
44
const major = parseInt(version[0], 10);
5-
const minor = parseInt(version[1], 10);
65

76
let args = "--allow-incomplete-coverage";
87

@@ -17,14 +16,30 @@ if (process.env.CI) {
1716

1817
if (process.argv.includes("--test-new-instrumentation")) {
1918
process.env.AIKIDO_TEST_NEW_INSTRUMENTATION = "true";
19+
20+
if (major < 22) {
21+
console.error(
22+
"Error:: --test-new-instrumentation is not supported on Node.js versions below 22."
23+
);
24+
process.exit(1);
25+
}
26+
27+
const excludedTestFilesForNewInstrumentation = [
28+
"**/sinks/**",
29+
"**/sources/**",
30+
];
31+
32+
for (const exclude of excludedTestFilesForNewInstrumentation) {
33+
args += ` --exclude='${exclude}'`;
34+
}
2035
}
2136

22-
execSync(`tap ${args}`, {
37+
execSync(`tap run ${args}`, {
2338
stdio: "inherit",
2439
env: {
2540
...process.env,
2641
AIKIDO_CI: "true",
27-
// In v23 some sub-dependencies are calling require on a esm module triggering an experimental warning
42+
// In v24 some sub-dependencies are calling require on a esm module triggering an experimental warning
2843
NODE_OPTIONS: major === 24 ? "--disable-warning=ExperimentalWarning" : "",
2944
},
3045
});

0 commit comments

Comments
 (0)