Skip to content

Commit

Permalink
feat(dotnet-sdk): read lh config options from file (#1043)
Browse files Browse the repository at this point in the history
Read options from file, refactor class to handle LH configuration variables and add unit tests
  • Loading branch information
KarlaCarvajal authored Oct 7, 2024
1 parent 1dc2c16 commit 8380256
Show file tree
Hide file tree
Showing 13 changed files with 396 additions and 213 deletions.
6 changes: 6 additions & 0 deletions sdk-dotnet/Examples/BasicExample/BasicExample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,10 @@
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
<PackageReference Include="LittleHorse.Sdk" Version="0.5.8-alpha" />
</ItemGroup>

<ItemGroup>
<Content Include=".config\**">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>
6 changes: 5 additions & 1 deletion sdk-dotnet/Examples/BasicExample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ private static void SetupApplication()
config.SetMinimumLevel(LogLevel.Debug);
})
.BuildServiceProvider();
}
}

static void Main(string[] args)
{
SetupApplication();
if (_serviceProvider != null)
{
var loggerFactory = _serviceProvider.GetRequiredService<ILoggerFactory>();
var config = new LHConfig(loggerFactory);
string filePath = Path.Combine(Directory.GetCurrentDirectory(), ".config/littlehorse.config");
if (File.Exists(filePath))
config = new LHConfig(filePath, loggerFactory);

MyWorker executable = new MyWorker();
var taskWorker = new LHTaskWorker<MyWorker>(executable, "greet-dotnet", config);
Expand Down
11 changes: 11 additions & 0 deletions sdk-dotnet/LittleHorse.Sdk.Tests/DefaultLHConfigVariables.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace LittleHorse.Sdk.Tests;

public static class DefaultLHConfigVariables
{
public static string LHC_API_HOST => "localhost";
public static int LHC_API_PORT => 2023;
public static string LHC_API_PROTOCOL => "PLAIN";
public static string LHC_CLIENT_ID => "client-";
public static int LHW_NUM_WORKER_THREADS => 8;
public static string LHW_TASK_WORKER_VERSION => string.Empty;
}
71 changes: 0 additions & 71 deletions sdk-dotnet/LittleHorse.Sdk.Tests/Internal/LHOptionsBinderTest.cs

This file was deleted.

36 changes: 0 additions & 36 deletions sdk-dotnet/LittleHorse.Sdk.Tests/LHConfigTest.cs

This file was deleted.

199 changes: 199 additions & 0 deletions sdk-dotnet/LittleHorse.Sdk.Tests/LHInputVariablesTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
using System;
using System.Collections.Generic;
using System.IO;
using Xunit;

namespace LittleHorse.Sdk.Tests.Internal
{
public class LHInputVariablesTest
{
private void CleanEnvironmentVariables()
{
Environment.SetEnvironmentVariable("LHC_API_HOST", null);
Environment.SetEnvironmentVariable("LHC_API_PORT", null);
Environment.SetEnvironmentVariable("LHC_API_PROTOCOL", null);
Environment.SetEnvironmentVariable("LHC_CLIENT_ID", null);
Environment.SetEnvironmentVariable("LHC_CA_CERT", null);
Environment.SetEnvironmentVariable("LHC_CLIENT_CERT", null);
Environment.SetEnvironmentVariable("LHC_CLIENT_KEY", null);
Environment.SetEnvironmentVariable("LHC_OAUTH_CLIENT_ID", null);
Environment.SetEnvironmentVariable("LHC_OAUTH_CLIENT_SECRET", null);
Environment.SetEnvironmentVariable("LHC_OAUTH_ACCESS_TOKEN_URL", null);
Environment.SetEnvironmentVariable("LHW_NUM_WORKER_THREADS", null);
Environment.SetEnvironmentVariable("LHW_TASK_WORKER_VERSION", null);
}

[Fact]
public void LHConfigVariables_WithLHEnvVariables_ShouldReturnSetOptions()
{
string host = "host";
string port = "254";
string protocol = "TLS";
string clientId = "client-id";
string caCert = "caCert";
string clientCert = "cert";
string clientKey = "client-key";
string oauthClientId = "oauth-client-id";
string oauthClientSecret = "oauth-client-secret";
string oauthAccessTokenUrl = "oauth-access-token-url";
string numberWorkerThreads = "15";
string taskWorkerVersion = "task-worker-version";

Environment.SetEnvironmentVariable("LHC_API_HOST", host);
Environment.SetEnvironmentVariable("LHC_API_PORT", port);
Environment.SetEnvironmentVariable("LHC_API_PROTOCOL", protocol);
Environment.SetEnvironmentVariable("LHC_CLIENT_ID", clientId);
Environment.SetEnvironmentVariable("LHC_CA_CERT", caCert);
Environment.SetEnvironmentVariable("LHC_CLIENT_CERT", clientCert);
Environment.SetEnvironmentVariable("LHC_CLIENT_KEY", clientKey);
Environment.SetEnvironmentVariable("LHC_OAUTH_CLIENT_ID", oauthClientId);
Environment.SetEnvironmentVariable("LHC_OAUTH_CLIENT_SECRET", oauthClientSecret);
Environment.SetEnvironmentVariable("LHC_OAUTH_ACCESS_TOKEN_URL", oauthAccessTokenUrl);
Environment.SetEnvironmentVariable("LHW_NUM_WORKER_THREADS", numberWorkerThreads);
Environment.SetEnvironmentVariable("LHW_TASK_WORKER_VERSION", taskWorkerVersion);

var inputVariables = new LHInputVariables();

Assert.Equal(host, inputVariables.LHC_API_HOST);
Assert.Equal(int.Parse(port), inputVariables.LHC_API_PORT);
Assert.Equal(protocol, inputVariables.LHC_API_PROTOCOL);
Assert.Equal(clientId, inputVariables.LHC_CLIENT_ID);
Assert.Equal(caCert, inputVariables.LHC_CA_CERT);
Assert.Equal(clientCert, inputVariables.LHC_CLIENT_CERT);
Assert.Equal(clientKey, inputVariables.LHC_CLIENT_KEY);
Assert.Equal(oauthClientId, inputVariables.LHC_OAUTH_CLIENT_ID);
Assert.Equal(oauthClientSecret, inputVariables.LHC_OAUTH_CLIENT_SECRET);
Assert.Equal(oauthAccessTokenUrl, inputVariables.LHC_OAUTH_ACCESS_TOKEN_URL);
Assert.Equal(int.Parse(numberWorkerThreads), inputVariables.LHW_NUM_WORKER_THREADS);
Assert.Equal(taskWorkerVersion, inputVariables.LHW_TASK_WORKER_VERSION);

CleanEnvironmentVariables();
}

[Fact]
public void LHConfigVariables_WithoutLHEnvVariables_ShouldReturnDefaultOptions()
{
var inputVariables = new LHInputVariables();

Assert.Equal(DefaultLHConfigVariables.LHC_API_HOST, inputVariables.LHC_API_HOST);
Assert.Equal(DefaultLHConfigVariables.LHC_API_PORT, inputVariables.LHC_API_PORT);
Assert.Equal(DefaultLHConfigVariables.LHC_API_PROTOCOL, inputVariables.LHC_API_PROTOCOL);
Assert.StartsWith(DefaultLHConfigVariables.LHC_CLIENT_ID, inputVariables.LHC_CLIENT_ID);
Assert.Equal(DefaultLHConfigVariables.LHW_NUM_WORKER_THREADS, inputVariables.LHW_NUM_WORKER_THREADS);
Assert.Equal(DefaultLHConfigVariables.LHW_TASK_WORKER_VERSION, inputVariables.LHW_TASK_WORKER_VERSION);
}

[Fact]
public void LHConfigVariables_WithLHOptionsInFile_ShouldReturnSetOptions()
{
const string lhConfigFileName = "littlehorse.config";
string inputVariablesFilePath = TestUtils.BuildFilePath(lhConfigFileName);
var keyValueLHConfigs = new Dictionary<string, string>
{
{ "LHC_API_HOST", "host-test" },
{ "LHC_API_PORT", "111" },
{ "LHC_API_PROTOCOL", "TLS" },
{ "LHC_CA_CERT", "ca_file_path" },
{ "LHW_TASK_WORKER_VERSION", "test" }
};
TestUtils.WriteContentInFile(keyValueLHConfigs, inputVariablesFilePath);

var inputVariables = new LHInputVariables(inputVariablesFilePath);

Assert.Equal(keyValueLHConfigs["LHC_API_HOST"], inputVariables.LHC_API_HOST);
Assert.Equal(int.Parse(keyValueLHConfigs["LHC_API_PORT"]), inputVariables.LHC_API_PORT);
Assert.Equal(keyValueLHConfigs["LHC_API_PROTOCOL"], inputVariables.LHC_API_PROTOCOL);
Assert.Equal(keyValueLHConfigs["LHC_CA_CERT"], inputVariables.LHC_CA_CERT);
Assert.Equal(keyValueLHConfigs["LHW_TASK_WORKER_VERSION"], inputVariables.LHW_TASK_WORKER_VERSION);
}

[Fact]
public void LHConfigVariables_WithNoLHOptionsInFile_ShouldReturnDefaultOptions()
{
const string lhConfigFileName = "littlehorse.config";
string inputVariablesFilePath = TestUtils.BuildFilePath(lhConfigFileName);
var keyValueLHConfigs = new Dictionary<string, string>
{
{ "API_HOST", "host" },
{ "API_PORT", "111" },
{ "API_PROTOCOL", "TLS" },
{ "CA_CERT", "ca-path" }
};
TestUtils.WriteContentInFile(keyValueLHConfigs, inputVariablesFilePath);

var inputVariables = new LHInputVariables(inputVariablesFilePath);

Assert.Equal(DefaultLHConfigVariables.LHC_API_HOST, inputVariables.LHC_API_HOST);
Assert.Equal(DefaultLHConfigVariables.LHC_API_PORT, inputVariables.LHC_API_PORT);
Assert.Equal(DefaultLHConfigVariables.LHC_API_PROTOCOL, inputVariables.LHC_API_PROTOCOL);
Assert.StartsWith(DefaultLHConfigVariables.LHC_CLIENT_ID, inputVariables.LHC_CLIENT_ID);
Assert.Equal(DefaultLHConfigVariables.LHW_NUM_WORKER_THREADS, inputVariables.LHW_NUM_WORKER_THREADS);
Assert.Equal(DefaultLHConfigVariables.LHW_TASK_WORKER_VERSION, inputVariables.LHW_TASK_WORKER_VERSION);
}

[Fact]
public void LHConfigVariables_WithEmptyLHFile_ShouldReturnDefaultOptions()
{
const string lhConfigFileName = "littlehorse.config";
string inputVariablesFilePath = TestUtils.BuildFilePath(lhConfigFileName);
var keyValueLHConfigs = new Dictionary<string, string>();
TestUtils.WriteContentInFile(keyValueLHConfigs, inputVariablesFilePath);

var inputVariables = new LHInputVariables(inputVariablesFilePath);

Assert.Equal(DefaultLHConfigVariables.LHC_API_HOST, inputVariables.LHC_API_HOST);
Assert.Equal(DefaultLHConfigVariables.LHC_API_PORT, inputVariables.LHC_API_PORT);
Assert.Equal(DefaultLHConfigVariables.LHC_API_PROTOCOL, inputVariables.LHC_API_PROTOCOL);
Assert.StartsWith(DefaultLHConfigVariables.LHC_CLIENT_ID, inputVariables.LHC_CLIENT_ID);
Assert.Equal(DefaultLHConfigVariables.LHW_NUM_WORKER_THREADS, inputVariables.LHW_NUM_WORKER_THREADS);
Assert.Equal(DefaultLHConfigVariables.LHW_TASK_WORKER_VERSION, inputVariables.LHW_TASK_WORKER_VERSION);
}

[Fact]
public void LHConfigVariables_WithSomeLHOptionsCommentedInFile_ShouldReturnSetOptions()
{
const string lhConfigFileName = "littlehorse.config";
string inputVariablesFilePath = TestUtils.BuildFilePath(lhConfigFileName);
var keyValueLHConfigs = new Dictionary<string, string>
{
{ "LHC_API_HOST", "host" },
{ "LHC_API_PORT", "111" },
{ "#LHC_API_PROTOCOL", "TLS" },
{ "#LHC_CA_CERT", "ca-path" }
};
TestUtils.WriteContentInFile(keyValueLHConfigs, inputVariablesFilePath);

var inputVariables = new LHInputVariables(inputVariablesFilePath);

Assert.Equal(keyValueLHConfigs["LHC_API_HOST"], inputVariables.LHC_API_HOST);
Assert.Equal(int.Parse(keyValueLHConfigs["LHC_API_PORT"]), inputVariables.LHC_API_PORT);
Assert.Equal(DefaultLHConfigVariables.LHC_API_PROTOCOL, inputVariables.LHC_API_PROTOCOL);
Assert.Null(inputVariables.LHC_CA_CERT);
Assert.Equal(string.Empty, inputVariables.LHW_TASK_WORKER_VERSION);
}

[Fact]
public void LHConfigVariables_WithAllLHOptionsCommentedInFile_ShouldReturnDefaultOptions()
{
const string lhConfigFileName = "littlehorse.config";
string inputVariablesFilePath = TestUtils.BuildFilePath(lhConfigFileName);
var keyValueLHConfigs = new Dictionary<string, string>
{
{ "#LHC_API_HOST", "host" },
{ "#LHC_API_PORT", "111" },
{ "#LHC_API_PROTOCOL", "TLS" },
{ "#LHC_CLIENT_ID", "TEST_CLIENT-ID" }
};
TestUtils.WriteContentInFile(keyValueLHConfigs, inputVariablesFilePath);

var inputVariables = new LHInputVariables(inputVariablesFilePath);

Assert.Equal(DefaultLHConfigVariables.LHC_API_HOST, inputVariables.LHC_API_HOST);
Assert.Equal(DefaultLHConfigVariables.LHC_API_PORT, inputVariables.LHC_API_PORT);
Assert.Equal(DefaultLHConfigVariables.LHC_API_PROTOCOL, inputVariables.LHC_API_PROTOCOL);
Assert.StartsWith(DefaultLHConfigVariables.LHC_CLIENT_ID, inputVariables.LHC_CLIENT_ID);
Assert.Equal(DefaultLHConfigVariables.LHW_NUM_WORKER_THREADS, inputVariables.LHW_NUM_WORKER_THREADS);
Assert.Equal(DefaultLHConfigVariables.LHW_TASK_WORKER_VERSION, inputVariables.LHW_TASK_WORKER_VERSION);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
31 changes: 31 additions & 0 deletions sdk-dotnet/LittleHorse.Sdk.Tests/TestUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Collections.Generic;
using System.IO;

namespace LittleHorse.Sdk.Tests;

public static class TestUtils
{
public static void WriteContentInFile(Dictionary<string, string> content, string fileName)
{
using (StreamWriter writer = new StreamWriter(fileName))
{
foreach (var kvp in content)
{
writer.WriteLine($"{kvp.Key}={kvp.Value}");
}

writer.Flush();
writer.Close();
}
}

public static string BuildFilePath(string fileName)
{
string parentDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Resources");
string subDirectory = Path.Combine(parentDirectory, "tmp");
Directory.CreateDirectory(subDirectory);
string filePath = Path.Combine(subDirectory, fileName);

return filePath;
}
}
Loading

0 comments on commit 8380256

Please sign in to comment.