2023-06-21 06:28:40 +00:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
|
|
|
|
namespace ContinuousTests
|
|
|
|
|
{
|
|
|
|
|
public class Configuration
|
|
|
|
|
{
|
|
|
|
|
public string LogPath { get; set; } = string.Empty;
|
|
|
|
|
public string[] CodexUrls { get; set; } = Array.Empty<string>();
|
2023-06-21 08:34:29 +00:00
|
|
|
|
public int SleepSecondsPerSingleTest { get; set; }
|
|
|
|
|
public int SleepSecondsPerAllTests { get; set; }
|
2023-06-21 09:01:48 +00:00
|
|
|
|
public bool KeepPassedTestLogs { get; set; }
|
2023-06-21 06:28:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ConfigLoader
|
|
|
|
|
{
|
|
|
|
|
private const string filename = "config.json";
|
|
|
|
|
|
|
|
|
|
public Configuration Load()
|
|
|
|
|
{
|
|
|
|
|
var config = Read();
|
2023-06-21 08:06:54 +00:00
|
|
|
|
|
2023-06-21 06:28:40 +00:00
|
|
|
|
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 { }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var logPath = Environment.GetEnvironmentVariable("LOGPATH");
|
|
|
|
|
var codexUrls = Environment.GetEnvironmentVariable("CODEXURLS");
|
2023-06-21 08:34:29 +00:00
|
|
|
|
var sleepPerSingle = Environment.GetEnvironmentVariable("SLEEPSECONDSPERSINGLETEST");
|
|
|
|
|
var sleepPerAll = Environment.GetEnvironmentVariable("SLEEPSECONDSPERALLTESTS");
|
2023-06-21 09:01:48 +00:00
|
|
|
|
var keep = Environment.GetEnvironmentVariable("KEEPPASSEDTESTLOGS");
|
2023-06-21 06:28:40 +00:00
|
|
|
|
|
2023-06-21 08:34:29 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(logPath) &&
|
|
|
|
|
!string.IsNullOrEmpty(codexUrls) &&
|
|
|
|
|
!string.IsNullOrEmpty(sleepPerSingle) &&
|
|
|
|
|
!string.IsNullOrEmpty(sleepPerAll))
|
2023-06-21 06:28:40 +00:00
|
|
|
|
{
|
|
|
|
|
var urls = codexUrls.Split(';', StringSplitOptions.RemoveEmptyEntries);
|
2023-06-21 08:34:29 +00:00
|
|
|
|
int secondsSingle;
|
|
|
|
|
int secondsAll;
|
|
|
|
|
if (int.TryParse(sleepPerSingle, out secondsSingle) && int.TryParse(sleepPerAll, out secondsAll))
|
2023-06-21 06:28:40 +00:00
|
|
|
|
{
|
|
|
|
|
if (urls.Length > 0)
|
|
|
|
|
{
|
2023-06-21 08:34:29 +00:00
|
|
|
|
return new Configuration
|
|
|
|
|
{
|
|
|
|
|
LogPath = logPath,
|
|
|
|
|
CodexUrls = urls,
|
|
|
|
|
SleepSecondsPerSingleTest = secondsSingle,
|
2023-06-21 09:01:48 +00:00
|
|
|
|
SleepSecondsPerAllTests = secondsAll,
|
|
|
|
|
KeepPassedTestLogs = keep == "1"
|
2023-06-21 08:34:29 +00:00
|
|
|
|
};
|
2023-06-21 06:28:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-21 08:34:29 +00:00
|
|
|
|
var nl = Environment.NewLine;
|
2023-06-21 06:28:40 +00:00
|
|
|
|
throw new Exception($"Unable to load configuration from '{filename}', and " +
|
2023-06-21 09:01:48 +00:00
|
|
|
|
"unable to load configuration from environment variables. " + nl +
|
|
|
|
|
"'LOGPATH' = Path where log files will be saved." + nl +
|
|
|
|
|
"'CODEXURLS' = Semi-colon separated URLs to codex APIs. e.g. 'https://hostaddr_one:port;https://hostaddr_two:port'" + 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);
|
2023-06-21 06:28:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Validate(Configuration configuration)
|
|
|
|
|
{
|
2023-06-21 08:34:29 +00:00
|
|
|
|
if (configuration.SleepSecondsPerSingleTest < 1)
|
2023-06-21 06:28:40 +00:00
|
|
|
|
{
|
2023-06-21 08:34:29 +00:00
|
|
|
|
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;
|
2023-06-21 06:28:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(configuration.LogPath))
|
|
|
|
|
{
|
|
|
|
|
throw new Exception($"Unvalid logpath set: '{configuration.LogPath}'");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!configuration.CodexUrls.Any())
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("No Codex URLs found.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|