Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to configure suite using yaml file config #50

Merged
merged 2 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
}
Loading