Skip to content

Commit

Permalink
registry: override net.LookupIP per test, not globally
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
  • Loading branch information
thaJeztah committed Nov 29, 2024
1 parent 63525a8 commit 3a732a9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 30 deletions.
6 changes: 3 additions & 3 deletions registry/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ var (
emptyServiceConfig, _ = newServiceConfig(ServiceOptions{})
validHostPortRegex = regexp.MustCompile(`^` + reference.DomainRegexp.String() + `$`)

// for mocking in unit tests
lookupIP = net.LookupIP

// certsDir is used to override defaultCertsDir.
certsDir string
)
Expand Down Expand Up @@ -285,6 +282,9 @@ func (config *serviceConfig) isSecureIndex(indexName string) bool {
return !isCIDRMatch(config.InsecureRegistryCIDRs, indexName)
}

// for mocking in unit tests.
var lookupIP = net.LookupIP

// isCIDRMatch returns true if URLHost matches an element of cidrs. URLHost is a URL.Host (`host:port` or `host`)
// where the `host` part can be either a domain name or an IP address. If it is a domain name, then it will be
// resolved to IP addresses for matching. If resolution fails, false is returned.
Expand Down
27 changes: 0 additions & 27 deletions registry/registry_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package registry // import "github.com/docker/docker/registry"
import (
"context"
"encoding/json"
"errors"
"io"
"net"
"net/http"
"net/http/httptest"
"testing"
Expand All @@ -32,31 +30,6 @@ func init() {

testHTTPServer = httptest.NewServer(handlerAccessLog(r))
testHTTPSServer = httptest.NewTLSServer(handlerAccessLog(r))

// override net.LookupIP
lookupIP = func(host string) ([]net.IP, error) {
if host == "127.0.0.1" {
// I believe in future Go versions this will fail, so let's fix it later
return net.LookupIP(host)
}
mockHosts := map[string][]net.IP{
"": {net.ParseIP("0.0.0.0")},
"localhost": {net.ParseIP("127.0.0.1"), net.ParseIP("::1")},
"example.com": {net.ParseIP("42.42.42.42")},
"other.com": {net.ParseIP("43.43.43.43")},
}
for h, addrs := range mockHosts {
if host == h {
return addrs, nil
}
for _, addr := range addrs {
if addr.String() == host {
return []net.IP{addr}, nil
}
}
}
return nil, errors.New("lookup: no such host")
}
}

func handlerAccessLog(handler http.Handler) http.Handler {
Expand Down
39 changes: 39 additions & 0 deletions registry/registry_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package registry // import "github.com/docker/docker/registry"

import (
"errors"
"net"
"testing"

"github.com/distribution/reference"
Expand All @@ -9,6 +11,40 @@ import (
is "gotest.tools/v3/assert/cmp"
)

// overrideLookupIP overrides net.LookupIP for testing.
func overrideLookupIP(t *testing.T) {
t.Helper()
restoreLookup := lookupIP

// override net.LookupIP
lookupIP = func(host string) ([]net.IP, error) {
if host == "127.0.0.1" {
// I believe in future Go versions this will fail, so let's fix it later
return net.LookupIP(host)
}
mockHosts := map[string][]net.IP{
"": {net.ParseIP("0.0.0.0")},
"localhost": {net.ParseIP("127.0.0.1"), net.ParseIP("::1")},
"example.com": {net.ParseIP("42.42.42.42")},
"other.com": {net.ParseIP("43.43.43.43")},
}
for h, addrs := range mockHosts {
if host == h {
return addrs, nil
}
for _, addr := range addrs {
if addr.String() == host {
return []net.IP{addr}, nil
}
}
}
return nil, errors.New("lookup: no such host")
}
t.Cleanup(func() {
lookupIP = restoreLookup
})
}

func TestParseRepositoryInfo(t *testing.T) {
type staticRepositoryInfo struct {
Index *registry.IndexInfo
Expand Down Expand Up @@ -242,6 +278,7 @@ func TestParseRepositoryInfo(t *testing.T) {
}

func TestNewIndexInfo(t *testing.T) {
overrideLookupIP(t)
testIndexInfo := func(config *serviceConfig, expectedIndexInfos map[string]*registry.IndexInfo) {
for indexName, expectedIndexInfo := range expectedIndexInfos {
index, err := newIndexInfo(config, indexName)
Expand Down Expand Up @@ -415,6 +452,7 @@ func TestMirrorEndpointLookup(t *testing.T) {
}

func TestAllowNondistributableArtifacts(t *testing.T) {
overrideLookupIP(t)
tests := []struct {
addr string
registries []string
Expand Down Expand Up @@ -460,6 +498,7 @@ func TestAllowNondistributableArtifacts(t *testing.T) {
}

func TestIsSecureIndex(t *testing.T) {
overrideLookupIP(t)
tests := []struct {
addr string
insecureRegistries []string
Expand Down

0 comments on commit 3a732a9

Please sign in to comment.