k8s.io/kubernetes@v1.29.3/pkg/kubelet/container/container_gc_test.go (about) 1 /* 2 Copyright 2023 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 container_test 18 19 import ( 20 "context" 21 "reflect" 22 "testing" 23 24 runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1" 25 . "k8s.io/kubernetes/pkg/kubelet/container" 26 ctest "k8s.io/kubernetes/pkg/kubelet/container/testing" 27 ) 28 29 func TestIsContainerFsSeparateFromImageFs(t *testing.T) { 30 runtime := &ctest.FakeRuntime{} 31 fakeSources := ctest.NewFakeReadyProvider() 32 33 gcContainer, err := NewContainerGC(runtime, GCPolicy{}, fakeSources) 34 if err != nil { 35 t.Errorf("unexpected error") 36 } 37 38 cases := []struct { 39 name string 40 containerFs []*runtimeapi.FilesystemUsage 41 imageFs []*runtimeapi.FilesystemUsage 42 writeableSeparateFromReadOnly bool 43 }{ 44 { 45 name: "Only images", 46 imageFs: []*runtimeapi.FilesystemUsage{{FsId: &runtimeapi.FilesystemIdentifier{Mountpoint: "image"}}}, 47 writeableSeparateFromReadOnly: false, 48 }, 49 { 50 name: "images and containers", 51 imageFs: []*runtimeapi.FilesystemUsage{{FsId: &runtimeapi.FilesystemIdentifier{Mountpoint: "image"}}}, 52 containerFs: []*runtimeapi.FilesystemUsage{{FsId: &runtimeapi.FilesystemIdentifier{Mountpoint: "container"}}}, 53 writeableSeparateFromReadOnly: true, 54 }, 55 { 56 name: "same filesystem", 57 imageFs: []*runtimeapi.FilesystemUsage{{FsId: &runtimeapi.FilesystemIdentifier{Mountpoint: "image"}}}, 58 containerFs: []*runtimeapi.FilesystemUsage{{FsId: &runtimeapi.FilesystemIdentifier{Mountpoint: "image"}}}, 59 writeableSeparateFromReadOnly: false, 60 }, 61 62 { 63 name: "Only containers", 64 containerFs: []*runtimeapi.FilesystemUsage{{FsId: &runtimeapi.FilesystemIdentifier{Mountpoint: "image"}}}, 65 writeableSeparateFromReadOnly: false, 66 }, 67 { 68 name: "neither are specified", 69 writeableSeparateFromReadOnly: false, 70 }, 71 { 72 name: "both are empty arrays", 73 writeableSeparateFromReadOnly: false, 74 containerFs: []*runtimeapi.FilesystemUsage{}, 75 imageFs: []*runtimeapi.FilesystemUsage{}, 76 }, 77 { 78 name: "FsId does not exist", 79 writeableSeparateFromReadOnly: false, 80 containerFs: []*runtimeapi.FilesystemUsage{{UsedBytes: &runtimeapi.UInt64Value{Value: 10}}}, 81 imageFs: []*runtimeapi.FilesystemUsage{{UsedBytes: &runtimeapi.UInt64Value{Value: 10}}}, 82 }, 83 } 84 85 for _, tc := range cases { 86 runtime.SetContainerFsStats(tc.containerFs) 87 runtime.SetImageFsStats(tc.imageFs) 88 actualCommand := gcContainer.IsContainerFsSeparateFromImageFs(context.TODO()) 89 90 if e, a := tc.writeableSeparateFromReadOnly, actualCommand; !reflect.DeepEqual(e, a) { 91 t.Errorf("%v: unexpected value; expected %v, got %v", tc.name, e, a) 92 } 93 runtime.SetContainerFsStats(nil) 94 runtime.SetImageFsStats(nil) 95 } 96 }