Skip to content

Commit

Permalink
Add more workload set tests (dotnet#40388)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsplaisted authored Apr 29, 2024
2 parents 7f35be0 + d7191eb commit d4bce66
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ protected RemoteDirectory(string path)

public abstract List<string> Directories { get; }

public abstract List<string> Files { get; }

public Assertions Should()
{
return new Assertions(this);
Expand Down
57 changes: 45 additions & 12 deletions src/Tests/dotnet-MsiInstallation.Tests/Framework/VMTestBase.cs
Original file line number Diff line number Diff line change
@@ -1,39 +1,61 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.DotNet.Cli.Utils;
using Microsoft.NET.Sdk.WorkloadManifestReader;
using NuGet.Versioning;

namespace Microsoft.DotNet.MsiInstallerTests.Framework
{
[Collection("VM Tests")]
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
_sdkInstallerVersion = new Lazy<string>(() =>
{
if (!string.IsNullOrEmpty(VM.VMTestSettings.SdkInstallerVersion))
{
return VM.VMTestSettings.SdkInstallerVersion;
}
else
{
return "8.0.203";
var sdkTestingDir = VM.GetRemoteDirectory(@"c:\SdkTesting");

string installerPrefix = "dotnet-sdk-";
string installerSuffix = "-win-x64.exe";

List<string> sdkInstallerVersions = new List<string>();
foreach (var file in sdkTestingDir.Files.Select(f => Path.GetFileName(f)))
{
if (file.StartsWith(installerPrefix) && file.EndsWith(installerSuffix))
{
sdkInstallerVersions.Add(file.Substring(installerPrefix.Length, file.Length - installerPrefix.Length - installerSuffix.Length));
}
}

if (sdkInstallerVersions.Count == 0)
{
throw new Exception("No SDK installer found on VM");
}

return sdkInstallerVersions.MaxBy(v => new NuGetVersion(v));
}
}
});
}

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

Lazy<string> _sdkInstallerVersion;

protected string SdkInstallerVersion => _sdkInstallerVersion.Value;

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

protected void InstallSdk(bool deployStage2 = true)
Expand Down Expand Up @@ -127,6 +149,17 @@ protected string GetInstalledSdkVersion()
return result.StdOut;
}

protected CommandResult InstallWorkload(string workloadName)
{
var result = VM.CreateRunCommand("dotnet", "workload", "install", workloadName, "--skip-manifest-update")
.WithDescription($"Install {workloadName} workload")
.Execute();

result.Should().Pass();

return result;
}

