AdGuardHome/internal/aghtest/aghtest.go

125 lines
3.0 KiB
Go
Raw Permalink Normal View History

// Package aghtest contains utilities for testing.
package aghtest
Pull request: 2273 clean tests output Merge in DNS/adguard-home from 2273-clean-tests-output to master Closes #2273. Squashed commit of the following: commit 7571a33fc1f76300bd256578b3afa95338e399c4 Merge: f17df0f9c a19523b25 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:45:30 2020 +0300 Merge branch 'master' into 2273-clean-tests-output commit f17df0f9ce2a3ed25db33fbc6a2e7cabd33f657b Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:35:42 2020 +0300 home: move build constraint on top line commit 3717c8ef5a51f9dcaa7e524bfa7b0f1d90bec93d Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:24:50 2020 +0300 all: add improvements to changelog commit de6d5afe39d74a3c3d3e0bbe6d0e09aea0214d56 Merge: 43d4f7acf 394fc5a9d Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:04:38 2020 +0300 Merge branch 'master' into 2273-clean-tests-output commit 43d4f7acf188e810aa7277cb6479110c9842e8be Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 13:38:13 2020 +0300 dnsfilter: remove redundant test logging commit 7194c1413006b8f52fc454e89ab052bf52e4e517 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 12:19:14 2020 +0300 testutil: improve comments commit 9f17ab08e287fa69ce30d9e7eec8ea8880f87716 Author: Eugene Burkov <e.burkov@adguard.com> Date: Sat Nov 14 01:22:08 2020 +0300 nclient4: fix wrong function name commit f355749149b2a4485792ba2bdcbc0bb4b629d726 Author: Eugene Burkov <e.burkov@adguard.com> Date: Sat Nov 14 01:07:22 2020 +0300 testutil: fix comments and naming commit f8c50a260bfae08d594a7f37d603941d3680a45e Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 14:09:50 2020 +0300 testutil: create a package and include it commit f169cdc4084b719de65aa0cdc65200b48785322e Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 20:15:58 2020 +0300 agherr: discard loggers output commit 360e736b5a2a30f2c5350448234f14b841e3ea27 Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 20:09:55 2020 +0300 all: replace default log writer with ioutil.Discard Closes #2273.
2020-11-16 12:52:05 +00:00
import (
2023-07-26 11:18:44 +01:00
"crypto/sha256"
Pull request: cover with tests Merge in DNS/adguard-home from 2271-cover-with-tests to master Updates #2271. Squashed commit of the following: commit db6440efe05171bc15367a2996521848ca348053 Merge: db7fa726b bf4c256c7 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 19:23:09 2020 +0300 Merge branch 'master' into 2271-cover-with-tests commit db7fa726bb91b08ec7aaa6c0c818c88b5feb87cd Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 18:26:51 2020 +0300 all: clean dependencies sum commit b8dc6078c4bcc0de1b7e9073832de122f6fe38a4 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 16:46:00 2020 +0300 testutil: improve code quality commit 001b7194682b1f00aa54dc5a28236faed5a5b02d Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 16:29:49 2020 +0300 testutil: enhance functionality commit f6ccd91a4df6c56778eab8ae50e88e3818b20dd3 Merge: 43fa2eefb 6358240e9 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:57:37 2020 +0300 Merge branch 'master' into 2271-cover-with-tests commit 43fa2eefbc10ef361603cacc1ca12092b12a057a Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 14:55:15 2020 +0300 querylog: replace fake log with real in tests commit b95bee7565a14a02c80c78131b3ced224663dd8a Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 12:38:59 2020 +0300 dnsfilter: replace thoughtless declaration with idiomatic one commit a210b1586092e7ae91a9e67c972fa2d2f6baded6 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 19:00:25 2020 +0300 all: refresh golibs dependencies commit 4ff97bd1ade6c80e274ff5716e44df4eba55bdd9 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 18:38:47 2020 +0300 all: remove std log commit 542dbda10fefce9f46d15489712b163d919b1291 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 13:46:39 2020 +0300 querylog: improve test logic and readability commit 796d402385925e8e62a1b4c7bf56e4ceec22418c Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 19:06:42 2020 +0300 all: improve code quality commit e81894c11ef15b0453e8e5297f1349936a32f9dd Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 18:32:30 2020 +0300 all: cover with tests commit 252d81fc8a50a91b02cf0f6f35cc22178a2a4d90 Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 17:32:01 2020 +0300 all: cover with tests
2020-11-16 16:45:31 +00:00
"io"
2023-10-11 15:31:41 +01:00
"net/http"
"net/http/httptest"
2023-09-07 15:13:48 +01:00
"net/netip"
2023-10-11 15:31:41 +01:00
"net/url"
Pull request: 2273 clean tests output Merge in DNS/adguard-home from 2273-clean-tests-output to master Closes #2273. Squashed commit of the following: commit 7571a33fc1f76300bd256578b3afa95338e399c4 Merge: f17df0f9c a19523b25 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:45:30 2020 +0300 Merge branch 'master' into 2273-clean-tests-output commit f17df0f9ce2a3ed25db33fbc6a2e7cabd33f657b Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:35:42 2020 +0300 home: move build constraint on top line commit 3717c8ef5a51f9dcaa7e524bfa7b0f1d90bec93d Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:24:50 2020 +0300 all: add improvements to changelog commit de6d5afe39d74a3c3d3e0bbe6d0e09aea0214d56 Merge: 43d4f7acf 394fc5a9d Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:04:38 2020 +0300 Merge branch 'master' into 2273-clean-tests-output commit 43d4f7acf188e810aa7277cb6479110c9842e8be Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 13:38:13 2020 +0300 dnsfilter: remove redundant test logging commit 7194c1413006b8f52fc454e89ab052bf52e4e517 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 12:19:14 2020 +0300 testutil: improve comments commit 9f17ab08e287fa69ce30d9e7eec8ea8880f87716 Author: Eugene Burkov <e.burkov@adguard.com> Date: Sat Nov 14 01:22:08 2020 +0300 nclient4: fix wrong function name commit f355749149b2a4485792ba2bdcbc0bb4b629d726 Author: Eugene Burkov <e.burkov@adguard.com> Date: Sat Nov 14 01:07:22 2020 +0300 testutil: fix comments and naming commit f8c50a260bfae08d594a7f37d603941d3680a45e Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 14:09:50 2020 +0300 testutil: create a package and include it commit f169cdc4084b719de65aa0cdc65200b48785322e Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 20:15:58 2020 +0300 agherr: discard loggers output commit 360e736b5a2a30f2c5350448234f14b841e3ea27 Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 20:09:55 2020 +0300 all: replace default log writer with ioutil.Discard Closes #2273.
2020-11-16 12:52:05 +00:00
"testing"
2024-03-12 14:45:11 +00:00
"time"
Pull request: 2273 clean tests output Merge in DNS/adguard-home from 2273-clean-tests-output to master Closes #2273. Squashed commit of the following: commit 7571a33fc1f76300bd256578b3afa95338e399c4 Merge: f17df0f9c a19523b25 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:45:30 2020 +0300 Merge branch 'master' into 2273-clean-tests-output commit f17df0f9ce2a3ed25db33fbc6a2e7cabd33f657b Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:35:42 2020 +0300 home: move build constraint on top line commit 3717c8ef5a51f9dcaa7e524bfa7b0f1d90bec93d Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:24:50 2020 +0300 all: add improvements to changelog commit de6d5afe39d74a3c3d3e0bbe6d0e09aea0214d56 Merge: 43d4f7acf 394fc5a9d Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:04:38 2020 +0300 Merge branch 'master' into 2273-clean-tests-output commit 43d4f7acf188e810aa7277cb6479110c9842e8be Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 13:38:13 2020 +0300 dnsfilter: remove redundant test logging commit 7194c1413006b8f52fc454e89ab052bf52e4e517 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 12:19:14 2020 +0300 testutil: improve comments commit 9f17ab08e287fa69ce30d9e7eec8ea8880f87716 Author: Eugene Burkov <e.burkov@adguard.com> Date: Sat Nov 14 01:22:08 2020 +0300 nclient4: fix wrong function name commit f355749149b2a4485792ba2bdcbc0bb4b629d726 Author: Eugene Burkov <e.burkov@adguard.com> Date: Sat Nov 14 01:07:22 2020 +0300 testutil: fix comments and naming commit f8c50a260bfae08d594a7f37d603941d3680a45e Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 14:09:50 2020 +0300 testutil: create a package and include it commit f169cdc4084b719de65aa0cdc65200b48785322e Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 20:15:58 2020 +0300 agherr: discard loggers output commit 360e736b5a2a30f2c5350448234f14b841e3ea27 Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 20:09:55 2020 +0300 all: replace default log writer with ioutil.Discard Closes #2273.
2020-11-16 12:52:05 +00:00
2024-03-12 14:45:11 +00:00
"github.com/AdguardTeam/dnsproxy/proxy"
Pull request: 2273 clean tests output Merge in DNS/adguard-home from 2273-clean-tests-output to master Closes #2273. Squashed commit of the following: commit 7571a33fc1f76300bd256578b3afa95338e399c4 Merge: f17df0f9c a19523b25 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:45:30 2020 +0300 Merge branch 'master' into 2273-clean-tests-output commit f17df0f9ce2a3ed25db33fbc6a2e7cabd33f657b Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:35:42 2020 +0300 home: move build constraint on top line commit 3717c8ef5a51f9dcaa7e524bfa7b0f1d90bec93d Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:24:50 2020 +0300 all: add improvements to changelog commit de6d5afe39d74a3c3d3e0bbe6d0e09aea0214d56 Merge: 43d4f7acf 394fc5a9d Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:04:38 2020 +0300 Merge branch 'master' into 2273-clean-tests-output commit 43d4f7acf188e810aa7277cb6479110c9842e8be Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 13:38:13 2020 +0300 dnsfilter: remove redundant test logging commit 7194c1413006b8f52fc454e89ab052bf52e4e517 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 12:19:14 2020 +0300 testutil: improve comments commit 9f17ab08e287fa69ce30d9e7eec8ea8880f87716 Author: Eugene Burkov <e.burkov@adguard.com> Date: Sat Nov 14 01:22:08 2020 +0300 nclient4: fix wrong function name commit f355749149b2a4485792ba2bdcbc0bb4b629d726 Author: Eugene Burkov <e.burkov@adguard.com> Date: Sat Nov 14 01:07:22 2020 +0300 testutil: fix comments and naming commit f8c50a260bfae08d594a7f37d603941d3680a45e Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 14:09:50 2020 +0300 testutil: create a package and include it commit f169cdc4084b719de65aa0cdc65200b48785322e Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 20:15:58 2020 +0300 agherr: discard loggers output commit 360e736b5a2a30f2c5350448234f14b841e3ea27 Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 20:09:55 2020 +0300 all: replace default log writer with ioutil.Discard Closes #2273.
2020-11-16 12:52:05 +00:00
"github.com/AdguardTeam/golibs/log"
2024-03-12 14:45:11 +00:00
"github.com/AdguardTeam/golibs/netutil"
"github.com/AdguardTeam/golibs/testutil"
"github.com/miekg/dns"
2023-10-11 15:31:41 +01:00
"github.com/stretchr/testify/require"
Pull request: 2273 clean tests output Merge in DNS/adguard-home from 2273-clean-tests-output to master Closes #2273. Squashed commit of the following: commit 7571a33fc1f76300bd256578b3afa95338e399c4 Merge: f17df0f9c a19523b25 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:45:30 2020 +0300 Merge branch 'master' into 2273-clean-tests-output commit f17df0f9ce2a3ed25db33fbc6a2e7cabd33f657b Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:35:42 2020 +0300 home: move build constraint on top line commit 3717c8ef5a51f9dcaa7e524bfa7b0f1d90bec93d Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:24:50 2020 +0300 all: add improvements to changelog commit de6d5afe39d74a3c3d3e0bbe6d0e09aea0214d56 Merge: 43d4f7acf 394fc5a9d Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:04:38 2020 +0300 Merge branch 'master' into 2273-clean-tests-output commit 43d4f7acf188e810aa7277cb6479110c9842e8be Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 13:38:13 2020 +0300 dnsfilter: remove redundant test logging commit 7194c1413006b8f52fc454e89ab052bf52e4e517 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 12:19:14 2020 +0300 testutil: improve comments commit 9f17ab08e287fa69ce30d9e7eec8ea8880f87716 Author: Eugene Burkov <e.burkov@adguard.com> Date: Sat Nov 14 01:22:08 2020 +0300 nclient4: fix wrong function name commit f355749149b2a4485792ba2bdcbc0bb4b629d726 Author: Eugene Burkov <e.burkov@adguard.com> Date: Sat Nov 14 01:07:22 2020 +0300 testutil: fix comments and naming commit f8c50a260bfae08d594a7f37d603941d3680a45e Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 14:09:50 2020 +0300 testutil: create a package and include it commit f169cdc4084b719de65aa0cdc65200b48785322e Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 20:15:58 2020 +0300 agherr: discard loggers output commit 360e736b5a2a30f2c5350448234f14b841e3ea27 Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 20:09:55 2020 +0300 all: replace default log writer with ioutil.Discard Closes #2273.
2020-11-16 12:52:05 +00:00
)
2023-09-07 15:13:48 +01:00
const (
// ReqHost is the common request host for filtering tests.
ReqHost = "www.host.example"
// ReqFQDN is the common request FQDN for filtering tests.
ReqFQDN = ReqHost + "."
)
Pull request: cover with tests Merge in DNS/adguard-home from 2271-cover-with-tests to master Updates #2271. Squashed commit of the following: commit db6440efe05171bc15367a2996521848ca348053 Merge: db7fa726b bf4c256c7 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 19:23:09 2020 +0300 Merge branch 'master' into 2271-cover-with-tests commit db7fa726bb91b08ec7aaa6c0c818c88b5feb87cd Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 18:26:51 2020 +0300 all: clean dependencies sum commit b8dc6078c4bcc0de1b7e9073832de122f6fe38a4 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 16:46:00 2020 +0300 testutil: improve code quality commit 001b7194682b1f00aa54dc5a28236faed5a5b02d Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 16:29:49 2020 +0300 testutil: enhance functionality commit f6ccd91a4df6c56778eab8ae50e88e3818b20dd3 Merge: 43fa2eefb 6358240e9 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:57:37 2020 +0300 Merge branch 'master' into 2271-cover-with-tests commit 43fa2eefbc10ef361603cacc1ca12092b12a057a Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 14:55:15 2020 +0300 querylog: replace fake log with real in tests commit b95bee7565a14a02c80c78131b3ced224663dd8a Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 12:38:59 2020 +0300 dnsfilter: replace thoughtless declaration with idiomatic one commit a210b1586092e7ae91a9e67c972fa2d2f6baded6 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 19:00:25 2020 +0300 all: refresh golibs dependencies commit 4ff97bd1ade6c80e274ff5716e44df4eba55bdd9 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 18:38:47 2020 +0300 all: remove std log commit 542dbda10fefce9f46d15489712b163d919b1291 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 13:46:39 2020 +0300 querylog: improve test logic and readability commit 796d402385925e8e62a1b4c7bf56e4ceec22418c Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 19:06:42 2020 +0300 all: improve code quality commit e81894c11ef15b0453e8e5297f1349936a32f9dd Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 18:32:30 2020 +0300 all: cover with tests commit 252d81fc8a50a91b02cf0f6f35cc22178a2a4d90 Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 17:32:01 2020 +0300 all: cover with tests
2020-11-16 16:45:31 +00:00
// ReplaceLogWriter moves logger output to w and uses Cleanup method of t to
// revert changes.
func ReplaceLogWriter(t testing.TB, w io.Writer) {
t.Helper()
prev := log.Writer()
t.Cleanup(func() { log.SetOutput(prev) })
Pull request: cover with tests Merge in DNS/adguard-home from 2271-cover-with-tests to master Updates #2271. Squashed commit of the following: commit db6440efe05171bc15367a2996521848ca348053 Merge: db7fa726b bf4c256c7 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 19:23:09 2020 +0300 Merge branch 'master' into 2271-cover-with-tests commit db7fa726bb91b08ec7aaa6c0c818c88b5feb87cd Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 18:26:51 2020 +0300 all: clean dependencies sum commit b8dc6078c4bcc0de1b7e9073832de122f6fe38a4 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 16:46:00 2020 +0300 testutil: improve code quality commit 001b7194682b1f00aa54dc5a28236faed5a5b02d Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 16:29:49 2020 +0300 testutil: enhance functionality commit f6ccd91a4df6c56778eab8ae50e88e3818b20dd3 Merge: 43fa2eefb 6358240e9 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:57:37 2020 +0300 Merge branch 'master' into 2271-cover-with-tests commit 43fa2eefbc10ef361603cacc1ca12092b12a057a Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 14:55:15 2020 +0300 querylog: replace fake log with real in tests commit b95bee7565a14a02c80c78131b3ced224663dd8a Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 12:38:59 2020 +0300 dnsfilter: replace thoughtless declaration with idiomatic one commit a210b1586092e7ae91a9e67c972fa2d2f6baded6 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 19:00:25 2020 +0300 all: refresh golibs dependencies commit 4ff97bd1ade6c80e274ff5716e44df4eba55bdd9 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 18:38:47 2020 +0300 all: remove std log commit 542dbda10fefce9f46d15489712b163d919b1291 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 13:46:39 2020 +0300 querylog: improve test logic and readability commit 796d402385925e8e62a1b4c7bf56e4ceec22418c Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 19:06:42 2020 +0300 all: improve code quality commit e81894c11ef15b0453e8e5297f1349936a32f9dd Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 18:32:30 2020 +0300 all: cover with tests commit 252d81fc8a50a91b02cf0f6f35cc22178a2a4d90 Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 17:32:01 2020 +0300 all: cover with tests
2020-11-16 16:45:31 +00:00
log.SetOutput(w)
}
// ReplaceLogLevel sets logging level to l and uses Cleanup method of t to
// revert changes.
func ReplaceLogLevel(t testing.TB, l log.Level) {
t.Helper()
Pull request: cover with tests Merge in DNS/adguard-home from 2271-cover-with-tests to master Updates #2271. Squashed commit of the following: commit db6440efe05171bc15367a2996521848ca348053 Merge: db7fa726b bf4c256c7 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 19:23:09 2020 +0300 Merge branch 'master' into 2271-cover-with-tests commit db7fa726bb91b08ec7aaa6c0c818c88b5feb87cd Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 18:26:51 2020 +0300 all: clean dependencies sum commit b8dc6078c4bcc0de1b7e9073832de122f6fe38a4 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 16:46:00 2020 +0300 testutil: improve code quality commit 001b7194682b1f00aa54dc5a28236faed5a5b02d Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 16:29:49 2020 +0300 testutil: enhance functionality commit f6ccd91a4df6c56778eab8ae50e88e3818b20dd3 Merge: 43fa2eefb 6358240e9 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:57:37 2020 +0300 Merge branch 'master' into 2271-cover-with-tests commit 43fa2eefbc10ef361603cacc1ca12092b12a057a Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 14:55:15 2020 +0300 querylog: replace fake log with real in tests commit b95bee7565a14a02c80c78131b3ced224663dd8a Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 12:38:59 2020 +0300 dnsfilter: replace thoughtless declaration with idiomatic one commit a210b1586092e7ae91a9e67c972fa2d2f6baded6 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 19:00:25 2020 +0300 all: refresh golibs dependencies commit 4ff97bd1ade6c80e274ff5716e44df4eba55bdd9 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 18:38:47 2020 +0300 all: remove std log commit 542dbda10fefce9f46d15489712b163d919b1291 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 13:46:39 2020 +0300 querylog: improve test logic and readability commit 796d402385925e8e62a1b4c7bf56e4ceec22418c Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 19:06:42 2020 +0300 all: improve code quality commit e81894c11ef15b0453e8e5297f1349936a32f9dd Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 18:32:30 2020 +0300 all: cover with tests commit 252d81fc8a50a91b02cf0f6f35cc22178a2a4d90 Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 17:32:01 2020 +0300 all: cover with tests
2020-11-16 16:45:31 +00:00
switch l {
case log.INFO, log.DEBUG, log.ERROR:
// Go on.
default:
t.Fatalf("wrong l value (must be one of %v, %v, %v)", log.INFO, log.DEBUG, log.ERROR)
}
prev := log.GetLevel()
t.Cleanup(func() { log.SetLevel(prev) })
Pull request: cover with tests Merge in DNS/adguard-home from 2271-cover-with-tests to master Updates #2271. Squashed commit of the following: commit db6440efe05171bc15367a2996521848ca348053 Merge: db7fa726b bf4c256c7 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 19:23:09 2020 +0300 Merge branch 'master' into 2271-cover-with-tests commit db7fa726bb91b08ec7aaa6c0c818c88b5feb87cd Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 18:26:51 2020 +0300 all: clean dependencies sum commit b8dc6078c4bcc0de1b7e9073832de122f6fe38a4 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 16:46:00 2020 +0300 testutil: improve code quality commit 001b7194682b1f00aa54dc5a28236faed5a5b02d Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 16:29:49 2020 +0300 testutil: enhance functionality commit f6ccd91a4df6c56778eab8ae50e88e3818b20dd3 Merge: 43fa2eefb 6358240e9 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:57:37 2020 +0300 Merge branch 'master' into 2271-cover-with-tests commit 43fa2eefbc10ef361603cacc1ca12092b12a057a Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 14:55:15 2020 +0300 querylog: replace fake log with real in tests commit b95bee7565a14a02c80c78131b3ced224663dd8a Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 12:38:59 2020 +0300 dnsfilter: replace thoughtless declaration with idiomatic one commit a210b1586092e7ae91a9e67c972fa2d2f6baded6 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 19:00:25 2020 +0300 all: refresh golibs dependencies commit 4ff97bd1ade6c80e274ff5716e44df4eba55bdd9 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 18:38:47 2020 +0300 all: remove std log commit 542dbda10fefce9f46d15489712b163d919b1291 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 13:46:39 2020 +0300 querylog: improve test logic and readability commit 796d402385925e8e62a1b4c7bf56e4ceec22418c Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 19:06:42 2020 +0300 all: improve code quality commit e81894c11ef15b0453e8e5297f1349936a32f9dd Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 18:32:30 2020 +0300 all: cover with tests commit 252d81fc8a50a91b02cf0f6f35cc22178a2a4d90 Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 17:32:01 2020 +0300 all: cover with tests
2020-11-16 16:45:31 +00:00
log.SetLevel(l)
}
2023-07-26 11:18:44 +01:00
// HostToIPs is a helper that generates one IPv4 and one IPv6 address from host.
2023-09-07 15:13:48 +01:00
func HostToIPs(host string) (ipv4, ipv6 netip.Addr) {
2023-07-26 11:18:44 +01:00
hash := sha256.Sum256([]byte(host))
2023-09-07 15:13:48 +01:00
return netip.AddrFrom4([4]byte(hash[:4])), netip.AddrFrom16([16]byte(hash[4:20]))
2023-07-26 11:18:44 +01:00
}
2023-10-11 15:31:41 +01:00
// StartHTTPServer is a helper that starts the HTTP server, which is configured
// to return data on every request, and returns the client and server URL.
func StartHTTPServer(t testing.TB, data []byte) (c *http.Client, u *url.URL) {
t.Helper()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write(data)
}))
t.Cleanup(srv.Close)
u, err := url.Parse(srv.URL)
require.NoError(t, err)
return srv.Client(), u
}
2024-03-12 14:45:11 +00:00
// testTimeout is a timeout for tests.
//
// TODO(e.burkov): Move into agdctest.
const testTimeout = 1 * time.Second
// StartLocalhostUpstream is a test helper that starts a DNS server on
// localhost.
func StartLocalhostUpstream(t *testing.T, h dns.Handler) (addr *url.URL) {
t.Helper()
startCh := make(chan netip.AddrPort)
defer close(startCh)
errCh := make(chan error)
srv := &dns.Server{
Addr: "127.0.0.1:0",
Net: string(proxy.ProtoTCP),
Handler: h,
ReadTimeout: testTimeout,
WriteTimeout: testTimeout,
}
srv.NotifyStartedFunc = func() {
addrPort := srv.Listener.Addr()
startCh <- netutil.NetAddrToAddrPort(addrPort)
}
go func() { errCh <- srv.ListenAndServe() }()
select {
case addrPort := <-startCh:
addr = &url.URL{
Scheme: string(proxy.ProtoTCP),
Host: addrPort.String(),
}
testutil.CleanupAndRequireSuccess(t, func() (err error) { return <-errCh })
testutil.CleanupAndRequireSuccess(t, srv.Shutdown)
case err := <-errCh:
require.NoError(t, err)
case <-time.After(testTimeout):
require.FailNow(t, "timeout exceeded")
}
return addr
}