projected_test.go 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266
  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 projected
  14. import (
  15. "fmt"
  16. "io/ioutil"
  17. "os"
  18. "path/filepath"
  19. "reflect"
  20. "strings"
  21. "testing"
  22. authenticationv1 "k8s.io/api/authentication/v1"
  23. "k8s.io/api/core/v1"
  24. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  25. "k8s.io/apimachinery/pkg/runtime"
  26. "k8s.io/apimachinery/pkg/types"
  27. "k8s.io/apimachinery/pkg/util/diff"
  28. utilruntime "k8s.io/apimachinery/pkg/util/runtime"
  29. clientset "k8s.io/client-go/kubernetes"
  30. "k8s.io/client-go/kubernetes/fake"
  31. clitesting "k8s.io/client-go/testing"
  32. pkgauthenticationv1 "k8s.io/kubernetes/pkg/apis/authentication/v1"
  33. pkgcorev1 "k8s.io/kubernetes/pkg/apis/core/v1"
  34. "k8s.io/kubernetes/pkg/volume"
  35. "k8s.io/kubernetes/pkg/volume/emptydir"
  36. volumetest "k8s.io/kubernetes/pkg/volume/testing"
  37. "k8s.io/kubernetes/pkg/volume/util"
  38. )
  39. func TestCollectDataWithSecret(t *testing.T) {
  40. caseMappingMode := int32(0400)
  41. cases := []struct {
  42. name string
  43. mappings []v1.KeyToPath
  44. secret *v1.Secret
  45. mode int32
  46. optional bool
  47. payload map[string]util.FileProjection
  48. success bool
  49. }{
  50. {
  51. name: "no overrides",
  52. secret: &v1.Secret{
  53. Data: map[string][]byte{
  54. "foo": []byte("foo"),
  55. "bar": []byte("bar"),
  56. },
  57. },
  58. mode: 0644,
  59. payload: map[string]util.FileProjection{
  60. "foo": {Data: []byte("foo"), Mode: 0644},
  61. "bar": {Data: []byte("bar"), Mode: 0644},
  62. },
  63. success: true,
  64. },
  65. {
  66. name: "basic 1",
  67. mappings: []v1.KeyToPath{
  68. {
  69. Key: "foo",
  70. Path: "path/to/foo.txt",
  71. },
  72. },
  73. secret: &v1.Secret{
  74. Data: map[string][]byte{
  75. "foo": []byte("foo"),
  76. "bar": []byte("bar"),
  77. },
  78. },
  79. mode: 0644,
  80. payload: map[string]util.FileProjection{
  81. "path/to/foo.txt": {Data: []byte("foo"), Mode: 0644},
  82. },
  83. success: true,
  84. },
  85. {
  86. name: "subdirs",
  87. mappings: []v1.KeyToPath{
  88. {
  89. Key: "foo",
  90. Path: "path/to/1/2/3/foo.txt",
  91. },
  92. },
  93. secret: &v1.Secret{
  94. Data: map[string][]byte{
  95. "foo": []byte("foo"),
  96. "bar": []byte("bar"),
  97. },
  98. },
  99. mode: 0644,
  100. payload: map[string]util.FileProjection{
  101. "path/to/1/2/3/foo.txt": {Data: []byte("foo"), Mode: 0644},
  102. },
  103. success: true,
  104. },
  105. {
  106. name: "subdirs 2",
  107. mappings: []v1.KeyToPath{
  108. {
  109. Key: "foo",
  110. Path: "path/to/1/2/3/foo.txt",
  111. },
  112. },
  113. secret: &v1.Secret{
  114. Data: map[string][]byte{
  115. "foo": []byte("foo"),
  116. "bar": []byte("bar"),
  117. },
  118. },
  119. mode: 0644,
  120. payload: map[string]util.FileProjection{
  121. "path/to/1/2/3/foo.txt": {Data: []byte("foo"), Mode: 0644},
  122. },
  123. success: true,
  124. },
  125. {
  126. name: "subdirs 3",
  127. mappings: []v1.KeyToPath{
  128. {
  129. Key: "foo",
  130. Path: "path/to/1/2/3/foo.txt",
  131. },
  132. {
  133. Key: "bar",
  134. Path: "another/path/to/the/esteemed/bar.bin",
  135. },
  136. },
  137. secret: &v1.Secret{
  138. Data: map[string][]byte{
  139. "foo": []byte("foo"),
  140. "bar": []byte("bar"),
  141. },
  142. },
  143. mode: 0644,
  144. payload: map[string]util.FileProjection{
  145. "path/to/1/2/3/foo.txt": {Data: []byte("foo"), Mode: 0644},
  146. "another/path/to/the/esteemed/bar.bin": {Data: []byte("bar"), Mode: 0644},
  147. },
  148. success: true,
  149. },
  150. {
  151. name: "non existent key",
  152. mappings: []v1.KeyToPath{
  153. {
  154. Key: "zab",
  155. Path: "path/to/foo.txt",
  156. },
  157. },
  158. secret: &v1.Secret{
  159. Data: map[string][]byte{
  160. "foo": []byte("foo"),
  161. "bar": []byte("bar"),
  162. },
  163. },
  164. mode: 0644,
  165. success: false,
  166. },
  167. {
  168. name: "mapping with Mode",
  169. mappings: []v1.KeyToPath{
  170. {
  171. Key: "foo",
  172. Path: "foo.txt",
  173. Mode: &caseMappingMode,
  174. },
  175. {
  176. Key: "bar",
  177. Path: "bar.bin",
  178. Mode: &caseMappingMode,
  179. },
  180. },
  181. secret: &v1.Secret{
  182. Data: map[string][]byte{
  183. "foo": []byte("foo"),
  184. "bar": []byte("bar"),
  185. },
  186. },
  187. mode: 0644,
  188. payload: map[string]util.FileProjection{
  189. "foo.txt": {Data: []byte("foo"), Mode: caseMappingMode},
  190. "bar.bin": {Data: []byte("bar"), Mode: caseMappingMode},
  191. },
  192. success: true,
  193. },
  194. {
  195. name: "mapping with defaultMode",
  196. mappings: []v1.KeyToPath{
  197. {
  198. Key: "foo",
  199. Path: "foo.txt",
  200. },
  201. {
  202. Key: "bar",
  203. Path: "bar.bin",
  204. },
  205. },
  206. secret: &v1.Secret{
  207. Data: map[string][]byte{
  208. "foo": []byte("foo"),
  209. "bar": []byte("bar"),
  210. },
  211. },
  212. mode: 0644,
  213. payload: map[string]util.FileProjection{
  214. "foo.txt": {Data: []byte("foo"), Mode: 0644},
  215. "bar.bin": {Data: []byte("bar"), Mode: 0644},
  216. },
  217. success: true,
  218. },
  219. {
  220. name: "optional non existent key",
  221. mappings: []v1.KeyToPath{
  222. {
  223. Key: "zab",
  224. Path: "path/to/foo.txt",
  225. },
  226. },
  227. secret: &v1.Secret{
  228. Data: map[string][]byte{
  229. "foo": []byte("foo"),
  230. "bar": []byte("bar"),
  231. },
  232. },
  233. mode: 0644,
  234. optional: true,
  235. payload: map[string]util.FileProjection{},
  236. success: true,
  237. },
  238. }
  239. for _, tc := range cases {
  240. testNamespace := "test_projected_namespace"
  241. tc.secret.ObjectMeta = metav1.ObjectMeta{
  242. Namespace: testNamespace,
  243. Name: tc.name,
  244. }
  245. source := makeProjection(tc.name, tc.mode, "secret")
  246. source.Sources[0].Secret.Items = tc.mappings
  247. source.Sources[0].Secret.Optional = &tc.optional
  248. testPodUID := types.UID("test_pod_uid")
  249. pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: testNamespace, UID: testPodUID}}
  250. client := fake.NewSimpleClientset(tc.secret)
  251. _, host := newTestHost(t, client)
  252. var myVolumeMounter = projectedVolumeMounter{
  253. projectedVolume: &projectedVolume{
  254. sources: source.Sources,
  255. podUID: pod.UID,
  256. plugin: &projectedPlugin{
  257. host: host,
  258. getSecret: host.GetSecretFunc(),
  259. },
  260. },
  261. source: *source,
  262. pod: pod,
  263. }
  264. actualPayload, err := myVolumeMounter.collectData()
  265. if err != nil && tc.success {
  266. t.Errorf("%v: unexpected failure making payload: %v", tc.name, err)
  267. continue
  268. }
  269. if err == nil && !tc.success {
  270. t.Errorf("%v: unexpected success making payload", tc.name)
  271. continue
  272. }
  273. if !tc.success {
  274. continue
  275. }
  276. if e, a := tc.payload, actualPayload; !reflect.DeepEqual(e, a) {
  277. t.Errorf("%v: expected and actual payload do not match", tc.name)
  278. }
  279. }
  280. }
  281. func TestCollectDataWithConfigMap(t *testing.T) {
  282. caseMappingMode := int32(0400)
  283. cases := []struct {
  284. name string
  285. mappings []v1.KeyToPath
  286. configMap *v1.ConfigMap
  287. mode int32
  288. optional bool
  289. payload map[string]util.FileProjection
  290. success bool
  291. }{
  292. {
  293. name: "no overrides",
  294. configMap: &v1.ConfigMap{
  295. Data: map[string]string{
  296. "foo": "foo",
  297. "bar": "bar",
  298. },
  299. },
  300. mode: 0644,
  301. payload: map[string]util.FileProjection{
  302. "foo": {Data: []byte("foo"), Mode: 0644},
  303. "bar": {Data: []byte("bar"), Mode: 0644},
  304. },
  305. success: true,
  306. },
  307. {
  308. name: "basic 1",
  309. mappings: []v1.KeyToPath{
  310. {
  311. Key: "foo",
  312. Path: "path/to/foo.txt",
  313. },
  314. },
  315. configMap: &v1.ConfigMap{
  316. Data: map[string]string{
  317. "foo": "foo",
  318. "bar": "bar",
  319. },
  320. },
  321. mode: 0644,
  322. payload: map[string]util.FileProjection{
  323. "path/to/foo.txt": {Data: []byte("foo"), Mode: 0644},
  324. },
  325. success: true,
  326. },
  327. {
  328. name: "subdirs",
  329. mappings: []v1.KeyToPath{
  330. {
  331. Key: "foo",
  332. Path: "path/to/1/2/3/foo.txt",
  333. },
  334. },
  335. configMap: &v1.ConfigMap{
  336. Data: map[string]string{
  337. "foo": "foo",
  338. "bar": "bar",
  339. },
  340. },
  341. mode: 0644,
  342. payload: map[string]util.FileProjection{
  343. "path/to/1/2/3/foo.txt": {Data: []byte("foo"), Mode: 0644},
  344. },
  345. success: true,
  346. },
  347. {
  348. name: "subdirs 2",
  349. mappings: []v1.KeyToPath{
  350. {
  351. Key: "foo",
  352. Path: "path/to/1/2/3/foo.txt",
  353. },
  354. },
  355. configMap: &v1.ConfigMap{
  356. Data: map[string]string{
  357. "foo": "foo",
  358. "bar": "bar",
  359. },
  360. },
  361. mode: 0644,
  362. payload: map[string]util.FileProjection{
  363. "path/to/1/2/3/foo.txt": {Data: []byte("foo"), Mode: 0644},
  364. },
  365. success: true,
  366. },
  367. {
  368. name: "subdirs 3",
  369. mappings: []v1.KeyToPath{
  370. {
  371. Key: "foo",
  372. Path: "path/to/1/2/3/foo.txt",
  373. },
  374. {
  375. Key: "bar",
  376. Path: "another/path/to/the/esteemed/bar.bin",
  377. },
  378. },
  379. configMap: &v1.ConfigMap{
  380. Data: map[string]string{
  381. "foo": "foo",
  382. "bar": "bar",
  383. },
  384. },
  385. mode: 0644,
  386. payload: map[string]util.FileProjection{
  387. "path/to/1/2/3/foo.txt": {Data: []byte("foo"), Mode: 0644},
  388. "another/path/to/the/esteemed/bar.bin": {Data: []byte("bar"), Mode: 0644},
  389. },
  390. success: true,
  391. },
  392. {
  393. name: "non existent key",
  394. mappings: []v1.KeyToPath{
  395. {
  396. Key: "zab",
  397. Path: "path/to/foo.txt",
  398. },
  399. },
  400. configMap: &v1.ConfigMap{
  401. Data: map[string]string{
  402. "foo": "foo",
  403. "bar": "bar",
  404. },
  405. },
  406. mode: 0644,
  407. success: false,
  408. },
  409. {
  410. name: "mapping with Mode",
  411. mappings: []v1.KeyToPath{
  412. {
  413. Key: "foo",
  414. Path: "foo.txt",
  415. Mode: &caseMappingMode,
  416. },
  417. {
  418. Key: "bar",
  419. Path: "bar.bin",
  420. Mode: &caseMappingMode,
  421. },
  422. },
  423. configMap: &v1.ConfigMap{
  424. Data: map[string]string{
  425. "foo": "foo",
  426. "bar": "bar",
  427. },
  428. },
  429. mode: 0644,
  430. payload: map[string]util.FileProjection{
  431. "foo.txt": {Data: []byte("foo"), Mode: caseMappingMode},
  432. "bar.bin": {Data: []byte("bar"), Mode: caseMappingMode},
  433. },
  434. success: true,
  435. },
  436. {
  437. name: "mapping with defaultMode",
  438. mappings: []v1.KeyToPath{
  439. {
  440. Key: "foo",
  441. Path: "foo.txt",
  442. },
  443. {
  444. Key: "bar",
  445. Path: "bar.bin",
  446. },
  447. },
  448. configMap: &v1.ConfigMap{
  449. Data: map[string]string{
  450. "foo": "foo",
  451. "bar": "bar",
  452. },
  453. },
  454. mode: 0644,
  455. payload: map[string]util.FileProjection{
  456. "foo.txt": {Data: []byte("foo"), Mode: 0644},
  457. "bar.bin": {Data: []byte("bar"), Mode: 0644},
  458. },
  459. success: true,
  460. },
  461. {
  462. name: "optional non existent key",
  463. mappings: []v1.KeyToPath{
  464. {
  465. Key: "zab",
  466. Path: "path/to/foo.txt",
  467. },
  468. },
  469. configMap: &v1.ConfigMap{
  470. Data: map[string]string{
  471. "foo": "foo",
  472. "bar": "bar",
  473. },
  474. },
  475. mode: 0644,
  476. optional: true,
  477. payload: map[string]util.FileProjection{},
  478. success: true,
  479. },
  480. }
  481. for _, tc := range cases {
  482. testNamespace := "test_projected_namespace"
  483. tc.configMap.ObjectMeta = metav1.ObjectMeta{
  484. Namespace: testNamespace,
  485. Name: tc.name,
  486. }
  487. source := makeProjection(tc.name, tc.mode, "configMap")
  488. source.Sources[0].ConfigMap.Items = tc.mappings
  489. source.Sources[0].ConfigMap.Optional = &tc.optional
  490. testPodUID := types.UID("test_pod_uid")
  491. pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: testNamespace, UID: testPodUID}}
  492. client := fake.NewSimpleClientset(tc.configMap)
  493. _, host := newTestHost(t, client)
  494. var myVolumeMounter = projectedVolumeMounter{
  495. projectedVolume: &projectedVolume{
  496. sources: source.Sources,
  497. podUID: pod.UID,
  498. plugin: &projectedPlugin{
  499. host: host,
  500. getConfigMap: host.GetConfigMapFunc(),
  501. },
  502. },
  503. source: *source,
  504. pod: pod,
  505. }
  506. actualPayload, err := myVolumeMounter.collectData()
  507. if err != nil && tc.success {
  508. t.Errorf("%v: unexpected failure making payload: %v", tc.name, err)
  509. continue
  510. }
  511. if err == nil && !tc.success {
  512. t.Errorf("%v: unexpected success making payload", tc.name)
  513. continue
  514. }
  515. if !tc.success {
  516. continue
  517. }
  518. if e, a := tc.payload, actualPayload; !reflect.DeepEqual(e, a) {
  519. t.Errorf("%v: expected and actual payload do not match", tc.name)
  520. }
  521. }
  522. }
  523. func TestCollectDataWithDownwardAPI(t *testing.T) {
  524. testNamespace := "test_projected_namespace"
  525. testPodUID := types.UID("test_pod_uid")
  526. testPodName := "podName"
  527. cases := []struct {
  528. name string
  529. volumeFile []v1.DownwardAPIVolumeFile
  530. pod *v1.Pod
  531. mode int32
  532. payload map[string]util.FileProjection
  533. success bool
  534. }{
  535. {
  536. name: "annotation",
  537. volumeFile: []v1.DownwardAPIVolumeFile{
  538. {Path: "annotation", FieldRef: &v1.ObjectFieldSelector{
  539. FieldPath: "metadata.annotations['a1']"}}},
  540. pod: &v1.Pod{
  541. ObjectMeta: metav1.ObjectMeta{
  542. Name: testPodName,
  543. Namespace: testNamespace,
  544. Annotations: map[string]string{
  545. "a1": "value1",
  546. "a2": "value2",
  547. },
  548. UID: testPodUID},
  549. },
  550. mode: 0644,
  551. payload: map[string]util.FileProjection{
  552. "annotation": {Data: []byte("value1"), Mode: 0644},
  553. },
  554. success: true,
  555. },
  556. {
  557. name: "annotation-error",
  558. volumeFile: []v1.DownwardAPIVolumeFile{
  559. {Path: "annotation", FieldRef: &v1.ObjectFieldSelector{
  560. FieldPath: "metadata.annotations['']"}}},
  561. pod: &v1.Pod{
  562. ObjectMeta: metav1.ObjectMeta{
  563. Name: testPodName,
  564. Namespace: testNamespace,
  565. Annotations: map[string]string{
  566. "a1": "value1",
  567. "a2": "value2",
  568. },
  569. UID: testPodUID},
  570. },
  571. mode: 0644,
  572. payload: map[string]util.FileProjection{
  573. "annotation": {Data: []byte("does-not-matter-because-this-test-case-will-fail-anyway"), Mode: 0644},
  574. },
  575. success: false,
  576. },
  577. {
  578. name: "labels",
  579. volumeFile: []v1.DownwardAPIVolumeFile{
  580. {Path: "labels", FieldRef: &v1.ObjectFieldSelector{
  581. FieldPath: "metadata.labels"}}},
  582. pod: &v1.Pod{
  583. ObjectMeta: metav1.ObjectMeta{
  584. Name: testPodName,
  585. Namespace: testNamespace,
  586. Labels: map[string]string{
  587. "key1": "value1",
  588. "key2": "value2"},
  589. UID: testPodUID},
  590. },
  591. mode: 0644,
  592. payload: map[string]util.FileProjection{
  593. "labels": {Data: []byte("key1=\"value1\"\nkey2=\"value2\""), Mode: 0644},
  594. },
  595. success: true,
  596. },
  597. {
  598. name: "annotations",
  599. volumeFile: []v1.DownwardAPIVolumeFile{
  600. {Path: "annotations", FieldRef: &v1.ObjectFieldSelector{
  601. FieldPath: "metadata.annotations"}}},
  602. pod: &v1.Pod{
  603. ObjectMeta: metav1.ObjectMeta{
  604. Name: testPodName,
  605. Namespace: testNamespace,
  606. Annotations: map[string]string{
  607. "a1": "value1",
  608. "a2": "value2"},
  609. UID: testPodUID},
  610. },
  611. mode: 0644,
  612. payload: map[string]util.FileProjection{
  613. "annotations": {Data: []byte("a1=\"value1\"\na2=\"value2\""), Mode: 0644},
  614. },
  615. success: true,
  616. },
  617. {
  618. name: "name",
  619. volumeFile: []v1.DownwardAPIVolumeFile{
  620. {Path: "name_file_name", FieldRef: &v1.ObjectFieldSelector{
  621. FieldPath: "metadata.name"}}},
  622. pod: &v1.Pod{
  623. ObjectMeta: metav1.ObjectMeta{
  624. Name: testPodName,
  625. Namespace: testNamespace,
  626. UID: testPodUID},
  627. },
  628. mode: 0644,
  629. payload: map[string]util.FileProjection{
  630. "name_file_name": {Data: []byte(testPodName), Mode: 0644},
  631. },
  632. success: true,
  633. },
  634. {
  635. name: "namespace",
  636. volumeFile: []v1.DownwardAPIVolumeFile{
  637. {Path: "namespace_file_name", FieldRef: &v1.ObjectFieldSelector{
  638. FieldPath: "metadata.namespace"}}},
  639. pod: &v1.Pod{
  640. ObjectMeta: metav1.ObjectMeta{
  641. Name: testPodName,
  642. Namespace: testNamespace,
  643. UID: testPodUID},
  644. },
  645. mode: 0644,
  646. payload: map[string]util.FileProjection{
  647. "namespace_file_name": {Data: []byte(testNamespace), Mode: 0644},
  648. },
  649. success: true,
  650. },
  651. }
  652. for _, tc := range cases {
  653. source := makeProjection("", tc.mode, "downwardAPI")
  654. source.Sources[0].DownwardAPI.Items = tc.volumeFile
  655. client := fake.NewSimpleClientset(tc.pod)
  656. _, host := newTestHost(t, client)
  657. var myVolumeMounter = projectedVolumeMounter{
  658. projectedVolume: &projectedVolume{
  659. sources: source.Sources,
  660. podUID: tc.pod.UID,
  661. plugin: &projectedPlugin{
  662. host: host,
  663. },
  664. },
  665. source: *source,
  666. pod: tc.pod,
  667. }
  668. actualPayload, err := myVolumeMounter.collectData()
  669. if err != nil && tc.success {
  670. t.Errorf("%v: unexpected failure making payload: %v", tc.name, err)
  671. continue
  672. }
  673. if err == nil && !tc.success {
  674. t.Errorf("%v: unexpected success making payload", tc.name)
  675. continue
  676. }
  677. if !tc.success {
  678. continue
  679. }
  680. if e, a := tc.payload, actualPayload; !reflect.DeepEqual(e, a) {
  681. t.Errorf("%v: expected and actual payload do not match", tc.name)
  682. }
  683. }
  684. }
  685. func TestCollectDataWithServiceAccountToken(t *testing.T) {
  686. scheme := runtime.NewScheme()
  687. utilruntime.Must(pkgauthenticationv1.RegisterDefaults(scheme))
  688. utilruntime.Must(pkgcorev1.RegisterDefaults(scheme))
  689. minute := int64(60)
  690. cases := []struct {
  691. name string
  692. svcacct string
  693. audience string
  694. expiration *int64
  695. path string
  696. payload map[string]util.FileProjection
  697. }{
  698. {
  699. name: "test good service account",
  700. audience: "https://example.com",
  701. path: "token",
  702. expiration: &minute,
  703. payload: map[string]util.FileProjection{
  704. "token": {Data: []byte("test_projected_namespace:foo:60:[https://example.com]"), Mode: 0600},
  705. },
  706. },
  707. {
  708. name: "test good service account other path",
  709. audience: "https://example.com",
  710. path: "other-token",
  711. expiration: &minute,
  712. payload: map[string]util.FileProjection{
  713. "other-token": {Data: []byte("test_projected_namespace:foo:60:[https://example.com]"), Mode: 0600},
  714. },
  715. },
  716. {
  717. name: "test good service account defaults audience",
  718. path: "token",
  719. expiration: &minute,
  720. payload: map[string]util.FileProjection{
  721. "token": {Data: []byte("test_projected_namespace:foo:60:[https://api]"), Mode: 0600},
  722. },
  723. },
  724. {
  725. name: "test good service account defaults expiration",
  726. audience: "https://example.com",
  727. path: "token",
  728. payload: map[string]util.FileProjection{
  729. "token": {Data: []byte("test_projected_namespace:foo:3600:[https://example.com]"), Mode: 0600},
  730. },
  731. },
  732. }
  733. for _, tc := range cases {
  734. t.Run(tc.name, func(t *testing.T) {
  735. testNamespace := "test_projected_namespace"
  736. source := makeProjection(tc.name, 0600, "serviceAccountToken")
  737. source.Sources[0].ServiceAccountToken.Audience = tc.audience
  738. source.Sources[0].ServiceAccountToken.ExpirationSeconds = tc.expiration
  739. source.Sources[0].ServiceAccountToken.Path = tc.path
  740. testPodUID := types.UID("test_pod_uid")
  741. pod := &v1.Pod{
  742. ObjectMeta: metav1.ObjectMeta{Namespace: testNamespace, UID: testPodUID},
  743. Spec: v1.PodSpec{ServiceAccountName: "foo"},
  744. }
  745. scheme.Default(pod)
  746. client := &fake.Clientset{}
  747. client.AddReactor("create", "serviceaccounts", clitesting.ReactionFunc(func(action clitesting.Action) (bool, runtime.Object, error) {
  748. tr := action.(clitesting.CreateAction).GetObject().(*authenticationv1.TokenRequest)
  749. scheme.Default(tr)
  750. if len(tr.Spec.Audiences) == 0 {
  751. tr.Spec.Audiences = []string{"https://api"}
  752. }
  753. tr.Status.Token = fmt.Sprintf("%v:%v:%d:%v", action.GetNamespace(), "foo", *tr.Spec.ExpirationSeconds, tr.Spec.Audiences)
  754. return true, tr, nil
  755. }))
  756. _, host := newTestHost(t, client)
  757. var myVolumeMounter = projectedVolumeMounter{
  758. projectedVolume: &projectedVolume{
  759. sources: source.Sources,
  760. podUID: pod.UID,
  761. plugin: &projectedPlugin{
  762. host: host,
  763. getServiceAccountToken: host.GetServiceAccountTokenFunc(),
  764. },
  765. },
  766. source: *source,
  767. pod: pod,
  768. }
  769. actualPayload, err := myVolumeMounter.collectData()
  770. if err != nil {
  771. t.Fatalf("unexpected failure making payload: %v", err)
  772. }
  773. if e, a := tc.payload, actualPayload; !reflect.DeepEqual(e, a) {
  774. t.Errorf("expected and actual payload do not match:\n%s", diff.ObjectReflectDiff(e, a))
  775. }
  776. })
  777. }
  778. }
  779. func newTestHost(t *testing.T, clientset clientset.Interface) (string, volume.VolumeHost) {
  780. tempDir, err := ioutil.TempDir("/tmp", "projected_volume_test.")
  781. if err != nil {
  782. t.Fatalf("can't make a temp rootdir: %v", err)
  783. }
  784. return tempDir, volumetest.NewFakeVolumeHost(tempDir, clientset, emptydir.ProbeVolumePlugins())
  785. }
  786. func TestCanSupport(t *testing.T) {
  787. pluginMgr := volume.VolumePluginMgr{}
  788. tempDir, host := newTestHost(t, nil)
  789. defer os.RemoveAll(tempDir)
  790. pluginMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, host)
  791. plugin, err := pluginMgr.FindPluginByName(projectedPluginName)
  792. if err != nil {
  793. t.Errorf("Can't find the plugin by name")
  794. }
  795. if plugin.GetPluginName() != projectedPluginName {
  796. t.Errorf("Wrong name: %s", plugin.GetPluginName())
  797. }
  798. if !plugin.CanSupport(&volume.Spec{Volume: &v1.Volume{VolumeSource: v1.VolumeSource{Projected: &v1.ProjectedVolumeSource{}}}}) {
  799. t.Errorf("Expected true")
  800. }
  801. if plugin.CanSupport(&volume.Spec{}) {
  802. t.Errorf("Expected false")
  803. }
  804. }
  805. func TestPlugin(t *testing.T) {
  806. var (
  807. testPodUID = types.UID("test_pod_uid")
  808. testVolumeName = "test_volume_name"
  809. testNamespace = "test_projected_namespace"
  810. testName = "test_projected_name"
  811. volumeSpec = makeVolumeSpec(testVolumeName, testName, 0644)
  812. secret = makeSecret(testNamespace, testName)
  813. client = fake.NewSimpleClientset(&secret)
  814. pluginMgr = volume.VolumePluginMgr{}
  815. rootDir, host = newTestHost(t, client)
  816. )
  817. defer os.RemoveAll(rootDir)
  818. pluginMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, host)
  819. plugin, err := pluginMgr.FindPluginByName(projectedPluginName)
  820. if err != nil {
  821. t.Errorf("Can't find the plugin by name")
  822. }
  823. pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: testNamespace, UID: testPodUID}}
  824. mounter, err := plugin.NewMounter(volume.NewSpecFromVolume(volumeSpec), pod, volume.VolumeOptions{})
  825. if err != nil {
  826. t.Errorf("Failed to make a new Mounter: %v", err)
  827. }
  828. if mounter == nil {
  829. t.Errorf("Got a nil Mounter")
  830. }
  831. volumePath := mounter.GetPath()
  832. if !strings.HasSuffix(volumePath, fmt.Sprintf("pods/test_pod_uid/volumes/kubernetes.io~projected/%s", testVolumeName)) {
  833. t.Errorf("Got unexpected path: %s", volumePath)
  834. }
  835. err = mounter.SetUp(volume.MounterArgs{})
  836. if err != nil {
  837. t.Errorf("Failed to setup volume: %v", err)
  838. }
  839. if _, err := os.Stat(volumePath); err != nil {
  840. if os.IsNotExist(err) {
  841. t.Errorf("SetUp() failed, volume path not created: %s", volumePath)
  842. } else {
  843. t.Errorf("SetUp() failed: %v", err)
  844. }
  845. }
  846. // secret volume should create its own empty wrapper path
  847. podWrapperMetadataDir := fmt.Sprintf("%v/pods/test_pod_uid/plugins/kubernetes.io~empty-dir/wrapped_test_volume_name", rootDir)
  848. if _, err := os.Stat(podWrapperMetadataDir); err != nil {
  849. if os.IsNotExist(err) {
  850. t.Errorf("SetUp() failed, empty-dir wrapper path is not created: %s", podWrapperMetadataDir)
  851. } else {
  852. t.Errorf("SetUp() failed: %v", err)
  853. }
  854. }
  855. doTestSecretDataInVolume(volumePath, secret, t)
  856. defer doTestCleanAndTeardown(plugin, testPodUID, testVolumeName, volumePath, t)
  857. }
  858. func TestInvalidPathProjected(t *testing.T) {
  859. var (
  860. testPodUID = types.UID("test_pod_uid")
  861. testVolumeName = "test_volume_name"
  862. testNamespace = "test_projected_namespace"
  863. testName = "test_projected_name"
  864. volumeSpec = makeVolumeSpec(testVolumeName, testName, 0644)
  865. secret = makeSecret(testNamespace, testName)
  866. client = fake.NewSimpleClientset(&secret)
  867. pluginMgr = volume.VolumePluginMgr{}
  868. rootDir, host = newTestHost(t, client)
  869. )
  870. volumeSpec.Projected.Sources[0].Secret.Items = []v1.KeyToPath{
  871. {Key: "missing", Path: "missing"},
  872. }
  873. defer os.RemoveAll(rootDir)
  874. pluginMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, host)
  875. plugin, err := pluginMgr.FindPluginByName(projectedPluginName)
  876. if err != nil {
  877. t.Errorf("Can't find the plugin by name")
  878. }
  879. pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: testNamespace, UID: testPodUID}}
  880. mounter, err := plugin.NewMounter(volume.NewSpecFromVolume(volumeSpec), pod, volume.VolumeOptions{})
  881. if err != nil {
  882. t.Errorf("Failed to make a new Mounter: %v", err)
  883. }
  884. if mounter == nil {
  885. t.Errorf("Got a nil Mounter")
  886. }
  887. volumePath := mounter.GetPath()
  888. if !strings.HasSuffix(volumePath, fmt.Sprintf("pods/test_pod_uid/volumes/kubernetes.io~projected/%s", testVolumeName)) {
  889. t.Errorf("Got unexpected path: %s", volumePath)
  890. }
  891. var mounterArgs volume.MounterArgs
  892. err = mounter.SetUp(mounterArgs)
  893. if err == nil {
  894. t.Errorf("Expected error while setting up secret")
  895. }
  896. _, err = os.Stat(volumePath)
  897. if err == nil {
  898. t.Errorf("Expected path %s to not exist", volumePath)
  899. }
  900. }
  901. // Test the case where the plugin's ready file exists, but the volume dir is not a
  902. // mountpoint, which is the state the system will be in after reboot. The dir
  903. // should be mounter and the secret data written to it.
  904. func TestPluginReboot(t *testing.T) {
  905. var (
  906. testPodUID = types.UID("test_pod_uid3")
  907. testVolumeName = "test_volume_name"
  908. testNamespace = "test_secret_namespace"
  909. testName = "test_secret_name"
  910. volumeSpec = makeVolumeSpec(testVolumeName, testName, 0644)
  911. secret = makeSecret(testNamespace, testName)
  912. client = fake.NewSimpleClientset(&secret)
  913. pluginMgr = volume.VolumePluginMgr{}
  914. rootDir, host = newTestHost(t, client)
  915. )
  916. defer os.RemoveAll(rootDir)
  917. pluginMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, host)
  918. plugin, err := pluginMgr.FindPluginByName(projectedPluginName)
  919. if err != nil {
  920. t.Errorf("Can't find the plugin by name")
  921. }
  922. pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: testNamespace, UID: testPodUID}}
  923. mounter, err := plugin.NewMounter(volume.NewSpecFromVolume(volumeSpec), pod, volume.VolumeOptions{})
  924. if err != nil {
  925. t.Errorf("Failed to make a new Mounter: %v", err)
  926. }
  927. if mounter == nil {
  928. t.Errorf("Got a nil Mounter")
  929. }
  930. podMetadataDir := fmt.Sprintf("%v/pods/test_pod_uid3/plugins/kubernetes.io~projected/test_volume_name", rootDir)
  931. util.SetReady(podMetadataDir)
  932. volumePath := mounter.GetPath()
  933. if !strings.HasSuffix(volumePath, fmt.Sprintf("pods/test_pod_uid3/volumes/kubernetes.io~projected/test_volume_name")) {
  934. t.Errorf("Got unexpected path: %s", volumePath)
  935. }
  936. err = mounter.SetUp(volume.MounterArgs{})
  937. if err != nil {
  938. t.Errorf("Failed to setup volume: %v", err)
  939. }
  940. if _, err := os.Stat(volumePath); err != nil {
  941. if os.IsNotExist(err) {
  942. t.Errorf("SetUp() failed, volume path not created: %s", volumePath)
  943. } else {
  944. t.Errorf("SetUp() failed: %v", err)
  945. }
  946. }
  947. doTestSecretDataInVolume(volumePath, secret, t)
  948. doTestCleanAndTeardown(plugin, testPodUID, testVolumeName, volumePath, t)
  949. }
  950. func TestPluginOptional(t *testing.T) {
  951. var (
  952. testPodUID = types.UID("test_pod_uid")
  953. testVolumeName = "test_volume_name"
  954. testNamespace = "test_secret_namespace"
  955. testName = "test_secret_name"
  956. trueVal = true
  957. volumeSpec = makeVolumeSpec(testVolumeName, testName, 0644)
  958. client = fake.NewSimpleClientset()
  959. pluginMgr = volume.VolumePluginMgr{}
  960. rootDir, host = newTestHost(t, client)
  961. )
  962. volumeSpec.VolumeSource.Projected.Sources[0].Secret.Optional = &trueVal
  963. defer os.RemoveAll(rootDir)
  964. pluginMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, host)
  965. plugin, err := pluginMgr.FindPluginByName(projectedPluginName)
  966. if err != nil {
  967. t.Errorf("Can't find the plugin by name")
  968. }
  969. pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: testNamespace, UID: testPodUID}}
  970. mounter, err := plugin.NewMounter(volume.NewSpecFromVolume(volumeSpec), pod, volume.VolumeOptions{})
  971. if err != nil {
  972. t.Errorf("Failed to make a new Mounter: %v", err)
  973. }
  974. if mounter == nil {
  975. t.Errorf("Got a nil Mounter")
  976. }
  977. volumePath := mounter.GetPath()
  978. if !strings.HasSuffix(volumePath, fmt.Sprintf("pods/test_pod_uid/volumes/kubernetes.io~projected/test_volume_name")) {
  979. t.Errorf("Got unexpected path: %s", volumePath)
  980. }
  981. err = mounter.SetUp(volume.MounterArgs{})
  982. if err != nil {
  983. t.Errorf("Failed to setup volume: %v", err)
  984. }
  985. if _, err := os.Stat(volumePath); err != nil {
  986. if os.IsNotExist(err) {
  987. t.Errorf("SetUp() failed, volume path not created: %s", volumePath)
  988. } else {
  989. t.Errorf("SetUp() failed: %v", err)
  990. }
  991. }
  992. // secret volume should create its own empty wrapper path
  993. podWrapperMetadataDir := fmt.Sprintf("%v/pods/test_pod_uid/plugins/kubernetes.io~empty-dir/wrapped_test_volume_name", rootDir)
  994. if _, err := os.Stat(podWrapperMetadataDir); err != nil {
  995. if os.IsNotExist(err) {
  996. t.Errorf("SetUp() failed, empty-dir wrapper path is not created: %s", podWrapperMetadataDir)
  997. } else {
  998. t.Errorf("SetUp() failed: %v", err)
  999. }
  1000. }
  1001. datadirSymlink := filepath.Join(volumePath, "..data")
  1002. datadir, err := os.Readlink(datadirSymlink)
  1003. if err != nil && os.IsNotExist(err) {
  1004. t.Fatalf("couldn't find volume path's data dir, %s", datadirSymlink)
  1005. } else if err != nil {
  1006. t.Fatalf("couldn't read symlink, %s", datadirSymlink)
  1007. }
  1008. datadirPath := filepath.Join(volumePath, datadir)
  1009. infos, err := ioutil.ReadDir(volumePath)
  1010. if err != nil {
  1011. t.Fatalf("couldn't find volume path, %s", volumePath)
  1012. }
  1013. if len(infos) != 0 {
  1014. for _, fi := range infos {
  1015. if fi.Name() != "..data" && fi.Name() != datadir {
  1016. t.Errorf("empty data volume directory, %s, is not empty. Contains: %s", datadirSymlink, fi.Name())
  1017. }
  1018. }
  1019. }
  1020. infos, err = ioutil.ReadDir(datadirPath)
  1021. if err != nil {
  1022. t.Fatalf("couldn't find volume data path, %s", datadirPath)
  1023. }
  1024. if len(infos) != 0 {
  1025. t.Errorf("empty data directory, %s, is not empty. Contains: %s", datadirSymlink, infos[0].Name())
  1026. }
  1027. defer doTestCleanAndTeardown(plugin, testPodUID, testVolumeName, volumePath, t)
  1028. }
  1029. func TestPluginOptionalKeys(t *testing.T) {
  1030. var (
  1031. testPodUID = types.UID("test_pod_uid")
  1032. testVolumeName = "test_volume_name"
  1033. testNamespace = "test_secret_namespace"
  1034. testName = "test_secret_name"
  1035. trueVal = true
  1036. volumeSpec = makeVolumeSpec(testVolumeName, testName, 0644)
  1037. secret = makeSecret(testNamespace, testName)
  1038. client = fake.NewSimpleClientset(&secret)
  1039. pluginMgr = volume.VolumePluginMgr{}
  1040. rootDir, host = newTestHost(t, client)
  1041. )
  1042. volumeSpec.VolumeSource.Projected.Sources[0].Secret.Items = []v1.KeyToPath{
  1043. {Key: "data-1", Path: "data-1"},
  1044. {Key: "data-2", Path: "data-2"},
  1045. {Key: "data-3", Path: "data-3"},
  1046. {Key: "missing", Path: "missing"},
  1047. }
  1048. volumeSpec.VolumeSource.Projected.Sources[0].Secret.Optional = &trueVal
  1049. defer os.RemoveAll(rootDir)
  1050. pluginMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, host)
  1051. plugin, err := pluginMgr.FindPluginByName(projectedPluginName)
  1052. if err != nil {
  1053. t.Errorf("Can't find the plugin by name")
  1054. }
  1055. pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: testNamespace, UID: testPodUID}}
  1056. mounter, err := plugin.NewMounter(volume.NewSpecFromVolume(volumeSpec), pod, volume.VolumeOptions{})
  1057. if err != nil {
  1058. t.Errorf("Failed to make a new Mounter: %v", err)
  1059. }
  1060. if mounter == nil {
  1061. t.Errorf("Got a nil Mounter")
  1062. }
  1063. volumePath := mounter.GetPath()
  1064. if !strings.HasSuffix(volumePath, fmt.Sprintf("pods/test_pod_uid/volumes/kubernetes.io~projected/test_volume_name")) {
  1065. t.Errorf("Got unexpected path: %s", volumePath)
  1066. }
  1067. err = mounter.SetUp(volume.MounterArgs{})
  1068. if err != nil {
  1069. t.Errorf("Failed to setup volume: %v", err)
  1070. }
  1071. if _, err := os.Stat(volumePath); err != nil {
  1072. if os.IsNotExist(err) {
  1073. t.Errorf("SetUp() failed, volume path not created: %s", volumePath)
  1074. } else {
  1075. t.Errorf("SetUp() failed: %v", err)
  1076. }
  1077. }
  1078. // secret volume should create its own empty wrapper path
  1079. podWrapperMetadataDir := fmt.Sprintf("%v/pods/test_pod_uid/plugins/kubernetes.io~empty-dir/wrapped_test_volume_name", rootDir)
  1080. if _, err := os.Stat(podWrapperMetadataDir); err != nil {
  1081. if os.IsNotExist(err) {
  1082. t.Errorf("SetUp() failed, empty-dir wrapper path is not created: %s", podWrapperMetadataDir)
  1083. } else {
  1084. t.Errorf("SetUp() failed: %v", err)
  1085. }
  1086. }
  1087. doTestSecretDataInVolume(volumePath, secret, t)
  1088. defer doTestCleanAndTeardown(plugin, testPodUID, testVolumeName, volumePath, t)
  1089. }
  1090. func makeVolumeSpec(volumeName, name string, defaultMode int32) *v1.Volume {
  1091. return &v1.Volume{
  1092. Name: volumeName,
  1093. VolumeSource: v1.VolumeSource{
  1094. Projected: makeProjection(name, defaultMode, "secret"),
  1095. },
  1096. }
  1097. }
  1098. func makeSecret(namespace, name string) v1.Secret {
  1099. return v1.Secret{
  1100. ObjectMeta: metav1.ObjectMeta{
  1101. Namespace: namespace,
  1102. Name: name,
  1103. },
  1104. Data: map[string][]byte{
  1105. "data-1": []byte("value-1"),
  1106. "data-2": []byte("value-2"),
  1107. "data-3": []byte("value-3"),
  1108. },
  1109. }
  1110. }
  1111. func makeProjection(name string, defaultMode int32, kind string) *v1.ProjectedVolumeSource {
  1112. var item v1.VolumeProjection
  1113. switch kind {
  1114. case "configMap":
  1115. item = v1.VolumeProjection{
  1116. ConfigMap: &v1.ConfigMapProjection{
  1117. LocalObjectReference: v1.LocalObjectReference{Name: name},
  1118. },
  1119. }
  1120. case "secret":
  1121. item = v1.VolumeProjection{
  1122. Secret: &v1.SecretProjection{
  1123. LocalObjectReference: v1.LocalObjectReference{Name: name},
  1124. },
  1125. }
  1126. case "downwardAPI":
  1127. item = v1.VolumeProjection{
  1128. DownwardAPI: &v1.DownwardAPIProjection{},
  1129. }
  1130. case "serviceAccountToken":
  1131. item = v1.VolumeProjection{
  1132. ServiceAccountToken: &v1.ServiceAccountTokenProjection{},
  1133. }
  1134. }
  1135. return &v1.ProjectedVolumeSource{
  1136. Sources: []v1.VolumeProjection{item},
  1137. DefaultMode: &defaultMode,
  1138. }
  1139. }
  1140. func doTestSecretDataInVolume(volumePath string, secret v1.Secret, t *testing.T) {
  1141. for key, value := range secret.Data {
  1142. secretDataHostPath := filepath.Join(volumePath, key)
  1143. if _, err := os.Stat(secretDataHostPath); err != nil {
  1144. t.Fatalf("SetUp() failed, couldn't find secret data on disk: %v", secretDataHostPath)
  1145. } else {
  1146. actualSecretBytes, err := ioutil.ReadFile(secretDataHostPath)
  1147. if err != nil {
  1148. t.Fatalf("Couldn't read secret data from: %v", secretDataHostPath)
  1149. }
  1150. actualSecretValue := string(actualSecretBytes)
  1151. if string(value) != actualSecretValue {
  1152. t.Errorf("Unexpected value; expected %q, got %q", value, actualSecretValue)
  1153. }
  1154. }
  1155. }
  1156. }
  1157. func doTestCleanAndTeardown(plugin volume.VolumePlugin, podUID types.UID, testVolumeName, volumePath string, t *testing.T) {
  1158. unmounter, err := plugin.NewUnmounter(testVolumeName, podUID)
  1159. if err != nil {
  1160. t.Errorf("Failed to make a new Unmounter: %v", err)
  1161. }
  1162. if unmounter == nil {
  1163. t.Errorf("Got a nil Unmounter")
  1164. }
  1165. if err := unmounter.TearDown(); err != nil {
  1166. t.Errorf("Expected success, got: %v", err)
  1167. }
  1168. if _, err := os.Stat(volumePath); err == nil {
  1169. t.Errorf("TearDown() failed, volume path still exists: %s", volumePath)
  1170. } else if !os.IsNotExist(err) {
  1171. t.Errorf("TearDown() failed: %v", err)
  1172. }
  1173. }