|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +namespace VirtualClient.Actions |
| 5 | +{ |
| 6 | + using Microsoft.Extensions.DependencyInjection; |
| 7 | + using Moq; |
| 8 | + using NUnit.Framework; |
| 9 | + using System; |
| 10 | + using System.Collections.Generic; |
| 11 | + using System.Threading; |
| 12 | + using System.Threading.Tasks; |
| 13 | + using VirtualClient.Actions.NetworkPerformance; |
| 14 | + using VirtualClient.Contracts; |
| 15 | + using Polly; |
| 16 | + using System.Net.Http; |
| 17 | + using System.Net; |
| 18 | + using System.IO; |
| 19 | + using System.Reflection; |
| 20 | + using VirtualClient.Common.Telemetry; |
| 21 | + using VirtualClient.Common.Contracts; |
| 22 | + |
| 23 | + [TestFixture] |
| 24 | + [Category("Unit")] |
| 25 | + public class NCPSClientExecutorTests |
| 26 | + { |
| 27 | + private MockFixture mockFixture; |
| 28 | + private DependencyPath mockPath; |
| 29 | + private NetworkingWorkloadState networkingWorkloadState; |
| 30 | + |
| 31 | + [SetUp] |
| 32 | + public void SetupTest() |
| 33 | + { |
| 34 | + this.mockFixture = new MockFixture(); |
| 35 | + this.mockPath = new DependencyPath("NetworkingWorkload", this.mockFixture.PlatformSpecifics.GetPackagePath("networkingworkload")); |
| 36 | + this.mockFixture.PackageManager.OnGetPackage().ReturnsAsync(this.mockPath); |
| 37 | + |
| 38 | + this.mockFixture.Directory.Setup(d => d.Exists(It.IsAny<string>())) |
| 39 | + .Returns(true); |
| 40 | + |
| 41 | + this.mockFixture.File.Setup(f => f.Exists(It.IsAny<string>())) |
| 42 | + .Returns(true); |
| 43 | + |
| 44 | + this.mockFixture.Parameters["PackageName"] = "Networking"; |
| 45 | + this.mockFixture.Parameters["ThreadCount"] = "16"; |
| 46 | + this.mockFixture.Parameters["TestDuration"] = "00:05:00"; |
| 47 | + this.mockFixture.Parameters["WarmupTime"] = "00:00:30"; |
| 48 | + this.mockFixture.Parameters["Delaytime"] = "00:00:00"; |
| 49 | + this.mockFixture.Parameters["ConfidenceLevel"] = "99"; |
| 50 | + |
| 51 | + string currentDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); |
| 52 | + string resultsPath = Path.Combine(currentDirectory, "Examples", "NCPS", "NCPS_Example_Results_Server.txt"); |
| 53 | + string results = File.ReadAllText(resultsPath); |
| 54 | + |
| 55 | + this.mockFixture.Process.StandardOutput.Append(results); |
| 56 | + |
| 57 | + this.mockFixture.FileSystem.Setup(rt => rt.File.ReadAllTextAsync(It.IsAny<string>(), It.IsAny<CancellationToken>())) |
| 58 | + .ReturnsAsync(results); |
| 59 | + |
| 60 | + this.SetupNetworkingWorkloadState(); |
| 61 | + } |
| 62 | + |
| 63 | + [Test] |
| 64 | + public void NCPSClientExecutorThrowsOnUnsupportedOS() |
| 65 | + { |
| 66 | + this.mockFixture.SystemManagement.SetupGet(sm => sm.Platform).Returns(PlatformID.Other); |
| 67 | + TestNCPSClientExecutor component = new TestNCPSClientExecutor(this.mockFixture.Dependencies, this.mockFixture.Parameters); |
| 68 | + |
| 69 | + Assert.ThrowsAsync<NotSupportedException>(() => component.ExecuteAsync(CancellationToken.None)); |
| 70 | + } |
| 71 | + |
| 72 | + [Test] |
| 73 | + public async Task NCPSClientExecutorExecutesAsExpected() |
| 74 | + { |
| 75 | + NetworkingWorkloadExecutorTests.TestNetworkingWorkloadExecutor networkingWorkloadExecutor = new NetworkingWorkloadExecutorTests.TestNetworkingWorkloadExecutor(this.mockFixture.Dependencies, this.mockFixture.Parameters); |
| 76 | + await networkingWorkloadExecutor.OnInitialize.Invoke(EventContext.None, CancellationToken.None); |
| 77 | + |
| 78 | + int processExecuted = 0; |
| 79 | + this.mockFixture.ProcessManager.OnCreateProcess = (file, arguments, workingDirectory) => |
| 80 | + { |
| 81 | + processExecuted++; |
| 82 | + this.networkingWorkloadState.ToolState = NetworkingWorkloadToolState.Stopped; |
| 83 | + var expectedStateItem = new Item<NetworkingWorkloadState>(nameof(NetworkingWorkloadState), this.networkingWorkloadState); |
| 84 | + |
| 85 | + this.mockFixture.ApiClient.Setup(client => client.GetStateAsync(It.IsAny<string>(), It.IsAny<CancellationToken>(), It.IsAny<IAsyncPolicy<HttpResponseMessage>>())) |
| 86 | + .ReturnsAsync(this.mockFixture.CreateHttpResponse(HttpStatusCode.OK, expectedStateItem)); |
| 87 | + |
| 88 | + return this.mockFixture.Process; |
| 89 | + }; |
| 90 | + |
| 91 | + TestNCPSClientExecutor component = new TestNCPSClientExecutor(this.mockFixture.Dependencies, this.mockFixture.Parameters); |
| 92 | + |
| 93 | + await component.ExecuteAsync(CancellationToken.None).ConfigureAwait(false); |
| 94 | + Assert.AreEqual(1, processExecuted); |
| 95 | + } |
| 96 | + |
| 97 | + [Test] |
| 98 | + public async Task NCPSClientExecutorExecutesAsExpectedWithIntegerTimeParameters() |
| 99 | + { |
| 100 | + this.mockFixture.Parameters["TestDuration"] = 120; |
| 101 | + this.mockFixture.Parameters["WarmupTime"] = 30; |
| 102 | + this.mockFixture.Parameters["Delaytime"] = 15; |
| 103 | + |
| 104 | + NetworkingWorkloadExecutorTests.TestNetworkingWorkloadExecutor networkingWorkloadExecutor = new NetworkingWorkloadExecutorTests.TestNetworkingWorkloadExecutor(this.mockFixture.Dependencies, this.mockFixture.Parameters); |
| 105 | + await networkingWorkloadExecutor.OnInitialize.Invoke(EventContext.None, CancellationToken.None); |
| 106 | + |
| 107 | + int processExecuted = 0; |
| 108 | + this.mockFixture.ProcessManager.OnCreateProcess = (file, arguments, workingDirectory) => |
| 109 | + { |
| 110 | + processExecuted++; |
| 111 | + this.networkingWorkloadState.ToolState = NetworkingWorkloadToolState.Stopped; |
| 112 | + var expectedStateItem = new Item<NetworkingWorkloadState>(nameof(NetworkingWorkloadState), this.networkingWorkloadState); |
| 113 | + |
| 114 | + this.mockFixture.ApiClient.Setup(client => client.GetStateAsync(It.IsAny<string>(), It.IsAny<CancellationToken>(), It.IsAny<IAsyncPolicy<HttpResponseMessage>>())) |
| 115 | + .ReturnsAsync(this.mockFixture.CreateHttpResponse(HttpStatusCode.OK, expectedStateItem)); |
| 116 | + |
| 117 | + return this.mockFixture.Process; |
| 118 | + }; |
| 119 | + |
| 120 | + TestNCPSClientExecutor component = new TestNCPSClientExecutor(this.mockFixture.Dependencies, this.mockFixture.Parameters); |
| 121 | + |
| 122 | + Assert.AreEqual(TimeSpan.FromSeconds(120), component.TestDuration); |
| 123 | + Assert.AreEqual(TimeSpan.FromSeconds(30), component.WarmupTime); |
| 124 | + Assert.AreEqual(TimeSpan.FromSeconds(15), component.DelayTime); |
| 125 | + |
| 126 | + await component.ExecuteAsync(CancellationToken.None).ConfigureAwait(false); |
| 127 | + Assert.AreEqual(1, processExecuted); |
| 128 | + } |
| 129 | + |
| 130 | + [Test] |
| 131 | + public void NCPSClientExecutorSupportsIntegerAndTimeSpanTimeFormats() |
| 132 | + { |
| 133 | + this.mockFixture.Parameters["TestDuration"] = 300; |
| 134 | + this.mockFixture.Parameters["WarmupTime"] = 60; |
| 135 | + this.mockFixture.Parameters["Delaytime"] = 30; |
| 136 | + |
| 137 | + TestNCPSClientExecutor executor = new TestNCPSClientExecutor(this.mockFixture.Dependencies, this.mockFixture.Parameters); |
| 138 | + |
| 139 | + Assert.AreEqual(TimeSpan.FromSeconds(300), executor.TestDuration); |
| 140 | + Assert.AreEqual(TimeSpan.FromSeconds(60), executor.WarmupTime); |
| 141 | + Assert.AreEqual(TimeSpan.FromSeconds(30), executor.DelayTime); |
| 142 | + |
| 143 | + this.mockFixture.Parameters["TestDuration"] = "00:05:00"; |
| 144 | + this.mockFixture.Parameters["WarmupTime"] = "00:01:00"; |
| 145 | + this.mockFixture.Parameters["Delaytime"] = "00:00:30"; |
| 146 | + |
| 147 | + executor = new TestNCPSClientExecutor(this.mockFixture.Dependencies, this.mockFixture.Parameters); |
| 148 | + |
| 149 | + Assert.AreEqual(TimeSpan.FromMinutes(5), executor.TestDuration); |
| 150 | + Assert.AreEqual(TimeSpan.FromMinutes(1), executor.WarmupTime); |
| 151 | + Assert.AreEqual(TimeSpan.FromSeconds(30), executor.DelayTime); |
| 152 | + |
| 153 | + this.mockFixture.Parameters["TestDuration"] = 180; |
| 154 | + executor = new TestNCPSClientExecutor(this.mockFixture.Dependencies, this.mockFixture.Parameters); |
| 155 | + TimeSpan integerBasedDuration = executor.TestDuration; |
| 156 | + |
| 157 | + this.mockFixture.Parameters["TestDuration"] = "00:03:00"; |
| 158 | + executor = new TestNCPSClientExecutor(this.mockFixture.Dependencies, this.mockFixture.Parameters); |
| 159 | + TimeSpan timespanBasedDuration = executor.TestDuration; |
| 160 | + |
| 161 | + Assert.AreEqual(integerBasedDuration, timespanBasedDuration); |
| 162 | + } |
| 163 | + |
| 164 | + private void SetupNetworkingWorkloadState() |
| 165 | + { |
| 166 | + this.networkingWorkloadState = new NetworkingWorkloadState(); |
| 167 | + this.networkingWorkloadState.Scenario = "AnyScenario"; |
| 168 | + this.networkingWorkloadState.Tool = NetworkingWorkloadTool.NCPS; |
| 169 | + this.networkingWorkloadState.ToolState = NetworkingWorkloadToolState.Running; |
| 170 | + this.networkingWorkloadState.Protocol = "TCP"; |
| 171 | + this.networkingWorkloadState.TestMode = "MockTestMode"; |
| 172 | + |
| 173 | + var expectedStateItem = new Item<NetworkingWorkloadState>(nameof(NetworkingWorkloadState), this.networkingWorkloadState); |
| 174 | + |
| 175 | + this.mockFixture.ApiClient.Setup(client => client.GetStateAsync(It.IsAny<string>(), It.IsAny<CancellationToken>(), It.IsAny<IAsyncPolicy<HttpResponseMessage>>())) |
| 176 | + .ReturnsAsync(this.mockFixture.CreateHttpResponse(HttpStatusCode.OK, expectedStateItem)); |
| 177 | + } |
| 178 | + |
| 179 | + private class TestNCPSClientExecutor : NCPSClientExecutor |
| 180 | + { |
| 181 | + public TestNCPSClientExecutor(IServiceCollection dependencies, IDictionary<string, IConvertible> parameters) |
| 182 | + : base(dependencies, parameters) |
| 183 | + { |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | +} |
0 commit comments