...

Source file src/github.com/google/go-containerregistry/pkg/registry/blobs_disk_test.go

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

     1  // Copyright 2023 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 registry_test
    16  
    17  import (
    18  	"fmt"
    19  	"net/http/httptest"
    20  	"os"
    21  	"path/filepath"
    22  	"strings"
    23  	"testing"
    24  
    25  	"github.com/google/go-containerregistry/pkg/name"
    26  	"github.com/google/go-containerregistry/pkg/registry"
    27  	"github.com/google/go-containerregistry/pkg/v1/random"
    28  	"github.com/google/go-containerregistry/pkg/v1/remote"
    29  	"github.com/google/go-containerregistry/pkg/v1/validate"
    30  )
    31  
    32  func TestDiskPush(t *testing.T) {
    33  	dir := t.TempDir()
    34  	reg := registry.New(registry.WithBlobHandler(registry.NewDiskBlobHandler(dir)))
    35  	srv := httptest.NewServer(reg)
    36  	defer srv.Close()
    37  
    38  	ref, err := name.ParseReference(strings.TrimPrefix(srv.URL, "http://") + "/foo/bar:latest")
    39  	if err != nil {
    40  		t.Fatal(err)
    41  	}
    42  	img, err := random.Image(1024, 5)
    43  	if err != nil {
    44  		t.Fatal(err)
    45  	}
    46  	if err := remote.Write(ref, img); err != nil {
    47  		t.Fatalf("remote.Write: %v", err)
    48  	}
    49  
    50  	// Test we can read and validate the image.
    51  	if _, err := remote.Image(ref); err != nil {
    52  		t.Fatalf("remote.Image: %v", err)
    53  	}
    54  	if err := validate.Image(img); err != nil {
    55  		t.Fatalf("validate.Image: %v", err)
    56  	}
    57  
    58  	// Collect the layer SHAs we expect to find.
    59  	want := map[string]bool{}
    60  	if h, err := img.ConfigName(); err != nil {
    61  		t.Fatal(err)
    62  	} else {
    63  		want[fmt.Sprintf("%s/%s", h.Algorithm, h.Hex)] = true
    64  	}
    65  	ls, err := img.Layers()
    66  	if err != nil {
    67  		t.Fatal(err)
    68  	}
    69  	for _, l := range ls {
    70  		if h, err := l.Digest(); err != nil {
    71  			t.Fatal(err)
    72  		} else {
    73  			want[fmt.Sprintf("%s/%s", h.Algorithm, h.Hex)] = true
    74  		}
    75  	}
    76  
    77  	// Test the blobs are there on disk.
    78  	for dig := range want {
    79  		if _, err := os.Stat(filepath.Join(dir, dig)); err != nil {
    80  			t.Fatalf("os.Stat(%s): %v", dig, err)
    81  		}
    82  		t.Logf("Found %s", dig)
    83  	}
    84  }
    85  

View as plain text