configmap_test.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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 configmap
  14. import (
  15. "fmt"
  16. "io/ioutil"
  17. "os"
  18. "path/filepath"
  19. "reflect"
  20. "strings"
  21. "testing"
  22. "k8s.io/api/core/v1"
  23. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  24. "k8s.io/apimachinery/pkg/types"
  25. clientset "k8s.io/client-go/kubernetes"
  26. "k8s.io/client-go/kubernetes/fake"
  27. "k8s.io/kubernetes/pkg/volume"
  28. "k8s.io/kubernetes/pkg/volume/emptydir"
  29. volumetest "k8s.io/kubernetes/pkg/volume/testing"
  30. "k8s.io/kubernetes/pkg/volume/util"
  31. )
  32. func TestMakePayload(t *testing.T) {
  33. caseMappingMode := int32(0400)
  34. cases := []struct {
  35. name string
  36. mappings []v1.KeyToPath
  37. configMap *v1.ConfigMap
  38. mode int32
  39. optional bool
  40. payload map[string]util.FileProjection
  41. success bool
  42. }{
  43. {
  44. name: "no overrides",
  45. configMap: &v1.ConfigMap{
  46. Data: map[string]string{
  47. "foo": "foo",
  48. "bar": "bar",
  49. },
  50. },
  51. mode: 0644,
  52. payload: map[string]util.FileProjection{
  53. "foo": {Data: []byte("foo"), Mode: 0644},
  54. "bar": {Data: []byte("bar"), Mode: 0644},
  55. },
  56. success: true,
  57. },
  58. {
  59. name: "no overrides binary data",
  60. configMap: &v1.ConfigMap{
  61. BinaryData: map[string][]byte{
  62. "foo": []byte("foo"),
  63. "bar": []byte("bar"),
  64. },
  65. },
  66. mode: 0644,
  67. payload: map[string]util.FileProjection{
  68. "foo": {Data: []byte("foo"), Mode: 0644},
  69. "bar": {Data: []byte("bar"), Mode: 0644},
  70. },
  71. success: true,
  72. },
  73. {
  74. name: "no overrides mixed data",
  75. configMap: &v1.ConfigMap{
  76. BinaryData: map[string][]byte{
  77. "foo": []byte("foo"),
  78. },
  79. Data: map[string]string{
  80. "bar": "bar",
  81. },
  82. },
  83. mode: 0644,
  84. payload: map[string]util.FileProjection{
  85. "foo": {Data: []byte("foo"), Mode: 0644},
  86. "bar": {Data: []byte("bar"), Mode: 0644},
  87. },
  88. success: true,
  89. },
  90. {
  91. name: "basic 1",
  92. mappings: []v1.KeyToPath{
  93. {
  94. Key: "foo",
  95. Path: "path/to/foo.txt",
  96. },
  97. },
  98. configMap: &v1.ConfigMap{
  99. Data: map[string]string{
  100. "foo": "foo",
  101. "bar": "bar",
  102. },
  103. },
  104. mode: 0644,
  105. payload: map[string]util.FileProjection{
  106. "path/to/foo.txt": {Data: []byte("foo"), Mode: 0644},
  107. },
  108. success: true,
  109. },
  110. {
  111. name: "subdirs",
  112. mappings: []v1.KeyToPath{
  113. {
  114. Key: "foo",
  115. Path: "path/to/1/2/3/foo.txt",
  116. },
  117. },
  118. configMap: &v1.ConfigMap{
  119. Data: map[string]string{
  120. "foo": "foo",
  121. "bar": "bar",
  122. },
  123. },
  124. mode: 0644,
  125. payload: map[string]util.FileProjection{
  126. "path/to/1/2/3/foo.txt": {Data: []byte("foo"), Mode: 0644},
  127. },
  128. success: true,
  129. },
  130. {
  131. name: "subdirs 2",
  132. mappings: []v1.KeyToPath{
  133. {
  134. Key: "foo",
  135. Path: "path/to/1/2/3/foo.txt",
  136. },
  137. },
  138. configMap: &v1.ConfigMap{
  139. Data: map[string]string{
  140. "foo": "foo",
  141. "bar": "bar",
  142. },
  143. },
  144. mode: 0644,
  145. payload: map[string]util.FileProjection{
  146. "path/to/1/2/3/foo.txt": {Data: []byte("foo"), Mode: 0644},
  147. },
  148. success: true,
  149. },
  150. {
  151. name: "subdirs 3",
  152. mappings: []v1.KeyToPath{
  153. {
  154. Key: "foo",
  155. Path: "path/to/1/2/3/foo.txt",
  156. },
  157. {
  158. Key: "bar",
  159. Path: "another/path/to/the/esteemed/bar.bin",
  160. },
  161. },
  162. configMap: &v1.ConfigMap{
  163. Data: map[string]string{
  164. "foo": "foo",
  165. "bar": "bar",
  166. },
  167. },
  168. mode: 0644,
  169. payload: map[string]util.FileProjection{
  170. "path/to/1/2/3/foo.txt": {Data: []byte("foo"), Mode: 0644},
  171. "another/path/to/the/esteemed/bar.bin": {Data: []byte("bar"), Mode: 0644},
  172. },
  173. success: true,
  174. },
  175. {
  176. name: "non existent key",
  177. mappings: []v1.KeyToPath{
  178. {
  179. Key: "zab",
  180. Path: "path/to/foo.txt",
  181. },
  182. },
  183. configMap: &v1.ConfigMap{
  184. Data: map[string]string{
  185. "foo": "foo",
  186. "bar": "bar",
  187. },
  188. },
  189. mode: 0644,
  190. success: false,
  191. },
  192. {
  193. name: "mapping with Mode",
  194. mappings: []v1.KeyToPath{
  195. {
  196. Key: "foo",
  197. Path: "foo.txt",
  198. Mode: &caseMappingMode,
  199. },
  200. {
  201. Key: "bar",
  202. Path: "bar.bin",
  203. Mode: &caseMappingMode,
  204. },
  205. },
  206. configMap: &v1.ConfigMap{
  207. Data: map[string]string{
  208. "foo": "foo",
  209. "bar": "bar",
  210. },
  211. },
  212. mode: 0644,
  213. payload: map[string]util.FileProjection{
  214. "foo.txt": {Data: []byte("foo"), Mode: caseMappingMode},
  215. "bar.bin": {Data: []byte("bar"), Mode: caseMappingMode},
  216. },
  217. success: true,
  218. },
  219. {
  220. name: "mapping with defaultMode",
  221. mappings: []v1.KeyToPath{
  222. {
  223. Key: "foo",
  224. Path: "foo.txt",
  225. },
  226. {
  227. Key: "bar",
  228. Path: "bar.bin",
  229. },
  230. },
  231. configMap: &v1.ConfigMap{
  232. Data: map[string]string{
  233. "foo": "foo",
  234. "bar": "bar",
  235. },
  236. },
  237. mode: 0644,
  238. payload: map[string]util.FileProjection{
  239. "foo.txt": {Data: []byte("foo"), Mode: 0644},
  240. "bar.bin": {Data: []byte("bar"), Mode: 0644},
  241. },
  242. success: true,
  243. },
  244. {
  245. name: "optional non existent key",
  246. mappings: []v1.KeyToPath{
  247. {
  248. Key: "zab",
  249. Path: "path/to/foo.txt",
  250. },
  251. },
  252. configMap: &v1.ConfigMap{
  253. Data: map[string]string{
  254. "foo": "foo",
  255. "bar": "bar",
  256. },
  257. },
  258. mode: 0644,
  259. optional: true,
  260. payload: map[string]util.FileProjection{},
  261. success: true,
  262. },
  263. }
  264. for _, tc := range cases {
  265. actualPayload, err := MakePayload(tc.mappings, tc.configMap, &tc.mode, tc.optional)
  266. if err != nil && tc.success {
  267. t.Errorf("%v: unexpected failure making payload: %v", tc.name, err)
  268. continue
  269. }
  270. if err == nil && !tc.success {
  271. t.Errorf("%v: unexpected success making payload", tc.name)
  272. continue
  273. }
  274. if !tc.success {
  275. continue
  276. }
  277. if e, a := tc.payload, actualPayload; !reflect.DeepEqual(e, a) {
  278. t.Errorf("%v: expected and actual payload do not match", tc.name)
  279. }
  280. }
  281. }
  282. func newTestHost(t *testing.T, clientset clientset.Interface) (string, volume.VolumeHost) {
  283. tempDir, err := ioutil.TempDir("/tmp", "configmap_volume_test.")
  284. if err != nil {
  285. t.Fatalf("can't make a temp rootdir: %v", err)
  286. }
  287. return tempDir, volumetest.NewFakeVolumeHost(tempDir, clientset, emptydir.ProbeVolumePlugins())
  288. }
  289. func TestCanSupport(t *testing.T) {
  290. pluginMgr := volume.VolumePluginMgr{}
  291. tempDir, host := newTestHost(t, nil)
  292. defer os.RemoveAll(tempDir)
  293. pluginMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, host)
  294. plugin, err := pluginMgr.FindPluginByName(configMapPluginName)
  295. if err != nil {
  296. t.Errorf("Can't find the plugin by name")
  297. }
  298. if plugin.GetPluginName() != configMapPluginName {
  299. t.Errorf("Wrong name: %s", plugin.GetPluginName())
  300. }
  301. if !plugin.CanSupport(&volume.Spec{Volume: &v1.Volume{VolumeSource: v1.VolumeSource{ConfigMap: &v1.ConfigMapVolumeSource{LocalObjectReference: v1.LocalObjectReference{Name: ""}}}}}) {
  302. t.Errorf("Expected true")
  303. }
  304. if plugin.CanSupport(&volume.Spec{}) {
  305. t.Errorf("Expected false")
  306. }
  307. }
  308. func TestPlugin(t *testing.T) {
  309. var (
  310. testPodUID = types.UID("test_pod_uid")
  311. testVolumeName = "test_volume_name"
  312. testNamespace = "test_configmap_namespace"
  313. testName = "test_configmap_name"
  314. volumeSpec = volumeSpec(testVolumeName, testName, 0644)
  315. configMap = configMap(testNamespace, testName)
  316. client = fake.NewSimpleClientset(&configMap)
  317. pluginMgr = volume.VolumePluginMgr{}
  318. tempDir, host = newTestHost(t, client)
  319. )
  320. defer os.RemoveAll(tempDir)
  321. pluginMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, host)
  322. plugin, err := pluginMgr.FindPluginByName(configMapPluginName)
  323. if err != nil {
  324. t.Errorf("Can't find the plugin by name")
  325. }
  326. pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: testNamespace, UID: testPodUID}}
  327. mounter, err := plugin.NewMounter(volume.NewSpecFromVolume(volumeSpec), pod, volume.VolumeOptions{})
  328. if err != nil {
  329. t.Errorf("Failed to make a new Mounter: %v", err)
  330. }
  331. if mounter == nil {
  332. t.Fatalf("Got a nil Mounter")
  333. }
  334. vName, err := plugin.GetVolumeName(volume.NewSpecFromVolume(volumeSpec))
  335. if err != nil {
  336. t.Errorf("Failed to GetVolumeName: %v", err)
  337. }
  338. if vName != "test_volume_name/test_configmap_name" {
  339. t.Errorf("Got unexpect VolumeName %v", vName)
  340. }
  341. volumePath := mounter.GetPath()
  342. if !strings.HasSuffix(volumePath, fmt.Sprintf("pods/test_pod_uid/volumes/kubernetes.io~configmap/test_volume_name")) {
  343. t.Errorf("Got unexpected path: %s", volumePath)
  344. }
  345. var mounterArgs volume.MounterArgs
  346. group := int64(1001)
  347. mounterArgs.FsGroup = &group
  348. err = mounter.SetUp(mounterArgs)
  349. if err != nil {
  350. t.Errorf("Failed to setup volume: %v", err)
  351. }
  352. if _, err := os.Stat(volumePath); err != nil {
  353. if os.IsNotExist(err) {
  354. t.Errorf("SetUp() failed, volume path not created: %s", volumePath)
  355. } else {
  356. t.Errorf("SetUp() failed: %v", err)
  357. }
  358. }
  359. doTestConfigMapDataInVolume(volumePath, configMap, t)
  360. doTestCleanAndTeardown(plugin, testPodUID, testVolumeName, volumePath, t)
  361. }
  362. // Test the case where the plugin's ready file exists, but the volume dir is not a
  363. // mountpoint, which is the state the system will be in after reboot. The dir
  364. // should be mounter and the configMap data written to it.
  365. func TestPluginReboot(t *testing.T) {
  366. var (
  367. testPodUID = types.UID("test_pod_uid3")
  368. testVolumeName = "test_volume_name"
  369. testNamespace = "test_configmap_namespace"
  370. testName = "test_configmap_name"
  371. volumeSpec = volumeSpec(testVolumeName, testName, 0644)
  372. configMap = configMap(testNamespace, testName)
  373. client = fake.NewSimpleClientset(&configMap)
  374. pluginMgr = volume.VolumePluginMgr{}
  375. rootDir, host = newTestHost(t, client)
  376. )
  377. defer os.RemoveAll(rootDir)
  378. pluginMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, host)
  379. plugin, err := pluginMgr.FindPluginByName(configMapPluginName)
  380. if err != nil {
  381. t.Errorf("Can't find the plugin by name")
  382. }
  383. pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: testNamespace, UID: testPodUID}}
  384. mounter, err := plugin.NewMounter(volume.NewSpecFromVolume(volumeSpec), pod, volume.VolumeOptions{})
  385. if err != nil {
  386. t.Errorf("Failed to make a new Mounter: %v", err)
  387. }
  388. if mounter == nil {
  389. t.Fatalf("Got a nil Mounter")
  390. }
  391. podMetadataDir := fmt.Sprintf("%v/pods/test_pod_uid3/plugins/kubernetes.io~configmap/test_volume_name", rootDir)
  392. util.SetReady(podMetadataDir)
  393. volumePath := mounter.GetPath()
  394. if !strings.HasSuffix(volumePath, fmt.Sprintf("pods/test_pod_uid3/volumes/kubernetes.io~configmap/test_volume_name")) {
  395. t.Errorf("Got unexpected path: %s", volumePath)
  396. }
  397. var mounterArgs volume.MounterArgs
  398. group := int64(1001)
  399. mounterArgs.FsGroup = &group
  400. err = mounter.SetUp(mounterArgs)
  401. if err != nil {
  402. t.Errorf("Failed to setup volume: %v", err)
  403. }
  404. if _, err := os.Stat(volumePath); err != nil {
  405. if os.IsNotExist(err) {
  406. t.Errorf("SetUp() failed, volume path not created: %s", volumePath)
  407. } else {
  408. t.Errorf("SetUp() failed: %v", err)
  409. }
  410. }
  411. doTestConfigMapDataInVolume(volumePath, configMap, t)
  412. doTestCleanAndTeardown(plugin, testPodUID, testVolumeName, volumePath, t)
  413. }
  414. func TestPluginOptional(t *testing.T) {
  415. var (
  416. testPodUID = types.UID("test_pod_uid")
  417. testVolumeName = "test_volume_name"
  418. testNamespace = "test_configmap_namespace"
  419. testName = "test_configmap_name"
  420. trueVal = true
  421. volumeSpec = volumeSpec(testVolumeName, testName, 0644)
  422. client = fake.NewSimpleClientset()
  423. pluginMgr = volume.VolumePluginMgr{}
  424. tempDir, host = newTestHost(t, client)
  425. )
  426. volumeSpec.VolumeSource.ConfigMap.Optional = &trueVal
  427. defer os.RemoveAll(tempDir)
  428. pluginMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, host)
  429. plugin, err := pluginMgr.FindPluginByName(configMapPluginName)
  430. if err != nil {
  431. t.Errorf("Can't find the plugin by name")
  432. }
  433. pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: testNamespace, UID: testPodUID}}
  434. mounter, err := plugin.NewMounter(volume.NewSpecFromVolume(volumeSpec), pod, volume.VolumeOptions{})
  435. if err != nil {
  436. t.Errorf("Failed to make a new Mounter: %v", err)
  437. }
  438. if mounter == nil {
  439. t.Errorf("Got a nil Mounter")
  440. }
  441. vName, err := plugin.GetVolumeName(volume.NewSpecFromVolume(volumeSpec))
  442. if err != nil {
  443. t.Errorf("Failed to GetVolumeName: %v", err)
  444. }
  445. if vName != "test_volume_name/test_configmap_name" {
  446. t.Errorf("Got unexpect VolumeName %v", vName)
  447. }
  448. volumePath := mounter.GetPath()
  449. if !strings.HasSuffix(volumePath, fmt.Sprintf("pods/test_pod_uid/volumes/kubernetes.io~configmap/test_volume_name")) {
  450. t.Errorf("Got unexpected path: %s", volumePath)
  451. }
  452. var mounterArgs volume.MounterArgs
  453. group := int64(1001)
  454. mounterArgs.FsGroup = &group
  455. err = mounter.SetUp(mounterArgs)
  456. if err != nil {
  457. t.Errorf("Failed to setup volume: %v", err)
  458. }
  459. if _, err := os.Stat(volumePath); err != nil {
  460. if os.IsNotExist(err) {
  461. t.Errorf("SetUp() failed, volume path not created: %s", volumePath)
  462. } else {
  463. t.Errorf("SetUp() failed: %v", err)
  464. }
  465. }
  466. datadirSymlink := filepath.Join(volumePath, "..data")
  467. datadir, err := os.Readlink(datadirSymlink)
  468. if err != nil && os.IsNotExist(err) {
  469. t.Fatalf("couldn't find volume path's data dir, %s", datadirSymlink)
  470. } else if err != nil {
  471. t.Fatalf("couldn't read symlink, %s", datadirSymlink)
  472. }
  473. datadirPath := filepath.Join(volumePath, datadir)
  474. infos, err := ioutil.ReadDir(volumePath)
  475. if err != nil {
  476. t.Fatalf("couldn't find volume path, %s", volumePath)
  477. }
  478. if len(infos) != 0 {
  479. for _, fi := range infos {
  480. if fi.Name() != "..data" && fi.Name() != datadir {
  481. t.Errorf("empty data directory, %s, is not empty. Contains: %s", datadirSymlink, fi.Name())
  482. }
  483. }
  484. }
  485. infos, err = ioutil.ReadDir(datadirPath)
  486. if err != nil {
  487. t.Fatalf("couldn't find volume data path, %s", datadirPath)
  488. }
  489. if len(infos) != 0 {
  490. t.Errorf("empty data directory, %s, is not empty. Contains: %s", datadirSymlink, infos[0].Name())
  491. }
  492. doTestCleanAndTeardown(plugin, testPodUID, testVolumeName, volumePath, t)
  493. }
  494. func TestPluginKeysOptional(t *testing.T) {
  495. var (
  496. testPodUID = types.UID("test_pod_uid")
  497. testVolumeName = "test_volume_name"
  498. testNamespace = "test_configmap_namespace"
  499. testName = "test_configmap_name"
  500. trueVal = true
  501. volumeSpec = volumeSpec(testVolumeName, testName, 0644)
  502. configMap = configMap(testNamespace, testName)
  503. client = fake.NewSimpleClientset(&configMap)
  504. pluginMgr = volume.VolumePluginMgr{}
  505. tempDir, host = newTestHost(t, client)
  506. )
  507. volumeSpec.VolumeSource.ConfigMap.Items = []v1.KeyToPath{
  508. {Key: "data-1", Path: "data-1"},
  509. {Key: "data-2", Path: "data-2"},
  510. {Key: "data-3", Path: "data-3"},
  511. {Key: "missing", Path: "missing"},
  512. }
  513. volumeSpec.VolumeSource.ConfigMap.Optional = &trueVal
  514. defer os.RemoveAll(tempDir)
  515. pluginMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, host)
  516. plugin, err := pluginMgr.FindPluginByName(configMapPluginName)
  517. if err != nil {
  518. t.Errorf("Can't find the plugin by name")
  519. }
  520. pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: testNamespace, UID: testPodUID}}
  521. mounter, err := plugin.NewMounter(volume.NewSpecFromVolume(volumeSpec), pod, volume.VolumeOptions{})
  522. if err != nil {
  523. t.Errorf("Failed to make a new Mounter: %v", err)
  524. }
  525. if mounter == nil {
  526. t.Errorf("Got a nil Mounter")
  527. }
  528. vName, err := plugin.GetVolumeName(volume.NewSpecFromVolume(volumeSpec))
  529. if err != nil {
  530. t.Errorf("Failed to GetVolumeName: %v", err)
  531. }
  532. if vName != "test_volume_name/test_configmap_name" {
  533. t.Errorf("Got unexpect VolumeName %v", vName)
  534. }
  535. volumePath := mounter.GetPath()
  536. if !strings.HasSuffix(volumePath, fmt.Sprintf("pods/test_pod_uid/volumes/kubernetes.io~configmap/test_volume_name")) {
  537. t.Errorf("Got unexpected path: %s", volumePath)
  538. }
  539. var mounterArgs volume.MounterArgs
  540. group := int64(1001)
  541. mounterArgs.FsGroup = &group
  542. err = mounter.SetUp(mounterArgs)
  543. if err != nil {
  544. t.Errorf("Failed to setup volume: %v", err)
  545. }
  546. if _, err := os.Stat(volumePath); err != nil {
  547. if os.IsNotExist(err) {
  548. t.Errorf("SetUp() failed, volume path not created: %s", volumePath)
  549. } else {
  550. t.Errorf("SetUp() failed: %v", err)
  551. }
  552. }
  553. doTestConfigMapDataInVolume(volumePath, configMap, t)
  554. doTestCleanAndTeardown(plugin, testPodUID, testVolumeName, volumePath, t)
  555. }
  556. func volumeSpec(volumeName, configMapName string, defaultMode int32) *v1.Volume {
  557. return &v1.Volume{
  558. Name: volumeName,
  559. VolumeSource: v1.VolumeSource{
  560. ConfigMap: &v1.ConfigMapVolumeSource{
  561. LocalObjectReference: v1.LocalObjectReference{
  562. Name: configMapName,
  563. },
  564. DefaultMode: &defaultMode,
  565. },
  566. },
  567. }
  568. }
  569. func TestInvalidConfigMapSetup(t *testing.T) {
  570. var (
  571. testPodUID = types.UID("test_pod_uid")
  572. testVolumeName = "test_volume_name"
  573. testNamespace = "test_configmap_namespace"
  574. testName = "test_configmap_name"
  575. volumeSpec = volumeSpec(testVolumeName, testName, 0644)
  576. configMap = configMap(testNamespace, testName)
  577. client = fake.NewSimpleClientset(&configMap)
  578. pluginMgr = volume.VolumePluginMgr{}
  579. tempDir, host = newTestHost(t, client)
  580. )
  581. volumeSpec.VolumeSource.ConfigMap.Items = []v1.KeyToPath{
  582. {Key: "missing", Path: "missing"},
  583. }
  584. defer os.RemoveAll(tempDir)
  585. pluginMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, host)
  586. plugin, err := pluginMgr.FindPluginByName(configMapPluginName)
  587. if err != nil {
  588. t.Errorf("Can't find the plugin by name")
  589. }
  590. pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: testNamespace, UID: testPodUID}}
  591. mounter, err := plugin.NewMounter(volume.NewSpecFromVolume(volumeSpec), pod, volume.VolumeOptions{})
  592. if err != nil {
  593. t.Errorf("Failed to make a new Mounter: %v", err)
  594. }
  595. if mounter == nil {
  596. t.Errorf("Got a nil Mounter")
  597. }
  598. vName, err := plugin.GetVolumeName(volume.NewSpecFromVolume(volumeSpec))
  599. if err != nil {
  600. t.Errorf("Failed to GetVolumeName: %v", err)
  601. }
  602. if vName != "test_volume_name/test_configmap_name" {
  603. t.Errorf("Got unexpect VolumeName %v", vName)
  604. }
  605. volumePath := mounter.GetPath()
  606. if !strings.HasSuffix(volumePath, fmt.Sprintf("pods/test_pod_uid/volumes/kubernetes.io~configmap/test_volume_name")) {
  607. t.Errorf("Got unexpected path: %s", volumePath)
  608. }
  609. var mounterArgs volume.MounterArgs
  610. group := int64(1001)
  611. mounterArgs.FsGroup = &group
  612. err = mounter.SetUp(mounterArgs)
  613. if err == nil {
  614. t.Errorf("Expected setup to fail")
  615. }
  616. _, err = os.Stat(volumePath)
  617. if err == nil {
  618. t.Errorf("Expected %s to not exist", volumePath)
  619. }
  620. doTestCleanAndTeardown(plugin, testPodUID, testVolumeName, volumePath, t)
  621. }
  622. func configMap(namespace, name string) v1.ConfigMap {
  623. return v1.ConfigMap{
  624. ObjectMeta: metav1.ObjectMeta{
  625. Namespace: namespace,
  626. Name: name,
  627. },
  628. Data: map[string]string{
  629. "data-1": "value-1",
  630. "data-2": "value-2",
  631. "data-3": "value-3",
  632. },
  633. }
  634. }
  635. func doTestConfigMapDataInVolume(volumePath string, configMap v1.ConfigMap, t *testing.T) {
  636. for key, value := range configMap.Data {
  637. configMapDataHostPath := filepath.Join(volumePath, key)
  638. if _, err := os.Stat(configMapDataHostPath); err != nil {
  639. t.Fatalf("SetUp() failed, couldn't find configMap data on disk: %v", configMapDataHostPath)
  640. } else {
  641. actualValue, err := ioutil.ReadFile(configMapDataHostPath)
  642. if err != nil {
  643. t.Fatalf("Couldn't read configMap data from: %v", configMapDataHostPath)
  644. }
  645. if value != string(actualValue) {
  646. t.Errorf("Unexpected value; expected %q, got %q", value, actualValue)
  647. }
  648. }
  649. }
  650. }
  651. func doTestCleanAndTeardown(plugin volume.VolumePlugin, podUID types.UID, testVolumeName, volumePath string, t *testing.T) {
  652. unmounter, err := plugin.NewUnmounter(testVolumeName, podUID)
  653. if err != nil {
  654. t.Errorf("Failed to make a new Unmounter: %v", err)
  655. }
  656. if unmounter == nil {
  657. t.Fatalf("Got a nil Unmounter")
  658. }
  659. if err := unmounter.TearDown(); err != nil {
  660. t.Errorf("Expected success, got: %v", err)
  661. }
  662. if _, err := os.Stat(volumePath); err == nil {
  663. t.Errorf("TearDown() failed, volume path still exists: %s", volumePath)
  664. } else if !os.IsNotExist(err) {
  665. t.Errorf("TearDown() failed: %v", err)
  666. }
  667. }