@@ -23,6 +23,7 @@ import (
2323
2424	cadvisorapiv2 "github.com/google/cadvisor/info/v2" 
2525	"github.com/stretchr/testify/assert" 
26+ 	"github.com/stretchr/testify/require" 
2627
2728	v1 "k8s.io/api/core/v1" 
2829	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 
@@ -546,6 +547,86 @@ func TestCadvisorImagesFsStatsKubeletSeparateDiskOff(t *testing.T) {
546547	assert .Equal (* imageFsInfo .Inodes - * imageFsInfo .InodesFree , * stats .InodesUsed )
547548}
548549
550+ func  TestImageFsStatsCustomResponse (t  * testing.T ) {
551+ 	featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .KubeletSeparateDiskGC , true )
552+ 	for  desc , tc  :=  range  map [string ]struct  {
553+ 		response                        * runtimeapi.ImageFsInfoResponse 
554+ 		callContainerFsInfo , shouldErr  bool 
555+ 	}{
556+ 		"image stats are nil" : {
557+ 			shouldErr : true ,
558+ 		},
559+ 		"no image filesystems in image stats" : {
560+ 			response : & runtimeapi.ImageFsInfoResponse {
561+ 				ImageFilesystems :     []* runtimeapi.FilesystemUsage {},
562+ 				ContainerFilesystems : []* runtimeapi.FilesystemUsage {{}},
563+ 			},
564+ 			shouldErr : true ,
565+ 		},
566+ 		"no container filesystems in image stats" : {
567+ 			response : & runtimeapi.ImageFsInfoResponse {
568+ 				ImageFilesystems :     []* runtimeapi.FilesystemUsage {{}},
569+ 				ContainerFilesystems : []* runtimeapi.FilesystemUsage {},
570+ 			},
571+ 			shouldErr : true ,
572+ 		},
573+ 		"image and container filesystem identifiers are nil" : {
574+ 			response : & runtimeapi.ImageFsInfoResponse {
575+ 				ImageFilesystems :     []* runtimeapi.FilesystemUsage {{}},
576+ 				ContainerFilesystems : []* runtimeapi.FilesystemUsage {{}},
577+ 			},
578+ 			shouldErr : false ,
579+ 		},
580+ 		"using different mountpoints but no used bytes set" : {
581+ 			response : & runtimeapi.ImageFsInfoResponse {
582+ 				ImageFilesystems : []* runtimeapi.FilesystemUsage {{
583+ 					FsId : & runtimeapi.FilesystemIdentifier {Mountpoint : "mnt-1" },
584+ 				}},
585+ 				ContainerFilesystems : []* runtimeapi.FilesystemUsage {{
586+ 					FsId : & runtimeapi.FilesystemIdentifier {Mountpoint : "mnt-2" },
587+ 				}},
588+ 			},
589+ 			callContainerFsInfo : true ,
590+ 			shouldErr :           false ,
591+ 		},
592+ 		"using different mountpoints and set used bytes" : {
593+ 			response : & runtimeapi.ImageFsInfoResponse {
594+ 				ImageFilesystems : []* runtimeapi.FilesystemUsage {{
595+ 					FsId :      & runtimeapi.FilesystemIdentifier {Mountpoint : "mnt-1" },
596+ 					UsedBytes : & runtimeapi.UInt64Value {Value : 10 },
597+ 				}},
598+ 				ContainerFilesystems : []* runtimeapi.FilesystemUsage {{
599+ 					FsId :      & runtimeapi.FilesystemIdentifier {Mountpoint : "mnt-2" },
600+ 					UsedBytes : & runtimeapi.UInt64Value {Value : 20 },
601+ 				}},
602+ 			},
603+ 			callContainerFsInfo : true ,
604+ 			shouldErr :           false ,
605+ 		},
606+ 	} {
607+ 		ctx  :=  context .Background ()
608+ 		mockCadvisor  :=  cadvisortest .NewMockInterface (t )
609+ 		mockRuntime  :=  containertest .NewMockRuntime (t )
610+ 
611+ 		res  :=  getTestFsInfo (1000 )
612+ 		mockCadvisor .EXPECT ().ImagesFsInfo ().Return (res , nil )
613+ 		mockRuntime .EXPECT ().ImageFsInfo (ctx ).Return (tc .response , nil )
614+ 		if  tc .callContainerFsInfo  {
615+ 			mockCadvisor .EXPECT ().ContainerFsInfo ().Return (res , nil )
616+ 		}
617+ 
618+ 		provider  :=  newCadvisorStatsProvider (mockCadvisor , & fakeResourceAnalyzer {}, mockRuntime , nil , NewFakeHostStatsProvider ())
619+ 		stats , containerfs , err  :=  provider .ImageFsStats (ctx )
620+ 		if  tc .shouldErr  {
621+ 			require .Error (t , err , desc )
622+ 			assert .Nil (t , stats )
623+ 			assert .Nil (t , containerfs )
624+ 		} else  {
625+ 			assert .NoError (t , err , desc )
626+ 		}
627+ 	}
628+ }
629+ 
549630func  TestCadvisorImagesFsStats (t  * testing.T ) {
550631	ctx  :=  context .Background ()
551632	var  (
0 commit comments