321 lines
8.6 KiB
Go
321 lines
8.6 KiB
Go
/*
|
|
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 podsandbox
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
crilabels "github.com/containerd/containerd/v2/internal/cri/labels"
|
|
"github.com/containerd/containerd/v2/pkg/oci"
|
|
docker "github.com/distribution/reference"
|
|
imagedigest "github.com/opencontainers/go-digest"
|
|
runtimespec "github.com/opencontainers/runtime-spec/specs-go"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestGetRepoDigestAndTag(t *testing.T) {
|
|
digest := imagedigest.Digest("sha256:e6693c20186f837fc393390135d8a598a96a833917917789d63766cab6c59582")
|
|
for _, test := range []struct {
|
|
desc string
|
|
ref string
|
|
schema1 bool
|
|
expectedRepoDigest string
|
|
expectedRepoTag string
|
|
}{
|
|
{
|
|
desc: "repo tag should be empty if original ref has no tag",
|
|
ref: "gcr.io/library/busybox@" + digest.String(),
|
|
expectedRepoDigest: "gcr.io/library/busybox@" + digest.String(),
|
|
},
|
|
{
|
|
desc: "repo tag should not be empty if original ref has tag",
|
|
ref: "gcr.io/library/busybox:latest",
|
|
expectedRepoDigest: "gcr.io/library/busybox@" + digest.String(),
|
|
expectedRepoTag: "gcr.io/library/busybox:latest",
|
|
},
|
|
{
|
|
desc: "repo digest should be empty if original ref is schema1 and has no digest",
|
|
ref: "gcr.io/library/busybox:latest",
|
|
schema1: true,
|
|
expectedRepoDigest: "",
|
|
expectedRepoTag: "gcr.io/library/busybox:latest",
|
|
},
|
|
{
|
|
desc: "repo digest should not be empty if original ref is schema1 but has digest",
|
|
ref: "gcr.io/library/busybox@sha256:e6693c20186f837fc393390135d8a598a96a833917917789d63766cab6c59594",
|
|
schema1: true,
|
|
expectedRepoDigest: "gcr.io/library/busybox@sha256:e6693c20186f837fc393390135d8a598a96a833917917789d63766cab6c59594",
|
|
expectedRepoTag: "",
|
|
},
|
|
} {
|
|
test := test
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
named, err := docker.ParseDockerRef(test.ref)
|
|
assert.NoError(t, err)
|
|
repoDigest, repoTag := getRepoDigestAndTag(named, digest, test.schema1)
|
|
assert.Equal(t, test.expectedRepoDigest, repoDigest)
|
|
assert.Equal(t, test.expectedRepoTag, repoTag)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestBuildLabels(t *testing.T) {
|
|
imageConfigLabels := map[string]string{
|
|
"a": "z",
|
|
"d": "y",
|
|
"long-label": strings.Repeat("example", 10000),
|
|
}
|
|
configLabels := map[string]string{
|
|
"a": "b",
|
|
"c": "d",
|
|
}
|
|
newLabels := buildLabels(configLabels, imageConfigLabels, crilabels.ContainerKindSandbox)
|
|
assert.Len(t, newLabels, 4)
|
|
assert.Equal(t, "b", newLabels["a"])
|
|
assert.Equal(t, "d", newLabels["c"])
|
|
assert.Equal(t, "y", newLabels["d"])
|
|
assert.Equal(t, crilabels.ContainerKindSandbox, newLabels[crilabels.ContainerKindLabel])
|
|
assert.NotContains(t, newLabels, "long-label")
|
|
|
|
newLabels["a"] = "e"
|
|
assert.Empty(t, configLabels[crilabels.ContainerKindLabel], "should not add new labels into original label")
|
|
assert.Equal(t, "b", configLabels["a"], "change in new labels should not affect original label")
|
|
}
|
|
|
|
func TestEnvDeduplication(t *testing.T) {
|
|
for _, test := range []struct {
|
|
desc string
|
|
existing []string
|
|
kv [][2]string
|
|
expected []string
|
|
}{
|
|
{
|
|
desc: "single env",
|
|
kv: [][2]string{
|
|
{"a", "b"},
|
|
},
|
|
expected: []string{"a=b"},
|
|
},
|
|
{
|
|
desc: "multiple envs",
|
|
kv: [][2]string{
|
|
{"a", "b"},
|
|
{"c", "d"},
|
|
{"e", "f"},
|
|
},
|
|
expected: []string{
|
|
"a=b",
|
|
"c=d",
|
|
"e=f",
|
|
},
|
|
},
|
|
{
|
|
desc: "env override",
|
|
kv: [][2]string{
|
|
{"k1", "v1"},
|
|
{"k2", "v2"},
|
|
{"k3", "v3"},
|
|
{"k3", "v4"},
|
|
{"k1", "v5"},
|
|
{"k4", "v6"},
|
|
},
|
|
expected: []string{
|
|
"k1=v5",
|
|
"k2=v2",
|
|
"k3=v4",
|
|
"k4=v6",
|
|
},
|
|
},
|
|
{
|
|
desc: "existing env",
|
|
existing: []string{
|
|
"k1=v1",
|
|
"k2=v2",
|
|
"k3=v3",
|
|
},
|
|
kv: [][2]string{
|
|
{"k3", "v4"},
|
|
{"k2", "v5"},
|
|
{"k4", "v6"},
|
|
},
|
|
expected: []string{
|
|
"k1=v1",
|
|
"k2=v5",
|
|
"k3=v4",
|
|
"k4=v6",
|
|
},
|
|
},
|
|
} {
|
|
test := test
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
var spec runtimespec.Spec
|
|
if len(test.existing) > 0 {
|
|
spec.Process = &runtimespec.Process{
|
|
Env: test.existing,
|
|
}
|
|
}
|
|
for _, kv := range test.kv {
|
|
oci.WithEnv([]string{kv[0] + "=" + kv[1]})(context.Background(), nil, nil, &spec)
|
|
}
|
|
assert.Equal(t, test.expected, spec.Process.Env)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPassThroughAnnotationsFilter(t *testing.T) {
|
|
for _, test := range []struct {
|
|
desc string
|
|
podAnnotations map[string]string
|
|
runtimePodAnnotations []string
|
|
passthroughAnnotations map[string]string
|
|
}{
|
|
{
|
|
desc: "should support direct match",
|
|
podAnnotations: map[string]string{"c": "d", "d": "e"},
|
|
runtimePodAnnotations: []string{"c"},
|
|
passthroughAnnotations: map[string]string{"c": "d"},
|
|
},
|
|
{
|
|
desc: "should support wildcard match",
|
|
podAnnotations: map[string]string{
|
|
"t.f": "j",
|
|
"z.g": "o",
|
|
"z": "o",
|
|
"y.ca": "b",
|
|
"y": "b",
|
|
},
|
|
runtimePodAnnotations: []string{"*.f", "z*g", "y.c*"},
|
|
passthroughAnnotations: map[string]string{
|
|
"t.f": "j",
|
|
"z.g": "o",
|
|
"y.ca": "b",
|
|
},
|
|
},
|
|
{
|
|
desc: "should support wildcard match all",
|
|
podAnnotations: map[string]string{
|
|
"t.f": "j",
|
|
"z.g": "o",
|
|
"z": "o",
|
|
"y.ca": "b",
|
|
"y": "b",
|
|
},
|
|
runtimePodAnnotations: []string{"*"},
|
|
passthroughAnnotations: map[string]string{
|
|
"t.f": "j",
|
|
"z.g": "o",
|
|
"z": "o",
|
|
"y.ca": "b",
|
|
"y": "b",
|
|
},
|
|
},
|
|
{
|
|
desc: "should support match including path separator",
|
|
podAnnotations: map[string]string{
|
|
"matchend.com/end": "1",
|
|
"matchend.com/end1": "2",
|
|
"matchend.com/1end": "3",
|
|
"matchmid.com/mid": "4",
|
|
"matchmid.com/mi1d": "5",
|
|
"matchmid.com/mid1": "6",
|
|
"matchhead.com/head": "7",
|
|
"matchhead.com/1head": "8",
|
|
"matchhead.com/head1": "9",
|
|
"matchall.com/abc": "10",
|
|
"matchall.com/def": "11",
|
|
"end/matchend": "12",
|
|
"end1/matchend": "13",
|
|
"1end/matchend": "14",
|
|
"mid/matchmid": "15",
|
|
"mi1d/matchmid": "16",
|
|
"mid1/matchmid": "17",
|
|
"head/matchhead": "18",
|
|
"1head/matchhead": "19",
|
|
"head1/matchhead": "20",
|
|
"abc/matchall": "21",
|
|
"def/matchall": "22",
|
|
"match1/match2": "23",
|
|
"nomatch/nomatch": "24",
|
|
},
|
|
runtimePodAnnotations: []string{
|
|
"matchend.com/end*",
|
|
"matchmid.com/mi*d",
|
|
"matchhead.com/*head",
|
|
"matchall.com/*",
|
|
"end*/matchend",
|
|
"mi*d/matchmid",
|
|
"*head/matchhead",
|
|
"*/matchall",
|
|
"match*/match*",
|
|
},
|
|
passthroughAnnotations: map[string]string{
|
|
"matchend.com/end": "1",
|
|
"matchend.com/end1": "2",
|
|
"matchmid.com/mid": "4",
|
|
"matchmid.com/mi1d": "5",
|
|
"matchhead.com/head": "7",
|
|
"matchhead.com/1head": "8",
|
|
"matchall.com/abc": "10",
|
|
"matchall.com/def": "11",
|
|
"end/matchend": "12",
|
|
"end1/matchend": "13",
|
|
"mid/matchmid": "15",
|
|
"mi1d/matchmid": "16",
|
|
"head/matchhead": "18",
|
|
"1head/matchhead": "19",
|
|
"abc/matchall": "21",
|
|
"def/matchall": "22",
|
|
"match1/match2": "23",
|
|
},
|
|
},
|
|
} {
|
|
test := test
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
passthroughAnnotations := getPassthroughAnnotations(test.podAnnotations, test.runtimePodAnnotations)
|
|
assert.Equal(t, test.passthroughAnnotations, passthroughAnnotations)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestEnsureRemoveAllNotExist(t *testing.T) {
|
|
// should never return an error for a non-existent path
|
|
if err := ensureRemoveAll(context.Background(), "/non/existent/path"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestEnsureRemoveAllWithDir(t *testing.T) {
|
|
dir := t.TempDir()
|
|
if err := ensureRemoveAll(context.Background(), dir); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestEnsureRemoveAllWithFile(t *testing.T) {
|
|
tmp, err := os.CreateTemp("", "test-ensure-removeall-with-dir")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tmp.Close()
|
|
if err := ensureRemoveAll(context.Background(), tmp.Name()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|