Skip to content

Commit

Permalink
Add startup class call and KIB_VAR_ environment variables to non stat…
Browse files Browse the repository at this point in the history
…ic test run
  • Loading branch information
Jandini committed Oct 15, 2023
1 parent 870a87b commit a1b01be
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 28 deletions.
5 changes: 2 additions & 3 deletions src/KiBoards.Xunit/Services/KiBoardsElasticClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Nest;
using Xunit.Abstractions;
using Xunit.Sdk;

namespace KiBoards.Services
{
Expand All @@ -22,12 +21,12 @@ public async Task IndexDocumentAsync<T>(T document) where T: class

if (!result.IsValid)
{
_messageSink.OnMessage(new DiagnosticMessage(result.DebugInformation));
_messageSink.WriteMessage(result.DebugInformation);
}
}
catch (Exception ex)
{
_messageSink.OnMessage(new DiagnosticMessage(ex.Message));
_messageSink.WriteException(ex);
}
}
}
Expand Down
121 changes: 97 additions & 24 deletions src/KiBoards.Xunit/Services/KiBoardsTestRunner.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using KiBoards.Models;
using Nest;
using System.Collections;
using System.Reflection;
using Xunit.Abstractions;
using Xunit.Sdk;
Expand All @@ -9,41 +10,107 @@ namespace KiBoards.Services
internal class KiBoardsTestRunner
{
private readonly KiBoardsElasticClient _elasticService;
private readonly KiBoardsTestRun _testRun;

public string Version { get; private set; }

public KiBoardsTestRunner(IMessageSink messageSink)
{
var uriString = Environment.GetEnvironmentVariable("KIB_ELASTICSEARCH_HOST") ?? "http://localhost:9200";
var connectionSettings = new ConnectionSettings(new Uri(uriString));

var elasticClient = new ElasticClient(connectionSettings
.DefaultMappingFor<TestRun>(m => m
.IndexName($"kiboards-testruns-{DateTime.UtcNow:yyyy-MM}")
.IdProperty(p => p.Id))
.DefaultMappingFor<KiBoardsTestCaseRun>(m => m
.IndexName($"kiboards-testcases-{DateTime.UtcNow:yyyy-MM}"))

.MaxRetryTimeout(TimeSpan.FromMinutes(5))
.EnableApiVersioningHeader()
.MaximumRetries(3));


_elasticService = new KiBoardsElasticClient(elasticClient, messageSink);

var version = Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion;
messageSink.OnMessage(new DiagnosticMessage($"KiBoards.Xunit {version} logging to {uriString}"));
try
{
_testRun = new KiBoardsTestRun()
{
Id = Guid.NewGuid().ToString(),
StartTime = DateTime.UtcNow,
MachineName = Environment.MachineName,
UserName = Environment.UserName,
Variables = new Dictionary<string, string>()
};

foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies().Where(a => a.GetCustomAttribute<TestStartupAttribute>() != null))
Startup(assembly, messageSink);

foreach (DictionaryEntry entry in Environment.GetEnvironmentVariables())
{
var name = entry.Key.ToString();
var value = entry.Value.ToString();

const string prefix = "KIB_VAR_";

if (name.Length > prefix.Length + 1 && name.StartsWith(prefix, StringComparison.OrdinalIgnoreCase) && !string.IsNullOrEmpty(value))
_testRun.Variables.TryAdd(name[prefix.Length..], value);
}

var uriString = Environment.GetEnvironmentVariable("KIB_ELASTICSEARCH_HOST") ?? "http://localhost:9200";
var connectionSettings = new ConnectionSettings(new Uri(uriString));

var elasticClient = new ElasticClient(connectionSettings
.DefaultMappingFor<KiBoardsTestRun>(m => m
.IndexName($"kiboards-testruns-{DateTime.UtcNow:yyyy-MM}")
.IdProperty(p => p.Id))
.DefaultMappingFor<KiBoardsTestCaseRun>(m => m
.IndexName($"kiboards-testcases-{DateTime.UtcNow:yyyy-MM}"))

.MaxRetryTimeout(TimeSpan.FromMinutes(5))
.EnableApiVersioningHeader()
.MaximumRetries(3));

_elasticService = new KiBoardsElasticClient(elasticClient, messageSink);

Version = Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion;
messageSink.WriteMessage($"KiBoards.Xunit {Version} logging to {uriString}");
}
catch (Exception ex)
{
messageSink.WriteException(ex);
}
}


