public void ListVMsMaxCountTest() { // Verify default max count Assert.Equal(Microsoft.Azure.Commands.Batch.Utils.Constants.DefaultMaxCount, cmdlet.MaxCount); // Setup cmdlet to list vms without filters and a max count BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys(); cmdlet.BatchContext = context; cmdlet.Pool = BatchTestHelpers.CreatePSCloudPool(); cmdlet.Name = null; cmdlet.Filter = null; int maxCount = 2; cmdlet.MaxCount = maxCount; string[] namesOfConstructedVMs = new[] { "vm1", "vm2", "vm3" }; // Build some vms instead of querying the service on a ListTVMs call YieldInjectionInterceptor interceptor = new YieldInjectionInterceptor((opContext, request) => { if (request is ListTVMsRequest) { ListTVMsResponse response = BatchTestHelpers.CreateListTVMsResponse(namesOfConstructedVMs); Task <object> task = Task <object> .Factory.StartNew(() => { return(response); }); return(task); } return(null); }); cmdlet.AdditionalBehaviors = new List <BatchClientBehavior>() { interceptor }; // Setup the cmdlet to write pipeline output to a list that can be examined later List <PSVM> pipeline = new List <PSVM>(); commandRuntimeMock.Setup(r => r.WriteObject(It.IsAny <PSVM>())) .Callback <object>(v => pipeline.Add((PSVM)v)); cmdlet.ExecuteCmdlet(); // Verify that the max count was respected Assert.Equal(maxCount, pipeline.Count); // Verify setting max count <= 0 doesn't return nothing cmdlet.MaxCount = -5; pipeline.Clear(); cmdlet.ExecuteCmdlet(); Assert.Equal(namesOfConstructedVMs.Length, pipeline.Count); }
public void ListBatchVMsByODataFilterTest() { // Setup cmdlet to list vms using an OData filter. BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys(); cmdlet.BatchContext = context; cmdlet.PoolName = "pool"; cmdlet.Name = null; cmdlet.Filter = "state -eq 'idle'"; string[] namesOfConstructedVMs = new[] { "vm1", "vm2" }; // Build some vms instead of querying the service on a ListTVMs call YieldInjectionInterceptor interceptor = new YieldInjectionInterceptor((opContext, request) => { if (request is ListTVMsRequest) { ListTVMsResponse response = BatchTestHelpers.CreateListTVMsResponse(namesOfConstructedVMs); Task <object> task = Task <object> .Factory.StartNew(() => { return(response); }); return(task); } return(null); }); cmdlet.AdditionalBehaviors = new List <BatchClientBehavior>() { interceptor }; // Setup the cmdlet to write pipeline output to a list that can be examined later List <PSVM> pipeline = new List <PSVM>(); commandRuntimeMock.Setup(r => r.WriteObject(It.IsAny <PSVM>())) .Callback <object>(v => pipeline.Add((PSVM)v)); cmdlet.ExecuteCmdlet(); // Verify that the cmdlet wrote the constructed vms to the pipeline Assert.Equal(2, pipeline.Count); int vmCount = 0; foreach (PSVM v in pipeline) { Assert.True(namesOfConstructedVMs.Contains(v.Name)); vmCount++; } Assert.Equal(namesOfConstructedVMs.Length, vmCount); }