github.com/containerd/Containerd@v1.4.13/remotes/docker/handler_test.go (about)

     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 docker
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  
    23  	"github.com/containerd/containerd/reference"
    24  )
    25  
    26  func TestAppendDistributionLabel(t *testing.T) {
    27  	for _, tc := range []struct {
    28  		originLabel string
    29  		repo        string
    30  		expected    string
    31  	}{
    32  		{
    33  			originLabel: "",
    34  			repo:        "",
    35  			expected:    "",
    36  		},
    37  		{
    38  			originLabel: "",
    39  			repo:        "library/busybox",
    40  			expected:    "library/busybox",
    41  		},
    42  		{
    43  			originLabel: "library/busybox",
    44  			repo:        "library/busybox",
    45  			expected:    "library/busybox",
    46  		},
    47  		// remove the duplicate one in origin
    48  		{
    49  			originLabel: "library/busybox,library/redis,library/busybox",
    50  			repo:        "library/alpine",
    51  			expected:    "library/alpine,library/busybox,library/redis",
    52  		},
    53  		// remove the empty repo
    54  		{
    55  			originLabel: "library/busybox,library/redis,library/busybox",
    56  			repo:        "",
    57  			expected:    "library/busybox,library/redis",
    58  		},
    59  		{
    60  			originLabel: "library/busybox,library/redis,library/busybox",
    61  			repo:        "library/redis",
    62  			expected:    "library/busybox,library/redis",
    63  		},
    64  	} {
    65  		if got := appendDistributionSourceLabel(tc.originLabel, tc.repo); !reflect.DeepEqual(got, tc.expected) {
    66  			t.Fatalf("expected %v, but got %v", tc.expected, got)
    67  		}
    68  	}
    69  }
    70  
    71  func TestDistributionSourceLabelKey(t *testing.T) {
    72  	expected := "containerd.io/distribution.source.testsource"
    73  	if got := distributionSourceLabelKey("testsource"); !reflect.DeepEqual(got, expected) {
    74  		t.Fatalf("expected %v, but got %v", expected, got)
    75  	}
    76  }
    77  
    78  func TestCommonPrefixComponents(t *testing.T) {
    79  	for _, tc := range []struct {
    80  		components []string
    81  		target     string
    82  		expected   int
    83  	}{
    84  		{
    85  			components: []string{"foo"},
    86  			target:     "foo/bar",
    87  			expected:   1,
    88  		},
    89  		{
    90  			components: []string{"bar"},
    91  			target:     "foo/bar",
    92  			expected:   0,
    93  		},
    94  		{
    95  			components: []string{"foo", "bar"},
    96  			target:     "foo/bar",
    97  			expected:   2,
    98  		},
    99  	} {
   100  		if got := commonPrefixComponents(tc.components, tc.target); !reflect.DeepEqual(got, tc.expected) {
   101  			t.Fatalf("expected %v, but got %v", tc.expected, got)
   102  		}
   103  	}
   104  }
   105  
   106  func TestSelectRepositoryMountCandidate(t *testing.T) {
   107  	for _, tc := range []struct {
   108  		refspec  reference.Spec
   109  		source   map[string]string
   110  		expected string
   111  	}{
   112  		{
   113  			refspec:  reference.Spec{},
   114  			source:   map[string]string{"": ""},
   115  			expected: "",
   116  		},
   117  		{
   118  			refspec:  reference.Spec{Locator: "user@host/path"},
   119  			source:   map[string]string{"containerd.io/distribution.source.host": "foo,path,bar"},
   120  			expected: "bar",
   121  		},
   122  		{
   123  			refspec:  reference.Spec{Locator: "user@host/path"},
   124  			source:   map[string]string{"containerd.io/distribution.source.host": "foo,bar,path"},
   125  			expected: "bar",
   126  		},
   127  	} {
   128  		if got := selectRepositoryMountCandidate(tc.refspec, tc.source); !reflect.DeepEqual(got, tc.expected) {
   129  			t.Fatalf("expected %v, but got %v", tc.expected, got)
   130  		}
   131  	}
   132  }