Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
migrated server integration tests to 2.0
- Loading branch information
Showing
13 changed files
with
347 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
"cSpell.words": [ | ||
"ASMNAME", | ||
"HMACSHA", | ||
"Integ", | ||
"Requ", | ||
"Shhh", | ||
"asms", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...ugDSC.Server-tests/BasicPullHandlerRoot/Configuration/SHARED/FYI/StaticTestConfig.dsc.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Configuration StaticTestConfig { | ||
|
||
Node StaticTestConfig { | ||
File TempDir { | ||
Ensure = 'Present' | ||
Type = 'Directory' | ||
DestinationPath = 'c:\temp' | ||
} | ||
|
||
File TempFile { | ||
DependsOn = "[File]TempDir" | ||
Ensure = 'Present' | ||
Type = 'File' | ||
DestinationPath = 'c:\temp\dsc-statictestconfig-file.txt' | ||
Contents = 'STATIC TEST CONFIG' | ||
} | ||
} | ||
} | ||
|
||
StaticTestConfig | ||
|
||
Publish-MOFToPullServer -PullServerWebConfig C:\DscService\WebSite\web.config -FullName .\StaticTestConfig\StaticTestConfig.mof -Verbose |
1 change: 1 addition & 0 deletions
1
....Server-tests/BasicPullHandlerRoot/Configuration/SHARED/FYI/StaticTestConfig.mof.checksum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
75D3763E4FFE0ED9CFE17F9EE915F1B81FE06E9B6D2287B09561565D61D1009C |
Binary file added
BIN
+3.02 KB
...server/TugDSC.Server-tests/BasicPullHandlerRoot/Configuration/SHARED/StaticTestConfig.mof
Binary file not shown.
Binary file added
BIN
+324 KB
...TugDSC.Server-tests/BasicPullHandlerRoot/Modules/xPSDesiredStateConfiguration/5.1.0.0.zip
Binary file not shown.
1 change: 1 addition & 0 deletions
1
...-tests/BasicPullHandlerRoot/Modules/xPSDesiredStateConfiguration/FYI/5.1.0.0.zip.checksum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
8FB19218B06E212F1FF251B6F8DE0B261DF3806F0FFD545090E3265A481A9716 |
1 change: 1 addition & 0 deletions
1
test/server/TugDSC.Server-tests/BasicPullHandlerRoot/RegKeys/RegistrationKeys.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
c3ea5066-ce5a-4d12-a42a-850be287b2d8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System; | ||
|
||
namespace TugDSC.Server.Providers | ||
{ | ||
public class IntegTestDscHandler : BasicDscHandler | ||
{ | ||
public IntegTestDscHandler() | ||
{ | ||
Console.WriteLine("CONSTRUCTING INTEG-TEST HANDLER"); | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
test/server/TugDSC.Server-tests/IntegTestDscHandlerProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System; | ||
using Microsoft.Extensions.Logging; | ||
using TugDSC.Ext; | ||
using TugDSC.Server.Util; | ||
|
||
namespace TugDSC.Server.Providers | ||
{ | ||
public class IntegTestDscHandlerProvider : BasicDscHandlerProvider | ||
{ | ||
private static readonly ProviderInfo MY_INFO = new ProviderInfo("integTest"); | ||
|
||
public override ProviderInfo Describe() => MY_INFO; | ||
|
||
public IntegTestDscHandlerProvider( | ||
ILogger<IntegTestDscHandlerProvider> pLogger, | ||
ILogger<IntegTestDscHandler> hLogger, | ||
ChecksumAlgorithmManager checksumManager, | ||
ChecksumHelper checksumHelper) | ||
: base(pLogger, hLogger, checksumManager, checksumHelper) | ||
{ | ||
Console.WriteLine("CONSTRUCTING INTEG-TEST HANDLER PROVIDER"); | ||
} | ||
|
||
protected override BasicDscHandler ConstructHandler() | ||
{ | ||
return new IntegTestDscHandler(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Reflection; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.TestHost; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using TugDSC.Client; | ||
using TugDSC.Client.CLIApp.Configuration; | ||
using TugDSC.Configuration; | ||
using TugDSC.Server.WebAppHost; | ||
using TugDSC.Testing.MSTest; | ||
|
||
namespace TugDSC.Server | ||
{ | ||
/// <summary> | ||
/// Set of <see cref="https://docs.microsoft.com/en-us/aspnet/core/testing/integration-testing" | ||
/// >Integration Tests</see> used to validate the Tug Server's protocol compliance. | ||
/// </summary> | ||
/// <remarks> | ||
/// We use the special support of the ASP.NET Core TestHost to setup a closed loop | ||
/// HTTP client/server test environment that runs in-process and in-memory and we | ||
/// use the Tug.Client to drive the tests and validate the expected protocol | ||
/// behaviors and responses. | ||
/// </remarks> | ||
[TestClass] | ||
public class ServerProtocolTests | ||
{ | ||
// We us a static Agent ID by default, but can be overridden when building the client config | ||
public const string DEFAULT_AGENT_ID = "12345678-0000-0000-0000-000000000001"; | ||
|
||
// When using the ASP.NET Core TestHost, only the URL Path is significant | ||
public const string DEFAULT_SERVER_URL = "http://localhost/"; | ||
|
||
// This is coordinated with the RegKey defined in the Basic Pull Handler's root | ||
public const string DEFAULT_REG_KEY = "c3ea5066-ce5a-4d12-a42a-850be287b2d8"; | ||
|
||
static TestServer _tugServer; | ||
static DscPullConfig _defaultConfig; | ||
static DscPullClient _defaultClient; | ||
|
||
[ClassInitialize] | ||
public static void Init(TestContext ctx) | ||
{ | ||
var myPath = typeof(ServerProtocolTests).GetTypeInfo().Assembly.Location; | ||
var myDir = Path.GetDirectoryName(myPath); | ||
Directory.SetCurrentDirectory(myDir); | ||
|
||
var hostBuilder = new WebHostBuilder() | ||
// .ConfigureLogging((hostCtx, logBuilder) => { | ||
|
||
// var x = logBuilder.Services.GetService<ILoggerFactory>(); | ||
// }) | ||
// .UseLoggerFactory(AppLog.Factory) | ||
.UseStartup<Startup>(); | ||
|
||
_tugServer = new TestServer(hostBuilder); | ||
|
||
_defaultConfig = BuildConfig(); | ||
_defaultClient = BuildClient(); | ||
} | ||
|
||
[TestMethod] | ||
public void TestRegisterDscAgent() | ||
{ | ||
_defaultClient.RegisterDscAgent().Wait(); | ||
} | ||
|
||
[TestMethod] | ||
public void TestRegisterDscAgent_BadContent_AgentInfo() | ||
{ | ||
var c = BuildConfig(); | ||
c.AgentInformation["foo"] = "bar"; | ||
|
||
Assert.That.ThrowsWhen<AggregateException>( | ||
condition: (ex) => | ||
ex.InnerException is HttpRequestException | ||
&& ex.InnerException.Message.Contains( | ||
"Response status code does not indicate success: 400 (Bad Request)"), | ||
action: () => | ||
BuildClient(c).RegisterDscAgent().Wait(), | ||
message: | ||
"Throws HTTP exception for unauthorized (401)"); | ||
} | ||
|
||
[TestMethod] | ||
public void TestRegisterDscAgent_BadContent_CertInfo() | ||
{ | ||
var c = BuildConfig(); | ||
c.CertificateInformation["foo"] = "bar"; | ||
|
||
Assert.That.ThrowsWhen<AggregateException>( | ||
condition: (ex) => | ||
ex.InnerException is HttpRequestException | ||
&& ex.InnerException.Message.Contains( | ||
"Response status code does not indicate success: 400 (Bad Request)"), | ||
action: () => | ||
BuildClient(c).RegisterDscAgent().Wait(), | ||
message: | ||
"Throws HTTP exception for unauthorized (401)"); | ||
} | ||
|
||
[TestMethod] | ||
public void TestGetDscAction() | ||
{ | ||
var actions = _defaultClient.GetDscAction().Result; | ||
Assert.IsNotNull(actions, "Actions are not missing or null"); | ||
|
||
var actionsArr = actions.ToArray(); | ||
Assert.AreEqual(1, actionsArr.Length, "Exactly 1 action response"); | ||
|
||
Assert.AreEqual(_defaultConfig.ConfigurationNames.First(), actionsArr[0].ConfigurationName, | ||
"Expected configuration name"); | ||
} | ||
|
||
[TestMethod] | ||
public void TestGetConfiguration() | ||
{ | ||
var configRoot = Path.Combine(Directory.GetCurrentDirectory(), | ||
"BasicPullHandlerRoot/Configuration"); | ||
var mofPath = Path.Combine(configRoot, "SHARED/StaticTestConfig.mof"); | ||
var csumPath = Path.Combine(configRoot, "SHARED/FYI/StaticTestConfig.mof.checksum"); | ||
var mofBody = File.ReadAllBytes(mofPath); | ||
var csumBody = File.ReadAllText(csumPath); | ||
|
||
var fileResult = _defaultClient.GetConfiguration(_defaultConfig.ConfigurationNames.First()).Result; | ||
Assert.IsNotNull(fileResult?.Content, "File result is not missing or null"); | ||
CollectionAssert.AreEqual(mofBody, fileResult.Content, "File result content"); | ||
Assert.AreEqual(csumBody, fileResult.Checksum, "Expected config checksum"); | ||
} | ||
|
||
[TestMethod] | ||
public void TestGetModule() | ||
{ | ||
var modName = "xPSDesiredStateConfiguration"; | ||
var modVers = "5.1.0.0"; | ||
var modulesRoot = Path.Combine(Directory.GetCurrentDirectory(), | ||
"BasicPullHandlerRoot/Modules"); | ||
var modPath = Path.Combine(modulesRoot, $"{modName}/{modVers}.zip"); | ||
var csumPath = Path.Combine(modulesRoot, $"{modName}/FYI/{modVers}.zip.checksum"); | ||
var modBody = File.ReadAllBytes(modPath); | ||
var csumBody = File.ReadAllText(csumPath); | ||
|
||
var fileResult = _defaultClient.GetModule(modName, modVers).Result; | ||
Assert.IsNotNull(fileResult?.Content, "File result is not missing or null"); | ||
CollectionAssert.AreEqual(modBody, fileResult.Content, "File result content"); | ||
Assert.AreEqual(csumBody, fileResult.Checksum, "Expected config checksum"); | ||
} | ||
|
||
protected static DscPullClient BuildClient(DscPullConfig config = null) | ||
{ | ||
if (config == null) | ||
config = _defaultConfig; | ||
return new DscPullClient(config, x => _tugServer.CreateClient()); | ||
} | ||
|
||
protected static DscPullConfig BuildConfig(bool newAgentId = false) | ||
{ | ||
var config = new DscPullConfig(); | ||
|
||
config.AgentId = newAgentId | ||
? Guid.NewGuid() | ||
: Guid.Parse(DEFAULT_AGENT_ID); | ||
|
||
config.AgentInformation = TugDSC.Client.CLIApp.Program.ComputeAgentInformation(); | ||
config.ConfigurationNames = new[] { "StaticTestConfig" }; | ||
|
||
// This is required for RegKey Authz | ||
config.CertificateInformation = new Model.CertificateInformation | ||
{ | ||
FriendlyName = "Tug.Client-Test", | ||
Issuer = "Tug.Client.Test", | ||
NotAfter = DateTime.Now.AddYears(1).ToString("O"), | ||
NotBefore = DateTime.Now.AddMinutes(-10).ToString("O"), | ||
Subject = "Tug.Client-TestNode", | ||
PublicKey = "U3lzdGVtLlNlY3VyaXR5LkNyeXB0b2dyYXBoeS5YNTA5Q2VydGlmaWNhdGVzLlB1YmxpY0tleQ==", | ||
Thumbprint = "8351F16C2B06634279F2C0287B5430452DA1CD94", | ||
Version = 3, | ||
}; | ||
|
||
config.ConfigurationRepositoryServer = new DscPullConfig.ServerConfig | ||
{ | ||
ServerUrl = new Uri(DEFAULT_SERVER_URL), | ||
RegistrationKey = DEFAULT_REG_KEY, | ||
}; | ||
|
||
// Resource Server Endpoint URL is same as Config Server Endpoint URL | ||
config.ResourceRepositoryServer = config.ConfigurationRepositoryServer; | ||
|
||
return config; | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
test/server/TugDSC.Server-tests/TugDSC.Server-tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp2.0</TargetFramework> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Update="appsettings.json;nlog.config;BasicPullHandlerRoot\**\*"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
|
||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" /> | ||
<PackageReference Include="MSTest.TestAdapter" Version="1.2.0" /> | ||
<PackageReference Include="MSTest.TestFramework" Version="1.2.0" /> | ||
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="2.0.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\src\testing\TugDSC.Testing.MSTest\TugDSC.Testing.MSTest.csproj" /> | ||
<ProjectReference Include="..\..\..\src\TugDSC.Abstractions\TugDSC.Abstractions.csproj" /> | ||
<ProjectReference Include="..\..\..\src\TugDSC.Client.CLIApp\TugDSC.Client.CLIApp.csproj" /> | ||
<ProjectReference Include="..\..\..\src\TugDSC.Server.Abstractions\TugDSC.Server.Abstractions.csproj" /> | ||
<ProjectReference Include="..\..\..\src\TugDSC.Server.WebAppHost\TugDSC.Server.WebAppHost.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"logSettings": { | ||
"LogType": "nlog", | ||
"DebugLog": "true" | ||
}, | ||
"appSettings": { | ||
"checksum": { | ||
"default": "SHA-256" | ||
}, | ||
|
||
"authz": { | ||
"params": { | ||
"RegistrationKeyPath": "BasicPullHandlerRoot/RegKeys", | ||
"RegistrationSavePath": "BasicPullHandlerRoot/Registrations" | ||
} | ||
}, | ||
|
||
// Enable this setup to use the BASIC DSC Handler | ||
"handler": { | ||
// The default provider is "basic" but we're going | ||
// going to use a specialized subclass that we've | ||
// built specifically to support our testing | ||
"provider": "integTest", | ||
|
||
// Our specialized handler inherits all the base features | ||
// of the "basic" pull handler including parameters | ||
"params": { | ||
// For testing purposes, we redefine these to make sure they get | ||
// placed under the _IGNORE subfolder so they are ignored by Git | ||
"RegistrationKeyPath": "BasicPullHandlerRoot/RegKeys", | ||
"RegistrationSavePath": "BasicPullHandlerRoot/Registrations", | ||
"ConfigurationPath": "BasicPullHandlerRoot/Configuration", | ||
"ModulePath": "BasicPullHandlerRoot/Modules", | ||
"ReportsPath": "BasicPullHandlerRoot/Reports" | ||
} | ||
} | ||
} | ||
} |