85 lines
1.7 KiB
Go
85 lines
1.7 KiB
Go
package utils
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestFuzzyAgo(t *testing.T) {
|
|
|
|
cases := map[string]string{
|
|
"1s": "less than a minute ago",
|
|
"30s": "less than a minute ago",
|
|
"1m08s": "about 1 minute ago",
|
|
"15m0s": "about 15 minutes ago",
|
|
"59m10s": "about 59 minutes ago",
|
|
"1h10m02s": "about 1 hour ago",
|
|
"15h0m01s": "about 15 hours ago",
|
|
"30h10m": "about 1 day ago",
|
|
"50h": "about 2 days ago",
|
|
"720h05m": "about 1 month ago",
|
|
"3000h10m": "about 4 months ago",
|
|
"8760h59m": "about 1 year ago",
|
|
"17601h59m": "about 2 years ago",
|
|
"262800h19m": "about 30 years ago",
|
|
}
|
|
|
|
for duration, expected := range cases {
|
|
d, e := time.ParseDuration(duration)
|
|
if e != nil {
|
|
t.Errorf("failed to create a duration: %s", e)
|
|
}
|
|
|
|
fuzzy := FuzzyAgo(d)
|
|
if fuzzy != expected {
|
|
t.Errorf("unexpected fuzzy duration value: %s for %s", fuzzy, duration)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestHostnameValidator(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input interface{}
|
|
wantsErr bool
|
|
}{
|
|
{
|
|
name: "valid hostname",
|
|
input: "internal.instance",
|
|
wantsErr: false,
|
|
},
|
|
{
|
|
name: "hostname with slashes",
|
|
input: "//internal.instance",
|
|
wantsErr: true,
|
|
},
|
|
{
|
|
name: "empty hostname",
|
|
input: " ",
|
|
wantsErr: true,
|
|
},
|
|
{
|
|
name: "hostname with colon",
|
|
input: "internal.instance:2205",
|
|
wantsErr: true,
|
|
},
|
|
{
|
|
name: "non-string hostname",
|
|
input: 62,
|
|
wantsErr: true,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
err := HostnameValidator(tt.input)
|
|
if tt.wantsErr {
|
|
assert.Error(t, err)
|
|
return
|
|
}
|
|
assert.Equal(t, nil, err)
|
|
})
|
|
}
|
|
}
|