Skip to content

Commit

Permalink
Add support for changing version of installed SDK in VM tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dsplaisted committed Mar 27, 2024
1 parent bcc7037 commit 4a553e1
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 9 deletions.
27 changes: 25 additions & 2 deletions src/Tests/dotnet-MsiInstallation.Tests/Framework/VMAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,26 @@ protected override SerializedVMAction SerializeDerivedProperties()
}
}

class VMMoveFolderAction : VMAction
{
public string SourcePath { get; set; }
public string TargetPath { get; set; }

public VMMoveFolderAction(VirtualMachine vm) : base(vm)
{
}

protected override SerializedVMAction SerializeDerivedProperties()
{
return new SerializedVMAction
{
Type = VMActionType.MoveFolderOnVM,
SourcePath = SourcePath,
TargetPath = TargetPath,
};
}
}

class VMWriteFileAction : VMAction
{
public string TargetPath { get; set; }
Expand Down Expand Up @@ -168,6 +188,7 @@ enum VMActionType
RunCommand,
CopyFileToVM,
CopyFolderToVM,
MoveFolderOnVM,
WriteFileToVM,
GetRemoteDirectory,
GetRemoteFile,
Expand All @@ -190,10 +211,10 @@ class SerializedVMAction
// Applies to RunCommand
public string WorkingDirectory { get; set; }

// Applies to CopyFileToVM, CopyFolderToVM, WriteFileToVM, GetRemoteDirectory, GetRemoteFile
// Applies to CopyFileToVM, CopyFolderToVM, MoveFolderOnVM, WriteFileToVM, GetRemoteDirectory, GetRemoteFile
public string TargetPath { get; set; }

// Applies to CopyFileToVM, CopyFolderToVM
// Applies to CopyFileToVM, CopyFolderToVM, MoveFolderOnVM
public string SourcePath { get; set; }

// Applies to CopyFileToVM, CopyFolderToVM
Expand Down Expand Up @@ -222,6 +243,8 @@ public string GetDescription()
return $"Copy file to VM: {SourcePath} -> {TargetPath}";
case VMActionType.CopyFolderToVM:
return $"Copy folder to VM: {SourcePath} -> {TargetPath}";
case VMActionType.MoveFolderOnVM:
return $"Move folder {SourcePath} -> {TargetPath}";
case VMActionType.WriteFileToVM:
return $"Write file to VM: {TargetPath}";
case VMActionType.GetRemoteDirectory:
Expand Down
41 changes: 34 additions & 7 deletions src/Tests/dotnet-MsiInstallation.Tests/Framework/VMTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,48 @@ protected void DeployStage2Sdk()

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)))
ChangeVersionFileContents(SdkInstallerVersion))
.Execute()
.Should()
.Pass();
}

protected void ChangeSdkVersion(string oldVersion, string newVersion)
{
var oldSdkFolder = $@"c:\Program Files\dotnet\sdk\{oldVersion}";
var newSdkFolder = $@"c:\Program Files\dotnet\sdk\{newVersion}";

new VMMoveFolderAction(VM)
{
SourcePath = oldSdkFolder,
TargetPath = newSdkFolder
}
.WithDescription($"Change SDK version to {newVersion}")
.Execute().Should().Pass();

ChangeVersionFileContents(newVersion)
.WithDescription("Update .version file")
.Execute()
.Should()
.Pass();

}

private VMWriteFileAction ChangeVersionFileContents(string sdkVersion)
{
var installedSdkFolder = $@"c:\Program Files\dotnet\sdk\{sdkVersion}";
var vmVersionFilePath = Path.Combine(installedSdkFolder, ".version");

var newVersionFileContents = File.ReadAllLines(Path.Combine(TestContext.Current.ToolsetUnderTest.SdkFolderUnderTest, ".version"));
newVersionFileContents[1] = sdkVersion;

return VM.WriteFile(vmVersionFilePath, string.Join(Environment.NewLine, newVersionFileContents));

}

protected string GetInstalledSdkVersion()
{
var command = VM.CreateRunCommand("dotnet", "--version");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,14 @@ VMActionResult Run(SerializedVMAction action)

return VMActionResult.Success();
}
else if (action.Type == VMActionType.MoveFolderOnVM)
{
var sourceSharePath = VMPathToSharePath(action.SourcePath);
var targetSharePath = VMPathToSharePath(action.TargetPath);
Directory.Move(sourceSharePath, targetSharePath);

return VMActionResult.Success();
}
else if (action.Type == VMActionType.WriteFileToVM)
{
var targetSharePath = VMPathToSharePath(action.TargetPath);
Expand Down

0 comments on commit 4a553e1

Please sign in to comment.