Skip to content

Commit

Permalink
fix IT by restore env variable change
Browse files Browse the repository at this point in the history
Signed-off-by: Kaituo Li <[email protected]>
  • Loading branch information
kaituo committed Feb 8, 2024
1 parent d0f5754 commit 20a6352
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,20 @@ public void testGetDimensionsFromSystemEnv() throws NoSuchFieldException, Illega
Field field = classOfMap.getDeclaredField("m");
field.setAccessible(true);
Map<String, String> writeableEnvironmentVariables = (Map<String, String>)field.get(System.getenv());
writeableEnvironmentVariables.put("TEST_VAR", "dummy1");
writeableEnvironmentVariables.put("SERVERLESS_EMR_JOB_ID", "dummy2");
Dimension result1 = DimensionUtils.constructDimension("TEST_VAR", parts);
assertEquals("TEST_VAR", result1.getName());
assertEquals("dummy1", result1.getValue());
Dimension result2 = DimensionUtils.constructDimension("jobId", parts);
assertEquals("jobId", result2.getName());
assertEquals("dummy2", result2.getValue());
try {
writeableEnvironmentVariables.put("TEST_VAR", "dummy1");
writeableEnvironmentVariables.put("SERVERLESS_EMR_JOB_ID", "dummy2");
Dimension result1 = DimensionUtils.constructDimension("TEST_VAR", parts);
assertEquals("TEST_VAR", result1.getName());
assertEquals("dummy1", result1.getValue());
Dimension result2 = DimensionUtils.constructDimension("jobId", parts);
assertEquals("jobId", result2.getName());
assertEquals("dummy2", result2.getValue());
} finally {
// since system environment is shared by other tests. Make sure to remove them before exiting.
writeableEnvironmentVariables.remove("SERVERLESS_EMR_JOB_ID");
writeableEnvironmentVariables.remove("TEST_VAR");
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ package org.apache.spark.sql.util
* Trait defining an interface for fetching environment variables.
*/
trait EnvironmentProvider {

/**
* Retrieves the value of an environment variable.
*
* @param name The name of the environment variable.
* @param default The default value to return if the environment variable is not set.
* @return The value of the environment variable if it exists, otherwise the default value.
* @param name
* The name of the environment variable.
* @param default
* The default value to return if the environment variable is not set.
* @return
* The value of the environment variable if it exists, otherwise the default value.
*/
def getEnvVar(name: String, default: String): String
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,22 @@
package org.apache.spark.sql.util

/**
* An implementation of `EnvironmentProvider` that fetches actual environment variables from the system.
* An implementation of `EnvironmentProvider` that fetches actual environment variables from the
* system.
*/
class RealEnvironment extends EnvironmentProvider {

/**
* Retrieves the value of an environment variable from the system or returns a default value if not present.
* Retrieves the value of an environment variable from the system or returns a default value if
* not present.
*
* @param name The name of the environment variable.
* @param default The default value to return if the environment variable is not set in the system.
* @return The value of the environment variable if it exists in the system, otherwise the default value.
* @param name
* The name of the environment variable.
* @param default
* The default value to return if the environment variable is not set in the system.
* @return
* The value of the environment variable if it exists in the system, otherwise the default
* value.
*/
def getEnvVar(name: String, default: String): String = sys.env.getOrElse(name, default)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,25 @@
package org.apache.spark.sql.util

/**
* A mock implementation of `EnvironmentProvider` for use in tests, where environment variables can be predefined.
* A mock implementation of `EnvironmentProvider` for use in tests, where environment variables
* can be predefined.
*
* @param inputMap A map representing the environment variables (name -> value).
* @param inputMap
* A map representing the environment variables (name -> value).
*/
class MockEnvironment(inputMap: Map[String, String]) extends EnvironmentProvider {

/**
* Retrieves the value of an environment variable from the input map or returns a default value if not present.
* Retrieves the value of an environment variable from the input map or returns a default value
* if not present.
*
* @param name The name of the environment variable.
* @param default The default value to return if the environment variable is not set in the input map.
* @return The value of the environment variable from the input map if it exists, otherwise the default value.
* @param name
* The name of the environment variable.
* @param default
* The default value to return if the environment variable is not set in the input map.
* @return
* The value of the environment variable from the input map if it exists, otherwise the
* default value.
*/
def getEnvVar(name: String, default: String): String = inputMap.getOrElse(name, default)
}

0 comments on commit 20a6352

Please sign in to comment.