|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.spark.deploy.k8s.features |
| 18 | + |
| 19 | +import io.fabric8.kubernetes.api.model.{EnvVarBuilder, VolumeBuilder, VolumeMountBuilder} |
| 20 | +import org.mockito.Mockito |
| 21 | +import org.scalatest.BeforeAndAfter |
| 22 | + |
| 23 | +import org.apache.spark.{SparkConf, SparkFunSuite} |
| 24 | +import org.apache.spark.deploy.k8s.{KubernetesConf, KubernetesDriverSpecificConf, KubernetesExecutorSpecificConf, KubernetesRoleSpecificConf, SparkPod} |
| 25 | + |
| 26 | +class LocalDirsFeatureStepSuite extends SparkFunSuite with BeforeAndAfter { |
| 27 | + private val defaultLocalDir = "/var/data/default-local-dir" |
| 28 | + private var sparkConf: SparkConf = _ |
| 29 | + private var kubernetesConf: KubernetesConf[_ <: KubernetesRoleSpecificConf] = _ |
| 30 | + |
| 31 | + before { |
| 32 | + val realSparkConf = new SparkConf(false) |
| 33 | + sparkConf = Mockito.spy(realSparkConf) |
| 34 | + kubernetesConf = KubernetesConf( |
| 35 | + sparkConf, |
| 36 | + KubernetesDriverSpecificConf( |
| 37 | + None, |
| 38 | + "app-name", |
| 39 | + "main", |
| 40 | + Seq.empty), |
| 41 | + "resource", |
| 42 | + "app-id", |
| 43 | + Map.empty, |
| 44 | + Map.empty, |
| 45 | + Map.empty, |
| 46 | + Map.empty) |
| 47 | + } |
| 48 | + |
| 49 | + test("Resolve to default local dir if neither env nor configuration are set") { |
| 50 | + Mockito.doReturn(null).when(sparkConf).get("spark.local.dir") |
| 51 | + Mockito.doReturn(null).when(sparkConf).getenv("SPARK_LOCAL_DIRS") |
| 52 | + val stepUnderTest = new LocalDirsFeatureStep(kubernetesConf, defaultLocalDir) |
| 53 | + val configuredPod = stepUnderTest.configurePod(SparkPod.initialPod()) |
| 54 | + assert(configuredPod.pod.getSpec.getVolumes.size === 1) |
| 55 | + assert(configuredPod.pod.getSpec.getVolumes.get(0) === |
| 56 | + new VolumeBuilder() |
| 57 | + .withName(s"spark-local-dir-1-default-local-dir") |
| 58 | + .withNewEmptyDir() |
| 59 | + .endEmptyDir() |
| 60 | + .build()) |
| 61 | + assert(configuredPod.container.getVolumeMounts.size === 1) |
| 62 | + assert(configuredPod.container.getVolumeMounts.get(0) === |
| 63 | + new VolumeMountBuilder() |
| 64 | + .withName(s"spark-local-dir-1-default-local-dir") |
| 65 | + .withMountPath(defaultLocalDir) |
| 66 | + .build()) |
| 67 | + assert(configuredPod.container.getEnv.size === 1) |
| 68 | + assert(configuredPod.container.getEnv.get(0) === |
| 69 | + new EnvVarBuilder() |
| 70 | + .withName("SPARK_LOCAL_DIRS") |
| 71 | + .withValue(defaultLocalDir) |
| 72 | + .build()) |
| 73 | + } |
| 74 | + |
| 75 | + test("Use configured local dirs split on comma if provided.") { |
| 76 | + Mockito.doReturn("/var/data/my-local-dir-1,/var/data/my-local-dir-2") |
| 77 | + .when(sparkConf).getenv("SPARK_LOCAL_DIRS") |
| 78 | + val stepUnderTest = new LocalDirsFeatureStep(kubernetesConf, defaultLocalDir) |
| 79 | + val configuredPod = stepUnderTest.configurePod(SparkPod.initialPod()) |
| 80 | + assert(configuredPod.pod.getSpec.getVolumes.size === 2) |
| 81 | + assert(configuredPod.pod.getSpec.getVolumes.get(0) === |
| 82 | + new VolumeBuilder() |
| 83 | + .withName(s"spark-local-dir-1-my-local-dir-1") |
| 84 | + .withNewEmptyDir() |
| 85 | + .endEmptyDir() |
| 86 | + .build()) |
| 87 | + assert(configuredPod.pod.getSpec.getVolumes.get(1) === |
| 88 | + new VolumeBuilder() |
| 89 | + .withName(s"spark-local-dir-2-my-local-dir-2") |
| 90 | + .withNewEmptyDir() |
| 91 | + .endEmptyDir() |
| 92 | + .build()) |
| 93 | + assert(configuredPod.container.getVolumeMounts.size === 2) |
| 94 | + assert(configuredPod.container.getVolumeMounts.get(0) === |
| 95 | + new VolumeMountBuilder() |
| 96 | + .withName(s"spark-local-dir-1-my-local-dir-1") |
| 97 | + .withMountPath("/var/data/my-local-dir-1") |
| 98 | + .build()) |
| 99 | + assert(configuredPod.container.getVolumeMounts.get(1) === |
| 100 | + new VolumeMountBuilder() |
| 101 | + .withName(s"spark-local-dir-2-my-local-dir-2") |
| 102 | + .withMountPath("/var/data/my-local-dir-2") |
| 103 | + .build()) |
| 104 | + assert(configuredPod.container.getEnv.size === 1) |
| 105 | + assert(configuredPod.container.getEnv.get(0) === |
| 106 | + new EnvVarBuilder() |
| 107 | + .withName("SPARK_LOCAL_DIRS") |
| 108 | + .withValue("/var/data/my-local-dir-1,/var/data/my-local-dir-2") |
| 109 | + .build()) |
| 110 | + } |
| 111 | +} |
0 commit comments