Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow endpoint specification for Python and Node test #1375

Merged
merged 2 commits into from May 12, 2024

Conversation

shohamazon
Copy link
Member

@shohamazon shohamazon commented May 2, 2024

Issue #, if available:

Description of changes:
This PR introduces the ability to specify endpoints when running tests for both Python and Node environments.

To run the tests in python using with external endpoints:
pytest --asyncio-mode=auto --cluster-endpoints=host:port,host2:port2 --standalone-endpoints=host1:port

In Node:
npm run test -- --cluster-endpoints=host:port,host2:port2 --standalone-endpoints=host1:port
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Copy link
Collaborator

@Yury-Fridlyand Yury-Fridlyand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want us to implement similar stuff for java, C# and go?

node/tests/RedisClient.test.ts Show resolved Hide resolved
beforeAll(async () => {
cluster = await RedisCluster.createCluster(true, 3, 0);
const args = process.argv.slice(2);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you reuse this piece of code by creating a function?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -71,8 +85,23 @@ def call_before_all_pytests(request):
"""
tls = request.config.getoption("--tls")
load_module = request.config.getoption("--load-module")
pytest.redis_cluster = RedisCluster(tls, True, load_module=load_module)
pytest.standalone_cluster = RedisCluster(tls, False, 1, 1, load_module=load_module)
endpoints = request.config.getoption("--cluster-endpoints")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename to cluster_endpoints

@shohamazon
Copy link
Member Author

Do you want us to implement similar stuff for java, C# and go?

Let's first wait for Bar's review for this pr and then decide what we should do

Copy link
Contributor

@barshaul barshaul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shohamazon Please keep this PR only for python changes, and open the node PR only after it will be approved.
Instead of having standalone endpoints and cluster endpoints, use single endpoints option and add cluster mode flag, as we do everywhere else. Also it will require passing TLS flag

@shohamazon shohamazon force-pushed the cluste_input branch 3 times, most recently from 24a04cc to 325a861 Compare May 8, 2024 11:06
endpoints = request.config.getoption("--cluster-endpoints")
standalone_endpoints = request.config.getoption("--standalone-endpoints")

if endpoints or standalone_endpoints:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a comment explaining the endpoints were passed by the caller, not creating clusters internally

pytest.redis_cluster = RedisCluster(tls=tls, addresses=endpoints)
if standalone_endpoints:
standalone_endpoints = [
endpoint.split(":") for endpoint in standalone_endpoints.split(",")
Copy link
Contributor

@barshaul barshaul May 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create an helper function parse_endpoints, and raise relevant error if the endpoints aren't parsable as they aren't in the right format


if endpoints or standalone_endpoints:
if endpoints:
endpoints = [endpoint.split(":") for endpoint in endpoints.split(",")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here also, endpoints -> cluster_endpoints

pytest.standalone_cluster = RedisCluster(
tls, addresses=standalone_endpoints
)
else:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add comment
// Not endpoints were provided, create new clusters

tls, True, load_module=load_module, addresses=endpoints
)
pytest.standalone_cluster = RedisCluster(
tls, False, 1, 1, load_module=load_module, addresses=standalone_endpoints
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add the filed names to make it more readable

@@ -93,13 +126,37 @@ def pytest_sessionfinish(session, exitstatus):
pass


def pytest_collection_modifyitems(config, items):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add documentation what are you doing in this function

):
if "cluster_mode" in item.fixturenames:
cluster_mode_value = item.callspec.params.get("cluster_mode", None)
if cluster_mode_value and not config.getoption("--cluster-endpoints"):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if cluster_mode_value is True

reason="Test skipped because cluster_mode=False"
)
)
elif not cluster_mode_value and not config.getoption(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

elif cluster_mode_value is False

if cluster_mode_value and not config.getoption("--cluster-endpoints"):
item.add_marker(
pytest.mark.skip(
reason="Test skipped because cluster_mode=False"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.. and cluster endpoints weren't provided

):
item.add_marker(
pytest.mark.skip(
reason="Test skipped because cluster_mode=True"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

@pytest.fixture()
async def redis_client(
request, cluster_mode: bool, protocol: ProtocolVersion
) -> AsyncGenerator[TRedisClient, None]:
"Get async socket client for tests"
client = await create_client(request, cluster_mode, protocol=protocol)
yield client
await flush_all_on_cluster(request, cluster_mode, protocol)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sure to put all flush related changes in a different commit

parser.addoption(
"--cluster-endpoints",
action="store",
help="Comma-separated list of cluster endpoints in the format host1:port1,host2:port2,...",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a note that the cluster will be flushed (both for standalone)

)
return await RedisClient.create(config)


async def flush_all_on_cluster(request, cluster_mode: bool, protocol: ProtocolVersion):
Copy link
Contributor

@barshaul barshaul May 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

flush_all_on_cluster -> teardown



async def flush_all_on_cluster(request, cluster_mode: bool, protocol: ProtocolVersion):
client = await create_client(request, cluster_mode, protocol=protocol, timeout=2000)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a comment why are we creating a new client

@@ -259,7 +259,8 @@ async def test_can_connect_with_auth_acl(
# Delete this user
await redis_client.custom_command(["ACL", "DELUSER", username])

async def test_select_standalone_database_id(self, request):
@pytest.mark.parametrize("cluster_mode", [False])
async def test_select_standalone_database_id(self, request, cluster_mode):
redis_client = await create_client(request, cluster_mode=False, database_id=4)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cluster_mode=False -> can be changed to cluster_mode

@@ -65,7 +69,16 @@ def parse_cluster_script_start_output(self, output: str):
nodes_list.append(NodeAddress(host, int(port)))
self.nodes_addr = nodes_list

def existing_cluster(self, addresses: List[List[str]]):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

existing_cluster -> init_from_existing_cluster

self.cluster_folder = ""
self.nodes_addr = []
for [host, port] in addresses:
print("host, port ", host, port)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

Comment on lines 80 to 84
if self.cluster_folder == "":
return
args_list = [sys.executable, SCRIPT_FILE]
if self.tls:
args_list.append("--tls")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if self.cluster_folder == "":
return
args_list = [sys.executable, SCRIPT_FILE]
if self.tls:
args_list.append("--tls")
if self.cluster_folder:
args_list = [sys.executable, SCRIPT_FILE]
if self.tls:
args_list.append("--tls")

@@ -50,12 +77,12 @@ describe("RedisClusterClient", () => {
});

const getOptions = (
ports: number[],
host_port: [string, number][],
Copy link
Contributor

@barshaul barshaul May 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

host_port -> address

@@ -40,6 +40,17 @@ export function flushallOnPort(port: number): Promise<void> {
);
}

export const parseEndpoints = (endpoints: string): [string, number][] => {
if (!endpoints.trim()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why? what does this scenario mean? if endpoints is an empty string, please add appropriate check

}

return endpoints.split(",").map((endpoint) => {
const [host, port] = endpoint.split(":");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handle errors and raise appropriate error if recieved format is bad


return endpoints.split(",").map((endpoint) => {
const [host, port] = endpoint.split(":");
return [host, Number(port)];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same - Number(port) can raise an error, make sure to handle those


private constructor(ports: number[], clusterFolder: string) {
this.usedPorts = ports;
private constructor(ports: [string, number][], clusterFolder?: string) {
Copy link
Contributor

@barshaul barshaul May 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename 'ports' to 'addresses'

private constructor(ports: number[], clusterFolder: string) {
this.usedPorts = ports;
private constructor(ports: [string, number][], clusterFolder?: string) {
this.address = ports;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't we get multiple addresses? change to this.addresses if so

.map((address) => address.split(":")[1])
.map((port) => Number(port));
.map((address) => address.split(":"))
.map((port) => [port[0], Number(port[1])]) as [string, number][];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.map((port) => [port[0], Number(port[1])]) as [string, number][];
.map((address) => [address[0], Number(address[1])]) as [string, number][];

@@ -295,23 +306,33 @@ export class RedisCluster {
});
}

public static connectToCluster(port: [string, number][]): RedisCluster {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public static connectToCluster(port: [string, number][]): RedisCluster {
public static connectToCluster(address: [string, number][]): RedisCluster {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we connecting to the cluster (e.g. creating a client), or just creating an cluster object ?
if not, change to initFromExistingCluster

},
),
);
if (this.clusterFolder !== "") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (this.clusterFolder) isn't good enough?

Copy link
Contributor

@barshaul barshaul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comments inline

Copy link
Contributor

@barshaul barshaul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix all before merging

@shohamazon shohamazon merged commit 1b960dc into aws:main May 12, 2024
12 checks passed
@shohamazon shohamazon deleted the cluste_input branch May 12, 2024 13:19
@@ -4,6 +4,7 @@

import { beforeAll, expect } from "@jest/globals";
import { exec } from "child_process";
import parseArgs from "minimist";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, update developer.md and/or package.json to include this module

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's updated, look at the pr

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, need to update dev doc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants