public void CannotModifyDependenciesOfABoundTask()
        {
            using (BatchClient client = ClientUnitTestCommon.CreateDummyClient())
            {
                var returnFakeTasks = ClientUnitTestCommon.SimulateServiceResponse <Models.TaskGetOptions, AzureOperationResponse <Models.CloudTask, Models.TaskGetHeaders> >(_ =>
                                                                                                                                                                              new AzureOperationResponse <Models.CloudTask, Models.TaskGetHeaders>
                {
                    Body = new Protocol.Models.CloudTask
                    {
                        DependsOn = new Protocol.Models.TaskDependencies
                        {
                            TaskIdRanges = new[] { new Protocol.Models.TaskIdRange(5, 15) },
                            TaskIds      = new List <string> {
                                "5"
                            }
                        },
                        Id    = "5",
                        State = Models.TaskState.Active,
                    }
                });

                var task = client.JobOperations.GetTask("job", "5", additionalBehaviors: returnFakeTasks);

                var newDependencies = Microsoft.Azure.Batch.TaskDependencies.OnId("hello");
                Assert.Throws <InvalidOperationException>(() => task.DependsOn = newDependencies);
            }
        }
        public void WhenATaskIsRetrievedFromTheService_ItsDependenciesAreSurfacedCorrectly()
        {
            using (BatchClient client = ClientUnitTestCommon.CreateDummyClient())
            {
                var returnFakeTasks = ClientUnitTestCommon.SimulateServiceResponse <Models.TaskListOptions, AzureOperationResponse <IPage <Models.CloudTask>, Models.TaskListHeaders> >(_ =>
                                                                                                                                                                                        new AzureOperationResponse <IPage <Models.CloudTask>, Models.TaskListHeaders>
                {
                    Body = new FakePage <Models.CloudTask>(new []
                    {
                        new Protocol.Models.CloudTask
                        {
                            DependsOn = new Protocol.Models.TaskDependencies
                            {
                                TaskIdRanges = new[] { new Protocol.Models.TaskIdRange(5, 15) },
                                TaskIds      = new List <string> {
                                    "5"
                                }
                            },
                            Id    = "5",
                            State = Models.TaskState.Active,
                        }
                    })
                });

                var tasks = client.JobOperations.ListTasks("job", additionalBehaviors: returnFakeTasks).ToList();

                var taskDependencies = tasks.First().DependsOn;

                Assert.Equal("5", taskDependencies.TaskIds.Single());
                Assert.Equal(5, taskDependencies.TaskIdRanges.Single().Start);
                Assert.Equal(15, taskDependencies.TaskIdRanges.Single().End);
            }
        }
        public void WhenAddingTasksWithDependenciesToAJob_TheDependenciesAreCorrectlyIncludedInTheRestProxy()
        {
            const string jobId = "id-123";

            using (BatchClient client = ClientUnitTestCommon.CreateDummyClient())
            {
                var returnFakeJob = ClientUnitTestCommon.SimulateServiceResponse <Models.JobGetOptions, AzureOperationResponse <Models.CloudJob, Models.JobGetHeaders> >(_ =>
                                                                                                                                                                         new AzureOperationResponse <Models.CloudJob, Models.JobGetHeaders>
                {
                    Body = new Protocol.Models.CloudJob {
                        Id = jobId, UsesTaskDependencies = true
                    }
                });

                var verifyTaskDependencies = ClientUnitTestCommon.SimulateServiceResponse <Models.TaskAddParameter, Models.TaskAddOptions, AzureOperationHeaderResponse <Models.TaskAddHeaders> >((parameters, options) =>
                {
                    Assert.Equal((Int32.Parse(parameters.Id) - 10).ToString(), parameters.DependsOn.TaskIds.Single());
                    Assert.Equal(5, parameters.DependsOn.TaskIdRanges.Single().Start);
                    Assert.Equal(15, parameters.DependsOn.TaskIdRanges.Single().End);

                    return(new AzureOperationHeaderResponse <Models.TaskAddHeaders>()
                    {
                        Response = new HttpResponseMessage(HttpStatusCode.Accepted)
                    });
                });


                var job = client.JobOperations.GetJob(jobId, additionalBehaviors: returnFakeJob);

                for (int j = 0; j < 5; j++)
                {
                    var taskWithDependencies = new Microsoft.Azure.Batch.CloudTask((10 + j).ToString(), "cmd /c hostname")
                    {
                        DependsOn = new Microsoft.Azure.Batch.TaskDependencies(new[] { j.ToString() }, new[] { new Microsoft.Azure.Batch.TaskIdRange(5, 15) }),
                    };

                    job.AddTask(taskWithDependencies, additionalBehaviors: verifyTaskDependencies);  // assertions happen in the callback
                }
            }
        }
