Ejemplo n.º 1
0
        /// <summary>
        /// Determines if an exception is a "NotFound" exception from the Batch service
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static bool IsExceptionNotFound(Exception e)
        {
            AggregateException aggEx          = e as AggregateException;
            BatchException     batchException = e as BatchException;

            if (aggEx != null)
            {
                aggEx = aggEx.Flatten(); //Flatten the aggregate exception

                batchException = aggEx.InnerExceptions.Cast <BatchException>().FirstOrDefault();
            }

            if (batchException != null)
            {
                if (batchException.RequestInformation != null &&
                    batchException.RequestInformation.BatchError != null)
                {
                    if (batchException.RequestInformation.HttpStatusCode == HttpStatusCode.NotFound)
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Works with AggregateException or "Exception" just pass it in :)
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="correctCode"></param>
        public static void AssertIsBatchExceptionAndHasCorrectAzureErrorCode(Exception ex, string correctCode, ITestOutputHelper outputHelper)
        {
            Exception theOneInner = ex;

            if (ex is AggregateException ae)
            {
                // some ugly mechanics all for confirming we get the correct exception and it has the correct "message"
                // there can be only one
                Assert.Single(ae.InnerExceptions);

                // get the one exception
                theOneInner = ae.InnerExceptions[0];
            }

            if (!(theOneInner is BatchException))
            {
                outputHelper.WriteLine(string.Format("AssertIsBatchExceptionAndHasCorrectAzureErrorCode: incorrect exception: {0}", ex.ToString()));
            }

            // it must have the correct type
            Assert.IsAssignableFrom <BatchException>(theOneInner);

            BatchException be = (BatchException)theOneInner;

            // whew we got it!  check the message
            Assert.Equal(expected: correctCode, actual: be.RequestInformation.BatchError.Code);
        }
        public void ReadClientRequestIdAndRequestIdFromException()
        {
            void test()
            {
                using BatchClient batchCli = TestUtilities.OpenBatchClient(TestUtilities.GetCredentialsFromEnvironment());
                Guid myClientRequestid = Guid.NewGuid();
                RequestInterceptor clientRequestIdGenerator = new ClientRequestIdProvider(
                    request => myClientRequestid);

                RequestInterceptor setReturnClientRequestId = new RequestInterceptor(
                    request =>
                {
                    request.Options.ReturnClientRequestId = true;
                });

                BatchException batchException = TestUtilities.AssertThrowsAsync <BatchException>(async() =>
                                                                                                 await batchCli.JobOperations.GetJobAsync("this-job-doesnt-exist", additionalBehaviors: new[] { clientRequestIdGenerator, setReturnClientRequestId })).Result;

                Assert.NotNull(batchException.RequestInformation);
                Assert.NotNull(batchException.RequestInformation.BatchError);
                Assert.NotNull(batchException.RequestInformation.BatchError.Message);

                Assert.Equal(BatchErrorCodeStrings.JobNotFound, batchException.RequestInformation.BatchError.Code);
                Assert.Equal(HttpStatusCode.NotFound, batchException.RequestInformation.HttpStatusCode);
                Assert.Contains("The specified job does not exist", batchException.RequestInformation.BatchError.Message.Value);

                Assert.Equal(myClientRequestid, batchException.RequestInformation.ClientRequestId);
                Assert.NotNull(batchException.RequestInformation.ServiceRequestId);
                Assert.Equal("The specified job does not exist.", batchException.RequestInformation.HttpStatusMessage);
            }

            SynchronizationContextHelper.RunTest(test, TestTimeout);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Extracts failure details from the BatchException object to create a more informative error message for the user.
        /// </summary>
        /// <param name="ex">The BatchException object</param>
        private void HandleBatchException(BatchException ex)
        {
            if (ex != null)
            {
                if (ex.RequestInformation != null && ex.RequestInformation.AzureError != null)
                {
                    StringBuilder str = new StringBuilder(ex.Message).AppendLine();

                    str.AppendFormat("Error Code: {0}", ex.RequestInformation.AzureError.Code).AppendLine();
                    str.AppendFormat("Error Message: {0}", ex.RequestInformation.AzureError.Message.Value).AppendLine();
                    str.AppendFormat("Client Request ID:{0}", ex.RequestInformation.ClientRequestId).AppendLine();
                    if (ex.RequestInformation.AzureError.Values != null)
                    {
                        foreach (AzureErrorDetail detail in ex.RequestInformation.AzureError.Values)
                        {
                            str.AppendFormat("{0}:{1}", detail.Key, detail.Value).AppendLine();
                        }
                    }
                    WriteExceptionError(new BatchException(ex.RequestInformation, str.ToString(), ex.InnerException));
                }
                else
                {
                    WriteExceptionError(ex);
                }
            }
        }
Ejemplo n.º 5
0
        private void HandleTaskCompletion(Task t)
        {
            this.IsCompleted = true;
            this.EndTimeUtc  = DateTime.UtcNow;
            if (t.Status == TaskStatus.RanToCompletion)
            {
                this.CompletedSuccessfully = true;
            }
            else if (t.Status == TaskStatus.Faulted ||
                     t.Status == TaskStatus.Canceled)
            {
                this.CompletedSuccessfully = false;
                this.FaultException        = t.Exception;

                //Process exception for client/server request ID if applicable
                if (this.FaultException is BatchException)
                {
                    BatchException batchE = this.FaultException as BatchException;
                    this.FaultClientRequestId = batchE.RequestInformation.ClientRequestID;
                    this.FaultServerRequestId = batchE.RequestInformation.ServiceRequestID;
                }
                else if (this.FaultException is AggregateException)
                {
                    //If there is only 1 batch exception we hit
                    AggregateException agg = this.FaultException as AggregateException;
                    agg = agg.Flatten();

                    BatchException batchE = agg.InnerExceptions.Where(e => e is BatchException).Cast <BatchException>().First();
                    this.FaultClientRequestId = batchE.RequestInformation.ClientRequestID;
                    this.FaultServerRequestId = batchE.RequestInformation.ServiceRequestID;
                }
            }
        }
        public void TestBatchExceptionCreatedFromBatchErrorWithNoBody()
        {
            Protocol.Models.BatchErrorException batchErrorException = new Protocol.Models.BatchErrorException()
            {
                Body     = null, //Body is null
                Response = new HttpResponseMessageWrapper(new HttpResponseMessage(HttpStatusCode.Accepted), string.Empty)
            };
            BatchException batchException = new BatchException(batchErrorException);

            Assert.Null(batchException.RequestInformation.BatchError);
        }
Ejemplo n.º 7
0
        protected override void ProcessRecord()
        {
            try
            {
                Validate.ValidateInternetConnection();
                ProcessRecord();
                OnProcessRecord();
            }
            catch (AggregateException ex)
            {
                // When the OM encounters an error, it'll throw an AggregateException with a nested BatchException.
                // BatchExceptions have special handling to extract detailed failure information.  When an AggregateException
                // is encountered, loop through the inner exceptions.  If there's a nested BatchException, perform the
                // special handling.  Otherwise, just write out the error.
                AggregateException flattened = ex.Flatten();
                foreach (Exception inner in flattened.InnerExceptions)
                {
                    BatchException asBatch = inner as BatchException;
                    if (asBatch != null)
                    {
                        HandleBatchException(asBatch);
                    }
                    else
                    {
                        WriteExceptionError(inner);
                    }
                }
            }
            catch (BatchException ex)
            {
                HandleBatchException(ex);
            }
            catch (CloudException ex)
            {
                var updatedEx = ex;

                if (ex.Response != null && ex.Response.Content != null)
                {
                    var message = FindDetailedMessage(ex.Response.Content);

                    if (message != null)
                    {
                        updatedEx = new CloudException(message, ex);
                    }
                }

                WriteExceptionError(updatedEx);
            }
            catch (Exception ex)
            {
                WriteExceptionError(ex);
            }
        }
Ejemplo n.º 8
0
        public void Bug1965363_2384616_Wat7OSVersionFeatures()
        {
            Action test = () =>
            {
                using (BatchClient batchCli = TestUtilities.OpenBatchClientAsync(TestUtilities.GetCredentialsFromEnvironment()).Result)
                {
                    PoolOperations poolOperations = batchCli.PoolOperations;
                    try
                    {
                        this.testOutputHelper.WriteLine("Listing OS Versions:");

                        // create pool tests

                        // forget to set CloudServiceConfiguration on Create, get error
                        {
                            CloudPool noArgs = poolOperations.CreatePool("Bug1965363ButNoOSFamily-" + TestUtilities.GetMyName(), PoolFixture.VMSize, default(CloudServiceConfiguration), targetDedicatedComputeNodes: 0);

                            BatchException ex    = TestUtilities.AssertThrows <BatchException>(() => noArgs.Commit());
                            string         exStr = ex.ToString();

                            // we are expecting an exception, assert if the exception is not the correct one.
                            Assert.Contains("cloudServiceConfiguration", exStr);
                        }

                        // create a pool WITH an osFamily
                        {
                            string poolIdHOSF = "Bug1965363HasOSF-" + TestUtilities.GetMyName();
                            try
                            {
                                CloudPool hasOSF = poolOperations.CreatePool(poolIdHOSF, PoolFixture.VMSize, new CloudServiceConfiguration(PoolFixture.OSFamily), targetDedicatedComputeNodes: 0);

                                hasOSF.Commit();
                            }
                            finally
                            {
                                poolOperations.DeletePool(poolIdHOSF);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        // special case os version beacuse it is a common failure and requires human intervention/editing
                        // test for expired os version
                        Assert.DoesNotContain("The specified OS Version does not exists", ex.ToString());

                        throw;
                    }
                }
            };

            SynchronizationContextHelper.RunTest(test, TestTimeout);
        }
        /// <summary>
        /// Adds a test certificate for use in Scenario tests. Returns the thumbprint of the cert.
        /// </summary>
        public static string AddTestCertificate(BatchController controller, BatchAccountContext context, string filePath)
        {
            RequestInterceptor interceptor = CreateHttpRecordingInterceptor();

            BatchClientBehavior[] behaviors = new BatchClientBehavior[] { interceptor };
            BatchClient           client    = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient);

            X509Certificate2       cert          = new X509Certificate2(filePath);
            ListCertificateOptions getParameters = new ListCertificateOptions(context, behaviors)
            {
                ThumbprintAlgorithm = BatchTestHelpers.TestCertificateAlgorithm,
                Thumbprint          = cert.Thumbprint,
                Select = "thumbprint,state"
            };

            try
            {
                PSCertificate existingCert = client.ListCertificates(getParameters).FirstOrDefault();
                DateTime      start        = DateTime.Now;
                DateTime      end          = start.AddMinutes(5);

                // Cert might still be deleting from other tests, so we wait for the delete to finish.
                while (existingCert != null && existingCert.State == CertificateState.Deleting)
                {
                    if (DateTime.Now > end)
                    {
                        throw new TimeoutException("Timed out waiting for existing cert to be deleted.");
                    }
                    Sleep(5000);
                    existingCert = client.ListCertificates(getParameters).FirstOrDefault();
                }
            }
            catch (AggregateException ex)
            {
                foreach (Exception inner in ex.InnerExceptions)
                {
                    BatchException batchEx = inner as BatchException;
                    // When the cert doesn't exist, we get a 404 error. For all other errors, throw.
                    if (batchEx == null || !batchEx.Message.Contains("CertificateNotFound"))
                    {
                        throw;
                    }
                }
            }

            NewCertificateParameters parameters = new NewCertificateParameters(context, null, cert.RawData, behaviors);

            client.AddCertificate(parameters);

            return(cert.Thumbprint);
        }
Ejemplo n.º 10
0
 /// <summary>
 /// Extracts failure details from the BatchException object to create a more informative error message for the user.
 /// </summary>
 /// <param name="ex">The BatchException object</param>
 private void HandleBatchException(BatchException ex)
 {
     if (ex != null)
     {
         if (ex.RequestInformation != null && ex.RequestInformation.BatchError != null)
         {
             WriteExceptionError(new BatchException(ex.RequestInformation, ex.ToString(), ex.InnerException));
         }
         else
         {
             WriteExceptionError(ex);
         }
     }
 }
        public void TestBatchExceptionCreatedWithRetryAfterDuration()
        {
            Protocol.Models.BatchErrorException batchErrorException = new Protocol.Models.BatchErrorException()
            {
                Body     = null, //Body is null
                Response = new HttpResponseMessageWrapper(new HttpResponseMessage(HttpStatusCode.Accepted), string.Empty)
            };
            batchErrorException.Response.Headers.Add(InternalConstants.RetryAfterHeader, new List <string> {
                "10"
            });
            BatchException batchException = new BatchException(batchErrorException);

            Assert.Equal(TimeSpan.FromSeconds(10), batchException.RequestInformation.RetryAfter);
        }
Ejemplo n.º 12
0
        private static CloudPool CreatePool(Settings unzipperSettings, BatchClient client)
        {
            //OSFamily 4 == OS 2012 R2. You can learn more about os families and versions at:
            //http://msdn.microsoft.com/en-us/library/azure/ee924680.aspx
            CloudPool pool = client.PoolOperations.CreatePool(
                poolId: unzipperSettings.PoolId,
                targetDedicated: unzipperSettings.PoolNodeCount,
                virtualMachineSize: unzipperSettings.MachineSize,
                cloudServiceConfiguration: new CloudServiceConfiguration(osFamily: "4"));

            pool.MaxTasksPerComputeNode = unzipperSettings.MaxTasksPerNode;
            Console.WriteLine("Adding pool {0}", unzipperSettings.PoolId);

            try
            {
                pool.Commit();
            }
            catch (AggregateException ae)
            {
                // Go through all exceptions and dump useful information
                ae.Handle(x =>
                {
                    Console.Error.WriteLine("Creating pool ID {0} failed", unzipperSettings.PoolId);
                    if (x is BatchException)
                    {
                        BatchException be = x as BatchException;

                        Console.WriteLine(be.ToString());
                        Console.WriteLine();
                    }
                    else
                    {
                        Console.WriteLine(x);
                    }

                    // can't continue without a pool
                    return(false);
                });
            }
            catch (BatchException be)
            {
                if (be.Message.Contains("conflict"))
                {
                    Console.WriteLine("pool already exists");
                }
            }

            return(pool);
        }
Ejemplo n.º 13
0
        public async Task LinearRetryRetriesOnBatchException()
        {
            TimeSpan    interval    = TimeSpan.FromSeconds(5);
            const int   maxRetries  = 10;
            LinearRetry linearRetry = new LinearRetry(interval, maxRetries);

            RequestInformation reqInfo = new RequestInformation()
            {
                HttpStatusCode = HttpStatusCode.InternalServerError
            };
            BatchException batchException = new BatchException(reqInfo, "Message", null);

            RetryDecision retryDecision = await linearRetry.ShouldRetryAsync(batchException, new OperationContext());

            Assert.Equal(interval, retryDecision.RetryDelay);
            Assert.Equal(true, retryDecision.ShouldRetry);
        }
        public void TestBatchExceptionCreatedWithRetryAfterDateTime()
        {
            DateTime retryAfter       = DateTime.UtcNow.Add(TimeSpan.FromSeconds(10));
            string   retryAfterString = retryAfter.ToString("r");

            Protocol.Models.BatchErrorException batchErrorException = new Protocol.Models.BatchErrorException()
            {
                Body     = null, //Body is null
                Response = new HttpResponseMessageWrapper(new HttpResponseMessage(HttpStatusCode.Accepted), string.Empty)
            };
            batchErrorException.Response.Headers.Add(InternalConstants.RetryAfterHeader, new List <string> {
                retryAfterString
            });
            BatchException batchException = new BatchException(batchErrorException);

            Assert.True(TimeSpan.FromSeconds(9) <= batchException.RequestInformation.RetryAfter && batchException.RequestInformation.RetryAfter <= TimeSpan.FromSeconds(10));
        }
        public void TestBatchExceptionCreatedWithRetryAfterDateTime()
        {
            DateTime retryAfter       = DateTime.UtcNow.Add(TimeSpan.FromSeconds(10));
            string   retryAfterString = retryAfter.ToString("r");

            Protocol.Models.BatchErrorException batchErrorException = new Protocol.Models.BatchErrorException()
            {
                Body     = null, //Body is null
                Response = new HttpResponseMessageWrapper(new HttpResponseMessage(HttpStatusCode.Accepted), string.Empty)
            };
            batchErrorException.Response.Headers.Add(InternalConstants.RetryAfterHeader, new List <string> {
                retryAfterString
            });
            BatchException batchException = new BatchException(batchErrorException);

            this.testOutputHelper.WriteLine($"RetryAfter: {batchException.RequestInformation.RetryAfter}");
            // Give some wiggle room in case tests are running slow
            Assert.True(batchException.RequestInformation.RetryAfter <= TimeSpan.FromSeconds(10));
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Completes the task of applying the user's changes to the form.
        /// </summary>
        /// <param name="exitDialog">If true, the form is dismiss.</param>
        /// <param name="batchException">Contains any exceptions from processing the server update.</param>
        private void PostEnd(bool exitDialog, BatchException batchException)
        {
            // Display any errors from the background processing.
            if (batchException != null)
            {
                foreach (Exception exception in batchException.Exceptions)
                {
                    MessageBox.Show(this, exception.Message, "Guardian Error");
                }
            }

            // If there were no exceptions and the user asked to exit the dialog, it is dismissed.
            if (batchException == null && exitDialog)
            {
                Close();
            }

            // This will re-initialize the order entry dialog with the user's preferences.
            ThreadPool.QueueUserWorkItem(new WaitCallback(InitializationThread));
        }
Ejemplo n.º 17
0
        public async Task ExponentialRetryRetriesOnBatchException()
        {
            TimeSpan         interval         = TimeSpan.FromSeconds(5);
            const int        maxRetries       = 10;
            ExponentialRetry exponentialRetry = new ExponentialRetry(interval, maxRetries);

            RequestInformation reqInfo = new RequestInformation()
            {
                HttpStatusCode = HttpStatusCode.InternalServerError
            };
            BatchException batchException = new BatchException(reqInfo, "Message", null);

            OperationContext context = new OperationContext();

            context.RequestResults.Add(new RequestResult(new RequestInformation(), batchException));

            RetryDecision retryDecision = await exponentialRetry.ShouldRetryAsync(batchException, context);

            Assert.Equal(interval, retryDecision.RetryDelay);
            Assert.Equal(true, retryDecision.ShouldRetry);
        }
Ejemplo n.º 18
0
        public void CustomAggregateToStringIncludesCallstack()
        {
            Guid clientRequestId = Guid.NewGuid();

            BatchException e1 = ThrowWithStack <BatchException>(() => {
                throw new BatchException(new RequestInformation()
                {
                    BatchError      = new BatchError(new Microsoft.Azure.Batch.Protocol.Models.BatchError(code: "Foo")),
                    ClientRequestId = clientRequestId,
                },
                                         "Test",
                                         null);
            });
            ArgumentNullException e2 = ThrowWithStack <ArgumentNullException>(() =>
            {
                throw new ArgumentNullException();
            });

            ParallelOperationsException parallelOperationsException = new ParallelOperationsException(new Exception[] { e1, e2 });
            string exceptionText = parallelOperationsException.ToString();

            Assert.Contains(typeof(ParallelOperationsException).Name, exceptionText);
            Assert.Contains("Exception #0", exceptionText);
            Assert.Contains("Exception #1", exceptionText);
            Assert.Contains("One or more errors occurred", exceptionText);

            //Ensure each exception logs a stacktrace from the ToString(). The stacktrace should include this test method's name.

            IEnumerable <string> innerExceptionDumps = GetInnerExceptionText(exceptionText);

            foreach (string innerExceptionText in innerExceptionDumps)
            {
                Assert.Contains("CustomAggregateToStringIncludesCallstack", innerExceptionText);
            }

            //Ensure that our BatchException was ToString()'ed correctly
            Assert.Contains(clientRequestId.ToString(), exceptionText);
        }
        public async Task ExponentialRetryHonorsRetryAfter()
        {
            TimeSpan         interval         = TimeSpan.FromSeconds(60);
            TimeSpan         retryAfter       = TimeSpan.FromSeconds(10);
            const int        maxRetries       = 10;
            ExponentialRetry exponentialRetry = new ExponentialRetry(interval, maxRetries);

            RequestInformation reqInfo = new RequestInformation()
            {
                HttpStatusCode = (HttpStatusCode)429,
                RetryAfter     = retryAfter
            };
            BatchException batchException = new BatchException(reqInfo, "Message", null);

            OperationContext context = new OperationContext();

            context.RequestResults.Add(new RequestResult(new RequestInformation(), batchException));

            RetryDecision retryDecision = await exponentialRetry.ShouldRetryAsync(batchException, context);

            Assert.Equal(retryAfter, retryDecision.RetryDelay);
            Assert.True(retryDecision.ShouldRetry);
        }
        /// <summary>
        /// Creates an MPI pool.
        /// </summary>
        public static void CreateMpiPoolIfNotExists(BatchController controller, BatchAccountContext context, int targetDedicated = 3)
        {
            BatchClient     client      = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient);
            ListPoolOptions listOptions = new ListPoolOptions(context)
            {
                PoolId = MpiPoolId
            };

            try
            {
                client.ListPools(listOptions);
                return; // The call returned without throwing an exception, so the pool exists
            }
            catch (AggregateException aex)
            {
                BatchException innerException = aex.InnerException as BatchException;
                if (innerException == null || innerException.RequestInformation == null || innerException.RequestInformation.AzureError == null ||
                    innerException.RequestInformation.AzureError.Code != BatchErrorCodeStrings.PoolNotFound)
                {
                    throw;
                }
                // We got the pool not found error, so continue and create the pool
            }

            string blobUrl = UploadBlobAndGetUrl(MpiSetupFileContainer, MpiSetupFileName, MpiSetupFileLocalPath);

            StartTask startTask = new StartTask();

            startTask.CommandLine   = string.Format("cmd /c set & {0} -unattend -force", MpiSetupFileName);
            startTask.ResourceFiles = new List <ResourceFile>();
            startTask.ResourceFiles.Add(new ResourceFile(blobUrl, MpiSetupFileName));
            startTask.RunElevated    = true;
            startTask.WaitForSuccess = true;

            CreateTestPool(controller, context, MpiPoolId, targetDedicated, startTask: startTask);
        }
        public void TestBoundJobVerbs()
        {
            void test()
            {
                using BatchClient batchCli = TestUtilities.OpenBatchClient(TestUtilities.GetCredentialsFromEnvironment());
                //Create a job

                string jobId = Constants.DefaultConveniencePrefix + TestUtilities.GetMyName() + "-TestBoundJobVerbs";

                try
                {
                    CloudJob cloudJob = batchCli.JobOperations.CreateJob(jobId, new PoolInformation());
                    cloudJob.PoolInformation = new PoolInformation()
                    {
                        PoolId = poolFixture.PoolId
                    };
                    cloudJob.Commit();

                    //Get the bound job
                    CloudJob job = batchCli.JobOperations.GetJob(jobId);

                    //Disable the job (via instance)
                    job.Disable(DisableJobOption.Terminate);

                    //Check the job state

                    CloudJob disabledJob = batchCli.JobOperations.GetJob(jobId);
                    testOutputHelper.WriteLine("DisabledJob State: {0}", disabledJob.State);
                    Assert.True(disabledJob.State == JobState.Disabled || disabledJob.State == JobState.Disabling);

                    //Enable the job (via instance)
                    job.Enable();

                    //Check the job state
                    CloudJob enabledJob = batchCli.JobOperations.GetJob(jobId);
                    testOutputHelper.WriteLine("EnabledJob state: {0}", enabledJob.State);
                    Assert.Equal(JobState.Active, JobState.Active);

                    //Disable the job (via operations)
                    batchCli.JobOperations.DisableJob(jobId, DisableJobOption.Terminate);

                    disabledJob = batchCli.JobOperations.GetJob(jobId);
                    testOutputHelper.WriteLine("DisabledJob State: {0}", disabledJob.State);
                    Assert.True(disabledJob.State == JobState.Disabled || disabledJob.State == JobState.Disabling);

                    //Enable the job (via operations)
                    batchCli.JobOperations.EnableJob(jobId);

                    //Check the job state
                    enabledJob = batchCli.JobOperations.GetJob(jobId);
                    testOutputHelper.WriteLine("EnabledJob state: {0}", enabledJob.State);
                    Assert.Equal(JobState.Active, JobState.Active);

                    //Terminate the job
                    job.Terminate("need some reason");

                    //Check the job state
                    CloudJob terminatedJob = batchCli.JobOperations.GetJob(jobId);
                    testOutputHelper.WriteLine("TerminatedJob state: {0}", terminatedJob.State);
                    Assert.True(terminatedJob.State == JobState.Terminating || terminatedJob.State == JobState.Completed);

                    if (terminatedJob.State == JobState.Terminating)
                    {
                        Thread.Sleep(TimeSpan.FromSeconds(5)); //Sleep and wait for the job to finish terminating before we issue a delete
                    }

                    //Delete the job
                    job.Delete();

                    //Check that the job doesn't exist anymore

                    try
                    {
                        testOutputHelper.WriteLine("Expected Exception: testing that job does NOT exist.");

                        CloudJob deletedJob = batchCli.JobOperations.GetJob(jobId);
                        Assert.Equal(JobState.Deleting, deletedJob.State);
                    }
                    catch (Exception e)
                    {
                        Assert.IsAssignableFrom <BatchException>(e);
                        BatchException be = e as BatchException;
                        Assert.NotNull(be.RequestInformation);
                        Assert.NotNull(be.RequestInformation.BatchError);
                        Assert.Equal(BatchErrorCodeStrings.JobNotFound, be.RequestInformation.BatchError.Code);

                        testOutputHelper.WriteLine("Job was deleted successfully");
                    }
                }
                finally
                {
                    TestUtilities.DeleteJobIfExistsAsync(batchCli, jobId).Wait();
                }
            }

            SynchronizationContextHelper.RunTest(test, TestTimeout);
        }
Ejemplo n.º 22
0
        /// <summary>
        /// Begins the task of applying the changes in the form to the data model.
        /// </summary>
        private void PostThread(object parameter)
        {
            // Extract the parameters from the threads parameter.
            object[]    parameters  = (object[])parameter;
            bool        exitDialog  = (bool)parameters[0];
            SourceOrder sourceOrder = (SourceOrder)parameters[1];

            // This batch will collect the information needed to execute a complex transaction on the server.  The first part of
            // the batch sets up the transaction: the assembly where the types and methods are found.  It also sets up a
            // transaction for a complex operation. In this case, the transaction is not that complex, just a single method to be
            // executed.
            Batch           batch       = new Batch();
            AssemblyPlan    assembly    = batch.Assemblies.Add("Trading Service");
            TypePlan        type        = assembly.Types.Add("MarkThree.Guardian.Trading.SourceOrder");
            TransactionPlan transaction = batch.Transactions.Add();
            MethodPlan      methodPlan  = transaction.Methods.Add(type, "Insert");

            // These are the parameters used to create a Source Order.  The Working Order will be created implicitly.
            methodPlan.Parameters.Add(new InputParameter("blotterId", this.blotter.BlotterId));
            methodPlan.Parameters.Add(new InputParameter("isBrokerMatch", sourceOrder.IsBrokerMatch));
            methodPlan.Parameters.Add(new InputParameter("isHedgeMatch", sourceOrder.IsHedgeMatch));
            methodPlan.Parameters.Add(new InputParameter("isInstitutionMatch", sourceOrder.IsInstitutionMatch));
            methodPlan.Parameters.Add(new InputParameter("submissionTypeCode", SubmissionType.UsePeferences));
            if (sourceOrder.MaximumVolatility != DBNull.Value)
            {
                methodPlan.Parameters.Add(new InputParameter("maximumVolatility", sourceOrder.MaximumVolatility));
            }
            if (sourceOrder.NewsFreeTime != DBNull.Value)
            {
                methodPlan.Parameters.Add(new InputParameter("newsFreeTime", sourceOrder.NewsFreeTime));
            }
            methodPlan.Parameters.Add(new InputParameter("orderedQuantity", sourceOrder.Quantity));
            methodPlan.Parameters.Add(new InputParameter("orderTypeCode", sourceOrder.OrderTypeCode));
            methodPlan.Parameters.Add(new InputParameter("priceTypeCode", PriceType.Market));
            methodPlan.Parameters.Add(new InputParameter("securityId", sourceOrder.SecurityId));
            methodPlan.Parameters.Add(new InputParameter("settlementId", sourceOrder.SettlementId));
            if (sourceOrder.StartTime != DBNull.Value)
            {
                methodPlan.Parameters.Add(new InputParameter("startTime", sourceOrder.StartTime));
            }
            if (sourceOrder.StopTime != DBNull.Value)
            {
                methodPlan.Parameters.Add(new InputParameter("stopTime", sourceOrder.StopTime));
            }
            methodPlan.Parameters.Add(new InputParameter("timeInForceCode", TimeInForce.Day));

            // This will execute the command on the server and return any exceptions.
            BatchException batchException = null;

            try
            {
                // Execute the batch.
                ClientMarketData.Execute(batch);
            }
            catch (BatchException exception)
            {
                // Any exceptions will be captured here and passed on to the foreground.
                batchException = exception;
            }

            // Call the foreground thread with the results of executing the batch on the server.  Also, in some cases the dialog is
            // going to be dismissed when the server data model has finished updating successfully.  Pass on the flag to the
            // foreground that will indicate whether the form is closed once the results are processed.
            BeginInvoke(this.postEndDelegate, new object[] { exitDialog, batchException });
        }
Ejemplo n.º 23
0
        static void Main(string[] args)
        {
            BatchSharedKeyCredentials cred = new BatchSharedKeyCredentials(url, account, key);
            string now = DateTime.UtcNow.ToString("r");

            using (BatchClient client = BatchClient.Open(cred))
            {
                //client.PoolOperations.EnableAutoScale("demo", "2");
                string formula = string.Format(@"
$TargetDedicated={1};
lifespan=time()-time(""{0}"");
span=TimeInterval_Minute * 60;
startup=TimeInterval_Minute * 10;
ratio=50;
$TargetDedicated=(lifespan>startup?(max($RunningTasks.GetSample(span, ratio), $ActiveTasks.GetSample(span, ratio)) == 0 ? 0 : $TargetDedicated):{1});
", now, 4);
                try {
                    CloudPool p = client.PoolOperations.CreatePool("formulasample", "4", "small");
                    p.AutoScaleEnabled = true;
                    p.AutoScaleFormula = formula;
                    p.Commit();
                } catch (Exception ex)
                {
                    // Go through all exceptions and dump useful information
                    (ex as AggregateException).Handle((x) =>
                    {
                        if (x is BatchException)
                        {
                            BatchException be = x as BatchException;

                            if (null != be.RequestInformation && null != be.RequestInformation.AzureError)
                            {
                                // Write the server side error information
                                Console.Error.WriteLine(be.RequestInformation.AzureError.Code);
                                Console.Error.WriteLine(be.RequestInformation.AzureError.Message.Value);

                                if (null != be.RequestInformation.AzureError.Values)
                                {
                                    foreach (var v in be.RequestInformation.AzureError.Values)
                                    {
                                        Console.Error.WriteLine(v.Key + " : " + v.Value);
                                    }
                                }
                            }
                        }

                        return(false);
                    });
                }
                //var result = client.PoolOperations.EvaluateAutoScale("demo", formula);
                //if(result.AutoScaleRun.Error != null)
                //{
                //    Console.WriteLine(result.AutoScaleRun.Error.Code + " : " + result.AutoScaleRun.Error.Message);
                //    foreach(var e in result.AutoScaleRun.Error.Values)
                //    {
                //        Console.WriteLine(" " + e.Name + " : " + e.Value);
                //    }
                //}
                //Console.WriteLine(result.AutoScaleRun.Results);
                //Console.ReadLine();
            }
        }
Ejemplo n.º 24
0
        /// <summary>
        /// Executes all the rules in an assembly until they've all inciated that they've triggered.
        /// </summary>
        /// <param name="argument">Arguments for the command.</param>
        public static void ExecuteOnce(CompilerResults compilerResults)
        {
            // The top level window of the application is needed as the owner of the error message dialog box that will pop up to
            // process the errors.
            System.Windows.Forms.Control activeForm      = Form.ActiveForm;
            System.Windows.Forms.Control topLevelControl = activeForm == null ? null : activeForm.TopLevelControl;

            // The assembly is the binary version of the rule that we compiled.
            Assembly assembly = compilerResults.CompiledAssembly;

            // Make a list of every method in the asssembly which is not inherited.  This is the way we track
            // which methods have executed to completion.  When a method returns a 'true' value, it is
            // removed from the list and is not run again.  When all the methods have triggered, the list
            // will be emtpy and the thread will exit.
            ArrayList typeList = new ArrayList();

            foreach (Type type in assembly.GetTypes())
            {
                TypeMethod typeMethod = new TypeMethod(assembly.CreateInstance(type.FullName));
                typeList.Add(typeMethod);

                foreach (MethodInfo methodInfo in type.GetMethods())
                {
                    if (methodInfo.DeclaringType == type && methodInfo.IsPublic && !methodInfo.IsStatic)
                    {
                        typeMethod.MethodList.Add(methodInfo);
                    }
                }
            }

            // Run through the list of rules that still need to be checked.  As a rule
            // triggers, its removed from the list.  When the list is empty, the thread
            // will exit.
            foreach (TypeMethod typeMethod in typeList)
            {
                foreach (MethodInfo methodInfo in typeMethod.MethodList)
                {
                    try
                    {
                        methodInfo.Invoke(typeMethod.Instance, null);
                    }
                    catch (Exception exception)
                    {
                        if (exception.InnerException.GetType() == typeof(BatchException))
                        {
                            BatchException batchException = (BatchException)exception.InnerException;
                            // Display each error from the batch until the user hits the 'Cancel' button, or until they are all displayed.
                            foreach (RemoteException remoteException in batchException.Exceptions)
                            {
                                if (MessageBox.Show(topLevelControl, remoteException.Message, "Quasar Error", MessageBoxButtons.OKCancel,
                                                    MessageBoxIcon.Error) == DialogResult.Cancel)
                                {
                                    break;
                                }
                            }
                        }
                        else
                        {
                            MessageBox.Show(exception.InnerException.Message, "Exception",
                                            System.Windows.Forms.MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                }
            }
        }
Ejemplo n.º 25
0
        /// <summary>
        /// Executes all the rules in an assembly until they've all inciated that they've triggered.
        /// </summary>
        /// <param name="argument">Arguments for the command.</param>
        public static void ExecutionCommand(params object[] argument)
        {
            // The top level window of the application is needed as the owner of the error message dialog box that will pop up to
            // process the errors.
            System.Windows.Forms.Control activeForm      = Form.ActiveForm;
            System.Windows.Forms.Control topLevelControl = activeForm == null ? null : activeForm.TopLevelControl;

            // The first argument in the variable list is the compiler results.  The assembly is the binary
            // version of the rule that we compiled.
            CompilerResults compilerResults = (CompilerResults)argument[0];
            Assembly        assembly        = compilerResults.CompiledAssembly;

            // Make a list of every method in the asssembly which is not inherited.  This is the way we track
            // which methods have executed to completion.  When a method returns a 'true' value, it is
            // removed from the list and is not run again.  When all the methods have triggered, the list
            // will be emtpy and the thread will exit.
            ArrayList typeList = new ArrayList();

            foreach (Type type in assembly.GetTypes())
            {
                TypeMethod typeMethod = new TypeMethod(assembly.CreateInstance(type.FullName));
                typeList.Add(typeMethod);

                foreach (MethodInfo methodInfo in type.GetMethods())
                {
                    if (methodInfo.DeclaringType == type && methodInfo.IsPublic && !methodInfo.IsStatic)
                    {
                        typeMethod.MethodList.Add(methodInfo);
                    }
                }
            }

            // Keep on calling the methods that haven't triggered until the list is empty.
            while (typeList.Count > 0)
            {
                // This list is used to remove methods from the list of rules.  When the
                // trigger returns 'true', it's removed from the list.
                ArrayList typeDeletionList = new ArrayList();

                // Run through the list of rules that still need to be checked.  As a rule
                // triggers, its removed from the list.  When the list is empty, the thread
                // will exit.
                foreach (TypeMethod typeMethod in typeList)
                {
                    ArrayList methodDeletionList = new ArrayList();

                    foreach (MethodInfo methodInfo in typeMethod.MethodList)
                    {
                        bool success = true;

                        try
                        {
                            success = (bool)methodInfo.Invoke(typeMethod.Instance, null);
                        }
                        catch (Exception exception)
                        {
                            if (exception.InnerException.GetType() == typeof(BatchException))
                            {
                                BatchException batchException = (BatchException)exception.InnerException;
                                // Display each error from the batch until the user hits the 'Cancel' button, or until they are all displayed.
                                foreach (RemoteException remoteException in batchException.Exceptions)
                                {
                                    if (MessageBox.Show(topLevelControl, remoteException.Message, "Quasar Error", MessageBoxButtons.OKCancel,
                                                        MessageBoxIcon.Error) == DialogResult.Cancel)
                                    {
                                        break;
                                    }
                                }
                            }
                            else
                            {
                                MessageBox.Show(exception.InnerException.Message, "Exception",
                                                System.Windows.Forms.MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }

                            success = true;
                        }

                        if (success)
                        {
                            methodDeletionList.Add(methodInfo);
                        }
                    }

                    // Delete the methods that have triggered.  The will not be run again.
                    foreach (MethodInfo methodInfo in methodDeletionList)
                    {
                        typeMethod.MethodList.Remove(methodInfo);
                    }

                    if (typeMethod.MethodList.Count == 0)
                    {
                        typeDeletionList.Add(typeMethod);
                    }
                }

                foreach (TypeMethod typeMethod in typeDeletionList)
                {
                    typeList.Remove(typeMethod);
                }

                // Sleep for a respectable amount of time and run the rules again.
                Thread.Sleep(1000);
            }
        }
Ejemplo n.º 26
0
        /// <summary>
        /// Extracts failure details from the BatchException object to create a more informative error message for the user.
        /// </summary>
        /// <param name="ex">The BatchException object</param>
        private void HandleBatchException(BatchException ex)
        {
            if (ex != null)
            {
                if (ex.RequestInformation != null && ex.RequestInformation.AzureError != null)
                {
                    StringBuilder str = new StringBuilder(ex.Message).AppendLine();

                    str.AppendFormat("Error Code: {0}", ex.RequestInformation.AzureError.Code).AppendLine();
                    str.AppendFormat("Error Message: {0}", ex.RequestInformation.AzureError.Message.Value).AppendLine();
                    str.AppendFormat("Client Request ID:{0}", ex.RequestInformation.ClientRequestId).AppendLine();
                    if (ex.RequestInformation.AzureError.Values != null)
                    {
                        foreach (AzureErrorDetail detail in ex.RequestInformation.AzureError.Values)
                        {
                            str.AppendFormat("{0}:{1}", detail.Key, detail.Value).AppendLine();
                        }
                    }
                    WriteExceptionError(new BatchException(ex.RequestInformation, str.ToString(), ex.InnerException));
                }
                else
                {
                    WriteExceptionError(ex);
                }
            }
        }
Ejemplo n.º 27
0
        public async static Task JobMain(string[] args)
        {
            //Load the configuration
            Settings        topNWordsConfiguration = Settings.Default;
            AccountSettings accountSettings        = AccountSettings.Default;

            CloudStorageAccount cloudStorageAccount = new CloudStorageAccount(
                new StorageCredentials(
                    accountSettings.StorageAccountName,
                    accountSettings.StorageAccountKey),
                accountSettings.StorageServiceUrl,
                useHttps: true);

            StagingStorageAccount stagingStorageAccount = new StagingStorageAccount(
                accountSettings.StorageAccountName,
                accountSettings.StorageAccountKey,
                cloudStorageAccount.BlobEndpoint.ToString());

            using (BatchClient client = BatchClient.Open(new BatchSharedKeyCredentials(accountSettings.BatchServiceUrl, accountSettings.BatchAccountName, accountSettings.BatchAccountKey)))
            {
                string stagingContainer = null;

                //OSFamily 4 == OS 2012 R2. You can learn more about os families and versions at:
                //http://msdn.microsoft.com/en-us/library/azure/ee924680.aspx
                CloudPool pool = client.PoolOperations.CreatePool(
                    topNWordsConfiguration.PoolId,
                    targetDedicatedComputeNodes: topNWordsConfiguration.PoolNodeCount,
                    virtualMachineSize: "small",
                    cloudServiceConfiguration: new CloudServiceConfiguration(osFamily: "4"));

                List <string> files = new List <string>
                {
                    Path.Combine(BatchStartTaskFolderName, BatchStartTaskTelemetryRunnerName),
                };

                files.AddRange(AIFilesToUpload);

                var resourceHelperTask = SampleHelpers.UploadResourcesAndCreateResourceFileReferencesAsync(
                    cloudStorageAccount,
                    AIBlobConatinerName,
                    files);

                List <ResourceFile> resourceFiles = resourceHelperTask.Result;

                pool.StartTask = new StartTask()
                {
                    CommandLine   = string.Format("cmd /c {0}", BatchStartTaskTelemetryRunnerName),
                    ResourceFiles = resourceFiles
                };

                Console.WriteLine("Adding pool {0}", topNWordsConfiguration.PoolId);
                try
                {
                    await GettingStartedCommon.CreatePoolIfNotExistAsync(client, pool);
                }
                catch (AggregateException ae)
                {
                    // Go through all exceptions and dump useful information
                    ae.Handle(x =>
                    {
                        Console.Error.WriteLine("Creating pool ID {0} failed", topNWordsConfiguration.PoolId);
                        if (x is BatchException)
                        {
                            BatchException be = x as BatchException;

                            Console.WriteLine(be.ToString());
                            Console.WriteLine();
                        }
                        else
                        {
                            Console.WriteLine(x);
                        }

                        // can't continue without a pool
                        return(false);
                    });
                }
                catch (BatchException be)
                {
                    Console.Error.WriteLine("Creating pool ID {0} failed", topNWordsConfiguration.PoolId);
                    Console.WriteLine(be.ToString());
                    Console.WriteLine();
                }

                try
                {
                    Console.WriteLine("Creating job: " + topNWordsConfiguration.JobId);
                    // get an empty unbound Job
                    CloudJob unboundJob = client.JobOperations.CreateJob();
                    unboundJob.Id = topNWordsConfiguration.JobId;
                    unboundJob.PoolInformation = new PoolInformation()
                    {
                        PoolId = topNWordsConfiguration.PoolId
                    };

                    // Commit Job to create it in the service
                    await unboundJob.CommitAsync();

                    // create file staging objects that represent the executable and its dependent assembly to run as the task.
                    // These files are copied to every node before the corresponding task is scheduled to run on that node.
                    FileToStage topNWordExe = new FileToStage(TopNWordsExeName, stagingStorageAccount);
                    FileToStage storageDll  = new FileToStage(StorageClientDllName, stagingStorageAccount);

                    // Upload application insights assemblies
                    List <FileToStage> aiStagedFiles = new List <FileToStage>();
                    foreach (string aiFile in AIFilesToUpload)
                    {
                        aiStagedFiles.Add(new FileToStage(aiFile, stagingStorageAccount));
                    }

                    // In this sample, the input data is copied separately to Storage and its URI is passed to the task as an argument.
                    // This approach is appropriate when the amount of input data is large such that copying it to every node via FileStaging
                    // is not desired and the number of tasks is small since a large number of readers of the blob might get throttled
                    // by Storage which will lengthen the overall processing time.
                    //
                    // You'll need to observe the behavior and use published techniques for finding the right balance of performance versus
                    // complexity.

                    string[] documents = Directory.GetFiles(topNWordsConfiguration.DocumentsRootPath);
                    await SampleHelpers.UploadResourcesAsync(cloudStorageAccount, BooksContainerName, documents);

                    // initialize a collection to hold the tasks that will be submitted in their entirety
                    List <CloudTask> tasksToRun = new List <CloudTask>(documents.Length);

                    for (int i = 0; i < documents.Length; i++)
                    {
                        CloudTask task = new CloudTask("task_no_" + i, String.Format("{0} --Task {1} {2} {3} {4}",
                                                                                     TopNWordsExeName,
                                                                                     "https://onbehalfoutput.blob.core.windows.net/" + documents[i],
                                                                                     topNWordsConfiguration.TopWordCount,
                                                                                     accountSettings.StorageAccountName,
                                                                                     accountSettings.StorageAccountKey));

                        //This is the list of files to stage to a container -- for each job, one container is created and
                        //files all resolve to Azure Blobs by their name (so two tasks with the same named file will create just 1 blob in
                        //the container).
                        task.FilesToStage = new List <IFileStagingProvider>
                        {
                            topNWordExe,
                            storageDll,
                        };

                        foreach (FileToStage stagedFile in aiStagedFiles)
                        {
                            task.FilesToStage.Add(stagedFile);
                        }

                        tasksToRun.Add(task);
                    }

                    // Commit all the tasks to the Batch Service. Ask AddTask to return information about the files that were staged.
                    // The container information is used later on to remove these files from Storage.
                    ConcurrentBag <ConcurrentDictionary <Type, IFileStagingArtifact> > fsArtifactBag = new ConcurrentBag <ConcurrentDictionary <Type, IFileStagingArtifact> >();
                    client.JobOperations.AddTask(topNWordsConfiguration.JobId, tasksToRun, fileStagingArtifacts: fsArtifactBag);

                    // loop through the bag of artifacts, looking for the one that matches our staged files. Once there,
                    // capture the name of the container holding the files so they can be deleted later on if that option
                    // was configured in the settings.
                    foreach (var fsBagItem in fsArtifactBag)
                    {
                        IFileStagingArtifact fsValue;
                        if (fsBagItem.TryGetValue(typeof(FileToStage), out fsValue))
                        {
                            SequentialFileStagingArtifact stagingArtifact = fsValue as SequentialFileStagingArtifact;
                            if (stagingArtifact != null)
                            {
                                stagingContainer = stagingArtifact.BlobContainerCreated;
                                Console.WriteLine(
                                    "Uploaded files to container: {0} -- you will be charged for their storage unless you delete them.",
                                    stagingArtifact.BlobContainerCreated);
                            }
                        }
                    }

                    //Get the job to monitor status.
                    CloudJob job = client.JobOperations.GetJob(topNWordsConfiguration.JobId);

                    Console.Write("Waiting for tasks to complete ...   ");
                    // Wait 20 minutes for all tasks to reach the completed state. The long timeout is necessary for the first
                    // time a pool is created in order to allow nodes to be added to the pool and initialized to run tasks.
                    IPagedEnumerable <CloudTask> ourTasks = job.ListTasks(new ODATADetailLevel(selectClause: "id"));
                    client.Utilities.CreateTaskStateMonitor().WaitAll(ourTasks, TaskState.Completed, TimeSpan.FromMinutes(20));
                    Console.WriteLine("tasks are done.");

                    foreach (CloudTask t in ourTasks)
                    {
                        Console.WriteLine("Task " + t.Id);

                        Console.WriteLine("stdout:" + Environment.NewLine + t.GetNodeFile(Batch.Constants.StandardOutFileName).ReadAsString());
                        Console.WriteLine();
                        Console.WriteLine("stderr:" + Environment.NewLine + t.GetNodeFile(Batch.Constants.StandardErrorFileName).ReadAsString());
                    }
                }
                finally
                {
                    //Delete the pool that we created
                    if (topNWordsConfiguration.ShouldDeletePool)
                    {
                        Console.WriteLine("Deleting pool: {0}", topNWordsConfiguration.PoolId);
                        client.PoolOperations.DeletePool(topNWordsConfiguration.PoolId);
                    }

                    //Delete the job that we created
                    if (topNWordsConfiguration.ShouldDeleteJob)
                    {
                        Console.WriteLine("Deleting job: {0}", topNWordsConfiguration.JobId);
                        client.JobOperations.DeleteJob(topNWordsConfiguration.JobId);
                    }

                    //Delete the containers we created
                    if (topNWordsConfiguration.ShouldDeleteContainer)
                    {
                        DeleteContainers(accountSettings, stagingContainer);
                    }
                }
            }
        }
        public void TestJobScheduleVerbs()
        {
            Action test = () =>
            {
                using (BatchClient batchCli = TestUtilities.OpenBatchClientAsync(TestUtilities.GetCredentialsFromEnvironment()).Result)
                {
                    string jobScheduleId = Microsoft.Azure.Batch.Constants.DefaultConveniencePrefix + TestUtilities.GetMyName() + "-TestEnableDisableDeleteJobSchedule";

                    try
                    {
                        Schedule schedule = new Schedule()
                        {
                            DoNotRunAfter = DateTime.UtcNow.Add(TimeSpan.FromDays(1))
                        };

                        JobSpecification jobSpecification = new JobSpecification(new PoolInformation()
                        {
                            PoolId = "DummyPool"
                        });

                        CloudJobSchedule unboundJobSchedule = batchCli.JobScheduleOperations.CreateJobSchedule(jobScheduleId, schedule, jobSpecification);
                        unboundJobSchedule.Commit();

                        CloudJobSchedule boundJobSchedule = batchCli.JobScheduleOperations.GetJobSchedule(jobScheduleId);

                        //Disable the job schedule via instance
                        boundJobSchedule.Disable();
                        boundJobSchedule.Refresh();

                        Assert.NotNull(boundJobSchedule.State);
                        Assert.Equal(JobScheduleState.Disabled, boundJobSchedule.State);

                        //Enable the job schedule via instance
                        boundJobSchedule.Enable();
                        boundJobSchedule.Refresh();

                        Assert.NotNull(boundJobSchedule.State);
                        Assert.Equal(JobScheduleState.Active, boundJobSchedule.State);

                        //Disable the job schedule via operations
                        batchCli.JobScheduleOperations.DisableJobSchedule(jobScheduleId);
                        boundJobSchedule.Refresh();

                        Assert.NotNull(boundJobSchedule.State);
                        Assert.Equal(JobScheduleState.Disabled, boundJobSchedule.State);

                        //Enable the job schedule via instance
                        batchCli.JobScheduleOperations.EnableJobSchedule(jobScheduleId);
                        boundJobSchedule.Refresh();

                        Assert.NotNull(boundJobSchedule.State);
                        Assert.Equal(JobScheduleState.Active, boundJobSchedule.State);

                        //Terminate the job schedule
                        batchCli.JobScheduleOperations.TerminateJobSchedule(jobScheduleId);

                        boundJobSchedule.Refresh();
                        Assert.True(boundJobSchedule.State == JobScheduleState.Completed || boundJobSchedule.State == JobScheduleState.Terminating);

                        //Delete the job schedule
                        boundJobSchedule.Delete();

                        //Wait for deletion to take
                        BatchException be = TestUtilities.AssertThrowsEventuallyAsync <BatchException>(() => boundJobSchedule.RefreshAsync(), TimeSpan.FromSeconds(30)).Result;
                        Assert.NotNull(be.RequestInformation);
                        Assert.NotNull(be.RequestInformation.BatchError);
                        Assert.Equal("JobScheduleNotFound", be.RequestInformation.BatchError.Code);
                    }
                    finally
                    {
                        // clean up
                        TestUtilities.DeleteJobScheduleIfExistsAsync(batchCli, jobScheduleId).Wait();
                    }
                }
            };

            SynchronizationContextHelper.RunTest(test, TestTimeout);
        }
Ejemplo n.º 29
0
        public void Bug1965363_2384616_Wat7OSVersionFeatures()
        {
            Action test = () =>
            {
                using (BatchClient batchCli = TestUtilities.OpenBatchClientAsync(TestUtilities.GetCredentialsFromEnvironment()).Result)
                {
                    PoolOperations poolOperations = batchCli.PoolOperations;
                    try
                    {
                        this.testOutputHelper.WriteLine("Listing OS Versions:");

                        /* bug 2384616 ListOsVersions hidden for wat 8
                         *
                         *  // test ListOSVersion
                         *  foreach (OSVersion curOSV in poolMgr.ListOSVersions())
                         *  {
                         *      this.testOutputHelper.WriteLine("Label: " + curOSV.Label);
                         *      this.testOutputHelper.WriteLine("    Version: " + curOSV.Version);
                         *      this.testOutputHelper.WriteLine("    Family: " + curOSV.Family);
                         *      this.testOutputHelper.WriteLine("    FamilyLabel: " + curOSV.FamilyLabel);
                         *      this.testOutputHelper.WriteLine("    isDefault: " + curOSV.IsDefault);
                         *      this.testOutputHelper.WriteLine("    IsActive: " + curOSV.IsActive);
                         *
                         *      string expDate;
                         *
                         *      if (curOSV.ExpirationDate.HasValue)
                         *      {
                         *          expDate = curOSV.ExpirationDate.Value.ToString();
                         *      }
                         *      else
                         *      {
                         *          expDate = "<null/novalue>";
                         *      }
                         *
                         *      this.testOutputHelper.WriteLine("    ExpirationDate: " + expDate);
                         *  }
                         *
                         */

                        // create pool tests


                        // forget to set CloudServiceConfiguration on Create, get error
                        {
                            CloudPool noArgs = poolOperations.CreatePool("Bug1965363ButNoOSFamily-" + TestUtilities.GetMyName(), PoolFixture.VMSize, default(CloudServiceConfiguration), targetDedicated: 0);

                            BatchException ex    = TestUtilities.AssertThrows <BatchException>(() => noArgs.Commit());
                            string         exStr = ex.ToString();

                            // we are expecting an exception, assert if the exception is not the correct one.
                            Assert.Contains("cloudServiceConfiguration", exStr);
                        }

                        // create a pool WITH an osFamily
                        {
                            string poolIdHOSF = "Bug1965363HasOSF-" + TestUtilities.GetMyName();
                            try
                            {
                                CloudPool hasOSF = poolOperations.CreatePool(poolIdHOSF, PoolFixture.VMSize, new CloudServiceConfiguration(PoolFixture.OSFamily), targetDedicated: 0);

                                hasOSF.Commit();
                            }
                            finally
                            {
                                poolOperations.DeletePool(poolIdHOSF);
                            }
                        }

                        // TODO: ultimately we will either need to find (via list) a family with more than one version or
                        //       manually update these strings as OS versions are depricated
                        //See here for other OS versions if this test fails: http://azure.microsoft.com/en-us/documentation/articles/cloud-services-guestos-update-matrix/
                        const string familyVersion0 = "*";
                        const string familyVersion1 = "WA-GUEST-OS-4.32_201605-01";

                        // "UpdatePoolOS" tests (ChangeOSVersion in OM)

                        // PoolManager
                        {
                            string poolIdChangeOSV = "Bug1965363ChangeOSVviaMGR-" + TestUtilities.GetMyName();
                            try
                            {
                                CloudPool unboundPool = poolOperations.CreatePool(
                                    poolIdChangeOSV,
                                    PoolFixture.VMSize,
                                    new CloudServiceConfiguration(PoolFixture.OSFamily, familyVersion0), // start with version 0
                                    targetDedicated: 0);

                                unboundPool.Commit();

                                // fetch the bound pool
                                CloudPool boundPool = poolOperations.GetPool(poolIdChangeOSV);

                                Assert.Equal(familyVersion0, boundPool.CloudServiceConfiguration.CurrentOSVersion);

                                // switch to new version
                                poolOperations.ChangeOSVersion(poolIdChangeOSV, familyVersion1);

                                // UpdatePoolOS is has latency???
                                PollForOSVersionChange(boundPool, familyVersion1);

                                // check to make sure the new value is set
                                boundPool.Refresh();

                                Assert.Equal(familyVersion1, boundPool.CloudServiceConfiguration.CurrentOSVersion);
                            }
                            finally
                            {
                                TestUtilities.DeletePoolIfExistsAsync(batchCli, poolIdChangeOSV).Wait();
                            }
                        }

                        // ICloudPool
                        {
                            string poolIdChangeOSV = "Bug1965363ChangeOSVviaPool-" + TestUtilities.GetMyName();
                            try
                            {
                                CloudPool unboundPool = poolOperations.CreatePool(
                                    poolIdChangeOSV,
                                    PoolFixture.VMSize,
                                    new CloudServiceConfiguration(PoolFixture.OSFamily, familyVersion0), // start with version 0
                                    targetDedicated: 0);

                                unboundPool.Commit();

                                // fetch the bound pool
                                CloudPool boundPool = poolOperations.GetPool(poolIdChangeOSV);

                                Assert.Equal(familyVersion0, boundPool.CloudServiceConfiguration.CurrentOSVersion);

                                // switch to new version
                                boundPool.ChangeOSVersion(familyVersion1);

                                // UpdatePoolOS is has latency???
                                PollForOSVersionChange(boundPool, familyVersion1);

                                // check to make sure the new value is set
                                boundPool.Refresh();

                                Assert.Equal(familyVersion1, boundPool.CloudServiceConfiguration.CurrentOSVersion);
                            }
                            finally
                            {
                                TestUtilities.DeletePoolIfExistsAsync(batchCli, poolIdChangeOSV).Wait();
                            }
                        }

                        // autopoolspec tests
                        {
                            string jobId = "Bug1965363WIName-" + TestUtilities.GetMyName();
                            // test not setting osversion
                            try
                            {
                                CloudJob unboundJob       = batchCli.JobOperations.CreateJob(jobId, new PoolInformation());
                                AutoPoolSpecification aps = new AutoPoolSpecification();
                                PoolSpecification     ps  = new PoolSpecification();

                                // test unbound set constraint
                                ps.CloudServiceConfiguration = new CloudServiceConfiguration(PoolFixture.OSFamily);

                                // test unbound get constraint
                                this.testOutputHelper.WriteLine("pus.CloudServiceConfiguration.OSFamily == " + ps.CloudServiceConfiguration.OSFamily);

                                ps.VirtualMachineSize = PoolFixture.VMSize;
                                ps.TargetDedicated    = 0; // trivial size for testing purposes

                                aps.PoolSpecification  = ps;
                                aps.PoolLifetimeOption = PoolLifetimeOption.Job;

                                unboundJob.PoolInformation.AutoPoolSpecification = aps;

                                // commit to test validation
                                unboundJob.Commit();

                                // get bound job
                                CloudJob boundJob = batchCli.JobOperations.GetJob(jobId);

                                // test bound get constraints
                                this.testOutputHelper.WriteLine("    OSFamily == " +
                                                                boundJob.PoolInformation.AutoPoolSpecification.PoolSpecification.CloudServiceConfiguration.OSFamily);

                                string targetOSVersion =
                                    boundJob.PoolInformation.AutoPoolSpecification.PoolSpecification.CloudServiceConfiguration.TargetOSVersion;

                                if (string.IsNullOrEmpty(targetOSVersion))
                                {
                                    targetOSVersion = "<null or empty";
                                }

                                this.testOutputHelper.WriteLine("    TargetOSVersion == " + targetOSVersion);
                            }
                            finally
                            {
                                // cleanup
                                TestUtilities.DeleteJobIfExistsAsync(batchCli, jobId).Wait();
                            }

                            {
                                string jobScheduleId = "Bug1965363WINameSettingAndChanging-" + TestUtilities.GetMyName();
                                // test setting osversion
                                try
                                {
                                    AutoPoolSpecification aps = new AutoPoolSpecification();
                                    PoolSpecification     ps  = new PoolSpecification();
                                    CloudJobSchedule      unboundJobSchedule = batchCli.JobScheduleOperations.CreateJobSchedule(
                                        jobScheduleId,
                                        new Schedule()
                                    {
                                        RecurrenceInterval = TimeSpan.FromDays(7)
                                    },
                                        new JobSpecification(new PoolInformation()
                                    {
                                        AutoPoolSpecification = aps
                                    }));

                                    // test unbound set constraint
                                    ps.CloudServiceConfiguration = new CloudServiceConfiguration(PoolFixture.OSFamily, familyVersion0);

                                    // test unbound get constraint
                                    this.testOutputHelper.WriteLine("pus.CloudServiceConfiguration.OSFamily == " + ps.CloudServiceConfiguration.OSFamily);
                                    this.testOutputHelper.WriteLine("pus.CloudServiceConfiguration.TargetOSVersion == " + ps.CloudServiceConfiguration.TargetOSVersion);

                                    ps.VirtualMachineSize = PoolFixture.VMSize;
                                    ps.TargetDedicated    = 0; // trivial size for testing purposes

                                    aps.PoolSpecification  = ps;
                                    aps.PoolLifetimeOption = PoolLifetimeOption.Job;

                                    unboundJobSchedule.Commit();

                                    // get bound job schedule
                                    CloudJobSchedule boundJobSchedule = batchCli.JobScheduleOperations.GetJobSchedule(jobScheduleId);

                                    // test bound get constraints
                                    this.testOutputHelper.WriteLine("    OSFamily == " + boundJobSchedule.JobSpecification.PoolInformation.AutoPoolSpecification.PoolSpecification.CloudServiceConfiguration.OSFamily);
                                    this.testOutputHelper.WriteLine("    TargetOSVersion == " + boundJobSchedule.JobSpecification.PoolInformation.AutoPoolSpecification.PoolSpecification.CloudServiceConfiguration.TargetOSVersion);

                                    // assert the value is as set above
                                    Assert.Equal(familyVersion0, boundJobSchedule.JobSpecification.PoolInformation.AutoPoolSpecification.PoolSpecification.CloudServiceConfiguration.TargetOSVersion);

                                    // change values
                                    const string altFamily    = "3";
                                    const string altOSVersion = "WA-GUEST-OS-3.39_201605-01";

                                    // change values on the bound PUS
                                    PoolSpecification boundPS = boundJobSchedule.JobSpecification.PoolInformation.AutoPoolSpecification.PoolSpecification;

                                    boundPS.CloudServiceConfiguration = new CloudServiceConfiguration(altFamily, altOSVersion);

                                    // flush changes
                                    boundJobSchedule.Commit();

                                    // confirm changes took
                                    boundJobSchedule.Refresh();

                                    Assert.Equal(altFamily, boundJobSchedule.JobSpecification.PoolInformation.AutoPoolSpecification.PoolSpecification.CloudServiceConfiguration.OSFamily);
                                    Assert.Equal(altOSVersion, boundJobSchedule.JobSpecification.PoolInformation.AutoPoolSpecification.PoolSpecification.CloudServiceConfiguration.TargetOSVersion);
                                }
                                finally
                                {
                                    // cleanup
                                    TestUtilities.DeleteJobScheduleIfExistsAsync(batchCli, jobScheduleId).Wait();
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        // special case os version beacuse it is a common failure and requires human intervention/editing
                        // test for expired os version
                        Assert.DoesNotContain("The specified OS Version does not exists", ex.ToString());

                        throw;
                    }
                }
            };

            SynchronizationContextHelper.RunTest(test, TestTimeout);
        }
 private static bool IsExceptionConflict(BatchException e)
 {
     return(e.RequestInformation != null && e.RequestInformation.HttpStatusCode == HttpStatusCode.Conflict);
 }
 private static bool IsExceptionNotFound(BatchException e)
 {
     return(e.RequestInformation != null && e.RequestInformation.HttpStatusCode == HttpStatusCode.NotFound);
 }
Ejemplo n.º 32
0
 /// <summary>
 /// Extracts failure details from the BatchException object to create a more informative error message for the user.
 /// </summary>
 /// <param name="ex">The BatchException object</param>
 private void HandleBatchException(BatchException ex)
 {
     if (ex != null)
     {
         if (ex.RequestInformation != null && ex.RequestInformation.BatchError != null)
         {
             WriteExceptionError(new BatchException(ex.RequestInformation, ex.ToString(), ex.InnerException));
         }
         else
         {
             WriteExceptionError(ex);
         }
     }
 }