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

Write test run config into yaml file for reproducing #60

Merged
merged 2 commits into from
Jan 15, 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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,13 @@ GITHUB_TOKEN="your_github_read_token" mvn verify -Pupgrade
```commandline
GITHUB_TOKEN="your_github_read_token" mvn test
```

## Reproducing test run
When every test run is executed, test suite automatically creates a `config.yaml` file
which contains all configured environment variables. Location of config file
is `$LOG_DIR/test-run-YYYY-MM-DD_HH-mm/config.yaml` where `$LOG_DIR` is by
default `${repo_root}/target/logs`.

```commandline
GITHUB_TOKEN="your_github_read_token" ENV_FILE=path_to_file/config.yaml mvn verify -Psmoke
```
36 changes: 36 additions & 0 deletions src/main/java/io/odh/test/Environment.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@
import io.odh.test.install.InstallTypes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
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.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
Expand Down Expand Up @@ -109,6 +113,11 @@ private Environment() {
}
});
LoggerUtils.logSeparator("-", 30);
try {
saveConfigurationFile();
} catch (IOException e) {
LOGGER.warn("Yaml configuration can't be saved");
}
}

public static void print() {
Expand Down Expand Up @@ -144,4 +153,31 @@ private static Map<String, Object> loadConfigurationFile() {
return Collections.emptyMap();
}
}

private static void saveConfigurationFile() throws IOException {
Path logPath = Environment.LOG_DIR;
Files.createDirectories(logPath);

LinkedHashMap<String, String> toSave = new LinkedHashMap<>();

VALUES.forEach((key, value) -> {
if (isWriteable(key, value)) {
toSave.put(key, value);
}
});

PrintWriter writer = new PrintWriter(logPath.resolve("config.yaml").toFile());
final DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
options.setPrettyFlow(true);
Yaml yaml = new Yaml(options);
yaml.dump(toSave, writer);
}

private static boolean isWriteable(String var, String value) {
return !value.equals("null")
&& !var.equals(CONFIG_FILE_PATH_ENV)
&& !var.equals(USER_PATH)
&& !value.equals("USER");
}
}