mount_pod_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. Copyright 2017 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 mountpod
  14. import (
  15. "io/ioutil"
  16. "os"
  17. "path"
  18. "testing"
  19. "k8s.io/klog"
  20. "k8s.io/api/core/v1"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. utiltesting "k8s.io/client-go/util/testing"
  23. "k8s.io/kubernetes/pkg/kubelet/configmap"
  24. kubepod "k8s.io/kubernetes/pkg/kubelet/pod"
  25. podtest "k8s.io/kubernetes/pkg/kubelet/pod/testing"
  26. "k8s.io/kubernetes/pkg/kubelet/secret"
  27. )
  28. func TestGetVolumeExec(t *testing.T) {
  29. // prepare PodManager
  30. pods := []*v1.Pod{
  31. {
  32. ObjectMeta: metav1.ObjectMeta{
  33. UID: "12345678",
  34. Name: "foo",
  35. Namespace: "bar",
  36. },
  37. Spec: v1.PodSpec{
  38. Containers: []v1.Container{
  39. {Name: "baz"},
  40. },
  41. },
  42. },
  43. }
  44. fakeSecretManager := secret.NewFakeManager()
  45. fakeConfigMapManager := configmap.NewFakeManager()
  46. podManager := kubepod.NewBasicPodManager(
  47. podtest.NewFakeMirrorClient(), fakeSecretManager, fakeConfigMapManager, podtest.NewMockCheckpointManager())
  48. podManager.SetPods(pods)
  49. // Prepare fake /var/lib/kubelet
  50. basePath, err := utiltesting.MkTmpdir("kubelet")
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. defer os.RemoveAll(basePath)
  55. regPath := path.Join(basePath, "plugin-containers")
  56. mgr, err := NewManager(basePath, podManager)
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. tests := []struct {
  61. name string
  62. json string
  63. expectError bool
  64. }{
  65. {
  66. "invalid json",
  67. "{{{}",
  68. true,
  69. },
  70. {
  71. "missing json",
  72. "", // this means no json file should be created
  73. false,
  74. },
  75. {
  76. "missing podNamespace",
  77. `{"podName": "foo", "podUID": "87654321", "containerName": "baz"}`,
  78. true,
  79. },
  80. {
  81. "missing podName",
  82. `{"podNamespace": "bar", "podUID": "87654321", "containerName": "baz"}`,
  83. true,
  84. },
  85. {
  86. "missing containerName",
  87. `{"podNamespace": "bar", "podName": "foo", "podUID": "87654321"}`,
  88. true,
  89. },
  90. {
  91. "missing podUID",
  92. `{"podNamespace": "bar", "podName": "foo", "containerName": "baz"}`,
  93. true,
  94. },
  95. {
  96. "missing pod",
  97. `{"podNamespace": "bar", "podName": "non-existing-pod", "podUID": "12345678", "containerName": "baz"}`,
  98. true,
  99. },
  100. {
  101. "invalid uid",
  102. `{"podNamespace": "bar", "podName": "foo", "podUID": "87654321", "containerName": "baz"}`,
  103. true,
  104. },
  105. {
  106. "invalid container",
  107. `{"podNamespace": "bar", "podName": "foo", "podUID": "12345678", "containerName": "invalid"}`,
  108. true,
  109. },
  110. {
  111. "valid pod",
  112. `{"podNamespace": "bar", "podName": "foo", "podUID": "12345678", "containerName": "baz"}`,
  113. false,
  114. },
  115. }
  116. for _, test := range tests {
  117. p := path.Join(regPath, "kubernetes.io~glusterfs.json")
  118. if len(test.json) > 0 {
  119. if err := ioutil.WriteFile(p, []byte(test.json), 0600); err != nil {
  120. t.Errorf("test %q: error writing %s: %v", test.name, p, err)
  121. continue
  122. }
  123. } else {
  124. // "" means no JSON file
  125. os.Remove(p)
  126. }
  127. pod, container, err := mgr.GetMountPod("kubernetes.io/glusterfs")
  128. if err != nil {
  129. klog.V(5).Infof("test %q returned error %s", test.name, err)
  130. }
  131. if err == nil && test.expectError {
  132. t.Errorf("test %q: expected error, got none", test.name)
  133. }
  134. if err != nil && !test.expectError {
  135. t.Errorf("test %q: unexpected error: %v", test.name, err)
  136. }
  137. if err == nil {
  138. // Pod must be returned when the json file was not empty
  139. if pod == nil && len(test.json) != 0 {
  140. t.Errorf("test %q: expected exec, got nil", test.name)
  141. }
  142. // Both pod and container must be returned
  143. if pod != nil && len(container) == 0 {
  144. t.Errorf("test %q: expected container name, got %q", test.name, container)
  145. }
  146. }
  147. }
  148. }