-
-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
243 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
package e2e | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/0xERR0R/blocky/util" | ||
"github.com/miekg/dns" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"github.com/testcontainers/testcontainers-go" | ||
"gorm.io/driver/mysql" | ||
"gorm.io/driver/postgres" | ||
"gorm.io/gorm" | ||
) | ||
|
||
var _ = Describe("Query logs functional tests", func() { | ||
var blocky, moka, database testcontainers.Container | ||
var db *gorm.DB | ||
var err error | ||
|
||
BeforeEach(func() { | ||
moka, err = createDNSMokkaContainer("moka1", `A google/NOERROR("A 1.2.3.4 123")`, `A unknown/NXDOMAIN()`) | ||
|
||
Expect(err).Should(Succeed()) | ||
DeferCleanup(moka.Terminate) | ||
}) | ||
|
||
FDescribe("Query logging into the mariaDB database", func() { | ||
BeforeEach(func() { | ||
|
||
database, err = createMariaDBContainer() | ||
Expect(err).Should(Succeed()) | ||
DeferCleanup(database.Terminate) | ||
|
||
blocky, err = createBlockyContainer(tmpDir, | ||
"logLevel: warn", | ||
"upstream:", | ||
" default:", | ||
" - moka1", | ||
"queryLog:", | ||
" type: mysql", | ||
" target: user:user@tcp(mariaDB:3306)/user?charset=utf8mb4&parseTime=True&loc=Local", | ||
) | ||
|
||
Expect(err).Should(Succeed()) | ||
DeferCleanup(blocky.Terminate) | ||
|
||
dbPort, err := getContainerPort(database, "3306/tcp") | ||
Expect(err).Should(Succeed()) | ||
|
||
host, err := database.Host(context.Background()) | ||
Expect(err).Should(Succeed()) | ||
|
||
dsn := fmt.Sprintf("user:user@tcp(%s:%d)/user?charset=utf8mb4&parseTime=True&loc=Local", host, dbPort) | ||
db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{}) | ||
Expect(err).Should(Succeed()) | ||
|
||
Eventually(countEntries).WithArguments(db).Should(BeNumerically("==", 0)) | ||
|
||
}) | ||
When("Some queries were performed", func() { | ||
It("Should store query log in the mariaDB database", func() { | ||
By("Performing 2 queries", func() { | ||
Expect(doDNSRequest(blocky, util.NewMsgWithQuestion("google.de.", dns.Type(dns.TypeA)))).Should(Not(BeNil())) | ||
Expect(doDNSRequest(blocky, util.NewMsgWithQuestion("unknown.domain.", dns.Type(dns.TypeA)))).Should(Not(BeNil())) | ||
}) | ||
|
||
By("check entries count asynchronously, since blocky flushes log entries in bulk", func() { | ||
Eventually(countEntries, "60s", "1s").WithArguments(db).Should(BeNumerically("==", 2)) | ||
}) | ||
|
||
By("check entry content", func() { | ||
entries, err := queryEntries(db) | ||
Expect(err).Should(Succeed()) | ||
|
||
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(getContainerLogs(blocky)).Should(BeEmpty()) | ||
}) | ||
|
||
}) | ||
}) | ||
|
||
Describe("Query logging into the postgres database", func() { | ||
BeforeEach(func() { | ||
|
||
database, err = createPostgresContainer() | ||
Expect(err).Should(Succeed()) | ||
DeferCleanup(database.Terminate) | ||
|
||
blocky, err = createBlockyContainer(tmpDir, | ||
"logLevel: warn", | ||
"upstream:", | ||
" default:", | ||
" - moka1", | ||
"queryLog:", | ||
" type: postgresql", | ||
" target: postgres://user:user@postgres:5432/user", | ||
) | ||
|
||
Expect(err).Should(Succeed()) | ||
DeferCleanup(blocky.Terminate) | ||
|
||
dbPort, err := getContainerPort(database, "5432/tcp") | ||
Expect(err).Should(Succeed()) | ||
|
||
dsn := fmt.Sprintf("postgres://user:user@localhost:%d/user", dbPort) | ||
db, err = gorm.Open(postgres.Open(dsn), &gorm.Config{}) | ||
Expect(err).Should(Succeed()) | ||
|
||
Eventually(countEntries).WithArguments(db).Should(BeNumerically("==", 0)) | ||
|
||
}) | ||
When("Some queries were performed", func() { | ||
msg := util.NewMsgWithQuestion("google.de.", dns.Type(dns.TypeA)) | ||
It("Should store query log in the postgres database", func() { | ||
By("Performing 2 queries", func() { | ||
Expect(doDNSRequest(blocky, msg)).Should(Not(BeNil())) | ||
Expect(doDNSRequest(blocky, msg)).Should(Not(BeNil())) | ||
}) | ||
|
||
By("check entries count asynchronously, since blocky flushes log entries in bulk", func() { | ||
Eventually(countEntries, "60s", "1s").WithArguments(db).Should(BeNumerically("==", 2)) | ||
}) | ||
|
||
By("check entry content", func() { | ||
entries, err := queryEntries(db) | ||
Expect(err).Should(Succeed()) | ||
|
||
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(getContainerLogs(blocky)).Should(BeEmpty()) | ||
}) | ||
|
||
}) | ||
}) | ||
}) | ||
|
||
type logEntry struct { | ||
ResponseType string | ||
QuestionType string | ||
QuestionName string | ||
Answer string | ||
ResponseCode string | ||
} | ||
|
||
func queryEntries(db *gorm.DB) ([]logEntry, error) { | ||
var entries []logEntry | ||
|
||
return entries, db.Find(&entries).Order("request_ts DESC").Error | ||
} | ||
|
||
func countEntries(db *gorm.DB) (int64, error) { | ||
var cnt int64 | ||
|
||
return cnt, db.Table("log_entries").Count(&cnt).Error | ||
} |