@@ -889,3 +889,57 @@ func TestIsSupportedContainerNamePrefix(t *testing.T) {
889889 }
890890 }
891891}
892+
893+ func TestChmodIfPermissionMismatch (t * testing.T ) {
894+ permissionMatchingPath , _ := getWorkDirPath ("permissionMatchingPath" )
895+ _ = makeDir (permissionMatchingPath )
896+ defer os .RemoveAll (permissionMatchingPath )
897+
898+ permissionMismatchPath , _ := getWorkDirPath ("permissionMismatchPath" )
899+ _ = os .MkdirAll (permissionMismatchPath , os .FileMode (0721 ))
900+ defer os .RemoveAll (permissionMismatchPath )
901+
902+ tests := []struct {
903+ desc string
904+ path string
905+ mode os.FileMode
906+ expectedError error
907+ }{
908+ {
909+ desc : "Invalid path" ,
910+ path : "invalid-path" ,
911+ mode : 0755 ,
912+ expectedError : fmt .Errorf ("CreateFile invalid-path: The system cannot find the file specified" ),
913+ },
914+ {
915+ desc : "permission matching path" ,
916+ path : permissionMatchingPath ,
917+ mode : 0755 ,
918+ expectedError : nil ,
919+ },
920+ {
921+ desc : "permission mismatch path" ,
922+ path : permissionMismatchPath ,
923+ mode : 0755 ,
924+ expectedError : nil ,
925+ },
926+ }
927+
928+ for _ , test := range tests {
929+ err := chmodIfPermissionMismatch (test .path , test .mode )
930+ if ! reflect .DeepEqual (err , test .expectedError ) {
931+ if err == nil || test .expectedError == nil && ! strings .Contains (err .Error (), test .expectedError .Error ()) {
932+ t .Errorf ("test[%s]: unexpected error: %v, expected error: %v" , test .desc , err , test .expectedError )
933+ }
934+ }
935+ }
936+ }
937+
938+ // getWorkDirPath returns the path to the current working directory
939+ func getWorkDirPath (dir string ) (string , error ) {
940+ path , err := os .Getwd ()
941+ if err != nil {
942+ return "" , err
943+ }
944+ return fmt .Sprintf ("%s%c%s" , path , os .PathSeparator , dir ), nil
945+ }
0 commit comments