...

Source file src/github.com/google/go-containerregistry/internal/compare/index.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  // Indexes compares the given indexes to each other and returns an error if
    28  // they differ.
    29  func Indexes(a, b v1.ImageIndex) error {
    30  	digests := []v1.Hash{}
    31  	manifests := []*v1.IndexManifest{}
    32  	sizes := []int64{}
    33  	mts := []types.MediaType{}
    34  
    35  	errs := []string{}
    36  
    37  	for _, idx := range []v1.ImageIndex{a, b} {
    38  		digest, err := idx.Digest()
    39  		if err != nil {
    40  			return err
    41  		}
    42  		digests = append(digests, digest)
    43  
    44  		manifest, err := idx.IndexManifest()
    45  		if err != nil {
    46  			return err
    47  		}
    48  		manifests = append(manifests, manifest)
    49  
    50  		size, err := idx.Size()
    51  		if err != nil {
    52  			return err
    53  		}
    54  		sizes = append(sizes, size)
    55  
    56  		mt, err := idx.MediaType()
    57  		if err != nil {
    58  			return err
    59  		}
    60  		mts = append(mts, mt)
    61  	}
    62  
    63  	if want, got := digests[0], digests[1]; want != got {
    64  		errs = append(errs, fmt.Sprintf("a.Digest() != b.Digest(); %s != %s", want, got))
    65  	}
    66  	if want, got := manifests[0], manifests[1]; !reflect.DeepEqual(want, got) {
    67  		errs = append(errs, fmt.Sprintf("a.Manifest() != b.Manifest(); %v != %v", want, got))
    68  	}
    69  	if want, got := sizes[0], sizes[1]; want != got {
    70  		errs = append(errs, fmt.Sprintf("a.Size() != b.Size(); %d != %d", want, got))
    71  	}
    72  	if want, got := mts[0], mts[1]; want != got {
    73  		errs = append(errs, fmt.Sprintf("a.MediaType() != b.MediaType(); %s != %s", want, got))
    74  	}
    75  
    76  	// TODO(jonjohnsonjr): Iterate over Manifest and compare Image and ImageIndex results.
    77  
    78  	if len(errs) != 0 {
    79  		return errors.New("Indexes differ:\n" + strings.Join(errs, "\n"))
    80  	}
    81  
    82  	return nil
    83  }
    84  

View as plain text