|
| 1 | +// ---------------------------------------------------------------------------------- |
| 2 | +// |
| 3 | +// Copyright Microsoft Corporation |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | +// ---------------------------------------------------------------------------------- |
| 14 | + |
| 15 | +using System; |
| 16 | +using Microsoft.Azure.Batch; |
| 17 | +using Microsoft.Azure.Batch.Common; |
| 18 | +using Microsoft.Azure.Batch.Protocol; |
| 19 | +using Microsoft.Azure.Batch.Protocol.Entities; |
| 20 | +using Microsoft.Azure.Commands.Batch.Models; |
| 21 | +using Microsoft.WindowsAzure.Commands.ScenarioTest; |
| 22 | +using Moq; |
| 23 | +using System.Collections.Generic; |
| 24 | +using System.Linq; |
| 25 | +using System.Management.Automation; |
| 26 | +using System.Threading.Tasks; |
| 27 | +using Xunit; |
| 28 | +using BatchClient = Microsoft.Azure.Commands.Batch.Models.BatchClient; |
| 29 | + |
| 30 | +namespace Microsoft.Azure.Commands.Batch.Test.Files |
| 31 | +{ |
| 32 | + public class GetBatchVMFileCommandTests |
| 33 | + { |
| 34 | + private GetBatchVMFileCommand cmdlet; |
| 35 | + private Mock<BatchClient> batchClientMock; |
| 36 | + private Mock<ICommandRuntime> commandRuntimeMock; |
| 37 | + |
| 38 | + public GetBatchVMFileCommandTests() |
| 39 | + { |
| 40 | + batchClientMock = new Mock<BatchClient>(); |
| 41 | + commandRuntimeMock = new Mock<ICommandRuntime>(); |
| 42 | + cmdlet = new GetBatchVMFileCommand() |
| 43 | + { |
| 44 | + CommandRuntime = commandRuntimeMock.Object, |
| 45 | + BatchClient = batchClientMock.Object, |
| 46 | + }; |
| 47 | + } |
| 48 | + |
| 49 | + [Fact] |
| 50 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 51 | + public void GetBatchVMFileParametersTest() |
| 52 | + { |
| 53 | + // Setup cmdlet without required parameters |
| 54 | + BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys(); |
| 55 | + cmdlet.BatchContext = context; |
| 56 | + cmdlet.PoolName = null; |
| 57 | + cmdlet.VMName = null; |
| 58 | + cmdlet.Name = null; |
| 59 | + cmdlet.VM = null; |
| 60 | + cmdlet.Filter = null; |
| 61 | + |
| 62 | + // Build some vm files instead of querying the service on a ListTVMFiles call |
| 63 | + YieldInjectionInterceptor interceptor = new YieldInjectionInterceptor((opContext, request) => |
| 64 | + { |
| 65 | + if (request is ListTVMFilesRequest) |
| 66 | + { |
| 67 | + ListTVMFilesResponse response = BatchTestHelpers.CreateListTVMFilesResponse(new string[] { "startup\\stdout.txt" }); |
| 68 | + Task<object> task = Task<object>.Factory.StartNew(() => { return response; }); |
| 69 | + return task; |
| 70 | + } |
| 71 | + return null; |
| 72 | + }); |
| 73 | + cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor }; |
| 74 | + |
| 75 | + Assert.Throws<ArgumentNullException>(() => cmdlet.ExecuteCmdlet()); |
| 76 | + |
| 77 | + cmdlet.PoolName = "pool"; |
| 78 | + cmdlet.VMName = "vm1"; |
| 79 | + |
| 80 | + // Verify no exceptions occur |
| 81 | + cmdlet.ExecuteCmdlet(); |
| 82 | + } |
| 83 | + |
| 84 | + [Fact] |
| 85 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 86 | + public void GetBatchVMFileTest() |
| 87 | + { |
| 88 | + // Setup cmdlet to get a Task file by name |
| 89 | + BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys(); |
| 90 | + cmdlet.BatchContext = context; |
| 91 | + cmdlet.PoolName = "pool"; |
| 92 | + cmdlet.VMName = "vm1"; |
| 93 | + cmdlet.Name = "startup\\stdout.txt"; |
| 94 | + cmdlet.Filter = null; |
| 95 | + |
| 96 | + // Build a vm file instead of querying the service on a GetVMFileProperties call |
| 97 | + YieldInjectionInterceptor interceptor = new YieldInjectionInterceptor((opContext, request) => |
| 98 | + { |
| 99 | + if (request is GetTVMFilePropertiesRequest) |
| 100 | + { |
| 101 | + GetTVMFilePropertiesResponse response = BatchTestHelpers.CreateGetTVMFilePropertiesResponse(cmdlet.Name); |
| 102 | + Task<object> task = Task<object>.Factory.StartNew(() => { return response; }); |
| 103 | + return task; |
| 104 | + } |
| 105 | + return null; |
| 106 | + }); |
| 107 | + cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor }; |
| 108 | + |
| 109 | + // Setup the cmdlet to write pipeline output to a list that can be examined later |
| 110 | + List<PSVMFile> pipeline = new List<PSVMFile>(); |
| 111 | + commandRuntimeMock.Setup(r => r.WriteObject(It.IsAny<PSVMFile>())).Callback<object>(f => pipeline.Add((PSVMFile)f)); |
| 112 | + |
| 113 | + cmdlet.ExecuteCmdlet(); |
| 114 | + |
| 115 | + // Verify that the cmdlet wrote the vm file returned from the OM to the pipeline |
| 116 | + Assert.Equal(1, pipeline.Count); |
| 117 | + Assert.Equal(cmdlet.Name, pipeline[0].Name); |
| 118 | + } |
| 119 | + |
| 120 | + [Fact] |
| 121 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 122 | + public void ListBatchVMFilesByODataFilterTest() |
| 123 | + { |
| 124 | + // Setup cmdlet to list vm files using an OData filter. |
| 125 | + BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys(); |
| 126 | + cmdlet.BatchContext = context; |
| 127 | + cmdlet.PoolName = "pool"; |
| 128 | + cmdlet.VMName = "vm1"; |
| 129 | + cmdlet.Name = null; |
| 130 | + cmdlet.Filter = "startswith(name,'startup')"; |
| 131 | + |
| 132 | + string[] namesOfConstructedVMFiles = new[] { "startup\\stdout.txt", "startup\\stderr.txt" }; |
| 133 | + |
| 134 | + // Build some vm files instead of querying the service on a ListTVMFiles call |
| 135 | + YieldInjectionInterceptor interceptor = new YieldInjectionInterceptor((opContext, request) => |
| 136 | + { |
| 137 | + if (request is ListTVMFilesRequest) |
| 138 | + { |
| 139 | + ListTVMFilesResponse response = BatchTestHelpers.CreateListTVMFilesResponse(namesOfConstructedVMFiles); |
| 140 | + Task<object> task = Task<object>.Factory.StartNew(() => { return response; }); |
| 141 | + return task; |
| 142 | + } |
| 143 | + return null; |
| 144 | + }); |
| 145 | + cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor }; |
| 146 | + |
| 147 | + // Setup the cmdlet to write pipeline output to a list that can be examined later |
| 148 | + List<PSVMFile> pipeline = new List<PSVMFile>(); |
| 149 | + commandRuntimeMock.Setup(r => |
| 150 | + r.WriteObject(It.IsAny<PSVMFile>())) |
| 151 | + .Callback<object>(f => pipeline.Add((PSVMFile)f)); |
| 152 | + |
| 153 | + cmdlet.ExecuteCmdlet(); |
| 154 | + |
| 155 | + // Verify that the cmdlet wrote the constructed vm files to the pipeline |
| 156 | + Assert.Equal(2, pipeline.Count); |
| 157 | + int taskCount = 0; |
| 158 | + foreach (PSVMFile f in pipeline) |
| 159 | + { |
| 160 | + Assert.True(namesOfConstructedVMFiles.Contains(f.Name)); |
| 161 | + taskCount++; |
| 162 | + } |
| 163 | + Assert.Equal(namesOfConstructedVMFiles.Length, taskCount); |
| 164 | + } |
| 165 | + |
| 166 | + [Fact] |
| 167 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 168 | + public void ListBatchVMFilesWithoutFiltersTest() |
| 169 | + { |
| 170 | + // Setup cmdlet to list vm files without filters. |
| 171 | + BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys(); |
| 172 | + cmdlet.BatchContext = context; |
| 173 | + cmdlet.PoolName = "pool"; |
| 174 | + cmdlet.VMName = "vm1"; |
| 175 | + cmdlet.Name = null; |
| 176 | + cmdlet.Filter = null; |
| 177 | + |
| 178 | + string[] namesOfConstructedVMFiles = new[] { "startup", "workitems", "shared" }; |
| 179 | + |
| 180 | + // Build some vm files instead of querying the service on a ListTVMFiles call |
| 181 | + YieldInjectionInterceptor interceptor = new YieldInjectionInterceptor((opContext, request) => |
| 182 | + { |
| 183 | + if (request is ListTVMFilesRequest) |
| 184 | + { |
| 185 | + ListTVMFilesResponse response = BatchTestHelpers.CreateListTVMFilesResponse(namesOfConstructedVMFiles); |
| 186 | + Task<object> task = Task<object>.Factory.StartNew(() => { return response; }); |
| 187 | + return task; |
| 188 | + } |
| 189 | + return null; |
| 190 | + }); |
| 191 | + cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor }; |
| 192 | + |
| 193 | + // Setup the cmdlet to write pipeline output to a list that can be examined later |
| 194 | + List<PSVMFile> pipeline = new List<PSVMFile>(); |
| 195 | + commandRuntimeMock.Setup(r => |
| 196 | + r.WriteObject(It.IsAny<PSVMFile>())) |
| 197 | + .Callback<object>(f => pipeline.Add((PSVMFile)f)); |
| 198 | + |
| 199 | + cmdlet.ExecuteCmdlet(); |
| 200 | + |
| 201 | + // Verify that the cmdlet wrote the constructed vm files to the pipeline |
| 202 | + Assert.Equal(3, pipeline.Count); |
| 203 | + int taskCount = 0; |
| 204 | + foreach (PSVMFile f in pipeline) |
| 205 | + { |
| 206 | + Assert.True(namesOfConstructedVMFiles.Contains(f.Name)); |
| 207 | + taskCount++; |
| 208 | + } |
| 209 | + Assert.Equal(namesOfConstructedVMFiles.Length, taskCount); |
| 210 | + } |
| 211 | + |
| 212 | + [Fact] |
| 213 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 214 | + public void ListVMFilesMaxCountTest() |
| 215 | + { |
| 216 | + // Verify default max count |
| 217 | + Assert.Equal(Microsoft.Azure.Commands.Batch.Utils.Constants.DefaultMaxCount, cmdlet.MaxCount); |
| 218 | + |
| 219 | + // Setup cmdlet to list vm files and a max count. |
| 220 | + BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys(); |
| 221 | + cmdlet.BatchContext = context; |
| 222 | + cmdlet.PoolName = "pool"; |
| 223 | + cmdlet.VMName = "vm1"; |
| 224 | + cmdlet.Name = null; |
| 225 | + cmdlet.Filter = null; |
| 226 | + int maxCount = 2; |
| 227 | + cmdlet.MaxCount = maxCount; |
| 228 | + |
| 229 | + string[] namesOfConstructedVMFiles = new[] { "startup", "workitems", "shared" }; |
| 230 | + |
| 231 | + // Build some vm files instead of querying the service on a ListTVMFiles call |
| 232 | + YieldInjectionInterceptor interceptor = new YieldInjectionInterceptor((opContext, request) => |
| 233 | + { |
| 234 | + if (request is ListTVMFilesRequest) |
| 235 | + { |
| 236 | + ListTVMFilesResponse response = BatchTestHelpers.CreateListTVMFilesResponse(namesOfConstructedVMFiles); |
| 237 | + Task<object> task = Task<object>.Factory.StartNew(() => { return response; }); |
| 238 | + return task; |
| 239 | + } |
| 240 | + return null; |
| 241 | + }); |
| 242 | + cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor }; |
| 243 | + |
| 244 | + // Setup the cmdlet to write pipeline output to a list that can be examined later |
| 245 | + List<PSVMFile> pipeline = new List<PSVMFile>(); |
| 246 | + commandRuntimeMock.Setup(r => |
| 247 | + r.WriteObject(It.IsAny<PSVMFile>())) |
| 248 | + .Callback<object>(f => pipeline.Add((PSVMFile)f)); |
| 249 | + |
| 250 | + cmdlet.ExecuteCmdlet(); |
| 251 | + |
| 252 | + // Verify that the max count was respected |
| 253 | + Assert.Equal(maxCount, pipeline.Count); |
| 254 | + |
| 255 | + // Verify setting max count <= 0 doesn't return nothing |
| 256 | + cmdlet.MaxCount = -5; |
| 257 | + pipeline.Clear(); |
| 258 | + cmdlet.ExecuteCmdlet(); |
| 259 | + |
| 260 | + Assert.Equal(namesOfConstructedVMFiles.Length, pipeline.Count); |
| 261 | + } |
| 262 | + } |
| 263 | +} |
0 commit comments