public void CanReadUsesTaskDependenciesFromABoundCloudJobScheduleTest() { const string jobId = "id-123"; const bool usesTaskDependencies = true; using (BatchClient client = BatchClient.Open(ClientUnitTestCommon.CreateDummySharedKeyCredential())) { Protocol.RequestInterceptor interceptor = new Protocol.RequestInterceptor( baseRequest => { var request = (Protocol.BatchRequest <Models.JobScheduleGetOptions, AzureOperationResponse <Models.CloudJobSchedule, Models.JobScheduleGetHeaders> >)baseRequest; request.ServiceRequestFunc = token => { var response = new AzureOperationResponse <Models.CloudJobSchedule, Models.JobScheduleGetHeaders> { Body = new Protocol.Models.CloudJobSchedule(jobId, schedule: new Protocol.Models.Schedule(), jobSpecification: new Protocol.Models.JobSpecification() { UsesTaskDependencies = true }) }; return(Task.FromResult(response)); }; }); Microsoft.Azure.Batch.CloudJobSchedule unboundCloudJob = client.JobScheduleOperations.GetJobSchedule(jobId, additionalBehaviors: new List <BatchClientBehavior> { interceptor }); Assert.Equal(usesTaskDependencies, unboundCloudJob.JobSpecification.UsesTaskDependencies); } }
public async Task TestBatchRequestCannotBeModifiedAfterExecutionStarted() { BatchSharedKeyCredentials credentials = new BatchSharedKeyCredentials( ClientUnitTestCommon.DummyBaseUrl, ClientUnitTestCommon.DummyAccountName, ClientUnitTestCommon.DummyAccountKey); using (BatchClient batchClient = await BatchClient.OpenAsync(ClientUnitTestCommon.CreateDummySharedKeyCredential())) { Protocol.RequestInterceptor interceptor = new Protocol.RequestInterceptor(req => { PoolAddBatchRequest addPoolRequest = req as PoolAddBatchRequest; addPoolRequest.ServiceRequestFunc = token => { Assert.Throws <InvalidOperationException>(() => addPoolRequest.CancellationToken = CancellationToken.None); Assert.Throws <InvalidOperationException>(() => addPoolRequest.Options = null); Assert.Throws <InvalidOperationException>(() => addPoolRequest.RetryPolicy = null); Assert.Throws <InvalidOperationException>(() => addPoolRequest.ServiceRequestFunc = null); Assert.Throws <InvalidOperationException>(() => addPoolRequest.Timeout = TimeSpan.FromSeconds(0)); Assert.Throws <InvalidOperationException>(() => addPoolRequest.ClientRequestIdProvider = null); Assert.Throws <InvalidOperationException>(() => addPoolRequest.Parameters = null); return(Task.FromResult(new AzureOperationHeaderResponse <Protocol.Models.PoolAddHeaders>())); }; }); CloudPool pool = batchClient.PoolOperations.CreatePool("dummy", "small", default(CloudServiceConfiguration), targetDedicated: 0); await pool.CommitAsync(additionalBehaviors : new[] { interceptor }); } }
public void Pool_WhenReturnedFromServer_HasExpectedBoundProperties() { const string cloudPoolId = "id-123"; const string cloudPoolDisplayName = "pool-display-name-test"; MetadataItem metadataItem = new MetadataItem("foo", "bar"); BatchSharedKeyCredentials credentials = ClientUnitTestCommon.CreateDummySharedKeyCredential(); using (BatchClient client = BatchClient.Open(credentials)) { Models.CloudPool protoPool = new Models.CloudPool(id: cloudPoolId, displayName: cloudPoolDisplayName, metadata: new[] { new Models.MetadataItem { Name = metadataItem.Name, Value = metadataItem.Value } }); CloudPool boundPool = client.PoolOperations.GetPool(string.Empty, additionalBehaviors: InterceptorFactory.CreateGetPoolRequestInterceptor(protoPool)); // Cannot change these bound properties. Assert.Throws <InvalidOperationException>(() => boundPool.DisplayName = "cannot-change-display-name"); Assert.Throws <InvalidOperationException>(() => boundPool.Id = "cannot-change-id"); Assert.Throws <InvalidOperationException>(() => boundPool.TargetDedicated = 1); Assert.Throws <InvalidOperationException>(() => boundPool.VirtualMachineSize = "cannot-change-1"); // Swap the value with the name and the name with the value. boundPool.Metadata = new[] { new MetadataItem(metadataItem.Value, metadataItem.Name) }; Assert.Equal(metadataItem.Name, boundPool.Metadata.First().Value); Assert.Equal(metadataItem.Value, boundPool.Metadata.First().Name); } }
public async Task TestDefaultBatchRequestTimeoutSet() { BatchSharedKeyCredentials credentials = ClientUnitTestCommon.CreateDummySharedKeyCredential(); TimeSpan requestTimeout = TimeSpan.MinValue; using (BatchClient client = await BatchClient.OpenAsync(credentials)) { Protocol.RequestInterceptor interceptor = new Protocol.RequestInterceptor(req => { requestTimeout = req.Timeout; var castRequest = (Protocol.BatchRequest < Protocol.Models.JobGetOptions, AzureOperationResponse <Protocol.Models.CloudJob, Protocol.Models.JobGetHeaders> >)req; castRequest.ServiceRequestFunc = (token) => { return(Task.FromResult(new AzureOperationResponse <Protocol.Models.CloudJob, Protocol.Models.JobGetHeaders>() { Body = new Protocol.Models.CloudJob() })); }; }); await client.JobOperations.GetJobAsync("foo", additionalBehaviors : new List <BatchClientBehavior> { interceptor }); Assert.Equal(Constants.DefaultSingleRestRequestClientTimeout, requestTimeout); } }
public async Task TestCancellationViaParameter() { BatchSharedKeyCredentials credentials = ClientUnitTestCommon.CreateDummySharedKeyCredential(); using (BatchClient client = await BatchClient.OpenAsync(credentials)) { List <IInheritedBehaviors> objectsToExamineForMethods = new List <IInheritedBehaviors>() { client.JobOperations, client.JobScheduleOperations, client.CertificateOperations, client.PoolOperations, }; foreach (IInheritedBehaviors o in objectsToExamineForMethods) { List <MethodInfo> methodsToCall = DiscoverCancellableMethods(o.GetType()); foreach (MethodInfo method in methodsToCall) { foreach (IInheritedBehaviors behaviorContainer in objectsToExamineForMethods) { behaviorContainer.CustomBehaviors.Clear(); behaviorContainer.CustomBehaviors.Add(CreateRequestInterceptorForCancellationMonitoring()); } await this.BatchRequestCancellationViaParameterTestAsync(method, o, TimeSpan.FromSeconds(0)); } } } }
public async Task TaskStateMonitorCancelled_ThrowsCancellationException() { TimeSpan timeout = TimeSpan.FromSeconds(0); const string dummyJobId = "Dummy"; using (BatchClient batchCli = BatchClient.Open(ClientUnitTestCommon.CreateDummySharedKeyCredential())) { List <string> taskIds = new List <string>() { "task1", "task2" }; //Create some tasks which are "bound" IEnumerable <Protocol.Models.CloudTask> protocolTasks = taskIds.Select(CreateProtocolCloudTask); IEnumerable <CloudTask> taskList = protocolTasks.Select(protoTask => CreateBoundCloudTask(batchCli, dummyJobId, protoTask)); TaskStateMonitor taskStateMonitor = batchCli.Utilities.CreateTaskStateMonitor(); using (CancellationTokenSource cts = new CancellationTokenSource(timeout)) { await Assert.ThrowsAsync <OperationCanceledException>(async() => await taskStateMonitor.WhenAll( taskList, TaskState.Completed, cts.Token, additionalBehaviors: InterceptorFactory.CreateListTasksRequestInterceptor(protocolTasks))); } } }
public async Task TaskStateMonitorTimedOut_ThrowsTimeoutException() { TimeSpan timeout = TimeSpan.FromSeconds(0); const string dummyJobId = "Dummy"; using (BatchClient batchCli = BatchClient.Open(ClientUnitTestCommon.CreateDummySharedKeyCredential())) { List <string> taskIds = new List <string>() { "task1", "task2" }; //Create some tasks which are "bound" IEnumerable <Protocol.Models.CloudTask> protocolTasks = taskIds.Select(CreateProtocolCloudTask); IEnumerable <CloudTask> taskList = protocolTasks.Select(protoTask => CreateBoundCloudTask(batchCli, dummyJobId, protoTask)); TaskStateMonitor taskStateMonitor = batchCli.Utilities.CreateTaskStateMonitor(); TimeoutException e = await Assert.ThrowsAsync <TimeoutException>(async() => await taskStateMonitor.WhenAll( taskList, TaskState.Completed, timeout, additionalBehaviors: InterceptorFactory.CreateListTasksRequestInterceptor(protocolTasks))); Assert.Contains(string.Format("waiting for resources after {0}", timeout), e.Message); Assert.IsType <OperationCanceledException>(e.InnerException); } }
public async Task TestRequestWhichDoesSupportSelect() { using (BatchClient client = await BatchClient.OpenAsync(ClientUnitTestCommon.CreateDummySharedKeyCredential())) { ODATADetailLevel detailLevel = new ODATADetailLevel(selectClause: "foo"); bool wasHit = false; BatchClientBehavior behavior = new Protocol.RequestInterceptor(request => { PoolGetBatchRequest poolGetRequest = request as PoolGetBatchRequest; poolGetRequest.ServiceRequestFunc = t => { Assert.Equal(detailLevel.SelectClause, poolGetRequest.Options.Select); wasHit = true; //Ensure the interceptor was hit return(Task.FromResult(new AzureOperationResponse <CloudPool, PoolGetHeaders>() { Body = new CloudPool() })); }; }); const string dummyPoolId = "dummy"; await client.PoolOperations.GetPoolAsync(dummyPoolId, detailLevel, new[] { behavior }); Assert.True(wasHit); } }
public void CloudJobSchedule_WhenSendingToTheServer_HasExpectedUnboundProperties() { const string jobScheduleId = "id-123"; const string displayName = "DisplayNameFoo"; MetadataItem metadataItem = new MetadataItem("foo", "bar"); BatchSharedKeyCredentials credentials = ClientUnitTestCommon.CreateDummySharedKeyCredential(); using (BatchClient client = BatchClient.Open(credentials)) { CloudJobSchedule jobSchedule = client.JobScheduleOperations.CreateJobSchedule(); jobSchedule.Id = jobScheduleId; jobSchedule.DisplayName = displayName; jobSchedule.Metadata = new List <MetadataItem> { metadataItem }; Assert.Equal(jobSchedule.Id, jobScheduleId); // can set an unbound object Assert.Equal(jobSchedule.Metadata.First().Name, metadataItem.Name); Assert.Equal(jobSchedule.Metadata.First().Value, metadataItem.Value); jobSchedule.Commit(additionalBehaviors: InterceptorFactory.CreateAddJobScheduleRequestInterceptor()); // writing isn't allowed for a jobSchedule that is in an read only state. Assert.Throws <InvalidOperationException>(() => jobSchedule.Id = "cannot-change-id"); Assert.Throws <InvalidOperationException>(() => jobSchedule.DisplayName = "cannot-change-display-name"); //Can still read though Assert.Equal(jobScheduleId, jobSchedule.Id); Assert.Equal(displayName, jobSchedule.DisplayName); } }
public void CannotSetUsesTaskDependenciesFromABoundCloudJob() { const string jobId = "id-123"; const bool usesTaskDependencies = true; // Bound BatchSharedKeyCredentials credentials = ClientUnitTestCommon.CreateDummySharedKeyCredential(); using (BatchClient client = BatchClient.Open(credentials)) { Protocol.RequestInterceptor interceptor = new Protocol.RequestInterceptor( baseRequest => { var request = (Protocol.BatchRequest <Models.JobGetOptions, AzureOperationResponse <Models.CloudJob, Models.JobGetHeaders> >)baseRequest; request.ServiceRequestFunc = (token) => { var response = new AzureOperationResponse <Models.CloudJob, Models.JobGetHeaders> { Body = new Protocol.Models.CloudJob { UsesTaskDependencies = usesTaskDependencies } }; return(Task.FromResult(response)); }; }); var cloudJob = client.JobOperations.GetJob(jobId, additionalBehaviors: new List <BatchClientBehavior> { interceptor }); Assert.Equal(usesTaskDependencies, cloudJob.UsesTaskDependencies); Assert.Throws <InvalidOperationException>(() => cloudJob.UsesTaskDependencies = false); } }
public void GetPoolResizeError() { var autoScaleRunError = new Models.AutoScaleRunError { Code = "InsufficientSampleData", Message = "Autoscale evaluation failed due to insufficient sample data", Values = new List <Models.NameValuePair> { new Models.NameValuePair { Name = "Message", Value = "Line 1, Col 24: Insufficient data from data set: $RunningTasks wanted 100%, received 0%" } } }; var autoScaleError = new Models.AutoScaleRun { Error = autoScaleRunError }; BatchSharedKeyCredentials credentials = ClientUnitTestCommon.CreateDummySharedKeyCredential(); using (BatchClient client = BatchClient.Open(credentials)) { Protocol.RequestInterceptor interceptor = new Protocol.RequestInterceptor(baseRequest => { var request = (Protocol.BatchRequest <Models.PoolGetOptions, AzureOperationResponse <Models.CloudPool, Models.PoolGetHeaders> >)baseRequest; request.ServiceRequestFunc = async(token) => { var response = new AzureOperationResponse <Models.CloudPool, Models.PoolGetHeaders> { Body = new Models.CloudPool { DisplayName = "batch-test", AutoScaleFormula = "$RunningTasks.GetSample(10 * TimeInterval_Second, 0 * TimeInterval_Second, 100);", AutoScaleRun = autoScaleError, EnableAutoScale = true, } }; var task = Task.FromResult(response); return(await task); }; }); var pool = client.PoolOperations.GetPool("batch-test", additionalBehaviors: new List <BatchClientBehavior> { interceptor }); Assert.Equal("batch-test", pool.DisplayName); Assert.Equal(pool.AutoScaleEnabled, true); Assert.Equal(pool.AutoScaleRun.Error.Code, "InsufficientSampleData"); Assert.Equal(pool.AutoScaleRun.Error.Message, "Autoscale evaluation failed due to insufficient sample data"); Assert.Equal(pool.AutoScaleRun.Error.Values.First().Name, "Message"); Assert.Equal(pool.AutoScaleRun.Error.Values.First().Value, "Line 1, Col 24: Insufficient data from data set: $RunningTasks wanted 100%, received 0%"); } }
public async Task CreateJobScheduleWithApplicationPackageReferences() { const string applicationId = "blender.exe"; const string version = "blender"; const string jobId = "mock-job"; BatchSharedKeyCredentials credentials = ClientUnitTestCommon.CreateDummySharedKeyCredential(); using (BatchClient client = BatchClient.Open(credentials)) { Protocol.RequestInterceptor interceptor = new Protocol.RequestInterceptor( baseRequest => { var request = (Protocol.BatchRequest <Models.JobScheduleGetOptions, AzureOperationResponse <Models.CloudJobSchedule, Models.JobScheduleGetHeaders> >)baseRequest; request.ServiceRequestFunc = (token) => { var response = new AzureOperationResponse <Models.CloudJobSchedule, Models.JobScheduleGetHeaders> { Body = new Models.CloudJobSchedule { JobSpecification = new Protocol.Models.JobSpecification { PoolInfo = new Models.PoolInformation { AutoPoolSpecification = new Models.AutoPoolSpecification { Pool = new Models.PoolSpecification { ApplicationPackageReferences = new[] { new Protocol.Models.ApplicationPackageReference { ApplicationId = applicationId, Version = version, } }, MaxTasksPerNode = 4 } } } } } }; return(Task.FromResult(response)); }; }); Microsoft.Azure.Batch.CloudJobSchedule cloudJobSchedule = await client.JobScheduleOperations.GetJobScheduleAsync(jobId, additionalBehaviors : new List <BatchClientBehavior> { interceptor }); Assert.Equal(cloudJobSchedule.JobSpecification.PoolInformation.AutoPoolSpecification.PoolSpecification.ApplicationPackageReferences.First().ApplicationId, applicationId); Assert.Equal(cloudJobSchedule.JobSpecification.PoolInformation.AutoPoolSpecification.PoolSpecification.ApplicationPackageReferences.First().Version, version); } }
public void CloudJob_WhenReturnedFromServer_HasExpectedBoundProperties() { const string jobId = "id-123"; const string displayName = "DisplayNameFoo"; string applicationVersion = "beta"; string applicationId = "test"; MetadataItem metadataItem = new MetadataItem("foo", "bar"); const int priority = 0; var onAllTasksComplete = OnAllTasksComplete.TerminateJob; BatchSharedKeyCredentials credentials = ClientUnitTestCommon.CreateDummySharedKeyCredential(); using (BatchClient client = BatchClient.Open(credentials)) { DateTime creationTime = DateTime.Now; Models.CloudJob protoJob = new Models.CloudJob( jobId, displayName, jobManagerTask: new Models.JobManagerTask() { ApplicationPackageReferences = new [] { new Models.ApplicationPackageReference() { ApplicationId = applicationId, Version = applicationVersion } } }, metadata: new[] { new Models.MetadataItem { Name = metadataItem.Name, Value = metadataItem.Value } }, creationTime: creationTime, priority: priority, url: ClientUnitTestCommon.DummyBaseUrl, onAllTasksComplete: Models.OnAllTasksComplete.NoAction); CloudJob boundJob = client.JobOperations.GetJob(jobId, additionalBehaviors: InterceptorFactory.CreateGetJobRequestInterceptor(protoJob)); Assert.Equal(jobId, boundJob.Id); // reading is allowed from a job that is returned from the server. Assert.Equal(creationTime, boundJob.CreationTime); Assert.Equal(displayName, boundJob.DisplayName); Assert.Equal(applicationId, boundJob.JobManagerTask.ApplicationPackageReferences.First().ApplicationId); Assert.Equal(applicationVersion, boundJob.JobManagerTask.ApplicationPackageReferences.First().Version); AssertPatchableJobPropertiesCanBeWritten(boundJob, priority, metadataItem, onAllTasksComplete); // Can only read a url from a returned object. Assert.Equal(ClientUnitTestCommon.DummyBaseUrl, boundJob.Url); // Cannot change a bound displayName, Id and any property on a JobManagerTask. Assert.Throws <InvalidOperationException>(() => boundJob.DisplayName = "cannot-change-display-name"); Assert.Throws <InvalidOperationException>(() => boundJob.Id = "cannot-change-id"); Assert.Throws <InvalidOperationException>(() => boundJob.JobManagerTask.ApplicationPackageReferences = new List <ApplicationPackageReference>()); Assert.Throws <InvalidOperationException>(() => boundJob.JobManagerTask = new JobManagerTask()); } }
public void CloudTask_WhenReturnedFromServer_HasExpectedBoundProperties() { const string jobId = "id-123"; const string taskId = "id-123"; const int exitCode = 1; const int exitCodeRangeStart = 0; const int exitCodeRangeEnd = 4; Models.ExitOptions terminateExitOption = new Models.ExitOptions() { JobAction = Models.JobAction.Terminate }; Models.ExitOptions disableExitOption = new Models.ExitOptions() { JobAction = Models.JobAction.Disable }; BatchSharedKeyCredentials credentials = ClientUnitTestCommon.CreateDummySharedKeyCredential(); using (BatchClient client = BatchClient.Open(credentials)) { Models.CloudTask cloudTask = new Models.CloudTask() { Id = jobId, ExitConditions = new Models.ExitConditions() { DefaultProperty = disableExitOption, ExitCodeRanges = new List <Models.ExitCodeRangeMapping>() { new Models.ExitCodeRangeMapping(exitCodeRangeStart, exitCodeRangeEnd, terminateExitOption) }, ExitCodes = new List <Models.ExitCodeMapping>() { new Models.ExitCodeMapping(exitCode, terminateExitOption) }, SchedulingError = terminateExitOption, } }; CloudTask boundTask = client.JobOperations.GetTask( jobId, taskId, additionalBehaviors: InterceptorFactory.CreateGetTaskRequestInterceptor(cloudTask)); Assert.Equal(taskId, boundTask.Id); // reading is allowed from a task that is returned from the server. Assert.Equal(disableExitOption.JobAction.ToString(), boundTask.ExitConditions.Default.JobAction.ToString()); Assert.Throws <InvalidOperationException>(() => boundTask.ExitConditions = new ExitConditions()); Assert.Throws <InvalidOperationException>(() => boundTask.DependsOn = new TaskDependencies(new List <string>(), new List <TaskIdRange>())); Assert.Throws <InvalidOperationException>(() => boundTask.RunElevated = true); Assert.Throws <InvalidOperationException>(() => boundTask.CommandLine = "Cannot change command line"); Assert.Throws <InvalidOperationException>(() => boundTask.ExitConditions.Default = new ExitOptions() { JobAction = JobAction.Terminate }); } }
public void CloudJob_WhenSendingToTheServer_HasExpectedUnboundProperties() { const string jobId = "id-123"; const string displayName = "DisplayNameFoo"; MetadataItem metadataItem = new MetadataItem("foo", "bar"); const int priority = 0; const string applicationId = "testApp"; const string applicationVersion = "beta"; BatchSharedKeyCredentials credentials = ClientUnitTestCommon.CreateDummySharedKeyCredential(); using (BatchClient client = BatchClient.Open(credentials)) { CloudJob cloudJob = client.JobOperations.CreateJob(jobId, new PoolInformation { AutoPoolSpecification = new AutoPoolSpecification { KeepAlive = false } }); cloudJob.Id = jobId; cloudJob.DisplayName = displayName; cloudJob.Metadata = new List <MetadataItem> { metadataItem }; cloudJob.Priority = priority; cloudJob.JobManagerTask = new JobManagerTask { ApplicationPackageReferences = new List <ApplicationPackageReference> { new ApplicationPackageReference { ApplicationId = applicationId, Version = applicationVersion } } }; cloudJob.OnAllTasksComplete = OnAllTasksComplete.NoAction; cloudJob.OnTaskFailure = OnTaskFailure.NoAction; Assert.Throws <InvalidOperationException>(() => cloudJob.Url); // cannot read a Url since it's unbound at this point. Assert.Equal(cloudJob.Id, jobId); // can set an unbound object Assert.Equal(cloudJob.Metadata.First().Name, metadataItem.Name); Assert.Equal(cloudJob.Metadata.First().Value, metadataItem.Value); Assert.Equal(cloudJob.OnAllTasksComplete, OnAllTasksComplete.NoAction); Assert.Equal(cloudJob.OnTaskFailure, OnTaskFailure.NoAction); cloudJob.Commit(additionalBehaviors: InterceptorFactory.CreateAddJobRequestInterceptor()); // writing isn't allowed for a job that is in an invalid state. Assert.Throws <InvalidOperationException>(() => cloudJob.Id = "cannot-change-id"); Assert.Throws <InvalidOperationException>(() => cloudJob.DisplayName = "cannot-change-display-name"); } }
public async Task AddTaskCollectionNullTaskThrows() { const string dummyJobId = "Dummy"; using (BatchClient batchCli = BatchClient.Open(ClientUnitTestCommon.CreateDummySharedKeyCredential())) { ArgumentNullException exception = await Assert.ThrowsAsync <ArgumentNullException>(() => batchCli.JobOperations.AddTaskAsync(dummyJobId, new List <CloudTask> { null })); string expectedString = string.Format(BatchErrorMessages.CollectionMustNotContainNull); Assert.Contains(expectedString, exception.Message); } }
public void Bug1432987CloudTaskTaskConstraints() { using (BatchClient batchCli = BatchClient.Open(ClientUnitTestCommon.CreateDummySharedKeyCredential())) { CloudTask badTask = new CloudTask("bug1432987TaskConstraints", "hostname"); TaskConstraints isThisBroken = badTask.Constraints; TaskConstraints trySettingThem = new TaskConstraints(null, null, null); badTask.Constraints = trySettingThem; TaskConstraints readThemBack = badTask.Constraints; } }
public void GetPoolWithApplicationReferencesTest() { const string applicationId = "blender.exe"; const string version = "blender"; BatchSharedKeyCredentials credentials = ClientUnitTestCommon.CreateDummySharedKeyCredential(); using (BatchClient client = BatchClient.Open(credentials)) { Protocol.RequestInterceptor interceptor = new Protocol.RequestInterceptor( baseRequest => { var request = (Protocol.BatchRequest <Models.PoolGetOptions, AzureOperationResponse <Models.CloudPool, Models.PoolGetHeaders> >)baseRequest; request.ServiceRequestFunc = (token) => { var response = new AzureOperationResponse <Models.CloudPool, Models.PoolGetHeaders> { Body = new Models.CloudPool { ApplicationPackageReferences = new[] { new Protocol.Models.ApplicationPackageReference { ApplicationId = applicationId, Version = version } }, CurrentDedicated = 4, CloudServiceConfiguration = new Models.CloudServiceConfiguration() { CurrentOSVersion = "3" }, Id = "pool-id" }, }; return(Task.FromResult(response)); }; }); Microsoft.Azure.Batch.CloudPool cloudPool = client.PoolOperations.GetPool("pool-id", additionalBehaviors: new List <BatchClientBehavior> { interceptor }); Assert.Equal(cloudPool.ApplicationPackageReferences.First().Version, version); Assert.Equal(cloudPool.ApplicationPackageReferences.First().ApplicationId, applicationId); } }
public void GetPoolStartTask() { var startTask = new Protocol.Models.StartTask { CommandLine = "-start", EnvironmentSettings = new[] { new Models.EnvironmentSetting { Name = "windows", Value = "foo" } }, MaxTaskRetryCount = 3, RunElevated = false, WaitForSuccess = false }; BatchSharedKeyCredentials credentials = ClientUnitTestCommon.CreateDummySharedKeyCredential(); using (BatchClient client = BatchClient.Open(credentials)) { Protocol.RequestInterceptor interceptor = new Protocol.RequestInterceptor(baseRequest => { var request = (Protocol.BatchRequest <Models.PoolGetOptions, AzureOperationResponse <Models.CloudPool, Models.PoolGetHeaders> >)baseRequest; request.ServiceRequestFunc = async(token) => { var response = new AzureOperationResponse <Models.CloudPool, Models.PoolGetHeaders> { Body = new Models.CloudPool { DisplayName = "batch-test", StartTask = startTask, } }; var task = Task.FromResult(response); return(await task); }; }); var pool = client.PoolOperations.GetPool("batch-test", additionalBehaviors: new List <BatchClientBehavior> { interceptor }); Assert.Equal("batch-test", pool.DisplayName); Assert.Equal(pool.StartTask.CommandLine, "-start"); Assert.Equal(pool.StartTask.EnvironmentSettings.FirstOrDefault().Name, "windows"); Assert.Equal(pool.StartTask.EnvironmentSettings.FirstOrDefault().Value, "foo"); Assert.Equal(pool.StartTask.MaxTaskRetryCount, 3); Assert.Equal(pool.StartTask.RunElevated, false); Assert.Equal(pool.StartTask.WaitForSuccess, false); } }
public void TestRandomBoundCloudTaskProperties() { using (BatchClient client = BatchClient.Open(ClientUnitTestCommon.CreateDummySharedKeyCredential())) { for (int i = 0; i < TestRunCount; i++) { Protocol.Models.CloudTask taskModel = this.customizedObjectFactory.GenerateNew <Protocol.Models.CloudTask>(); CloudTask boundTask = new CloudTask(client, "Foo", taskModel, client.CustomBehaviors); ObjectComparer.CheckEqualityResult result = this.objectComparer.CheckEquality(boundTask, taskModel); Assert.True(result.Equal, result.Message); } } }
private async Task TestSetBatchRequestServerTimeoutHelperAsync(TimeSpan?clientTimeout, int?serverTimeoutInSeconds) { TimeSpan expectedClientTimeout = clientTimeout ?? DefaultClientTimeout; int expectedServerTimeoutInSeconds = serverTimeoutInSeconds ?? DefaultServerTimeoutInSeconds; TimeSpan?serverTimeout = serverTimeoutInSeconds.HasValue ? TimeSpan.FromSeconds(serverTimeoutInSeconds.Value) : TimeSpan.FromSeconds(DefaultServerTimeoutInSeconds); using (BatchClient batchCli = BatchClient.Open(ClientUnitTestCommon.CreateDummySharedKeyCredential())) { batchCli.CustomBehaviors.Add(new BatchRequestTimeout(serverTimeout, clientTimeout)); batchCli.CustomBehaviors.Add(new Protocol.RequestInterceptor((req) => ConfirmTimeoutWasSetInterceptor(req, expectedClientTimeout, expectedServerTimeoutInSeconds))); await batchCli.PoolOperations.GetPoolAsync("Foo"); } }
public async Task CheckListApplicationSummariesIsReturningAValidList() { const string applicationId = "blender.exe"; const string displayName = "blender"; IList <string> versions = new[] { "1.0", "1.5" }; BatchSharedKeyCredentials credentials = ClientUnitTestCommon.CreateDummySharedKeyCredential(); using (BatchClient client = await BatchClient.OpenAsync(credentials)) { Protocol.RequestInterceptor interceptor = new Protocol.RequestInterceptor(baseRequest => { var request = (Protocol.BatchRequest <Models.ApplicationListOptions, AzureOperationResponse <IPage <Models.ApplicationSummary>, Models.ApplicationListHeaders> >)baseRequest; request.ServiceRequestFunc = (token) => { var response = new AzureOperationResponse <IPage <Models.ApplicationSummary>, Models.ApplicationListHeaders> { Body = new FakePage <Models.ApplicationSummary>(new[] { new Models.ApplicationSummary { Id = applicationId, DisplayName = displayName, Versions = versions }, }) }; return(Task.FromResult(response)); }; }); IPagedEnumerable <Microsoft.Azure.Batch.ApplicationSummary> applicationSummaries = client.ApplicationOperations.ListApplicationSummaries(additionalBehaviors: new List <BatchClientBehavior> { interceptor }); Assert.Equal(1, applicationSummaries.Count()); var applicationSummary = applicationSummaries.First(); Assert.Equal(applicationId, applicationSummary.Id); Assert.Equal(displayName, applicationSummary.DisplayName); Assert.Equal(versions.First(), applicationSummary.Versions.First()); Assert.Equal(versions.Count, applicationSummary.Versions.ToList().Count); } }
public async Task ExceptionOnClientError_ResultingExceptionContainsDetails() { const string expectedCode = "badness"; const string failingTaskId = "baz"; using (BatchClient batchCli = BatchClient.Open(ClientUnitTestCommon.CreateDummySharedKeyCredential())) { var tasksToAdd = new List <CloudTask> { new CloudTask("foo", "bar"), new CloudTask(failingTaskId, "qux") }; var results = new List <Protocol.Models.TaskAddResult> { new Protocol.Models.TaskAddResult(Protocol.Models.TaskAddStatus.Success, "foo"), new Protocol.Models.TaskAddResult( Protocol.Models.TaskAddStatus.Clienterror, failingTaskId, error: new Protocol.Models.BatchError( expectedCode, new Protocol.Models.ErrorMessage(value: "Test value"), new List <Protocol.Models.BatchErrorDetail> { new Protocol.Models.BatchErrorDetail("key", "value") })) }; ParallelOperationsException parallelOperationsException = await Assert.ThrowsAsync <ParallelOperationsException>( () => batchCli.JobOperations.AddTaskAsync( "dummy", tasksToAdd, additionalBehaviors: InterceptorFactory.CreateAddTaskCollectionInterceptor(results))); Assert.Equal(1, parallelOperationsException.InnerExceptions.Count); var exception = parallelOperationsException.InnerException as AddTaskCollectionTerminatedException; Assert.NotNull(exception); Assert.NotNull(exception.AddTaskResult); Assert.Equal(failingTaskId, exception.AddTaskResult.TaskId); Assert.Equal(AddTaskStatus.ClientError, exception.AddTaskResult.Status); Assert.Equal(expectedCode, exception.AddTaskResult.Error.Code); Assert.Equal("Addition of a task failed with unexpected status code. Details: TaskId=baz, Status=ClientError, Error.Code=badness, Error.Message=Test value, Error.Values=[key=value]", exception.Message); } }
public void TestRandomBoundPrepReleaseTaskExecutionInformationProperties() { using (BatchClient client = BatchClient.Open(ClientUnitTestCommon.CreateDummySharedKeyCredential())) { for (int i = 0; i < TestRunCount; i++) { Protocol.Models.JobPreparationAndReleaseTaskExecutionInformation jobPrepReleaseExecutionInfo = this.customizedObjectFactory.GenerateNew <Protocol.Models.JobPreparationAndReleaseTaskExecutionInformation>(); JobPreparationAndReleaseTaskExecutionInformation boundJobPrepReleaseExecutionInfo = new JobPreparationAndReleaseTaskExecutionInformation(jobPrepReleaseExecutionInfo); ObjectComparer.CheckEqualityResult result = this.objectComparer.CheckEquality(boundJobPrepReleaseExecutionInfo, jobPrepReleaseExecutionInfo); Assert.True(result.Equal, result.Message); } } }
public async Task GetPoolWithApplicationPackageReferences() { const string applicationId = "blender.exe"; const string version = "blender"; const string poolName = "test-pool"; BatchSharedKeyCredentials credentials = ClientUnitTestCommon.CreateDummySharedKeyCredential(); using (BatchClient client = BatchClient.Open(credentials)) { Protocol.RequestInterceptor interceptor = new Protocol.RequestInterceptor(baseRequest => { var request = (Protocol.BatchRequest <Models.PoolGetOptions, AzureOperationResponse <Models.CloudPool, Models.PoolGetHeaders> >)baseRequest; request.ServiceRequestFunc = async(token) => { var response = new AzureOperationResponse <Models.CloudPool, Models.PoolGetHeaders> { Body = new Models.CloudPool { ApplicationPackageReferences = new[] { new Protocol.Models.ApplicationPackageReference { ApplicationId = applicationId, Version = version } }, } }; Task <AzureOperationResponse <Models.CloudPool, Models.PoolGetHeaders> > task = Task.FromResult(response); return(await task); }; }); var pool = await client.PoolOperations.GetPoolAsync(poolName, additionalBehaviors : new List <BatchClientBehavior> { interceptor }); var appRefs = pool.ApplicationPackageReferences; Assert.Equal(applicationId, appRefs[0].ApplicationId); Assert.Equal(version, appRefs[0].Version); } }
public void Pool_WhenReturnedFromServer_HasExpectedUnboundProperties() { const string cloudPoolId = "id-123"; const string osFamily = "2"; const string virtualMachineSize = "4"; const string cloudPoolDisplayName = "pool-display-name-test"; MetadataItem metadataItem = new MetadataItem("foo", "bar"); BatchSharedKeyCredentials credentials = ClientUnitTestCommon.CreateDummySharedKeyCredential(); using (BatchClient client = BatchClient.Open(credentials)) { CloudPool cloudPool = client.PoolOperations.CreatePool(cloudPoolId, virtualMachineSize, new CloudServiceConfiguration(osFamily)); cloudPool.DisplayName = cloudPoolDisplayName; cloudPool.Metadata = new List <MetadataItem> { metadataItem }; Assert.Equal(cloudPoolId, cloudPool.Id); // can set an unbound object Assert.Equal(cloudPool.Metadata.First().Name, metadataItem.Name); Assert.Equal(cloudPool.Metadata.First().Value, metadataItem.Value); cloudPool.Commit(additionalBehaviors: InterceptorFactory.CreateAddPoolRequestInterceptor()); // writing isn't allowed for a cloudPool that is in an readonly state. Assert.Throws <InvalidOperationException>(() => cloudPool.AutoScaleFormula = "Foo"); Assert.Throws <InvalidOperationException>(() => cloudPool.DisplayName = "Foo"); Assert.Throws <InvalidOperationException>(() => cloudPool.CloudServiceConfiguration = null); Assert.Throws <InvalidOperationException>(() => cloudPool.ResizeTimeout = TimeSpan.FromSeconds(10)); Assert.Throws <InvalidOperationException>(() => cloudPool.Metadata = null); Assert.Throws <InvalidOperationException>(() => cloudPool.TargetDedicated = 5); Assert.Throws <InvalidOperationException>(() => cloudPool.VirtualMachineConfiguration = null); Assert.Throws <InvalidOperationException>(() => cloudPool.VirtualMachineSize = "small"); //read is allowed though Assert.Null(cloudPool.AutoScaleFormula); Assert.Equal(cloudPoolDisplayName, cloudPool.DisplayName); Assert.NotNull(cloudPool.CloudServiceConfiguration); Assert.Null(cloudPool.ResizeTimeout); Assert.Equal(1, cloudPool.Metadata.Count); Assert.Null(cloudPool.TargetDedicated); Assert.Null(cloudPool.VirtualMachineConfiguration); Assert.Equal(virtualMachineSize, cloudPool.VirtualMachineSize); } }
public async Task UpdateBoundJobWithNewAutoTerminationPropertiesTest() { BatchSharedKeyCredentials credentials = ClientUnitTestCommon.CreateDummySharedKeyCredential(); using (BatchClient client = await BatchClient.OpenAsync(credentials)) { Models.CloudJob protoJob = new Models.CloudJob(id: "id", onAllTasksComplete: Models.OnAllTasksComplete.NoAction, onTaskFailure: Models.OnTaskFailure.PerformExitOptionsJobAction); CloudJob boundJob = await client.JobOperations.GetJobAsync(string.Empty, additionalBehaviors : InterceptorFactory.CreateGetJobRequestInterceptor(protoJob)); Assert.Equal(OnAllTasksComplete.NoAction, boundJob.OnAllTasksComplete); Assert.Equal(OnTaskFailure.PerformExitOptionsJobAction, boundJob.OnTaskFailure); // Can update job's auto complete properties. boundJob.OnAllTasksComplete = OnAllTasksComplete.TerminateJob; Assert.Equal(OnAllTasksComplete.TerminateJob, boundJob.OnAllTasksComplete); } }
public void GetPoolsListTest() { BatchSharedKeyCredentials credentials = ClientUnitTestCommon.CreateDummySharedKeyCredential(); using (BatchClient client = BatchClient.Open(credentials)) { Protocol.RequestInterceptor interceptor = new Protocol.RequestInterceptor(baseRequest => { var request = (Protocol.BatchRequest <Models.PoolListOptions, AzureOperationResponse <IPage <Models.CloudPool>, Models.PoolListHeaders> >)baseRequest; request.ServiceRequestFunc = async(token) => { var response = new AzureOperationResponse <IPage <Models.CloudPool>, Models.PoolListHeaders> { Body = new FakePage <Models.CloudPool>(new List <Models.CloudPool> { new Models.CloudPool { DisplayName = "batch-test" }, new Models.CloudPool { DisplayName = "foobar", CloudServiceConfiguration = new Models.CloudServiceConfiguration("3"), AllocationState = Models.AllocationState.Steady }, }) }; Task <AzureOperationResponse <IPage <Models.CloudPool>, Models.PoolListHeaders> > task = Task.FromResult(response); return(await task); }; }); IPagedEnumerable <Microsoft.Azure.Batch.CloudPool> asyncPools = client.PoolOperations.ListPools(additionalBehaviors: new List <BatchClientBehavior> { interceptor }); var pools = new List <Microsoft.Azure.Batch.CloudPool>(asyncPools); Assert.Equal(2, pools.Count); Assert.Equal("batch-test", pools[0].DisplayName); Assert.Equal("foobar", pools[1].DisplayName); // enums are in the same namespace. Assert.Equal(AllocationState.Steady, pools[1].AllocationState); } }
public void CheckIfGetApplicationPackageReferencesIsReadableButNotWritableOnABoundPool() { const string applicationId = "blender.exe"; const string version = "blender"; BatchSharedKeyCredentials credentials = ClientUnitTestCommon.CreateDummySharedKeyCredential(); using (BatchClient client = BatchClient.Open(credentials)) { Protocol.RequestInterceptor interceptor = new Protocol.RequestInterceptor( baseRequest => { var request = (Protocol.BatchRequest <Models.PoolGetOptions, AzureOperationResponse <Models.CloudPool, Models.PoolGetHeaders> >)baseRequest; request.ServiceRequestFunc = (token) => { var response = new AzureOperationResponse <Models.CloudPool, Models.PoolGetHeaders> { Body = new Models.CloudPool { ApplicationPackageReferences = new[] { new Protocol.Models.ApplicationPackageReference { ApplicationId = applicationId, Version = version } } } }; return(Task.FromResult(response)); }; }); Microsoft.Azure.Batch.CloudPool cloudPool = client.PoolOperations.GetPool("pool-id", additionalBehaviors: new List <BatchClientBehavior> { interceptor }); Assert.Throws <InvalidOperationException>(() => cloudPool.ApplicationPackageReferences.First().ApplicationId = applicationId); Assert.Throws <InvalidOperationException>(() => cloudPool.ApplicationPackageReferences.First().Version = version); Assert.Equal(cloudPool.ApplicationPackageReferences.First().Version, version); Assert.Equal(cloudPool.ApplicationPackageReferences.First().ApplicationId, applicationId); } }
public async Task AddTaskCollectionNoHandlerThrows() { const string dummyJobId = "Dummy"; using (BatchClient batchCli = BatchClient.Open(ClientUnitTestCommon.CreateDummySharedKeyCredential())) { //Clear the behaviors so that there is no way there is a AddTaskResultHandler defined batchCli.CustomBehaviors.Clear(); CloudTask task = new CloudTask("Foo", "Bar"); BatchClientException exception = await Assert.ThrowsAsync <BatchClientException>(() => batchCli.JobOperations.AddTaskAsync(dummyJobId, new List <CloudTask> { task })); string expectedString = string.Format(BatchErrorMessages.GeneralBehaviorMissing, typeof(AddTaskCollectionResultHandler)); Assert.Equal(expectedString, exception.Message); } }