Skip to content

Commit

Permalink
fix: refactored hostname provider for vpn
Browse files Browse the repository at this point in the history
  • Loading branch information
kulbachnyi.v committed Dec 29, 2023
1 parent c306ac4 commit 6457632
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 23 deletions.
68 changes: 55 additions & 13 deletions Vostok.ServiceDiscovery.Tests/ReplicaInfoBuilder_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,14 @@ public void Should_be_configurable()
.SetCommitHash("ASDF")
.SetReleaseDate("released now")
.SetDependencies(new List<string> {"dep-a", "dep-b"})
.SetTags(new TagCollection{"tag1", {"tag2", "value"}})
.SetHostnameProvider(useFqdn => "newHostName"), false);
.SetTags(new TagCollection {"tag1", {"tag2", "value"}})
.SetVpnHostnameProvider(() => "newHostname"),
false);

info.ReplicaInfo.Environment.Should().Be("custom-environment");
info.ReplicaInfo.Application.Should().Be("Vostok.App.1");
info.ReplicaInfo.Replica.Should().Be("https://github.com:123/vostok");
info.Tags.Should().BeEquivalentTo(new TagCollection{"tag1", {"tag2", "value"}});
info.Tags.Should().BeEquivalentTo(new TagCollection {"tag1", {"tag2", "value"}});

var properties = info.ReplicaInfo.Properties;

Expand All @@ -84,20 +85,24 @@ public void Should_be_configurable()
properties[ReplicaInfoKeys.ReleaseDate].Should().Be("released now");
properties[ReplicaInfoKeys.Dependencies].Should().Be("dep-a;dep-b");

properties[ReplicaInfoKeys.Host].Should().Be("newHostName");
properties[ReplicaInfoKeys.Host].Should().Be("newHostname");
}

[Test]
public void Should_build_url_from_parts()
[TestCase(null)]
[TestCase("newhostname")]
[TestCase("")]
public void Should_build_url_from_parts(string hostname)
{
var info = ReplicaInfoBuilder.Build(
setup => setup
.SetScheme("https")
.SetPort(123)
.SetUrlPath("vostok"), false)
setup => setup
.SetScheme("https")
.SetPort(123)
.SetUrlPath("vostok")
.SetVpnHostnameProvider(() => hostname),
false)
.ReplicaInfo;

var host = EnvironmentInfo.Host.ToLowerInvariant();
var host = string.IsNullOrEmpty(hostname) ? EnvironmentInfo.Host.ToLowerInvariant() : hostname;

info.Replica.Should().Be($"https://{host}:123/vostok");

Expand Down Expand Up @@ -151,7 +156,8 @@ public void Should_add_properties()
{
builder.SetProperty("key1", "value1");
builder.SetProperty("key2", "value2");
}, false);
},
false);

var properties = info.ReplicaInfo.Properties;
properties["key1"].Should().Be("value1");
Expand All @@ -166,7 +172,8 @@ public void Should_rewrite_properties()
{
builder.SetProperty("key", "value1");
builder.SetProperty("key", "value2");
}, false);
},
false);

var properties = info.ReplicaInfo.Properties;
properties["key"].Should().Be("value2");
Expand All @@ -182,5 +189,40 @@ public void Should_rewrite_default_properties()
info.ReplicaInfo.Replica.Should().Be($"{host}({Process.GetCurrentProcess().Id})");
info.ReplicaInfo.Properties[ReplicaInfoKeys.Replica].Should().Be("value");
}

[Test]
public void Should_not_configure_hostname_when_uri_set()
{
var url = new UriBuilder
{
Scheme = "https",
Host = "github.com",
Port = 123,
Path = "vostok"
}.Uri;

var info = ReplicaInfoBuilder.Build(
setup => setup
.SetUrl(url)
.SetVpnHostnameProvider(() => "newHostname"),
false
);

info.ReplicaInfo.Replica.Should().Be("https://github.com:123/vostok");
}

[Test]
public void Should_configure_hostname_when_uri_not_set()
{
var info = ReplicaInfoBuilder.Build(
setup => setup
.SetUrl(null)
.SetPort(123)
.SetVpnHostnameProvider(() => "newhostname"),
false
);

info.ReplicaInfo.Replica.Should().Be("http://newhostname:123/");
}
}
}
2 changes: 1 addition & 1 deletion Vostok.ServiceDiscovery/IReplicaInfoBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public interface IReplicaInfoBuilder
/// <para>Default value: <c>null</c></para>
/// <para>Should not be called in conjunction with <see cref="SetUrl"/>.</para>
/// </summary>
IReplicaInfoBuilder SetHostnameProvider([CanBeNull] Func<bool, string> hostNameProvider);
IReplicaInfoBuilder SetVpnHostnameProvider([CanBeNull] Func<string> vpnHostnameProvider);

/// <summary>
/// <para>Sets build commit hash.</para>
Expand Down
2 changes: 1 addition & 1 deletion Vostok.ServiceDiscovery/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Vostok.ServiceDiscovery.IReplicaInfoBuilder.SetHostnameProvider(System.Func<bool, string> hostNameProvider) -> Vostok.ServiceDiscovery.IReplicaInfoBuilder
Vostok.ServiceDiscovery.IReplicaInfoBuilder.SetVpnHostnameProvider(System.Func<string> vpnHostnameProvider) -> Vostok.ServiceDiscovery.IReplicaInfoBuilder
15 changes: 7 additions & 8 deletions Vostok.ServiceDiscovery/ReplicaInfoBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,12 @@ internal class ReplicaInfoBuilder : IReplicaInfoBuilder
private List<string> dependencies;
private TagCollection tags;

private bool useFQDN;
private Func<bool, string> hostNameProvider;
private Func<string> vpnHostnameProvider;

private ReplicaInfoBuilder(bool useFQDN)
{
environment = "default";
application = EnvironmentInfo.Application;
this.useFQDN = useFQDN;
host = useFQDN ? EnvironmentInfo.FQDN : EnvironmentInfo.Host;
processName = EnvironmentInfo.ProcessName;
processId = EnvironmentInfo.ProcessId;
Expand All @@ -61,11 +59,12 @@ public static ServiceBeaconInfo Build(ReplicaInfoSetup setup, bool useFQDN)

public ServiceBeaconInfo Build()
{
if (hostNameProvider != null)
var vpnHostname = vpnHostnameProvider.Invoke();
if (!string.IsNullOrEmpty(vpnHostname))
{
host = hostNameProvider(useFQDN);
host = vpnHostname;
}

url ??= BuildUrl();
replica ??= url.ToString();

Expand Down Expand Up @@ -173,9 +172,9 @@ public IReplicaInfoBuilder SetUrlPath(string path)
return this;
}

public IReplicaInfoBuilder SetHostnameProvider(Func<bool, string> hostNameProvider)
public IReplicaInfoBuilder SetVpnHostnameProvider(Func<string> vpnHostnameProvider)
{
this.hostNameProvider = hostNameProvider;
this.vpnHostnameProvider = vpnHostnameProvider;
return this;
}

Expand Down

0 comments on commit 6457632

Please sign in to comment.