diff --git a/packages/solarwinds-apm/package.json b/packages/solarwinds-apm/package.json index 607a56e9..25f39c7b 100644 --- a/packages/solarwinds-apm/package.json +++ b/packages/solarwinds-apm/package.json @@ -41,7 +41,8 @@ "build": "tsc", "lint": "prettier --check . && eslint . --max-warnings=0", "lint:fix": "eslint --fix . && prettier --write .", - "publish": "node ../../scripts/publish.js" + "publish": "node ../../scripts/publish.js", + "test": "swtest -p test/tsconfig.json -c src" }, "dependencies": { "@opentelemetry/auto-instrumentations-node": "^0.39.0", @@ -74,6 +75,7 @@ "devDependencies": { "@opentelemetry/api": "1.4.x", "@solarwinds-apm/eslint-config": "workspace:^", + "@solarwinds-apm/test": "workspace:^", "@types/node": "^16.0.0", "eslint": "^8.47.0", "json5": "^2.2.3", diff --git a/packages/solarwinds-apm/test/config.test.ts b/packages/solarwinds-apm/test/config.test.ts new file mode 100644 index 00000000..8ca7ff8a --- /dev/null +++ b/packages/solarwinds-apm/test/config.test.ts @@ -0,0 +1,77 @@ +import { DiagLogLevel } from "@opentelemetry/api" +import { ExtendedSwConfiguration, readConfig } from "../src/config" +import { describe, it, expect, beforeEach } from "@solarwinds-apm/test" +import { oboe } from "@solarwinds-apm/bindings" + +describe("readConfig", () => { + beforeEach(() => { + for (const key of Object.keys(process.env)) { + if (key.startsWith("SW_APM_")) Reflect.deleteProperty(process.env, key) + } + process.env.SW_APM_SERVICE_KEY = "token:name" + }) + + it("returns proper defaults", () => { + const config = readConfig() + const expected: ExtendedSwConfiguration = { + token: "token", + serviceName: "name", + enabled: true, + otelLogLevel: DiagLogLevel.INFO, + oboeLogLevel: oboe.DEBUG_INFO, + triggerTraceEnabled: true, + runtimeMetrics: true, + insertTraceContextIntoLogs: false, + insertTraceContextIntoQueries: false, + } + + expect(config).to.deep.include(expected) + }) + + it("parses booleans", () => { + process.env.SW_APM_ENABLED = "0" + + const config = readConfig() + expect(config).to.include({ enabled: false }) + }) + + it("parses tracing mode", () => { + process.env.SW_APM_TRACING_MODE = "enabled" + + const config = readConfig() + expect(config).to.include({ tracingMode: true }) + }) + + it("parses trusted path", () => { + process.env.SW_APM_TRUSTED_PATH = "package.json" + + const config = readConfig() + expect(config.certificate).to.include("solarwinds-apm") + }) + + it("parses transaction settings", () => { + process.env.SW_APM_CONFIG_FILE = "test/test.config.js" + + const config = readConfig() + expect(config.transactionSettings).not.to.be.undefined + expect(config.transactionSettings).to.have.length(3) + }) + + it("throws on bad boolean", () => { + process.env.SW_APM_ENABLED = "foo" + + expect(readConfig).to.throw() + }) + + it("throws on bad tracing mode", () => { + process.env.SW_APM_TRACING_MODE = "foo" + + expect(readConfig).to.throw() + }) + + it("throws on non-existent trusted path", () => { + process.env.SW_APM_TRUSTED_PATH = "foo" + + expect(readConfig).to.throw() + }) +}) diff --git a/packages/solarwinds-apm/test/test.config.js b/packages/solarwinds-apm/test/test.config.js new file mode 100644 index 00000000..acf7e94c --- /dev/null +++ b/packages/solarwinds-apm/test/test.config.js @@ -0,0 +1,7 @@ +module.exports = { + transactionSettings: [ + { tracing: "enabled", regex: /^hello$/ }, + { tracing: "disabled", regex: "[A-Z]" }, + { tracing: "enabled", matcher: (ident) => ident.startsWith("foo") }, + ], +} diff --git a/packages/solarwinds-apm/test/tsconfig.json b/packages/solarwinds-apm/test/tsconfig.json new file mode 100644 index 00000000..73d600c7 --- /dev/null +++ b/packages/solarwinds-apm/test/tsconfig.json @@ -0,0 +1,4 @@ +{ + "extends": "../../../tsconfig.base.test.json", + "references": [{ "path": "../tsconfig.json" }] +}