Skip to content

Commit

Permalink
Test refactoring (0xERR0R#798)
Browse files Browse the repository at this point in the history
* test: refactor tests

* chore: fix possible race condition in cache
  • Loading branch information
0xERR0R committed Dec 29, 2022
1 parent 30086dc commit 53a7d4f
Show file tree
Hide file tree
Showing 30 changed files with 1,817 additions and 1,059 deletions.
10 changes: 7 additions & 3 deletions e2e/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

. "github.com/0xERR0R/blocky/helpertest"
"github.com/0xERR0R/blocky/util"
"github.com/miekg/dns"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/testcontainers/testcontainers-go"
Expand Down Expand Up @@ -37,9 +36,14 @@ var _ = Describe("Basic functional tests", func() {
DeferCleanup(blocky.Terminate)
})
It("Should start and answer DNS queries", func() {
msg := util.NewMsgWithQuestion("google.de.", dns.Type(dns.TypeA))
msg := util.NewMsgWithQuestion("google.de.", A)

Expect(doDNSRequest(blocky, msg)).Should(BeDNSRecord("google.de.", dns.TypeA, 123, "1.2.3.4"))
Expect(doDNSRequest(blocky, msg)).
Should(
SatisfyAll(
BeDNSRecord("google.de.", A, "1.2.3.4"),
HaveTTL(BeNumerically("==", 123)),
))
})
It("should return 'healthy' container status (healthcheck)", func() {
Eventually(func(g Gomega) string {
Expand Down
19 changes: 14 additions & 5 deletions e2e/blocking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package e2e
import (
. "github.com/0xERR0R/blocky/helpertest"
"github.com/0xERR0R/blocky/util"
"github.com/miekg/dns"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/testcontainers/testcontainers-go"
Expand Down Expand Up @@ -43,9 +42,14 @@ var _ = Describe("External lists and query blocking", func() {
})

It("should start with warning in log work without errors", func() {
msg := util.NewMsgWithQuestion("google.com.", dns.Type(dns.TypeA))
msg := util.NewMsgWithQuestion("google.com.", A)

Expect(doDNSRequest(blocky, msg)).Should(BeDNSRecord("google.com.", dns.TypeA, 123, "1.2.3.4"))
Expect(doDNSRequest(blocky, msg)).
Should(
SatisfyAll(
BeDNSRecord("google.com.", A, "1.2.3.4"),
HaveTTL(BeNumerically("==", 123)),
))

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

Expect(doDNSRequest(blocky, msg)).Should(BeDNSRecord("blockeddomain.com.", dns.TypeA, 6*60*60, "0.0.0.0"))
Expect(doDNSRequest(blocky, msg)).
Should(
SatisfyAll(
BeDNSRecord("blockeddomain.com.", A, "0.0.0.0"),
HaveTTL(BeNumerically("==", 6*60*60)),
))

Expect(getContainerLogs(blocky)).Should(BeEmpty())
})
Expand Down
59 changes: 41 additions & 18 deletions e2e/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

. "github.com/0xERR0R/blocky/helpertest"
"github.com/0xERR0R/blocky/util"
"github.com/miekg/dns"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/testcontainers/testcontainers-go"
Expand Down Expand Up @@ -74,38 +73,62 @@ var _ = Describe("Metrics functional tests", func() {

When("Some query results are cached", func() {
BeforeEach(func() {
Eventually(fetchBlockyMetrics).WithArguments(metricsURL).Should(ContainElement("blocky_cache_entry_count 0"))
Eventually(fetchBlockyMetrics).WithArguments(metricsURL).Should(ContainElement("blocky_cache_hit_count 0"))
Eventually(fetchBlockyMetrics).WithArguments(metricsURL).Should(ContainElement("blocky_cache_miss_count 0"))
Eventually(fetchBlockyMetrics).WithArguments(metricsURL).
Should(
SatisfyAll(
ContainElement("blocky_cache_entry_count 0"),
ContainElement("blocky_cache_hit_count 0"),
ContainElement("blocky_cache_miss_count 0"),
))
})

It("Should increment cache counts", func() {
msg := util.NewMsgWithQuestion("google.de.", dns.Type(dns.TypeA))
msg := util.NewMsgWithQuestion("google.de.", A)

By("first query, should increment the cache miss count and the total count", func() {
Expect(doDNSRequest(blocky, msg)).Should(BeDNSRecord("google.de.", dns.TypeA, 123, "1.2.3.4"))

Eventually(fetchBlockyMetrics).WithArguments(metricsURL).Should(ContainElement("blocky_cache_entry_count 1"))
Eventually(fetchBlockyMetrics).WithArguments(metricsURL).Should(ContainElement("blocky_cache_hit_count 0"))
Eventually(fetchBlockyMetrics).WithArguments(metricsURL).Should(ContainElement("blocky_cache_miss_count 1"))
Expect(doDNSRequest(blocky, msg)).
Should(
SatisfyAll(
BeDNSRecord("google.de.", A, "1.2.3.4"),
HaveTTL(BeNumerically("==", 123)),
))

Eventually(fetchBlockyMetrics).WithArguments(metricsURL).
Should(
SatisfyAll(
ContainElement("blocky_cache_entry_count 1"),
ContainElement("blocky_cache_hit_count 0"),
ContainElement("blocky_cache_miss_count 1"),
))
})

By("Same query again, should increment the cache hit count", func() {
Expect(doDNSRequest(blocky, msg)).Should(BeDNSRecord("google.de.", dns.TypeA, 0, "1.2.3.4"))

Eventually(fetchBlockyMetrics).WithArguments(metricsURL).Should(ContainElement("blocky_cache_entry_count 1"))
Eventually(fetchBlockyMetrics).WithArguments(metricsURL).Should(ContainElement("blocky_cache_hit_count 1"))
Eventually(fetchBlockyMetrics).WithArguments(metricsURL).Should(ContainElement("blocky_cache_miss_count 1"))
Expect(doDNSRequest(blocky, msg)).
Should(
SatisfyAll(
BeDNSRecord("google.de.", A, "1.2.3.4"),
HaveTTL(BeNumerically("<=", 123)),
))

Eventually(fetchBlockyMetrics).WithArguments(metricsURL).
Should(
SatisfyAll(
ContainElement("blocky_cache_entry_count 1"),
ContainElement("blocky_cache_hit_count 1"),
ContainElement("blocky_cache_miss_count 1"),
))
})
})
})

When("Lists are loaded", func() {
It("Should expose list cache sizes per group as metrics", func() {
Eventually(fetchBlockyMetrics).WithArguments(metricsURL).
Should(ContainElement("blocky_blacklist_cache{group=\"group1\"} 1"))
Eventually(fetchBlockyMetrics).WithArguments(metricsURL).
Should(ContainElement("blocky_blacklist_cache{group=\"group2\"} 3"))
Should(
SatisfyAll(
ContainElement("blocky_blacklist_cache{group=\"group1\"} 1"),
ContainElement("blocky_blacklist_cache{group=\"group2\"} 3"),
))
})
})
})
Expand Down
68 changes: 38 additions & 30 deletions e2e/querylog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,25 @@ var _ = Describe("Query logs functional tests", func() {

Expect(entries).Should(HaveLen(2))

Expect(entries[0]).Should(SatisfyAll(
HaveField("ResponseType", "RESOLVED"),
HaveField("QuestionType", "A"),
HaveField("QuestionName", "google.de"),
HaveField("Answer", "A (1.2.3.4)"),
HaveField("ResponseCode", "NOERROR"),
))

Expect(entries[1]).Should(SatisfyAll(
HaveField("ResponseType", "RESOLVED"),
HaveField("QuestionType", "A"),
HaveField("QuestionName", "unknown.domain"),
HaveField("Answer", ""),
HaveField("ResponseCode", "NXDOMAIN"),
))
Expect(entries[0]).
Should(
SatisfyAll(
HaveField("ResponseType", "RESOLVED"),
HaveField("QuestionType", "A"),
HaveField("QuestionName", "google.de"),
HaveField("Answer", "A (1.2.3.4)"),
HaveField("ResponseCode", "NOERROR"),
))

Expect(entries[1]).
Should(
SatisfyAll(
HaveField("ResponseType", "RESOLVED"),
HaveField("QuestionType", "A"),
HaveField("QuestionName", "unknown.domain"),
HaveField("Answer", ""),
HaveField("ResponseCode", "NXDOMAIN"),
))
})
})
})
Expand Down Expand Up @@ -149,21 +153,25 @@ var _ = Describe("Query logs functional tests", func() {

Expect(entries).Should(HaveLen(2))

Expect(entries[0]).Should(SatisfyAll(
HaveField("ResponseType", "RESOLVED"),
HaveField("QuestionType", "A"),
HaveField("QuestionName", "google.de"),
HaveField("Answer", "A (1.2.3.4)"),
HaveField("ResponseCode", "NOERROR"),
))

Expect(entries[1]).Should(SatisfyAll(
HaveField("ResponseType", "CACHED"),
HaveField("QuestionType", "A"),
HaveField("QuestionName", "google.de"),
HaveField("Answer", "A (1.2.3.4)"),
HaveField("ResponseCode", "NOERROR"),
))
Expect(entries[0]).
Should(
SatisfyAll(
HaveField("ResponseType", "RESOLVED"),
HaveField("QuestionType", "A"),
HaveField("QuestionName", "google.de"),
HaveField("Answer", "A (1.2.3.4)"),
HaveField("ResponseCode", "NOERROR"),
))

Expect(entries[1]).
Should(
SatisfyAll(
HaveField("ResponseType", "CACHED"),
HaveField("QuestionType", "A"),
HaveField("QuestionName", "google.de"),
HaveField("Answer", "A (1.2.3.4)"),
HaveField("ResponseCode", "NOERROR"),
))
})
})
})
Expand Down
33 changes: 26 additions & 7 deletions e2e/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
. "github.com/0xERR0R/blocky/helpertest"
"github.com/0xERR0R/blocky/util"
"github.com/go-redis/redis/v8"
"github.com/miekg/dns"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/testcontainers/testcontainers-go"
Expand Down Expand Up @@ -71,9 +70,14 @@ var _ = Describe("Redis configuration tests", func() {
DeferCleanup(blocky2.Terminate)
})
It("2nd instance of blocky should use cache from redis", func() {
msg := util.NewMsgWithQuestion("google.de.", dns.Type(dns.TypeA))
msg := util.NewMsgWithQuestion("google.de.", A)
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"))
Expect(doDNSRequest(blocky1, msg)).
Should(
SatisfyAll(
BeDNSRecord("google.de.", A, "1.2.3.4"),
HaveTTL(BeNumerically("==", 123)),
))
})

