...

Source file src/github.com/google/go-containerregistry/pkg/v1/remote/check_test.go

Documentation: github.com/google/go-containerregistry/pkg/v1/remote

     1  // Copyright 2019 Google LLC All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package remote
    16  
    17  import (
    18  	"fmt"
    19  	"net/http"
    20  	"net/http/httptest"
    21  	"net/url"
    22  	"testing"
    23  
    24  	"github.com/google/go-containerregistry/pkg/authn"
    25  )
    26  
    27  func TestCheckPushPermission(t *testing.T) {
    28  	for _, c := range []struct {
    29  		status  int
    30  		wantErr bool
    31  	}{{
    32  		http.StatusCreated,
    33  		false,
    34  	}, {
    35  		http.StatusAccepted,
    36  		false,
    37  	}, {
    38  		http.StatusForbidden,
    39  		true,
    40  	}, {
    41  		http.StatusBadRequest,
    42  		true,
    43  	}} {
    44  		expectedRepo := "write/time"
    45  		initiatePath := fmt.Sprintf("/v2/%s/blobs/uploads/", expectedRepo)
    46  		somewhereElse := fmt.Sprintf("/v2/%s/blobs/uploads/somewhere/else", expectedRepo)
    47  		server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    48  			switch r.URL.Path {
    49  			case "/v2/":
    50  				w.WriteHeader(http.StatusOK)
    51  			case initiatePath:
    52  				if r.Method != http.MethodPost {
    53  					t.Errorf("Method; got %v, want %v", r.Method, http.MethodPost)
    54  				}
    55  				w.Header().Set("Location", "somewhere/else")
    56  				http.Error(w, "", c.status)
    57  			case somewhereElse:
    58  				if r.Method != http.MethodDelete {
    59  					t.Errorf("Method; got %v, want %v", r.Method, http.MethodDelete)
    60  				}
    61  			default:
    62  				t.Fatalf("Unexpected path: %v", r.URL.Path)
    63  			}
    64  		}))
    65  		defer server.Close()
    66  		u, err := url.Parse(server.URL)
    67  		if err != nil {
    68  			t.Fatalf("url.Parse(%v) = %v", server.URL, err)
    69  		}
    70  
    71  		ref := mustNewTag(t, fmt.Sprintf("%s/%s:latest", u.Host, expectedRepo))
    72  		if err := CheckPushPermission(ref, authn.DefaultKeychain, http.DefaultTransport); (err != nil) != c.wantErr {
    73  			t.Errorf("CheckPermission(%d): got error = %v, want err = %t", c.status, err, c.wantErr)
    74  		}
    75  	}
    76  }
    77  

View as plain text