projected_test.go 34 KB

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