@@ -24,6 +24,7 @@ import (
24
24
"fmt"
25
25
"os"
26
26
"os/exec"
27
+ "path/filepath"
27
28
"reflect"
28
29
"sort"
29
30
"strings"
@@ -36,6 +37,7 @@ import (
36
37
"golang.org/x/sys/unix"
37
38
utilexec "k8s.io/utils/exec"
38
39
testexec "k8s.io/utils/exec/testing"
40
+ "k8s.io/utils/ptr"
39
41
)
40
42
41
43
func TestReadProcMountsFrom (t * testing.T ) {
@@ -874,3 +876,116 @@ func makeFakeCommandAction(stdout string, err error, cmdFn func()) testexec.Fake
874
876
return testexec .InitFakeCmd (& c , cmd , args ... )
875
877
}
876
878
}
879
+
880
+ func TestIsLikelyNotMountPoint (t * testing.T ) {
881
+ mounter := Mounter {"fake/path" , ptr .To (true ), true , true }
882
+
883
+ tests := []struct {
884
+ fileName string
885
+ targetLinkName string
886
+ setUp func (base , fileName , targetLinkName string ) error
887
+ cleanUp func (base , fileName , targetLinkName string ) error
888
+ expectedResult bool
889
+ expectError bool
890
+ }{
891
+ {
892
+ "Dir" ,
893
+ "" ,
894
+ func (base , fileName , targetLinkName string ) error {
895
+ return os .Mkdir (filepath .Join (base , fileName ), 0o750 )
896
+ },
897
+ func (base , fileName , targetLinkName string ) error {
898
+ return os .Remove (filepath .Join (base , fileName ))
899
+ },
900
+ true ,
901
+ false ,
902
+ },
903
+ {
904
+ "InvalidDir" ,
905
+ "" ,
906
+ func (base , fileName , targetLinkName string ) error {
907
+ return nil
908
+ },
909
+ func (base , fileName , targetLinkName string ) error {
910
+ return nil
911
+ },
912
+ true ,
913
+ true ,
914
+ },
915
+ {
916
+ "ValidSymLink" ,
917
+ "targetSymLink" ,
918
+ func (base , fileName , targetLinkName string ) error {
919
+ targeLinkPath := filepath .Join (base , targetLinkName )
920
+ if err := os .Mkdir (targeLinkPath , 0o750 ); err != nil {
921
+ return err
922
+ }
923
+
924
+ filePath := filepath .Join (base , fileName )
925
+ if err := os .Symlink (targeLinkPath , filePath ); err != nil {
926
+ return err
927
+ }
928
+ return nil
929
+ },
930
+ func (base , fileName , targetLinkName string ) error {
931
+ if err := os .Remove (filepath .Join (base , fileName )); err != nil {
932
+ return err
933
+ }
934
+ return os .Remove (filepath .Join (base , targetLinkName ))
935
+ },
936
+ true ,
937
+ false ,
938
+ },
939
+ {
940
+ "InvalidSymLink" ,
941
+ "targetSymLink2" ,
942
+ func (base , fileName , targetLinkName string ) error {
943
+ targeLinkPath := filepath .Join (base , targetLinkName )
944
+ if err := os .Mkdir (targeLinkPath , 0o750 ); err != nil {
945
+ return err
946
+ }
947
+
948
+ filePath := filepath .Join (base , fileName )
949
+ if err := os .Symlink (targeLinkPath , filePath ); err != nil {
950
+ return err
951
+ }
952
+ return os .Remove (targeLinkPath )
953
+ },
954
+ func (base , fileName , targetLinkName string ) error {
955
+ return os .Remove (filepath .Join (base , fileName ))
956
+ },
957
+ true ,
958
+ true ,
959
+ },
960
+ }
961
+
962
+ for _ , test := range tests {
963
+ // test with absolute and relative path
964
+ baseList := []string {t .TempDir (), "./" }
965
+ for _ , base := range baseList {
966
+ if err := test .setUp (base , test .fileName , test .targetLinkName ); err != nil {
967
+ t .Fatalf ("unexpected error in setUp(%s, %s): %v" , test .fileName , test .targetLinkName , err )
968
+ }
969
+
970
+ filePath := filepath .Join (base , test .fileName )
971
+ result , err := mounter .IsLikelyNotMountPoint (filePath )
972
+ if result != test .expectedResult {
973
+ t .Errorf ("Expect result not equal with IsLikelyNotMountPoint(%s) return: %t, expected: %t" , filePath , result , test .expectedResult )
974
+ }
975
+
976
+ if base == "./" {
977
+ if err := test .cleanUp (base , test .fileName , test .targetLinkName ); err != nil {
978
+ t .Fatalf ("unexpected error in cleanUp(%s, %s): %v" , test .fileName , test .targetLinkName , err )
979
+ }
980
+ }
981
+
982
+ if (err != nil ) != test .expectError {
983
+ if test .expectError {
984
+ t .Errorf ("Expect error during IsLikelyNotMountPoint(%s)" , filePath )
985
+ } else {
986
+ t .Errorf ("Expect error is nil during IsLikelyNotMountPoint(%s): %v" , filePath , err )
987
+ }
988
+ }
989
+ }
990
+ }
991
+ }
0 commit comments