summaryrefslogtreecommitdiffstats
path: root/tests/cli_tests/test_dsn.py
blob: 8c9528d41c855ac2d1b39f2e2421adee85c38a19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import os

import pexpect
from textwrap import dedent
import pytest


def test_using_dsn():
    config_content = dedent(
        """
        [alias_dsn]
        local = redis://localhost:6379/15
        """
    )
    with open("/tmp/iredisrc", "w+") as etc_config:
        etc_config.write(config_content)

    cli = pexpect.spawn("iredis --iredisrc /tmp/iredisrc --dsn local", timeout=1)
    cli.logfile_read = open("cli_test.log", "ab")
    cli.expect(["iredis", "localhost:6379[15]>"])
    cli.close()

    # overwrite with -n
    cli = pexpect.spawn("iredis --iredisrc /tmp/iredisrc --dsn local -n 3", timeout=1)
    cli.logfile_read = open("cli_test.log", "ab")
    cli.expect(["iredis", "localhost:6379[3]>"])
    cli.close()

    # dsn not exists
    cli = pexpect.spawn("iredis --iredisrc /tmp/iredisrc --dsn ghost-dsn", timeout=1)
    cli.expect(["Could not find the specified DSN in the config file."])
    cli.close()
    assert cli.status == 1


@pytest.mark.skipif(
    not os.path.exists("/tmp/redis/redis.sock"), reason="unix socket is not found"
)
def test_using_dsn_unix():
    config_content = dedent(
        """
        [alias_dsn]
        unix = unix:///tmp/redis/redis.sock?db=3
        """
    )
    with open("/tmp/iredisrc", "w+") as etc_config:
        etc_config.write(config_content)

    cli = pexpect.spawn("iredis --iredisrc /tmp/iredisrc --dsn unix", timeout=2)
    cli.logfile_read = open("cli_test.log", "ab")
    cli.expect(["iredis", "redis /tmp/redis/redis.sock[3]>"])

    cli.close()