forked from 0xERR0R/blocky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis_test.go
175 lines (152 loc) · 4.96 KB
/
redis_test.go
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package e2e
import (
"context"
"strings"
. "github.com/0xERR0R/blocky/helpertest"
"github.com/0xERR0R/blocky/util"
"github.com/go-redis/redis/v8"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/testcontainers/testcontainers-go"
)
var _ = Describe("Redis configuration tests", func() {
var (
e2eNet *testcontainers.DockerNetwork
blocky1, blocky2, mokka testcontainers.Container
redisClient *redis.Client
err error
)
BeforeEach(func(ctx context.Context) {
e2eNet = getRandomNetwork(ctx)
redisDB, err := createRedisContainer(ctx, e2eNet)
Expect(err).Should(Succeed())
redisConnectionString, err := redisDB.ConnectionString(ctx)
Expect(err).Should(Succeed())
redisConnectionString = strings.ReplaceAll(redisConnectionString, "redis://", "")
redisClient = redis.NewClient(&redis.Options{
Addr: redisConnectionString,
})
Expect(dbSize(ctx, redisClient)).Should(BeNumerically("==", 0))
mokka, err = createDNSMokkaContainer(ctx, "moka1", e2eNet, `A google/NOERROR("A 1.2.3.4 123")`)
Expect(err).Should(Succeed())
})
Describe("Cache sharing between blocky instances", func() {
When("Redis and 2 blocky instances are configured", func() {
BeforeEach(func(ctx context.Context) {
blocky1, err = createBlockyContainer(ctx, e2eNet,
"log:",
" level: warn",
"upstreams:",
" groups:",
" default:",
" - moka1",
"redis:",
" address: redis:6379",
)
Expect(err).Should(Succeed())
blocky2, err = createBlockyContainer(ctx, e2eNet,
"log:",
" level: warn",
"upstreams:",
" groups:",
" default:",
" - moka1",
"redis:",
" address: redis:6379",
)
Expect(err).Should(Succeed())
})
It("2nd instance of blocky should use cache from redis", func(ctx context.Context) {
msg := util.NewMsgWithQuestion("google.de.", A)
By("Query first blocky instance, should store cache in redis", func() {
Eventually(doDNSRequest, "5s", "2ms").WithArguments(ctx, blocky1, msg).
Should(
SatisfyAll(
BeDNSRecord("google.de.", A, "1.2.3.4"),
HaveTTL(BeNumerically("==", 123)),
))
})
By("Check redis, must contain one cache entry", func() {
Eventually(dbSize, "5s", "2ms").WithArguments(ctx, redisClient).Should(BeNumerically("==", 1))
})
By("Shutdown the upstream DNS server", func() {
Expect(mokka.Terminate(ctx)).Should(Succeed())
})
By("Query second blocky instance, should use cache from redis", func() {
Eventually(doDNSRequest, "5s", "2ms").WithArguments(ctx, blocky2, msg).
Should(
SatisfyAll(
BeDNSRecord("google.de.", A, "1.2.3.4"),
HaveTTL(BeNumerically("<=", 123)),
))
})
By("No warnings/errors in log", func() {
Expect(getContainerLogs(ctx, blocky1)).Should(BeEmpty())
Expect(getContainerLogs(ctx, blocky2)).Should(BeEmpty())
})
})
})
})
Describe("Cache loading on startup", func() {
When("Redis and 1 blocky instance are configured", func() {
BeforeEach(func(ctx context.Context) {
blocky1, err = createBlockyContainer(ctx, e2eNet,
"log:",
" level: warn",
"upstreams:",
" groups:",
" default:",
" - moka1",
"redis:",
" address: redis:6379",
)
Expect(err).Should(Succeed())
})
It("should load cache from redis after start", func(ctx context.Context) {
msg := util.NewMsgWithQuestion("google.de.", A)
By("Query first blocky instance, should store cache in redis\"", func() {
Eventually(doDNSRequest, "5s", "2ms").WithArguments(ctx, blocky1, msg).
Should(
SatisfyAll(
BeDNSRecord("google.de.", A, "1.2.3.4"),
HaveTTL(BeNumerically("==", 123)),
))
})
By("Check redis, must contain one cache entry", func() {
Eventually(dbSize).WithArguments(ctx, redisClient).Should(BeNumerically("==", 1))
})
By("start other instance of blocky now -> it should load the cache from redis", func() {
blocky2, err = createBlockyContainer(ctx, e2eNet,
"log:",
" level: warn",
"upstreams:",
" groups:",
" default:",
" - moka1",
"redis:",
" address: redis:6379",
)
Expect(err).Should(Succeed())
})
By("Shutdown the upstream DNS server", func() {
Expect(mokka.Terminate(ctx)).Should(Succeed())
})
By("Query second blocky instance", func() {
Eventually(doDNSRequest, "5s", "2ms").WithArguments(ctx, blocky2, msg).
Should(
SatisfyAll(
BeDNSRecord("google.de.", A, "1.2.3.4"),
HaveTTL(BeNumerically("<=", 123)),
))
})
By("No warnings/errors in log", func() {
Expect(getContainerLogs(ctx, blocky1)).Should(BeEmpty())
Expect(getContainerLogs(ctx, blocky2)).Should(BeEmpty())
})
})
})
})
})
func dbSize(ctx context.Context, redisClient *redis.Client) (int64, error) {
return redisClient.DBSize(ctx).Result()
}