...

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

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

     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 partial
    16  
    17  import (
    18  	"fmt"
    19  	"io"
    20  	"testing"
    21  
    22  	v1 "github.com/google/go-containerregistry/pkg/v1"
    23  	"github.com/google/go-containerregistry/pkg/v1/types"
    24  )
    25  
    26  type testUIC struct {
    27  	UncompressedImageCore
    28  	configFile []byte
    29  }
    30  
    31  func (t testUIC) RawConfigFile() ([]byte, error) {
    32  	return t.configFile, nil
    33  }
    34  
    35  type testCIC struct {
    36  	CompressedImageCore
    37  	configFile []byte
    38  }
    39  
    40  func (t testCIC) LayerByDigest(h v1.Hash) (CompressedLayer, error) {
    41  	return nil, fmt.Errorf("no layer by diff ID %v", h)
    42  }
    43  
    44  func (t testCIC) RawConfigFile() ([]byte, error) {
    45  	return t.configFile, nil
    46  }
    47  
    48  func TestConfigLayer(t *testing.T) {
    49  	cases := []v1.Image{
    50  		&compressedImageExtender{
    51  			CompressedImageCore: testCIC{
    52  				configFile: []byte("{}"),
    53  			},
    54  		},
    55  		&uncompressedImageExtender{
    56  			UncompressedImageCore: testUIC{
    57  				configFile: []byte("{}"),
    58  			},
    59  		},
    60  	}
    61  
    62  	for _, image := range cases {
    63  		hash, err := image.ConfigName()
    64  		if err != nil {
    65  			t.Fatalf("Error getting config name: %v", err)
    66  		}
    67  
    68  		if _, err := image.LayerByDigest(hash); err == nil {
    69  			t.Error("LayerByDigest(config hash) returned nil error, wanted error")
    70  		}
    71  
    72  		layer, err := ConfigLayer(image)
    73  		if err != nil {
    74  			t.Fatalf("ConfigLayer: %v", err)
    75  		}
    76  		lr, err := layer.Uncompressed()
    77  		if err != nil {
    78  			t.Fatalf("Error getting uncompressed layer: %v", err)
    79  		}
    80  		zr, err := layer.Compressed()
    81  		if err != nil {
    82  			t.Fatalf("Error getting compressed layer: %v", err)
    83  		}
    84  
    85  		cfgLayerBytes, err := io.ReadAll(lr)
    86  		if err != nil {
    87  			t.Fatalf("Error reading config layer bytes: %v", err)
    88  		}
    89  		zcfgLayerBytes, err := io.ReadAll(zr)
    90  		if err != nil {
    91  			t.Fatalf("Error reading config layer bytes: %v", err)
    92  		}
    93  
    94  		cfgFile, err := image.RawConfigFile()
    95  		if err != nil {
    96  			t.Fatalf("Error getting raw config file: %v", err)
    97  		}
    98  
    99  		if string(cfgFile) != string(cfgLayerBytes) {
   100  			t.Errorf("Config file layer doesn't match raw config file")
   101  		}
   102  		if string(cfgFile) != string(zcfgLayerBytes) {
   103  			t.Errorf("Config file layer doesn't match raw config file")
   104  		}
   105  
   106  		size, err := layer.Size()
   107  		if err != nil {
   108  			t.Fatalf("Error getting config layer size: %v", err)
   109  		}
   110  		if size != int64(len(cfgFile)) {
   111  			t.Errorf("Size() = %d, want %d", size, len(cfgFile))
   112  		}
   113  
   114  		digest, err := layer.Digest()
   115  		if err != nil {
   116  			t.Fatalf("Digest() = %v", err)
   117  		}
   118  		if digest != hash {
   119  			t.Errorf("ConfigLayer().Digest() != ConfigName(); %v, %v", digest, hash)
   120  		}
   121  
   122  		diffid, err := layer.DiffID()
   123  		if err != nil {
   124  			t.Fatalf("DiffId() = %v", err)
   125  		}
   126  		if diffid != hash {
   127  			t.Errorf("ConfigLayer().DiffID() != ConfigName(); %v, %v", diffid, hash)
   128  		}
   129  
   130  		mt, err := layer.MediaType()
   131  		if err != nil {
   132  			t.Fatalf("Error getting config layer media type: %v", err)
   133  		}
   134  
   135  		if mt != types.OCIConfigJSON {
   136  			t.Errorf("MediaType() = %v, want %v", mt, types.OCIConfigJSON)
   137  		}
   138  	}
   139  }
   140  

View as plain text