os.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package testing
  14. import (
  15. "errors"
  16. "os"
  17. "time"
  18. )
  19. // FakeOS mocks out certain OS calls to avoid perturbing the filesystem
  20. // If a member of the form `*Fn` is set, that function will be called in place
  21. // of the real call.
  22. type FakeOS struct {
  23. StatFn func(string) (os.FileInfo, error)
  24. ReadDirFn func(string) ([]os.FileInfo, error)
  25. MkdirAllFn func(string, os.FileMode) error
  26. SymlinkFn func(string, string) error
  27. HostName string
  28. Removes []string
  29. Files map[string][]*os.FileInfo
  30. }
  31. func NewFakeOS() *FakeOS {
  32. return &FakeOS{
  33. Removes: []string{},
  34. Files: make(map[string][]*os.FileInfo),
  35. }
  36. }
  37. // Mkdir is a fake call that just returns nil.
  38. func (f *FakeOS) MkdirAll(path string, perm os.FileMode) error {
  39. if f.MkdirAllFn != nil {
  40. return f.MkdirAllFn(path, perm)
  41. }
  42. return nil
  43. }
  44. // Symlink is a fake call that just returns nil.
  45. func (f *FakeOS) Symlink(oldname string, newname string) error {
  46. if f.SymlinkFn != nil {
  47. return f.SymlinkFn(oldname, newname)
  48. }
  49. return nil
  50. }
  51. // Stat is a fake that returns an error
  52. func (f FakeOS) Stat(path string) (os.FileInfo, error) {
  53. if f.StatFn != nil {
  54. return f.StatFn(path)
  55. }
  56. return nil, errors.New("unimplemented testing mock")
  57. }
  58. // Remove is a fake call that returns nil.
  59. func (f *FakeOS) Remove(path string) error {
  60. f.Removes = append(f.Removes, path)
  61. return nil
  62. }
  63. // RemoveAll is a fake call that just returns nil.
  64. func (f *FakeOS) RemoveAll(path string) error {
  65. f.Removes = append(f.Removes, path)
  66. return nil
  67. }
  68. // Create is a fake call that returns nil.
  69. func (FakeOS) Create(path string) (*os.File, error) {
  70. return nil, nil
  71. }
  72. // Chmod is a fake call that returns nil.
  73. func (FakeOS) Chmod(path string, perm os.FileMode) error {
  74. return nil
  75. }
  76. // Hostname is a fake call that returns nil.
  77. func (f *FakeOS) Hostname() (name string, err error) {
  78. return f.HostName, nil
  79. }
  80. // Chtimes is a fake call that returns nil.
  81. func (FakeOS) Chtimes(path string, atime time.Time, mtime time.Time) error {
  82. return nil
  83. }
  84. // Pipe is a fake call that returns nil.
  85. func (FakeOS) Pipe() (r *os.File, w *os.File, err error) {
  86. return nil, nil, nil
  87. }
  88. // ReadDir is a fake call that returns the files under the directory.
  89. func (f *FakeOS) ReadDir(dirname string) ([]os.FileInfo, error) {
  90. if f.ReadDirFn != nil {
  91. return f.ReadDirFn(dirname)
  92. }
  93. return nil, nil
  94. }
  95. // Glob is a fake call that returns nil.
  96. func (f *FakeOS) Glob(pattern string) ([]string, error) {
  97. return nil, nil
  98. }