...

Source file src/github.com/google/go-containerregistry/pkg/v1/tarball/image_test.go

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

     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 tarball
    16  
    17  import (
    18  	"io"
    19  	"testing"
    20  
    21  	"github.com/google/go-containerregistry/pkg/name"
    22  	v1 "github.com/google/go-containerregistry/pkg/v1"
    23  	"github.com/google/go-containerregistry/pkg/v1/validate"
    24  )
    25  
    26  func TestManifestAndConfig(t *testing.T) {
    27  	img, err := ImageFromPath("testdata/test_image_1.tar", nil)
    28  	if err != nil {
    29  		t.Fatalf("Error loading image: %v", err)
    30  	}
    31  	manifest, err := img.Manifest()
    32  	if err != nil {
    33  		t.Fatalf("Error loading manifest: %v", err)
    34  	}
    35  	if len(manifest.Layers) != 1 {
    36  		t.Fatalf("layers should be 1, got %d", len(manifest.Layers))
    37  	}
    38  
    39  	config, err := img.ConfigFile()
    40  	if err != nil {
    41  		t.Fatalf("Error loading config file: %v", err)
    42  	}
    43  	if len(config.History) != 1 {
    44  		t.Fatalf("history length should be 1, got %d", len(config.History))
    45  	}
    46  
    47  	if err := validate.Image(img); err != nil {
    48  		t.Errorf("Validate() = %v", err)
    49  	}
    50  }
    51  
    52  func TestNullManifest(t *testing.T) {
    53  	img, err := ImageFromPath("testdata/null_manifest.tar", nil)
    54  	if err == nil {
    55  		t.Fatalf("Error expected loading null image: %v", img)
    56  	}
    57  }
    58  
    59  func TestNoManifest(t *testing.T) {
    60  	img, err := ImageFromPath("testdata/no_manifest.tar", nil)
    61  	if err == nil {
    62  		t.Fatalf("Error expected loading image: %v", img)
    63  	}
    64  }
    65  
    66  func TestBundleSingle(t *testing.T) {
    67  	img, err := ImageFromPath("testdata/test_bundle.tar", nil)
    68  	if err == nil {
    69  		t.Fatalf("Error expected loading image: %v", img)
    70  	}
    71  }
    72  
    73  func TestDocker25(t *testing.T) {
    74  	img, err := ImageFromPath("testdata/hello-world-v25.tar", nil)
    75  	if err != nil {
    76  		t.Fatal(err)
    77  	}
    78  	if err := validate.Image(img); err != nil {
    79  		t.Fatal(err)
    80  	}
    81  }
    82  
    83  func TestBundleMultiple(t *testing.T) {
    84  	for _, imgName := range []string{
    85  		"test_image_1",
    86  		"test_image_2",
    87  		"test_image_1:latest",
    88  		"test_image_2:latest",
    89  		"index.docker.io/library/test_image_1:latest",
    90  	} {
    91  		t.Run(imgName, func(t *testing.T) {
    92  			tag, err := name.NewTag(imgName, name.WeakValidation)
    93  			if err != nil {
    94  				t.Fatalf("Error creating tag: %v", err)
    95  			}
    96  			img, err := ImageFromPath("testdata/test_bundle.tar", &tag)
    97  			if err != nil {
    98  				t.Fatalf("Error loading image: %v", err)
    99  			}
   100  			if _, err := img.Manifest(); err != nil {
   101  				t.Fatalf("Unexpected error loading manifest: %v", err)
   102  			}
   103  
   104  			if err := validate.Image(img); err != nil {
   105  				t.Errorf("Validate() = %v", err)
   106  			}
   107  		})
   108  	}
   109  }
   110  
   111  func TestLayerLink(t *testing.T) {
   112  	tag, err := name.NewTag("bazel/v1/tarball:test_image_3", name.WeakValidation)
   113  	if err != nil {
   114  		t.Fatalf("Error creating tag: %v", err)
   115  	}
   116  	img, err := ImageFromPath("testdata/test_link.tar", &tag)
   117  	if err != nil {
   118  		t.Fatalf("Error loading image: %v", img)
   119  	}
   120  	hash := v1.Hash{
   121  		Algorithm: "sha256",
   122  		Hex:       "8897395fd26dc44ad0e2a834335b33198cb41ac4d98dfddf58eced3853fa7b17",
   123  	}
   124  	layer, err := img.LayerByDiffID(hash)
   125  	if err != nil {
   126  		t.Fatalf("Error getting layer by diff ID: %v, %v", hash, err)
   127  	}
   128  	rc, err := layer.Uncompressed()
   129  	if err != nil {
   130  		t.Fatal(err)
   131  	}
   132  	bs, err := io.ReadAll(rc)
   133  	if err != nil {
   134  		t.Fatal(err)
   135  	}
   136  	if len(bs) == 0 {
   137  		t.Errorf("layer.Uncompressed() returned a link file")
   138  	}
   139  }
   140  
   141  func TestLoadManifest(t *testing.T) {
   142  	manifest, err := LoadManifest(pathOpener("testdata/test_load_manifest.tar"))
   143  	if err != nil {
   144  		t.Fatalf("Error load manifest: %v", err)
   145  	}
   146  	if len(manifest) == 0 {
   147  		t.Fatalf("get nothing")
   148  	}
   149  }
   150  

View as plain text