...

Source file src/github.com/google/go-containerregistry/internal/compare/layer.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  	"strings"
    21  
    22  	v1 "github.com/google/go-containerregistry/pkg/v1"
    23  	"github.com/google/go-containerregistry/pkg/v1/types"
    24  )
    25  
    26  // Layers compares the given layers to each other and returns an error if they
    27  // differ.  Note that this does not compare the actual contents (by calling
    28  // Compressed or Uncompressed).
    29  func Layers(a, b v1.Layer) error {
    30  	digests := []v1.Hash{}
    31  	diffids := []v1.Hash{}
    32  	sizes := []int64{}
    33  	mts := []types.MediaType{}
    34  	errs := []string{}
    35  
    36  	for _, layer := range []v1.Layer{a, b} {
    37  		digest, err := layer.Digest()
    38  		if err != nil {
    39  			return err
    40  		}
    41  		digests = append(digests, digest)
    42  
    43  		diffid, err := layer.DiffID()
    44  		if err != nil {
    45  			return err
    46  		}
    47  		diffids = append(diffids, diffid)
    48  
    49  		size, err := layer.Size()
    50  		if err != nil {
    51  			return err
    52  		}
    53  		sizes = append(sizes, size)
    54  
    55  		mt, err := layer.MediaType()
    56  		if err != nil {
    57  			return err
    58  		}
    59  		mts = append(mts, mt)
    60  	}
    61  
    62  	if want, got := digests[0], digests[1]; want != got {
    63  		errs = append(errs, fmt.Sprintf("a.Digest() != b.Digest(); %s != %s", want, got))
    64  	}
    65  	if want, got := diffids[0], diffids[1]; want != got {
    66  		errs = append(errs, fmt.Sprintf("a.DiffID() != b.DiffID(); %s != %s", want, got))
    67  	}
    68  	if want, got := sizes[0], sizes[1]; want != got {
    69  		errs = append(errs, fmt.Sprintf("a.Size() != b.Size(); %d != %d", want, got))
    70  	}
    71  	if want, got := mts[0], mts[1]; want != got {
    72  		errs = append(errs, fmt.Sprintf("a.MediaType() != b.MediaType(); %s != %s", want, got))
    73  	}
    74  
    75  	if len(errs) != 0 {
    76  		return errors.New("Layers differ:\n" + strings.Join(errs, "\n"))
    77  	}
    78  
    79  	return nil
    80  }
    81  

View as plain text