Skip to content

Commit

Permalink
Include timeout test case
Browse files Browse the repository at this point in the history
  • Loading branch information
ycombinator committed Jan 25, 2024
1 parent 2b70234 commit b05739a
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions providers/shared/fqdn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func TestFQDN(t *testing.T) {
osHostname string
expectedFQDN string
expectedErrRegex string
timeout time.Duration
}{
// This test case depends on network, particularly DNS,
// being available. If it starts to fail often enough
Expand All @@ -46,23 +47,34 @@ func TestFQDN(t *testing.T) {
"long_nonexistent_hostname": {
osHostname: "foo.bar.elastic.co",
expectedFQDN: "",
expectedErrRegex: makeErrorRegex("foo.bar.elastic.co"),
expectedErrRegex: makeErrorRegex("foo.bar.elastic.co", false),
},
"short_nonexistent_hostname": {
osHostname: "foobarbaz",
expectedFQDN: "",
expectedErrRegex: makeErrorRegex("foobarbaz"),
expectedErrRegex: makeErrorRegex("foobarbaz", false),
},
"long_mixed_case_hostname": {
osHostname: "eLaSTic.co",
expectedFQDN: "elastic.co",
expectedErrRegex: "",
},
"nonexistent_timeout": {
osHostname: "foobarbaz",
expectedFQDN: "",
expectedErrRegex: makeErrorRegex("foobarbaz", true),
timeout: 1 * time.Millisecond,
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
timeout := test.timeout
if timeout == 0 {
timeout = 10 * time.Second
}

ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

actualFQDN, err := fqdn(ctx, test.osHostname)
Expand All @@ -77,11 +89,16 @@ func TestFQDN(t *testing.T) {
}
}

func makeErrorRegex(osHostname string) string {
func makeErrorRegex(osHostname string, withTimeout bool) string {
timeoutStr := ".*"
if withTimeout {
timeoutStr = ": i/o timeout"
}

return fmt.Sprintf(
"could not get FQDN, all methods failed: "+
"failed looking up CNAME: lookup %s.*: "+
"failed looking up IP: lookup %s.*",
"failed looking up IP: lookup %s"+timeoutStr,
osHostname,
osHostname,
)
Expand Down

0 comments on commit b05739a

Please sign in to comment.