PageRenderTime 84ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/pkg/volume/host_path/host_path_test.go

https://gitlab.com/unofficial-mirrors/kubernetes
Go | 687 lines | 595 code | 75 blank | 17 comment | 126 complexity | 3050af8d8cbb05ddc5b463e978ddaf21 MD5 | raw file
  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 {
  322. return true
  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 setUp() error {
  346. err := os.MkdirAll("/tmp/ExistingFolder", os.FileMode(0755))
  347. if err != nil {
  348. return err
  349. }
  350. f, err := os.OpenFile("/tmp/ExistingFolder/foo", os.O_CREATE, os.FileMode(0644))
  351. defer f.Close()
  352. if err != nil {
  353. return err
  354. }
  355. return nil
  356. }
  357. func tearDown() {
  358. os.RemoveAll("/tmp/ExistingFolder")
  359. }
  360. func TestOSFileTypeChecker(t *testing.T) {
  361. err := setUp()
  362. if err != nil {
  363. t.Error(err)
  364. }
  365. defer tearDown()
  366. testCases := []struct {
  367. name string
  368. path string
  369. desiredType string
  370. isDir bool
  371. isFile bool
  372. isSocket bool
  373. isBlock bool
  374. isChar bool
  375. }{
  376. {
  377. name: "Existing Folder",
  378. path: "/tmp/ExistingFolder",
  379. desiredType: string(utilmount.FileTypeDirectory),
  380. isDir: true,
  381. },
  382. {
  383. name: "Existing File",
  384. path: "/tmp/ExistingFolder/foo",
  385. desiredType: string(utilmount.FileTypeFile),
  386. isFile: true,
  387. },
  388. {
  389. name: "Existing Socket File",
  390. path: "/tmp/ExistingFolder/foo",
  391. desiredType: string(v1.HostPathSocket),
  392. isSocket: true,
  393. },
  394. {
  395. name: "Existing Character Device",
  396. path: "/tmp/ExistingFolder/foo",
  397. desiredType: string(v1.HostPathCharDev),
  398. isChar: true,
  399. },
  400. {
  401. name: "Existing Block Device",
  402. path: "/tmp/ExistingFolder/foo",
  403. desiredType: string(v1.HostPathBlockDev),
  404. isBlock: true,
  405. },
  406. }
  407. for i, tc := range testCases {
  408. fakeFTC := &fakeFileTypeChecker{desiredType: tc.desiredType}
  409. oftc := newFileTypeChecker(tc.path, fakeFTC)
  410. path := oftc.GetPath()
  411. if path != tc.path {
  412. t.Errorf("[%d: %q] got unexpected path: %s", i, tc.name, path)
  413. }
  414. exist := oftc.Exists()
  415. if !exist {
  416. t.Errorf("[%d: %q] path: %s does not exist", i, tc.name, path)
  417. }
  418. if tc.isDir {
  419. if !oftc.IsDir() {
  420. t.Errorf("[%d: %q] expected folder, got unexpected: %s", i, tc.name, path)
  421. }
  422. if oftc.IsFile() {
  423. t.Errorf("[%d: %q] expected folder, got unexpected file: %s", i, tc.name, path)
  424. }
  425. if oftc.IsSocket() {
  426. t.Errorf("[%d: %q] expected folder, got unexpected socket file: %s", i, tc.name, path)
  427. }
  428. if oftc.IsBlock() {
  429. t.Errorf("[%d: %q] expected folder, got unexpected block device: %s", i, tc.name, path)
  430. }
  431. if oftc.IsChar() {
  432. t.Errorf("[%d: %q] expected folder, got unexpected character device: %s", i, tc.name, path)
  433. }
  434. }
  435. if tc.isFile {
  436. if !oftc.IsFile() {
  437. t.Errorf("[%d: %q] expected file, got unexpected: %s", i, tc.name, path)
  438. }
  439. if oftc.IsDir() {
  440. t.Errorf("[%d: %q] expected file, got unexpected folder: %s", i, tc.name, path)
  441. }
  442. if oftc.IsSocket() {
  443. t.Errorf("[%d: %q] expected file, got unexpected socket file: %s", i, tc.name, path)
  444. }
  445. if oftc.IsBlock() {
  446. t.Errorf("[%d: %q] expected file, got unexpected block device: %s", i, tc.name, path)
  447. }
  448. if oftc.IsChar() {
  449. t.Errorf("[%d: %q] expected file, got unexpected character device: %s", i, tc.name, path)
  450. }
  451. }
  452. if tc.isSocket {
  453. if !oftc.IsSocket() {
  454. t.Errorf("[%d: %q] expected socket file, got unexpected: %s", i, tc.name, path)
  455. }
  456. if oftc.IsDir() {
  457. t.Errorf("[%d: %q] expected socket file, got unexpected folder: %s", i, tc.name, path)
  458. }
  459. if !oftc.IsFile() {
  460. t.Errorf("[%d: %q] expected socket file, got unexpected file: %s", i, tc.name, path)
  461. }
  462. if oftc.IsBlock() {
  463. t.Errorf("[%d: %q] expected socket file, got unexpected block device: %s", i, tc.name, path)
  464. }
  465. if oftc.IsChar() {
  466. t.Errorf("[%d: %q] expected socket file, got unexpected character device: %s", i, tc.name, path)
  467. }
  468. }
  469. if tc.isChar {
  470. if !oftc.IsChar() {
  471. t.Errorf("[%d: %q] expected character device, got unexpected: %s", i, tc.name, path)
  472. }
  473. if oftc.IsDir() {
  474. t.Errorf("[%d: %q] expected character device, got unexpected folder: %s", i, tc.name, path)
  475. }
  476. if !oftc.IsFile() {
  477. t.Errorf("[%d: %q] expected character device, got unexpected file: %s", i, tc.name, path)
  478. }
  479. if oftc.IsSocket() {
  480. t.Errorf("[%d: %q] expected character device, got unexpected socket file: %s", i, tc.name, path)
  481. }
  482. if oftc.IsBlock() {
  483. t.Errorf("[%d: %q] expected character device, got unexpected block device: %s", i, tc.name, path)
  484. }
  485. }
  486. if tc.isBlock {
  487. if !oftc.IsBlock() {
  488. t.Errorf("[%d: %q] expected block device, got unexpected: %s", i, tc.name, path)
  489. }
  490. if oftc.IsDir() {
  491. t.Errorf("[%d: %q] expected block device, got unexpected folder: %s", i, tc.name, path)
  492. }
  493. if !oftc.IsFile() {
  494. t.Errorf("[%d: %q] expected block device, got unexpected file: %s", i, tc.name, path)
  495. }
  496. if oftc.IsSocket() {
  497. t.Errorf("[%d: %q] expected block device, got unexpected socket file: %s", i, tc.name, path)
  498. }
  499. if oftc.IsChar() {
  500. t.Errorf("[%d: %q] expected block device, got unexpected character device: %s", i, tc.name, path)
  501. }
  502. }
  503. }
  504. }
  505. type fakeHostPathTypeChecker struct {
  506. name string
  507. path string
  508. exists bool
  509. isDir bool
  510. isFile bool
  511. isSocket bool
  512. isBlock bool
  513. isChar bool
  514. validpathType []*v1.HostPathType
  515. invalidpathType []*v1.HostPathType
  516. }
  517. func (ftc *fakeHostPathTypeChecker) MakeFile() error { return nil }
  518. func (ftc *fakeHostPathTypeChecker) MakeDir() error { return nil }
  519. func (ftc *fakeHostPathTypeChecker) Exists() bool { return ftc.exists }
  520. func (ftc *fakeHostPathTypeChecker) IsFile() bool { return ftc.isFile }
  521. func (ftc *fakeHostPathTypeChecker) IsDir() bool { return ftc.isDir }
  522. func (ftc *fakeHostPathTypeChecker) IsBlock() bool { return ftc.isBlock }
  523. func (ftc *fakeHostPathTypeChecker) IsChar() bool { return ftc.isChar }
  524. func (ftc *fakeHostPathTypeChecker) IsSocket() bool { return ftc.isSocket }
  525. func (ftc *fakeHostPathTypeChecker) GetPath() string { return ftc.path }
  526. func TestHostPathTypeCheckerInternal(t *testing.T) {
  527. testCases := []fakeHostPathTypeChecker{
  528. {
  529. name: "Existing Folder",
  530. path: "/existingFolder",
  531. isDir: true,
  532. exists: true,
  533. validpathType: newHostPathTypeList(string(v1.HostPathDirectoryOrCreate), string(v1.HostPathDirectory)),
  534. invalidpathType: newHostPathTypeList(string(v1.HostPathFileOrCreate), string(v1.HostPathFile),
  535. string(v1.HostPathSocket), string(v1.HostPathCharDev), string(v1.HostPathBlockDev)),
  536. },
  537. {
  538. name: "New Folder",
  539. path: "/newFolder",
  540. isDir: false,
  541. exists: false,
  542. validpathType: newHostPathTypeList(string(v1.HostPathDirectoryOrCreate)),
  543. invalidpathType: newHostPathTypeList(string(v1.HostPathDirectory), string(v1.HostPathFile),
  544. string(v1.HostPathSocket), string(v1.HostPathCharDev), string(v1.HostPathBlockDev)),
  545. },
  546. {
  547. name: "Existing File",
  548. path: "/existingFile",
  549. isFile: true,
  550. exists: true,
  551. validpathType: newHostPathTypeList(string(v1.HostPathFileOrCreate), string(v1.HostPathFile)),
  552. invalidpathType: newHostPathTypeList(string(v1.HostPathDirectoryOrCreate), string(v1.HostPathDirectory),
  553. string(v1.HostPathSocket), string(v1.HostPathCharDev), string(v1.HostPathBlockDev)),
  554. },
  555. {
  556. name: "New File",
  557. path: "/newFile",
  558. isFile: false,
  559. exists: false,
  560. validpathType: newHostPathTypeList(string(v1.HostPathFileOrCreate)),
  561. invalidpathType: newHostPathTypeList(string(v1.HostPathDirectory),
  562. string(v1.HostPathSocket), string(v1.HostPathCharDev), string(v1.HostPathBlockDev)),
  563. },
  564. {
  565. name: "Existing Socket",
  566. path: "/existing.socket",
  567. isSocket: true,
  568. isFile: true,
  569. exists: true,
  570. validpathType: newHostPathTypeList(string(v1.HostPathSocket), string(v1.HostPathFileOrCreate), string(v1.HostPathFile)),
  571. invalidpathType: newHostPathTypeList(string(v1.HostPathDirectoryOrCreate), string(v1.HostPathDirectory),
  572. string(v1.HostPathCharDev), string(v1.HostPathBlockDev)),
  573. },
  574. {
  575. name: "Existing Character Device",
  576. path: "/existing.char",
  577. isChar: true,
  578. isFile: true,
  579. exists: true,
  580. validpathType: newHostPathTypeList(string(v1.HostPathCharDev), string(v1.HostPathFileOrCreate), string(v1.HostPathFile)),
  581. invalidpathType: newHostPathTypeList(string(v1.HostPathDirectoryOrCreate), string(v1.HostPathDirectory),
  582. string(v1.HostPathSocket), string(v1.HostPathBlockDev)),
  583. },
  584. {
  585. name: "Existing Block Device",
  586. path: "/existing.block",
  587. isBlock: true,
  588. isFile: true,
  589. exists: true,
  590. validpathType: newHostPathTypeList(string(v1.HostPathBlockDev), string(v1.HostPathFileOrCreate), string(v1.HostPathFile)),
  591. invalidpathType: newHostPathTypeList(string(v1.HostPathDirectoryOrCreate), string(v1.HostPathDirectory),
  592. string(v1.HostPathSocket), string(v1.HostPathCharDev)),
  593. },
  594. }
  595. for i, tc := range testCases {
  596. for _, pathType := range tc.validpathType {
  597. err := checkTypeInternal(&tc, pathType)
  598. if err != nil {
  599. t.Errorf("[%d: %q] [%q] expected nil, got %v", i, tc.name, string(*pathType), err)
  600. }
  601. }
  602. for _, pathType := range tc.invalidpathType {
  603. checkResult := checkTypeInternal(&tc, pathType)
  604. if checkResult == nil {
  605. t.Errorf("[%d: %q] [%q] expected error, got nil", i, tc.name, string(*pathType))
  606. }
  607. }
  608. }
  609. }