protected WorkloadSet GetRollback()
{
var result = VM.CreateRunCommand("dotnet", "workload", "update", "--print-rollback")
Expand Down
19 changes: 19 additions & 0 deletions src/Tests/dotnet-MsiInstallation.Tests/Framework/VirtualMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ namespace Microsoft.DotNet.MsiInstallerTests.Framework
{
class VirtualMachine : IDisposable
{
private static VirtualMachine s_Instance;
private static object s_Lock = new object();

ITestOutputHelper Log { get; }
public VMControl VMControl { get; }

Expand All @@ -24,6 +27,15 @@ class VirtualMachine : IDisposable

public VirtualMachine(ITestOutputHelper log)
{
lock (s_Lock)
{
if (s_Instance != null)
{
throw new Exception(nameof(VirtualMachine) + " already created. This can be caused by running multiple tests which use the VM in parallel, which is not supported.");
}
s_Instance = this;
}

Log = log;

var testSettingsFile = Path.Combine(Environment.CurrentDirectory, "VMTestSettings.json");
Expand Down Expand Up @@ -104,6 +116,11 @@ public void Dispose()
File.WriteAllText(_stateFile, json);

VMControl.Dispose();

lock (s_Lock)
{
s_Instance = null;
}
}

JsonSerializerOptions GetSerializerOptions()
Expand Down Expand Up @@ -508,6 +525,8 @@ VMActionResult GetResult()
public override bool Exists => GetResult().Exists;

public override List<string> Directories => GetResult().Directories;

public override List<string> Files => GetResult().Files;
}

public class VMSnapshot
Expand Down
15 changes: 1 addition & 14 deletions src/Tests/dotnet-MsiInstallation.Tests/MsiInstallerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public void InstallStateShouldBeRemovedOnSdkUninstall()
InstallWorkload("wasm-tools");
ApplyRC1Manifests();
var featureBand = new SdkFeatureBand(SdkInstallerVersion);
var installStatePath = $@"c:\ProgramData\dotnet\workloads\{featureBand}\InstallState\default.json";
var installStatePath = $@"c:\ProgramData\dotnet\workloads\x64\{featureBand}\InstallState\default.json";
VM.GetRemoteFile(installStatePath).Should().Exist();
UninstallSdk();
VM.GetRemoteFile(installStatePath).Should().NotExist();
Expand Down Expand Up @@ -310,19 +310,6 @@ void CheckForDuplicateManifests()
return installedManifestVersions;
}



CommandResult InstallWorkload(string workloadName)
{
var result = VM.CreateRunCommand("dotnet", "workload", "install", workloadName, "--skip-manifest-update")
.WithDescription($"Install {workloadName} workload")
.Execute();

result.Should().Pass();

return result;
}

string ListWorkloads()
{
var result = VM.CreateRunCommand("dotnet", "workload", "list", "--machine-readable")
Expand Down
130 changes: 117 additions & 13 deletions src/Tests/dotnet-MsiInstallation.Tests/WorkloadSetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ public void DoesNotUseWorkloadSetsByDefault()

var originalRollback = GetRollback();

VM.CreateRunCommand("dotnet", "nuget", "add", "source", @"c:\SdkTesting\WorkloadSets")
.WithDescription("Add WorkloadSets to NuGet.config")
.Execute()
.Should()
.Pass();
AddNuGetSource(@"c:\SdkTesting\WorkloadSets");

VM.CreateRunCommand("dotnet", "workload", "update")
.Execute()
Expand All @@ -48,8 +44,9 @@ public void DoesNotUseWorkloadSetsByDefault()

void UpdateAndSwitchToWorkloadSetMode(out string updatedWorkloadVersion, out WorkloadSet rollbackAfterUpdate)
{
var featureBand = new SdkFeatureBand(SdkInstallerVersion).ToStringWithoutPrerelease();
var originalWorkloadVersion = GetWorkloadVersion();
originalWorkloadVersion.Should().StartWith("8.0.200-manifests.");
originalWorkloadVersion.Should().StartWith($"{featureBand}-manifests.");

VM.CreateRunCommand("dotnet", "workload", "update")
.Execute()
Expand All @@ -58,7 +55,7 @@ void UpdateAndSwitchToWorkloadSetMode(out string updatedWorkloadVersion, out Wor

rollbackAfterUpdate = GetRollback();
updatedWorkloadVersion = GetWorkloadVersion();
updatedWorkloadVersion.Should().StartWith("8.0.200-manifests.");
updatedWorkloadVersion.Should().StartWith($"{featureBand}-manifests.");
updatedWorkloadVersion.Should().NotBe(originalWorkloadVersion);

GetUpdateMode().Should().Be("manifests");
Expand All @@ -81,11 +78,7 @@ public void UpdateWithWorkloadSets()

UpdateAndSwitchToWorkloadSetMode(out string _, out WorkloadSet rollbackAfterUpdate);

VM.CreateRunCommand("dotnet", "nuget", "add", "source", @"c:\SdkTesting\WorkloadSets")
.WithDescription("Add WorkloadSets to NuGet.config")
.Execute()
.Should()
.Pass();
AddNuGetSource(@"c:\SdkTesting\WorkloadSets");

VM.CreateRunCommand("dotnet", "workload", "update")
.Execute()
Expand All @@ -96,7 +89,7 @@ public void UpdateWithWorkloadSets()

newRollback.ManifestVersions.Should().NotBeEquivalentTo(rollbackAfterUpdate.ManifestVersions);

GetWorkloadVersion().Should().Be("8.0.201");
GetWorkloadVersion().Should().Be("8.0.300-preview.0.24217.2");
}

[Fact]
Expand All @@ -118,6 +111,108 @@ public void UpdateInWorkloadSetModeWithNoAvailableWorkloadSet()
GetWorkloadVersion().Should().Be(updatedWorkloadVersion);
}

[Theory]
[InlineData("8.0.300-preview.0.24178.1")]
[InlineData("8.0.204")]
public void UpdateToSpecificWorkloadSetVersion(string versionToInstall)
{
InstallSdk();

var workloadVersionBeforeUpdate = GetWorkloadVersion();
workloadVersionBeforeUpdate.Should().NotBe(versionToInstall);

AddNuGetSource(@"c:\SdkTesting\WorkloadSets");

VM.CreateRunCommand("dotnet", "workload", "update", "--version", versionToInstall)
.Execute()
.Should()
.Pass();

VM.CreateRunCommand("dotnet", "workload", "search")
.WithIsReadOnly(true)
.Execute()
.Should()
.Pass();

GetWorkloadVersion().Should().Be(versionToInstall);

// Installing a workload shouldn't update workload version
InstallWorkload("aspire");

GetWorkloadVersion().Should().Be(versionToInstall);
}

[Fact]
public void UpdateToUnavailableWorkloadSetVersion()
{
string unavailableWorkloadSetVersion = "8.0.300-preview.test.42";

InstallSdk();

var workloadVersionBeforeUpdate = GetWorkloadVersion();

VM.CreateRunCommand("dotnet", "workload", "update", "--version", unavailableWorkloadSetVersion)
.Execute()
.Should()
.Fail()
.And
.HaveStdOutContaining(unavailableWorkloadSetVersion);

VM.CreateRunCommand("dotnet", "workload", "search")
.WithIsReadOnly(true)
.Execute()
.Should()
.Pass();

GetWorkloadVersion().Should().Be(workloadVersionBeforeUpdate);
}


[Fact]
public void UpdateWorkloadSetWithoutAvailableManifests()
{
InstallSdk();

UpdateAndSwitchToWorkloadSetMode(out string updatedWorkloadVersion, out WorkloadSet rollbackAfterUpdate);

var workloadVersionBeforeUpdate = GetWorkloadVersion();

VM.CreateRunCommand("dotnet", "workload", "update", "--source", @"c:\SdkTesting\workloadsets")
.Execute()
.Should()
.Fail();


VM.CreateRunCommand("dotnet", "workload", "search")
.WithIsReadOnly(true)
.Execute()
.Should()
.Pass();

GetWorkloadVersion().Should().Be(workloadVersionBeforeUpdate);
}

[Fact]
public void UpdateToWorkloadSetVersionWithManifestsNotAvailable()
{
InstallSdk();

var workloadVersionBeforeUpdate = GetWorkloadVersion();

VM.CreateRunCommand("dotnet", "workload", "update", "--version", @"8.0.300-preview.0.24217.2", "--source", @"c:\SdkTesting\workloadsets")
.Execute()
.Should()
.Fail();

VM.CreateRunCommand("dotnet", "workload", "search")
.WithIsReadOnly(true)
.Execute()
.Should()
.Pass();

GetWorkloadVersion().Should().Be(workloadVersionBeforeUpdate);
}

string GetWorkloadVersion()
{
var result = VM.CreateRunCommand("dotnet", "workload", "--version")
Expand All @@ -139,5 +234,14 @@ string GetUpdateMode()

return result.StdOut;
}

void AddNuGetSource(string source)
{
VM.CreateRunCommand("dotnet", "nuget", "add", "source", source)
.WithDescription($"Add {source} to NuGet.config")
.Execute()
.Should()
.Pass();
}
}
}

0 comments on commit d4bce66

Please sign in to comment.