Skip to content

Commit

Permalink
refactor(tests): move DeferCleanup into helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ThinkChaos committed Dec 1, 2023
1 parent f371857 commit 891d0fb
Show file tree
Hide file tree
Showing 32 changed files with 131 additions and 178 deletions.
3 changes: 2 additions & 1 deletion cmd/healthcheck_test.go
Expand Up @@ -29,7 +29,6 @@ var _ = Describe("Healthcheck command", func() {
err := srv.ListenAndServe()
Expect(err).Should(Succeed())
}()
DeferCleanup(srv.Shutdown)

Eventually(func() error {
c := NewHealthcheckCommand()
Expand Down Expand Up @@ -61,5 +60,7 @@ func createMockServer(port string) *dns.Server {
Expect(err).Should(Succeed())
})

DeferCleanup(res.Shutdown)

return res
}
1 change: 0 additions & 1 deletion cmd/root_test.go
Expand Up @@ -34,7 +34,6 @@ var _ = Describe("root command", func() {

tmpDir = NewTmpFolder("RootCommand")
Expect(tmpDir.Error).Should(Succeed())
DeferCleanup(tmpDir.Clean)

tmpFile = tmpDir.CreateStringFile("config",
"upstreams:",
Expand Down
1 change: 0 additions & 1 deletion cmd/serve_test.go
Expand Up @@ -25,7 +25,6 @@ var _ = Describe("Serve command", func() {
port = helpertest.GetStringPort(basePort)
tmpDir = helpertest.NewTmpFolder("config")
Expect(tmpDir.Error).Should(Succeed())
DeferCleanup(tmpDir.Clean)
configPath = defaultConfigPath
})

Expand Down
1 change: 0 additions & 1 deletion config/config_test.go
Expand Up @@ -31,7 +31,6 @@ var _ = Describe("Config", func() {
BeforeEach(func() {
tmpDir = helpertest.NewTmpFolder("config")
Expect(tmpDir.Error).Should(Succeed())
DeferCleanup(tmpDir.Clean)
})

Describe("Deprecated parameters are converted", func() {
Expand Down
15 changes: 3 additions & 12 deletions e2e/basic_test.go
Expand Up @@ -14,15 +14,14 @@ import (
)

var _ = Describe("Basic functional tests", func() {
var blocky, moka testcontainers.Container
var blocky testcontainers.Container
var err error

Describe("Container start", func() {
BeforeEach(func(ctx context.Context) {
moka, err = createDNSMokkaContainer(ctx, "moka1", `A google/NOERROR("A 1.2.3.4 123")`)
_, err = createDNSMokkaContainer(ctx, "moka1", `A google/NOERROR("A 1.2.3.4 123")`)

Expect(err).Should(Succeed())
DeferCleanup(moka.Terminate)
})
When("wrong port configuration is provided", func() {
BeforeEach(func(ctx context.Context) {
Expand All @@ -41,8 +40,6 @@ var _ = Describe("Basic functional tests", func() {
state, err := blocky.State(ctx)
Expect(err).Should(Succeed())
Expect(state.ExitCode).Should(Equal(1))

DeferCleanup(blocky.Terminate)
})
It("should fail to start", func(ctx context.Context) {
Eventually(blocky.IsRunning, "5s", "2ms").Should(BeFalse())
Expand All @@ -61,7 +58,6 @@ var _ = Describe("Basic functional tests", func() {
)

Expect(err).Should(Succeed())
DeferCleanup(blocky.Terminate)
})
It("Should start and answer DNS queries", func(ctx context.Context) {
msg := util.NewMsgWithQuestion("google.de.", A)
Expand Down Expand Up @@ -93,7 +89,6 @@ var _ = Describe("Basic functional tests", func() {
)

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

It("should not open http port", func(ctx context.Context) {
Expand All @@ -116,7 +111,6 @@ var _ = Describe("Basic functional tests", func() {
)

Expect(err).Should(Succeed())
DeferCleanup(blocky.Terminate)
})
It("should serve http content", func(ctx context.Context) {
host, port, err := getContainerHostPort(ctx, blocky, "4000/tcp")
Expand All @@ -143,10 +137,8 @@ var _ = Describe("Basic functional tests", func() {

Describe("Logging", func() {
BeforeEach(func(ctx context.Context) {
moka, err = createDNSMokkaContainer(ctx, "moka1", `A google/NOERROR("A 1.2.3.4 123")`)

_, err = createDNSMokkaContainer(ctx, "moka1", `A google/NOERROR("A 1.2.3.4 123")`)
Expect(err).Should(Succeed())
DeferCleanup(moka.Terminate)
})
When("log privacy is enabled", func() {
BeforeEach(func(ctx context.Context) {
Expand All @@ -160,7 +152,6 @@ var _ = Describe("Basic functional tests", func() {
" privacy: true",
)
Expect(err).Should(Succeed())
DeferCleanup(blocky.Terminate)
})
It("should not log answers and questions", func(ctx context.Context) {
msg := util.NewMsgWithQuestion("google.com.", A)
Expand Down
16 changes: 3 additions & 13 deletions e2e/blocking_test.go
Expand Up @@ -11,13 +11,11 @@ import (
)

var _ = Describe("External lists and query blocking", func() {
var blocky, httpServer, moka testcontainers.Container
var blocky testcontainers.Container
var err error
BeforeEach(func(ctx context.Context) {
moka, err = createDNSMokkaContainer(ctx, "moka", `A google/NOERROR("A 1.2.3.4 123")`)

_, err = createDNSMokkaContainer(ctx, "moka", `A google/NOERROR("A 1.2.3.4 123")`)
Expect(err).Should(Succeed())
DeferCleanup(moka.Terminate)
})
Describe("List download on startup", func() {
When("external blacklist ist not available", func() {
Expand All @@ -40,9 +38,7 @@ var _ = Describe("External lists and query blocking", func() {
" default:",
" - ads",
)

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

It("should start with warning in log work without errors", func(ctx context.Context) {
Expand Down Expand Up @@ -77,15 +73,12 @@ var _ = Describe("External lists and query blocking", func() {
" default:",
" - ads",
)

Expect(err).Should(HaveOccurred())

// check container exit status
state, err := blocky.State(ctx)
Expect(err).Should(Succeed())
Expect(state.ExitCode).Should(Equal(1))

DeferCleanup(blocky.Terminate)
})

It("should fail to start", func(ctx context.Context) {
Expand All @@ -100,10 +93,8 @@ var _ = Describe("External lists and query blocking", func() {
Describe("Query blocking against external blacklists", func() {
When("external blacklists are defined and available", func() {
BeforeEach(func(ctx context.Context) {
httpServer, err = createHTTPServerContainer(ctx, "httpserver", tmpDir, "list.txt", "blockeddomain.com")

_, err = createHTTPServerContainer(ctx, "httpserver", tmpDir, "list.txt", "blockeddomain.com")
Expect(err).Should(Succeed())
DeferCleanup(httpServer.Terminate)

blocky, err = createBlockyContainer(ctx, tmpDir,
"log:",
Expand All @@ -122,7 +113,6 @@ var _ = Describe("External lists and query blocking", func() {
)

Expect(err).Should(Succeed())
DeferCleanup(blocky.Terminate)
})
It("should download external list on startup and block queries", func(ctx context.Context) {
msg := util.NewMsgWithQuestion("blockeddomain.com.", A)
Expand Down
37 changes: 24 additions & 13 deletions e2e/containers.go
Expand Up @@ -39,6 +39,18 @@ const (
blockyImage = "blocky-e2e"
)

func deferTerminate[T testcontainers.Container](container T, err error) (T, error) {
ginkgo.DeferCleanup(func(ctx context.Context) error {
if container.IsRunning() {
return container.Terminate(ctx)
}

return nil
})

return container, err
}

func createDNSMokkaContainer(ctx context.Context, alias string, rules ...string) (testcontainers.Container, error) {
mokaRules := make(map[string]string)

Expand All @@ -55,10 +67,10 @@ func createDNSMokkaContainer(ctx context.Context, alias string, rules ...string)
Env: mokaRules,
}

return testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
return deferTerminate(testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
}))
}

func createHTTPServerContainer(ctx context.Context, alias string, tmpDir *helpertest.TmpFolder,
Expand Down Expand Up @@ -89,10 +101,10 @@ func createHTTPServerContainer(ctx context.Context, alias string, tmpDir *helper
},
}

return testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
return deferTerminate(testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
}))
}

func WithNetwork(network string) testcontainers.CustomizeRequestOption {
Expand All @@ -103,17 +115,17 @@ func WithNetwork(network string) testcontainers.CustomizeRequestOption {
}

func createRedisContainer(ctx context.Context) (*redis.RedisContainer, error) {
return redis.RunContainer(ctx,
return deferTerminate(redis.RunContainer(ctx,
testcontainers.WithImage(redisImage),
redis.WithLogLevel(redis.LogLevelVerbose),
WithNetwork("redis"),
)
))
}

func createPostgresContainer(ctx context.Context) (*postgres.PostgresContainer, error) {
const waitLogOccurrence = 2

return postgres.RunContainer(ctx,
return deferTerminate(postgres.RunContainer(ctx,
testcontainers.WithImage(postgresImage),

postgres.WithDatabase("user"),
Expand All @@ -124,17 +136,17 @@ func createPostgresContainer(ctx context.Context) (*postgres.PostgresContainer,
WithOccurrence(waitLogOccurrence).
WithStartupTimeout(startupTimeout)),
WithNetwork("postgres"),
)
))
}

func createMariaDBContainer(ctx context.Context) (*mariadb.MariaDBContainer, error) {
return mariadb.RunContainer(ctx,
return deferTerminate(mariadb.RunContainer(ctx,
testcontainers.WithImage(mariaDBImage),
mariadb.WithDatabase("user"),
mariadb.WithUsername("user"),
mariadb.WithPassword("user"),
WithNetwork("mariaDB"),
)
))
}

const (
Expand Down Expand Up @@ -178,10 +190,10 @@ func createBlockyContainer(ctx context.Context, tmpDir *helpertest.TmpFolder,
WaitingFor: wait.ForHealthCheck().WithStartupTimeout(startupTimeout),
}

container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
container, err := deferTerminate(testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
}))
if err != nil {
// attach container log if error occurs
if r, err := container.Logs(ctx); err == nil {
Expand All @@ -196,7 +208,6 @@ func createBlockyContainer(ctx context.Context, tmpDir *helpertest.TmpFolder,
// check if DNS/HTTP interface is working.
// Sometimes the internal health check returns OK, but the container port is not mapped yet
err = checkBlockyReadiness(ctx, cfg, container)

if err != nil {
return container, fmt.Errorf("container not ready: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion e2e/e2e_suite_test.go
Expand Up @@ -54,6 +54,6 @@ var _ = BeforeSuite(func(ctx context.Context) {

tmpDir = helpertest.NewTmpFolder("config")
Expect(tmpDir.Error).Should(Succeed())
DeferCleanup(tmpDir.Clean)

SetDefaultEventuallyTimeout(5 * time.Second)
})
17 changes: 6 additions & 11 deletions e2e/metrics_test.go
Expand Up @@ -16,27 +16,24 @@ import (
)

var _ = Describe("Metrics functional tests", func() {
var blocky, moka, httpServer1, httpServer2 testcontainers.Container
var blocky testcontainers.Container
var err error
var metricsURL string

Describe("Metrics", func() {
BeforeEach(func(ctx context.Context) {
moka, err = createDNSMokkaContainer(ctx, "moka1", `A google/NOERROR("A 1.2.3.4 123")`)

_, err = createDNSMokkaContainer(ctx, "moka1", `A google/NOERROR("A 1.2.3.4 123")`)
Expect(err).Should(Succeed())
DeferCleanup(moka.Terminate)

httpServer1, err = createHTTPServerContainer(ctx, "httpserver1", tmpDir, "list1.txt", "domain1.com")

_, err = createHTTPServerContainer(ctx, "httpserver1", tmpDir, "list1.txt", "domain1.com")
Expect(err).Should(Succeed())
DeferCleanup(httpServer1.Terminate)

httpServer2, err = createHTTPServerContainer(ctx, "httpserver2", tmpDir, "list2.txt",
_, err = createHTTPServerContainer(ctx, "httpserver2", tmpDir, "list2.txt",
"domain1.com", "domain2", "domain3")
Expect(err).Should(Succeed())

_, err = createHTTPServerContainer(ctx, "httpserver2", tmpDir, "list2.txt", "domain1.com", "domain2", "domain3")
Expect(err).Should(Succeed())
DeferCleanup(httpServer2.Terminate)

blocky, err = createBlockyContainer(ctx, tmpDir,
"upstreams:",
Expand All @@ -54,9 +51,7 @@ var _ = Describe("Metrics functional tests", func() {
"prometheus:",
" enable: true",
)

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

host, port, err := getContainerHostPort(ctx, blocky, "4000/tcp")
Expect(err).Should(Succeed())
Expand Down
14 changes: 2 additions & 12 deletions e2e/querylog_test.go
Expand Up @@ -16,24 +16,21 @@ import (
)

var _ = Describe("Query logs functional tests", func() {
var blocky, moka testcontainers.Container
var blocky testcontainers.Container
var postgresDB *postgres.PostgresContainer
var mariaDB *mariadb.MariaDBContainer
var db *gorm.DB
var err error

BeforeEach(func(ctx context.Context) {
moka, err = createDNSMokkaContainer(ctx, "moka1", `A google/NOERROR("A 1.2.3.4 123")`, `A unknown/NXDOMAIN()`)

_, err = createDNSMokkaContainer(ctx, "moka1", `A google/NOERROR("A 1.2.3.4 123")`, `A unknown/NXDOMAIN()`)
Expect(err).Should(Succeed())
DeferCleanup(moka.Terminate)
})

Describe("Query logging into the mariaDB database", func() {
BeforeEach(func(ctx context.Context) {
mariaDB, err = createMariaDBContainer(ctx)
Expect(err).Should(Succeed())
DeferCleanup(mariaDB.Terminate)

blocky, err = createBlockyContainer(ctx, tmpDir,
"log:",
Expand All @@ -47,10 +44,6 @@ var _ = Describe("Query logs functional tests", func() {
" target: user:user@tcp(mariaDB:3306)/user?charset=utf8mb4&parseTime=True&loc=Local",
" flushInterval: 1s",
)

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

Expect(err).Should(Succeed())

connectionString, err := mariaDB.ConnectionString(ctx,
Expand Down Expand Up @@ -113,7 +106,6 @@ var _ = Describe("Query logs functional tests", func() {
BeforeEach(func(ctx context.Context) {
postgresDB, err = createPostgresContainer(ctx)
Expect(err).Should(Succeed())
DeferCleanup(postgresDB.Terminate)

blocky, err = createBlockyContainer(ctx, tmpDir,
"log:",
Expand All @@ -127,9 +119,7 @@ var _ = Describe("Query logs functional tests", func() {
" target: postgres://user:user@postgres:5432/user",
" flushInterval: 1s",
)

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

connectionString, err := postgresDB.ConnectionString(ctx, "sslmode=disable")
Expect(err).Should(Succeed())
Expand Down

0 comments on commit 891d0fb

Please sign in to comment.