...

Source file src/github.com/distribution/reference/sort_test.go

Documentation: github.com/distribution/reference

     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 reference
    18  
    19  import (
    20  	"io"
    21  	"math/rand"
    22  	"testing"
    23  
    24  	"github.com/opencontainers/go-digest"
    25  )
    26  
    27  func TestReferenceSorting(t *testing.T) {
    28  	t.Parallel()
    29  	digested := func(seed int64) string {
    30  		b, err := io.ReadAll(io.LimitReader(rand.New(rand.NewSource(seed)), 64))
    31  		if err != nil {
    32  			panic(err)
    33  		}
    34  		return digest.FromBytes(b).String()
    35  	}
    36  	// Add z. prefix to string sort after "sha256:"
    37  	r1 := func(name, tag string, seed int64) string {
    38  		return "z.containerd.io/" + name + ":" + tag + "@" + digested(seed)
    39  	}
    40  	r2 := func(name, tag string) string {
    41  		return "z.containerd.io/" + name + ":" + tag
    42  	}
    43  	r3 := func(name string, seed int64) string {
    44  		return "z.containerd.io/" + name + "@" + digested(seed)
    45  	}
    46  
    47  	for i, tc := range []struct {
    48  		unsorted []string
    49  		expected []string
    50  	}{
    51  		{
    52  			unsorted: []string{r2("name", "latest"), r3("name", 1), r1("name", "latest", 1)},
    53  			expected: []string{r1("name", "latest", 1), r2("name", "latest"), r3("name", 1)},
    54  		},
    55  		{
    56  			unsorted: []string{"can't parse this:latest", r3("name", 1), r2("name", "latest")},
    57  			expected: []string{r2("name", "latest"), r3("name", 1), "can't parse this:latest"},
    58  		},
    59  		{
    60  			unsorted: []string{digested(1), r3("name", 1), r2("name", "latest")},
    61  			expected: []string{r2("name", "latest"), r3("name", 1), digested(1)},
    62  		},
    63  		{
    64  			unsorted: []string{r2("name", "tag2"), r2("name", "tag3"), r2("name", "tag1")},
    65  			expected: []string{r2("name", "tag1"), r2("name", "tag2"), r2("name", "tag3")},
    66  		},
    67  		{
    68  			unsorted: []string{r2("name-2", "tag"), r2("name-3", "tag"), r2("name-1", "tag")},
    69  			expected: []string{r2("name-1", "tag"), r2("name-2", "tag"), r2("name-3", "tag")},
    70  		},
    71  	} {
    72  		sorted := Sort(tc.unsorted)
    73  		if len(sorted) != len(tc.expected) {
    74  			t.Errorf("[%d]: Mismatched sized, got %d, expected %d", i, len(sorted), len(tc.expected))
    75  			continue
    76  		}
    77  		for j := range sorted {
    78  			if sorted[j] != tc.expected[j] {
    79  				t.Errorf("[%d]: Wrong value at %d, got %q, expected %q", i, j, sorted[j], tc.expected[j])
    80  				break
    81  			}
    82  		}
    83  	}
    84  }
    85  

View as plain text