diff --git a/.gitignore b/.gitignore index 45c565ce..0c7c50bd 100644 --- a/.gitignore +++ b/.gitignore @@ -38,3 +38,4 @@ build/ ## Suite specific files **/*.kubeconfig env +config.yaml diff --git a/src/main/java/io/odh/test/Environment.java b/src/main/java/io/odh/test/Environment.java index bcdb75a0..7efd23da 100644 --- a/src/main/java/io/odh/test/Environment.java +++ b/src/main/java/io/odh/test/Environment.java @@ -7,11 +7,16 @@ import io.odh.test.install.InstallTypes; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.yaml.snakeyaml.Yaml; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; +import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Objects; @@ -25,10 +30,16 @@ public class Environment { private static final Logger LOGGER = LoggerFactory.getLogger(Environment.class); private static final Map VALUES = new HashMap<>(); + private static String config; + + private static final Map YAML_DATA = loadConfigurationFile(); + public static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm"); public static final String USER_PATH = System.getProperty("user.dir"); + private static final String CONFIG_FILE_PATH_ENV = "ENV_FILE"; + private static final String USERNAME_ENV = "KUBE_USERNAME"; private static final String PASSWORD_ENV = "KUBE_PASSWORD"; private static final String TOKEN_ENV = "KUBE_TOKEN"; @@ -80,7 +91,8 @@ public class Environment { public static final Path LOG_DIR = getOrDefault(LOG_DIR_ENV, Paths::get, Paths.get(USER_PATH, "target", "logs")).resolve("test-run-" + DATE_FORMAT.format(LocalDateTime.now())); - private Environment() { } + private Environment() { + } static { String debugFormat = "{}: {}"; @@ -94,14 +106,19 @@ private Environment() { } }); } - public static void print() { } + public static void print() { + } private static String getOrDefault(String varName, String defaultValue) { return getOrDefault(varName, String::toString, defaultValue); } private static T getOrDefault(String var, Function converter, T defaultValue) { - String value = System.getenv(var); + String value = System.getenv(var) != null ? + System.getenv(var) : + (Objects.requireNonNull(YAML_DATA).get(var) != null ? + YAML_DATA.get(var).toString() : + null); T returnValue = defaultValue; if (value != null) { returnValue = converter.apply(value); @@ -109,4 +126,17 @@ private static T getOrDefault(String var, Function converter, T d VALUES.put(var, String.valueOf(returnValue)); return returnValue; } + + private static Map loadConfigurationFile() { + config = System.getenv().getOrDefault(CONFIG_FILE_PATH_ENV, + Paths.get(System.getProperty("user.dir"), "config.yaml").toAbsolutePath().toString()); + Yaml yaml = new Yaml(); + try { + File yamlFile = new File(config).getAbsoluteFile(); + return yaml.load(new FileInputStream(yamlFile)); + } catch (IOException ex) { + LOGGER.info("Yaml configuration not provider or not exists"); + return Collections.emptyMap(); + } + } }