Add (and fix from) tests
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/pr Build is passing Details

Signed-off-by: Sam Therapy <sam@samtherapy.net>
This commit is contained in:
Sam Therapy 2022-07-24 19:47:51 +02:00
parent 20d3196a8c
commit d820b75db1
Signed by: sam
GPG Key ID: 4D8B07C18F31ACBD
3 changed files with 43 additions and 11 deletions

View File

@ -46,7 +46,7 @@ func ParseCLI(version string) (Options, error) {
reverse = flag.Bool("reverse", false, "do a reverse lookup", flag.OptShorthand('x'))
timeout = flag.Float32("timeout", 1, "Timeout, in `seconds`")
retry = flag.Int("retry", 2, "number of `times` to retry")
retry = flag.Int("retries", 2, "number of `times` to retry")
dnssec = flag.Bool("dnssec", false, "enable DNSSEC", flag.OptShorthand('D'))
truncate = flag.Bool("no-truncate", false, "ignore truncation if a UDP request truncates (default= retry with TCP)")
@ -130,15 +130,6 @@ func ParseCLI(version string) (Options, error) {
},
}
// Set timeout to 0.5 seconds if set below 0.5
if float32(opts.Request.Timeout) < (0.5 * float32(time.Second)) {
opts.Request.Timeout = (time.Second / 2)
}
if opts.Request.Retries < 0 {
opts.Request.Timeout = 0
}
opts.Logger.Info("POSIX flags parsed")
opts.Logger.Debug(fmt.Sprintf("%+v", opts))
@ -166,5 +157,14 @@ func ParseCLI(version string) (Options, error) {
}
opts.Logger.Info("Port set to", opts.Port)
// Set timeout to 0.5 seconds if set below 0.5
if opts.Request.Timeout < (time.Second / 2) {
opts.Request.Timeout = (time.Second / 2)
}
if opts.Request.Retries < 0 {
opts.Request.Retries = 0
}
return opts, nil
}

View File

@ -5,6 +5,7 @@ package cli_test
import (
"os"
"testing"
"time"
"git.froth.zone/sam/awl/cli"
"gotest.tools/v3/assert"
@ -53,6 +54,37 @@ func TestVersion(t *testing.T) {
os.Args = old
}
func TestTimeout(t *testing.T) {
args := [][]string{
{"awl", "+timeout=0"},
{"awl", "--timeout", "0"},
}
for _, test := range args {
old := os.Args
os.Args = test
opt, err := cli.ParseCLI("TEST")
assert.NilError(t, err)
assert.Equal(t, opt.Request.Timeout, time.Second/2)
os.Args = old
}
}
func TestRetries(t *testing.T) {
args := [][]string{
{"awl", "+retry=-2"},
{"awl", "+tries=-2"},
{"awl", "--retries", "-2"},
}
for _, test := range args {
old := os.Args
os.Args = test
opt, err := cli.ParseCLI("TEST")
assert.NilError(t, err)
assert.Equal(t, opt.Request.Retries, 0)
os.Args = old
}
}
func FuzzFlags(f *testing.F) {
testcases := []string{"git.froth.zone", "", "!12345", "google.com.edu.org.fr"}
for _, tc := range testcases {

View File

@ -86,7 +86,7 @@ _Default Ports_:
Set the timeout period. Floating point numbers are accepted.++
0.5 seconds is the minimum.
*--retry* _int_, *+tries*=_int_, *+ retry*=_int_++
*--retries* _int_, *+tries*=_int_, *+ retry*=_int_++
Set the number of retries.++
Retry is one more than tries, dig style