Skip to content

Commit

Permalink
migrated server integration tests to 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ebekker committed Feb 6, 2018
1 parent cc54ef7 commit 430a3b5
Show file tree
Hide file tree
Showing 13 changed files with 347 additions and 0 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Expand Up @@ -3,6 +3,7 @@
"cSpell.words": [
"ASMNAME",
"HMACSHA",
"Integ",
"Requ",
"Shhh",
"asms",
Expand Down
15 changes: 15 additions & 0 deletions TugDSC.sln
Expand Up @@ -37,6 +37,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "client", "client", "{3FC7F2
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TugDSC.Client.CLIApp-tests", "test\client\TugDSC.Client.CLIApp-tests\TugDSC.Client.CLIApp-tests.csproj", "{45EA7E4A-BE09-4CA7-AC8C-8AAB356032BB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TugDSC.Server-tests", "test\server\TugDSC.Server-tests\TugDSC.Server-tests.csproj", "{733DD3AE-9D09-4F42-9668-AED7A3E93766}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -170,6 +172,18 @@ Global
{45EA7E4A-BE09-4CA7-AC8C-8AAB356032BB}.Release|x64.Build.0 = Release|x64
{45EA7E4A-BE09-4CA7-AC8C-8AAB356032BB}.Release|x86.ActiveCfg = Release|x86
{45EA7E4A-BE09-4CA7-AC8C-8AAB356032BB}.Release|x86.Build.0 = Release|x86
{733DD3AE-9D09-4F42-9668-AED7A3E93766}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{733DD3AE-9D09-4F42-9668-AED7A3E93766}.Debug|Any CPU.Build.0 = Debug|Any CPU
{733DD3AE-9D09-4F42-9668-AED7A3E93766}.Debug|x64.ActiveCfg = Debug|x64
{733DD3AE-9D09-4F42-9668-AED7A3E93766}.Debug|x64.Build.0 = Debug|x64
{733DD3AE-9D09-4F42-9668-AED7A3E93766}.Debug|x86.ActiveCfg = Debug|x86
{733DD3AE-9D09-4F42-9668-AED7A3E93766}.Debug|x86.Build.0 = Debug|x86
{733DD3AE-9D09-4F42-9668-AED7A3E93766}.Release|Any CPU.ActiveCfg = Release|Any CPU
{733DD3AE-9D09-4F42-9668-AED7A3E93766}.Release|Any CPU.Build.0 = Release|Any CPU
{733DD3AE-9D09-4F42-9668-AED7A3E93766}.Release|x64.ActiveCfg = Release|x64
{733DD3AE-9D09-4F42-9668-AED7A3E93766}.Release|x64.Build.0 = Release|x64
{733DD3AE-9D09-4F42-9668-AED7A3E93766}.Release|x86.ActiveCfg = Release|x86
{733DD3AE-9D09-4F42-9668-AED7A3E93766}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{97DECA03-C0C3-4F41-A0FE-F51CA949D501} = {BA29CE93-33EE-419B-A855-DA934573FA83}
Expand All @@ -187,5 +201,6 @@ Global
{66F0098E-71A2-4145-87C1-7251CA25A667} = {BA29CE93-33EE-419B-A855-DA934573FA83}
{3FC7F219-6BFC-45F2-95D2-9DFC16566565} = {76F0F40C-622E-453C-A012-6A26C1F97F41}
{45EA7E4A-BE09-4CA7-AC8C-8AAB356032BB} = {3FC7F219-6BFC-45F2-95D2-9DFC16566565}
{733DD3AE-9D09-4F42-9668-AED7A3E93766} = {6CA6E17D-22E1-4192-A7D9-2E8E13335C82}
EndGlobalSection
EndGlobal
@@ -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
@@ -0,0 +1 @@
75D3763E4FFE0ED9CFE17F9EE915F1B81FE06E9B6D2287B09561565D61D1009C
Binary file not shown.
Binary file not shown.
@@ -0,0 +1 @@
8FB19218B06E212F1FF251B6F8DE0B261DF3806F0FFD545090E3265A481A9716
@@ -0,0 +1 @@
c3ea5066-ce5a-4d12-a42a-850be287b2d8
12 changes: 12 additions & 0 deletions test/server/TugDSC.Server-tests/IntegTestDscHandler.cs
@@ -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 test/server/TugDSC.Server-tests/IntegTestDscHandlerProvider.cs
@@ -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();
}
}
}
196 changes: 196 additions & 0 deletions test/server/TugDSC.Server-tests/ServerProtocolTests.cs
@@ -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 test/server/TugDSC.Server-tests/TugDSC.Server-tests.csproj
@@ -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>
38 changes: 38 additions & 0 deletions test/server/TugDSC.Server-tests/appsettings.json
@@ -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"
}
}
}
}

0 comments on commit 430a3b5

Please sign in to comment.