فهرست منبع

Merge pull request #46356 from thaJeztah/registry_cleanup_v1strip

registry: simplify `trimV1Address`
Sebastiaan van Stijn 1 سال پیش
والد
کامیت
69c19cf0b0
2فایلهای تغییر یافته به همراه54 افزوده شده و 49 حذف شده
  1. 48 34
      registry/endpoint_test.go
  2. 6 15
      registry/endpoint_v1.go

+ 48 - 34
registry/endpoint_test.go

@@ -5,44 +5,58 @@ import (
 	"net/http/httptest"
 	"net/url"
 	"testing"
+
+	"gotest.tools/v3/assert"
+	is "gotest.tools/v3/assert/cmp"
 )
 
-func TestEndpointParse(t *testing.T) {
-	testData := []struct {
-		str      string
-		expected string
+func TestV1EndpointParse(t *testing.T) {
+	tests := []struct {
+		address     string
+		expected    string
+		expectedErr string
 	}{
-		{IndexServer, IndexServer},
-		{"http://0.0.0.0:5000/v1/", "http://0.0.0.0:5000/v1/"},
-		{"http://0.0.0.0:5000", "http://0.0.0.0:5000/v1/"},
-		{"0.0.0.0:5000", "https://0.0.0.0:5000/v1/"},
-		{"http://0.0.0.0:5000/nonversion/", "http://0.0.0.0:5000/nonversion/v1/"},
-		{"http://0.0.0.0:5000/v0/", "http://0.0.0.0:5000/v0/v1/"},
-	}
-	for _, td := range testData {
-		e, err := newV1EndpointFromStr(td.str, nil, nil)
-		if err != nil {
-			t.Errorf("%q: %s", td.str, err)
-		}
-		if e == nil {
-			t.Logf("something's fishy, endpoint for %q is nil", td.str)
-			continue
-		}
-		if e.String() != td.expected {
-			t.Errorf("expected %q, got %q", td.expected, e.String())
-		}
-	}
-}
-
-func TestEndpointParseInvalid(t *testing.T) {
-	testData := []string{
-		"http://0.0.0.0:5000/v2/",
+		{
+			address:  IndexServer,
+			expected: IndexServer,
+		},
+		{
+			address:  "https://0.0.0.0:5000/v1/",
+			expected: "https://0.0.0.0:5000/v1/",
+		},
+		{
+			address:  "https://0.0.0.0:5000",
+			expected: "https://0.0.0.0:5000/v1/",
+		},
+		{
+			address:  "0.0.0.0:5000",
+			expected: "https://0.0.0.0:5000/v1/",
+		},
+		{
+			address:  "https://0.0.0.0:5000/nonversion/",
+			expected: "https://0.0.0.0:5000/nonversion/v1/",
+		},
+		{
+			address:  "https://0.0.0.0:5000/v0/",
+			expected: "https://0.0.0.0:5000/v0/v1/",
+		},
+		{
+			address:     "https://0.0.0.0:5000/v2/",
+			expectedErr: "search is not supported on v2 endpoints: https://0.0.0.0:5000/v2/",
+		},
 	}
-	for _, td := range testData {
-		e, err := newV1EndpointFromStr(td, nil, nil)
-		if err == nil {
-			t.Errorf("expected error parsing %q: parsed as %q", td, e)
-		}
+	for _, tc := range tests {
+		tc := tc
+		t.Run(tc.address, func(t *testing.T) {
+			ep, err := newV1EndpointFromStr(tc.address, nil, nil)
+			if tc.expectedErr != "" {
+				assert.Check(t, is.Error(err, tc.expectedErr))
+				assert.Check(t, is.Nil(ep))
+			} else {
+				assert.NilError(t, err)
+				assert.Check(t, is.Equal(ep.String(), tc.expected))
+			}
+		})
 	}
 }
 

+ 6 - 15
registry/endpoint_v1.go

@@ -82,23 +82,14 @@ func validateEndpoint(endpoint *v1Endpoint) error {
 	return nil
 }
 
-// trimV1Address trims the version off the address and returns the
-// trimmed address or an error if there is a non-V1 version.
+// trimV1Address trims the "v1" version suffix off the address and returns
+// the trimmed address. It returns an error on "v2" endpoints.
 func trimV1Address(address string) (string, error) {
-	address = strings.TrimSuffix(address, "/")
-	chunks := strings.Split(address, "/")
-	apiVersionStr := chunks[len(chunks)-1]
-	if apiVersionStr == "v1" {
-		return strings.Join(chunks[:len(chunks)-1], "/"), nil
+	trimmed := strings.TrimSuffix(address, "/")
+	if strings.HasSuffix(trimmed, "/v2") {
+		return "", invalidParamf("search is not supported on v2 endpoints: %s", address)
 	}
-
-	for k, v := range apiVersions {
-		if k != APIVersion1 && apiVersionStr == v {
-			return "", invalidParamf("unsupported V1 version path %s", apiVersionStr)
-		}
-	}
-
-	return address, nil
+	return strings.TrimSuffix(trimmed, "/v1"), nil
 }
 
 func newV1EndpointFromStr(address string, tlsConfig *tls.Config, headers http.Header) (*v1Endpoint, error) {