...

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

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

     1  // Copyright 2018 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 random
    16  
    17  import (
    18  	"math/rand"
    19  	"testing"
    20  
    21  	v1 "github.com/google/go-containerregistry/pkg/v1"
    22  	"github.com/google/go-containerregistry/pkg/v1/types"
    23  	"github.com/google/go-containerregistry/pkg/v1/validate"
    24  )
    25  
    26  func TestRandomIndex(t *testing.T) {
    27  	ii, err := Index(1024, 5, 3)
    28  	if err != nil {
    29  		t.Fatalf("Error loading index: %v", err)
    30  	}
    31  
    32  	if err := validate.Index(ii); err != nil {
    33  		t.Errorf("validate.Index() = %v", err)
    34  	}
    35  
    36  	digest, err := ii.Digest()
    37  	if err != nil {
    38  		t.Fatalf("Digest(): unexpected err: %v", err)
    39  	}
    40  
    41  	if _, err := ii.Image(digest); err == nil {
    42  		t.Errorf("Image(%s): expected err, got nil", digest)
    43  	}
    44  
    45  	if _, err := ii.ImageIndex(digest); err == nil {
    46  		t.Errorf("ImageIndex(%s): expected err, got nil", digest)
    47  	}
    48  
    49  	mt, err := ii.MediaType()
    50  	if err != nil {
    51  		t.Errorf("MediaType(): unexpected err: %v", err)
    52  	}
    53  
    54  	if got, want := mt, types.OCIImageIndex; got != want {
    55  		t.Errorf("MediaType(): got: %v, want: %v", got, want)
    56  	}
    57  
    58  	man, err := ii.IndexManifest()
    59  	if err != nil {
    60  		t.Errorf("IndexManifest(): unexpected err: %v", err)
    61  	}
    62  
    63  	if got, want := man.MediaType, types.OCIImageIndex; got != want {
    64  		t.Errorf("MediaType: got: %v, want: %v", got, want)
    65  	}
    66  }
    67  
    68  func TestRandomIndexSource(t *testing.T) {
    69  	indexDigest := func(o ...Option) v1.Hash {
    70  		img, err := Index(1024, 2, 2, o...)
    71  		if err != nil {
    72  			t.Fatalf("Image: %v", err)
    73  		}
    74  
    75  		h, err := img.Digest()
    76  		if err != nil {
    77  			t.Fatalf("Digest(): %v", err)
    78  		}
    79  		return h
    80  	}
    81  
    82  	digest0a := indexDigest(WithSource(rand.NewSource(0)))
    83  	digest0b := indexDigest(WithSource(rand.NewSource(0)))
    84  	digest1 := indexDigest(WithSource(rand.NewSource(1)))
    85  
    86  	if digest0a != digest0b {
    87  		t.Error("Expected the index digest to be the same with the same seed")
    88  	}
    89  
    90  	if digest0a == digest1 {
    91  		t.Error("Expected the index digest to be different with different seeds")
    92  	}
    93  
    94  	digestA := indexDigest()
    95  	digestB := indexDigest()
    96  
    97  	if digestA == digestB {
    98  		t.Error("Expected the index digest to be different with different random seeds")
    99  	}
   100  }
   101  

View as plain text