...

Source file src/github.com/google/go-containerregistry/pkg/crane/digest_test.go

Documentation: github.com/google/go-containerregistry/pkg/crane

     1  // Copyright 2021 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 crane
    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/v1/types"
    25  )
    26  
    27  func TestDigest_MissingDigest(t *testing.T) {
    28  	response := []byte("doesn't matter")
    29  	digest := "sha256:477c34d98f9e090a4441cf82d2f1f03e64c8eb730e8c1ef39a8595e685d4df65" // Digest of "doesn't matter"
    30  	getCalled := false
    31  
    32  	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    33  		if r.URL.Path == "/v2/" {
    34  			w.WriteHeader(http.StatusOK)
    35  			return
    36  		}
    37  		w.Header().Set("Content-Type", string(types.DockerManifestSchema2))
    38  		if r.Method == http.MethodGet {
    39  			getCalled = true
    40  			w.Header().Set("Docker-Content-Digest", digest)
    41  		}
    42  		// This will automatically set the Content-Length header.
    43  		w.Write(response)
    44  	}))
    45  	defer server.Close()
    46  	u, err := url.Parse(server.URL)
    47  	if err != nil {
    48  		t.Fatalf("url.Parse(%v) = %v", server.URL, err)
    49  	}
    50  
    51  	got, err := Digest(fmt.Sprintf("%s/repo:latest", u.Host))
    52  	if err != nil {
    53  		t.Fatalf("Digest: %v", err)
    54  	}
    55  	if got != digest {
    56  		t.Errorf("Digest: got %q, want %q", got, digest)
    57  	}
    58  	if !getCalled {
    59  		t.Errorf("Digest: expected GET to be called")
    60  	}
    61  }
    62  

View as plain text