...

Source file src/github.com/containerd/continuity/digests_test.go

Documentation: github.com/containerd/continuity

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package continuity
    18  
    19  import (
    20  	"fmt"
    21  	"reflect"
    22  	"testing"
    23  
    24  	"github.com/opencontainers/go-digest"
    25  )
    26  
    27  // TestUniqifyDigests ensures that we deterministically sort digest entries
    28  // for a resource.
    29  func TestUniqifyDigests(t *testing.T) {
    30  	for _, testcase := range []struct {
    31  		description string
    32  		input       [][]digest.Digest // input is a series of digest collections from separate resources.
    33  		expected    []digest.Digest
    34  		err         error
    35  	}{
    36  		{
    37  			description: "simple merge",
    38  			input: [][]digest.Digest{
    39  				{"sha1:abc", "sha256:def"},
    40  				{"sha1:abc", "sha256:def"},
    41  			},
    42  			expected: []digest.Digest{"sha1:abc", "sha256:def"},
    43  		},
    44  		{
    45  			description: "simple reversed order",
    46  			input: [][]digest.Digest{
    47  				{"sha1:abc", "sha256:def"},
    48  				{"sha256:def", "sha1:abc"},
    49  			},
    50  			expected: []digest.Digest{"sha1:abc", "sha256:def"},
    51  		},
    52  		{
    53  			description: "conflicting values for sha1",
    54  			input: [][]digest.Digest{
    55  				{"sha1:abc", "sha256:def"},
    56  				{"sha256:def", "sha1:def"},
    57  			},
    58  			err: fmt.Errorf("conflicting digests for sha1 found"),
    59  		},
    60  	} {
    61  		fatalf := func(format string, args ...interface{}) {
    62  			t.Fatalf(testcase.description+": "+format, args...)
    63  		}
    64  
    65  		var assembled []digest.Digest
    66  
    67  		for _, ds := range testcase.input {
    68  			assembled = append(assembled, ds...)
    69  		}
    70  
    71  		merged, err := uniqifyDigests(assembled...)
    72  		if err != testcase.err {
    73  			if testcase.err == nil {
    74  				fatalf("unexpected error uniqifying digests: %v", err)
    75  			}
    76  
    77  			if err != testcase.err && err.Error() != testcase.err.Error() {
    78  				// compare by string till we create nice error type
    79  				fatalf("unexpected error uniqifying digests: %v != %v", err, testcase.err)
    80  			}
    81  		}
    82  
    83  		if !reflect.DeepEqual(merged, testcase.expected) {
    84  			fatalf("unexpected uniquification: %v != %v", merged, testcase.expected)
    85  		}
    86  
    87  	}
    88  }
    89  

View as plain text