...

Source file src/github.com/google/go-containerregistry/pkg/v1/partial/index_test.go

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

     1  // Copyright 2020 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 partial_test
    16  
    17  import (
    18  	"testing"
    19  
    20  	v1 "github.com/google/go-containerregistry/pkg/v1"
    21  	"github.com/google/go-containerregistry/pkg/v1/empty"
    22  	"github.com/google/go-containerregistry/pkg/v1/mutate"
    23  	"github.com/google/go-containerregistry/pkg/v1/partial"
    24  	"github.com/google/go-containerregistry/pkg/v1/random"
    25  	"github.com/google/go-containerregistry/pkg/v1/types"
    26  )
    27  
    28  func TestFindManifests(t *testing.T) {
    29  	ii, err := random.Index(100, 5, 6) // random image of 6 manifests, each having 5 layers of size 100
    30  	if err != nil {
    31  		t.Fatal("could not create random index:", err)
    32  	}
    33  	m, _ := ii.IndexManifest()
    34  	digest := m.Manifests[0].Digest
    35  
    36  	matcher := func(desc v1.Descriptor) bool {
    37  		return desc.Digest != digest
    38  	}
    39  
    40  	descriptors, err := partial.FindManifests(ii, matcher)
    41  	expected := len(m.Manifests) - 1
    42  	switch {
    43  	case err != nil:
    44  		t.Error("unexpected error:", err)
    45  	case len(descriptors) != expected:
    46  		t.Errorf("failed on manifests, actual %d, expected %d", len(descriptors), expected)
    47  	}
    48  }
    49  
    50  func TestFindImages(t *testing.T) {
    51  	// create our imageindex with which to work
    52  	ii, err := random.Index(100, 5, 6) // random image of 6 manifests, each having 5 layers of size 100
    53  	if err != nil {
    54  		t.Fatal("could not create random index:", err)
    55  	}
    56  	m, _ := ii.IndexManifest()
    57  	digest := m.Manifests[0].Digest
    58  
    59  	matcher := func(desc v1.Descriptor) bool {
    60  		return desc.Digest != digest
    61  	}
    62  	images, err := partial.FindImages(ii, matcher)
    63  	expected := len(m.Manifests) - 1
    64  	switch {
    65  	case err != nil:
    66  		t.Error("unexpected error:", err)
    67  	case len(images) != expected:
    68  		t.Errorf("failed on images, actual %d, expected %d", len(images), expected)
    69  	}
    70  }
    71  
    72  func TestFindIndexes(t *testing.T) {
    73  	// there is no utility to generate an index of indexes, so we need to create one
    74  	// base index
    75  	var (
    76  		indexCount = 5
    77  		imageCount = 7
    78  	)
    79  	base := empty.Index
    80  	// we now have 5 indexes and 5 images, so wrap them into a single index
    81  	adds := []mutate.IndexAddendum{}
    82  	for i := 0; i < indexCount; i++ {
    83  		ii, err := random.Index(100, 1, 1)
    84  		if err != nil {
    85  			t.Fatalf("%d: unable to create random index: %v", i, err)
    86  		}
    87  		adds = append(adds, mutate.IndexAddendum{
    88  			Add: ii,
    89  			Descriptor: v1.Descriptor{
    90  				MediaType: types.OCIImageIndex,
    91  			},
    92  		})
    93  	}
    94  	for i := 0; i < imageCount; i++ {
    95  		img, err := random.Image(100, 1)
    96  		if err != nil {
    97  			t.Fatalf("%d: unable to create random image: %v", i, err)
    98  		}
    99  		adds = append(adds, mutate.IndexAddendum{
   100  			Add: img,
   101  			Descriptor: v1.Descriptor{
   102  				MediaType: types.OCIManifestSchema1,
   103  			},
   104  		})
   105  	}
   106  
   107  	// just see if it finds all of the indexes
   108  	matcher := func(desc v1.Descriptor) bool {
   109  		return true
   110  	}
   111  	index := mutate.AppendManifests(base, adds...)
   112  	idxes, err := partial.FindIndexes(index, matcher)
   113  	switch {
   114  	case err != nil:
   115  		t.Error("unexpected error:", err)
   116  	case len(idxes) != indexCount:
   117  		t.Errorf("failed on index, actual %d, expected %d", len(idxes), indexCount)
   118  	}
   119  }
   120  

View as plain text