os.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. HostName string
  26. Removes []string
  27. Files map[string][]*os.FileInfo
  28. }
  29. func NewFakeOS() *FakeOS {
  30. return &FakeOS{
  31. Removes: []string{},
  32. Files: make(map[string][]*os.FileInfo),
  33. }
  34. }
  35. // Mkdir is a fake call that just returns nil.
  36. func (FakeOS) MkdirAll(path string, perm os.FileMode) error {
  37. return nil
  38. }
  39. // Symlink is a fake call that just returns nil.
  40. func (FakeOS) Symlink(oldname string, newname string) error {
  41. return nil
  42. }
  43. // Stat is a fake that returns an error
  44. func (f FakeOS) Stat(path string) (os.FileInfo, error) {
  45. if f.StatFn != nil {
  46. return f.StatFn(path)
  47. }
  48. return nil, errors.New("unimplemented testing mock")
  49. }
  50. // Remove is a fake call that returns nil.
  51. func (f *FakeOS) Remove(path string) error {
  52. f.Removes = append(f.Removes, path)
  53. return nil
  54. }
  55. // Create is a fake call that returns nil.
  56. func (FakeOS) Create(path string) (*os.File, error) {
  57. return nil, nil
  58. }
  59. // Hostname is a fake call that returns nil.
  60. func (f *FakeOS) Hostname() (name string, err error) {
  61. return f.HostName, nil
  62. }
  63. // Chtimes is a fake call that returns nil.
  64. func (FakeOS) Chtimes(path string, atime time.Time, mtime time.Time) error {
  65. return nil
  66. }
  67. // Pipe is a fake call that returns nil.
  68. func (FakeOS) Pipe() (r *os.File, w *os.File, err error) {
  69. return nil, nil, nil
  70. }
  71. // ReadDir is a fake call that returns the files under the directory.
  72. func (f *FakeOS) ReadDir(dirname string) ([]os.FileInfo, error) {
  73. if f.ReadDirFn != nil {
  74. return f.ReadDirFn(dirname)
  75. }
  76. return nil, errors.New("unimplemented testing mock")
  77. }