...

Source file src/github.com/google/go-containerregistry/internal/compare/image.go

Documentation: github.com/google/go-containerregistry/internal/compare

     1  // Copyright 2019 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 compare
    16  
    17  import (
    18  	"errors"
    19  	"fmt"
    20  	"reflect"
    21  	"strings"
    22  
    23  	v1 "github.com/google/go-containerregistry/pkg/v1"
    24  	"github.com/google/go-containerregistry/pkg/v1/types"
    25  )
    26  
    27  // Images compares the given images to each other and returns an error if they
    28  // differ.
    29  func Images(a, b v1.Image) error {
    30  	digests := []v1.Hash{}
    31  	manifests := []*v1.Manifest{}
    32  	cns := []v1.Hash{}
    33  	sizes := []int64{}
    34  	mts := []types.MediaType{}
    35  	layerss := [][]v1.Layer{}
    36  
    37  	errs := []string{}
    38  
    39  	for _, img := range []v1.Image{a, b} {
    40  		layers, err := img.Layers()
    41  		if err != nil {
    42  			return err
    43  		}
    44  		layerss = append(layerss, layers)
    45  
    46  		digest, err := img.Digest()
    47  		if err != nil {
    48  			return err
    49  		}
    50  		digests = append(digests, digest)
    51  
    52  		manifest, err := img.Manifest()
    53  		if err != nil {
    54  			return err
    55  		}
    56  		manifests = append(manifests, manifest)
    57  
    58  		cn, err := img.ConfigName()
    59  		if err != nil {
    60  			return err
    61  		}
    62  		cns = append(cns, cn)
    63  
    64  		size, err := img.Size()
    65  		if err != nil {
    66  			return err
    67  		}
    68  		sizes = append(sizes, size)
    69  
    70  		mt, err := img.MediaType()
    71  		if err != nil {
    72  			return err
    73  		}
    74  		mts = append(mts, mt)
    75  	}
    76  
    77  	if want, got := digests[0], digests[1]; want != got {
    78  		errs = append(errs, fmt.Sprintf("a.Digest() != b.Digest(); %s != %s", want, got))
    79  	}
    80  	if want, got := cns[0], cns[1]; want != got {
    81  		errs = append(errs, fmt.Sprintf("a.ConfigName() != b.ConfigName(); %s != %s", want, got))
    82  	}
    83  	if want, got := manifests[0], manifests[1]; !reflect.DeepEqual(want, got) {
    84  		errs = append(errs, fmt.Sprintf("a.Manifest() != b.Manifest(); %v != %v", want, got))
    85  	}
    86  	if want, got := sizes[0], sizes[1]; want != got {
    87  		errs = append(errs, fmt.Sprintf("a.Size() != b.Size(); %d != %d", want, got))
    88  	}
    89  	if want, got := mts[0], mts[1]; want != got {
    90  		errs = append(errs, fmt.Sprintf("a.MediaType() != b.MediaType(); %s != %s", want, got))
    91  	}
    92  
    93  	if len(layerss[0]) != len(layerss[1]) {
    94  		// If we have fewer layers than the first image, abort with an error so we don't panic.
    95  		return errors.New("len(a.Layers()) != len(b.Layers())")
    96  	}
    97  
    98  	// Compare each layer.
    99  	for i := 0; i < len(layerss[0]); i++ {
   100  		if err := Layers(layerss[0][i], layerss[1][i]); err != nil {
   101  			// Wrap the error in newlines to delineate layer errors.
   102  			errs = append(errs, fmt.Sprintf("Layers[%d]: %v\n", i, err))
   103  		}
   104  	}
   105  
   106  	if len(errs) != 0 {
   107  		return errors.New("Images differ:\n" + strings.Join(errs, "\n"))
   108  	}
   109  
   110  	return nil
   111  }
   112  

View as plain text