github.com/Schaudge/grailbase@v0.0.0-20240223061707-44c758a471c0/file/s3file/s3transport/dns_test.go (about) 1 package s3transport 2 3 import ( 4 "fmt" 5 "net" 6 "testing" 7 "time" 8 9 "github.com/stretchr/testify/assert" 10 ) 11 12 func TestResolver(t *testing.T) { 13 var ( 14 gotHost string 15 stubIP []net.IP 16 stubError error 17 stubNow time.Time 18 ) 19 stubLookupIP := func(host string) ([]net.IP, error) { 20 gotHost = host 21 return stubIP, stubError 22 } 23 r := newResolver(stubLookupIP, func() time.Time { return stubNow }) 24 25 stubIP, stubError = []net.IP{{1, 2, 3, 4}, {10, 20, 30, 40}}, nil 26 stubNow = time.Unix(1600000000, 0) 27 gotIP, gotError := r.LookupIP("s3.example.com") 28 assert.Equal(t, "s3.example.com", gotHost) 29 assert.NoError(t, gotError) 30 assert.Equal(t, []net.IP{{1, 2, 3, 4}, {10, 20, 30, 40}}, gotIP) 31 32 stubIP, stubError = nil, fmt.Errorf("stub err") 33 stubNow = stubNow.Add(dnsCacheTime - 1) 34 gotHost = "should not be called" 35 gotIP, gotError = r.LookupIP("s3.example.com") 36 assert.Equal(t, "should not be called", gotHost) 37 assert.NoError(t, gotError) 38 assert.Equal(t, []net.IP{{1, 2, 3, 4}, {10, 20, 30, 40}}, gotIP) 39 40 stubIP, stubError = []net.IP{{5, 6, 7, 8}}, nil 41 gotIP, gotError = r.LookupIP("s3-us-west-2.example.com") 42 assert.Equal(t, "s3-us-west-2.example.com", gotHost) 43 assert.NoError(t, gotError) 44 assert.Equal(t, []net.IP{{5, 6, 7, 8}}, gotIP) 45 46 stubIP, stubError = []net.IP{{21, 22, 23, 24}}, nil 47 gotHost = "" 48 stubNow = stubNow.Add(2) 49 gotIP, gotError = r.LookupIP("s3.example.com") 50 assert.Equal(t, "s3.example.com", gotHost) 51 assert.NoError(t, gotError) 52 assert.Equal(t, []net.IP{{21, 22, 23, 24}}, gotIP) 53 }