...

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

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

     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 layout
    16  
    17  import (
    18  	"testing"
    19  
    20  	v1 "github.com/google/go-containerregistry/pkg/v1"
    21  	"github.com/google/go-containerregistry/pkg/v1/types"
    22  	"github.com/google/go-containerregistry/pkg/v1/validate"
    23  )
    24  
    25  func TestIndex(t *testing.T) {
    26  	idx, err := ImageIndexFromPath(testPath)
    27  	if err != nil {
    28  		t.Fatalf("ImageIndexFromPath() = %v", err)
    29  	}
    30  
    31  	if err := validate.Index(idx); err != nil {
    32  		t.Errorf("validate.Index() = %v", err)
    33  	}
    34  
    35  	mt, err := idx.MediaType()
    36  	if err != nil {
    37  		t.Fatalf("MediaType() = %v", err)
    38  	}
    39  
    40  	if got, want := mt, types.OCIImageIndex; got != want {
    41  		t.Errorf("MediaType(); want: %v got: %v", want, got)
    42  	}
    43  
    44  	indexHash, _ := v1.NewHash("sha256:2b29a2b8dea3af91ea7d0154be1da0c92d55ddd098540930fc8d3db7de377fdb")
    45  	ii, err := idx.ImageIndex(indexHash)
    46  	if err != nil {
    47  		t.Fatalf("ImageIndex() = %v", err)
    48  	}
    49  
    50  	mt, err = ii.MediaType()
    51  	if err != nil {
    52  		t.Fatalf("MediaType() = %v", err)
    53  	}
    54  
    55  	if got, want := mt, types.DockerManifestList; got != want {
    56  		t.Errorf("MediaType(); want: %v got: %v", want, got)
    57  	}
    58  }
    59  
    60  func TestIndexErrors(t *testing.T) {
    61  	idx, err := ImageIndexFromPath(testPath)
    62  	if err != nil {
    63  		t.Fatalf("ImageIndexFromPath() = %v", err)
    64  	}
    65  
    66  	if _, err := idx.Image(bogusDigest); err == nil {
    67  		t.Errorf("idx.Image(%s) = nil, expected err", bogusDigest)
    68  	}
    69  
    70  	if _, err := idx.Image(indexDigest); err == nil {
    71  		t.Errorf("idx.Image(%s) = nil, expected err", bogusDigest)
    72  	}
    73  
    74  	if _, err := idx.ImageIndex(bogusDigest); err == nil {
    75  		t.Errorf("idx.ImageIndex(%s) = nil, expected err", bogusDigest)
    76  	}
    77  
    78  	if _, err := idx.ImageIndex(manifestDigest); err == nil {
    79  		t.Errorf("idx.ImageIndex(%s) = nil, expected err", bogusDigest)
    80  	}
    81  }
    82  

View as plain text