os.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. Copyright 2015 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 container
  14. import (
  15. "io/ioutil"
  16. "os"
  17. "path/filepath"
  18. "time"
  19. )
  20. // OSInterface collects system level operations that need to be mocked out
  21. // during tests.
  22. type OSInterface interface {
  23. MkdirAll(path string, perm os.FileMode) error
  24. Symlink(oldname string, newname string) error
  25. Stat(path string) (os.FileInfo, error)
  26. Remove(path string) error
  27. RemoveAll(path string) error
  28. Create(path string) (*os.File, error)
  29. Chmod(path string, perm os.FileMode) error
  30. Hostname() (name string, err error)
  31. Chtimes(path string, atime time.Time, mtime time.Time) error
  32. Pipe() (r *os.File, w *os.File, err error)
  33. ReadDir(dirname string) ([]os.FileInfo, error)
  34. Glob(pattern string) ([]string, error)
  35. }
  36. // RealOS is used to dispatch the real system level operations.
  37. type RealOS struct{}
  38. // MkdirAll will call os.MkdirAll to create a directory.
  39. func (RealOS) MkdirAll(path string, perm os.FileMode) error {
  40. return os.MkdirAll(path, perm)
  41. }
  42. // Symlink will call os.Symlink to create a symbolic link.
  43. func (RealOS) Symlink(oldname string, newname string) error {
  44. return os.Symlink(oldname, newname)
  45. }
  46. // Stat will call os.Stat to get the FileInfo for a given path
  47. func (RealOS) Stat(path string) (os.FileInfo, error) {
  48. return os.Stat(path)
  49. }
  50. // Remove will call os.Remove to remove the path.
  51. func (RealOS) Remove(path string) error {
  52. return os.Remove(path)
  53. }
  54. // RemoveAll will call os.RemoveAll to remove the path and its children.
  55. func (RealOS) RemoveAll(path string) error {
  56. return os.RemoveAll(path)
  57. }
  58. // Create will call os.Create to create and return a file
  59. // at path.
  60. func (RealOS) Create(path string) (*os.File, error) {
  61. return os.Create(path)
  62. }
  63. // Chmod will change the permissions on the specified path or return
  64. // an error.
  65. func (RealOS) Chmod(path string, perm os.FileMode) error {
  66. return os.Chmod(path, perm)
  67. }
  68. // Hostname will call os.Hostname to return the hostname.
  69. func (RealOS) Hostname() (name string, err error) {
  70. return os.Hostname()
  71. }
  72. // Chtimes will call os.Chtimes to change the atime and mtime of the path
  73. func (RealOS) Chtimes(path string, atime time.Time, mtime time.Time) error {
  74. return os.Chtimes(path, atime, mtime)
  75. }
  76. // Pipe will call os.Pipe to return a connected pair of pipe.
  77. func (RealOS) Pipe() (r *os.File, w *os.File, err error) {
  78. return os.Pipe()
  79. }
  80. // ReadDir will call ioutil.ReadDir to return the files under the directory.
  81. func (RealOS) ReadDir(dirname string) ([]os.FileInfo, error) {
  82. return ioutil.ReadDir(dirname)
  83. }
  84. // Glob will call filepath.Glob to return the names of all files matching
  85. // pattern.
  86. func (RealOS) Glob(pattern string) ([]string, error) {
  87. return filepath.Glob(pattern)
  88. }