Exemple #4
0
        public void CreateCloudTaskWithApplicationPackageReferences()
        {
            const string jobId              = "id-123";
            const string taskId             = "id-123";
            const string applicationId      = "testApp";
            const string applicationVersion = "beta";

            using (BatchClient client = ClientUnitTestCommon.CreateDummyClient())
            {
                Models.CloudJob returnFakeJob = new Models.CloudJob(jobId);
                var             job           = client.JobOperations.GetJob(jobId, additionalBehaviors: InterceptorFactory.CreateGetJobRequestInterceptor(returnFakeJob));

                var verifyAPRs = ClientUnitTestCommon.SimulateServiceResponse <Models.TaskAddParameter, Models.TaskAddOptions, AzureOperationHeaderResponse <Models.TaskAddHeaders> >(
                    (parameters, options) =>
                {
                    Assert.Equal(applicationId, parameters.ApplicationPackageReferences.First().ApplicationId);
                    Assert.Equal(applicationVersion, parameters.ApplicationPackageReferences.First().Version);

                    return(new AzureOperationHeaderResponse <Models.TaskAddHeaders>
                    {
                        Response = new HttpResponseMessage(HttpStatusCode.Accepted)
                    });
                });

                var taskWithAPRs = new CloudTask(taskId, "cmd /c hostname")
                {
                    ApplicationPackageReferences = new List <ApplicationPackageReference>
                    {
                        new ApplicationPackageReference
                        {
                            ApplicationId = applicationId,
                            Version       = applicationVersion
                        }
                    }
                };

                job.AddTask(taskWithAPRs, additionalBehaviors: verifyAPRs);  // assertions happen in the callback
            }
        }
