k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/pkg/kubelet/kubelet_getters_test.go (about) 1 /* 2 Copyright 2016 The Kubernetes 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 kubelet 18 19 import ( 20 "path/filepath" 21 "testing" 22 23 "github.com/stretchr/testify/assert" 24 kubecontainer "k8s.io/kubernetes/pkg/kubelet/container" 25 ) 26 27 func TestKubeletDirs(t *testing.T) { 28 testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */) 29 defer testKubelet.Cleanup() 30 kubelet := testKubelet.kubelet 31 root := kubelet.rootDirectory 32 33 var exp, got string 34 35 got = kubelet.getPodsDir() 36 exp = filepath.Join(root, "pods") 37 assert.Equal(t, exp, got) 38 39 got = kubelet.getPodLogsDir() 40 assert.Equal(t, kubelet.podLogsDirectory, got) 41 42 got = kubelet.getPluginsDir() 43 exp = filepath.Join(root, "plugins") 44 assert.Equal(t, exp, got) 45 46 got = kubelet.getPluginsRegistrationDir() 47 exp = filepath.Join(root, "plugins_registry") 48 assert.Equal(t, exp, got) 49 50 got = kubelet.getPluginDir("foobar") 51 exp = filepath.Join(root, "plugins/foobar") 52 assert.Equal(t, exp, got) 53 54 got = kubelet.getPodDir("abc123") 55 exp = filepath.Join(root, "pods/abc123") 56 assert.Equal(t, exp, got) 57 58 got = kubelet.getPodVolumesDir("abc123") 59 exp = filepath.Join(root, "pods/abc123/volumes") 60 assert.Equal(t, exp, got) 61 62 got = kubelet.getPodVolumeDir("abc123", "plugin", "foobar") 63 exp = filepath.Join(root, "pods/abc123/volumes/plugin/foobar") 64 assert.Equal(t, exp, got) 65 66 got = kubelet.getPodVolumeDevicesDir("abc123") 67 exp = filepath.Join(root, "pods/abc123/volumeDevices") 68 assert.Equal(t, exp, got) 69 70 got = kubelet.getPodVolumeDeviceDir("abc123", "plugin") 71 exp = filepath.Join(root, "pods/abc123/volumeDevices/plugin") 72 assert.Equal(t, exp, got) 73 74 got = kubelet.getPodPluginsDir("abc123") 75 exp = filepath.Join(root, "pods/abc123/plugins") 76 assert.Equal(t, exp, got) 77 78 got = kubelet.getPodPluginDir("abc123", "foobar") 79 exp = filepath.Join(root, "pods/abc123/plugins/foobar") 80 assert.Equal(t, exp, got) 81 82 got = kubelet.getVolumeDevicePluginsDir() 83 exp = filepath.Join(root, "plugins") 84 assert.Equal(t, exp, got) 85 86 got = kubelet.getVolumeDevicePluginDir("foobar") 87 exp = filepath.Join(root, "plugins", "foobar", "volumeDevices") 88 assert.Equal(t, exp, got) 89 90 got = kubelet.getPodContainerDir("abc123", "def456") 91 exp = filepath.Join(root, "pods/abc123/containers/def456") 92 assert.Equal(t, exp, got) 93 94 got = kubelet.getPodResourcesDir() 95 exp = filepath.Join(root, "pod-resources") 96 assert.Equal(t, exp, got) 97 98 got = kubelet.GetHostname() 99 exp = "127.0.0.1" 100 assert.Equal(t, exp, got) 101 102 got = kubelet.getPodVolumeSubpathsDir("abc123") 103 exp = filepath.Join(root, "pods/abc123/volume-subpaths") 104 assert.Equal(t, exp, got) 105 } 106 107 func TestHandlerSupportsUserNamespaces(t *testing.T) { 108 testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */) 109 defer testKubelet.Cleanup() 110 kubelet := testKubelet.kubelet 111 112 kubelet.runtimeState.setRuntimeHandlers([]kubecontainer.RuntimeHandler{ 113 { 114 Name: "has-support", 115 SupportsUserNamespaces: true, 116 }, 117 { 118 Name: "has-no-support", 119 SupportsUserNamespaces: false, 120 }, 121 }) 122 123 got, err := kubelet.HandlerSupportsUserNamespaces("has-support") 124 assert.Equal(t, true, got) 125 assert.NoError(t, err) 126 127 got, err = kubelet.HandlerSupportsUserNamespaces("has-no-support") 128 assert.Equal(t, false, got) 129 assert.NoError(t, err) 130 131 got, err = kubelet.HandlerSupportsUserNamespaces("unknown") 132 assert.Equal(t, false, got) 133 assert.Error(t, err) 134 }