...

Source file src/github.com/sigstore/cosign/v2/pkg/oci/walk/walk_test.go

Documentation: github.com/sigstore/cosign/v2/pkg/oci/walk

     1  //
     2  // Copyright 2021 The Sigstore Authors.
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //     http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package walk
    17  
    18  import (
    19  	"context"
    20  	"errors"
    21  	"testing"
    22  
    23  	"github.com/google/go-containerregistry/pkg/v1/mutate"
    24  	"github.com/google/go-containerregistry/pkg/v1/random"
    25  	"github.com/sigstore/cosign/v2/pkg/oci"
    26  	"github.com/sigstore/cosign/v2/pkg/oci/signed"
    27  )
    28  
    29  func TestMapImage(t *testing.T) {
    30  	i, err := random.Image(300 /* bytes */, 3 /* layers */)
    31  	if err != nil {
    32  		t.Fatalf("random.Image() = %v", err)
    33  	}
    34  	si := signed.Image(i)
    35  
    36  	t.Run("walk image, no errors", func(t *testing.T) {
    37  		calls := 0
    38  		err := SignedEntity(context.Background(), si, func(_ context.Context, _ oci.SignedEntity) error {
    39  			calls++
    40  			return nil
    41  		})
    42  		if err != nil {
    43  			t.Fatalf("Map() = %v", err)
    44  		}
    45  		if calls != 1 {
    46  			t.Fatalf("Map called %d times, wanted 1", calls)
    47  		}
    48  	})
    49  
    50  	t.Run("error propagates", func(t *testing.T) {
    51  		want := errors.New("this is the error I expect")
    52  		got := SignedEntity(context.Background(), si, func(_ context.Context, _ oci.SignedEntity) error {
    53  			return want
    54  		})
    55  		if !errors.Is(got, want) {
    56  			t.Fatalf("Map() = %v, wanted %v", got, want)
    57  		}
    58  	})
    59  }
    60  
    61  func TestMapImageIndex(t *testing.T) {
    62  	ii, err := random.Index(300 /* bytes */, 3 /* layers */, 2 /* images */)
    63  	if err != nil {
    64  		t.Fatalf("random.Image() = %v", err)
    65  	}
    66  	ii2, err := random.Index(300 /* bytes */, 3 /* layers */, 2 /* images */)
    67  	if err != nil {
    68  		t.Fatalf("random.Image() = %v", err)
    69  	}
    70  	sii := signed.ImageIndex(mutate.AppendManifests(ii, mutate.IndexAddendum{
    71  		Add: ii2,
    72  	}))
    73  
    74  	t.Run("six calls to identity mutator", func(t *testing.T) {
    75  		calls := 0
    76  		err := SignedEntity(context.Background(), sii, func(_ context.Context, _ oci.SignedEntity) error {
    77  			calls++
    78  			return nil
    79  		})
    80  		if err != nil {
    81  			t.Fatalf("Map() = %v", err)
    82  		}
    83  		if calls != 6 {
    84  			t.Fatalf("Map called %d times, wanted 6", calls)
    85  		}
    86  	})
    87  }
    88  

View as plain text