Exemple #5
0
        public async Task ExitConditionsAreSentOnTask()
        {
            const string jobId  = "id-123";
            const string taskId = "id-001";

            using (BatchClient client = ClientUnitTestCommon.CreateDummyClient())
            {
                Models.CloudJob protoJob = new Models.CloudJob(id: jobId, onAllTasksComplete: Models.OnAllTasksComplete.NoAction, onTaskFailure: Models.OnTaskFailure.PerformExitOptionsJobAction);

                var fakeAddTaskResponse = ClientUnitTestCommon.SimulateServiceResponse <Models.TaskAddParameter, Models.TaskAddOptions, AzureOperationHeaderResponse <Models.TaskAddHeaders> >((parameters, options) =>
                {
                    Assert.Equal((Models.JobAction?)JobAction.Terminate, parameters.ExitConditions.DefaultProperty.JobAction);
                    Assert.Equal(0, parameters.ExitConditions.ExitCodeRanges.First().Start);
                    Assert.Equal(4, parameters.ExitConditions.ExitCodeRanges.First().End);
                    Assert.Equal((Models.JobAction?)JobAction.Disable, parameters.ExitConditions.ExitCodeRanges.First().ExitOptions.JobAction);
                    // These need to be compared as strings because they are different types but we are interested in the values being the same.
                    Assert.Equal(DependencyAction.Satisfy.ToString(), parameters.ExitConditions.ExitCodeRanges.First().ExitOptions.DependencyAction.ToString());
                    Assert.Equal(3, parameters.ExitConditions.ExitCodes.First().Code);
                    Assert.Equal((Models.JobAction?)JobAction.None, parameters.ExitConditions.ExitCodes.First().ExitOptions.JobAction);
                    Assert.Equal((Models.JobAction?)JobAction.Terminate, parameters.ExitConditions.FileUploadError.JobAction);

                    return(new AzureOperationHeaderResponse <Models.TaskAddHeaders>()
                    {
                        Response = new HttpResponseMessage(HttpStatusCode.Accepted)
                    });
                });

                CloudJob boundJob = await client.JobOperations.GetJobAsync(string.Empty, additionalBehaviors : InterceptorFactory.CreateGetJobRequestInterceptor(protoJob));

                boundJob.OnAllTasksComplete = OnAllTasksComplete.TerminateJob;

                CloudTask cloudTask = new CloudTask(taskId, "cmd /c echo hello world");

                cloudTask.ExitConditions = new ExitConditions()
                {
                    Default = new ExitOptions()
                    {
                        JobAction = JobAction.Terminate
                    },
                    ExitCodeRanges = new List <ExitCodeRangeMapping>()
                    {
                        new ExitCodeRangeMapping(0, 4, new ExitOptions()
                        {
                            DependencyAction = DependencyAction.Satisfy, JobAction = JobAction.Disable,
                        })
                    },
                    ExitCodes = new List <ExitCodeMapping>()
                    {
                        new ExitCodeMapping(3, new ExitOptions()
                        {
                            JobAction = JobAction.None
                        })
                    },
                    PreProcessingError = new ExitOptions()
                    {
                        JobAction = JobAction.Terminate
                    },
                    FileUploadError = new ExitOptions()
                    {
                        JobAction = JobAction.Terminate
                    }
                };

                boundJob.AddTask(cloudTask, additionalBehaviors: fakeAddTaskResponse);
            }
        }
        public async Task CreateTaskWithExitConditionsTest()
        {
            const string jobId  = "id-123";
            const string taskId = "id-001";

            BatchSharedKeyCredentials credentials = ClientUnitTestCommon.CreateDummySharedKeyCredential();

            using (BatchClient client = await BatchClient.OpenAsync(credentials))
            {
                Models.CloudJob protoJob = new Models.CloudJob(id: jobId, onAllTasksComplete: Models.OnAllTasksComplete.NoAction, onTaskFailure: Models.OnTaskFailure.PerformExitOptionsJobAction);

                var fakeAddTaskResponse = ClientUnitTestCommon.SimulateServiceResponse <Models.TaskAddParameter, Models.TaskAddOptions, AzureOperationHeaderResponse <Models.TaskAddHeaders> >((parameters, options) =>
                {
                    Assert.Equal((Models.JobAction?)JobAction.Terminate, parameters.ExitConditions.DefaultProperty.JobAction);
                    Assert.Equal(0, parameters.ExitConditions.ExitCodeRanges.First().Start);
                    Assert.Equal(4, parameters.ExitConditions.ExitCodeRanges.First().End);
                    Assert.Equal((Models.JobAction?)JobAction.Disable, parameters.ExitConditions.ExitCodeRanges.First().ExitOptions.JobAction);
                    Assert.Equal(3, parameters.ExitConditions.ExitCodes.First().Code);
                    Assert.Equal((Models.JobAction?)JobAction.None, parameters.ExitConditions.ExitCodes.First().ExitOptions.JobAction);

                    return(new AzureOperationHeaderResponse <Models.TaskAddHeaders>()
                    {
                        Response = new HttpResponseMessage(HttpStatusCode.Accepted)
                    });
                });

                CloudJob boundJob = await client.JobOperations.GetJobAsync(string.Empty, additionalBehaviors : InterceptorFactory.CreateGetJobRequestInterceptor(protoJob));

                boundJob.OnAllTasksComplete = OnAllTasksComplete.TerminateJob;

                // Cannot change OnTaskFailure on a bound job
                Assert.Throws <InvalidOperationException>(() => boundJob.OnTaskFailure = OnTaskFailure.NoAction);

                CloudTask cloudTask = new CloudTask(taskId, "cmd /c echo hello world");

                cloudTask.ExitConditions = new ExitConditions()
                {
                    Default = new ExitOptions()
                    {
                        JobAction = JobAction.Terminate
                    },
                    ExitCodeRanges = new List <ExitCodeRangeMapping>()
                    {
                        new ExitCodeRangeMapping(0, 4, new ExitOptions()
                        {
                            JobAction = JobAction.Disable
                        })
                    },
                    ExitCodes = new List <ExitCodeMapping>()
                    {
                        new ExitCodeMapping(3, new ExitOptions()
                        {
                            JobAction = JobAction.None
                        })
                    },
                    SchedulingError = new ExitOptions()
                    {
                        JobAction = JobAction.Terminate
                    },
                };

                boundJob.AddTask(cloudTask, additionalBehaviors: fakeAddTaskResponse);
            }
        }