...

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

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

     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 remote
    16  
    17  import (
    18  	"bytes"
    19  	"encoding/json"
    20  	"fmt"
    21  	"io"
    22  	"log"
    23  	"net/http/httptest"
    24  	"net/url"
    25  	"testing"
    26  
    27  	"github.com/google/go-containerregistry/pkg/name"
    28  	"github.com/google/go-containerregistry/pkg/registry"
    29  	v1 "github.com/google/go-containerregistry/pkg/v1"
    30  	"github.com/google/go-containerregistry/pkg/v1/mutate"
    31  	"github.com/google/go-containerregistry/pkg/v1/random"
    32  	"github.com/google/go-containerregistry/pkg/v1/types"
    33  )
    34  
    35  var fatal = log.Fatal
    36  var helper = func() {}
    37  
    38  func must[T any](t T, err error) T {
    39  	helper()
    40  	if err != nil {
    41  		fatal(err)
    42  	}
    43  	return t
    44  }
    45  
    46  type fakeSchema1 struct {
    47  	b []byte
    48  }
    49  
    50  func (f *fakeSchema1) MediaType() (types.MediaType, error) {
    51  	return types.DockerManifestSchema1, nil
    52  }
    53  
    54  func (f *fakeSchema1) RawManifest() ([]byte, error) {
    55  	return f.b, nil
    56  }
    57  
    58  func toSchema1(t *testing.T, img v1.Image) *fakeSchema1 {
    59  	t.Helper()
    60  
    61  	fsl := []fslayer{}
    62  
    63  	layers := must(img.Layers())
    64  	for i := len(layers) - 1; i >= 0; i-- {
    65  		l := layers[i]
    66  		dig := must(l.Digest())
    67  		fsl = append(fsl, fslayer{
    68  			BlobSum: dig.String(),
    69  		})
    70  	}
    71  
    72  	return &fakeSchema1{
    73  		b: must(json.Marshal(&schema1Manifest{FSLayers: fsl})),
    74  	}
    75  }
    76  
    77  func TestSchema1(t *testing.T) {
    78  	fatal = t.Fatal
    79  	helper = t.Helper
    80  
    81  	rnd := must(random.Image(1024, 3))
    82  	s1 := toSchema1(t, rnd)
    83  
    84  	// Set up a fake registry.
    85  	s := httptest.NewServer(registry.New())
    86  	defer s.Close()
    87  	u := must(url.Parse(s.URL))
    88  
    89  	dst := fmt.Sprintf("%s/test/foreign/upload", u.Host)
    90  	ref := must(name.ParseReference(dst))
    91  
    92  	if err := Write(ref, rnd); err != nil {
    93  		t.Fatal(err)
    94  	}
    95  
    96  	tag := ref.Context().Tag("schema1")
    97  
    98  	if err := Put(tag, s1); err != nil {
    99  		t.Fatal(err)
   100  	}
   101  
   102  	pulled := must(Get(tag))
   103  	img := must(pulled.Schema1())
   104  
   105  	if err := Write(ref.Context().Tag("repushed"), img); err != nil {
   106  		t.Fatal(err)
   107  	}
   108  
   109  	mustErr := func(a any, err error) {
   110  		t.Helper()
   111  		if err == nil {
   112  			t.Fatalf("should have failed, got %T", a)
   113  		}
   114  	}
   115  
   116  	mustErr(img.ConfigFile())
   117  	mustErr(img.Manifest())
   118  	mustErr(img.LayerByDiffID(v1.Hash{}))
   119  
   120  	h, sz, err := v1.SHA256(bytes.NewReader(s1.b))
   121  	if err != nil {
   122  		t.Fatal(err)
   123  	}
   124  	if got, want := must(img.Size()), sz; got != want {
   125  		t.Errorf("Size(): got %d, want %d", got, want)
   126  	}
   127  	if got, want := must(img.Digest()), h; got != want {
   128  		t.Errorf("Digest(): got %s, want %s", got, want)
   129  	}
   130  
   131  	if got, want := must(io.ReadAll(mutate.Extract(img))), must(io.ReadAll(mutate.Extract(rnd))); !bytes.Equal(got, want) {
   132  		t.Error("filesystems are different")
   133  	}
   134  }
   135  

View as plain text