File: handler_test.go

package info (click to toggle)
containerd 2.1.4~ds2-5
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 21,772 kB
  • sloc: sh: 1,885; makefile: 591
file content (133 lines) | stat: -rw-r--r-- 3,521 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
   Copyright The containerd Authors.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package docker

import (
	"reflect"
	"testing"

	"github.com/containerd/containerd/v2/pkg/labels"
	"github.com/containerd/containerd/v2/pkg/reference"
)

func TestAppendDistributionLabel(t *testing.T) {
	for _, tc := range []struct {
		originLabel string
		repo        string
		expected    string
	}{
		{
			originLabel: "",
			repo:        "",
			expected:    "",
		},
		{
			originLabel: "",
			repo:        "library/busybox",
			expected:    "library/busybox",
		},
		{
			originLabel: "library/busybox",
			repo:        "library/busybox",
			expected:    "library/busybox",
		},
		// remove the duplicate one in origin
		{
			originLabel: "library/busybox,library/redis,library/busybox",
			repo:        "library/alpine",
			expected:    "library/alpine,library/busybox,library/redis",
		},
		// remove the empty repo
		{
			originLabel: "library/busybox,library/redis,library/busybox",
			repo:        "",
			expected:    "library/busybox,library/redis",
		},
		{
			originLabel: "library/busybox,library/redis,library/busybox",
			repo:        "library/redis",
			expected:    "library/busybox,library/redis",
		},
	} {
		if got := appendDistributionSourceLabel(tc.originLabel, tc.repo); !reflect.DeepEqual(got, tc.expected) {
			t.Fatalf("expected %v, but got %v", tc.expected, got)
		}
	}
}

func TestDistributionSourceLabelKey(t *testing.T) {
	expected := labels.LabelDistributionSource + ".testsource"
	if got := distributionSourceLabelKey("testsource"); !reflect.DeepEqual(got, expected) {
		t.Fatalf("expected %v, but got %v", expected, got)
	}
}

func TestCommonPrefixComponents(t *testing.T) {
	for _, tc := range []struct {
		components []string
		target     string
		expected   int
	}{
		{
			components: []string{"foo"},
			target:     "foo/bar",
			expected:   1,
		},
		{
			components: []string{"bar"},
			target:     "foo/bar",
			expected:   0,
		},
		{
			components: []string{"foo", "bar"},
			target:     "foo/bar",
			expected:   2,
		},
	} {
		if got := commonPrefixComponents(tc.components, tc.target); !reflect.DeepEqual(got, tc.expected) {
			t.Fatalf("expected %v, but got %v", tc.expected, got)
		}
	}
}

func TestSelectRepositoryMountCandidate(t *testing.T) {
	for _, tc := range []struct {
		refspec  reference.Spec
		source   map[string]string
		expected string
	}{
		{
			refspec:  reference.Spec{},
			source:   map[string]string{"": ""},
			expected: "",
		},
		{
			refspec:  reference.Spec{Locator: "user@host/path"},
			source:   map[string]string{labels.LabelDistributionSource + ".host": "foo,path,bar"},
			expected: "bar",
		},
		{
			refspec:  reference.Spec{Locator: "user@host/path"},
			source:   map[string]string{labels.LabelDistributionSource + ".host": "foo,bar,path"},
			expected: "bar",
		},
	} {
		if got := selectRepositoryMountCandidate(tc.refspec, tc.source); !reflect.DeepEqual(got, tc.expected) {
			t.Fatalf("expected %v, but got %v", tc.expected, got)
		}
	}
}