private void Startup(Assembly assembly, IMessageSink messageSink)
{
try
{
var startup = assembly.GetCustomAttribute<TestStartupAttribute>();
Type type = assembly.GetType(startup.ClassName);

if (type != null)
{
if (type.GetConstructor(new Type[] { typeof(string), typeof(IMessageSink) }) != null)
Activator.CreateInstance(type, _testRun.Id, messageSink);
else if (type.GetConstructor(new Type[] { typeof(string) }) != null)
Activator.CreateInstance(type, _testRun.Id);
else if (type.GetConstructor(new Type[] { typeof(IMessageSink) }) != null)
Activator.CreateInstance(type, messageSink);
else if (type.GetConstructor(new Type[] { }) != null)
Activator.CreateInstance(type);
}
}
catch (Exception ex)
{
messageSink.WriteException(ex);
}
}

public async Task IndexTestRunAsync(TestRun testRun)
public async Task IndexTestRunAsync(RunSummary summary)
{
await _elasticService.IndexDocumentAsync(testRun);
_testRun.Summary = new KiBoardsTestRunSummary()
{
Total = summary.Total,
Failed = summary.Failed,
Skipped = summary.Skipped,
Time = summary.Time,
};

await _elasticService.IndexDocumentAsync(_testRun);
}

public async Task IndexTestCaseRunAsync(ITestResultMessage testResult)
{
await _elasticService.IndexDocumentAsync(new KiBoardsTestCaseRun()
{
Id = (TestFramework.TestRun.Id + testResult.TestCase.UniqueID).ComputeMD5(),
TestRun = TestFramework.TestRun,
Id = (_testRun.Id + testResult.TestCase.UniqueID).ComputeMD5(),
TestRun = _testRun,
ExecutionTime = testResult.ExecutionTime,
Output = testResult.Output,
Failed = testResult is ITestFailed failed ? new KiBoardsTestCaseRunFailed()
Expand Down Expand Up @@ -78,6 +145,12 @@ await _elasticService.IndexDocumentAsync(new KiBoardsTestCaseRun()
Skipped = testResult is ITestSkipped skipped ? new KiBoardsTestCaseRunSkipped() { Reason = skipped.Reason } : null,
Status = testResult is ITestPassed ? "Passed" : testResult is ITestFailed ? "Failed" : testResult is ITestSkipped ? "Skipped" : "Other"
});
}
}

internal void AddTestCases(IEnumerable<IXunitTestCase> testCases)
{
_testRun.Name = string.Join(",", testCases.Select(a => Path.GetFileNameWithoutExtension(a.TestMethod.TestClass.Class.Assembly.AssemblyPath)).Distinct());
_testRun.Hash = string.Join(",", testCases.OrderBy(a => a.UniqueID).Select(a => a.UniqueID)).ComputeMD5();
}
}
}
10 changes: 10 additions & 0 deletions src/KiBoards.Xunit/TestStartupAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[AttributeUsage(AttributeTargets.Assembly)]
public class TestStartupAttribute : Attribute
{
public string ClassName { get; set; }

public TestStartupAttribute(string className)
{
ClassName = className;
}
}
6 changes: 5 additions & 1 deletion src/SimpleTest/SimpleTest.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
Expand All @@ -22,4 +22,8 @@
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\KiBoards.Xunit\KiBoards.Xunit.csproj" />
</ItemGroup>

</Project>
7 changes: 7 additions & 0 deletions src/SimpleTest/Simple_Must.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[assembly: TestFramework("KiBoards.TestFramework", "KiBoards.Xunit")]
[assembly: TestStartup("SimpleTest.Startup")]

namespace SimpleTest
{
Expand All @@ -12,5 +13,11 @@ public void Not_DivideByZero(int a, int b)

Assert.Equal(0, a);
}

[Fact]
public void Pass()
{
Assert.Equal(0, 0);
}
}
}
13 changes: 13 additions & 0 deletions src/SimpleTest/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Reflection;
using Xunit.Abstractions;

namespace SimpleTest
{
public class Startup
{
public Startup(IMessageSink messageSink)
{
Environment.SetEnvironmentVariable("KIB_VAR_VERSION", Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion);
}
}
}

0 comments on commit a1b01be

Please sign in to comment.