Skip to content

Commit

Permalink
build(tests): WIP: small code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
0xERR0R committed Nov 17, 2022
1 parent 45c2fe5 commit 051d618
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 91 deletions.
6 changes: 1 addition & 5 deletions e2e/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,14 @@ var _ = Describe("Basic functional tests", func() {
It("Should start and answer DNS queries", func() {
msg := util.NewMsgWithQuestion("google.de.", dns.Type(dns.TypeA))

resp, err := doDNSRequest(blocky, msg)
Expect(err).Should(Succeed())

Expect(resp.Answer).Should(BeDNSRecord("google.de.", dns.TypeA, 123, "1.2.3.4"))
Expect(doDNSRequest(blocky, msg)).Should(BeDNSRecord("google.de.", dns.TypeA, 123, "1.2.3.4"))
})
It("should return 'healthy' container status (healthcheck)", func() {
Eventually(func(g Gomega) string {
state, err := blocky.State(context.Background())
g.Expect(err).NotTo(HaveOccurred())

return state.Health.Status
// TODO check HEALTHCHECK definition and reduce wait time
}, "2m", "1s").Should(Equal("healthy"))
})
})
Expand Down
12 changes: 3 additions & 9 deletions e2e/blocking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/testcontainers/testcontainers-go"
)

var _ = FDescribe("External lists and query blocking", func() {
var _ = Describe("External lists and query blocking", func() {
var blocky, httpServer, moka testcontainers.Container
var err error
BeforeEach(func() {
Expand Down Expand Up @@ -45,10 +45,7 @@ var _ = FDescribe("External lists and query blocking", func() {

msg := util.NewMsgWithQuestion("google.com.", dns.Type(dns.TypeA))

resp, err := doDNSRequest(blocky, msg)
Expect(err).Should(Succeed())

Expect(resp.Answer).Should(BeDNSRecord("google.com.", dns.TypeA, 123, "1.2.3.4"))
Expect(doDNSRequest(blocky, msg)).Should(BeDNSRecord("google.com.", dns.TypeA, 123, "1.2.3.4"))

Expect(getContainerLogs(blocky)).Should(ContainElement(ContainSubstring("error during file processing")))
})
Expand Down Expand Up @@ -111,10 +108,7 @@ var _ = FDescribe("External lists and query blocking", func() {
It("should download external list on startup and block queries", func() {
msg := util.NewMsgWithQuestion("blockeddomain.com.", dns.Type(dns.TypeA))

resp, err := doDNSRequest(blocky, msg)
Expect(err).Should(Succeed())

Expect(resp.Answer).Should(BeDNSRecord("blockeddomain.com.", dns.TypeA, 6*60*60, "0.0.0.0"))
Expect(doDNSRequest(blocky, msg)).Should(BeDNSRecord("blockeddomain.com.", dns.TypeA, 6*60*60, "0.0.0.0"))

Expect(getContainerLogs(blocky)).Should(BeEmpty())
})
Expand Down
15 changes: 11 additions & 4 deletions e2e/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ import (
//nolint:gochecknoglobals
var NetworkName = fmt.Sprintf("blocky-e2e-network_%d", time.Now().Unix())

const (
redisImage = "redis:7"
mokaImage = "ghcr.io/0xerr0r/dns-mokka:latest"
staticServerImage = "halverneus/static-file-server:latest"
blockyImage = "blocky-e2e"
)

func createDNSMokkaContainer(alias string, rules ...string) (testcontainers.Container, error) {
ctx := context.Background()

Expand All @@ -29,7 +36,7 @@ func createDNSMokkaContainer(alias string, rules ...string) (testcontainers.Cont
}

req := testcontainers.ContainerRequest{
Image: "ghcr.io/0xerr0r/dns-mokka:latest",
Image: mokaImage,
Networks: []string{NetworkName},
ExposedPorts: []string{"53/tcp", "53/udp"},
NetworkAliases: map[string][]string{NetworkName: {alias}},
Expand All @@ -56,7 +63,7 @@ func createHTTPServerContainer(tmpDir *helpertest.TmpFolder,

ctx := context.Background()
req := testcontainers.ContainerRequest{
Image: "halverneus/static-file-server:latest",
Image: staticServerImage,
Networks: []string{NetworkName},
NetworkAliases: map[string][]string{NetworkName: {"httpserver"}},

Expand All @@ -81,7 +88,7 @@ func createRedisContainer() (testcontainers.Container, error) {
ctx := context.Background()

req := testcontainers.ContainerRequest{
Image: "redis:7",
Image: redisImage,
Networks: []string{NetworkName},
ExposedPorts: []string{"6379/tcp"},
NetworkAliases: map[string][]string{NetworkName: {"redis"}},
Expand All @@ -108,7 +115,7 @@ func createBlockyContainer(tmpDir *helpertest.TmpFolder, lines ...string) (testc

ctx := context.Background()
req := testcontainers.ContainerRequest{
Image: "blocky-e2e",
Image: blockyImage,
Networks: []string{NetworkName},

ExposedPorts: []string{"53/tcp", "53/udp", "4000/tcp"},
Expand Down
131 changes: 64 additions & 67 deletions e2e/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/testcontainers/testcontainers-go"
)

var _ = FDescribe("Redis configuration tests", func() {
var _ = Describe("Redis configuration tests", func() {
var blocky1, blocky2, redisDB, moka testcontainers.Container
var redisClient *redis.Client
var err error
Expand All @@ -31,6 +31,8 @@ var _ = FDescribe("Redis configuration tests", func() {
Addr: fmt.Sprintf("localhost:%d", redisPort),
})

Expect(dbSize(redisClient)).Should(BeNumerically("==", 0))

moka, err = createDNSMokkaContainer("moka1", `A google/NOERROR("A 1.2.3.4 123")`)

Expect(err).Should(Succeed())
Expand All @@ -43,6 +45,7 @@ var _ = FDescribe("Redis configuration tests", func() {
When("Redis and 2 blocky instances are configured", func() {
BeforeEach(func() {
blocky1, err = createBlockyContainer(tmpDir,
"logLevel: warn",
"upstream:",
" default:",
" - moka1",
Expand All @@ -54,6 +57,7 @@ var _ = FDescribe("Redis configuration tests", func() {
DeferCleanup(blocky1.Terminate)

blocky2, err = createBlockyContainer(tmpDir,
"logLevel: warn",
"upstream:",
" default:",
" - moka1",
Expand All @@ -65,35 +69,27 @@ var _ = FDescribe("Redis configuration tests", func() {
DeferCleanup(blocky2.Terminate)
})
It("2nd instance of blocky should use cache from redis", func() {
// check data in redis
Expect(redisClient.DBSize(context.Background()).Result()).Should(BeNumerically("==", 0))

msg := util.NewMsgWithQuestion("google.de.", dns.Type(dns.TypeA))

// blocky1 will resolve this query and store the cache in redis
resp, err := doDNSRequest(blocky1, msg)
Expect(err).Should(Succeed())

// check data in redis
Eventually(func(g Gomega) int64 {
size, err := redisClient.DBSize(context.Background()).Result()
g.Expect(err).NotTo(HaveOccurred())

return size
}).Should(BeNumerically("==", 1))

Expect(resp.Answer).Should(BeDNSRecord("google.de.", dns.TypeA, 123, "1.2.3.4"))

// shut down the upstream DNS server
Expect(moka.Terminate(context.Background())).Should(Succeed())

// ask blocky2 now
// blocky2 will resolve this query and store the cache in redis
resp, err = doDNSRequest(blocky2, msg)
Expect(err).Should(Succeed())

Expect(resp.Answer).Should(BeDNSRecord("google.de.", dns.TypeA, 0, "1.2.3.4"))

By("Query first blocky instance, should store cache in redis", func() {
Expect(doDNSRequest(blocky1, msg)).Should(BeDNSRecord("google.de.", dns.TypeA, 123, "1.2.3.4"))
})

By("Check redis, must contain one cache entry", func() {
Eventually(dbSize).WithArguments(redisClient).Should(BeNumerically("==", 1))
})

By("Shutdown the upstream DNS server", func() {
Expect(moka.Terminate(context.Background())).Should(Succeed())
})

By("Query second blocky instance, should use cache from redis", func() {
Expect(doDNSRequest(blocky2, msg)).Should(BeDNSRecord("google.de.", dns.TypeA, 0, "1.2.3.4"))
})

By("No warnings/errors in log", func() {
Expect(getContainerLogs(blocky1)).Should(BeEmpty())
Expect(getContainerLogs(blocky2)).Should(BeEmpty())
})
})
})
})
Expand All @@ -102,6 +98,7 @@ var _ = FDescribe("Redis configuration tests", func() {
When("Redis and 1 blocky instance are configured", func() {
BeforeEach(func() {
blocky1, err = createBlockyContainer(tmpDir,
"logLevel: warn",
"upstream:",
" default:",
" - moka1",
Expand All @@ -113,46 +110,46 @@ var _ = FDescribe("Redis configuration tests", func() {
DeferCleanup(blocky1.Terminate)
})
It("should load cache from redis after start", func() {
// check data in redis
Expect(redisClient.DBSize(context.Background()).Result()).Should(BeNumerically("==", 0))

msg := util.NewMsgWithQuestion("google.de.", dns.Type(dns.TypeA))

// blocky1 will resolve this query and store the cache in redis
resp, err := doDNSRequest(blocky1, msg)
Expect(err).Should(Succeed())

// check data in redis
Eventually(func(g Gomega) int64 {
size, err := redisClient.DBSize(context.Background()).Result()
g.Expect(err).NotTo(HaveOccurred())

return size
}).Should(BeNumerically("==", 1))

Expect(resp.Answer).Should(BeDNSRecord("google.de.", dns.TypeA, 123, "1.2.3.4"))

// start other instance of blocky now -> it should load the cache from redis
blocky2, err = createBlockyContainer(tmpDir,
"upstream:",
" default:",
" - moka1",
"redis:",
" address: redis:6379",
)

// shut down the upstream DNS server
Expect(moka.Terminate(context.Background())).Should(Succeed())

Expect(err).Should(Succeed())
DeferCleanup(blocky2.Terminate)

// blocky should be able to resolve the query again
resp, err = doDNSRequest(blocky2, msg)
Expect(err).Should(Succeed())

Expect(resp.Answer).Should(BeDNSRecord("google.de.", dns.TypeA, 0, "1.2.3.4"))
By("Query first blocky instance, should store cache in redis\"", func() {
Expect(doDNSRequest(blocky1, msg)).Should(BeDNSRecord("google.de.", dns.TypeA, 123, "1.2.3.4"))
})

By("Check redis, must contain one cache entry", func() {
Eventually(dbSize).WithArguments(redisClient).Should(BeNumerically("==", 1))
})

By("start other instance of blocky now -> it should load the cache from redis", func() {
blocky2, err = createBlockyContainer(tmpDir,
"logLevel: warn",
"upstream:",
" default:",
" - moka1",
"redis:",
" address: redis:6379",
)

Expect(err).Should(Succeed())
DeferCleanup(blocky2.Terminate)
})

By("Shutdown the upstream DNS server", func() {
Expect(moka.Terminate(context.Background())).Should(Succeed())
})

By("Query second blocky instance", func() {
Expect(doDNSRequest(blocky2, msg)).Should(BeDNSRecord("google.de.", dns.TypeA, 0, "1.2.3.4"))
})

By("No warnings/errors in log", func() {
Expect(getContainerLogs(blocky1)).Should(BeEmpty())
Expect(getContainerLogs(blocky2)).Should(BeEmpty())
})
})
})
})
})

func dbSize(redisClient *redis.Client) (int64, error) {
return redisClient.DBSize(context.Background()).Result()
}
9 changes: 3 additions & 6 deletions e2e/upstream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ var _ = Describe("Upstream resolver configuration tests", func() {
DeferCleanup(blocky.Terminate)
})
It("should not start", func() {

// TODO check log message
Expect(blocky.IsRunning()).Should(BeFalse())
Expect(getContainerLogs(blocky)).
Should(ContainElement(ContainSubstring("unable to reach any DNS resolvers configured for resolver group default")))
})
})
})
Expand Down Expand Up @@ -73,10 +73,7 @@ var _ = Describe("Upstream resolver configuration tests", func() {
By("query without timeout", func() {
msg := util.NewMsgWithQuestion("example.com.", dns.Type(dns.TypeA))

resp, err := doDNSRequest(blocky, msg)
Expect(err).Should(Succeed())

Expect(resp.Answer).Should(BeDNSRecord("example.com.", dns.TypeA, 123, "1.2.3.4"))
Expect(doDNSRequest(blocky, msg)).Should(BeDNSRecord("example.com.", dns.TypeA, 123, "1.2.3.4"))
})

By("query with timeout", func() {
Expand Down
2 changes: 2 additions & 0 deletions helpertest/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ func (matcher *dnsRecordMatcher) matchSingle(rr dns.RR) (success bool, err error
// Match checks the DNS record
func (matcher *dnsRecordMatcher) Match(actual interface{}) (success bool, err error) {
switch i := actual.(type) {
case *dns.Msg:
return matcher.Match(i.Answer)
case dns.RR:
return matcher.matchSingle(i)
case []dns.RR:
Expand Down

0 comments on commit 051d618

Please sign in to comment.