Skip to content

Commit

Permalink
Return info about exposed endpoints from InstallMethod
Browse files Browse the repository at this point in the history
  • Loading branch information
cezarypiatek committed May 31, 2023
1 parent 0b41a20 commit 245a370
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 17 deletions.
7 changes: 4 additions & 3 deletions src/GrpcTestKit/GrpcTestKit.Demo/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

namespace GrpcTestKit.Demo
{

[Explicit]
public class Tests
{
[Test]
Expand Down Expand Up @@ -65,10 +67,9 @@ await grpcMockClient.MockClientStreaming
[Test]
public async Task test_with_testcharts()
{
await using var connector = new TestChartGrpcMockServerConnector("protos", grpcPort: 8889);

await using var connector = new TestChartGrpcMockServerConnector("protos");

await connector.Install();
var connectionInfo = await connector.Install();

var grpcMockClient = connector.CreateClient();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class TestContainerGrpcMockServerConnector : IGrpcMockServerConnector
private readonly string _dockerImage;
private readonly int _grpcPort;
private IContainer? container;
private string? _stubbingUrl;

public TestContainerGrpcMockServerConnector(string protoDirectory, int grpcPort = 5033, string dockerImage = "cezarypiatek/grpc-mock-server")
{
Expand All @@ -29,7 +30,7 @@ public TestContainerGrpcMockServerConnector(string protoDirectory, int grpcPort
_grpcPort = grpcPort;
}

public async Task Install()
public async Task<GrpcMockServerConnectionInfo> Install()
{
container = new ContainerBuilder()
.WithImage(_dockerImage)
Expand All @@ -43,29 +44,29 @@ public async Task Install()
.WithBindMount(_protoDirectory, "/protos")
.WithAutoRemove(true)
.WithCleanUp(false)

.Build();
var st = Stopwatch.StartNew();
await container.StartAsync();
st.Stop();
Console.WriteLine($"Container startup time: {st.Elapsed}");

var wireMockPort = container.GetMappedPublicPort("9095");
this._stubbingUrl = $"http://localhost:{wireMockPort}";
return new GrpcMockServerConnectionInfo($"http://localhost:{_grpcPort}", _stubbingUrl);
}

public IGrpcMockClient CreateClient()
{
if (container == null)
if (_stubbingUrl == null)
{
throw new InvalidOperationException("Connector not installed. Call Install() method first.");
}

var wireMockPort = container.GetMappedPublicPort("9095");
var baseUrl = $"http://localhost:{wireMockPort}";
var wireMockApiClient = RestClient.For<IWireMockAdminApi>(baseUrl);
return new GrpcMockClient(wireMockApiClient, baseUrl);
var wireMockApiClient = RestClient.For<IWireMockAdminApi>(_stubbingUrl);
return new GrpcMockClient(wireMockApiClient, _stubbingUrl);
}



public async ValueTask DisposeAsync()
{
if (container != null)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace GrpcTestKit.TestConnectors;

public class GrpcMockServerConnectionInfo
{
public string GrpcEndpoint { get; }
public string StubbingEndpoint { get; }

public GrpcMockServerConnectionInfo(string grpcEndpoint, string stubbingEndpoint)
{
GrpcEndpoint = grpcEndpoint;
StubbingEndpoint = stubbingEndpoint;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace GrpcTestKit.TestConnectors;

public interface IGrpcMockServerConnector: System.IAsyncDisposable
public interface IGrpcMockServerConnector: IAsyncDisposable
{
Task Install();
Task<GrpcMockServerConnectionInfo> Install();
IGrpcMockClient CreateClient();
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,32 @@ public class TestChartGrpcMockServerConnector : IGrpcMockServerConnector
private readonly string _protoDirectory;
private readonly int _grpcPort;
private readonly string _dockerImage;
private readonly bool _exposeGrpcPortOnLocalhost;
private readonly string _releaseName;
private readonly ChartInstaller _chartInstaller;
private readonly ChartFromLocalPath _chart;
private Release? _release;
private int _wireMockPort;

public TestChartGrpcMockServerConnector(string protoDirectory, int grpcPort = 5033, string dockerImage = "cezarypiatek/grpc-mock-server", string? releaseName = null)
public TestChartGrpcMockServerConnector(string protoDirectory, int grpcPort = 5033, string dockerImage = "cezarypiatek/grpc-mock-server", string? releaseName = null, bool exposeGrpcPortOnLocalhost = false)
{
_protoDirectory = Path.GetFullPath(protoDirectory);
_grpcPort = grpcPort;
_dockerImage = dockerImage;
_exposeGrpcPortOnLocalhost = exposeGrpcPortOnLocalhost;
_releaseName = releaseName ?? "grpcmockserverconnector";
_chartInstaller = new ChartInstaller(new ProcessLauncher(new ConsoleProcessOutputWriter()));
_chart = new ChartFromLocalPath("./charts/grpcmockserver");
}

public async Task Install()
public async Task<GrpcMockServerConnectionInfo> Install()
{
var allProtoFile = Directory.EnumerateFiles(_protoDirectory, "*.proto", SearchOption.AllDirectories);

var overrides = new
{
dockerImage = _dockerImage,
grpcPort = _grpcPort,
protoFiles = allProtoFile.Select(x => new
{
key = Guid.NewGuid().ToString("N"),
Expand All @@ -41,8 +44,16 @@ public async Task Install()
};

_release = await _chartInstaller.Install(_chart, _releaseName, overrides);
_ = await _release.StartPortForwardForService(serviceName: $"{_releaseName}-grpcmockserver-service", servicePort: 5033, localPort: _grpcPort);
if (_exposeGrpcPortOnLocalhost)
{
_ = await _release.StartPortForwardForService(serviceName: $"{_releaseName}-grpcmockserver-service", servicePort: _grpcPort, localPort: _grpcPort);
}
_wireMockPort = await _release.StartPortForwardForService(serviceName: $"{_releaseName}-grpcmockserver-service", servicePort: 9095);
return new GrpcMockServerConnectionInfo
(
grpcEndpoint: _exposeGrpcPortOnLocalhost ? $"http://127.0.0.1:{_grpcPort}": $"http://{_releaseName}-grpcmockserver-service:{_grpcPort}" ,
stubbingEndpoint: $"http://127.0.0.1:{ _wireMockPort}"
);
}

public IGrpcMockClient CreateClient()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ spec:
app: {{.Release.Name}}-grpcmockserver
ports:
- name: grpcport
port: 5033
port: {{ .Values.grpcPort | default 5033}}
targetPort: 5033
protocol: TCP
- name: wiremockport
Expand Down

0 comments on commit 245a370

Please sign in to comment.