host_path_test.go 20 KB

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