Skip to content
Prev Previous commit
Next Next commit
Move VM testing framework to subfolder
  • Loading branch information
dsplaisted committed Mar 18, 2024
commit ae688137f9a8db206b2fc1f2b68d3a9dca66cbf1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using System.Threading.Tasks;
using FluentAssertions.Execution;

namespace Microsoft.DotNet.MsiInstallerTests
namespace Microsoft.DotNet.MsiInstallerTests.Framework
{
abstract class RemoteDirectory
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using System.Threading.Tasks;
using FluentAssertions.Execution;

namespace Microsoft.DotNet.MsiInstallerTests
namespace Microsoft.DotNet.MsiInstallerTests.Framework
{
abstract class RemoteFile
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using System.Threading.Tasks;
using Microsoft.DotNet.Cli.Utils;

namespace Microsoft.DotNet.MsiInstallerTests
namespace Microsoft.DotNet.MsiInstallerTests.Framework
{
abstract class VMAction
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using Microsoft.Management.Infrastructure;
using Microsoft.Management.Infrastructure.Serialization;

namespace Microsoft.DotNet.MsiInstallerTests
namespace Microsoft.DotNet.MsiInstallerTests.Framework
{
internal class VMControl : IDisposable
{
Expand Down Expand Up @@ -73,7 +73,7 @@ public CommandResult RunCommandOnVM(string[] args, string workingDirectory = nul
{
var remoteCommand = new RemoteCommand(Log, VMMachineName, _psExecPath, workingDirectory, args);

for (int i=0; i<3; i++)
for (int i = 0; i < 3; i++)
{
var result = remoteCommand.Execute();
if (result.ExitCode != 6 && !result.StdErr.Contains("The handle is invalid"))
Expand Down Expand Up @@ -204,7 +204,7 @@ public async Task SetRunningAsync(bool running)
{
VMEnabledState getCurrentState()
{
var state = (VMEnabledState) (UInt16)VMInstance.CimInstanceProperties["EnabledState"].Value;
var state = (VMEnabledState)(ushort)VMInstance.CimInstanceProperties["EnabledState"].Value;
return state;
}

Expand All @@ -219,7 +219,7 @@ VMEnabledState getCurrentState()

var methodParameters = new CimMethodParametersCollection()
{
CimMethodParameter.Create("RequestedState", (UInt16) targetState, CimFlags.In)
CimMethodParameter.Create("RequestedState", (ushort) targetState, CimFlags.In)
};


Expand All @@ -245,13 +245,13 @@ private async Task WaitForJobSuccess(CimMethodResult result)
{
CimInstance job = (CimInstance)result.OutParameters["Job"].Value;
job = _session.GetInstance(virtNamespace, job);
while (IsRunning((JobState)(UInt16)job.CimInstanceProperties["JobState"].Value))
while (IsRunning((JobState)(ushort)job.CimInstanceProperties["JobState"].Value))
{
await Task.Delay(100);
job = _session.GetInstance(job.CimSystemProperties.Namespace, job);
}

var jobState = (JobState)(UInt16)job.CimInstanceProperties["JobState"].Value;
var jobState = (JobState)(ushort)job.CimInstanceProperties["JobState"].Value;
if (jobState != JobState.Completed && jobState != JobState.CompletedWithWarnings)
{
Log.WriteLine("Job failed: " + jobState);
Expand All @@ -271,7 +271,7 @@ private async Task WaitForJobSuccess(CimMethodResult result)
}
else
{
if (result.ReturnValue.Value is UInt32 returnValue)
if (result.ReturnValue.Value is uint returnValue)
{
if (returnValue != 0)
{
Expand Down Expand Up @@ -305,7 +305,7 @@ enum JobState
CompletedWithWarnings = 32768
}

enum VMEnabledState : UInt16
enum VMEnabledState : ushort
{
Unknown = 0,
Other = 1,
Expand All @@ -314,7 +314,7 @@ enum VMEnabledState : UInt16
ShuttingDown = 4,
NotApplicable = 5,
EnabledButOffline = 6,

}

class RemoteCommand : TestCommand
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.DotNet.MsiInstallerTests
namespace Microsoft.DotNet.MsiInstallerTests.Framework
{
internal class VMStateTree
{
public string SnapshotId { get; set; }
public string SnapshotId { get; set; }
public string SnapshotName { get; set; }

public Dictionary<SerializedVMAction, (VMActionResult actionResult, VMStateTree resultingState)> Actions { get; set; } = new();
Expand All @@ -18,7 +18,8 @@ public SerializableVMStateTree ToSerializeable()
{
SnapshotId = SnapshotId,
SnapshotName = SnapshotName,
Actions = Actions.Select(a => new SerializableVMStateTree.Entry() {
Actions = Actions.Select(a => new SerializableVMStateTree.Entry()
{
Action = a.Key,
ActionResult = a.Value.actionResult,
ResultingState = a.Value.resultingState.ToSerializeable()
Expand Down
123 changes: 123 additions & 0 deletions src/Tests/dotnet-MsiInstallation.Tests/Framework/VMTestBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.NET.Sdk.WorkloadManifestReader;

namespace Microsoft.DotNet.MsiInstallerTests.Framework
{
public class VMTestBase : SdkTest, IDisposable
{
internal VirtualMachine VM { get; }

public VMTestBase(ITestOutputHelper log) : base(log)
{
VM = new VirtualMachine(Log);
}

public virtual void Dispose()
{
VM.Dispose();
}

protected string SdkInstallerVersion
{
get
{
if (!string.IsNullOrEmpty(VM.VMTestSettings.SdkInstallerVersion))
{
return VM.VMTestSettings.SdkInstallerVersion;
}
else
{
return "8.0.203";
}
}
}

protected string SdkInstallerFileName => $"dotnet-sdk-{SdkInstallerVersion}-win-x64.exe";

protected void InstallSdk(bool deployStage2 = true)
{
VM.CreateRunCommand("setx", "DOTNET_NOLOGO", "true")
.WithDescription("Disable .NET SDK first run message")
.Execute()
.Should()
.Pass();

VM.CreateRunCommand($@"c:\SdkTesting\{SdkInstallerFileName}", "/quiet")
.WithDescription($"Install SDK {SdkInstallerVersion}")
.Execute()
.Should()
.Pass();

if (deployStage2)
{
DeployStage2Sdk();
}
}

protected void UninstallSdk()
{
VM.CreateRunCommand($@"c:\SdkTesting\{SdkInstallerFileName}", "/quiet", "/uninstall")
.WithDescription($"Uninstall SDK {SdkInstallerVersion}")
.Execute()
.Should()
.Pass();
}

protected void DeployStage2Sdk()
{
if (!VM.VMTestSettings.ShouldTestStage2)
{
return;
}

var installedSdkFolder = $@"c:\Program Files\dotnet\sdk\{SdkInstallerVersion}";

Log.WriteLine($"Deploying SDK from {TestContext.Current.ToolsetUnderTest.SdkFolderUnderTest} to {installedSdkFolder} on VM.");

var vmVersionFilePath = Path.Combine(installedSdkFolder, ".version");

var existingVersionFileContents = VM.GetRemoteFile(vmVersionFilePath).ReadAllText().Split(Environment.NewLine);
var newVersionFileContents = File.ReadAllLines(Path.Combine(TestContext.Current.ToolsetUnderTest.SdkFolderUnderTest, ".version"));
newVersionFileContents[1] = existingVersionFileContents[1];

// TODO: It would be nice if the description included the date/time of the SDK build, to distinguish different snapshots
VM.CreateActionGroup("Deploy Stage 2 SDK",
VM.CopyFolder(TestContext.Current.ToolsetUnderTest.SdkFolderUnderTest, installedSdkFolder),
VM.WriteFile(vmVersionFilePath, string.Join(Environment.NewLine, newVersionFileContents)))
.Execute()
.Should()
.Pass();
}

protected string GetInstalledSdkVersion()
{
var command = VM.CreateRunCommand("dotnet", "--version");
command.IsReadOnly = true;
var result = command.Execute();
result.Should().Pass();
return result.StdOut;
}

protected WorkloadSet GetRollback()
{
var result = VM.CreateRunCommand("dotnet", "workload", "update", "--print-rollback")
.WithIsReadOnly(true)
.Execute();

result.Should().Pass();

return ParseRollbackOutput(result.StdOut);
}

protected WorkloadSet ParseRollbackOutput(string output)
{
var filteredOutput = string.Join(Environment.NewLine,
output.Split(Environment.NewLine)
.Except(["==workloadRollbackDefinitionJsonOutputStart==", "==workloadRollbackDefinitionJsonOutputEnd=="]));

return WorkloadSet.FromJson(filteredOutput, defaultFeatureBand: new SdkFeatureBand(SdkInstallerVersion));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using System.Text;
using System.Threading.Tasks;

namespace Microsoft.DotNet.MsiInstallerTests
namespace Microsoft.DotNet.MsiInstallerTests.Framework
{
internal class VMTestSettings
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Microsoft.DotNet.MsiInstallerTests
namespace Microsoft.DotNet.MsiInstallerTests.Framework
{
class VirtualMachine : IDisposable
{
Expand Down Expand Up @@ -130,7 +130,7 @@ void Recurse(VMStateTree node)
Log.WriteLine($"Removing missing snapshot from tree: {nodeToRemove.Value.resultingState.SnapshotName}");
node.Actions.Remove(nodeToRemove.Key);
}

foreach (var result in node.Actions.Select(a => a.Value.resultingState))
{
Recurse(result);
Expand Down Expand Up @@ -211,7 +211,7 @@ void LogActionResult(SerializedVMAction action, VMActionResult result)
}
else if (action.Type == VMActionType.ActionGroup && result.GroupedResults != null)
{
for (int i=0; i<result.GroupedResults.Count; i++)
for (int i = 0; i < result.GroupedResults.Count; i++)
{
LogActionResult(action.Actions[i], result.GroupedResults[i]);
}
Expand Down
Loading