| 1 |
from textwrap import dedent |
| 2 |
|
| 3 |
from packaging.version import parse as version_parse # noqa: F401 |
| 4 |
import pexpect |
| 5 |
import pytest |
| 6 |
|
| 7 |
|
| 8 |
def test_start_on_connection_error(): |
| 9 |
cli = pexpect.spawn("iredis -p 12345", timeout=1) |
| 10 |
cli.logfile_read = open("cli_test.log", "ab") |
| 11 |
cli.expect(r"Error \d+ connecting to 127.0.0.1:12345. Connection refused.") |
| 12 |
cli.close() |
| 13 |
|
| 14 |
|
| 15 |
def test_start_with_client_name(): |
| 16 |
cli = pexpect.spawn("iredis --client_name custom_name", timeout=2) |
| 17 |
cli.expect("iredis") |
| 18 |
cli.sendline("CLIENT GETNAME") |
| 19 |
cli.expect("custom_name") |
| 20 |
cli.close() |
| 21 |
|
| 22 |
|
| 23 |
def test_short_help_option(config): |
| 24 |
c = pexpect.spawn("iredis -h", timeout=2) |
| 25 |
|
| 26 |
c.expect("Show this message and exit.") |
| 27 |
|
| 28 |
c = pexpect.spawn("iredis -h 127.0.0.1") |
| 29 |
c.expect("127.0.0.1:6379>") |
| 30 |
|
| 31 |
c.close() |
| 32 |
|
| 33 |
|
| 34 |
@pytest.mark.skipif("version_parse(os.environ['REDIS_VERSION']) != version_parse('5')") |
| 35 |
def test_server_version_in_starting_on5(): |
| 36 |
c = pexpect.spawn("iredis", timeout=2) |
| 37 |
c.expect("redis-server 5") |
| 38 |
c.close() |
| 39 |
|
| 40 |
|
| 41 |
@pytest.mark.skipif("version_parse(os.environ['REDIS_VERSION']) != version_parse('6')") |
| 42 |
def test_server_version_in_starting_on6(): |
| 43 |
c = pexpect.spawn("iredis", timeout=2) |
| 44 |
c.expect("redis-server 6") |
| 45 |
c.close() |
| 46 |
|
| 47 |
|
| 48 |
def test_connection_using_url(clean_redis): |
| 49 |
c = pexpect.spawn("iredis --url redis://localhost:6379/7", timeout=2) |
| 50 |
c.logfile_read = open("cli_test.log", "ab") |
| 51 |
c.expect(["iredis", "127.0.0.1:6379[7]>"]) |
| 52 |
c.sendline("set current-db 7") |
| 53 |
c.expect("OK") |
| 54 |
c.close() |
| 55 |
|
| 56 |
|
| 57 |
def test_connection_using_url_from_env(clean_redis, monkeypatch): |
| 58 |
monkeypatch.setenv("IREDIS_URL", "redis://localhost:6379/7") |
| 59 |
c = pexpect.spawn("iredis", timeout=2) |
| 60 |
c.logfile_read = open("cli_test.log", "ab") |
| 61 |
c.expect(["iredis", "localhost:6379[7]>"]) |
| 62 |
c.sendline("set current-db 7") |
| 63 |
c.expect("OK") |
| 64 |
c.close() |
| 65 |
|
| 66 |
|
| 67 |
@pytest.mark.xfail(reason="current test in github action, socket not supported.") |
| 68 |
# https://github.community/t5/GitHub-Actions/Job-service-command/td-p/33901# |
| 69 |
# https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idservices |
| 70 |
def test_connect_via_socket(fake_redis_socket): |
| 71 |
config_content = dedent( |
| 72 |
""" |
| 73 |
[main] |
| 74 |
log_location = /tmp/iredis1.log |
| 75 |
no_info=True |
| 76 |
""" |
| 77 |
) |
| 78 |
with open("/tmp/iredisrc", "w+") as etc_config: |
| 79 |
etc_config.write(config_content) |
| 80 |
|
| 81 |
c = pexpect.spawn("iredis --iredisrc /tmp/iredisrc -s /tmp/test.sock", timeout=2) |
| 82 |
c.logfile_read = open("cli_test.log", "ab") |
| 83 |
c.expect("redis /tmp/test.sock") |
| 84 |
|
| 85 |
c.close() |
| 86 |
|
| 87 |
|
| 88 |
def test_iredis_start_with_prompt(): |
| 89 |
cli = pexpect.spawn("iredis --prompt '{host}abc{port}def{client_name}'", timeout=2) |
| 90 |
cli.logfile_read = open("cli_test.log", "ab") |
| 91 |
cli.expect("iredis") |
| 92 |
cli.expect("127.0.0.1abc6379defNone") |
| 93 |
cli.close() |