Skip to content

Commit f7c2ce7

Browse files
committed
fix: adding dns lookup supported by node 8
1 parent 7792633 commit f7c2ce7

File tree

1 file changed

+72
-31
lines changed

1 file changed

+72
-31
lines changed

plugins/node/opentelemetry-plugin-dns/test/integrations/dnspromise-lookup.test.ts

Lines changed: 72 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,23 @@ const logger = new NoopLogger();
3333
const provider = new NodeTracerProvider({ logger });
3434
provider.addSpanProcessor(new SimpleSpanProcessor(memoryExporter));
3535

36+
async function lookupPromise(hostname:string, options:dns.LookupOptions = {}): Promise<any>{
37+
return new Promise((resolve, reject) => {
38+
dns.lookup(hostname, options,(err, address, family) => {
39+
if(err) reject(err);
40+
if (options.all){
41+
resolve(address);
42+
}
43+
else{
44+
resolve({ address, family });
45+
}
46+
});
47+
});
48+
};
49+
3650
describe('dns.promises.lookup()', () => {
3751
before(function (done) {
38-
// skip tests if node version is not supported
39-
if (semver.lte(process.versions.node, '10.6.0')) {
40-
this.skip();
41-
}
42-
52+
4353
// if node version is supported, it's mandatory for CI
4454
if (process.env.CI) {
4555
plugin.enable(dns, provider, provider.logger);
@@ -69,9 +79,9 @@ describe('dns.promises.lookup()', () => {
6979
[4, 6].forEach(ipversion => {
7080
it(`should export a valid span with "family" arg to ${ipversion}`, async () => {
7181
const hostname = 'google.com';
72-
const { address, family } = await dns.promises.lookup(
82+
const {address, family} = await lookupPromise(
7383
hostname,
74-
ipversion
84+
{family:ipversion}
7585
);
7686
assert.ok(address);
7787
assert.ok(family);
@@ -87,7 +97,9 @@ describe('dns.promises.lookup()', () => {
8797
describe('with no options param', () => {
8898
it('should export a valid span', async () => {
8999
const hostname = 'google.com';
90-
const { address, family } = await dns.promises.lookup(hostname);
100+
const { address, family } = await lookupPromise(
101+
hostname
102+
);
91103

92104
assert.ok(address);
93105
assert.ok(family);
@@ -101,7 +113,9 @@ describe('dns.promises.lookup()', () => {
101113
it('should export a valid span with error NOT_FOUND', async () => {
102114
const hostname = 'ᚕ';
103115
try {
104-
await dns.promises.lookup(hostname);
116+
await lookupPromise(
117+
hostname
118+
);
105119
assert.fail();
106120
} catch (error) {
107121
const spans = memoryExporter.getFinishedSpans();
@@ -122,52 +136,79 @@ describe('dns.promises.lookup()', () => {
122136
it('should export a valid span with error INVALID_ARGUMENT when "family" param is equal to -1', async () => {
123137
const hostname = 'google.com';
124138
try {
125-
await dns.promises.lookup(hostname, -1);
139+
await lookupPromise(hostname, {family: -1});
126140
assert.fail();
127141
} catch (error) {
128142
const spans = memoryExporter.getFinishedSpans();
129143
const [span] = spans;
130144

131145
assert.strictEqual(spans.length, 1);
132-
assertSpan(span, {
133-
addresses: [],
134-
hostname,
135-
forceStatus: {
136-
code: CanonicalCode.INVALID_ARGUMENT,
137-
message: error!.message,
138-
},
139-
});
146+
if (semver.lt(process.versions.node, '9.0.0')) {
147+
assertSpan(span, {
148+
addresses: [],
149+
// tslint:disable-next-line:no-any
150+
hostname: hostname as any,
151+
forceStatus: {
152+
code: CanonicalCode.UNKNOWN,
153+
message: error!.message,
154+
},
155+
});
156+
}
157+
else{
158+
assertSpan(span, {
159+
addresses: [],
160+
// tslint:disable-next-line:no-any
161+
hostname: hostname as any,
162+
forceStatus: {
163+
code: CanonicalCode.INVALID_ARGUMENT,
164+
message: error!.message,
165+
},
166+
});
167+
}
140168
}
141169
});
142170

143171
it('should export a valid span with error INVALID_ARGUMENT when "hostname" param is a number', async () => {
144172
const hostname = 1234;
145173
try {
146174
// tslint:disable-next-line:no-any
147-
await dns.promises.lookup(hostname as any, 4);
175+
await lookupPromise(hostname as any, {family: 4});
148176
assert.fail();
149177
} catch (error) {
150178
const spans = memoryExporter.getFinishedSpans();
151179
const [span] = spans;
152180

153181
assert.strictEqual(spans.length, 1);
154-
assertSpan(span, {
155-
addresses: [],
156-
// tslint:disable-next-line:no-any
157-
hostname: hostname as any,
158-
forceStatus: {
159-
code: CanonicalCode.INVALID_ARGUMENT,
160-
message: error!.message,
161-
},
162-
});
182+
if (semver.lt(process.versions.node, '9.0.0')) {
183+
assertSpan(span, {
184+
addresses: [],
185+
// tslint:disable-next-line:no-any
186+
hostname: hostname as any,
187+
forceStatus: {
188+
code: CanonicalCode.UNKNOWN,
189+
message: error!.message,
190+
},
191+
});
192+
}
193+
else{
194+
assertSpan(span, {
195+
addresses: [],
196+
// tslint:disable-next-line:no-any
197+
hostname: hostname as any,
198+
forceStatus: {
199+
code: CanonicalCode.INVALID_ARGUMENT,
200+
message: error!.message,
201+
},
202+
});
203+
}
163204
}
164205
});
165206
});
166207
describe('with options param', () => {
167208
[4, 6].forEach(ipversion => {
168209
it(`should export a valid span with "family" to ${ipversion}`, async () => {
169210
const hostname = 'google.com';
170-
const { address, family } = await dns.promises.lookup(hostname, {
211+
const { address, family } = await lookupPromise(hostname, {
171212
family: ipversion,
172213
});
173214

@@ -183,7 +224,7 @@ describe('dns.promises.lookup()', () => {
183224

184225
it(`should export a valid span when setting "verbatim" property to true and "family" to ${ipversion}`, async () => {
185226
const hostname = 'google.com';
186-
const { address, family } = await dns.promises.lookup(hostname, {
227+
const { address, family } = await lookupPromise(hostname, {
187228
family: ipversion,
188229
verbatim: true,
189230
});
@@ -201,7 +242,7 @@ describe('dns.promises.lookup()', () => {
201242

202243
it('should export a valid span when setting "all" property to true', async () => {
203244
const hostname = 'montreal.ca';
204-
const addresses = await dns.promises.lookup(hostname, { all: true });
245+
const addresses = await lookupPromise(hostname, { all: true });
205246

206247
assert.ok(addresses instanceof Array);
207248

0 commit comments

Comments
 (0)