github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/shim/service_test.go (about) 1 // Copyright 2021 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package shim 16 17 import ( 18 "testing" 19 20 specs "github.com/opencontainers/runtime-spec/specs-go" 21 "github.com/SagerNet/gvisor/pkg/shim/utils" 22 ) 23 24 func TestCgroupPath(t *testing.T) { 25 for _, tc := range []struct { 26 name string 27 path string 28 want string 29 }{ 30 { 31 name: "simple", 32 path: "foo/pod123/container", 33 want: "foo/pod123", 34 }, 35 { 36 name: "absolute", 37 path: "/foo/pod123/container", 38 want: "/foo/pod123", 39 }, 40 { 41 name: "no-container", 42 path: "foo/pod123", 43 want: "foo/pod123", 44 }, 45 { 46 name: "no-container-absolute", 47 path: "/foo/pod123", 48 want: "/foo/pod123", 49 }, 50 { 51 name: "double-pod", 52 path: "/foo/podium/pod123/container", 53 want: "/foo/podium/pod123", 54 }, 55 { 56 name: "start-pod", 57 path: "pod123/container", 58 want: "pod123", 59 }, 60 { 61 name: "start-pod-absolute", 62 path: "/pod123/container", 63 want: "/pod123", 64 }, 65 { 66 name: "slashes", 67 path: "///foo/////pod123//////container", 68 want: "/foo/pod123", 69 }, 70 { 71 name: "no-pod", 72 path: "/foo/nopod123/container", 73 want: "/foo/nopod123/container", 74 }, 75 } { 76 t.Run(tc.name, func(t *testing.T) { 77 spec := specs.Spec{ 78 Linux: &specs.Linux{ 79 CgroupsPath: tc.path, 80 }, 81 } 82 updated := updateCgroup(&spec) 83 if spec.Linux.CgroupsPath != tc.want { 84 t.Errorf("updateCgroup(%q), want: %q, got: %q", tc.path, tc.want, spec.Linux.CgroupsPath) 85 } 86 if shouldUpdate := tc.path != tc.want; shouldUpdate != updated { 87 t.Errorf("updateCgroup(%q)=%v, want: %v", tc.path, updated, shouldUpdate) 88 } 89 }) 90 } 91 } 92 93 // Test cases that cgroup path should not be updated. 94 func TestCgroupNoUpdate(t *testing.T) { 95 for _, tc := range []struct { 96 name string 97 spec *specs.Spec 98 }{ 99 { 100 name: "empty", 101 spec: &specs.Spec{}, 102 }, 103 { 104 name: "subcontainer", 105 spec: &specs.Spec{ 106 Linux: &specs.Linux{ 107 CgroupsPath: "foo/pod123/container", 108 }, 109 Annotations: map[string]string{ 110 utils.ContainerTypeAnnotation: utils.ContainerTypeContainer, 111 }, 112 }, 113 }, 114 } { 115 t.Run(tc.name, func(t *testing.T) { 116 if updated := updateCgroup(tc.spec); updated { 117 t.Errorf("updateCgroup(%+v), got: %v, want: false", tc.spec.Linux, updated) 118 } 119 }) 120 } 121 }