From e7d059ceed480020b52d4e9cecfabf467b0e271a Mon Sep 17 00:00:00 2001 From: benbierens Date: Fri, 2 Jun 2023 09:03:46 +0200 Subject: [PATCH] Makes default configuration overridable from environment variables. --- DistTestCore/Configuration.cs | 46 +++++++++++++++++++++++++++++++---- README.md | 11 +++++++++ 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/DistTestCore/Configuration.cs b/DistTestCore/Configuration.cs index b9f21de3..f66c39db 100644 --- a/DistTestCore/Configuration.cs +++ b/DistTestCore/Configuration.cs @@ -5,11 +5,28 @@ namespace DistTestCore { public class Configuration { + private readonly string? kubeConfigFile; + private readonly string logPath; + private readonly bool logDebug; + private readonly string dataFilesPath; + private readonly CodexLogLevel codexLogLevel; + private readonly TestRunnerLocation runnerLocation; + + public Configuration() + { + kubeConfigFile = GetNullableEnvVarOrDefault("KUBECONFIG", null); + logPath = GetEnvVarOrDefault("LOGPATH", "CodexTestLogs"); + logDebug = GetEnvVarOrDefault("LOGDEBUG", "false").ToLowerInvariant() == "true"; + dataFilesPath = GetEnvVarOrDefault("DATAFILEPATH", "TestDataFiles"); + codexLogLevel = ParseEnum(GetEnvVarOrDefault("LOGLEVEL", nameof(CodexLogLevel.Trace))); + runnerLocation = ParseEnum(GetEnvVarOrDefault("RUNNERLOCATION", nameof(TestRunnerLocation.ExternalToCluster))); + } + public KubernetesWorkflow.Configuration GetK8sConfiguration(ITimeSet timeSet) { return new KubernetesWorkflow.Configuration( k8sNamespacePrefix: "ct-", - kubeConfigFile: null, + kubeConfigFile: kubeConfigFile, operationTimeout: timeSet.K8sOperationTimeout(), retryDelay: timeSet.WaitForK8sServiceDelay(), locationMap: new[] @@ -22,22 +39,22 @@ namespace DistTestCore public Logging.LogConfig GetLogConfig() { - return new Logging.LogConfig("CodexTestLogs", debugEnabled: false); + return new Logging.LogConfig(logPath, debugEnabled: logDebug); } public string GetFileManagerFolder() { - return "TestDataFiles"; + return dataFilesPath; } public CodexLogLevel GetCodexLogLevel() { - return CodexLogLevel.Trace; + return codexLogLevel; } public TestRunnerLocation GetTestRunnerLocation() { - return TestRunnerLocation.ExternalToCluster; + return runnerLocation; } public RunningContainerAddress GetAddress(RunningContainer container) @@ -48,6 +65,25 @@ namespace DistTestCore } return container.ClusterExternalAddress; } + + private static string GetEnvVarOrDefault(string varName, string defaultValue) + { + var v = Environment.GetEnvironmentVariable(varName); + if (v == null) return defaultValue; + return v; + } + + private static string? GetNullableEnvVarOrDefault(string varName, string? defaultValue) + { + var v = Environment.GetEnvironmentVariable(varName); + if (v == null) return defaultValue; + return v; + } + + private static T ParseEnum(string value) + { + return (T)Enum.Parse(typeof(T), value, true); + } } public enum TestRunnerLocation diff --git a/README.md b/README.md index 9f5ebefd..4d4760c0 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,17 @@ Tests are devided into two assemblies: `/Tests` and `/LongTests`. TODO: All tests will eventually be running as part of a dedicated CI pipeline and kubernetes cluster. Currently, we're developing these tests and the infra-code to support it by running the whole thing locally. +## Configuration +Test executing can be configured using the following environment variables. +| Variable | Description | Default | +|----------------|------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------| +| KUBECONFIG | Optional path (abs or rel) to kubeconfig YAML file. When null, uses system default (docker-desktop) kubeconfig if available. | (null) | +| LOGPATH | Path (abs or rel) where log files will be saved. | "CodexTestLogs" | +| LOGDEBUG | When "true", enables additional test-runner debug log output. | "false" | +| DATAFILEPATH | Path (abs or rel) where temporary test data files will be saved. | "TestDataFiles" | +| LOGLEVEL | Codex log-level. (case-insensitive) | "Trace" | +| RUNNERLOCATION | Use "ExternalToCluster" when test app is running outside of the k8s cluster. Use "InternalToCluster" when tests are run from inside a pod/container. | "ExternalToCluster" | + ## Test logs Because tests potentially take a long time to run, logging is in place to help you investigate failures afterwards. Should a test fail, all Codex terminal output (as well as metrics if they have been enabled) will be downloaded and stored along with a detailed, step-by-step log of the test. If something's gone wrong and you're here to discover the details, head for the logs.