0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-11-24 00:17:37 +01:00
mongodb/jstests/sharding/proxy_protocol_connect.js
Matt Broadstone 771dabd098 SERVER-81339 Convert ReplSetTest and ShardingTest to modules (#26332)
GitOrigin-RevId: 744aa110a53786b23c62ff53f87a1418b5991e8d
2024-08-20 22:00:49 +00:00

57 lines
2.2 KiB
JavaScript

/**
* Validate we can connect over the proxy protocol port with the protocol appended.
* @tags: [
* requires_fcv_52,
* # TODO (SERVER-88125): Re-enable this test or add an explanation why it is incompatible.
* embedded_router_incompatible,
* ]
*/
if (_isWindows()) {
quit();
}
import {ProxyProtocolServer} from "jstests/sharding/libs/proxy_protocol.js";
import {ShardingTest} from "jstests/libs/shardingtest.js";
// Test that you can connect to the load balancer port over a proxy.
function testProxyProtocolConnect(ingressPort, egressPort, version) {
let proxy_server = new ProxyProtocolServer(ingressPort, egressPort, version);
proxy_server.start();
let st = new ShardingTest(
{shards: 1, mongos: 1, mongosOptions: {setParameter: {"loadBalancerPort": egressPort}}});
const uri = `mongodb://127.0.0.1:${ingressPort}/?loadBalanced=true`;
const conn = new Mongo(uri);
assert.neq(null, conn, 'Client was unable to connect to the load balancer port');
assert.commandWorked(conn.getDB('admin').runCommand({hello: 1}));
proxy_server.stop();
st.stop();
}
// Test that you can't connect to the load balancer port without being proxied.
function testProxyProtocolConnectFailure(lbPort, sendLoadBalanced) {
let st = new ShardingTest(
{shards: 1, mongos: 1, mongosOptions: {setParameter: {"loadBalancerPort": lbPort}}});
const hostName = st.s.host.substring(0, st.s.host.indexOf(":"));
const uri = `mongodb://${hostName}:${lbPort}/?loadBalanced=${sendLoadBalanced}`;
try {
new Mongo(uri);
assert(false, 'Client was unable to connect to the load balancer port');
} catch (err) {
assert(checkLog.checkContainsOnceJsonStringMatch(
st.s, 6067900, "msg", "Error while parsing proxy protocol header"),
"Connection failed for some reason other than lacking a proxy protocol header");
}
st.stop();
}
const ingressPort = allocatePort();
const egressPort = allocatePort();
testProxyProtocolConnect(ingressPort, egressPort, 1);
testProxyProtocolConnect(ingressPort, egressPort, 2);
testProxyProtocolConnectFailure(egressPort, "true");
testProxyProtocolConnectFailure(egressPort, "false");