fileutil_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2015 CoreOS, Inc.
  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. "testing"
  22. )
  23. func TestIsDirWriteable(t *testing.T) {
  24. tmpdir, err := ioutil.TempDir("", "")
  25. if err != nil {
  26. t.Fatalf("unexpected ioutil.TempDir error: %v", err)
  27. }
  28. defer os.RemoveAll(tmpdir)
  29. if err = IsDirWriteable(tmpdir); err != nil {
  30. t.Fatalf("unexpected IsDirWriteable error: %v", err)
  31. }
  32. if err = os.Chmod(tmpdir, 0444); err != nil {
  33. t.Fatalf("unexpected os.Chmod error: %v", err)
  34. }
  35. me, err := user.Current()
  36. if err != nil {
  37. // err can be non-nil when cross compiled
  38. // http://stackoverflow.com/questions/20609415/cross-compiling-user-current-not-implemented-on-linux-amd64
  39. t.Skipf("failed to get current user: %v", err)
  40. }
  41. if me.Name == "root" || me.Name == "Administrator" {
  42. // ideally we should check CAP_DAC_OVERRIDE.
  43. // but it does not matter for tests.
  44. t.Skipf("running as a superuser")
  45. }
  46. if err := IsDirWriteable(tmpdir); err == nil {
  47. t.Fatalf("expected IsDirWriteable to error")
  48. }
  49. }
  50. func TestReadDir(t *testing.T) {
  51. tmpdir, err := ioutil.TempDir("", "")
  52. defer os.RemoveAll(tmpdir)
  53. if err != nil {
  54. t.Fatalf("unexpected ioutil.TempDir error: %v", err)
  55. }
  56. files := []string{"def", "abc", "xyz", "ghi"}
  57. for _, f := range files {
  58. var fh *os.File
  59. fh, err = os.Create(filepath.Join(tmpdir, f))
  60. if err != nil {
  61. t.Fatalf("error creating file: %v", err)
  62. }
  63. if err = fh.Close(); err != nil {
  64. t.Fatalf("error closing file: %v", err)
  65. }
  66. }
  67. fs, err := ReadDir(tmpdir)
  68. if err != nil {
  69. t.Fatalf("error calling ReadDir: %v", err)
  70. }
  71. wfs := []string{"abc", "def", "ghi", "xyz"}
  72. if !reflect.DeepEqual(fs, wfs) {
  73. t.Fatalf("ReadDir: got %v, want %v", fs, wfs)
  74. }
  75. }
  76. func TestExist(t *testing.T) {
  77. f, err := ioutil.TempFile(os.TempDir(), "fileutil")
  78. if err != nil {
  79. t.Fatal(err)
  80. }
  81. f.Close()
  82. if g := Exist(f.Name()); g != true {
  83. t.Errorf("exist = %v, want true", g)
  84. }
  85. os.Remove(f.Name())
  86. if g := Exist(f.Name()); g != false {
  87. t.Errorf("exist = %v, want false", g)
  88. }
  89. }