Skip to content

Commit 15ed85e

Browse files
committed
tests: fix IPv6 functional tests with IPv4 host
Functional tests for IPv6 connections use a wrappper over the core Node.js "dns.lookup()" API. This utility operates by resolving a hostname to its corresponding IPv6 address. By default, the test harness injects a hostname based on the value of the MYSQLX_HOST environment variable, or by omission "localhost". Although "localhost" works fine, assigning an IPv4 address to the MYSQLX_HOST is also a common practice, and "dns.lookup()" is incapable to resolve an IPv4 address to its corresponding IPv6 address. This causes the IPv6 connection tests to fail. This patch addresses this issue and updates the test suite alongside the IPv6 address resolution utility to be able to work with both common hostnames and IPv4 addresses. Change-Id: I1037e6e3914b9dadfa1d7ba3d8433117a602db8d
1 parent 92c5d1a commit 15ed85e

File tree

6 files changed

+49
-41
lines changed

6 files changed

+49
-41
lines changed

test/fixtures/getIPv4Address.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, Oracle and/or its affiliates.
2+
* Copyright (c) 2021, 2022, Oracle and/or its affiliates.
33
*
44
* This program is free software; you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License, version 2.0, as
@@ -30,17 +30,12 @@
3030

3131
'use strict';
3232

33-
const dns = require('dns');
33+
const dns = require('dns').promises;
3434

