Skip to content

Commit 0a30e93

Browse files
bersprocketsMarcelo Vanzin
authored andcommitted
[SPARK-22940][SQL] HiveExternalCatalogVersionsSuite should succeed on platforms that don't have wget
## What changes were proposed in this pull request? Modified HiveExternalCatalogVersionsSuite.scala to use Utils.doFetchFile to download different versions of Spark binaries rather than launching wget as an external process. On platforms that don't have wget installed, this suite fails with an error. cloud-fan : would you like to check this change? ## How was this patch tested? 1) test-only of HiveExternalCatalogVersionsSuite on several platforms. Tested bad mirror, read timeout, and redirects. 2) ./dev/run-tests Author: Bruce Robbins <[email protected]> Closes #20147 from bersprockets/SPARK-22940-alt. (cherry picked from commit c0b7424) Signed-off-by: Marcelo Vanzin <[email protected]>
1 parent fd4e304 commit 0a30e93

File tree

1 file changed

+42
-6
lines changed

1 file changed

+42
-6
lines changed

sql/hive/src/test/scala/org/apache/spark/sql/hive/HiveExternalCatalogVersionsSuite.scala

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818
package org.apache.spark.sql.hive
1919

2020
import java.io.File
21-
import java.nio.file.Files
21+
import java.nio.charset.StandardCharsets
22+
import java.nio.file.{Files, Paths}
2223

2324
import scala.sys.process._
2425

25-
import org.apache.spark.TestUtils
26+
import org.apache.hadoop.conf.Configuration
27+
28+
import org.apache.spark.{SecurityManager, SparkConf, TestUtils}
2629
import org.apache.spark.sql.{QueryTest, Row, SparkSession}
2730
import org.apache.spark.sql.catalyst.TableIdentifier
2831
import org.apache.spark.sql.catalyst.catalog.CatalogTableType
@@ -55,14 +58,19 @@ class HiveExternalCatalogVersionsSuite extends SparkSubmitTestUtils {
5558
private def tryDownloadSpark(version: String, path: String): Unit = {
5659
// Try mirrors a few times until one succeeds
5760
for (i <- 0 until 3) {
61+
// we don't retry on a failure to get mirror url. If we can't get a mirror url,
62+
// the test fails (getStringFromUrl will throw an exception)
5863
val preferredMirror =
59-
Seq("wget", "https://www.apache.org/dyn/closer.lua?preferred=true", "-q", "-O", "-").!!.trim
60-
val url = s"$preferredMirror/spark/spark-$version/spark-$version-bin-hadoop2.7.tgz"
64+
getStringFromUrl("https://www.apache.org/dyn/closer.lua?preferred=true")
65+
val filename = s"spark-$version-bin-hadoop2.7.tgz"
66+
val url = s"$preferredMirror/spark/spark-$version/$filename"
6167
logInfo(s"Downloading Spark $version from $url")
62-
if (Seq("wget", url, "-q", "-P", path).! == 0) {
68+
try {
69+
getFileFromUrl(url, path, filename)
6370
return
71+
} catch {
72+
case ex: Exception => logWarning(s"Failed to download Spark $version from $url", ex)
6473
}
65-
logWarning(s"Failed to download Spark $version from $url")
6674
}
6775
fail(s"Unable to download Spark $version")
6876
}
@@ -85,6 +93,34 @@ class HiveExternalCatalogVersionsSuite extends SparkSubmitTestUtils {
8593
new File(tmpDataDir, name).getCanonicalPath
8694
}
8795

96+
private def getFileFromUrl(urlString: String, targetDir: String, filename: String): Unit = {
97+
val conf = new SparkConf
98+
// if the caller passes the name of an existing file, we want doFetchFile to write over it with
99+
// the contents from the specified url.
100+
conf.set("spark.files.overwrite", "true")
101+
val securityManager = new SecurityManager(conf)
102+
val hadoopConf = new Configuration
103+
104+
val outDir = new File(targetDir)
105+
if (!outDir.exists()) {
106+
outDir.mkdirs()
107+
}
108+
109+
// propagate exceptions up to the caller of getFileFromUrl
110+
Utils.doFetchFile(urlString, outDir, filename, conf, securityManager, hadoopConf)
111+
}
112+
113+
private def getStringFromUrl(urlString: String): String = {
114+
val contentFile = File.createTempFile("string-", ".txt")
115+
contentFile.deleteOnExit()
116+
117+
// exceptions will propagate to the caller of getStringFromUrl
118+
getFileFromUrl(urlString, contentFile.getParent, contentFile.getName)
119+
120+
val contentPath = Paths.get(contentFile.toURI)
121+
new String(Files.readAllBytes(contentPath), StandardCharsets.UTF_8)
122+
}
123+
88124
override def beforeAll(): Unit = {
89125
super.beforeAll()
90126

0 commit comments

Comments
 (0)