Test project: Enable netcore3.1 testing, switch win64 Snap7 and use custom port.

- Add netcoreapp3.1 target framework, allowing this to run on linux and macos as well.
- Switch windows snap7 DLL to 64bit version 1.4.2. This also improves UnitTest stability (reduces false positives) on the CI (including appveyor)
- Changing the port used for S7NetTests when communicating with Snap7 to a value > 1000 allows tests to run on Linux without elevated privileges.
This commit is contained in:
Serge Camille
2020-09-13 10:01:38 +02:00
parent 12a2e3c0b1
commit 9198fc1686
7 changed files with 34 additions and 17 deletions

View File

@@ -29,7 +29,7 @@ namespace S7.Net.UnitTest.Helpers
Console.WriteLine(Server.EventText(ref Event)); Console.WriteLine(Server.EventText(ref Event));
} }
public static void Start() public static void Start(short port)
{ {
Server = new S7Server(); Server = new S7Server();
// Share some resources with our virtual PLC // Share some resources with our virtual PLC
@@ -59,7 +59,14 @@ namespace S7.Net.UnitTest.Helpers
// Start the server onto the default adapter. // Start the server onto the default adapter.
// To select an adapter we have to use Server->StartTo("192.168.x.y"). // To select an adapter we have to use Server->StartTo("192.168.x.y").
// Start() is the same of StartTo("0.0.0.0"); // Start() is the same of StartTo("0.0.0.0");
Server.SetParam(S7Consts.p_u16_LocalPort, ref port);
int Error = Server.Start(); int Error = Server.Start();
if (Error != 0)
{
throw new Exception($"Error starting Snap7 server: {Server.ErrorText(Error)}");
}
//if (Error == 0) //if (Error == 0)
//{ //{
// // Now the server is running ... wait a key to terminate // // Now the server is running ... wait a key to terminate

View File

@@ -1,15 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>net452</TargetFrameworks> <TargetFrameworks>net452;netcoreapp3.1</TargetFrameworks>
<SignAssembly>true</SignAssembly> <SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>Properties\S7.Net.snk</AssemblyOriginatorKeyFile> <AssemblyOriginatorKeyFile>Properties\S7.Net.snk</AssemblyOriginatorKeyFile>
<IsPackable>false</IsPackable> <IsPackable>false</IsPackable>
<Copyright>Copyright © 2014</Copyright> <Copyright>Copyright © 2014</Copyright>
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="GitHubActionsTestLogger" Version="1.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.0" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.2" /> <PackageReference Include="MSTest.TestAdapter" Version="2.1.2" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.2" /> <PackageReference Include="MSTest.TestFramework" Version="2.1.2" />
@@ -23,14 +25,13 @@
<ProjectReference Include="..\S7.Net\S7.Net.csproj" /> <ProjectReference Include="..\S7.Net\S7.Net.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup Condition="'$(TargetFramework)'=='net452'">
<Reference Include="System.ServiceProcess" /> <Reference Include="System.ServiceProcess" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Update="snap7.dll"> <None Update="runtimes\win-x64\native\snap7.dll" Link="snap7.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None> </None>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -6,10 +6,12 @@ using Microsoft.VisualStudio.TestTools.UnitTesting;
using S7.Net; using S7.Net;
using S7.Net.UnitTest.Helpers; using S7.Net.UnitTest.Helpers;
using S7.Net.UnitTest; using S7.Net.UnitTest;
using System.ServiceProcess;
using S7.Net.Types; using S7.Net.Types;
using S7.UnitTest.Helpers; using S7.UnitTest.Helpers;
using System.Threading.Tasks; using System.Threading.Tasks;
#if NETFRAMEWORK
using System.ServiceProcess;
#endif
using System.Threading; using System.Threading;
#endregion #endregion

View File

@@ -6,9 +6,11 @@ using Microsoft.VisualStudio.TestTools.UnitTesting;
using S7.Net; using S7.Net;
using S7.Net.UnitTest.Helpers; using S7.Net.UnitTest.Helpers;
using S7.Net.UnitTest; using S7.Net.UnitTest;
using System.ServiceProcess;
using S7.Net.Types; using S7.Net.Types;
using S7.UnitTest.Helpers; using S7.UnitTest.Helpers;
#if NETFRAMEWORK
using System.ServiceProcess;
#endif
#endregion #endregion
@@ -41,6 +43,7 @@ namespace S7.Net.UnitTest
#region Constants #region Constants
const int DB2 = 2; const int DB2 = 2;
const int DB4 = 4; const int DB4 = 4;
const short TestServerPort = 31122;
#endregion #endregion
#region Private fields #region Private fields
@@ -53,16 +56,21 @@ namespace S7.Net.UnitTest
/// </summary> /// </summary>
public S7NetTests() public S7NetTests()
{ {
plc = new Plc(CpuType.S7300, "127.0.0.1", 0, 2); plc = CreatePlc();
//ConsoleManager.Show(); //ConsoleManager.Show();
ShutDownServiceS7oiehsx64(); ShutDownServiceS7oiehsx64();
} }
private static Plc CreatePlc()
{
return new Plc(CpuType.S7300, "localhost", TestServerPort, 0, 2);
}
[TestInitialize] [TestInitialize]
public void Setup() public void Setup()
{ {
S7TestServer.Start(); S7TestServer.Start(TestServerPort);
plc.Open(); plc.Open();
} }
@@ -931,9 +939,9 @@ namespace S7.Net.UnitTest
{ {
plc.Close(); plc.Close();
S7TestServer.Stop(); S7TestServer.Stop();
S7TestServer.Start(); S7TestServer.Start(TestServerPort);
var reachablePlc = new Plc(CpuType.S7300, "127.0.0.1", 0, 2); var reachablePlc = CreatePlc();
Assert.IsTrue(reachablePlc.IsAvailable); Assert.IsTrue(reachablePlc.IsAvailable);
} }
@@ -1028,6 +1036,7 @@ namespace S7.Net.UnitTest
#region Private methods #region Private methods
private static void ShutDownServiceS7oiehsx64() private static void ShutDownServiceS7oiehsx64()
{ {
#if NETFRAMEWORK
ServiceController[] services = ServiceController.GetServices(); ServiceController[] services = ServiceController.GetServices();
var service = services.FirstOrDefault(s => s.ServiceName == "s7oiehsx64"); var service = services.FirstOrDefault(s => s.ServiceName == "s7oiehsx64");
if (service != null) if (service != null)
@@ -1037,6 +1046,7 @@ namespace S7.Net.UnitTest
service.Stop(); service.Stop();
} }
} }
#endif
} }
#region IDisposable Support #region IDisposable Support

View File

@@ -36,11 +36,8 @@ namespace Snap7
public class S7Consts public class S7Consts
{ {
#if __MonoCS__ // Assuming that we are using Unix release of Mono (otherwise modify it) public const string Snap7LibName = "snap7";
public const string Snap7LibName = "libsnap7.so";
#else
public const string Snap7LibName = "snap7.dll";
#endif
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// PARAMS LIST // PARAMS LIST
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------

Binary file not shown.

Binary file not shown.