PageRenderTime 58ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/pkg/volume/host_path/host_path_test.go

https://bitbucket.org/Jake-Qu/kubernetes-mirror
Go | 691 lines | 598 code | 76 blank | 17 comment | 126 complexity | 96a2506285ffbc8216679b6546ad4d4b MD5 | raw file
Possible License(s): MIT, MPL-2.0-no-copyleft-exception, 0BSD, CC0-1.0, BSD-2-Clause, Apache-2.0, BSD-3-Clause
  1. /*
  2. Copyright 2014 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 host_path
  14. import (
  15. "errors"
  16. "fmt"
  17. "os"
  18. "testing"
  19. "k8s.io/api/core/v1"
  20. "k8s.io/apimachinery/pkg/api/resource"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "k8s.io/apimachinery/pkg/types"
  23. "k8s.io/apimachinery/pkg/util/uuid"
  24. "k8s.io/client-go/kubernetes/fake"
  25. utilfile "k8s.io/kubernetes/pkg/util/file"
  26. utilmount "k8s.io/kubernetes/pkg/util/mount"
  27. "k8s.io/kubernetes/pkg/volume"
  28. volumetest "k8s.io/kubernetes/pkg/volume/testing"
  29. )
  30. func newHostPathType(pathType string) *v1.HostPathType {
  31. hostPathType := new(v1.HostPathType)
  32. *hostPathType = v1.HostPathType(pathType)
  33. return hostPathType
  34. }
  35. func newHostPathTypeList(pathType ...string) []*v1.HostPathType {
  36. typeList := []*v1.HostPathType{}
  37. for _, ele := range pathType {
  38. typeList = append(typeList, newHostPathType(ele))
  39. }
  40. return typeList
  41. }
  42. func TestCanSupport(t *testing.T) {
  43. plugMgr := volume.VolumePluginMgr{}
  44. plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), nil /* prober */, volumetest.NewFakeVolumeHost("fake", nil, nil))
  45. plug, err := plugMgr.FindPluginByName("kubernetes.io/host-path")
  46. if err != nil {
  47. t.Errorf("Can't find the plugin by name")
  48. }
  49. if plug.GetPluginName() != "kubernetes.io/host-path" {
  50. t.Errorf("Wrong name: %s", plug.GetPluginName())
  51. }
  52. if !plug.CanSupport(&volume.Spec{Volume: &v1.Volume{VolumeSource: v1.VolumeSource{HostPath: &v1.HostPathVolumeSource{}}}}) {
  53. t.Errorf("Expected true")
  54. }
  55. if !plug.CanSupport(&volume.Spec{PersistentVolume: &v1.PersistentVolume{Spec: v1.PersistentVolumeSpec{PersistentVolumeSource: v1.PersistentVolumeSource{HostPath: &v1.HostPathVolumeSource{}}}}}) {
  56. t.Errorf("Expected true")
  57. }
  58. if plug.CanSupport(&volume.Spec{Volume: &v1.Volume{VolumeSource: v1.VolumeSource{}}}) {
  59. t.Errorf("Expected false")
  60. }
  61. }
  62. func TestGetAccessModes(t *testing.T) {
  63. plugMgr := volume.VolumePluginMgr{}
  64. plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), nil /* prober */, volumetest.NewFakeVolumeHost("/tmp/fake", nil, nil))
  65. plug, err := plugMgr.FindPersistentPluginByName("kubernetes.io/host-path")
  66. if err != nil {
  67. t.Errorf("Can't find the plugin by name")
  68. }
  69. if len(plug.GetAccessModes()) != 1 || plug.GetAccessModes()[0] != v1.ReadWriteOnce {
  70. t.Errorf("Expected %s PersistentVolumeAccessMode", v1.ReadWriteOnce)
  71. }
  72. }
  73. func TestRecycler(t *testing.T) {
  74. plugMgr := volume.VolumePluginMgr{}
  75. pluginHost := volumetest.NewFakeVolumeHost("/tmp/fake", nil, nil)
  76. plugMgr.InitPlugins([]volume.VolumePlugin{&hostPathPlugin{nil, volume.VolumeConfig{}}}, nil, pluginHost)
  77. spec := &volume.Spec{PersistentVolume: &v1.PersistentVolume{Spec: v1.PersistentVolumeSpec{PersistentVolumeSource: v1.PersistentVolumeSource{HostPath: &v1.HostPathVolumeSource{Path: "/foo"}}}}}
  78. _, err := plugMgr.FindRecyclablePluginBySpec(spec)
  79. if err != nil {
  80. t.Errorf("Can't find the plugin by name")
  81. }
  82. }
  83. func TestDeleter(t *testing.T) {
  84. // Deleter has a hard-coded regex for "/tmp".
  85. tempPath := fmt.Sprintf("/tmp/hostpath/%s", uuid.NewUUID())
  86. defer os.RemoveAll(tempPath)
  87. err := os.MkdirAll(tempPath, 0750)
  88. if err != nil {
  89. t.Fatalf("Failed to create tmp directory for deleter: %v", err)
  90. }
  91. plugMgr := volume.VolumePluginMgr{}
  92. plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), nil /* prober */, volumetest.NewFakeVolumeHost("/tmp/fake", nil, nil))
  93. spec := &volume.Spec{PersistentVolume: &v1.PersistentVolume{Spec: v1.PersistentVolumeSpec{PersistentVolumeSource: v1.PersistentVolumeSource{HostPath: &v1.HostPathVolumeSource{Path: tempPath}}}}}
  94. plug, err := plugMgr.FindDeletablePluginBySpec(spec)
  95. if err != nil {
  96. t.Errorf("Can't find the plugin by name")
  97. }
  98. deleter, err := plug.NewDeleter(spec)
  99. if err != nil {
  100. t.Errorf("Failed to make a new Deleter: %v", err)
  101. }
  102. if deleter.GetPath() != tempPath {
  103. t.Errorf("Expected %s but got %s", tempPath, deleter.GetPath())
  104. }
  105. if err := deleter.Delete(); err != nil {
  106. t.Errorf("Mock Recycler expected to return nil but got %s", err)
  107. }
  108. if exists, _ := utilfile.FileExists(tempPath); exists {
  109. t.Errorf("Temp path expected to be deleted, but was found at %s", tempPath)
  110. }
  111. }
  112. func TestDeleterTempDir(t *testing.T) {
  113. tests := map[string]struct {
  114. expectedFailure bool
  115. path string
  116. }{
  117. "just-tmp": {true, "/tmp"},
  118. "not-tmp": {true, "/nottmp"},
  119. "good-tmp": {false, "/tmp/scratch"},
  120. }
  121. for name, test := range tests {
  122. plugMgr := volume.VolumePluginMgr{}
  123. plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), nil /* prober */, volumetest.NewFakeVolumeHost("/tmp/fake", nil, nil))
  124. spec := &volume.Spec{PersistentVolume: &v1.PersistentVolume{Spec: v1.PersistentVolumeSpec{PersistentVolumeSource: v1.PersistentVolumeSource{HostPath: &v1.HostPathVolumeSource{Path: test.path}}}}}
  125. plug, _ := plugMgr.FindDeletablePluginBySpec(spec)
  126. deleter, _ := plug.NewDeleter(spec)
  127. err := deleter.Delete()
  128. if err == nil && test.expectedFailure {
  129. t.Errorf("Expected failure for test '%s' but got nil err", name)
  130. }
  131. if err != nil && !test.expectedFailure {
  132. t.Errorf("Unexpected failure for test '%s': %v", name, err)
  133. }
  134. }
  135. }
  136. func TestProvisioner(t *testing.T) {
  137. tempPath := fmt.Sprintf("/tmp/hostpath/%s", uuid.NewUUID())
  138. defer os.RemoveAll(tempPath)
  139. err := os.MkdirAll(tempPath, 0750)
  140. if err != nil {
  141. t.Errorf("Failed to create tempPath %s error:%v", tempPath, err)
  142. }
  143. plugMgr := volume.VolumePluginMgr{}
  144. plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{ProvisioningEnabled: true}),
  145. nil,
  146. volumetest.NewFakeVolumeHost("/tmp/fake", nil, nil))
  147. spec := &volume.Spec{PersistentVolume: &v1.PersistentVolume{Spec: v1.PersistentVolumeSpec{PersistentVolumeSource: v1.PersistentVolumeSource{HostPath: &v1.HostPathVolumeSource{Path: tempPath}}}}}
  148. plug, err := plugMgr.FindCreatablePluginBySpec(spec)
  149. if err != nil {
  150. t.Errorf("Can't find the plugin by name")
  151. }
  152. options := volume.VolumeOptions{
  153. PVC: volumetest.CreateTestPVC("1Gi", []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce}),
  154. PersistentVolumeReclaimPolicy: v1.PersistentVolumeReclaimDelete,
  155. }
  156. creater, err := plug.NewProvisioner(options)
  157. if err != nil {
  158. t.Errorf("Failed to make a new Provisioner: %v", err)
  159. }
  160. pv, err := creater.Provision()
  161. if err != nil {
  162. t.Errorf("Unexpected error creating volume: %v", err)
  163. }
  164. if pv.Spec.HostPath.Path == "" {
  165. t.Errorf("Expected pv.Spec.HostPath.Path to not be empty: %#v", pv)
  166. }
  167. expectedCapacity := resource.NewQuantity(1*1024*1024*1024, resource.BinarySI)
  168. actualCapacity := pv.Spec.Capacity[v1.ResourceStorage]
  169. expectedAmt := expectedCapacity.Value()
  170. actualAmt := actualCapacity.Value()
  171. if expectedAmt != actualAmt {
  172. t.Errorf("Expected capacity %+v but got %+v", expectedAmt, actualAmt)
  173. }
  174. if pv.Spec.PersistentVolumeReclaimPolicy != v1.PersistentVolumeReclaimDelete {
  175. t.Errorf("Expected reclaim policy %+v but got %+v", v1.PersistentVolumeReclaimDelete, pv.Spec.PersistentVolumeReclaimPolicy)
  176. }
  177. os.RemoveAll(pv.Spec.HostPath.Path)
  178. }
  179. func TestInvalidHostPath(t *testing.T) {
  180. plugMgr := volume.VolumePluginMgr{}
  181. plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), nil /* prober */, volumetest.NewFakeVolumeHost("fake", nil, nil))
  182. plug, err := plugMgr.FindPluginByName(hostPathPluginName)
  183. if err != nil {
  184. t.Fatalf("Unable to find plugin %s by name: %v", hostPathPluginName, err)
  185. }
  186. spec := &v1.Volume{
  187. Name: "vol1",
  188. VolumeSource: v1.VolumeSource{HostPath: &v1.HostPathVolumeSource{Path: "/no/backsteps/allowed/.."}},
  189. }
  190. pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{UID: types.UID("poduid")}}
  191. mounter, err := plug.NewMounter(volume.NewSpecFromVolume(spec), pod, volume.VolumeOptions{})
  192. if err != nil {
  193. t.Fatal(err)
  194. }
  195. err = mounter.SetUp(nil)
  196. expectedMsg := "invalid HostPath `/no/backsteps/allowed/..`: must not contain '..'"
  197. if err.Error() != expectedMsg {
  198. t.Fatalf("expected error `%s` but got `%s`", expectedMsg, err)
  199. }
  200. }
  201. func TestPlugin(t *testing.T) {
  202. plugMgr := volume.VolumePluginMgr{}
  203. plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), nil /* prober */, volumetest.NewFakeVolumeHost("fake", nil, nil))
  204. plug, err := plugMgr.FindPluginByName("kubernetes.io/host-path")
  205. if err != nil {
  206. t.Errorf("Can't find the plugin by name")
  207. }
  208. volPath := "/tmp/vol1"
  209. spec := &v1.Volume{
  210. Name: "vol1",
  211. VolumeSource: v1.VolumeSource{HostPath: &v1.HostPathVolumeSource{Path: volPath, Type: newHostPathType(string(v1.HostPathDirectoryOrCreate))}},
  212. }
  213. pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{UID: types.UID("poduid")}}
  214. defer os.RemoveAll(volPath)
  215. mounter, err := plug.NewMounter(volume.NewSpecFromVolume(spec), pod, volume.VolumeOptions{})
  216. if err != nil {
  217. t.Errorf("Failed to make a new Mounter: %v", err)
  218. }
  219. if mounter == nil {
  220. t.Fatalf("Got a nil Mounter")
  221. }
  222. path := mounter.GetPath()
  223. if path != volPath {
  224. t.Errorf("Got unexpected path: %s", path)
  225. }
  226. if err := mounter.SetUp(nil); err != nil {
  227. t.Errorf("Expected success, got: %v", err)
  228. }
  229. unmounter, err := plug.NewUnmounter("vol1", types.UID("poduid"))
  230. if err != nil {
  231. t.Errorf("Failed to make a new Unmounter: %v", err)
  232. }
  233. if unmounter == nil {
  234. t.Fatalf("Got a nil Unmounter")
  235. }
  236. if err := unmounter.TearDown(); err != nil {
  237. t.Errorf("Expected success, got: %v", err)
  238. }
  239. }
  240. func TestPersistentClaimReadOnlyFlag(t *testing.T) {
  241. pv := &v1.PersistentVolume{
  242. ObjectMeta: metav1.ObjectMeta{
  243. Name: "pvA",
  244. },
  245. Spec: v1.PersistentVolumeSpec{
  246. PersistentVolumeSource: v1.PersistentVolumeSource{
  247. HostPath: &v1.HostPathVolumeSource{Path: "foo", Type: newHostPathType(string(v1.HostPathDirectoryOrCreate))},
  248. },
  249. ClaimRef: &v1.ObjectReference{
  250. Name: "claimA",
  251. },
  252. },
  253. }
  254. defer os.RemoveAll("foo")
  255. claim := &v1.PersistentVolumeClaim{
  256. ObjectMeta: metav1.ObjectMeta{
  257. Name: "claimA",
  258. Namespace: "nsA",
  259. },
  260. Spec: v1.PersistentVolumeClaimSpec{
  261. VolumeName: "pvA",
  262. },
  263. Status: v1.PersistentVolumeClaimStatus{
  264. Phase: v1.ClaimBound,
  265. },
  266. }
  267. client := fake.NewSimpleClientset(pv, claim)
  268. plugMgr := volume.VolumePluginMgr{}
  269. plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), nil /* prober */, volumetest.NewFakeVolumeHost("/tmp/fake", client, nil))
  270. plug, _ := plugMgr.FindPluginByName(hostPathPluginName)
  271. // readOnly bool is supplied by persistent-claim volume source when its mounter creates other volumes
  272. spec := volume.NewSpecFromPersistentVolume(pv, true)
  273. pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{UID: types.UID("poduid")}}
  274. mounter, _ := plug.NewMounter(spec, pod, volume.VolumeOptions{})
  275. if mounter == nil {
  276. t.Fatalf("Got a nil Mounter")
  277. }
  278. if !mounter.GetAttributes().ReadOnly {
  279. t.Errorf("Expected true for mounter.IsReadOnly")
  280. }
  281. }
  282. type fakeFileTypeChecker struct {
  283. desiredType string
  284. }
  285. func (fftc *fakeFileTypeChecker) Mount(source string, target string, fstype string, options []string) error {
  286. return nil
  287. }
  288. func (fftc *fakeFileTypeChecker) Unmount(target string) error {
  289. return nil
  290. }
  291. func (fftc *fakeFileTypeChecker) List() ([]utilmount.MountPoint, error) {
  292. return nil, nil
  293. }
  294. func (fftc *fakeFileTypeChecker) IsMountPointMatch(mp utilmount.MountPoint, dir string) bool {
  295. return false
  296. }
  297. func (fftc *fakeFileTypeChecker) IsNotMountPoint(file string) (bool, error) {
  298. return false, nil
  299. }
  300. func (fftc *fakeFileTypeChecker) IsLikelyNotMountPoint(file string) (bool, error) {
  301. return false, nil
  302. }
  303. func (fftc *fakeFileTypeChecker) DeviceOpened(pathname string) (bool, error) {
  304. return false, nil
  305. }
  306. func (fftc *fakeFileTypeChecker) PathIsDevice(pathname string) (bool, error) {
  307. return false, nil
  308. }
  309. func (fftc *fakeFileTypeChecker) GetDeviceNameFromMount(mountPath, pluginDir string) (string, error) {
  310. return "fake", nil
  311. }
  312. func (fftc *fakeFileTypeChecker) MakeRShared(path string) error {
  313. return nil
  314. }
  315. func (fftc *fakeFileTypeChecker) MakeFile(pathname string) error {
  316. return nil
  317. }
  318. func (fftc *fakeFileTypeChecker) MakeDir(pathname string) error {
  319. return nil
  320. }
  321. func (fftc *fakeFileTypeChecker) ExistsPath(pathname string) (bool, error) {
  322. return true, nil
  323. }
  324. func (fftc *fakeFileTypeChecker) GetFileType(_ string) (utilmount.FileType, error) {
  325. return utilmount.FileType(fftc.desiredType), nil
  326. }
  327. func (fftc *fakeFileTypeChecker) PrepareSafeSubpath(subPath utilmount.Subpath) (newHostPath string, cleanupAction func(), err error) {
  328. return "", nil, nil
  329. }
  330. func (fftc *fakeFileTypeChecker) CleanSubPaths(_, _ string) error {
  331. return nil
  332. }
  333. func (fftc *fakeFileTypeChecker) SafeMakeDir(_, _ string, _ os.FileMode) error {
  334. return nil
  335. }
  336. func (fftc *fakeFileTypeChecker) GetMountRefs(pathname string) ([]string, error) {
  337. return nil, errors.New("not implemented")
  338. }
  339. func (fftc *fakeFileTypeChecker) GetFSGroup(pathname string) (int64, error) {
  340. return -1, errors.New("not implemented")
  341. }
  342. func (fftc *fakeFileTypeChecker) GetSELinuxSupport(pathname string) (bool, error) {
  343. return false, errors.New("not implemented")
  344. }
  345. func (fftc *fakeFileTypeChecker) GetMode(pathname string) (os.FileMode, error) {
  346. return 0, errors.New("not implemented")
  347. }
  348. func setUp() error {
  349. err := os.MkdirAll("/tmp/ExistingFolder", os.FileMode(0755))
  350. if err != nil {
  351. return err
  352. }
  353. f, err := os.OpenFile("/tmp/ExistingFolder/foo", os.O_CREATE, os.FileMode(0644))
  354. defer f.Close()
  355. if err != nil {
  356. return err
  357. }
  358. return nil
  359. }
  360. func tearDown() {
  361. os.RemoveAll("/tmp/ExistingFolder")
  362. }
  363. func TestOSFileTypeChecker(t *testing.T) {
  364. err := setUp()
  365. if err != nil {
  366. t.Error(err)
  367. }
  368. defer tearDown()
  369. testCases := []struct {
  370. name string
  371. path string
  372. desiredType string
  373. isDir bool
  374. isFile bool
  375. isSocket bool
  376. isBlock bool
  377. isChar bool
  378. }{
  379. {
  380. name: "Existing Folder",
  381. path: "/tmp/ExistingFolder",
  382. desiredType: string(utilmount.FileTypeDirectory),
  383. isDir: true,
  384. },
  385. {
  386. name: "Existing File",
  387. path: "/tmp/ExistingFolder/foo",
  388. desiredType: string(utilmount.FileTypeFile),
  389. isFile: true,
  390. },
  391. {
  392. name: "Existing Socket File",
  393. path: "/tmp/ExistingFolder/foo",
  394. desiredType: string(v1.HostPathSocket),
  395. isSocket: true,
  396. },
  397. {
  398. name: "Existing Character Device",
  399. path: "/tmp/ExistingFolder/foo",
  400. desiredType: string(v1.HostPathCharDev),
  401. isChar: true,
  402. },
  403. {
  404. name: "Existing Block Device",
  405. path: "/tmp/ExistingFolder/foo",
  406. desiredType: string(v1.HostPathBlockDev),
  407. isBlock: true,
  408. },
  409. }
  410. for i, tc := range testCases {
  411. fakeFTC := &fakeFileTypeChecker{desiredType: tc.desiredType}
  412. oftc := newFileTypeChecker(tc.path, fakeFTC)
  413. path := oftc.GetPath()
  414. if path != tc.path {
  415. t.Errorf("[%d: %q] got unexpected path: %s", i, tc.name, path)
  416. }
  417. exist := oftc.Exists()
  418. if !exist {
  419. t.Errorf("[%d: %q] path: %s does not exist", i, tc.name, path)
  420. }
  421. if tc.isDir {
  422. if !oftc.IsDir() {
  423. t.Errorf("[%d: %q] expected folder, got unexpected: %s", i, tc.name, path)
  424. }
  425. if oftc.IsFile() {
  426. t.Errorf("[%d: %q] expected folder, got unexpected file: %s", i, tc.name, path)
  427. }
  428. if oftc.IsSocket() {
  429. t.Errorf("[%d: %q] expected folder, got unexpected socket file: %s", i, tc.name, path)
  430. }
  431. if oftc.IsBlock() {
  432. t.Errorf("[%d: %q] expected folder, got unexpected block device: %s", i, tc.name, path)
  433. }
  434. if oftc.IsChar() {
  435. t.Errorf("[%d: %q] expected folder, got unexpected character device: %s", i, tc.name, path)
  436. }
  437. }
  438. if tc.isFile {
  439. if !oftc.IsFile() {
  440. t.Errorf("[%d: %q] expected file, got unexpected: %s", i, tc.name, path)
  441. }
  442. if oftc.IsDir() {
  443. t.Errorf("[%d: %q] expected file, got unexpected folder: %s", i, tc.name, path)
  444. }
  445. if oftc.IsSocket() {
  446. t.Errorf("[%d: %q] expected file, got unexpected socket file: %s", i, tc.name, path)
  447. }
  448. if oftc.IsBlock() {
  449. t.Errorf("[%d: %q] expected file, got unexpected block device: %s", i, tc.name, path)
  450. }
  451. if oftc.IsChar() {
  452. t.Errorf("[%d: %q] expected file, got unexpected character device: %s", i, tc.name, path)
  453. }
  454. }
  455. if tc.isSocket {
  456. if !oftc.IsSocket() {
  457. t.Errorf("[%d: %q] expected socket file, got unexpected: %s", i, tc.name, path)
  458. }
  459. if oftc.IsDir() {
  460. t.Errorf("[%d: %q] expected socket file, got unexpected folder: %s", i, tc.name, path)
  461. }
  462. if !oftc.IsFile() {
  463. t.Errorf("[%d: %q] expected socket file, got unexpected file: %s", i, tc.name, path)
  464. }
  465. if oftc.IsBlock() {
  466. t.Errorf("[%d: %q] expected socket file, got unexpected block device: %s", i, tc.name, path)
  467. }
  468. if oftc.IsChar() {
  469. t.Errorf("[%d: %q] expected socket file, got unexpected character device: %s", i, tc.name, path)
  470. }
  471. }
  472. if tc.isChar {
  473. if !oftc.IsChar() {
  474. t.Errorf("[%d: %q] expected character device, got unexpected: %s", i, tc.name, path)
  475. }
  476. if oftc.IsDir() {
  477. t.Errorf("[%d: %q] expected character device, got unexpected folder: %s", i, tc.name, path)
  478. }
  479. if !oftc.IsFile() {
  480. t.Errorf("[%d: %q] expected character device, got unexpected file: %s", i, tc.name, path)
  481. }
  482. if oftc.IsSocket() {
  483. t.Errorf("[%d: %q] expected character device, got unexpected socket file: %s", i, tc.name, path)
  484. }
  485. if oftc.IsBlock() {
  486. t.Errorf("[%d: %q] expected character device, got unexpected block device: %s", i, tc.name, path)
  487. }
  488. }
  489. if tc.isBlock {
  490. if !oftc.IsBlock() {
  491. t.Errorf("[%d: %q] expected block device, got unexpected: %s", i, tc.name, path)
  492. }
  493. if oftc.IsDir() {
  494. t.Errorf("[%d: %q] expected block device, got unexpected folder: %s", i, tc.name, path)
  495. }
  496. if !oftc.IsFile() {
  497. t.Errorf("[%d: %q] expected block device, got unexpected file: %s", i, tc.name, path)
  498. }
  499. if oftc.IsSocket() {
  500. t.Errorf("[%d: %q] expected block device, got unexpected socket file: %s", i, tc.name, path)
  501. }
  502. if oftc.IsChar() {
  503. t.Errorf("[%d: %q] expected block device, got unexpected character device: %s", i, tc.name, path)
  504. }
  505. }
  506. }
  507. }
  508. type fakeHostPathTypeChecker struct {
  509. name string
  510. path string
  511. exists bool
  512. isDir bool
  513. isFile bool
  514. isSocket bool
  515. isBlock bool
  516. isChar bool
  517. validpathType []*v1.HostPathType
  518. invalidpathType []*v1.HostPathType
  519. }
  520. func (ftc *fakeHostPathTypeChecker) MakeFile() error { return nil }
  521. func (ftc *fakeHostPathTypeChecker) MakeDir() error { return nil }
  522. func (ftc *fakeHostPathTypeChecker) Exists() bool { return ftc.exists }
  523. func (ftc *fakeHostPathTypeChecker) IsFile() bool { return ftc.isFile }
  524. func (ftc *fakeHostPathTypeChecker) IsDir() bool { return ftc.isDir }
  525. func (ftc *fakeHostPathTypeChecker) IsBlock() bool { return ftc.isBlock }
  526. func (ftc *fakeHostPathTypeChecker) IsChar() bool { return ftc.isChar }
  527. func (ftc *fakeHostPathTypeChecker) IsSocket() bool { return ftc.isSocket }
  528. func (ftc *fakeHostPathTypeChecker) GetPath() string { return ftc.path }
  529. func TestHostPathTypeCheckerInternal(t *testing.T) {
  530. testCases := []fakeHostPathTypeChecker{
  531. {
  532. name: "Existing Folder",
  533. path: "/existingFolder",
  534. isDir: true,
  535. exists: true,
  536. validpathType: newHostPathTypeList(string(v1.HostPathDirectoryOrCreate), string(v1.HostPathDirectory)),
  537. invalidpathType: newHostPathTypeList(string(v1.HostPathFileOrCreate), string(v1.HostPathFile),
  538. string(v1.HostPathSocket), string(v1.HostPathCharDev), string(v1.HostPathBlockDev)),
  539. },
  540. {
  541. name: "New Folder",
  542. path: "/newFolder",
  543. isDir: false,
  544. exists: false,
  545. validpathType: newHostPathTypeList(string(v1.HostPathDirectoryOrCreate)),
  546. invalidpathType: newHostPathTypeList(string(v1.HostPathDirectory), string(v1.HostPathFile),
  547. string(v1.HostPathSocket), string(v1.HostPathCharDev), string(v1.HostPathBlockDev)),
  548. },
  549. {
  550. name: "Existing File",
  551. path: "/existingFile",
  552. isFile: true,
  553. exists: true,
  554. validpathType: newHostPathTypeList(string(v1.HostPathFileOrCreate), string(v1.HostPathFile)),
  555. invalidpathType: newHostPathTypeList(string(v1.HostPathDirectoryOrCreate), string(v1.HostPathDirectory),
  556. string(v1.HostPathSocket), string(v1.HostPathCharDev), string(v1.HostPathBlockDev)),
  557. },
  558. {
  559. name: "New File",
  560. path: "/newFile",
  561. isFile: false,
  562. exists: false,
  563. validpathType: newHostPathTypeList(string(v1.HostPathFileOrCreate)),
  564. invalidpathType: newHostPathTypeList(string(v1.HostPathDirectory),
  565. string(v1.HostPathSocket), string(v1.HostPathCharDev), string(v1.HostPathBlockDev)),
  566. },
  567. {
  568. name: "Existing Socket",
  569. path: "/existing.socket",
  570. isSocket: true,
  571. isFile: true,
  572. exists: true,
  573. validpathType: newHostPathTypeList(string(v1.HostPathSocket), string(v1.HostPathFileOrCreate), string(v1.HostPathFile)),
  574. invalidpathType: newHostPathTypeList(string(v1.HostPathDirectoryOrCreate), string(v1.HostPathDirectory),
  575. string(v1.HostPathCharDev), string(v1.HostPathBlockDev)),
  576. },
  577. {
  578. name: "Existing Character Device",
  579. path: "/existing.char",
  580. isChar: true,
  581. isFile: true,
  582. exists: true,
  583. validpathType: newHostPathTypeList(string(v1.HostPathCharDev), string(v1.HostPathFileOrCreate), string(v1.HostPathFile)),
  584. invalidpathType: newHostPathTypeList(string(v1.HostPathDirectoryOrCreate), string(v1.HostPathDirectory),
  585. string(v1.HostPathSocket), string(v1.HostPathBlockDev)),
  586. },
  587. {
  588. name: "Existing Block Device",
  589. path: "/existing.block",
  590. isBlock: true,
  591. isFile: true,
  592. exists: true,
  593. validpathType: newHostPathTypeList(string(v1.HostPathBlockDev), string(v1.HostPathFileOrCreate), string(v1.HostPathFile)),
  594. invalidpathType: newHostPathTypeList(string(v1.HostPathDirectoryOrCreate), string(v1.HostPathDirectory),
  595. string(v1.HostPathSocket), string(v1.HostPathCharDev)),
  596. },
  597. }
  598. for i, tc := range testCases {
  599. for _, pathType := range tc.validpathType {
  600. err := checkTypeInternal(&tc, pathType)
  601. if err != nil {
  602. t.Errorf("[%d: %q] [%q] expected nil, got %v", i, tc.name, string(*pathType), err)
  603. }
  604. }
  605. for _, pathType := range tc.invalidpathType {
  606. checkResult := checkTypeInternal(&tc, pathType)
  607. if checkResult == nil {
  608. t.Errorf("[%d: %q] [%q] expected error, got nil", i, tc.name, string(*pathType))
  609. }
  610. }
  611. }
  612. }