empty_dir_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. // +build linux
  2. /*
  3. Copyright 2014 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package emptydir
  15. import (
  16. "os"
  17. "path/filepath"
  18. "testing"
  19. "k8s.io/utils/mount"
  20. v1 "k8s.io/api/core/v1"
  21. "k8s.io/apimachinery/pkg/api/resource"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "k8s.io/apimachinery/pkg/types"
  24. utiltesting "k8s.io/client-go/util/testing"
  25. "k8s.io/kubernetes/pkg/volume"
  26. volumetest "k8s.io/kubernetes/pkg/volume/testing"
  27. "k8s.io/kubernetes/pkg/volume/util"
  28. )
  29. // Construct an instance of a plugin, by name.
  30. func makePluginUnderTest(t *testing.T, plugName, basePath string) volume.VolumePlugin {
  31. plugMgr := volume.VolumePluginMgr{}
  32. plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, volumetest.NewFakeVolumeHost(t, basePath, nil, nil))
  33. plug, err := plugMgr.FindPluginByName(plugName)
  34. if err != nil {
  35. t.Errorf("Can't find the plugin by name")
  36. }
  37. return plug
  38. }
  39. func TestCanSupport(t *testing.T) {
  40. tmpDir, err := utiltesting.MkTmpdir("emptydirTest")
  41. if err != nil {
  42. t.Fatalf("can't make a temp dir: %v", err)
  43. }
  44. defer os.RemoveAll(tmpDir)
  45. plug := makePluginUnderTest(t, "kubernetes.io/empty-dir", tmpDir)
  46. if plug.GetPluginName() != "kubernetes.io/empty-dir" {
  47. t.Errorf("Wrong name: %s", plug.GetPluginName())
  48. }
  49. if !plug.CanSupport(&volume.Spec{Volume: &v1.Volume{VolumeSource: v1.VolumeSource{EmptyDir: &v1.EmptyDirVolumeSource{}}}}) {
  50. t.Errorf("Expected true")
  51. }
  52. if plug.CanSupport(&volume.Spec{Volume: &v1.Volume{VolumeSource: v1.VolumeSource{}}}) {
  53. t.Errorf("Expected false")
  54. }
  55. }
  56. type fakeMountDetector struct {
  57. medium v1.StorageMedium
  58. isMount bool
  59. }
  60. func (fake *fakeMountDetector) GetMountMedium(path string) (v1.StorageMedium, bool, error) {
  61. return fake.medium, fake.isMount, nil
  62. }
  63. func TestPluginEmptyRootContext(t *testing.T) {
  64. doTestPlugin(t, pluginTestConfig{
  65. medium: v1.StorageMediumDefault,
  66. expectedSetupMounts: 0,
  67. expectedTeardownMounts: 0})
  68. }
  69. func TestPluginHugetlbfs(t *testing.T) {
  70. doTestPlugin(t, pluginTestConfig{
  71. medium: v1.StorageMediumHugePages,
  72. expectedSetupMounts: 1,
  73. expectedTeardownMounts: 0,
  74. shouldBeMountedBeforeTeardown: true,
  75. })
  76. }
  77. type pluginTestConfig struct {
  78. medium v1.StorageMedium
  79. idempotent bool
  80. expectedSetupMounts int
  81. shouldBeMountedBeforeTeardown bool
  82. expectedTeardownMounts int
  83. }
  84. // doTestPlugin sets up a volume and tears it back down.
  85. func doTestPlugin(t *testing.T, config pluginTestConfig) {
  86. basePath, err := utiltesting.MkTmpdir("emptydir_volume_test")
  87. if err != nil {
  88. t.Fatalf("can't make a temp rootdir: %v", err)
  89. }
  90. defer os.RemoveAll(basePath)
  91. var (
  92. volumePath = filepath.Join(basePath, "pods/poduid/volumes/kubernetes.io~empty-dir/test-volume")
  93. metadataDir = filepath.Join(basePath, "pods/poduid/plugins/kubernetes.io~empty-dir/test-volume")
  94. plug = makePluginUnderTest(t, "kubernetes.io/empty-dir", basePath)
  95. volumeName = "test-volume"
  96. spec = &v1.Volume{
  97. Name: volumeName,
  98. VolumeSource: v1.VolumeSource{EmptyDir: &v1.EmptyDirVolumeSource{Medium: config.medium}},
  99. }
  100. physicalMounter = mount.NewFakeMounter(nil)
  101. mountDetector = fakeMountDetector{}
  102. pod = &v1.Pod{
  103. ObjectMeta: metav1.ObjectMeta{
  104. UID: types.UID("poduid"),
  105. },
  106. Spec: v1.PodSpec{
  107. Containers: []v1.Container{
  108. {
  109. Resources: v1.ResourceRequirements{
  110. Requests: v1.ResourceList{
  111. v1.ResourceName("hugepages-2Mi"): resource.MustParse("100Mi"),
  112. },
  113. },
  114. },
  115. },
  116. },
  117. }
  118. )
  119. if config.idempotent {
  120. physicalMounter.MountPoints = []mount.MountPoint{
  121. {
  122. Path: volumePath,
  123. },
  124. }
  125. util.SetReady(metadataDir)
  126. }
  127. mounter, err := plug.(*emptyDirPlugin).newMounterInternal(volume.NewSpecFromVolume(spec),
  128. pod,
  129. physicalMounter,
  130. &mountDetector,
  131. volume.VolumeOptions{})
  132. if err != nil {
  133. t.Errorf("Failed to make a new Mounter: %v", err)
  134. }
  135. if mounter == nil {
  136. t.Errorf("Got a nil Mounter")
  137. }
  138. volPath := mounter.GetPath()
  139. if volPath != volumePath {
  140. t.Errorf("Got unexpected path: %s", volPath)
  141. }
  142. if err := mounter.SetUp(volume.MounterArgs{}); err != nil {
  143. t.Errorf("Expected success, got: %v", err)
  144. }
  145. // Stat the directory and check the permission bits
  146. fileinfo, err := os.Stat(volPath)
  147. if !config.idempotent {
  148. if err != nil {
  149. if os.IsNotExist(err) {
  150. t.Errorf("SetUp() failed, volume path not created: %s", volPath)
  151. } else {
  152. t.Errorf("SetUp() failed: %v", err)
  153. }
  154. }
  155. if e, a := perm, fileinfo.Mode().Perm(); e != a {
  156. t.Errorf("Unexpected file mode for %v: expected: %v, got: %v", volPath, e, a)
  157. }
  158. } else if err == nil {
  159. // If this test is for idempotency and we were able
  160. // to stat the volume path, it's an error.
  161. t.Errorf("Volume directory was created unexpectedly")
  162. }
  163. log := physicalMounter.GetLog()
  164. // Check the number of mounts performed during setup
  165. if e, a := config.expectedSetupMounts, len(log); e != a {
  166. t.Errorf("Expected %v physicalMounter calls during setup, got %v", e, a)
  167. } else if config.expectedSetupMounts == 1 &&
  168. (log[0].Action != mount.FakeActionMount || (log[0].FSType != "tmpfs" && log[0].FSType != "hugetlbfs")) {
  169. t.Errorf("Unexpected physicalMounter action during setup: %#v", log[0])
  170. }
  171. physicalMounter.ResetLog()
  172. // Make an unmounter for the volume
  173. teardownMedium := v1.StorageMediumDefault
  174. if config.medium == v1.StorageMediumMemory {
  175. teardownMedium = v1.StorageMediumMemory
  176. }
  177. unmounterMountDetector := &fakeMountDetector{medium: teardownMedium, isMount: config.shouldBeMountedBeforeTeardown}
  178. unmounter, err := plug.(*emptyDirPlugin).newUnmounterInternal(volumeName, types.UID("poduid"), physicalMounter, unmounterMountDetector)
  179. if err != nil {
  180. t.Errorf("Failed to make a new Unmounter: %v", err)
  181. }
  182. if unmounter == nil {
  183. t.Errorf("Got a nil Unmounter")
  184. }
  185. // Tear down the volume
  186. if err := unmounter.TearDown(); err != nil {
  187. t.Errorf("Expected success, got: %v", err)
  188. }
  189. if _, err := os.Stat(volPath); err == nil {
  190. t.Errorf("TearDown() failed, volume path still exists: %s", volPath)
  191. } else if !os.IsNotExist(err) {
  192. t.Errorf("TearDown() failed: %v", err)
  193. }
  194. log = physicalMounter.GetLog()
  195. // Check the number of physicalMounter calls during tardown
  196. if e, a := config.expectedTeardownMounts, len(log); e != a {
  197. t.Errorf("Expected %v physicalMounter calls during teardown, got %v", e, a)
  198. } else if config.expectedTeardownMounts == 1 && log[0].Action != mount.FakeActionUnmount {
  199. t.Errorf("Unexpected physicalMounter action during teardown: %#v", log[0])
  200. }
  201. physicalMounter.ResetLog()
  202. }
  203. func TestPluginBackCompat(t *testing.T) {
  204. basePath, err := utiltesting.MkTmpdir("emptydirTest")
  205. if err != nil {
  206. t.Fatalf("can't make a temp dir: %v", err)
  207. }
  208. defer os.RemoveAll(basePath)
  209. plug := makePluginUnderTest(t, "kubernetes.io/empty-dir", basePath)
  210. spec := &v1.Volume{
  211. Name: "vol1",
  212. }
  213. pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{UID: types.UID("poduid")}}
  214. mounter, err := plug.NewMounter(volume.NewSpecFromVolume(spec), pod, volume.VolumeOptions{})
  215. if err != nil {
  216. t.Errorf("Failed to make a new Mounter: %v", err)
  217. }
  218. if mounter == nil {
  219. t.Fatalf("Got a nil Mounter")
  220. }
  221. volPath := mounter.GetPath()
  222. if volPath != filepath.Join(basePath, "pods/poduid/volumes/kubernetes.io~empty-dir/vol1") {
  223. t.Errorf("Got unexpected path: %s", volPath)
  224. }
  225. }
  226. // TestMetrics tests that MetricProvider methods return sane values.
  227. func TestMetrics(t *testing.T) {
  228. // Create an empty temp directory for the volume
  229. tmpDir, err := utiltesting.MkTmpdir("empty_dir_test")
  230. if err != nil {
  231. t.Fatalf("Can't make a tmp dir: %v", err)
  232. }
  233. defer os.RemoveAll(tmpDir)
  234. plug := makePluginUnderTest(t, "kubernetes.io/empty-dir", tmpDir)
  235. spec := &v1.Volume{
  236. Name: "vol1",
  237. }
  238. pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{UID: types.UID("poduid")}}
  239. mounter, err := plug.NewMounter(volume.NewSpecFromVolume(spec), pod, volume.VolumeOptions{})
  240. if err != nil {
  241. t.Errorf("Failed to make a new Mounter: %v", err)
  242. }
  243. if mounter == nil {
  244. t.Fatalf("Got a nil Mounter")
  245. }
  246. // Need to create the subdirectory
  247. os.MkdirAll(mounter.GetPath(), 0755)
  248. expectedEmptyDirUsage, err := volumetest.FindEmptyDirectoryUsageOnTmpfs()
  249. if err != nil {
  250. t.Errorf("Unexpected error finding expected empty directory usage on tmpfs: %v", err)
  251. }
  252. // TODO(pwittroc): Move this into a reusable testing utility
  253. metrics, err := mounter.GetMetrics()
  254. if err != nil {
  255. t.Errorf("Unexpected error when calling GetMetrics %v", err)
  256. }
  257. if e, a := expectedEmptyDirUsage.Value(), metrics.Used.Value(); e != a {
  258. t.Errorf("Unexpected value for empty directory; expected %v, got %v", e, a)
  259. }
  260. if metrics.Capacity.Value() <= 0 {
  261. t.Errorf("Expected Capacity to be greater than 0")
  262. }
  263. if metrics.Available.Value() <= 0 {
  264. t.Errorf("Expected Available to be greater than 0")
  265. }
  266. }
  267. func TestGetHugePagesMountOptions(t *testing.T) {
  268. testCases := map[string]struct {
  269. pod *v1.Pod
  270. shouldFail bool
  271. expectedResult string
  272. }{
  273. "testWithProperValues": {
  274. pod: &v1.Pod{
  275. Spec: v1.PodSpec{
  276. Containers: []v1.Container{
  277. {
  278. Resources: v1.ResourceRequirements{
  279. Requests: v1.ResourceList{
  280. v1.ResourceName("hugepages-2Mi"): resource.MustParse("100Mi"),
  281. },
  282. },
  283. },
  284. },
  285. },
  286. },
  287. shouldFail: false,
  288. expectedResult: "pagesize=2Mi",
  289. },
  290. "testWithProperValuesAndDifferentPageSize": {
  291. pod: &v1.Pod{
  292. Spec: v1.PodSpec{
  293. Containers: []v1.Container{
  294. {
  295. Resources: v1.ResourceRequirements{
  296. Requests: v1.ResourceList{
  297. v1.ResourceName("hugepages-1Gi"): resource.MustParse("2Gi"),
  298. },
  299. },
  300. },
  301. {
  302. Resources: v1.ResourceRequirements{
  303. Requests: v1.ResourceList{
  304. v1.ResourceName("hugepages-1Gi"): resource.MustParse("4Gi"),
  305. },
  306. },
  307. },
  308. },
  309. },
  310. },
  311. shouldFail: false,
  312. expectedResult: "pagesize=1Gi",
  313. },
  314. "InitContainerAndContainerHasProperValues": {
  315. pod: &v1.Pod{
  316. Spec: v1.PodSpec{
  317. InitContainers: []v1.Container{
  318. {
  319. Resources: v1.ResourceRequirements{
  320. Requests: v1.ResourceList{
  321. v1.ResourceName("hugepages-1Gi"): resource.MustParse("2Gi"),
  322. },
  323. },
  324. },
  325. {
  326. Resources: v1.ResourceRequirements{
  327. Requests: v1.ResourceList{
  328. v1.ResourceName("hugepages-1Gi"): resource.MustParse("4Gi"),
  329. },
  330. },
  331. },
  332. },
  333. },
  334. },
  335. shouldFail: false,
  336. expectedResult: "pagesize=1Gi",
  337. },
  338. "InitContainerAndContainerHasDifferentPageSizes": {
  339. pod: &v1.Pod{
  340. Spec: v1.PodSpec{
  341. InitContainers: []v1.Container{
  342. {
  343. Resources: v1.ResourceRequirements{
  344. Requests: v1.ResourceList{
  345. v1.ResourceName("hugepages-2Mi"): resource.MustParse("2Gi"),
  346. },
  347. },
  348. },
  349. {
  350. Resources: v1.ResourceRequirements{
  351. Requests: v1.ResourceList{
  352. v1.ResourceName("hugepages-1Gi"): resource.MustParse("4Gi"),
  353. },
  354. },
  355. },
  356. },
  357. },
  358. },
  359. shouldFail: true,
  360. expectedResult: "",
  361. },
  362. "ContainersWithMultiplePageSizes": {
  363. pod: &v1.Pod{
  364. Spec: v1.PodSpec{
  365. Containers: []v1.Container{
  366. {
  367. Resources: v1.ResourceRequirements{
  368. Requests: v1.ResourceList{
  369. v1.ResourceName("hugepages-1Gi"): resource.MustParse("2Gi"),
  370. },
  371. },
  372. },
  373. {
  374. Resources: v1.ResourceRequirements{
  375. Requests: v1.ResourceList{
  376. v1.ResourceName("hugepages-2Mi"): resource.MustParse("100Mi"),
  377. },
  378. },
  379. },
  380. },
  381. },
  382. },
  383. shouldFail: true,
  384. expectedResult: "",
  385. },
  386. "PodWithNoHugePagesRequest": {
  387. pod: &v1.Pod{},
  388. shouldFail: true,
  389. expectedResult: "",
  390. },
  391. }
  392. for testCaseName, testCase := range testCases {
  393. value, err := getPageSizeMountOptionFromPod(testCase.pod)
  394. if testCase.shouldFail && err == nil {
  395. t.Errorf("Expected an error in %v", testCaseName)
  396. } else if !testCase.shouldFail && err != nil {
  397. t.Errorf("Unexpected error in %v, got %v", testCaseName, err)
  398. } else if testCase.expectedResult != value {
  399. t.Errorf("Unexpected mountOptions for Pod. Expected %v, got %v", testCase.expectedResult, value)
  400. }
  401. }
  402. }