package api import ( "os" "path/filepath" "testing" ) // A config file that exists but refuses a write is not writable. This is what a // ConfigMap mounted into a pod looks like, and calling it writable made the // console offer an editor whose every save failed. func TestIsFileWritable(t *testing.T) { if os.Geteuid() == 0 { t.Skip("root can write a read-only file") } dir := t.TempDir() rw := filepath.Join(dir, "rw.yaml") if err := os.WriteFile(rw, []byte("x"), 0o644); err != nil { t.Fatal(err) } ro := filepath.Join(dir, "ro.yaml") if err := os.WriteFile(ro, []byte("x"), 0o444); err != nil { t.Fatal(err) } cases := map[string]struct { path string want bool }{ "writable file": {rw, true}, "read-only file": {ro, false}, "missing file, open dir": {filepath.Join(dir, "new.yaml"), true}, "missing file, no dir": {filepath.Join(dir, "nodir", "x.yaml"), false}, "no path": {"", false}, } for name, tc := range cases { if got := isFileWritable(tc.path); got != tc.want { t.Errorf("%s: isFileWritable(%q) = %v, want %v", name, tc.path, got, tc.want) } } // A directory that refuses new files makes a missing file unwritable too. locked := filepath.Join(dir, "locked") if err := os.Mkdir(locked, 0o555); err != nil { t.Fatal(err) } t.Cleanup(func() { _ = os.Chmod(locked, 0o755) }) if isFileWritable(filepath.Join(locked, "x.yaml")) { t.Error("a file in a directory that refuses new files was reported writable") } }