summaryrefslogtreecommitdiff
path: root/test/suites/remote_redis_unix.bash
blob: 601e17c5c2d1f2c4a30ed677dd7cbb0343144622 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
SUITE_remote_redis_unix_PROBE() {
    if ! $CCACHE --version | fgrep -q -- redis-storage &> /dev/null; then
        echo "redis-storage not available"
        return
    fi
    if ! command -v redis-server &> /dev/null; then
        echo "redis-server not found"
        return
    fi
    if redis-server --unixsocket /foo/redis.sock 2>&1 | grep -q "FATAL CONFIG FILE ERROR"; then
        # "Bad directive or wrong number of arguments"
        echo "redis-server without unixsocket"
        return
    fi
    if ! command -v redis-cli &> /dev/null; then
        echo "redis-cli not found"
        return
    fi
    if ! redis-cli -s /foo/redis.sock --version &> /dev/null; then
        # "Unrecognized option or bad number of args"
        echo "redis-cli without socket"
        return
    fi
}

start_redis_unix_server() {
    local socket="$1"
    local password="${2:-}"

    redis-server --bind localhost --unixsocket "${socket}" --port 0 >/dev/null &
    # Wait for server start.
    i=0
    while [ $i -lt 100 ] && ! redis-cli -s "${socket}" ping &>/dev/null; do
        sleep 0.1
        i=$((i + 1))
    done

    if [ -n "${password}" ]; then
        redis-cli -s "${socket}" config set requirepass "${password}" &>/dev/null
    fi
}

SUITE_remote_redis_unix_SETUP() {
    unset CCACHE_NODIRECT

    generate_code 1 test.c
}

expect_number_of_redis_unix_cache_entries() {
    local expected=$1
    local socket=$2
    local actual

    actual=$(redis-cli -s "$socket" keys "ccache:*" 2>/dev/null | wc -l)
    if [ "$actual" -ne "$expected" ]; then
        test_failed_internal "Found $actual (expected $expected) entries in $socket"
    fi
}

SUITE_remote_redis_unix() {
    # -------------------------------------------------------------------------
    TEST "Base case"

    socket=$(mktemp)
    redis_url="redis+unix:${socket}"
    export CCACHE_REMOTE_STORAGE="${redis_url}"

    start_redis_unix_server "${socket}"

    $CCACHE_COMPILE -c test.c
    expect_stat direct_cache_hit 0
    expect_stat cache_miss 1
    expect_stat files_in_cache 2
    expect_number_of_redis_unix_cache_entries 2 "${socket}" # result + manifest

    $CCACHE_COMPILE -c test.c
    expect_stat direct_cache_hit 1
    expect_stat cache_miss 1
    expect_stat files_in_cache 2
    expect_number_of_redis_unix_cache_entries 2 "${socket}" # result + manifest

    $CCACHE -C >/dev/null
    expect_stat files_in_cache 0
    expect_number_of_redis_unix_cache_entries 2 "${socket}" # result + manifest

    $CCACHE_COMPILE -c test.c
    expect_stat direct_cache_hit 2
    expect_stat cache_miss 1
    expect_stat files_in_cache 2 # fetched from remote
    expect_number_of_redis_unix_cache_entries 2 "${socket}" # result + manifest

    # -------------------------------------------------------------------------
    TEST "Password"

    socket=$(mktemp)
    password=secret123
    redis_url="redis+unix://${password}@localhost${socket}"
    export CCACHE_REMOTE_STORAGE="${redis_url}"

    start_redis_unix_server "${socket}" "${password}"

    CCACHE_DEBUG=1 $CCACHE_COMPILE -c test.c
    expect_stat direct_cache_hit 0
    expect_stat cache_miss 1
    expect_stat files_in_cache 2
    expect_number_of_redis_unix_cache_entries 2 "${socket}" # result + manifest
    expect_not_contains test.o.*.ccache-log "${password}"

    $CCACHE_COMPILE -c test.c
    expect_stat direct_cache_hit 1
    expect_stat cache_miss 1
    expect_stat files_in_cache 2
    expect_number_of_redis_unix_cache_entries 2 "${socket}" # result + manifest

    $CCACHE -C >/dev/null
    expect_stat files_in_cache 0
    expect_number_of_redis_unix_cache_entries 2 "${socket}" # result + manifest

    $CCACHE_COMPILE -c test.c
    expect_stat direct_cache_hit 2
    expect_stat cache_miss 1
    expect_stat files_in_cache 2 # fetched from remote
    expect_number_of_redis_unix_cache_entries 2 "${socket}" # result + manifest

    # -------------------------------------------------------------------------
    TEST "Unreachable server"

    export CCACHE_REMOTE_STORAGE="redis+unix:///foo"

    $CCACHE_COMPILE -c test.c
    expect_stat direct_cache_hit 0
    expect_stat cache_miss 1
    expect_stat files_in_cache 2
    expect_stat remote_storage_error 1
}