Skip to content

Commit

Permalink
add more unit test to test cluster_manager role
Browse files Browse the repository at this point in the history
Signed-off-by: Anan Zhuang <[email protected]>
  • Loading branch information
ananzh committed May 24, 2022
1 parent ad55ab4 commit 0ae26de
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 5 deletions.
40 changes: 39 additions & 1 deletion test/acceptance/sniff.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ test('Should update the connection pool', (t) => {
});

// run the sniffer
// TODO: modify node roles when master is not supported
// TODO: remove this test when master is not supported
client.transport.sniff((err, hosts) => {
t.error(err);
t.equal(hosts.length, 4);
Expand Down Expand Up @@ -99,6 +99,44 @@ test('Should update the connection pool', (t) => {

t.equal(client.connectionPool.size, 4);
});

// run the sniffer
client.transport.sniff((err, hosts) => {
t.error(err);
t.equal(hosts.length, 4);

const ids = Object.keys(nodes);
for (let i = 0; i < hosts.length; i++) {
const id = ids[i];
// the first node will be an update of the existing one
if (id === 'node0') {
t.same(hosts[i], {
url: new URL(nodes[id].url),
id: id,
roles: {
cluster_manager: true,
data: true,
ingest: true,
},
});
} else {
t.same(hosts[i], {
url: new URL(nodes[id].url),
id: id,
roles: {
cluster_manager: true,
data: true,
ingest: true,
},
ssl: null,
agent: null,
proxy: null,
});
}
}

t.equal(client.connectionPool.size, 4);
});
t.teardown(shutdown);
});
});
Expand Down
45 changes: 44 additions & 1 deletion test/unit/base-connection-pool.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,8 @@ test('API', (t) => {
t.ok(pool.connections.find((c) => c.id === 'a2').roles !== null);
});

t.test('Should not update existing connections (same url, different id)', (t) => {
// TODO: remove this test when master is not supported
t.test('Should not update existing connections (master, same url, different id)', (t) => {
t.plan(3);
class CustomBaseConnectionPool extends BaseConnectionPool {
markAlive(connection) {
Expand Down Expand Up @@ -463,6 +464,48 @@ test('API', (t) => {
);
});

t.test('Should not update existing connections (cluster_manager, same url, different id)', (t) => {
t.plan(3);
class CustomBaseConnectionPool extends BaseConnectionPool {
markAlive(connection) {
t.ok('called');
super.markAlive(connection);
}
}
const pool = new CustomBaseConnectionPool({ Connection });
pool.addConnection([
{
url: new URL('http://127.0.0.1:9200'),
id: 'http://127.0.0.1:9200/',
roles: {
cluster_manager: true,
data: true,
ingest: true,
},
},
]);

pool.update([
{
url: new URL('http://127.0.0.1:9200'),
id: 'a1',
roles: true,
},
]);

// roles will never be updated, we only use it to do
// a dummy check to see if the connection has been updated
t.same(pool.connections.find((c) => c.id === 'a1').roles, {
cluster_manager: true,
data: true,
ingest: true,
});
t.equal(
pool.connections.find((c) => c.id === 'http://127.0.0.1:9200/'),
undefined
);
});

t.test('Add a new connection', (t) => {
t.plan(2);
const pool = new BaseConnectionPool({ Connection });
Expand Down
46 changes: 44 additions & 2 deletions test/unit/connection-pool.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,8 @@ test('API', (t) => {
t.ok(pool.connections.find((c) => c.id === 'a2').roles !== null);
});

t.test('Should not update existing connections (same url, different id)', (t) => {
// remove this test when master is not supported
t.test('Should not update existing connections (master, same url, different id)', (t) => {
t.plan(3);
class CustomConnectionPool extends ConnectionPool {
markAlive(connection) {
Expand Down Expand Up @@ -705,6 +706,48 @@ test('API', (t) => {
);
});

t.test('Should not update existing connections (cluster_manager, same url, different id)', (t) => {
t.plan(3);
class CustomConnectionPool extends ConnectionPool {
markAlive(connection) {
t.ok('called');
super.markAlive(connection);
}
}
const pool = new CustomConnectionPool({ Connection });
pool.addConnection([
{
url: new URL('http://127.0.0.1:9200'),
id: 'http://127.0.0.1:9200/',
roles: {
cluster_manager: true,
data: true,
ingest: true,
},
},
]);

pool.update([
{
url: new URL('http://127.0.0.1:9200'),
id: 'a1',
roles: true,
},
]);

// roles will never be updated, we only use it to do
// a dummy check to see if the connection has been updated
t.same(pool.connections.find((c) => c.id === 'a1').roles, {
cluster_manager: true,
data: true,
ingest: true,
});
t.equal(
pool.connections.find((c) => c.id === 'http://127.0.0.1:9200/'),
undefined
);
});

t.test('Add a new connection', (t) => {
t.plan(2);
const pool = new ConnectionPool({ Connection });
Expand Down Expand Up @@ -817,7 +860,6 @@ test('Node selector', (t) => {
t.end();
});

// TODO: modify node roles when master is not supported
test('Node filter', (t) => {
t.test('default', (t) => {
t.plan(1);
Expand Down
42 changes: 41 additions & 1 deletion test/unit/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,6 @@ test('should set cluster_manager role', (t) => {
t.end();
});

// master role is deprecated
// TODO: delete this test when maste role is completed removed
test('should set master role', (t) => {
t.test('Update the value of a role', (t) => {
Expand Down Expand Up @@ -810,6 +809,47 @@ test('should set master role', (t) => {
t.end();
});

test('should set cluster_manager role', (t) => {
t.test('Update the value of a role', (t) => {
t.plan(2);

const connection = new Connection({
url: new URL('http://localhost:9200'),
});

t.same(connection.roles, {
data: true,
ingest: true,
});

connection.setRole('cluster_manager', false);

t.same(connection.roles, {
cluster_manager: false,
data: true,
ingest: true,
});
});

t.test('Invalid value', (t) => {
t.plan(2);

const connection = new Connection({
url: new URL('http://localhost:9200'),
});

try {
connection.setRole('cluster_manager', 1);
t.fail('Shoud throw');
} catch (err) {
t.ok(err instanceof ConfigurationError);
t.equal(err.message, 'enabled should be a boolean');
}
});

t.end();
});

test('Util.inspect Connection class should hide agent, ssl and auth', (t) => {
t.plan(1);

Expand Down

0 comments on commit 0ae26de

Please sign in to comment.