Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit test refactoring #1245

Merged
merged 15 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
"ghcr.io/devcontainers/features/docker-in-docker:2": {
"dockerDashComposeVersion": "v2"
},
"ghcr.io/devcontainers/features/python:1": {}
"ghcr.io/devcontainers/features/python:1": {},
"ghcr.io/devcontainers/features/github-cli:1": {},
"ghcr.io/rocker-org/devcontainer-features/apt-packages:1": {
"packages": "dnsutils "
}
},
"remoteEnv": {
"LOCAL_WORKSPACE_FOLDER": "${localWorkspaceFolder}",
Expand All @@ -27,7 +31,9 @@
"GitHub.vscode-github-actions"
],
"settings": {
"go.lintFlags": ["--config=${containerWorkspaceFolder}/.golangci.yml"],
"go.lintFlags": [
"--config=${containerWorkspaceFolder}/.golangci.yml"
],
"go.alternateTools": {
"go-langserver": "gopls"
},
Expand All @@ -39,6 +45,15 @@
},
"[markdown]": {
"editor.defaultFormatter": "yzhang.markdown-all-in-one"
},
"markiscodecoverage.searchCriteria": "**/*.lcov",
"runItOn": {
"commands": [
{
"match": "\\.go$",
"cmd": "${workspaceRoot}/.devcontainer/scripts/runItOnGo.sh ${fileDirname} ${workspaceRoot}"
}
]
}
}
}
Expand Down
5 changes: 0 additions & 5 deletions .devcontainer/scripts/generate-lcov.sh

This file was deleted.

39 changes: 39 additions & 0 deletions .devcontainer/scripts/runItOnGo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/bash -e

FOLDER_PATH=$1
if [ -z "${FOLDER_PATH}" ]; then
FOLDER_PATH=$PWD
fi

BASE_PATH=$2
if [ -z "${BASE_PATH}" ]; then
BASE_PATH=$WORKSPACE_FOLDER
fi

if [ "$FOLDER_PATH" = "$BASE_PATH" ]; then
echo "Skipping lcov creation for base path"
exit 1
fi

