cs-codex-dist-tests/ContinuousTests/Configuration.cs

110 lines
4.4 KiB
C#
Raw Normal View History

2023-06-23 08:14:16 +00:00
using DistTestCore.Codex;
using Newtonsoft.Json;
namespace ContinuousTests
{
public class Configuration
{
public string LogPath { get; set; } = string.Empty;
2023-06-25 07:53:10 +00:00
public string DataPath { get; set; } = string.Empty;
2023-06-23 08:14:16 +00:00
public CodexDeployment CodexDeployment { get; set; } = null!;
public bool KeepPassedTestLogs { get; set; }
}
public class ConfigLoader
{
private const string filename = "config.json";
public Configuration Load()
{
var config = Read();
Validate(config);
return config;
}
private Configuration Read()
{
if (File.Exists(filename))
{
var lines = File.ReadAllText(filename);
try
{
var result = JsonConvert.DeserializeObject<Configuration>(lines);
if (result != null) return result;
}
catch { }
}
2023-06-25 07:53:10 +00:00
var logPath = "logs";// Environment.GetEnvironmentVariable("LOGPATH");
var codexDeploymentJson = "C:\\Users\\Ben\\Desktop\\codex-deployment.json"; //Environment.GetEnvironmentVariable("CODEXDEPLOYMENT");
var sleepPerSingle = "10";// Environment.GetEnvironmentVariable("SLEEPSECONDSPERSINGLETEST");
var sleepPerAll = "10";// Environment.GetEnvironmentVariable("SLEEPSECONDSPERALLTESTS");
var keep = ""; // Environment.GetEnvironmentVariable("KEEPPASSEDTESTLOGS");
2023-06-21 08:34:29 +00:00
if (!string.IsNullOrEmpty(logPath) &&
2023-06-23 08:14:16 +00:00
!string.IsNullOrEmpty(codexDeploymentJson) &&
2023-06-21 08:34:29 +00:00
!string.IsNullOrEmpty(sleepPerSingle) &&
!string.IsNullOrEmpty(sleepPerAll))
{
2023-06-23 08:14:16 +00:00
try
{
2023-06-23 08:14:16 +00:00
return new Configuration
{
LogPath = logPath,
CodexDeployment = ParseCodexDeploymentJson(codexDeploymentJson),
2023-06-25 07:53:10 +00:00
//SleepSecondsPerSingleTest = Convert.ToInt32(sleepPerSingle),
//SleepSecondsPerAllTests = Convert.ToInt32(sleepPerAll),
2023-06-23 08:14:16 +00:00
KeepPassedTestLogs = keep == "1"
};
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex);
}
}
2023-06-21 08:34:29 +00:00
var nl = Environment.NewLine;
throw new Exception($"Unable to load configuration from '{filename}', and " +
"unable to load configuration from environment variables. " + nl +
"'LOGPATH' = Path where log files will be saved." + nl +
2023-06-23 08:14:16 +00:00
"'CODEXDEPLOYMENT' = Path to codex-deployment JSON file." + nl +
"'SLEEPSECONDSPERSINGLETEST' = Seconds to sleep after each individual test." + nl +
"'SLEEPSECONDSPERALLTESTS' = Seconds to sleep after all tests, before starting again." + nl +
"'KEEPPASSEDTESTLOGS' = (Optional, default: 0) Set to '1' to keep log files of tests that passed." + nl +
2023-06-21 08:34:29 +00:00
nl);
}
private void Validate(Configuration configuration)
{
2023-06-25 07:53:10 +00:00
//if (configuration.SleepSecondsPerSingleTest < 1)
//{
// Console.WriteLine("Warning: configuration.SleepSecondsPerSingleTest was less than 1 seconds. Using 1 seconds instead!");
// configuration.SleepSecondsPerSingleTest = 1;
//}
//if (configuration.SleepSecondsPerAllTests < 1)
//{
// Console.WriteLine("Warning: configuration.SleepSecondsPerAllTests was less than 10 seconds. Using 10 seconds instead!");
// configuration.SleepSecondsPerAllTests = 10;
//}
if (string.IsNullOrEmpty(configuration.LogPath))
{
throw new Exception($"Unvalid logpath set: '{configuration.LogPath}'");
}
if (configuration.CodexDeployment == null || !configuration.CodexDeployment.CodexContainers.Any())
{
2023-06-23 08:14:16 +00:00
throw new Exception("No Codex deployment found.");
}
}
2023-06-23 08:14:16 +00:00
private CodexDeployment ParseCodexDeploymentJson(string filename)
{
var d = JsonConvert.DeserializeObject<CodexDeployment>(File.ReadAllText(filename))!;
if (d == null) throw new Exception("Unable to parse " + filename);
return d;
}
}
}