Skip to content

Commit

Permalink
add basic config tests
Browse files Browse the repository at this point in the history
  • Loading branch information
raphael-theriault-swi committed Aug 31, 2023
1 parent 16dc71d commit 2f8c258
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/solarwinds-apm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
77 changes: 77 additions & 0 deletions packages/solarwinds-apm/test/config.test.ts
Original file line number Diff line number Diff line change
@@ -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()
})
})
7 changes: 7 additions & 0 deletions packages/solarwinds-apm/test/test.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
transactionSettings: [
{ tracing: "enabled", regex: /^hello$/ },
{ tracing: "disabled", regex: "[A-Z]" },
{ tracing: "enabled", matcher: (ident) => ident.startsWith("foo") },
],
}
4 changes: 4 additions & 0 deletions packages/solarwinds-apm/test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../../../tsconfig.base.test.json",
"references": [{ "path": "../tsconfig.json" }]
}

0 comments on commit 2f8c258

Please sign in to comment.