1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-04 15:02:09 +03:00

fix(sentinel): Migrated to the new testing framework, fixed issues that were discovered during transition

* [CAE-342] Fix a couple of bugs

* Fixed issue with nodes masterauth persistency, changed docker container

* [CAE-342] Fixed a couple of sentinel issues, enabled most tests

* [CAE-342] Added comment

* [CAE-342] Migrate majority of tests to testUtils

* [CAE-342] Minor refactor

* .

* [CAE-342] Using cae containers for sentinel

* [CAE-342] Improved resiliency of the legacy tests, added TSdoc comment

* [CAE-342] Some extra logging, removed unneeded changes

* [CAE-342] Moved docker env as optional part of redisserverdockerconfig

* [CAE-342] Move password to serverArguments

* [CAE-342] Moved ts-node to devDependencies

* [CAE-342] Reverted legacy testing framework improvements
This commit is contained in:
Hristo Temelski
2025-04-30 15:56:29 +03:00
committed by GitHub
parent 048df302e4
commit 10ff6debab
9 changed files with 1459 additions and 1253 deletions

View File

@@ -6,8 +6,11 @@ import {
TypeMapping,
// CommandPolicies,
createClient,
createSentinel,
RedisClientOptions,
RedisClientType,
RedisSentinelOptions,
RedisSentinelType,
RedisPoolOptions,
RedisClientPoolType,
createClientPool,
@@ -15,7 +18,8 @@ import {
RedisClusterOptions,
RedisClusterType
} from '@redis/client/index';
import { RedisServerDockerConfig, spawnRedisServer, spawnRedisCluster } from './dockers';
import { RedisNode } from '@redis/client/lib/sentinel/types'
import { spawnRedisServer, spawnRedisCluster, spawnRedisSentinel, RedisServerDockerOptions } from './dockers';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
@@ -68,6 +72,24 @@ interface ClientTestOptions<
disableClientSetup?: boolean;
}
interface SentinelTestOptions<
M extends RedisModules,
F extends RedisFunctions,
S extends RedisScripts,
RESP extends RespVersions,
TYPE_MAPPING extends TypeMapping
> extends CommonTestOptions {
sentinelOptions?: Partial<RedisSentinelOptions<M, F, S, RESP, TYPE_MAPPING>>;
clientOptions?: Partial<RedisClientOptions<M, F, S, RESP, TYPE_MAPPING>>;
scripts?: S;
functions?: F;
modules?: M;
disableClientSetup?: boolean;
replicaPoolSize?: number;
masterPoolSize?: number;
reserveClient?: boolean;
}
interface ClientPoolTestOptions<
M extends RedisModules,
F extends RedisFunctions,
@@ -148,13 +170,14 @@ export default class TestUtils {
}
readonly #VERSION_NUMBERS: Array<number>;
readonly #DOCKER_IMAGE: RedisServerDockerConfig;
readonly #DOCKER_IMAGE: RedisServerDockerOptions;
constructor({ string, numbers }: Version, dockerImageName: string) {
this.#VERSION_NUMBERS = numbers;
this.#DOCKER_IMAGE = {
image: dockerImageName,
version: string
version: string,
mode: "server"
};
}
@@ -179,7 +202,6 @@ export default class TestUtils {
}
isVersionGreaterThanHook(minimumVersion: Array<number> | undefined): void {
const isVersionGreaterThanHook = this.isVersionGreaterThan.bind(this);
const versionNumber = this.#VERSION_NUMBERS.join('.');
const minimumVersionString = minimumVersion?.join('.');
@@ -272,6 +294,81 @@ export default class TestUtils {
});
}
testWithClientSentinel<
M extends RedisModules = {},
F extends RedisFunctions = {},
S extends RedisScripts = {},
RESP extends RespVersions = 2,
TYPE_MAPPING extends TypeMapping = {}
>(
title: string,
fn: (sentinel: RedisSentinelType<M, F, S, RESP, TYPE_MAPPING>) => unknown,
options: SentinelTestOptions<M, F, S, RESP, TYPE_MAPPING>
): void {
let dockerPromises: ReturnType<typeof spawnRedisSentinel>;
const passIndex = options.serverArguments.indexOf('--requirepass')+1;
let password: string | undefined = undefined;
if (passIndex != 0) {
password = options.serverArguments[passIndex];
}
if (this.isVersionGreaterThan(options.minimumDockerVersion)) {
const dockerImage = this.#DOCKER_IMAGE;
before(function () {
this.timeout(30000);
dockerPromises = spawnRedisSentinel(dockerImage, options.serverArguments);
return dockerPromises;
});
}
it(title, async function () {
this.timeout(30000);
if (options.skipTest) return this.skip();
if (!dockerPromises) return this.skip();
const promises = await dockerPromises;
const rootNodes: Array<RedisNode> = promises.map(promise => ({
host: "127.0.0.1",
port: promise.port
}));
const sentinel = createSentinel({
name: 'mymaster',
sentinelRootNodes: rootNodes,
nodeClientOptions: {
password: password || undefined,
},
sentinelClientOptions: {
password: password || undefined,
},
replicaPoolSize: options?.replicaPoolSize || 0,
scripts: options?.scripts || {},
modules: options?.modules || {},
functions: options?.functions || {},
masterPoolSize: options?.masterPoolSize || undefined,
reserveClient: options?.reserveClient || false,
}) as RedisSentinelType<M, F, S, RESP, TYPE_MAPPING>;
if (options.disableClientSetup) {
return fn(sentinel);
}
await sentinel.connect();
try {
await sentinel.flushAll();
await fn(sentinel);
} finally {
if (sentinel.isOpen) {
await sentinel.flushAll();
sentinel.destroy();
}
}
});
}
testWithClientIfVersionWithinRange<
M extends RedisModules = {},
F extends RedisFunctions = {},
@@ -290,9 +387,28 @@ export default class TestUtils {
} else {
console.warn(`Skipping test ${title} because server version ${this.#VERSION_NUMBERS.join('.')} is not within range ${range[0].join(".")} - ${range[1] !== 'LATEST' ? range[1].join(".") : 'LATEST'}`)
}
}
testWithClienSentineltIfVersionWithinRange<
M extends RedisModules = {},
F extends RedisFunctions = {},
S extends RedisScripts = {},
RESP extends RespVersions = 2,
TYPE_MAPPING extends TypeMapping = {}
>(
range: ([minVersion: Array<number>, maxVersion: Array<number>] | [minVersion: Array<number>, 'LATEST']),
title: string,
fn: (sentinel: RedisSentinelType<M, F, S, RESP, TYPE_MAPPING>) => unknown,
options: SentinelTestOptions<M, F, S, RESP, TYPE_MAPPING>
): void {
if (this.isVersionInRange(range[0], range[1] === 'LATEST' ? [Infinity, Infinity, Infinity] : range[1])) {
return this.testWithClientSentinel(`${title} [${range[0].join('.')}] - [${(range[1] === 'LATEST') ? range[1] : range[1].join(".")}] `, fn, options)
} else {
console.warn(`Skipping test ${title} because server version ${this.#VERSION_NUMBERS.join('.')} is not within range ${range[0].join(".")} - ${range[1] !== 'LATEST' ? range[1].join(".") : 'LATEST'}`)
}
}
testWithClientPool<
M extends RedisModules = {},
F extends RedisFunctions = {},