Skip to content

Handle logging arguments in provider's getEngineAddress #536

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/unreleased/bug-fixes-536.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
component: sdk/provider
kind: bug-fixes
body: Handle logging arguments in provider's getEngineAddress
time: 2025-03-10T11:10:52.170215+01:00
custom:
PR: "536"
84 changes: 84 additions & 0 deletions sdk/Pulumi.Tests/Provider/ProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,88 @@ public async Task NotImplementedErrorIncludesName()
Assert.Contains("GetSchema", exc.Message);
}
}

public class ProviderEngineAddressTests
{
private static string? GetEngineAddress(string[] args)
{
// Helper method to access the private static method for testing
var method = typeof(Pulumi.Experimental.Provider.Provider).GetMethod(
"GetEngineAddress",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);

try
{
return (string?)method?.Invoke(null, new object[] { args });
}
catch (System.Reflection.TargetInvocationException ex)
{
if (ex.InnerException != null)
throw ex.InnerException;
throw;
}
}

[Fact]
public void GetEngineAddress_WithValidAddress_ReturnsAddress()
{
var args = new[] { "127.0.0.1:51776" };
var address = GetEngineAddress(args);
Assert.Equal("127.0.0.1:51776", address);
}

[Fact]
public void GetEngineAddress_WithLoggingArgs_ReturnsAddress()
{
var args = new[] {
"--logtostderr",
"-v=3",
"127.0.0.1:51776",
"--logflow"
};
var address = GetEngineAddress(args);
Assert.Equal("127.0.0.1:51776", address);
}

[Fact]
public void GetEngineAddress_WithTracingArg_ReturnsAddress()
{
var args = new[] {
"--tracing",
"1",
"127.0.0.1:51776"
};
var address = GetEngineAddress(args);
Assert.Equal("127.0.0.1:51776", address);
}

[Fact]
public void GetEngineAddress_WithNoArgs_ReturnsNull()
{
var args = Array.Empty<string>();
var address = GetEngineAddress(args);
Assert.Null(address);
}

[Fact]
public void GetEngineAddress_WithOnlyLoggingArgs_ReturnsNull()
{
var args = new[] { "--logtostderr", "-v=3", "--logflow" };
var address = GetEngineAddress(args);
Assert.Null(address);
}

[Fact]
public void GetEngineAddress_WithMultipleAddresses_ThrowsException()
{
var args = new[] {
"127.0.0.1:51776",
"127.0.0.1:51777"
};
var ex = Assert.Throws<ArgumentException>(() => GetEngineAddress(args));
Assert.Equal(
"Expected at most one engine address argument, but got 2 non-logging arguments",
ex.Message);
}
}
}
41 changes: 39 additions & 2 deletions sdk/Pulumi/Provider/Provider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,8 @@ internal static Microsoft.Extensions.Hosting.IHost BuildHost(
// maxRpcMessageSize raises the gRPC Max message size from `4194304` (4mb) to `419430400` (400mb)
var maxRpcMessageSize = 400 * 1024 * 1024;

var engineAddress = GetEngineAddress(args);

return Host.CreateDefaultBuilder()
.ConfigureWebHostDefaults(webBuilder =>
{
Expand All @@ -560,9 +562,9 @@ internal static Microsoft.Extensions.Hosting.IHost BuildHost(
config.Sources.Clear();

var memConfig = new Dictionary<string, string?>();
if (args.Length > 0)
if (engineAddress != null)
{
memConfig.Add("Host", args[0]);
memConfig.Add("Host", engineAddress);
}
if (version != null)
{
Expand Down Expand Up @@ -600,6 +602,41 @@ internal static Microsoft.Extensions.Hosting.IHost BuildHost(
})
.Build();
}

private static string? GetEngineAddress(string[] args)
{
var cleanArgs = new List<string>();

for (int i = 0; i < args.Length; i++)
{
var arg = args[i];

// Skip logging-related arguments
if (arg == "--logtostderr") continue;
if (arg.StartsWith("-v=")) continue;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can also be passed like "-v10" without an =

if (arg == "--logflow") continue;
if (arg == "--tracing")
{
i++; // Skip the tracing value
continue;
}

cleanArgs.Add(arg);
}

if (cleanArgs.Count == 0)
{
return null;
}

if (cleanArgs.Count > 1)
{
throw new ArgumentException(
$"Expected at most one engine address argument, but got {cleanArgs.Count} non-logging arguments");
}

return cleanArgs[0];
}
}

class ResourceProviderService : ResourceProvider.ResourceProviderBase, IDisposable
Expand Down
Loading