From 38a18e50bf12bb40df710e99b0b0a76e7c258723 Mon Sep 17 00:00:00 2001 From: jjllee Date: Thu, 15 Aug 2024 14:28:39 -0700 Subject: [PATCH] require 95% test and function test coverage --- .../package.json | 1 + .../aws-opentelemetry-configurator.test.ts | 12 ++++++ .../test/register.test.ts | 40 +++++++++++++++++++ package.json | 1 + 4 files changed, 54 insertions(+) diff --git a/aws-distro-opentelemetry-node-autoinstrumentation/package.json b/aws-distro-opentelemetry-node-autoinstrumentation/package.json index 3e287f6..8765020 100644 --- a/aws-distro-opentelemetry-node-autoinstrumentation/package.json +++ b/aws-distro-opentelemetry-node-autoinstrumentation/package.json @@ -32,6 +32,7 @@ "prepublishOnly": "npm run compile", "tdd": "yarn test -- --watch-extensions ts --watch", "test": "nyc ts-mocha --timeout 10000 -p tsconfig.json --require '@opentelemetry/contrib-test-utils' 'test/**/*.ts'", + "test:coverage": "nyc --all --check-coverage --functions 95 --lines 95 ts-mocha --timeout 10000 -p tsconfig.json --require '@opentelemetry/contrib-test-utils' 'test/**/*.ts'", "watch": "tsc -w" }, "bugs": { diff --git a/aws-distro-opentelemetry-node-autoinstrumentation/test/aws-opentelemetry-configurator.test.ts b/aws-distro-opentelemetry-node-autoinstrumentation/test/aws-opentelemetry-configurator.test.ts index b22de06..a6081f7 100644 --- a/aws-distro-opentelemetry-node-autoinstrumentation/test/aws-opentelemetry-configurator.test.ts +++ b/aws-distro-opentelemetry-node-autoinstrumentation/test/aws-opentelemetry-configurator.test.ts @@ -53,6 +53,13 @@ describe('AwsOpenTelemetryConfiguratorTest', () => { awsOtelConfigurator = new AwsOpentelemetryConfigurator([]); }); + // Cleanup any span processors to avoid unit test conflicts + after(() => { + (awsOtelConfigurator as any).spanProcessors.forEach((spanProcessor: SpanProcessor) => { + spanProcessor.shutdown(); + }); + }); + // The probability of this passing once without correct IDs is low, 20 times is inconceivable. it('ProvideGenerateXrayIdsTest', () => { const tracerProvider: NodeTracerProvider = new NodeTracerProvider(awsOtelConfigurator.configure()); @@ -262,6 +269,11 @@ describe('AwsOpenTelemetryConfiguratorTest', () => { delete process.env.OTEL_METRIC_EXPORT_INTERVAL; delete process.env.OTEL_AWS_APPLICATION_SIGNALS_ENABLED; } + + // shut down exporters for test cleanup + spanProcessors.forEach(spanProcessor => { + spanProcessor.shutdown(); + }); }); it('ApplicationSignalsExporterProviderTest', () => { diff --git a/aws-distro-opentelemetry-node-autoinstrumentation/test/register.test.ts b/aws-distro-opentelemetry-node-autoinstrumentation/test/register.test.ts index 756b2d4..d4a2c2f 100644 --- a/aws-distro-opentelemetry-node-autoinstrumentation/test/register.test.ts +++ b/aws-distro-opentelemetry-node-autoinstrumentation/test/register.test.ts @@ -2,12 +2,52 @@ // SPDX-License-Identifier: Apache-2.0 // Modifications Copyright The OpenTelemetry Authors. Licensed under the Apache License 2.0 License. +import { NodeSDK } from '@opentelemetry/sdk-node'; import * as assert from 'assert'; import { spawnSync, SpawnSyncReturns } from 'child_process'; +import expect from 'expect'; +import { setAwsDefaultEnvironmentVariables } from '../src/register'; // The OpenTelemetry Authors code // Extend register.test.ts functionality to also test exported span with Application Signals enabled describe('Register', function () { + it('Requires without error', () => { + const originalPrototypeStart = NodeSDK.prototype.start + NodeSDK.prototype.start = () => {} + try { + require('../src/register'); + } catch(err: unknown) { + assert.fail(`require register unexpectedly failed: ${err}`); + } + + NodeSDK.prototype.start = originalPrototypeStart + }) + + it('Tests AWS Default Environment Variables', () => { + this.beforeEach(() => { + delete process.env.OTEL_EXPORTER_OTLP_PROTOCOL + delete process.env.OTEL_PROPAGATORS + delete process.env.OTEL_NODE_DISABLED_INSTRUMENTATIONS + }) + + it('sets AWS Default Environment Variables', () => { + setAwsDefaultEnvironmentVariables(); + expect(process.env.OTEL_EXPORTER_OTLP_PROTOCOL).toEqual('http/protobuf'); + expect(process.env.OTEL_PROPAGATORS).toEqual('xray,tracecontext,b3,b3multi'); + expect(process.env.OTEL_NODE_DISABLED_INSTRUMENTATIONS).toEqual('fs'); + }) + + it('Does not set AWS Default Environment Variables', () => { + process.env.OTEL_EXPORTER_OTLP_PROTOCOL = 'customProtocol'; + process.env.OTEL_PROPAGATORS = 'customPropagators'; + process.env.OTEL_NODE_DISABLED_INSTRUMENTATIONS = 'customDisabledInstrumentations'; + setAwsDefaultEnvironmentVariables(); + expect(process.env.OTEL_EXPORTER_OTLP_PROTOCOL).toEqual('customProtocol'); + expect(process.env.OTEL_PROPAGATORS).toEqual('customPropagators'); + expect(process.env.OTEL_NODE_DISABLED_INSTRUMENTATIONS).toEqual('customDisabledInstrumentations'); + }) + }); + it('can load auto instrumentation from command line', () => { const proc: SpawnSyncReturns = spawnSync( process.execPath, diff --git a/package.json b/package.json index 2786cb9..79312ed 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "compile": "lerna run compile", "prewatch": "npm run precompile", "test": "lerna run test", + "test:coverage": "lerna run test:coverage", "test:ci:changed": "lerna run test --since origin/main", "test:browser": "lerna run test:browser --concurrency 1", "test-all-versions": "npm run --if-present --workspaces test-all-versions",