Skip to content

Commit

Permalink
chore(instrumentation-fs): consolidate common test utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
mhassan1 committed Feb 17, 2023
1 parent 8af53f5 commit 40d77e9
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 120 deletions.
61 changes: 2 additions & 59 deletions plugins/node/instrumentation-fs/test/fs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { context, trace, SpanStatusCode, SpanKind } from '@opentelemetry/api';
import { context, trace } from '@opentelemetry/api';
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
import {
BasicTracerProvider,
InMemorySpanExporter,
ReadableSpan,
SimpleSpanProcessor,
} from '@opentelemetry/sdk-trace-base';
import * as assert from 'assert';
Expand All @@ -34,6 +33,7 @@ import {
SYNC_FUNCTIONS,
} from '../src/constants';
import { indexFs, splitTwoLevels } from '../src/utils';
import { assertSpans, makeRootSpanName } from './utils';

const TEST_ATTRIBUTE = 'test.attr';
const TEST_VALUE = 'test.attr.value';
Expand Down Expand Up @@ -149,16 +149,6 @@ describe('fs instrumentation', () => {
]);
});
};
const makeRootSpanName = (name: FMember): string => {
let rsn: string;
if (Array.isArray(name)) {
rsn = `${name[0]}.${name[1]}`;
} else {
rsn = `${name}`;
}
rsn = `${rsn} test span`;
return rsn;
};

const callbackTest: TestCreator<FMember> = (
name: FMember,
Expand Down Expand Up @@ -443,50 +433,3 @@ describe('fs instrumentation', () => {
});
});
});

const assertSpans = (spans: ReadableSpan[], expected: any) => {
assert.strictEqual(
spans.length,
expected.length,
`Expected ${expected.length} spans, got ${spans.length}(${spans
.map((s: any) => `"${s.name}"`)
.join(', ')})`
);

spans.forEach((span, i) => {
assertSpan(span, expected[i]);
});
};

const assertSpan = (span: ReadableSpan, expected: any) => {
assert(span);
assert.strictEqual(span.name, expected.name);
assert.strictEqual(
span.kind,
SpanKind.INTERNAL,
'Expected to be of INTERNAL kind'
);
if (expected.parentSpan) {
assert.strictEqual(
span.parentSpanId,
expected.parentSpan.spanContext().spanId
);
}
if (expected.attributes) {
assert.deepEqual(span.attributes, expected.attributes);
}
if (expected.error) {
assert(
expected.error.test(span.status.message),
`Expected "${span.status.message}" to match ${expected.error}`
);
assert.strictEqual(span.status.code, SpanStatusCode.ERROR);
} else {
assert.strictEqual(
span.status.code,
SpanStatusCode.UNSET,
'Expected status to be unset'
);
assert.strictEqual(span.status.message, undefined);
}
};
64 changes: 3 additions & 61 deletions plugins/node/instrumentation-fs/test/fsPromises.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { context, trace, SpanStatusCode, SpanKind } from '@opentelemetry/api';
import { context, trace } from '@opentelemetry/api';
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
import {
BasicTracerProvider,
InMemorySpanExporter,
ReadableSpan,
SimpleSpanProcessor,
} from '@opentelemetry/sdk-trace-base';
import * as assert from 'assert';
import Instrumentation from '../src';
import * as sinon from 'sinon';
import type * as FSPromisesType from 'fs/promises';
import tests, { FsFunction, TestCase, TestCreator } from './definitions';
import type { FMember, FPMember, EndHook } from '../src/types';
import type { FPMember, EndHook } from '../src/types';
import { assertSpans, makeRootSpanName } from './utils';

const supportsPromises =
parseInt(process.versions.node.split('.')[0], 10) >= 14;
Expand Down Expand Up @@ -67,17 +67,6 @@ if (supportsPromises) {
context.disable();
});

const makeRootSpanName = (name: FMember): string => {
let rsn: string;
if (Array.isArray(name)) {
rsn = `${name[0]}.${name[1]}`;
} else {
rsn = `${name}`;
}
rsn = `${rsn} test span`;
return rsn;
};

const promiseTest: TestCreator<FPMember> = (
name: FPMember,
args,
Expand Down Expand Up @@ -162,50 +151,3 @@ if (supportsPromises) {
});
});
}

const assertSpans = (spans: ReadableSpan[], expected: any) => {
assert.strictEqual(
spans.length,
expected.length,
`Expected ${expected.length} spans, got ${spans.length}(${spans
.map((s: any) => `"${s.name}"`)
.join(', ')})`
);

spans.forEach((span, i) => {
assertSpan(span, expected[i]);
});
};

const assertSpan = (span: ReadableSpan, expected: any) => {
assert(span);
assert.strictEqual(span.name, expected.name);
assert.strictEqual(
span.kind,
SpanKind.INTERNAL,
'Expected to be of INTERNAL kind'
);
if (expected.parentSpan) {
assert.strictEqual(
span.parentSpanId,
expected.parentSpan.spanContext().spanId
);
}
if (expected.attributes) {
assert.deepEqual(span.attributes, expected.attributes);
}
if (expected.error) {
assert(
expected.error.test(span.status.message),
`Expected "${span.status.message}" to match ${expected.error}`
);
assert.strictEqual(span.status.code, SpanStatusCode.ERROR);
} else {
assert.strictEqual(
span.status.code,
SpanStatusCode.UNSET,
'Expected status to be unset'
);
assert.strictEqual(span.status.message, undefined);
}
};
77 changes: 77 additions & 0 deletions plugins/node/instrumentation-fs/test/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { ReadableSpan } from '@opentelemetry/sdk-trace-base';
import * as assert from 'assert';
import { SpanKind, SpanStatusCode } from '@opentelemetry/api';
import type { FMember } from '../src/types';

export const assertSpans = (spans: ReadableSpan[], expected: any) => {
assert.strictEqual(
spans.length,
expected.length,
`Expected ${expected.length} spans, got ${spans.length}(${spans
.map((s: any) => `"${s.name}"`)
.join(', ')})`
);

spans.forEach((span, i) => {
assertSpan(span, expected[i]);
});
};

const assertSpan = (span: ReadableSpan, expected: any) => {
assert(span);
assert.strictEqual(span.name, expected.name);
assert.strictEqual(
span.kind,
SpanKind.INTERNAL,
'Expected to be of INTERNAL kind'
);
if (expected.parentSpan) {
assert.strictEqual(
span.parentSpanId,
expected.parentSpan.spanContext().spanId
);
}
if (expected.attributes) {
assert.deepEqual(span.attributes, expected.attributes);
}
if (expected.error) {
assert(
expected.error.test(span.status.message),
`Expected "${span.status.message}" to match ${expected.error}`
);
assert.strictEqual(span.status.code, SpanStatusCode.ERROR);
} else {
assert.strictEqual(
span.status.code,
SpanStatusCode.UNSET,
'Expected status to be unset'
);
assert.strictEqual(span.status.message, undefined);
}
};

export const makeRootSpanName = (name: FMember): string => {
let rsn: string;
if (Array.isArray(name)) {
rsn = `${name[0]}.${name[1]}`;
} else {
rsn = `${name}`;
}
rsn = `${rsn} test span`;
return rsn;
};

0 comments on commit 40d77e9

Please sign in to comment.