Skip to content

Commit

Permalink
Add ability to configure suite using yaml file config (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
kornys authored Jan 10, 2024
1 parent 9fd336e commit 421091f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ build/
## Suite specific files
**/*.kubeconfig
env
config.yaml
36 changes: 33 additions & 3 deletions src/main/java/io/odh/test/Environment.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,10 +30,16 @@ public class Environment {
private static final Logger LOGGER = LoggerFactory.getLogger(Environment.class);
private static final Map<String, String> VALUES = new HashMap<>();

private static String config;

private static final Map<String, Object> 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";
Expand Down Expand Up @@ -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 = "{}: {}";
Expand All @@ -94,19 +106,37 @@ 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> T getOrDefault(String var, Function<String, T> 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);
}
VALUES.put(var, String.valueOf(returnValue));
return returnValue;
}

private static Map<String, Object> 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();
}
}
}

0 comments on commit 421091f

Please sign in to comment.