Skip to content

Commit

Permalink
Add support for different VM test start states
Browse files Browse the repository at this point in the history
  • Loading branch information
dsplaisted committed May 6, 2024
1 parent effb24f commit c830887
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 23 deletions.
37 changes: 37 additions & 0 deletions src/Tests/dotnet-MsiInstallation.Tests/Framework/VMStateTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,41 @@ public VMStateTree ToVMStateTree()
return tree;
}
}

internal class VMState
{
public string DefaultRootState { get; set; }

public Dictionary<string, VMStateTree> VMStates { get; set; } = new Dictionary<string, VMStateTree>();

public SerializableVMState ToSerializable()
{
return new SerializableVMState()
{
DefaultRootState = DefaultRootState,
VMStates = VMStates.Values.Select(v => v.ToSerializeable()).ToList()
};
}

public VMStateTree GetRootState()
{
return VMStates[DefaultRootState];
}
}

internal class SerializableVMState
{
public string DefaultRootState { get; set; }

public List<SerializableVMStateTree> VMStates { get; set; }

public VMState ToVMState()
{
return new VMState()
{
DefaultRootState = DefaultRootState,
VMStates = VMStates.Select(s => s.ToVMStateTree()).ToDictionary(s => s.SnapshotName)
};
}
}
}
12 changes: 10 additions & 2 deletions src/Tests/dotnet-MsiInstallation.Tests/Framework/VMTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,22 @@ protected void DeployStage2Sdk()
return;
}

var installedSdkFolder = $@"c:\Program Files\dotnet\sdk\{SdkInstallerVersion}";
var result = VM.CreateRunCommand("dotnet", "--version")
.WithIsReadOnly(true)
.Execute();

result.Should().Pass();

string existingVersionToOverwrite = result.StdOut;

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

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

// 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),
ChangeVersionFileContents(SdkInstallerVersion))
ChangeVersionFileContents(existingVersionToOverwrite))
.Execute()
.Should()
.Pass();
Expand Down
90 changes: 69 additions & 21 deletions src/Tests/dotnet-MsiInstallation.Tests/Framework/VirtualMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class VirtualMachine : IDisposable

public VMTestSettings VMTestSettings { get; }

VMStateTree _rootState;
VMState _vmState;
VMStateTree _currentState;
VMStateTree _currentAppliedState;

Expand Down Expand Up @@ -81,38 +81,45 @@ public VirtualMachine(ITestOutputHelper log)
if (File.Exists(_stateFile))
{
string json = File.ReadAllText(_stateFile);
_rootState = JsonSerializer.Deserialize<SerializableVMStateTree>(json, GetSerializerOptions()).ToVMStateTree();
_vmState = JsonSerializer.Deserialize<SerializableVMState>(json, GetSerializerOptions()).ToVMState();
}
else
{
var snapshots = VMControl.GetSnapshots();
var testStartSnapshots = snapshots.Where(s => s.name.Contains("Test start", StringComparison.OrdinalIgnoreCase)).ToList();
if (testStartSnapshots.Count == 0)
{
throw new Exception("No test start snapshots found");
}
else if (testStartSnapshots.Count > 1)
_vmState = new VMState();
}

// Determine test start state
var snapshots = VMControl.GetSnapshots();
var testStartSnapshots = snapshots.Where(s => s.name.Contains("Test start", StringComparison.OrdinalIgnoreCase)).ToList();
if (testStartSnapshots.Count == 0)
{
throw new Exception("No test start snapshots found");
}
else if (testStartSnapshots.Count > 1)
{
foreach (var snapshot in testStartSnapshots)
{
foreach (var snapshot in testStartSnapshots)
{
Log.WriteLine(snapshot.id + ": " + snapshot.name);
}
throw new Exception("Multiple test start snapshots found");
Log.WriteLine(snapshot.id + ": " + snapshot.name);
}
_rootState = new VMStateTree
throw new Exception("Multiple test start snapshots found");
}

_vmState.DefaultRootState = testStartSnapshots[0].name;
if (!_vmState.VMStates.ContainsKey(_vmState.DefaultRootState))
{
_vmState.VMStates[_vmState.DefaultRootState] = new VMStateTree()
{
SnapshotId = testStartSnapshots[0].Item1,
SnapshotName = testStartSnapshots[0].Item2
SnapshotId = testStartSnapshots[0].id,
SnapshotName = testStartSnapshots[0].name
};
}

_currentState = _rootState;
_currentState = _vmState.GetRootState();

TrimMissingSnapshots();
}
public void Dispose()
{
string json = JsonSerializer.Serialize(_rootState.ToSerializeable(), GetSerializerOptions());
string json = JsonSerializer.Serialize(_vmState.ToSerializable(), GetSerializerOptions());
File.WriteAllText(_stateFile, json);

VMControl.Dispose();
Expand All @@ -137,7 +144,17 @@ public void TrimMissingSnapshots()
{
var snapshotIds = VMControl.GetSnapshots().Select(s => s.id).ToHashSet();

Recurse(_rootState);
foreach (var state in _vmState.VMStates.Values.ToList())
{
if (!snapshotIds.Contains(state.SnapshotId))
{
_vmState.VMStates.Remove(state.SnapshotId);
}
else
{
Recurse(state);
}
}

void Recurse(VMStateTree node)
{
Expand All @@ -155,6 +172,37 @@ void Recurse(VMStateTree node)
}
}

public void SetCurrentState(string stateName)
{
if (_vmState.VMStates.TryGetValue(stateName, out var state))
{
_currentState = state;
}
else
{
var snapshots = VMControl.GetSnapshots();
var matchingSnapshots = snapshots.Where(s => s.name.Equals(stateName, StringComparison.OrdinalIgnoreCase)).ToList();
if (matchingSnapshots.Count == 0)
{
throw new Exception($"No snapshot found with name {stateName}");
}
else if (matchingSnapshots.Count > 1)
{
throw new Exception($"Multiple snapshots found with name {stateName}");
}
else
{
var newState = new VMStateTree()
{
SnapshotId = matchingSnapshots[0].id,
SnapshotName = matchingSnapshots[0].name,
};
_vmState.VMStates[stateName] = newState;
_currentState = newState;
}
}
}

public VMRunAction CreateRunCommand(params string[] args)
{
return new VMRunAction(this, args.ToList());
Expand Down

0 comments on commit c830887

Please sign in to comment.