forked from genuinetools/reg
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change Ping to check for Docker header
Change Registry.Ping() to: - respect http 401 response code as valid - conform to the Specs by requiring header Docker-Distribution-API-Version Change the TokenTransport unit tests to emit that header. Remove the naive workaround inside Registry.Pingable() as iterating over all sites that do not support the old ping would require huge and constant effort. (Not only GCR.) I add separate Registry.PingClient, because the Client handles 401 by closing the body so it disables the possibility of disinguishing whether the 401 contained Docker-Distribution-API-Version header. Refactor: simplify current TestPingable Signed-off-by: Jakub Bielecki <47531708+jabielecki@users.noreply.github.com>
- Loading branch information
1 parent
180ad1c
commit 6c84d84
Showing
4 changed files
with
110 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,94 @@ | ||
package registry | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/docker/docker/api/types" | ||
) | ||
|
||
func createClientAndPing(httpCode int, headerName string, headerValue string) (*Registry, func(), error) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set(headerName, headerValue) | ||
w.WriteHeader(httpCode) | ||
})) | ||
|
||
auth := types.AuthConfig{ServerAddress: ts.URL} | ||
r, err := New(context.Background(), auth, Opt{Insecure: true}) | ||
return r, ts.Close, err | ||
} | ||
|
||
func TestPing(t *testing.T) { | ||
testcases := []struct { | ||
httpCode int | ||
headerName string | ||
headerValue string | ||
wantErr error | ||
}{ | ||
{ | ||
httpCode: 200, | ||
headerName: "whatever", | ||
headerValue: "whatever", | ||
wantErr: ErrNoDockerHeader, | ||
}, | ||
{ | ||
httpCode: 401, | ||
headerName: "wrong", | ||
headerValue: "whatever", | ||
wantErr: ErrNoDockerHeader, | ||
}, | ||
{ | ||
httpCode: 200, | ||
headerName: "docker-distribution-api-version", | ||
headerValue: "registry/2.0", | ||
wantErr: nil, | ||
}, | ||
{ | ||
httpCode: 401, | ||
headerName: "Docker-Distribution-API-Version", | ||
headerValue: "registry/2.1", | ||
// Many popular servers do allow unauthenticated image pulls, but require authentication to visit the base url. | ||
// This conforms to Docker Registry v2 API Specification https://docs.docker.com/registry/spec/api/ | ||
// Thus `401 Unauthorized` is as good response as `200 OK`, if only it has the proper Docker header. | ||
wantErr: nil, | ||
}, | ||
} | ||
for _, tc := range testcases { | ||
r, closeFunc, err := createClientAndPing(tc.httpCode, tc.headerName, tc.headerValue) | ||
defer closeFunc() | ||
if err != tc.wantErr { | ||
t.Fatalf("when creating client and performing ping for (%v, %q, %q), got error %#v but expected %#v", tc.httpCode, tc.headerName, tc.headerValue, err, tc.wantErr) | ||
} | ||
if err != nil { | ||
continue | ||
} | ||
err = r.Ping(context.Background()) | ||
if err != tc.wantErr { | ||
t.Fatalf("when repeating ping for (%v, %q, %q), got error %#v but expected %#v", tc.httpCode, tc.headerName, tc.headerValue, err, tc.wantErr) | ||
} | ||
} | ||
} | ||
|
||
func TestPingable(t *testing.T) { | ||
testcases := map[string]struct { | ||
testcases := []struct { | ||
registry Registry | ||
expect bool | ||
want bool | ||
}{ | ||
"Docker": { | ||
{ | ||
registry: Registry{URL: "https://registry-1.docker.io"}, | ||
expect: true, | ||
}, | ||
"GCR_global": { | ||
registry: Registry{URL: "https://gcr.io"}, | ||
expect: false, | ||
want: true, | ||
}, | ||
"GCR_asia": { | ||
{ | ||
registry: Registry{URL: "https://asia.gcr.io"}, | ||
expect: false, | ||
want: true, | ||
}, | ||
} | ||
for label, testcase := range testcases { | ||
actual := testcase.registry.Pingable() | ||
if testcase.expect != actual { | ||
t.Fatalf("%s: expected (%v), got (%v)", label, testcase.expect, actual) | ||
for _, testcase := range testcases { | ||
got := testcase.registry.Pingable() | ||
if testcase.want != got { | ||
t.Fatalf("%s pingable: expected (%v), got (%v)", testcase.registry.URL, testcase.want, got) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters