refactor: Make tests less ugly #102

Merged
sam merged 12 commits from refactor-tests-pretty into master 2022-09-21 23:08:32 +00:00
8 changed files with 170 additions and 103 deletions
Showing only changes of commit 193a41ed9c - Show all commits

View file

@ -11,51 +11,64 @@ import (
)
func TestEmpty(t *testing.T) {
t.Parallel()
args := []string{"awl", "-4"}
opts, err := cli.ParseCLI(args, "TEST")
assert.NilError(t, err)
assert.Equal(t, opts.Request.Port, 53)
assert.Assert(t, opts.IPv4)
assert.Equal(t, opts.Request.Port, 53)
}
func TestTLSPort(t *testing.T) {
t.Parallel()
args := []string{"awl", "-T"}
opts, err := cli.ParseCLI(args, "TEST")
assert.NilError(t, err)
assert.Equal(t, opts.Request.Port, 853)
}
func TestSubnet(t *testing.T) {
args := []string{"awl", "--subnet", "127.0.0.1/32"}
func TestValidSubnet(t *testing.T) {
t.Parallel()
opts, err := cli.ParseCLI(args, "TEST")
tests := []struct {
args []string
want uint16
}{
{[]string{"awl", "--subnet", "127.0.0.1/32"}, uint16(1)},
{[]string{"awl", "--subnet", "0"}, uint16(1)},
{[]string{"awl", "--subnet", "::/0"}, uint16(2)},
}
assert.NilError(t, err)
assert.Equal(t, opts.EDNS.Subnet.Family, uint16(1))
for _, test := range tests {
test := test
args = []string{"awl", "--subnet", "0"}
t.Run("", func(t *testing.T) {
t.Parallel()
opts, err = cli.ParseCLI(args, "TEST")
assert.NilError(t, err)
assert.Equal(t, opts.EDNS.Subnet.Family, uint16(1))
opts, err := cli.ParseCLI(test.args, "TEST")
args = []string{"awl", "--subnet", "::/0"}
assert.NilError(t, err)
assert.Equal(t, opts.EDNS.Subnet.Family, test.want)
})
}
}
opts, err = cli.ParseCLI(args, "TEST")
assert.NilError(t, err)
assert.Equal(t, opts.EDNS.Subnet.Family, uint16(2))
func TestInvalidSubnet(t *testing.T) {
t.Parallel()
args = []string{"awl", "--subnet", "/"}
args := []string{"awl", "--subnet", "/"}
opts, err = cli.ParseCLI(args, "TEST")
_, err := cli.ParseCLI(args, "TEST")
assert.ErrorContains(t, err, "EDNS subnet")
}
func TestMBZ(t *testing.T) {
t.Parallel()
args := []string{"awl", "--zflag", "G"}
_, err := cli.ParseCLI(args, "TEST")
@ -64,6 +77,8 @@ func TestMBZ(t *testing.T) {
}
func TestInvalidFlag(t *testing.T) {
t.Parallel()
args := []string{"awl", "--treebug"}
_, err := cli.ParseCLI(args, "TEST")
@ -72,6 +87,8 @@ func TestInvalidFlag(t *testing.T) {
}
func TestInvalidDig(t *testing.T) {
t.Parallel()
args := []string{"awl", "+a"}
_, err := cli.ParseCLI(args, "TEST")
@ -80,6 +97,8 @@ func TestInvalidDig(t *testing.T) {
}
func TestVersion(t *testing.T) {
t.Parallel()
args := []string{"awl", "--version"}
_, err := cli.ParseCLI(args, "test")
@ -88,6 +107,8 @@ func TestVersion(t *testing.T) {
}
func TestTimeout(t *testing.T) {
t.Parallel()
args := [][]string{
{"awl", "+timeout=0"},
{"awl", "--timeout", "0"},
@ -95,14 +116,20 @@ func TestTimeout(t *testing.T) {
for _, test := range args {
test := test
opt, err := cli.ParseCLI(test, "TEST")
t.Run(test[1], func(t *testing.T) {
t.Parallel()
assert.NilError(t, err)
assert.Equal(t, opt.Request.Timeout, time.Second/2)
opt, err := cli.ParseCLI(test, "TEST")
assert.NilError(t, err)
assert.Equal(t, opt.Request.Timeout, time.Second/2)
})
}
}
func TestRetries(t *testing.T) {
t.Parallel()
args := [][]string{
{"awl", "+retry=-2"},
{"awl", "+tries=-2"},
@ -111,10 +138,14 @@ func TestRetries(t *testing.T) {
for _, test := range args {
test := test
opt, err := cli.ParseCLI(test, "TEST")
t.Run(test[1], func(t *testing.T) {
t.Parallel()
assert.NilError(t, err)
assert.Equal(t, opt.Request.Retries, 0)
opt, err := cli.ParseCLI(test, "TEST")
assert.NilError(t, err)
assert.Equal(t, opt.Request.Retries, 0)
})
}
}

View file

@ -3,7 +3,6 @@
package cli_test
import (
"strconv"
"testing"
"git.froth.zone/sam/awl/cli"
@ -87,7 +86,7 @@ func TestDefaultServer(t *testing.T) {
in string
want string
}{
{"DNSCRYPT", "sdns://AQMAAAAAAAAAETk0LjE0MC4xNC4xNDo1NDQzINErR_JS3PLCu_iZEIbq95zkSV2LFsigxDIuUso_OQhzIjIuZG5zY3J5cHQuZGVmYXVsdC5uczEuYWRndWFyZC5jb20"},
{"DNSCrypt", "sdns://AQMAAAAAAAAAETk0LjE0MC4xNC4xNDo1NDQzINErR_JS3PLCu_iZEIbq95zkSV2LFsigxDIuUso_OQhzIjIuZG5zY3J5cHQuZGVmYXVsdC5uczEuYWRndWFyZC5jb20"},
{"TLS", "dns.google"},
{"HTTPS", "https://dns.cloudflare.com/dns-query"},
{"QUIC", "dns.adguard.com"},
@ -95,13 +94,14 @@ func TestDefaultServer(t *testing.T) {
for _, test := range tests {
test := test
t.Run(test.in, func(t *testing.T) {
t.Parallel()
args := []string{}
opts := new(util.Options)
opts.Logger = util.InitLogger(0)
switch test.in {
case "DNSCRYPT":
case "DNSCrypt":
opts.DNSCrypt = true
case "TLS":
opts.TLS = true
@ -121,38 +121,48 @@ func TestFlagSetting(t *testing.T) {
t.Parallel()
tests := []struct {
in []string
in string
expected string
over string
}{
{[]string{"@sdns://AQMAAAAAAAAAETk0LjE0MC4xNC4xNDo1NDQzINErR_JS3PLCu_iZEIbq95zkSV2LFsigxDIuUso_OQhzIjIuZG5zY3J5cHQuZGVmYXVsdC5uczEuYWRndWFyZC5jb20"}},
{[]string{"@tls://dns.google"}},
{[]string{"@https://dns.cloudflare.com/dns-query"}},
{[]string{"@quic://dns.adguard.com"}},
{[]string{"@tcp://dns.froth.zone"}},
{[]string{"@udp://dns.example.com"}},
{"@sdns://AQMAAAAAAAAAETk0LjE0MC4xNC4xNDo1NDQzINErR_JS3PLCu_iZEIbq95zkSV2LFsigxDIuUso_OQhzIjIuZG5zY3J5cHQuZGVmYXVsdC5uczEuYWRndWFyZC5jb20", "sdns://AQMAAAAAAAAAETk0LjE0MC4xNC4xNDo1NDQzINErR_JS3PLCu_iZEIbq95zkSV2LFsigxDIuUso_OQhzIjIuZG5zY3J5cHQuZGVmYXVsdC5uczEuYWRndWFyZC5jb20", "DNSCrypt"},
{"@tls://dns.google", "dns.google", "TLS"},
{"@https://dns.cloudflare.com/dns-query", "https://dns.cloudflare.com/dns-query", "HTTPS"},
{"@quic://dns.adguard.com", "dns.adguard.com", "QUIC"},
{"@tcp://dns.froth.zone", "dns.froth.zone", "TCP"},
{"@udp://dns.example.com", "dns.example.com", "UDP"},
}
for i, test := range tests {
for _, test := range tests {
test := test
i := i
t.Run(strconv.Itoa(i), func(t *testing.T) {
t.Run(test.over, func(t *testing.T) {
t.Parallel()
opts := new(util.Options)
opts.Logger = util.InitLogger(0)
t.Parallel()
err := cli.ParseMiscArgs(test.in, opts)
err := cli.ParseMiscArgs([]string{test.in}, opts)
assert.NilError(t, err)
switch i {
case 0:
switch test.over {
case "DNSCrypt":
assert.Assert(t, opts.DNSCrypt)
case 1:
assert.Equal(t, opts.Request.Server, test.expected)
case "TLS":
assert.Assert(t, opts.TLS)
case 2:
assert.Equal(t, opts.Request.Server, test.expected)
case "HTTPS":
assert.Assert(t, opts.HTTPS)
case 3:
assert.Equal(t, opts.Request.Server, test.expected)
case "QUIC":
assert.Assert(t, opts.QUIC)
case 4:
assert.Equal(t, opts.Request.Server, test.expected)
case "TCP":
assert.Assert(t, opts.TCP)
case 5:
assert.Equal(t, opts.Request.Server, test.expected)
case "UDP":
assert.Assert(t, true)
assert.Equal(t, opts.Request.Server, test.expected)
}
})
}

View file

@ -4,53 +4,73 @@
package conf_test
import (
"runtime"
"testing"
"git.froth.zone/sam/awl/conf"
"gotest.tools/v3/assert"
)
func TestGetPlan9Config(t *testing.T) {
func TestPlan9Config(t *testing.T) {
t.Parallel()
if runtime.GOOS != "plan9" {
t.Skip("Not running Plan 9, skipping")
}
ndbs := []struct {
in string
want string
}{
{`ip=192.168.122.45 ipmask=255.255.255.0 ipgw=192.168.122.1
sys=chog9
dns=192.168.122.1`, "192.168.122.1"},
{`ipnet=murray-hill ip=135.104.0.0 ipmask=255.255.0.0
dns=135.104.10.1
ntp=ntp.cs.bell-labs.com
ipnet=plan9 ip=135.104.9.0 ipmask=255.255.255.0
ntp=oncore.cs.bell-labs.com
smtp=smtp1.cs.bell-labs.com
ip=135.104.9.6 sys=anna dom=anna.cs.bell-labs.com
smtp=smtp2.cs.bell-labs.com`, "135.104.10.1"},
}
conf, err := conf.GetDNSConfig()
for _, ndb := range ndbs {
// Go is a little quirky
ndb := ndb
t.Run(ndb.want, func(t *testing.T) {
t.Parallel()
act, err := conf.GetPlan9Config(ndb.in)
assert.NilError(t, err)
assert.Equal(t, ndb.want, act.Servers[0])
})
}
invalid := `sys = spindle
dom=spindle.research.bell-labs.com
bootf=/mips/9powerboot
ip=135.104.117.32 ether=080069020677
proto=il`
act, err := conf.GetPlan9Config(invalid)
assert.ErrorContains(t, err, "no DNS servers found")
assert.Assert(t, act == nil)
assert.NilError(t, err)
assert.Assert(t, len(conf.Servers) != 0)
}
// Old config grabber tests
// func TestGetValidPlan9Config(t *testing.T) {
// t.Parallel()
// if runtime.GOOS != "plan9" {
// t.Skip("Not running Plan 9, skipping")
// }
// ndbs := []struct {
// in string
// want string
// }{
// {`ip=192.168.122.45 ipmask=255.255.255.0 ipgw=192.168.122.1
// sys=chog9
// dns=192.168.122.1`, "192.168.122.1"},
// {`ipnet=murray-hill ip=135.104.0.0 ipmask=255.255.0.0
// dns=135.104.10.1
// ntp=ntp.cs.bell-labs.com
// ipnet=plan9 ip=135.104.9.0 ipmask=255.255.255.0
// ntp=oncore.cs.bell-labs.com
// smtp=smtp1.cs.bell-labs.com
// ip=135.104.9.6 sys=anna dom=anna.cs.bell-labs.com
// smtp=smtp2.cs.bell-labs.com`, "135.104.10.1"},
// }
// for _, ndb := range ndbs {
// // Go is a little quirky
// ndb := ndb
// t.Run(ndb.want, func(t *testing.T) {
// t.Parallel()
// act, err := conf.GetDNSConfig(ndb.in)
// assert.NilError(t, err)
// assert.Equal(t, ndb.want, act.Servers[0])
// })
// }
// }
// func TestInvalidPlan9Config(t *testing.T) {
// invalid := `sys = spindle
// dom=spindle.research.bell-labs.com
// bootf=/mips/9powerboot
// ip=135.104.117.32 ether=080069020677
// proto=il`
// act, err := conf.GetDNSConfig(invalid)
// assert.ErrorContains(t, err, "no DNS servers found")
// assert.Assert(t, act == nil)
// }

View file

@ -13,8 +13,10 @@ import (
"gotest.tools/v3/assert"
)
func TestNonWinConfig(t *testing.T) {
if runtime.GOOS == "windows" || runtime.GOOS == "plan9" {
func TestUnixConfig(t *testing.T) {
t.Parallel()
if runtime.GOOS == "windows" || runtime.GOOS == "plan9" || runtime.GOOS == "js" || runtime.GOOS == "zos" {
t.Skip("Not running Unix-like, skipping")
}

View file

@ -3,31 +3,35 @@
package main
import (
"os"
"testing"
"github.com/stefansundin/go-zflag"
"gotest.tools/v3/assert"
)
func TestMain(t *testing.T) { //nolint: paralleltest // Race conditions
os.Stdout = os.NewFile(0, os.DevNull)
os.Stderr = os.NewFile(0, os.DevNull)
func TestRun(t *testing.T) {
t.Parallel()
args := []string{"awl", "+yaml", "@1.1.1.1"}
args := [][]string{
{"awl", "+yaml", "@1.1.1.1"},
{"awl", "+short", "@1.1.1.1"},
}
_, code, err := run(args)
assert.NilError(t, err)
assert.Equal(t, code, 0)
for _, test := range args {
test := test
args = []string{"awl", "+short", "@1.1.1.1"}
_, code, err = run(args)
assert.NilError(t, err)
assert.Equal(t, code, 0)
t.Run("", func(t *testing.T) {
t.Parallel()
_, code, err := run(test)
assert.NilError(t, err)
assert.Equal(t, code, 0)
})
}
}
func TestHelp(t *testing.T) {
t.Parallel()
args := []string{"awl", "-h"}
_, code, err := run(args)

View file

@ -22,10 +22,10 @@ func TestQuic(t *testing.T) {
opts := util.Options{
QUIC: true,
Logger: util.InitLogger(0),
Request: util.Request{Server: "dns.adguard.com", Port: 853},
Request: util.Request{Server: "dns.adguard.com", Port: 853, Timeout: 500 * time.Millisecond},
}
testCase := util.Request{Server: "dns.//./,,adguard.com", Type: dns.TypeA, Name: "git.froth.zone"}
testCase2 := util.Request{Server: "dns.adguard.com", Type: dns.TypeA, Name: "git.froth.zone"}
testCase := util.Request{Server: "dns.//./,,adguard.com", Port: 853, Type: dns.TypeA, Name: "git.froth.zone", Timeout: 100 * time.Millisecond}
testCase2 := util.Request{Server: "dns.adguard.com", Port: 853, Type: dns.TypeA, Name: "git.froth.zone", Timeout: 100 * time.Millisecond}
var testCases []util.Request

View file

@ -22,7 +22,7 @@ func TestResolve(t *testing.T) {
Port: 1,
Type: dns.TypeA,
Name: "example.com.",
Timeout: time.Second / 2,
Timeout: time.Millisecond * 100,
Retries: 0,
},
}

View file

@ -99,7 +99,7 @@ func TestRealPrint(t *testing.T) {
Authority: true,
Additional: true,
Statistics: true,
UcodeTranslate: false,
UcodeTranslate: true,
TTL: true,
HumanTTL: true,
ShowQuery: true,