Skip to content

Commit

Permalink
Disable cmdline check on nix (#12)
Browse files Browse the repository at this point in the history
Disable command line check on non-Windows
  • Loading branch information
badgeratu authored Jul 22, 2021
1 parent 2e26ed9 commit ee1044c
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 23 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ In order to target as many possible .NET platforms as possible with minimal exte

Dapr Sidekick also includes a package reference to [Newtonsoft Json.NET](https://github.com/JamesNK/Newtonsoft.Json) for parsing JSON log messages from Dapr.

## Non-Windows Platforms

On platforms other than Windows (such as Linux and Mac OS) some features of Dapr Sidekick may not be available due to the required native API calls not being available. These include:

| Feature | Platforms | Notes |
| --------------------------- | ------------ | ----------------------------------------------------------------- |
| Attach to existing instance | Linux/Mac OS | Will not detect existing `daprd` instance for same AppId and Port |

## Acknowledgements

Dapr Sidekick has been under active development at [Man Group](http://www.man.com/) since 2020.
Expand Down
1 change: 1 addition & 0 deletions all.sln
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{A321995D-B530-4CAD-B467-16A9A7F36C67}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
README.md = README.md
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ConsulSample", "ConsulSample", "{2DD56FCA-D5C3-4340-B5C5-27E284887A5F}"
Expand Down
9 changes: 8 additions & 1 deletion src/Man.Dapr.Sidekick/Native/NativeProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ public static extern IntPtr OpenProcess(

public static int GetCommandLine(System.Diagnostics.Process process, out string commandLine)
{
if (!DaprConstants.IsWindows)
{
// Return an empty command-line on non-Windows platforms
commandLine = string.Empty;
return 0;
}

var rc = 0;
commandLine = null;
var hProcess = OpenProcess(
Expand Down Expand Up @@ -174,7 +181,7 @@ public static int GetCommandLine(System.Diagnostics.Process process, out string

public static IEnumerable<string> CommandLineToArgs(string commandLine)
{
if (string.IsNullOrEmpty(commandLine))
if (string.IsNullOrEmpty(commandLine) || !DaprConstants.IsWindows)
{
return new string[0];
}
Expand Down
35 changes: 22 additions & 13 deletions src/Man.Dapr.Sidekick/Process/PortAvailabilityChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,31 @@ public int GetAvailablePort(int startingPort, IEnumerable<int> reservedPorts = n
throw new ArgumentException($"Starting Port cannot be greater than {ushort.MaxValue}", nameof(startingPort));
}

var ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
try
{
// IPGlobalProperties.GetIPGlobalProperties() is not implemented in some platforms (throws System.NotImplementedException).
// For those we cannot do automatic port assignment, so just return starting port.
var ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();

var connectionsEndpoints = ipGlobalProperties.GetActiveTcpConnections().Select(c => c.LocalEndPoint);
var tcpListenersEndpoints = ipGlobalProperties.GetActiveTcpListeners();
var udpListenersEndpoints = ipGlobalProperties.GetActiveUdpListeners();
var portsInUse = connectionsEndpoints.Concat(tcpListenersEndpoints)
.Concat(udpListenersEndpoints)
.Select(e => e.Port)
.ToList();
// Add any additional reserved ports
if (reservedPorts != null)
{
portsInUse.AddRange(reservedPorts);
}

var connectionsEndpoints = ipGlobalProperties.GetActiveTcpConnections().Select(c => c.LocalEndPoint);
var tcpListenersEndpoints = ipGlobalProperties.GetActiveTcpListeners();
var udpListenersEndpoints = ipGlobalProperties.GetActiveUdpListeners();
var portsInUse = connectionsEndpoints.Concat(tcpListenersEndpoints)
.Concat(udpListenersEndpoints)
.Select(e => e.Port)
.ToList();
// Add any additional reserved ports
if (reservedPorts != null)
return Enumerable.Range(startingPort, ushort.MaxValue - startingPort + 1).Except(portsInUse).FirstOrDefault();
}
catch
{
portsInUse.AddRange(reservedPorts);
return startingPort;
}

return Enumerable.Range(startingPort, ushort.MaxValue - startingPort + 1).Except(portsInUse).FirstOrDefault();
}
}
}
4 changes: 4 additions & 0 deletions tests/Man.Dapr.Sidekick.Tests/Man.Dapr.Sidekick.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@
<ProjectReference Include="..\..\src\Man.Dapr.Sidekick\Man.Dapr.Sidekick.csproj" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.10.0" />
</ItemGroup>

</Project>
4 changes: 1 addition & 3 deletions tests/Man.Dapr.Sidekick.Tests/Process/DaprProcessTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#if NETFRAMEWORK
using System.Linq;
using System.Linq;
using Man.Dapr.Sidekick.Logging;
using NSubstitute;
using NUnit.Framework;
Expand Down Expand Up @@ -273,4 +272,3 @@ public void Should_throw_exception_when_duplicate_process()
}
}
}
#endif
15 changes: 12 additions & 3 deletions tests/Man.Dapr.Sidekick.Tests/Process/ProcessCommandLineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,18 @@ public void Should_retrieve_commandline()
Assert.That(cmd, Is.Not.Null);
Assert.That(cmd.Process, Is.SameAs(process));

// Command line should always contain process EXE name
Assert.That(cmd.CommandLine, Is.Not.Null);
Assert.That(cmd.Arguments, Is.Not.Empty);
if (DaprConstants.IsWindows)
{
// Command line should always contain process EXE name
Assert.That(cmd.CommandLine, Is.Not.Null);
Assert.That(cmd.Arguments, Is.Not.Empty);
}
else
{
// On non-windows platforms everything is empty
Assert.That(cmd.CommandLine, Is.Empty);
Assert.That(cmd.Arguments, Is.Empty);
}

var arguments = cmd.GetArgumentsAsDictionary();
Assert.That(arguments, Is.Not.Null);
Expand Down
27 changes: 24 additions & 3 deletions tests/Man.Dapr.Sidekick.Tests/TestResourceHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System.IO;
#if !NETFRAMEWORK
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
#endif

namespace Man.Dapr.Sidekick
{
Expand All @@ -25,12 +29,17 @@ public static string GetResourceFileText(string resourceFile)
using var streamReader = new StreamReader(stream);
return streamReader.ReadToEnd();
}
#if NETFRAMEWORK

public static string CompileTestSystemProcessExe()
{
var filename = Path.ChangeExtension(Path.GetTempFileName(), ".exe");
var source = GetResourceFileText("ProcessProgram.cs");
var filename = Path.GetTempFileName();
if (DaprConstants.IsWindows)
{
filename = Path.ChangeExtension(filename, "exe");
}

#if NETFRAMEWORK
var provider = System.CodeDom.Compiler.CodeDomProvider.CreateProvider("CSharp");
var cp = new System.CodeDom.Compiler.CompilerParameters
{
Expand All @@ -39,10 +48,22 @@ public static string CompileTestSystemProcessExe()
GenerateInMemory = false
};
provider.CompileAssemblyFromSource(cp, source);
#else
var assemblyPath = Path.GetDirectoryName(typeof(object).Assembly.Location);
var syntaxTree = CSharpSyntaxTree.ParseText(source);
var compilation = CSharpCompilation
.Create(Path.GetFileName(filename))
.WithOptions(new CSharpCompilationOptions(OutputKind.ConsoleApplication))
.AddReferences(
MetadataReference.CreateFromFile(Path.Combine(assemblyPath, "System.Private.CoreLib.dll")),
MetadataReference.CreateFromFile(Path.Combine(assemblyPath, "System.Console.dll")),
MetadataReference.CreateFromFile(Path.Combine(assemblyPath, "System.Runtime.dll")))
.AddSyntaxTrees(syntaxTree);
var result = compilation.Emit(filename);
#endif

return filename;
}
#endif

public static void DeleteTestProcess(string filename, int waitMilliseconds = 2000)
{
Expand Down

0 comments on commit ee1044c

Please sign in to comment.