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

102 lines
3.3 KiB
C#
Raw Normal View History

2023-06-22 08:17:12 +00:00
using DistTestCore;
2023-06-22 08:33:21 +00:00
using DistTestCore.Codex;
2023-06-22 08:17:12 +00:00
namespace CodexNetDeployer
2023-06-22 07:51:25 +00:00
{
public class Configuration
{
public Configuration(
string codexImage,
string gethImage,
string contractsImage,
string kubeConfigFile,
string kubeNamespace,
int? numberOfCodexNodes,
2023-06-22 13:58:18 +00:00
int? numberOfValidators,
2023-06-22 08:17:12 +00:00
int? storageQuota,
2023-06-22 08:33:21 +00:00
CodexLogLevel codexLogLevel,
2023-06-22 08:17:12 +00:00
TestRunnerLocation runnerLocation)
2023-06-22 07:51:25 +00:00
{
CodexImage = codexImage;
GethImage = gethImage;
ContractsImage = contractsImage;
KubeConfigFile = kubeConfigFile;
KubeNamespace = kubeNamespace;
NumberOfCodexNodes = numberOfCodexNodes;
2023-06-22 13:58:18 +00:00
NumberOfValidators = numberOfValidators;
2023-06-22 07:51:25 +00:00
StorageQuota = storageQuota;
2023-06-22 08:17:12 +00:00
CodexLogLevel = codexLogLevel;
RunnerLocation = runnerLocation;
2023-06-22 07:51:25 +00:00
}
public string CodexImage { get; }
public string GethImage { get; }
public string ContractsImage { get; }
public string KubeConfigFile { get; }
public string KubeNamespace { get; }
public int? NumberOfCodexNodes { get; }
2023-06-22 13:58:18 +00:00
public int? NumberOfValidators { get; }
2023-06-22 07:51:25 +00:00
public int? StorageQuota { get; }
2023-06-22 08:33:21 +00:00
public CodexLogLevel CodexLogLevel { get; }
2023-06-22 08:17:12 +00:00
public TestRunnerLocation RunnerLocation { get; }
2023-06-22 07:51:25 +00:00
public void PrintConfig()
{
ForEachProperty(onString: Print, onInt: Print);
}
public List<string> Validate()
{
var errors = new List<string>();
ForEachProperty(
onString: (n, v) => StringIsSet(n, v, errors),
onInt: (n, v) => IntIsOverZero(n, v, errors));
2023-06-22 13:58:18 +00:00
if (NumberOfValidators > NumberOfCodexNodes)
{
errors.Add($"{nameof(NumberOfValidators)} ({NumberOfValidators}) may not be greater than {nameof(NumberOfCodexNodes)} ({NumberOfCodexNodes}).");
}
2023-06-22 07:51:25 +00:00
return errors;
}
private void ForEachProperty(Action<string, string> onString, Action<string, int?> onInt)
{
var properties = GetType().GetProperties();
foreach (var p in properties)
{
if (p.PropertyType == typeof(string)) onString(p.Name, (string)p.GetValue(this)!);
if (p.PropertyType == typeof(int?)) onInt(p.Name, (int?)p.GetValue(this)!);
}
}
private static void IntIsOverZero(string variable, int? value, List<string> errors)
{
if (value == null || value.Value < 1)
{
errors.Add($"{variable} is must be set and must be greater than 0.");
}
}
private static void StringIsSet(string variable, string value, List<string> errors)
{
if (string.IsNullOrWhiteSpace(value))
{
errors.Add($"{variable} is must be set.");
}
}
private static void Print(string variable, string value)
{
Console.WriteLine($"\t{variable}: '{value}'");
}
private static void Print(string variable, int? value)
{
if (value != null) Print(variable, value.ToString()!);
else Print(variable, "<NONE>");
}
}
}