Browse Source

registry: move v1 endpoint tests to endpoint_test.go

Moves the TestPingRegistryEndpoint and TestEndpoint tests.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Sebastiaan van Stijn 2 years ago
parent
commit
19f6f0b3db
2 changed files with 96 additions and 93 deletions
  1. 96 0
      registry/endpoint_test.go
  2. 0 93
      registry/registry_test.go

+ 96 - 0
registry/endpoint_test.go

@@ -4,12 +4,108 @@ import (
 	"net/http"
 	"net/http/httptest"
 	"net/url"
+	"os"
+	"strings"
 	"testing"
 
+	"github.com/docker/docker/api/types/registry"
 	"gotest.tools/v3/assert"
 	is "gotest.tools/v3/assert/cmp"
+	"gotest.tools/v3/skip"
 )
 
+func TestPingRegistryEndpoint(t *testing.T) {
+	skip.If(t, os.Getuid() != 0, "skipping test that requires root")
+	testPing := func(index *registry.IndexInfo, expectedStandalone bool, assertMessage string) {
+		ep, err := newV1Endpoint(index, nil)
+		if err != nil {
+			t.Fatal(err)
+		}
+		regInfo, err := ep.ping()
+		if err != nil {
+			t.Fatal(err)
+		}
+
+		assert.Equal(t, regInfo.Standalone, expectedStandalone, assertMessage)
+	}
+
+	testPing(makeIndex("/v1/"), true, "Expected standalone to be true (default)")
+	testPing(makeHTTPSIndex("/v1/"), true, "Expected standalone to be true (default)")
+	testPing(makePublicIndex(), false, "Expected standalone to be false for public index")
+}
+
+func TestEndpoint(t *testing.T) {
+	skip.If(t, os.Getuid() != 0, "skipping test that requires root")
+	// Simple wrapper to fail test if err != nil
+	expandEndpoint := func(index *registry.IndexInfo) *v1Endpoint {
+		endpoint, err := newV1Endpoint(index, nil)
+		if err != nil {
+			t.Fatal(err)
+		}
+		return endpoint
+	}
+
+	assertInsecureIndex := func(index *registry.IndexInfo) {
+		index.Secure = true
+		_, err := newV1Endpoint(index, nil)
+		assert.ErrorContains(t, err, "insecure-registry", index.Name+": Expected insecure-registry  error for insecure index")
+		index.Secure = false
+	}
+
+	assertSecureIndex := func(index *registry.IndexInfo) {
+		index.Secure = true
+		_, err := newV1Endpoint(index, nil)
+		assert.ErrorContains(t, err, "certificate signed by unknown authority", index.Name+": Expected cert error for secure index")
+		index.Secure = false
+	}
+
+	index := &registry.IndexInfo{}
+	index.Name = makeURL("/v1/")
+	endpoint := expandEndpoint(index)
+	assert.Equal(t, endpoint.String(), index.Name, "Expected endpoint to be "+index.Name)
+	assertInsecureIndex(index)
+
+	index.Name = makeURL("")
+	endpoint = expandEndpoint(index)
+	assert.Equal(t, endpoint.String(), index.Name+"/v1/", index.Name+": Expected endpoint to be "+index.Name+"/v1/")
+	assertInsecureIndex(index)
+
+	httpURL := makeURL("")
+	index.Name = strings.SplitN(httpURL, "://", 2)[1]
+	endpoint = expandEndpoint(index)
+	assert.Equal(t, endpoint.String(), httpURL+"/v1/", index.Name+": Expected endpoint to be "+httpURL+"/v1/")
+	assertInsecureIndex(index)
+
+	index.Name = makeHTTPSURL("/v1/")
+	endpoint = expandEndpoint(index)
+	assert.Equal(t, endpoint.String(), index.Name, "Expected endpoint to be "+index.Name)
+	assertSecureIndex(index)
+
+	index.Name = makeHTTPSURL("")
+	endpoint = expandEndpoint(index)
+	assert.Equal(t, endpoint.String(), index.Name+"/v1/", index.Name+": Expected endpoint to be "+index.Name+"/v1/")
+	assertSecureIndex(index)
+
+	httpsURL := makeHTTPSURL("")
+	index.Name = strings.SplitN(httpsURL, "://", 2)[1]
+	endpoint = expandEndpoint(index)
+	assert.Equal(t, endpoint.String(), httpsURL+"/v1/", index.Name+": Expected endpoint to be "+httpsURL+"/v1/")
+	assertSecureIndex(index)
+
+	badEndpoints := []string{
+		"http://127.0.0.1/v1/",
+		"https://127.0.0.1/v1/",
+		"http://127.0.0.1",
+		"https://127.0.0.1",
+		"127.0.0.1",
+	}
+	for _, address := range badEndpoints {
+		index.Name = address
+		_, err := newV1Endpoint(index, nil)
+		assert.Check(t, err != nil, "Expected error while expanding bad endpoint: %s", address)
+	}
+}
+
 func TestV1EndpointParse(t *testing.T) {
 	tests := []struct {
 		address     string

+ 0 - 93
registry/registry_test.go

@@ -4,7 +4,6 @@ import (
 	"net/http"
 	"net/http/httputil"
 	"os"
-	"strings"
 	"testing"
 
 	"github.com/docker/distribution/reference"
@@ -45,98 +44,6 @@ func spawnTestRegistrySession(t *testing.T) *session {
 	return r
 }
 
-func TestPingRegistryEndpoint(t *testing.T) {
-	skip.If(t, os.Getuid() != 0, "skipping test that requires root")
-	testPing := func(index *registry.IndexInfo, expectedStandalone bool, assertMessage string) {
-		ep, err := newV1Endpoint(index, nil)
-		if err != nil {
-			t.Fatal(err)
-		}
-		regInfo, err := ep.ping()
-		if err != nil {
-			t.Fatal(err)
-		}
-
-		assert.Equal(t, regInfo.Standalone, expectedStandalone, assertMessage)
-	}
-
-	testPing(makeIndex("/v1/"), true, "Expected standalone to be true (default)")
-	testPing(makeHTTPSIndex("/v1/"), true, "Expected standalone to be true (default)")
-	testPing(makePublicIndex(), false, "Expected standalone to be false for public index")
-}
-
-func TestEndpoint(t *testing.T) {
-	skip.If(t, os.Getuid() != 0, "skipping test that requires root")
-	// Simple wrapper to fail test if err != nil
-	expandEndpoint := func(index *registry.IndexInfo) *v1Endpoint {
-		endpoint, err := newV1Endpoint(index, nil)
-		if err != nil {
-			t.Fatal(err)
-		}
-		return endpoint
-	}
-
-	assertInsecureIndex := func(index *registry.IndexInfo) {
-		index.Secure = true
-		_, err := newV1Endpoint(index, nil)
-		assert.ErrorContains(t, err, "insecure-registry", index.Name+": Expected insecure-registry  error for insecure index")
-		index.Secure = false
-	}
-
-	assertSecureIndex := func(index *registry.IndexInfo) {
-		index.Secure = true
-		_, err := newV1Endpoint(index, nil)
-		assert.ErrorContains(t, err, "certificate signed by unknown authority", index.Name+": Expected cert error for secure index")
-		index.Secure = false
-	}
-
-	index := &registry.IndexInfo{}
-	index.Name = makeURL("/v1/")
-	endpoint := expandEndpoint(index)
-	assert.Equal(t, endpoint.String(), index.Name, "Expected endpoint to be "+index.Name)
-	assertInsecureIndex(index)
-
-	index.Name = makeURL("")
-	endpoint = expandEndpoint(index)
-	assert.Equal(t, endpoint.String(), index.Name+"/v1/", index.Name+": Expected endpoint to be "+index.Name+"/v1/")
-	assertInsecureIndex(index)
-
-	httpURL := makeURL("")
-	index.Name = strings.SplitN(httpURL, "://", 2)[1]
-	endpoint = expandEndpoint(index)
-	assert.Equal(t, endpoint.String(), httpURL+"/v1/", index.Name+": Expected endpoint to be "+httpURL+"/v1/")
-	assertInsecureIndex(index)
-
-	index.Name = makeHTTPSURL("/v1/")
-	endpoint = expandEndpoint(index)
-	assert.Equal(t, endpoint.String(), index.Name, "Expected endpoint to be "+index.Name)
-	assertSecureIndex(index)
-
-	index.Name = makeHTTPSURL("")
-	endpoint = expandEndpoint(index)
-	assert.Equal(t, endpoint.String(), index.Name+"/v1/", index.Name+": Expected endpoint to be "+index.Name+"/v1/")
-	assertSecureIndex(index)
-
-	httpsURL := makeHTTPSURL("")
-	index.Name = strings.SplitN(httpsURL, "://", 2)[1]
-	endpoint = expandEndpoint(index)
-	assert.Equal(t, endpoint.String(), httpsURL+"/v1/", index.Name+": Expected endpoint to be "+httpsURL+"/v1/")
-	assertSecureIndex(index)
-
-	badEndpoints := []string{
-		"http://127.0.0.1/v1/",
-		"https://127.0.0.1/v1/",
-		"http://127.0.0.1",
-		"https://127.0.0.1",
-		"127.0.0.1",
-	}
-	for _, address := range badEndpoints {
-		index.Name = address
-		_, err := newV1Endpoint(index, nil)
-		assert.Check(t, err != nil, "Expected error while expanding bad endpoint: %s", address)
-	}
-}
-
 func TestParseRepositoryInfo(t *testing.T) {
 	type staticRepositoryInfo struct {
 		Index         *registry.IndexInfo