FOLDER_NAME=${FOLDER_PATH#"$BASE_PATH/"}
WORK_NAME="$(echo "$FOLDER_NAME" | sed 's/\//-/g')"
WORK_FILE_NAME="$WORK_NAME.ginkgo"
WORK_FILE_PATH="/tmp/$WORK_FILE_NAME"
OUTPUT_FOLDER="$BASE_PATH/coverage"
OUTPUT_FILE_PATH="$OUTPUT_FOLDER/$WORK_NAME.lcov"


mkdir -p "$OUTPUT_FOLDER"

echo "-- Start $FOLDER_NAME ($(date '+%T')) --"

TIMEFORMAT=' - Ginkgo tests finished in: %R seconds'
time ginkgo --label-filter="!e2e" --keep-going --timeout=5m --output-dir=/tmp --coverprofile="$WORK_FILE_NAME" --covermode=atomic --cover -r -p "$FOLDER_PATH" || true

TIMEFORMAT=' - lcov convert finished in: %R seconds'
time gcov2lcov -infile="$WORK_FILE_PATH" -outfile="$OUTPUT_FILE_PATH" || true

TIMEFORMAT=' - cleanup finished in: %R seconds'
time rm "$WORK_FILE_PATH" || true

echo "-- Finished $FOLDER_NAME ($(date '+%T')) --"
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ LICENSE
vendor
e2e/
.devcontainer/
coverage.txt
coverage/
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ node_modules
package-lock.json
vendor/
coverage.txt
lcov.*
coverage/
6 changes: 6 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ linters-settings:
forbid-focus-container: true
# Don't trigger warnings for HaveLen(0)
allow-havelen-zero: true
stylecheck:
# Whietlist dot imports for test packages.
dot-import-whitelist:
- "github.com/onsi/ginkgo"
- "github.com/onsi/gomega"
- "github.com/0xERR0R/blocky/helpertest"

issues:
exclude-rules:
Expand Down
9 changes: 0 additions & 9 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,5 @@
"ui.semanticTokens": true,
"formatting.gofumpt": true,
"build.standaloneTags": ["ignore", "tools"]
},
"runItOn": {
"commands": [
{
"match": "\\.go$",
"cmd": "./.devcontainer/scripts/generate-lcov.sh",
"silent": true
}
]
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package util
package helpertest

import (
"context"
Expand Down
5 changes: 2 additions & 3 deletions lists/list_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
. "github.com/0xERR0R/blocky/evt"
"github.com/0xERR0R/blocky/lists/parsers"
"github.com/0xERR0R/blocky/log"
"github.com/0xERR0R/blocky/util"
"github.com/google/uuid"
"github.com/sirupsen/logrus"

Expand Down Expand Up @@ -449,11 +448,11 @@ var _ = Describe("ListCache", func() {
})

type MockDownloader struct {
util.MockCallSequence[string]
MockCallSequence[string]
}

func newMockDownloader(driver func(res chan<- string, err chan<- error)) *MockDownloader {
return &MockDownloader{util.NewMockCallSequence(driver)}
return &MockDownloader{NewMockCallSequence(driver)}
}

func (m *MockDownloader) DownloadFile(_ string) (io.ReadCloser, error) {
Expand Down
6 changes: 3 additions & 3 deletions lists/parsers/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"errors"
"fmt"

"github.com/0xERR0R/blocky/util"
. "github.com/0xERR0R/blocky/helpertest"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
Expand Down Expand Up @@ -163,10 +163,10 @@ func iteratorToList[T any](forEach func(func(T) error) error) []T {
return res
}

type mockParser[T any] struct{ util.MockCallSequence[T] }
type mockParser[T any] struct{ MockCallSequence[T] }

func newMockParser[T any](driver func(chan<- T, chan<- error)) SeriesParser[T] {
return &mockParser[T]{util.NewMockCallSequence(driver)}
return &mockParser[T]{NewMockCallSequence(driver)}
}

func (m *mockParser[T]) Next(ctx context.Context) (_ T, rerr error) {
Expand Down
12 changes: 8 additions & 4 deletions redis/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import (
. "github.com/onsi/gomega"
)

const (
exampleComKey = CacheStorePrefix + "example.com"
)

var (
redisServer *miniredis.Miniredis
redisClient *Client
Expand Down Expand Up @@ -100,10 +104,10 @@ var _ = Describe("Redis client", func() {

By("Database has one entry with correct TTL", func() {
Eventually(func() bool {
return redisServer.DB(redisConfig.Database).Exists(CacheStorePrefix + "example.com")
return redisServer.DB(redisConfig.Database).Exists(exampleComKey)
}).Should(BeTrue())

ttl := redisServer.DB(redisConfig.Database).TTL(CacheStorePrefix + "example.com")
ttl := redisServer.DB(redisConfig.Database).TTL(exampleComKey)
Expect(ttl.Seconds()).Should(BeNumerically("~", 123))
})
})
Expand All @@ -125,10 +129,10 @@ var _ = Describe("Redis client", func() {

By("Database has one entry with default TTL", func() {
Eventually(func() bool {
return redisServer.DB(redisConfig.Database).Exists(CacheStorePrefix + "example.com")
return redisServer.DB(redisConfig.Database).Exists(exampleComKey)
}).Should(BeTrue())

ttl := redisServer.DB(redisConfig.Database).TTL(CacheStorePrefix + "example.com")
ttl := redisServer.DB(redisConfig.Database).TTL(exampleComKey)
Expect(ttl.Seconds()).Should(BeNumerically("~", defaultCacheTime.Seconds()))
})
})
Expand Down
22 changes: 12 additions & 10 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ var (
sut *Server
err error
baseURL string
queryURL string
googleMockUpstream, fritzboxMockUpstream, clientMockUpstream *resolver.MockUDPUpstreamServer
)

var _ = BeforeSuite(func() {
baseURL = "http://localhost:" + GetStringPort(httpBasePort) + "/"
queryURL = baseURL + "dns-query"
var upstreamGoogle, upstreamFritzbox, upstreamClient config.Upstream
ctx, cancelFn := context.WithCancel(context.Background())
DeferCleanup(cancelFn)
Expand Down Expand Up @@ -415,7 +417,7 @@ var _ = Describe("Running DNS server", func() {
Context("DOH over GET (RFC 8484)", func() {
When("DOH get request with 'example.com' is performed", func() {
It("should get a valid response", func() {
resp, err := http.Get(baseURL + "dns-query?dns=AAABAAABAAAAAAAAA3d3dwdleGFtcGxlA2NvbQAAAQAB")
resp, err := http.Get(queryURL + "?dns=AAABAAABAAAAAAAAA3d3dwdleGFtcGxlA2NvbQAAAQAB")
Expect(err).Should(Succeed())
DeferCleanup(resp.Body.Close)

Expand All @@ -434,7 +436,7 @@ var _ = Describe("Running DNS server", func() {
})
When("Request does not contain a valid DNS message", func() {
It("should return 'Bad Request'", func() {
resp, err := http.Get(baseURL + "dns-query?dns=xxxx")
resp, err := http.Get(queryURL + "?dns=xxxx")
Expect(err).Should(Succeed())
DeferCleanup(resp.Body.Close)

Expand All @@ -443,7 +445,7 @@ var _ = Describe("Running DNS server", func() {
})
When("Request's parameter does not contain a valid base64'", func() {
It("should return 'Bad Request'", func() {
resp, err := http.Get(baseURL + "dns-query?dns=äöä")
resp, err := http.Get(queryURL + "?dns=äöä")
Expect(err).Should(Succeed())
DeferCleanup(resp.Body.Close)

Expand All @@ -452,7 +454,7 @@ var _ = Describe("Running DNS server", func() {
})
When("Request does not contain a dns parameter", func() {
It("should return 'Bad Request'", func() {
resp, err := http.Get(baseURL + "dns-query?test")
resp, err := http.Get(queryURL + "?test")
Expect(err).Should(Succeed())
DeferCleanup(resp.Body.Close)

Expand All @@ -463,7 +465,7 @@ var _ = Describe("Running DNS server", func() {
It("should return 'URI Too Long'", func() {
longBase64msg := base64.StdEncoding.EncodeToString([]byte(strings.Repeat("t", 513)))

resp, err := http.Get(baseURL + "dns-query?dns=" + longBase64msg)
resp, err := http.Get(queryURL + "?dns=" + longBase64msg)
Expect(err).Should(Succeed())
DeferCleanup(resp.Body.Close)

Expand All @@ -482,7 +484,7 @@ var _ = Describe("Running DNS server", func() {
rawDNSMessage, err := msg.Pack()
Expect(err).Should(Succeed())

resp, err = http.Post(baseURL+"dns-query",
resp, err = http.Post(queryURL,
"application/dns-message", bytes.NewReader(rawDNSMessage))
Expect(err).Should(Succeed())
DeferCleanup(resp.Body.Close)
Expand All @@ -507,7 +509,7 @@ var _ = Describe("Running DNS server", func() {
rawDNSMessage, err := msg.Pack()
Expect(err).Should(Succeed())

resp, err = http.Post(baseURL+"dns-query/client123",
resp, err = http.Post(queryURL+"/client123",
"application/dns-message", bytes.NewReader(rawDNSMessage))
Expect(err).Should(Succeed())
DeferCleanup(resp.Body.Close)
Expand All @@ -531,7 +533,7 @@ var _ = Describe("Running DNS server", func() {
It("should return 'Payload Too Large'", func() {
largeMessage := []byte(strings.Repeat("t", 513))

resp, err = http.Post(baseURL+"dns-query", "application/dns-message", bytes.NewReader(largeMessage))
resp, err = http.Post(queryURL, "application/dns-message", bytes.NewReader(largeMessage))
Expect(err).Should(Succeed())
DeferCleanup(resp.Body.Close)

Expand All @@ -540,7 +542,7 @@ var _ = Describe("Running DNS server", func() {
})
When("Request has wrong type", func() {
It("should return 'Unsupported Media Type'", func() {
resp, err = http.Post(baseURL+"dns-query", "application/text", bytes.NewReader([]byte("a")))
resp, err = http.Post(queryURL, "application/text", bytes.NewReader([]byte("a")))
Expect(err).Should(Succeed())
DeferCleanup(resp.Body.Close)

Expand All @@ -553,7 +555,7 @@ var _ = Describe("Running DNS server", func() {
rawDNSMessage, err := msg.Pack()
Expect(err).Should(Succeed())

resp, err = http.Post(baseURL+"dns-query",
resp, err = http.Post(queryURL,
"application/dns-message", bytes.NewReader(rawDNSMessage))
Expect(err).Should(Succeed())
DeferCleanup(resp.Body.Close)
Expand Down