fake_hostutil.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 hostutil
  14. import (
  15. "errors"
  16. "os"
  17. "sync"
  18. "k8s.io/utils/mount"
  19. )
  20. // FakeHostUtil is a fake HostUtils implementation for testing
  21. type FakeHostUtil struct {
  22. MountPoints []mount.MountPoint
  23. Filesystem map[string]FileType
  24. mutex sync.Mutex
  25. }
  26. // NewFakeHostUtil returns a struct that implements the HostUtils interface
  27. // for testing
  28. // TODO: no callers were initializing the struct with any MountPoints. Check
  29. // if those are still being used by any callers and if MountPoints still need
  30. // to be a part of the struct.
  31. func NewFakeHostUtil(fs map[string]FileType) *FakeHostUtil {
  32. return &FakeHostUtil{
  33. Filesystem: fs,
  34. }
  35. }
  36. // Compile-time check to make sure FakeHostUtil implements interface
  37. var _ HostUtils = &FakeHostUtil{}
  38. // DeviceOpened checks if block device referenced by pathname is in use by
  39. // checking if is listed as a device in the in-memory mountpoint table.
  40. func (hu *FakeHostUtil) DeviceOpened(pathname string) (bool, error) {
  41. hu.mutex.Lock()
  42. defer hu.mutex.Unlock()
  43. for _, mp := range hu.MountPoints {
  44. if mp.Device == pathname {
  45. return true, nil
  46. }
  47. }
  48. return false, nil
  49. }
  50. // PathIsDevice always returns true
  51. func (hu *FakeHostUtil) PathIsDevice(pathname string) (bool, error) {
  52. return true, nil
  53. }
  54. // GetDeviceNameFromMount given a mount point, find the volume id
  55. func (hu *FakeHostUtil) GetDeviceNameFromMount(mounter mount.Interface, mountPath, pluginMountDir string) (string, error) {
  56. return getDeviceNameFromMount(mounter, mountPath, pluginMountDir)
  57. }
  58. // MakeRShared checks if path is shared and bind-mounts it as rshared if needed.
  59. // No-op for testing
  60. func (hu *FakeHostUtil) MakeRShared(path string) error {
  61. return nil
  62. }
  63. // GetFileType checks for file/directory/socket/block/character devices.
  64. // Defaults to Directory if otherwise unspecified.
  65. func (hu *FakeHostUtil) GetFileType(pathname string) (FileType, error) {
  66. if t, ok := hu.Filesystem[pathname]; ok {
  67. return t, nil
  68. }
  69. return FileType("Directory"), nil
  70. }
  71. // PathExists checks if pathname exists.
  72. func (hu *FakeHostUtil) PathExists(pathname string) (bool, error) {
  73. if _, ok := hu.Filesystem[pathname]; ok {
  74. return true, nil
  75. }
  76. return false, nil
  77. }
  78. // EvalHostSymlinks returns the path name after evaluating symlinks.
  79. // No-op for testing
  80. func (hu *FakeHostUtil) EvalHostSymlinks(pathname string) (string, error) {
  81. return pathname, nil
  82. }
  83. // GetOwner returns the integer ID for the user and group of the given path
  84. // Not implemented for testing
  85. func (hu *FakeHostUtil) GetOwner(pathname string) (int64, int64, error) {
  86. return -1, -1, errors.New("GetOwner not implemented")
  87. }
  88. // GetSELinuxSupport tests if pathname is on a mount that supports SELinux.
  89. // Not implemented for testing
  90. func (hu *FakeHostUtil) GetSELinuxSupport(pathname string) (bool, error) {
  91. return false, errors.New("GetSELinuxSupport not implemented")
  92. }
  93. // GetMode returns permissions of pathname.
  94. // Not implemented for testing
  95. func (hu *FakeHostUtil) GetMode(pathname string) (os.FileMode, error) {
  96. return 0, errors.New("not implemented")
  97. }