Skip to content
This repository has been archived by the owner on Jun 1, 2024. It is now read-only.

Commit

Permalink
Read Elasticsearch server version from a root page response (#502)
Browse files Browse the repository at this point in the history
Co-authored-by: Nenad Vicentic <vicentic@immounited.com>
  • Loading branch information
nenadvicentic and Nenad Vicentic authored Jan 28, 2023
1 parent 578d5cb commit a537c60
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,18 @@ public Version EffectiveVersion
{
try
{
var response = _client.Cat.Nodes<StringResponse>(new CatNodesRequestParameters()
{
Headers = new[] { "v" }
});
var response = _client.DoRequest<DynamicResponse>(HttpMethod.GET, "/");
if (!response.Success) return null;

var discoveredVersion = response.Body.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
.FirstOrDefault();
var discoveredVersion = response.Dictionary["version"]["number"];

if (discoveredVersion == null)
if (!discoveredVersion.HasValue)
return null;

return new Version(discoveredVersion);
if (discoveredVersion.Value is not string strVersion)
return null;

return new Version(strVersion);

}
catch (Exception ex)
Expand Down
13 changes: 7 additions & 6 deletions test/Serilog.Sinks.Elasticsearch.Tests/ElasticsearchSinkTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Elasticsearch.Net;
using Serilog.Sinks.Elasticsearch.Tests.Stubs;
using System.Text;
using Xunit;

Expand All @@ -18,7 +19,7 @@ public void Ctor_DetectElasticsearchVersionSetToTrue_SetsTypeName(string elastic
/* ARRANGE */
var options = new ElasticsearchSinkOptions
{
Connection = FakeResponse(elasticVersion),
Connection = FakeProductCheckResponse(elasticVersion),
TypeName = configuredTypeName
};

Expand All @@ -41,7 +42,7 @@ public void Ctor_DetectElasticsearchVersionSetToFalseAssumesVersion7_SetsTypeNam
/* ARRANGE */
var options = new ElasticsearchSinkOptions
{
Connection = FakeResponse(elasticVersion),
Connection = FakeProductCheckResponse(elasticVersion),
DetectElasticsearchVersion = false,
TypeName = configuredTypeName
};
Expand All @@ -65,7 +66,7 @@ public void CreateLogger_DetectElasticsearchVersionSetToTrue_SetsTypeName(string
/* ARRANGE */
var options = new ElasticsearchSinkOptions
{
Connection = FakeResponse(elasticVersion),
Connection = FakeProductCheckResponse(elasticVersion),
DetectElasticsearchVersion = true,
TypeName = configuredTypeName
};
Expand All @@ -83,10 +84,10 @@ public void CreateLogger_DetectElasticsearchVersionSetToTrue_SetsTypeName(string
Assert.Equal(expectedTypeName, options.TypeName);
}

private static IConnection FakeResponse(string responseText)
private static IConnection FakeProductCheckResponse(string responseText)
{
byte[] responseBody = Encoding.UTF8.GetBytes(responseText);
return new InMemoryConnection(responseBody, contentType: "text/plain; charset=UTF-8");
var productCheckResponse = ConnectionStub.ModifiedProductCheckResponse(responseText);
return new InMemoryConnection(productCheckResponse);
}
}
}
27 changes: 21 additions & 6 deletions test/Serilog.Sinks.Elasticsearch.Tests/Stubs/ConnectionStub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public override TReturn Request<TReturn>(RequestData requestData)
$" to be productCheck pre-flight request");

_productCheckDone = true;
return ReturnConnectionStatus<TReturn>(requestData); // root page returned
return ReturnConnectionStatus<TReturn>(requestData); // hard-coded root page returned
}

byte[] responseBytes = Array.Empty<byte>();
Expand All @@ -66,6 +66,7 @@ public override TReturn Request<TReturn>(RequestData requestData)

int responseStatusCode = 200;
string contentType = null;
InMemoryHttpResponse productCheckResponse = null;

switch (requestData.Method)
{
Expand All @@ -79,13 +80,10 @@ public override TReturn Request<TReturn>(RequestData requestData)
switch (requestData.Uri.PathAndQuery.ToLower())
{
case "/":
// ReturnConnectionStatus(...) call at the bottom will return dummy product page
// when root "/" is requested.
productCheckResponse = ModifiedProductCheckResponse(_productVersion);
break;
case "/_cat/nodes":
case "/_cat/nodes?h=v":
responseBytes = Encoding.UTF8.GetBytes(_productVersion);
responseStatusCode = 200;
contentType = "text/plain; charset=UTF-8";
break;
}
Expand All @@ -100,12 +98,29 @@ public override TReturn Request<TReturn>(RequestData requestData)
break;
}

return ReturnConnectionStatus<TReturn>(requestData, responseBytes, responseStatusCode, contentType);
return ReturnConnectionStatus<TReturn>(requestData, productCheckResponse, responseBytes, responseStatusCode, contentType);
}

public override Task<TResponse> RequestAsync<TResponse>(RequestData requestData, CancellationToken cancellationToken)
{
return Task.FromResult(Request<TResponse>(requestData));
}

public static InMemoryHttpResponse ModifiedProductCheckResponse(string productVersion)
{
var productCheckResponse = ValidProductCheckResponse();
if (productVersion is not null)
{
using var originalMemoryStream = new MemoryStream(productCheckResponse.ResponseBytes, false);
{
var json = LowLevelRequestResponseSerializer.Instance.Deserialize<dynamic>(originalMemoryStream);
json["version"]["number"] = productVersion;
using var modifiedMemoryStream = new MemoryStream();
LowLevelRequestResponseSerializer.Instance.Serialize(json, modifiedMemoryStream);
productCheckResponse.ResponseBytes = modifiedMemoryStream.ToArray();
}
}
return productCheckResponse;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public DiscoverVersionTests()
public void TemplatePutToCorrectUrl()
{
var uri = _templateGet.Item1;
uri.AbsolutePath.Should().Be("/_cat/nodes");
uri.AbsolutePath.Should().Be("/");
}
}
}

0 comments on commit a537c60

Please sign in to comment.