By("Check redis, must contain one cache entry", func() {
Expand All @@ -85,7 +89,12 @@ var _ = Describe("Redis configuration tests", func() {
})

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"))
Expect(doDNSRequest(blocky2, msg)).
Should(
SatisfyAll(
BeDNSRecord("google.de.", A, "1.2.3.4"),
HaveTTL(BeNumerically("<=", 123)),
))
})

By("No warnings/errors in log", func() {
Expand Down Expand Up @@ -113,9 +122,14 @@ var _ = Describe("Redis configuration tests", func() {
DeferCleanup(blocky1.Terminate)
})
It("should load cache from redis after start", func() {
msg := util.NewMsgWithQuestion("google.de.", dns.Type(dns.TypeA))
msg := util.NewMsgWithQuestion("google.de.", A)
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"))
Expect(doDNSRequest(blocky1, msg)).
Should(
SatisfyAll(
BeDNSRecord("google.de.", A, "1.2.3.4"),
HaveTTL(BeNumerically("==", 123)),
))
})

By("Check redis, must contain one cache entry", func() {
Expand All @@ -142,7 +156,12 @@ var _ = Describe("Redis configuration tests", func() {
})

By("Query second blocky instance", func() {
Expect(doDNSRequest(blocky2, msg)).Should(BeDNSRecord("google.de.", dns.TypeA, 0, "1.2.3.4"))
Expect(doDNSRequest(blocky2, msg)).
Should(
SatisfyAll(
BeDNSRecord("google.de.", A, "1.2.3.4"),
HaveTTL(BeNumerically("<=", 123)),
))
})

By("No warnings/errors in log", func() {
Expand Down
12 changes: 8 additions & 4 deletions e2e/upstream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,17 @@ var _ = Describe("Upstream resolver configuration tests", func() {
})
It("should consider the timeout parameter", func() {
By("query without timeout", func() {
msg := util.NewMsgWithQuestion("example.com.", dns.Type(dns.TypeA))

Expect(doDNSRequest(blocky, msg)).Should(BeDNSRecord("example.com.", dns.TypeA, 123, "1.2.3.4"))
msg := util.NewMsgWithQuestion("example.com.", A)
Expect(doDNSRequest(blocky, msg)).
Should(
SatisfyAll(
BeDNSRecord("example.com.", A, "1.2.3.4"),
HaveTTL(BeNumerically("==", 123)),
))
})

By("query with timeout", func() {
msg := util.NewMsgWithQuestion("delay.com/.", dns.Type(dns.TypeA))
msg := util.NewMsgWithQuestion("delay.com/.", A)

resp, err := doDNSRequest(blocky, msg)
Expect(err).Should(Succeed())
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ require (

require (
github.com/DATA-DOG/go-sqlmock v1.5.0
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
github.com/docker/go-connections v0.4.0
github.com/dosgo/zigtool v0.0.0-20210923085854-9c6fc1d62198
github.com/golangci/golangci-lint v1.50.1
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ github.com/Shopify/logrus-bugsnag v0.0.0-20171204204709-577dee27f20d/go.mod h1:H
github.com/abice/go-enum v0.5.4 h1:d6/smT5ZlVO/u9rSkYDP6XSp9NDMmQ16sUUR27O1gBA=
github.com/abice/go-enum v0.5.4/go.mod h1:TfHm+vl7PLBrd0oaWNUPRylZvC5XzScwIEGS2+jN+j4=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafoB+tBA3gMyHYHrpOtNuDiK/uB5uXxq5wM=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
Expand Down
Loading

0 comments on commit 53a7d4f

Please sign in to comment.