35-
module.exports = function (host) {
36-
return new Promise(resolve => {
37-
dns.lookup(host, { family: 4 }, (err, address) => {
38-
if (err) {
39-
// ignore errors
40-
return resolve();
41-
}
42-
43-
resolve(address);
44-
});
45-
});
35+
module.exports = function ({ host } = {}) {
36+
return dns.lookup(host, { family: 4 })
37+
.then(({ address }) => address)
38+
// Ultimately, if the IPv4 address cannot be obtained, it should be
39+
// "undefined".
40+
.catch(() => undefined);
4641
};

test/fixtures/getIPv6Address.js

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2021, Oracle and/or its affiliates.
2+
* Copyright (c) 2020, 2022, Oracle and/or its affiliates.
33
*
44
* This program is free software; you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License, version 2.0, as
@@ -30,17 +30,30 @@
3030

3131
'use strict';
3232

33-
const dns = require('dns');
33+
const dns = require('dns').promises;
34+
const getIPv4Address = require('./getIPv4Address');
3435

35-
module.exports = function (host) {
36-
return new Promise(resolve => {
37-
dns.lookup(host, { family: 6 }, (err, address) => {
38-
if (err) {
39-
// ignore errors
40-
return resolve();
41-
}
42-
43-
resolve(address);
44-
});
45-
});
36+
module.exports = function ({ host, port } = {}) {
37+
// The host can be a common name, IPv4 or IPv6 address.
38+
// "dns.lookup()"" does not work with IP addresses, so we first need to
39+
// ensure we use the resolved common name instead.
40+
// So, we can start by retrieving the IPv4 address.
41+
return getIPv4Address({ host })
42+
.then(address => {
43+
// Then we use the IPv4 address to obtain the common name.
44+
return dns.lookupService(address, port);
45+
})
46+
.then(({ hostname }) => {
47+
// We can then use the common name to finally obtain any available
48+
// IPv6 address. Of course, if the original host is already
49+
// provided as a common name, there is one extra step involved,
50+
// but, at least, we tried our best to obtain an address.
51+
return dns.lookup(hostname, { family: 6 });
52+
})
53+
.then(({ address }) => {
54+
return address;
55+
})
56+
// Ultimately, if the IPv6 address cannot be obtained, it should be
57+
// "undefined".
58+
.catch(() => undefined);
4659
};

test/functional/default/connection/dns-srv.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2021, Oracle and/or its affiliates.
2+
* Copyright (c) 2020, 2022, Oracle and/or its affiliates.
33
*
44
* This program is free software; you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License, version 2.0, as
@@ -78,7 +78,7 @@ describe('connecting to the MySQL server using DNS SRV', () => {
7878
}];
7979

8080
beforeEach('setup fake DNS server in the current host', () => {
81-
return fixtures.getIPv4Address(os.hostname())
81+
return fixtures.getIPv4Address({ host: os.hostname() })
8282
.then(host => {
8383
return fixtures.createRecordServer({ host, service: baseConfig.host, records });
8484
})
@@ -121,7 +121,7 @@ describe('connecting to the MySQL server using DNS SRV', () => {
121121
let originalServers, fakeServer;
122122

123123
beforeEach('setup fake DNS server in the current host', () => {
124-
return fixtures.getIPv4Address(os.hostname())
124+
return fixtures.getIPv4Address({ host: os.hostname() })
125125
.then(host => {
126126
return fixtures.createRecordServer({ host, service: baseConfig.host, records: [] });
127127
})

test/functional/default/connection/ipv6.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2021, Oracle and/or its affiliates.
2+
* Copyright (c) 2020, 2022, Oracle and/or its affiliates.
33
*
44
* This program is free software; you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License, version 2.0, as
@@ -43,7 +43,7 @@ describe('connecting to the MySQL server using IPv6', () => {
4343
const baseConfig = { schema: undefined, socket: undefined };
4444

4545
beforeEach('resolve IPv6 address', () => {
46-
return fixtures.getIPv6Address(config.host)
46+
return fixtures.getIPv6Address({ host: config.host, port: config.port })
4747
.then(address => {
4848
host = address;
4949
});

test/functional/extended/connection/dns-srv.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, Oracle and/or its affiliates.
2+
* Copyright (c) 2021, 2022, Oracle and/or its affiliates.
33
*
44
* This program is free software; you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License, version 2.0, as
@@ -49,7 +49,7 @@ describe('connecting to the MySQL server using DNS SRV', () => {
4949

5050
return mysqlx.getSession(`mysqlx+srv://${srvConfig.user}:${srvConfig.password}@${srvConfig.host}`)
5151
.then(session => {
52-
return fixtures.getIPv4Address(session.inspect().host)
52+
return fixtures.getIPv4Address({ host: session.inspect().host })
5353
.then(address => {
5454
expect(address).to.equal(endpoint.address);
5555
return session.close();
@@ -71,7 +71,7 @@ describe('connecting to the MySQL server using DNS SRV', () => {
7171

7272
return mysqlx.getSession(srvConfig)
7373
.then(session => {
74-
return fixtures.getIPv4Address(session.inspect().host)
74+
return fixtures.getIPv4Address({ host: session.inspect().host })
7575
.then(address => {
7676
expect(address).to.equal(endpoints[0].address);
7777
return session.close();
@@ -84,7 +84,7 @@ describe('connecting to the MySQL server using DNS SRV', () => {
8484
return mysqlx.getSession(srvConfig);
8585
})
8686
.then(session => {
87-
return fixtures.getIPv4Address(session.inspect().host)
87+
return fixtures.getIPv4Address({ host: session.inspect().host })
8888
.then(address => {
8989
expect(address).to.equal(endpoints[1].address);
9090
return session.close();
@@ -97,7 +97,7 @@ describe('connecting to the MySQL server using DNS SRV', () => {
9797
return mysqlx.getSession(srvConfig);
9898
})
9999
.then(session => {
100-
return fixtures.getIPv4Address(session.inspect().host)
100+
return fixtures.getIPv4Address({ host: session.inspect().host })
101101
.then(address => {
102102
expect(address).to.equal(endpoints[0].address);
103103
return session.close();
@@ -124,7 +124,7 @@ describe('connecting to the MySQL server using DNS SRV', () => {
124124

125125
return pool.getSession()
126126
.then(session => {
127-
return fixtures.getIPv4Address(session.inspect().host);
127+
return fixtures.getIPv4Address({ host: session.inspect().host });
128128
})
129129
.then(address => {
130130
expect(address).to.equal(endpoints[0].address);
@@ -136,7 +136,7 @@ describe('connecting to the MySQL server using DNS SRV', () => {
136136
return pool.getSession();
137137
})
138138
.then(session => {
139-
return fixtures.getIPv4Address(session.inspect().host);
139+
return fixtures.getIPv4Address({ host: session.inspect().host });
140140
})
141141
.then(address => {
142142
expect(address).to.equal(endpoints[1].address);
@@ -148,7 +148,7 @@ describe('connecting to the MySQL server using DNS SRV', () => {
148148
return pool.getSession();
149149
})
150150
.then(session => {
151-
return fixtures.getIPv4Address(session.inspect().host);
151+
return fixtures.getIPv4Address({ host: session.inspect().host });
152152
})
153153
.then(address => {
154154
expect(address).to.equal(endpoints[0].address);

test/functional/extended/connection/failures.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2021, Oracle and/or its affiliates.
2+
* Copyright (c) 2020, 2022, Oracle and/or its affiliates.
33
*
44
* This program is free software; you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License, version 2.0, as
@@ -198,7 +198,7 @@ describe('connection failures', () => {
198198
return expect.fail();
199199
})
200200
.catch(err => {
201-
return fixtures.getIPv4Address(serverShutdownConfig.host)
201+
return fixtures.getIPv4Address({ host: serverShutdownConfig.host })
202202
.then(address => {
203203
expect(err.code).to.equal('ECONNREFUSED');
204204
expect(err.address).to.equal(address);

0 commit comments

Comments
 (0)