New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Detecting virtual machine hypervisor, #167 #527
Merged
Merged
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
496344a
Detecting Hyper-V hypervisor, #167
lukasz-pyrzyk 0f5ea5b
Fixed typo
lukasz-pyrzyk 7d9a04e
Detecting VirtualBox
lukasz-pyrzyk d311f7a
Null checks for HyperV
lukasz-pyrzyk 5ff0a20
Detecting VMWare
lukasz-pyrzyk dd9a1c1
Typo in VMware
lukasz-pyrzyk a44e47c
Added information about VM overhead and vm detector
lukasz-pyrzyk c5e21c4
Usage of the default static instanse, code cleanup
lukasz-pyrzyk 48afbbb
Less null checks by usage of ContainsVmIdentifier
lukasz-pyrzyk 28e8946
Removed redundant #IF in stringextensions
lukasz-pyrzyk 8e69272
Running VmIdentifier on the Appveyor
lukasz-pyrzyk f6dc6ec
Message to const, initialization of the skip field from the static ctor
lukasz-pyrzyk 5a0d4c2
Check with conditional operator
lukasz-pyrzyk 0ea4fcf
Running WMI code only on classic .net
lukasz-pyrzyk f831698
Usage of RuntimeInformation.IsClassic
lukasz-pyrzyk File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace BenchmarkDotNet.Portability | ||
{ | ||
public class HyperV : VirtualMachineHypervisor | ||
{ | ||
public static HyperV Default { get; } = new HyperV(); | ||
|
||
private HyperV() { } | ||
|
||
public override string Name => "Hyper-V"; | ||
|
||
public override bool IsVirtualMachine(string manufacturer, string model) | ||
{ | ||
return ContainsVmIdentifier(manufacturer, "microsoft") && ContainsVmIdentifier(model, "virtual"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace BenchmarkDotNet.Portability | ||
{ | ||
public class VMware : VirtualMachineHypervisor | ||
{ | ||
public static VMware Default { get; } = new VMware(); | ||
|
||
private VMware() { } | ||
|
||
public override string Name => "VMware"; | ||
|
||
public override bool IsVirtualMachine(string manufacturer, string model) | ||
{ | ||
return ContainsVmIdentifier(model, "vmware"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace BenchmarkDotNet.Portability | ||
{ | ||
public class VirtualBox : VirtualMachineHypervisor | ||
{ | ||
public static VirtualBox Default { get; } = new VirtualBox(); | ||
|
||
private VirtualBox() { } | ||
|
||
public override string Name => "VirtualBox"; | ||
|
||
public override bool IsVirtualMachine(string manufacturer, string model) | ||
{ | ||
return ContainsVmIdentifier(model, "virtualbox"); | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/BenchmarkDotNet.Core/Portability/VirtualMachineHypervisor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace BenchmarkDotNet.Portability | ||
{ | ||
public abstract class VirtualMachineHypervisor | ||
{ | ||
public abstract string Name { get; } | ||
|
||
public abstract bool IsVirtualMachine(string manufacturer, string model); | ||
|
||
protected bool ContainsVmIdentifier(string systemInformation, string vmIdentifier) | ||
{ | ||
return systemInformation != null && systemInformation.ContainsWithIgnoreCase(vmIdentifier); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using BenchmarkDotNet.Portability; | ||
using Xunit; | ||
|
||
namespace BenchmarkDotNet.Tests.Portability | ||
{ | ||
public class HyperVTests | ||
{ | ||
private readonly VirtualMachineHypervisor hypervisor = HyperV.Default; | ||
|
||
[Fact] | ||
public void ContainsCorrectName() | ||
{ | ||
Assert.Equal("Hyper-V", hypervisor.Name); | ||
} | ||
|
||
[Theory] | ||
[InlineData("Microsoft Corporation", "Virtual Machine", true)] | ||
[InlineData("microsoft corporation", "virtual machine", true)] | ||
[InlineData("microsoft", "virtual", true)] | ||
[InlineData("Dell", "virtual", false)] | ||
[InlineData("Dell", "ubuntu", false)] | ||
[InlineData("microsoft corporation", null, false)] | ||
[InlineData(null, "virtual machine", false)] | ||
public void DetectsVirtualMachine(string manufacturer, string model, bool expectedResult) | ||
{ | ||
bool result = hypervisor.IsVirtualMachine(manufacturer, model); | ||
Assert.Equal(expectedResult, result); | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
tests/BenchmarkDotNet.Tests/Portability/RuntimeInformationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using BenchmarkDotNet.Portability; | ||
using BenchmarkDotNet.Tests.XUnit; | ||
using Xunit; | ||
|
||
namespace BenchmarkDotNet.Tests.Portability | ||
{ | ||
public class RuntimeInformationTests | ||
{ | ||
[AppVeyorWithClassicFrameworkOnlyFact] | ||
public void DetectsHyperVOnAppveyor() | ||
{ | ||
VirtualMachineHypervisor hypervisor = RuntimeInformation.GetVirtualMachineHypervisor(); | ||
|
||
Assert.Equal(hypervisor, HyperV.Default); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using BenchmarkDotNet.Portability; | ||
using Xunit; | ||
|
||
namespace BenchmarkDotNet.Tests.Portability | ||
{ | ||
public class VMwareTests | ||
{ | ||
private readonly VirtualMachineHypervisor hypervisor = VMware.Default; | ||
|
||
[Fact] | ||
public void ContainsCorrectName() | ||
{ | ||
Assert.Equal("VMware", hypervisor.Name); | ||
} | ||
|
||
[Theory] | ||
[InlineData("VMWare Inc", "VMWare", true)] | ||
[InlineData("VMWare Inc", "vmWare", true)] | ||
[InlineData("redundant", "vmWare", true)] | ||
[InlineData(null, "vmWare", true)] | ||
[InlineData("VMWare Inc", "redundant", false)] | ||
[InlineData("VMWare Inc", null, false)] | ||
public void DetectsVirtualMachine(string manufacturer, string model, bool expectedResult) | ||
{ | ||
bool result = hypervisor.IsVirtualMachine(manufacturer, model); | ||
Assert.Equal(expectedResult, result); | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
tests/BenchmarkDotNet.Tests/Portability/VirtualBoxTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using BenchmarkDotNet.Portability; | ||
using Xunit; | ||
|
||
namespace BenchmarkDotNet.Tests.Portability | ||
{ | ||
public class VirtualBoxTests | ||
{ | ||
private readonly VirtualMachineHypervisor hypervisor = VirtualBox.Default; | ||
|
||
[Fact] | ||
public void ContainsCorrectName() | ||
{ | ||
Assert.Equal("VirtualBox", hypervisor.Name); | ||
} | ||
|
||
[Theory] | ||
[InlineData("redundant", "virtualbox", true)] | ||
[InlineData("redundant", "VirtualBox", true)] | ||
[InlineData("redundant", "vmware", false)] | ||
[InlineData("redundant", null, false)] | ||
public void DetectsVirtualMachine(string manufacturer, string model, bool expectedResult) | ||
{ | ||
bool result = hypervisor.IsVirtualMachine(manufacturer, model); | ||
Assert.Equal(expectedResult, result); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
tests/BenchmarkDotNet.Tests/XUnit/AppveyorOnlyFactAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
using BenchmarkDotNet.Portability; | ||
using Xunit; | ||
|
||
namespace BenchmarkDotNet.Tests.XUnit | ||
{ | ||
public class AppVeyorOnlyFactAttribute : FactAttribute | ||
{ | ||
private const string Message = "Test is available only on the AppVeyor"; | ||
private static readonly string skip; | ||
|
||
static AppVeyorOnlyFactAttribute() | ||
{ | ||
string value = Environment.GetEnvironmentVariable("APPVEYOR"); | ||
bool appVeyorDetected = !string.IsNullOrEmpty(value) && value.EqualsWithIgnoreCase("true"); | ||
skip = appVeyorDetected ? null : Message; | ||
} | ||
|
||
public override string Skip => skip; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
tests/BenchmarkDotNet.Tests/XUnit/AppveyorWithClassicFrameworkOnlyFactAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
namespace BenchmarkDotNet.Tests.XUnit | ||
{ | ||
public class AppVeyorWithClassicFrameworkOnlyFactAttribute : AppVeyorOnlyFactAttribute | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
{ | ||
private const string Message = "Test requires classic .NET Framework"; | ||
|
||
private bool classicDotNet | ||
{ | ||
get | ||
{ | ||
#if !CORE | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
return true; | ||
#else | ||
return false; | ||
#endif | ||
} | ||
} | ||
|
||
public override string Skip => base.Skip ?? (classicDotNet ? null : Message); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.