fileutil_test.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2015 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package fileutil
  15. import (
  16. "io/ioutil"
  17. "os"
  18. "os/user"
  19. "path/filepath"
  20. "reflect"
  21. "runtime"
  22. "testing"
  23. )
  24. func TestIsDirWriteable(t *testing.T) {
  25. tmpdir, err := ioutil.TempDir("", "")
  26. if err != nil {
  27. t.Fatalf("unexpected ioutil.TempDir error: %v", err)
  28. }
  29. defer os.RemoveAll(tmpdir)
  30. if err = IsDirWriteable(tmpdir); err != nil {
  31. t.Fatalf("unexpected IsDirWriteable error: %v", err)
  32. }
  33. if err = os.Chmod(tmpdir, 0444); err != nil {
  34. t.Fatalf("unexpected os.Chmod error: %v", err)
  35. }
  36. me, err := user.Current()
  37. if err != nil {
  38. // err can be non-nil when cross compiled
  39. // http://stackoverflow.com/questions/20609415/cross-compiling-user-current-not-implemented-on-linux-amd64
  40. t.Skipf("failed to get current user: %v", err)
  41. }
  42. if me.Name == "root" || runtime.GOOS == "windows" {
  43. // ideally we should check CAP_DAC_OVERRIDE.
  44. // but it does not matter for tests.
  45. // Chmod is not supported under windows.
  46. t.Skipf("running as a superuser or in windows")
  47. }
  48. if err := IsDirWriteable(tmpdir); err == nil {
  49. t.Fatalf("expected IsDirWriteable to error")
  50. }
  51. }
  52. func TestReadDir(t *testing.T) {
  53. tmpdir, err := ioutil.TempDir("", "")
  54. defer os.RemoveAll(tmpdir)
  55. if err != nil {
  56. t.Fatalf("unexpected ioutil.TempDir error: %v", err)
  57. }
  58. files := []string{"def", "abc", "xyz", "ghi"}
  59. for _, f := range files {
  60. var fh *os.File
  61. fh, err = os.Create(filepath.Join(tmpdir, f))
  62. if err != nil {
  63. t.Fatalf("error creating file: %v", err)
  64. }
  65. if err = fh.Close(); err != nil {
  66. t.Fatalf("error closing file: %v", err)
  67. }
  68. }
  69. fs, err := ReadDir(tmpdir)
  70. if err != nil {
  71. t.Fatalf("error calling ReadDir: %v", err)
  72. }
  73. wfs := []string{"abc", "def", "ghi", "xyz"}
  74. if !reflect.DeepEqual(fs, wfs) {
  75. t.Fatalf("ReadDir: got %v, want %v", fs, wfs)
  76. }
  77. }
  78. func TestExist(t *testing.T) {
  79. f, err := ioutil.TempFile(os.TempDir(), "fileutil")
  80. if err != nil {
  81. t.Fatal(err)
  82. }
  83. f.Close()
  84. if g := Exist(f.Name()); !g {
  85. t.Errorf("exist = %v, want true", g)
  86. }
  87. os.Remove(f.Name())
  88. if g := Exist(f.Name()); g {
  89. t.Errorf("exist = %v, want false", g)
  90. }
  91. }