Browse Source

Merge pull request #46555 from thaJeztah/distribution_test_fixes

fix faulty tests for distribution-client
Sebastiaan van Stijn 1 year ago
parent
commit
a1d966c492
2 changed files with 23 additions and 2 deletions
  1. 21 0
      distribution/pull_v2_test.go
  2. 2 2
      integration-cli/docker_cli_push_test.go

+ 21 - 0
distribution/pull_v2_test.go

@@ -268,6 +268,27 @@ func TestPullSchema2Config(t *testing.T) {
 			name: "unauthorized",
 			name: "unauthorized",
 			handler: func(callCount int, w http.ResponseWriter) {
 			handler: func(callCount int, w http.ResponseWriter) {
 				w.WriteHeader(http.StatusUnauthorized)
 				w.WriteHeader(http.StatusUnauthorized)
+				// FIXME: current distribution client does not handle plain-text error-responses, so this response is ignored.
+				_, _ = w.Write([]byte("you need to be authenticated"))
+			},
+			expectError:    "unauthorized: authentication required",
+			expectAttempts: 1,
+		},
+		{
+			name: "unauthorized JSON",
+			handler: func(callCount int, w http.ResponseWriter) {
+				w.Header().Set("Content-Type", "application/json")
+				w.WriteHeader(http.StatusUnauthorized)
+				_, _ = w.Write([]byte(`					{ "errors":	[{"code": "UNAUTHORIZED", "message": "you need to be authenticated", "detail": "more detail"}]}`))
+			},
+			expectError:    "unauthorized: you need to be authenticated",
+			expectAttempts: 1,
+		},
+		{
+			name: "unauthorized JSON no body",
+			handler: func(callCount int, w http.ResponseWriter) {
+				w.Header().Set("Content-Type", "application/json")
+				w.WriteHeader(http.StatusUnauthorized)
 			},
 			},
 			expectError:    "unauthorized: authentication required",
 			expectError:    "unauthorized: authentication required",
 			expectAttempts: 1,
 			expectAttempts: 1,

+ 2 - 2
integration-cli/docker_cli_push_test.go

@@ -235,13 +235,13 @@ func getTestTokenService(status int, body string, retries int) *httptest.Server
 	return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
 	return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
 		mu.Lock()
 		mu.Lock()
 		if retries > 0 {
 		if retries > 0 {
-			w.WriteHeader(http.StatusServiceUnavailable)
 			w.Header().Set("Content-Type", "application/json")
 			w.Header().Set("Content-Type", "application/json")
+			w.WriteHeader(http.StatusServiceUnavailable)
 			w.Write([]byte(`{"errors":[{"code":"UNAVAILABLE","message":"cannot create token at this time"}]}`))
 			w.Write([]byte(`{"errors":[{"code":"UNAVAILABLE","message":"cannot create token at this time"}]}`))
 			retries--
 			retries--
 		} else {
 		} else {
-			w.WriteHeader(status)
 			w.Header().Set("Content-Type", "application/json")
 			w.Header().Set("Content-Type", "application/json")
+			w.WriteHeader(status)
 			w.Write([]byte(body))
 			w.Write([]byte(body))
 		}
 		}
 		mu.Unlock()
 		mu.Unlock()