Skip to content

Commit

Permalink
Add saved objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Jandini committed Oct 16, 2023
1 parent 5c16747 commit edce124
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 29 deletions.
7 changes: 6 additions & 1 deletion src/KiBoards.Xunit/Services/KiBoardsTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public KiBoardsTestRunner(IMessageSink messageSink)
Variables = new Dictionary<string, string>()
};

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

foreach (DictionaryEntry entry in Environment.GetEnvironmentVariables())
Expand Down Expand Up @@ -74,11 +75,15 @@ private void Startup(Assembly assembly, IMessageSink messageSink)
{
try
{


var startup = assembly.GetCustomAttribute<TestStartupAttribute>();
Type type = assembly.GetType(startup.ClassName);

if (type != null)
{
messageSink.WriteMessage($"Invoking {type.FullName}");

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)
Expand Down
11 changes: 11 additions & 0 deletions src/KiBoards/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Xunit.Abstractions;
using Xunit.Sdk;

namespace KiBoards
{
internal static class Extensions
{
public static void WriteMessage(this IMessageSink messageSink, string message) => messageSink.OnMessage(new DiagnosticMessage(message));
public static void WriteException(this IMessageSink messageSink, Exception exception) => messageSink.OnMessage(new DiagnosticMessage("{0}\n{1}", exception.Message, exception.StackTrace));
}
}
6 changes: 6 additions & 0 deletions src/KiBoards/KiBoards.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,10 @@
<ItemGroup>
<ProjectReference Include="..\KiBoards.Xunit\KiBoards.Xunit.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="KiBoards.ndjson">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
3 changes: 3 additions & 0 deletions src/KiBoards/KiBoards.ndjson
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"attributes":{"fieldAttrs":"{\"id.keyword\":{\"count\":2},\"summary.time\":{\"customLabel\":\"Time\",\"count\":2},\"context.version\":{\"count\":2},\"id\":{\"customLabel\":\"Id\",\"count\":5},\"summary.failed\":{\"customLabel\":\"Failed\",\"count\":2},\"summary.total\":{\"customLabel\":\"Total\",\"count\":2},\"userName\":{\"customLabel\":\"User\",\"count\":1},\"summary.skipped\":{\"customLabel\":\"Skipped\",\"count\":2},\"name\":{\"customLabel\":\"Name\",\"count\":3},\"summary.passed\":{\"customLabel\":\"Passed\",\"count\":1},\"hash\":{\"customLabel\":\"Hash\",\"count\":1},\"machineName\":{\"customLabel\":\"Machine\",\"count\":1},\"startTime\":{\"customLabel\":\"Started\"},\"status\":{\"customLabel\":\"Status\"}}","fieldFormatMap":"{\"summary.time\":{\"id\":\"duration\",\"params\":{\"outputFormat\":\"humanizePrecise\"}},\"status\":{\"id\":\"color\",\"params\":{\"parsedUrl\":{\"origin\":\"http://localhost:5601\",\"pathname\":\"/app/management/kibana/objects\",\"basePath\":\"\"},\"fieldType\":\"string\",\"colors\":[{\"range\":\"-Infinity:Infinity\",\"regex\":\"Failed\",\"text\":\"#E7664C\",\"background\":\"#ffffff\"},{\"range\":\"-Infinity:Infinity\",\"regex\":\"Passed\",\"text\":\"#54B399\",\"background\":\"#ffffff\"}]}},\"startTime\":{\"id\":\"date\"}}","fields":"[]","name":"Runs","runtimeFieldMap":"{}","sourceFilters":"[]","timeFieldName":"startTime","title":"*testruns*","typeMeta":"{}"},"coreMigrationVersion":"8.6.2","created_at":"2023-10-13T18:34:18.231Z","id":"ac6ead8c-f3af-455a-a126-ec584b688cd9","migrationVersion":{"index-pattern":"8.0.0"},"references":[],"type":"index-pattern","updated_at":"2023-10-16T10:35:25.850Z","version":"WzI3OTEsMl0="}
{"attributes":{"columns":["id","name","status","summary.time","summary.total","summary.passed","summary.failed","summary.skipped","userName","machineName","hash"],"description":"","grid":{"columns":{"context.version":{"width":204},"id":{"width":299},"machineName":{"width":100},"name":{"width":128},"startTime":{"width":210},"status":{"width":100},"summary.failed":{"width":111},"summary.passed":{"width":100},"summary.skipped":{"width":84},"summary.time":{"width":211},"summary.total":{"width":125}}},"hideChart":false,"isTextBasedQuery":false,"kibanaSavedObjectMeta":{"searchSourceJSON":"{\"query\":{\"query\":\"\",\"language\":\"kuery\"},\"filter\":[],\"indexRefName\":\"kibanaSavedObjectMeta.searchSourceJSON.index\"}"},"sort":[["startTime","desc"],["summary.failed","asc"]],"timeRestore":false,"title":"Test Runs","usesAdHocDataView":false},"coreMigrationVersion":"8.6.2","created_at":"2023-10-14T11:36:26.113Z","id":"e8a11710-6a85-11ee-a384-fbd389354190","migrationVersion":{"search":"8.0.0"},"references":[{"id":"ac6ead8c-f3af-455a-a126-ec584b688cd9","name":"kibanaSavedObjectMeta.searchSourceJSON.index","type":"index-pattern"}],"type":"search","updated_at":"2023-10-16T10:35:58.831Z","version":"WzI3OTQsMl0="}
{"excludedObjects":[],"excludedObjectsCount":0,"exportedCount":2,"missingRefCount":0,"missingReferences":[]}
5 changes: 5 additions & 0 deletions src/KiBoards/KiBoardsAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[AttributeUsage(AttributeTargets.Assembly)]
public class KiBoardsAttribute : Attribute
{

}
8 changes: 8 additions & 0 deletions src/KiBoards/Models/Objects/ImportSavedObjectsResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace KiBoards.Models.Objects
{
class ImportObjectsResponse
{
public int SuccessCount { get; set; }
public bool Success { get; set; }
}
}
42 changes: 20 additions & 22 deletions src/KiBoards/Services/KiBoardsKibanaClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Net.Http.Json;
using System.Text.Json;
using KiBoards.Models.Objects;
using KiBoards.Models.Settings;
using KiBoards.Models.Status;

Expand All @@ -23,30 +25,26 @@ public async Task SetDarkModeAsync(bool darkMode, CancellationToken cancellation
}


public async Task<KibanaStatusResponse> GetStatus(CancellationToken cancellationToken) => await _httpClient.GetFromJsonAsync<KibanaStatusResponse>("api/status", cancellationToken);

public async Task<ImportObjectsResponse> ImportSavedObjectsAsync(string ndjsonFile) => await ImportSavedObjectsAsync(ndjsonFile, false, CancellationToken.None);
public async Task<ImportObjectsResponse> ImportSavedObjectsAsync(string ndjsonFile, bool overwrite) => await ImportSavedObjectsAsync(ndjsonFile, overwrite, CancellationToken.None);
public async Task<ImportObjectsResponse> ImportSavedObjectsAsync(string ndjsonFile, bool overwrite, CancellationToken cancellationToken)
{
var multipartContent = new MultipartFormDataContent();
var streamContent = new StreamContent(File.Open(ndjsonFile, FileMode.Open));
multipartContent.Add(streamContent, "file", ndjsonFile);
var response = await _httpClient.PostAsync($"/api/saved_objects/_import?overwrite={overwrite.ToString().ToLower()}", multipartContent);

response.EnsureSuccessStatusCode();
var result = await response.Content.ReadFromJsonAsync<ImportObjectsResponse>(new JsonSerializerOptions() { PropertyNameCaseInsensitive = true }, cancellationToken);

public async Task WaitForKibanaAsync(CancellationToken cancellationToken, int retryDelay = 5000)
{
while (!cancellationToken.IsCancellationRequested)
{
try
{
var response = await GetStatus(cancellationToken);

string level = response?.Status?.Overall?.Level ?? throw new Exception("Kibana status is not available.");

if (level != "available")
throw new Exception("Kibana not available.");

break;
}
catch (Exception)
{
await Task.Delay(retryDelay, cancellationToken);
}
}
return result;
}



public async Task<KibanaStatusResponse> GetStatus() => await GetStatus(CancellationToken.None);
public async Task<KibanaStatusResponse> GetStatus(CancellationToken cancellationToken) => await _httpClient.GetFromJsonAsync<KibanaStatusResponse>("api/status", cancellationToken);


}
}
39 changes: 33 additions & 6 deletions src/KiBoards/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace KiBoards
{
public class Startup
{
{
static void WriteMessage(IMessageSink messageSink, string message)
{
messageSink.OnMessage(new DiagnosticMessage(message));
Expand All @@ -21,12 +21,39 @@ public Startup(IMessageSink messageSink)
var kibanaUri = new Uri(Environment.GetEnvironmentVariable("KIB_KIBANA_HOST") ?? "http://localhost:5601");
var kibanaClient = new KiBoardsKibanaClient(kibanaUri, httpClient);

WriteMessage(messageSink, $"Waiting for Kibana {kibanaUri}");
messageSink.WriteMessage($"Waiting for Kibana {kibanaUri}");

await kibanaClient.WaitForKibanaAsync(CancellationToken.None);
await kibanaClient.SetDarkModeAsync(true, CancellationToken.None);
});

while (true)
{
try
{
var response = await kibanaClient.GetStatus(CancellationToken.None);

string level = response?.Status?.Overall?.Level ?? throw new Exception("Kibana status is not available.");

if (level != "available")
throw new Exception("Kibana not available.");

messageSink.WriteMessage($"Kibana status: {level}");
break;
}
catch (Exception ex)
{
messageSink.WriteMessage(ex.Message);
await Task.Delay(5000);
}
}

var ndjsonFiles = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.ndjson");

messageSink.WriteMessage($"Found {ndjsonFiles.Length} ndjson file(s)");

foreach (var ndjsonFile in ndjsonFiles) {
WriteMessage(messageSink, $"Imporing {ndjsonFile}");
var results = await kibanaClient.ImportSavedObjectsAsync(ndjsonFile);
WriteMessage(messageSink, $"Import {results.Success} {results.SuccessCount}");
}
});
}
}
}
1 change: 1 addition & 0 deletions src/SimpleTest/Simple_Must.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[assembly: KiBoards()]
[assembly: TestFramework("KiBoards.TestFramework", "KiBoards.Xunit")]
[assembly: TestStartup("SimpleTest.Startup")]

Expand Down

0 comments on commit edce124

Please sign in to comment.