-
Notifications
You must be signed in to change notification settings - Fork 545
/
Copy pathutils.test.ts
179 lines (160 loc) · 5.3 KB
/
utils.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* 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 { diag, Span, SpanStatusCode } from '@opentelemetry/api';
import * as assert from 'assert';
import * as sinon from 'sinon';
import { AttributeNames } from '../../src/enums/AttributeNames';
import { IgnoreMatcher } from '../../src/types';
import * as utils from '../../src/utils';
describe('Utility', () => {
describe('satisfiesPattern()', () => {
it('string pattern', () => {
const answer1 = utils.satisfiesPattern('localhost', 'localhost');
assert.strictEqual(answer1, true);
const answer2 = utils.satisfiesPattern('hostname', 'localhost');
assert.strictEqual(answer2, false);
});
it('regex pattern', () => {
const answer1 = utils.satisfiesPattern('LocalHost', /localhost/i);
assert.strictEqual(answer1, true);
const answer2 = utils.satisfiesPattern('Montreal.ca', /montreal.ca/);
assert.strictEqual(answer2, false);
});
it('should throw if type is unknown', () => {
try {
utils.satisfiesPattern('google.com', true as unknown as IgnoreMatcher);
assert.fail();
} catch (error) {
assert.strictEqual(error instanceof TypeError, true);
}
});
it('function pattern', () => {
const answer1 = utils.satisfiesPattern(
'montreal.ca',
(url: string) => url === 'montreal.ca'
);
assert.strictEqual(answer1, true);
const answer2 = utils.satisfiesPattern(
'montreal.ca',
(url: string) => url !== 'montreal.ca'
);
assert.strictEqual(answer2, false);
});
});
describe('isIgnored()', () => {
let satisfiesPatternStub: sinon.SinonSpy<[string, IgnoreMatcher], boolean>;
beforeEach(() => {
satisfiesPatternStub = sinon.spy(utils, 'satisfiesPattern');
});
afterEach(() => {
satisfiesPatternStub.restore();
});
it('should call isSatisfyPattern, n match', () => {
const answer1 = utils.isIgnored('localhost', ['test']);
assert.strictEqual(answer1, false);
assert.strictEqual(
(utils.satisfiesPattern as sinon.SinonSpy).callCount,
1
);
});
it('should call isSatisfyPattern, match for function', () => {
satisfiesPatternStub.restore();
const answer1 = utils.isIgnored('api.montreal.ca', [
url => url.endsWith('montreal.ca'),
]);
assert.strictEqual(answer1, true);
});
it('should call isSatisfyPattern, match for a single matcher', () => {
const answer1 = utils.isIgnored('api.montreal.ca', url =>
url.endsWith('montreal.ca')
);
assert.strictEqual(answer1, true);
});
it('should not re-throw when function throws an exception', () => {
satisfiesPatternStub.restore();
const onException = (e: Error) => {
diag.error('error', e);
};
for (const callback of [undefined, onException]) {
assert.doesNotThrow(() =>
utils.isIgnored(
'test',
[
url => {
throw new Error('test');
},
],
callback
)
);
}
});
it('should call onException when function throws an exception', () => {
satisfiesPatternStub.restore();
const onException = sinon.spy();
assert.doesNotThrow(() =>
utils.isIgnored(
'test',
[
url => {
throw new Error('test');
},
],
onException
)
);
assert.strictEqual((onException as sinon.SinonSpy).callCount, 1);
});
it('should not call isSatisfyPattern', () => {
utils.isIgnored('test', []);
assert.strictEqual(
(utils.satisfiesPattern as sinon.SinonSpy).callCount,
0
);
});
it('should return false on empty list', () => {
const answer1 = utils.isIgnored('test', []);
assert.strictEqual(answer1, false);
});
it('should not throw and return false when list is undefined', () => {
const answer2 = utils.isIgnored('test', undefined);
assert.strictEqual(answer2, false);
});
});
describe('setError()', () => {
it('should have error attributes', () => {
// Prepare
const errorMessage = 'test error';
const span = {
setAttributes(attrs) {},
setStatus(status) {},
} as Span;
const mockSpan = sinon.mock(span);
mockSpan.expects('setAttributes').withExactArgs({
[AttributeNames.DNS_ERROR_NAME]: 'Error',
[AttributeNames.DNS_ERROR_MESSAGE]: errorMessage,
});
mockSpan.expects('setStatus').withExactArgs({
code: SpanStatusCode.ERROR,
message: errorMessage,
});
// Act
utils.setError(new Error(errorMessage), span);
// Assert
mockSpan.verify();
});
});
});