/// <summary> /// Get Batch Context with keys /// </summary> public static BatchAccountContext GetBatchAccountContextWithKeys(BatchController controller, string accountName) { BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); BatchAccountContext context = client.ListKeys(null, accountName); return context; }
/// <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) { BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); X509Certificate2 cert = new X509Certificate2(filePath); ListCertificateOptions getParameters = new ListCertificateOptions(context) { 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 (BatchException ex) { // When the cert doesn't exist, we get a 404 error. For all other errors, throw. if (ex == null || !ex.Message.Contains("NotFound")) { throw; } } NewCertificateParameters parameters = new NewCertificateParameters(context, null, cert.RawData); client.AddCertificate(parameters); return(cert.Thumbprint); }
/// <summary> /// Creates a test Pool for use in Scenario tests. /// </summary> public static void CreateTestPool(BatchController controller, BatchAccountContext context, string poolName) { YieldInjectionInterceptor interceptor = CreateHttpRecordingInterceptor(); BatchClientBehavior[] behaviors = new BatchClientBehavior[] { interceptor }; BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); NewPoolParameters parameters = new NewPoolParameters() { Context = context, PoolName = poolName, OSFamily = "4", TargetOSVersion = "*", TargetDedicated = 1, AdditionalBehaviors = behaviors }; client.CreatePool(parameters); }
/// <summary> /// Waits for the specified task to complete /// </summary> public static void WaitForTaskCompletion(BatchController controller, BatchAccountContext context, string jobId, string taskId) { RequestInterceptor interceptor = CreateHttpRecordingInterceptor(); BatchClientBehavior[] behaviors = new BatchClientBehavior[] { interceptor }; BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); ListTaskOptions options = new ListTaskOptions(context, jobId, null, behaviors) { TaskId = taskId }; IEnumerable <PSCloudTask> tasks = client.ListTasks(options); // Save time by not waiting during playback scenarios if (HttpMockServer.Mode == HttpRecorderMode.Record) { TaskStateMonitor monitor = context.BatchOMClient.Utilities.CreateTaskStateMonitor(); monitor.WaitAll(tasks.Select(t => t.omObject), TaskState.Completed, TimeSpan.FromMinutes(2), null); } }
/// <summary> /// Creates a test Task for use in Scenario tests. /// </summary> public static void CreateTestTask(BatchController controller, BatchAccountContext context, string workItemName, string jobName, string taskName, string cmdLine = "cmd /c dir /s") { YieldInjectionInterceptor interceptor = CreateHttpRecordingInterceptor(); BatchClientBehavior[] behaviors = new BatchClientBehavior[] { interceptor }; BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); NewTaskParameters parameters = new NewTaskParameters() { Context = context, WorkItemName = workItemName, JobName = jobName, TaskName = taskName, CommandLine = cmdLine, RunElevated = true, AdditionalBehaviors = behaviors }; client.CreateTask(parameters); }
/// <summary> /// Creates a test task for use in Scenario tests. /// </summary> public static void CreateTestTask(BatchController controller, BatchAccountContext context, string jobId, string taskId, string cmdLine = "cmd /c dir /s", int numInstances = 0) { BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); PSMultiInstanceSettings multiInstanceSettings = null; if (numInstances > 1) { multiInstanceSettings = new PSMultiInstanceSettings("cmd /c echo coordinating", numInstances); } NewTaskParameters parameters = new NewTaskParameters(context, jobId, null, taskId) { CommandLine = cmdLine, MultiInstanceSettings = multiInstanceSettings, UserIdentity = new PSUserIdentity(new PSAutoUserSpecification(AutoUserScope.Task, numInstances <= 1 ? ElevationLevel.Admin : ElevationLevel.NonAdmin)) }; client.CreateTask(parameters); }
/// <summary> /// Creates a test task for use in Scenario tests. /// </summary> public static void CreateTestTask(BatchController controller, BatchAccountContext context, string jobId, string taskId, string cmdLine = "cmd /c dir /s", int numInstances = 0) { BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); PSMultiInstanceSettings multiInstanceSettings = null; if (numInstances > 1) { multiInstanceSettings = new PSMultiInstanceSettings(numInstances); multiInstanceSettings.CoordinationCommandLine = "cmd /c echo coordinating"; } NewTaskParameters parameters = new NewTaskParameters(context, jobId, null, taskId) { CommandLine = cmdLine, MultiInstanceSettings = multiInstanceSettings, RunElevated = numInstances <= 1 }; client.CreateTask(parameters); }
/// <summary> /// Creates a test pool for use in Scenario tests. /// </summary> public static void CreateTestPool(BatchController controller, BatchAccountContext context, string poolId, int targetDedicated, CertificateReference certReference = null) { BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); PSCertificateReference[] certReferences = null; if (certReference != null) { certReferences = new PSCertificateReference[] { new PSCertificateReference(certReference) }; } NewPoolParameters parameters = new NewPoolParameters(context, poolId) { VirtualMachineSize = "small", OSFamily = "4", TargetOSVersion = "*", TargetDedicated = targetDedicated, CertificateReferences = certReferences, }; client.CreatePool(parameters); }
/// <summary> /// Waits for the specified task to complete /// </summary> public static void WaitForTaskCompletion(BatchController controller, BatchAccountContext context, string workItemName, string jobName, string taskName) { YieldInjectionInterceptor interceptor = CreateHttpRecordingInterceptor(); BatchClientBehavior[] behaviors = new BatchClientBehavior[] { interceptor }; BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); ListTaskOptions options = new ListTaskOptions() { Context = context, WorkItemName = workItemName, JobName = jobName, TaskName = taskName, AdditionalBehaviors = behaviors }; IEnumerable <PSCloudTask> tasks = client.ListTasks(options); ITaskStateMonitor monitor = context.BatchOMClient.OpenToolbox().CreateTaskStateMonitor(); monitor.WaitAll(tasks.Select(t => t.omObject), TaskState.Completed, TimeSpan.FromMinutes(2), null, behaviors); }
public static void WaitForSteadyPoolAllocation(BatchController controller, BatchAccountContext context, string poolId) { BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); ListPoolOptions options = new ListPoolOptions(context) { PoolId = poolId }; DateTime timeout = DateTime.Now.AddMinutes(5); PSCloudPool pool = client.ListPools(options).First(); while (pool.AllocationState != AllocationState.Steady) { if (DateTime.Now > timeout) { throw new TimeoutException("Timed out waiting for steady allocation state"); } Sleep(5000); pool = client.ListPools(options).First(); } }
/// <summary> /// Waits for a compute node to get to the idle state /// </summary> public static void WaitForIdleComputeNode(BatchController controller, BatchAccountContext context, string poolId, string computeNodeId) { BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); ListComputeNodeOptions options = new ListComputeNodeOptions(context, poolId, null) { ComputeNodeId = computeNodeId }; DateTime timeout = DateTime.Now.AddMinutes(2); PSComputeNode computeNode = client.ListComputeNodes(options).First(); if (computeNode.State != ComputeNodeState.Idle) { if (DateTime.Now > timeout) { throw new TimeoutException("Timed out waiting for idle compute node"); } Sleep(5000); computeNode = client.ListComputeNodes(options).First(); } }
/// <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 static string WaitForOSVersionChange(BatchController controller, BatchAccountContext context, string poolId) { BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); ListPoolOptions options = new ListPoolOptions(context) { PoolId = poolId }; DateTime timeout = DateTime.Now.AddMinutes(5); PSCloudPool pool = client.ListPools(options).First(); while (pool.CloudServiceConfiguration.CurrentOSVersion != pool.CloudServiceConfiguration.TargetOSVersion) { if (DateTime.Now > timeout) { throw new TimeoutException("Timed out waiting for active state pool"); } Sleep(5000); pool = client.ListPools(options).First(); } return(pool.CloudServiceConfiguration.TargetOSVersion); }
/// <summary> /// Creates a test job schedule for use in Scenario tests. /// </summary> public static void CreateTestJobSchedule(BatchController controller, BatchAccountContext context, string jobScheduleId, TimeSpan?recurrenceInterval) { BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); PSJobSpecification jobSpecification = new PSJobSpecification(); jobSpecification.PoolInformation = new PSPoolInformation(); jobSpecification.PoolInformation.PoolId = SharedPool; PSSchedule schedule = new PSSchedule(); if (recurrenceInterval != null) { schedule = new PSSchedule(); schedule.RecurrenceInterval = recurrenceInterval; } NewJobScheduleParameters parameters = new NewJobScheduleParameters(context, jobScheduleId) { JobSpecification = jobSpecification, Schedule = schedule }; client.CreateJobSchedule(parameters); }
/// <summary> /// Waits for a recent job on a job schedule and returns its id. If a previous job is specified, this method waits until a new job is created. /// </summary> public static string WaitForRecentJob(BatchController controller, BatchAccountContext context, string jobScheduleId, string previousJob = null) { DateTime timeout = DateTime.Now.AddMinutes(2); BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); ListJobScheduleOptions options = new ListJobScheduleOptions(context) { JobScheduleId = jobScheduleId, Filter = null, MaxCount = Constants.DefaultMaxCount }; PSCloudJobSchedule jobSchedule = client.ListJobSchedules(options).First(); while (jobSchedule.ExecutionInformation.RecentJob == null || string.Equals(jobSchedule.ExecutionInformation.RecentJob.Id, previousJob, StringComparison.OrdinalIgnoreCase)) { if (DateTime.Now > timeout) { throw new TimeoutException("Timed out waiting for recent job"); } Sleep(5000); jobSchedule = client.ListJobSchedules(options).First(); } return(jobSchedule.ExecutionInformation.RecentJob.Id); }
/// <summary> /// Deletes a certificate. /// </summary> public static void WaitForCertificateToFailDeletion(BatchController controller, BatchAccountContext context, string thumbprintAlgorithm, string thumbprint) { BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); ListCertificateOptions parameters = new ListCertificateOptions(context) { ThumbprintAlgorithm = BatchTestHelpers.TestCertificateAlgorithm, Thumbprint = thumbprint }; PSCertificate cert = client.ListCertificates(parameters).First(); DateTime timeout = DateTime.Now.AddMinutes(2); while (cert.State != CertificateState.DeleteFailed) { if (DateTime.Now > timeout) { throw new TimeoutException("Timed out waiting for failed certificate deletion"); } Sleep(10000); cert = client.ListCertificates(parameters).First(); } }
/// <summary> /// Creates a test workitem for use in Scenario tests. /// </summary> public static void CreateTestWorkItem(BatchController controller, BatchAccountContext context, string workItemName, TimeSpan? recurrenceInterval) { YieldInjectionInterceptor interceptor = CreateHttpRecordingInterceptor(); BatchClientBehavior[] behaviors = new BatchClientBehavior[] { interceptor }; BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); PSJobExecutionEnvironment jobExecutionEnvironment = new PSJobExecutionEnvironment(); jobExecutionEnvironment.PoolName = DefaultPoolName; PSWorkItemSchedule schedule = null; if (recurrenceInterval != null) { schedule = new PSWorkItemSchedule(); schedule.RecurrenceInterval = recurrenceInterval; } NewWorkItemParameters parameters = new NewWorkItemParameters(context, workItemName, behaviors) { JobExecutionEnvironment = jobExecutionEnvironment, Schedule = schedule }; client.CreateWorkItem(parameters); }
/// <summary> /// Deletes a job used in a Scenario test. /// </summary> public static void DeleteJob(BatchController controller, BatchAccountContext context, string jobId) { BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); client.DeleteJob(context, jobId); }
/// <summary> /// Waits for a compute node to get to the idle state /// </summary> public static void WaitForIdleComputeNode(BatchController controller, BatchAccountContext context, string poolId, string computeNodeId) { RequestInterceptor interceptor = CreateHttpRecordingInterceptor(); BatchClientBehavior[] behaviors = new BatchClientBehavior[] { interceptor }; BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); ListComputeNodeOptions options = new ListComputeNodeOptions(context, poolId, null, behaviors) { ComputeNodeId = computeNodeId }; DateTime timeout = DateTime.Now.AddMinutes(2); PSComputeNode computeNode = client.ListComputeNodes(options).First(); if (computeNode.State != ComputeNodeState.Idle) { if (DateTime.Now > timeout) { throw new TimeoutException("Timed out waiting for idle compute node"); } Sleep(5000); computeNode = client.ListComputeNodes(options).First(); } }
/// <summary> /// Deletes a job used in a Scenario test. /// </summary> public static void DeleteJob(BatchController controller, BatchAccountContext context, string jobId) { RequestInterceptor interceptor = CreateHttpRecordingInterceptor(); BatchClientBehavior[] behaviors = new BatchClientBehavior[] { interceptor }; BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); client.DeleteJob(context, jobId, behaviors); }
/// <summary> /// Waits for the specified task to complete /// </summary> public static void WaitForTaskCompletion(BatchController controller, BatchAccountContext context, string workItemName, string jobName, string taskName) { YieldInjectionInterceptor interceptor = CreateHttpRecordingInterceptor(); BatchClientBehavior[] behaviors = new BatchClientBehavior[] { interceptor }; BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); ListTaskOptions options = new ListTaskOptions(context, workItemName, jobName, null, behaviors) { TaskName = taskName }; IEnumerable<PSCloudTask> tasks = client.ListTasks(options); ITaskStateMonitor monitor = context.BatchOMClient.OpenToolbox().CreateTaskStateMonitor(); monitor.WaitAll(tasks.Select(t => t.omObject), TaskState.Completed, TimeSpan.FromMinutes(2), null, behaviors); }
/// <summary> /// Gets the number of pools under the specified account /// </summary> public static int GetPoolCount(BatchController controller, BatchAccountContext context) { RequestInterceptor interceptor = CreateHttpRecordingInterceptor(); BatchClientBehavior[] behaviors = new BatchClientBehavior[] { interceptor }; BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); ListPoolOptions options = new ListPoolOptions(context, behaviors); return client.ListPools(options).Count(); }
public static void WaitForSteadyPoolAllocation(BatchController controller, BatchAccountContext context, string poolId) { RequestInterceptor interceptor = CreateHttpRecordingInterceptor(); BatchClientBehavior[] behaviors = new BatchClientBehavior[] { interceptor }; BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); ListPoolOptions options = new ListPoolOptions(context, behaviors) { PoolId = poolId }; DateTime timeout = DateTime.Now.AddMinutes(2); PSCloudPool pool = client.ListPools(options).First(); while (pool.AllocationState != AllocationState.Steady) { if (DateTime.Now > timeout) { throw new TimeoutException("Timed out waiting for steady allocation state"); } Sleep(5000); pool = client.ListPools(options).First(); } }
/// <summary> /// Creates a compute node user for use in Scenario tests. /// </summary> public static void CreateComputeNodeUser(BatchController controller, BatchAccountContext context, string poolId, string computeNodeId, string computeNodeUserName) { BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); NewComputeNodeUserParameters parameters = new NewComputeNodeUserParameters(context, poolId, computeNodeId, null) { ComputeNodeUserName = computeNodeUserName, Password = "******", }; client.CreateComputeNodeUser(parameters); }
/// <summary> /// Creates a test pool for use in Scenario tests. /// </summary> public static void CreateTestPool(BatchController controller, BatchAccountContext context, string poolName) { YieldInjectionInterceptor interceptor = CreateHttpRecordingInterceptor(); BatchClientBehavior[] behaviors = new BatchClientBehavior[] { interceptor }; BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); NewPoolParameters parameters = new NewPoolParameters(context, poolName, behaviors) { OSFamily = "4", TargetOSVersion = "*", TargetDedicated = 1 }; client.CreatePool(parameters); }
/// <summary> /// Deletes a compute node user for use in Scenario tests. /// </summary> public static void DeleteComputeNodeUser(BatchController controller, BatchAccountContext context, string poolId, string computeNodeId, string computeNodeUserName) { BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); ComputeNodeUserOperationParameters parameters = new ComputeNodeUserOperationParameters(context, poolId, computeNodeId, computeNodeUserName); client.DeleteComputeNodeUser(parameters); }
/// <summary> /// Deletes a user used in a Scenario test. /// </summary> public static void DeleteUser(BatchController controller, BatchAccountContext context, string poolName, string vmName, string vmUserName) { YieldInjectionInterceptor interceptor = CreateHttpRecordingInterceptor(); BatchClientBehavior[] behaviors = new BatchClientBehavior[] { interceptor }; BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); VMUserOperationParameters parameters = new VMUserOperationParameters(context, poolName, vmName, vmUserName, behaviors); client.DeleteVMUser(parameters); }
/// <summary> /// Creates a test user for use in Scenario tests. /// </summary> public static void CreateTestUser(BatchController controller, BatchAccountContext context, string poolName, string vmName, string vmUserName) { YieldInjectionInterceptor interceptor = CreateHttpRecordingInterceptor(); BatchClientBehavior[] behaviors = new BatchClientBehavior[] { interceptor }; BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); NewVMUserParameters parameters = new NewVMUserParameters(context, poolName, vmName, null, behaviors) { VMUserName = vmUserName, Password = "******", }; client.CreateVMUser(parameters); }
/// <summary> /// Deletes a workitem used in a Scenario test. /// </summary> public static void DeleteWorkItem(BatchController controller, BatchAccountContext context, string workItemName) { YieldInjectionInterceptor interceptor = CreateHttpRecordingInterceptor(); BatchClientBehavior[] behaviors = new BatchClientBehavior[] { interceptor }; BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); client.DeleteWorkItem(context, workItemName, behaviors); }
public static void DisableAutoScale(BatchController controller, BatchAccountContext context, string poolId) { RequestInterceptor interceptor = CreateHttpRecordingInterceptor(); BatchClientBehavior[] behaviors = new BatchClientBehavior[] { interceptor }; BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); PoolOperationParameters parameters = new PoolOperationParameters(context, poolId, null, behaviors); client.DisableAutoScale(parameters); }
/// <summary> /// Gets the id of a compute node in the specified pool /// </summary> public static string GetComputeNodeId(BatchController controller, BatchAccountContext context, string poolId) { BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); ListComputeNodeOptions options = new ListComputeNodeOptions(context, poolId, null); return client.ListComputeNodes(options).First().Id; }
public static string WaitForOSVersionChange(BatchController controller, BatchAccountContext context, string poolId) { RequestInterceptor interceptor = CreateHttpRecordingInterceptor(); BatchClientBehavior[] behaviors = new BatchClientBehavior[] { interceptor }; BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); ListPoolOptions options = new ListPoolOptions(context, behaviors) { PoolId = poolId }; DateTime timeout = DateTime.Now.AddMinutes(2); PSCloudPool pool = client.ListPools(options).First(); while (pool.CurrentOSVersion != pool.TargetOSVersion) { if (DateTime.Now > timeout) { throw new TimeoutException("Timed out waiting for active state pool"); } Sleep(5000); pool = client.ListPools(options).First(); } return pool.TargetOSVersion; }
/// <summary> /// Creates a test job for use in Scenario tests. /// </summary> public static void CreateTestJob(BatchController controller, BatchAccountContext context, string jobId) { RequestInterceptor interceptor = CreateHttpRecordingInterceptor(); BatchClientBehavior[] behaviors = new BatchClientBehavior[] { interceptor }; BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); PSPoolInformation poolInfo = new PSPoolInformation(); poolInfo.PoolId = SharedPool; NewJobParameters parameters = new NewJobParameters(context, jobId, behaviors) { PoolInformation = poolInfo }; client.CreateJob(parameters); }
/// <summary> /// Gets the CurrentDedicated count from a pool /// </summary> public static int GetPoolCurrentDedicated(BatchController controller, BatchAccountContext context, string poolId) { RequestInterceptor interceptor = CreateHttpRecordingInterceptor(); BatchClientBehavior[] behaviors = new BatchClientBehavior[] { interceptor }; BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); ListPoolOptions options = new ListPoolOptions(context, behaviors) { PoolId = poolId }; PSCloudPool pool = client.ListPools(options).First(); return pool.CurrentDedicated.Value; }
/// <summary> /// Creates a test task for use in Scenario tests. /// </summary> public static void CreateTestTask(BatchController controller, BatchAccountContext context, string jobId, string taskId, string cmdLine = "cmd /c dir /s") { RequestInterceptor interceptor = CreateHttpRecordingInterceptor(); BatchClientBehavior[] behaviors = new BatchClientBehavior[] { interceptor }; BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); NewTaskParameters parameters = new NewTaskParameters(context, jobId, null, taskId, behaviors) { CommandLine = cmdLine, RunElevated = true }; client.CreateTask(parameters); }
/// <summary> /// Creates a test job schedule for use in Scenario tests. /// </summary> public static void CreateTestJobSchedule(BatchController controller, BatchAccountContext context, string jobScheduleId, TimeSpan? recurrenceInterval) { RequestInterceptor interceptor = CreateHttpRecordingInterceptor(); BatchClientBehavior[] behaviors = new BatchClientBehavior[] { interceptor }; BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); PSJobSpecification jobSpecification = new PSJobSpecification(); jobSpecification.PoolInformation = new PSPoolInformation(); jobSpecification.PoolInformation.PoolId = SharedPool; PSSchedule schedule = new PSSchedule(); if (recurrenceInterval != null) { schedule = new PSSchedule(); schedule.RecurrenceInterval = recurrenceInterval; } NewJobScheduleParameters parameters = new NewJobScheduleParameters(context, jobScheduleId, behaviors) { JobSpecification = jobSpecification, Schedule = schedule }; client.CreateJobSchedule(parameters); }
/// <summary> /// Waits for a recent job on a workitem and returns its name. If a previous job is specified, this method waits until a new job is created. /// </summary> public static string WaitForRecentJob(BatchController controller, BatchAccountContext context, string workItemName, string previousJob = null) { DateTime timeout = DateTime.Now.AddMinutes(2); YieldInjectionInterceptor interceptor = CreateHttpRecordingInterceptor(); BatchClientBehavior[] behaviors = new BatchClientBehavior[] { interceptor }; BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); ListWorkItemOptions options = new ListWorkItemOptions(context, behaviors) { WorkItemName = workItemName, Filter = null, MaxCount = Constants.DefaultMaxCount }; PSCloudWorkItem workItem = client.ListWorkItems(options).First(); while (workItem.ExecutionInformation.RecentJob == null || string.Equals(workItem.ExecutionInformation.RecentJob.Name, previousJob, StringComparison.OrdinalIgnoreCase)) { if (DateTime.Now > timeout) { throw new TimeoutException("Timed out waiting for recent job"); } Sleep(5000); workItem = client.ListWorkItems(options).First(); } return workItem.ExecutionInformation.RecentJob.Name; }
/// <summary> /// Waits for a recent job on a job schedule and returns its id. If a previous job is specified, this method waits until a new job is created. /// </summary> public static string WaitForRecentJob(BatchController controller, BatchAccountContext context, string jobScheduleId, string previousJob = null) { DateTime timeout = DateTime.Now.AddMinutes(2); RequestInterceptor interceptor = CreateHttpRecordingInterceptor(); BatchClientBehavior[] behaviors = new BatchClientBehavior[] { interceptor }; BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); ListJobScheduleOptions options = new ListJobScheduleOptions(context, behaviors) { JobScheduleId = jobScheduleId, Filter = null, MaxCount = Constants.DefaultMaxCount }; PSCloudJobSchedule jobSchedule = client.ListJobSchedules(options).First(); while (jobSchedule.ExecutionInformation.RecentJob == null || string.Equals(jobSchedule.ExecutionInformation.RecentJob.Id, previousJob, StringComparison.OrdinalIgnoreCase)) { if (DateTime.Now > timeout) { throw new TimeoutException("Timed out waiting for recent job"); } Sleep(5000); jobSchedule = client.ListJobSchedules(options).First(); } return jobSchedule.ExecutionInformation.RecentJob.Id; }
/// <summary> /// Terminates a job /// </summary> public static void TerminateJob(BatchController controller, BatchAccountContext context, string jobId) { BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); TerminateJobParameters parameters = new TerminateJobParameters(context, jobId, null); client.TerminateJob(parameters); }
/// <summary> /// Waits for the specified task to complete /// </summary> public static void WaitForTaskCompletion(BatchController controller, BatchAccountContext context, string jobId, string taskId) { RequestInterceptor interceptor = CreateHttpRecordingInterceptor(); BatchClientBehavior[] behaviors = new BatchClientBehavior[] { interceptor }; BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); ListTaskOptions options = new ListTaskOptions(context, jobId, null, behaviors) { TaskId = taskId }; IEnumerable<PSCloudTask> tasks = client.ListTasks(options); // Save time by not waiting during playback scenarios if (HttpMockServer.Mode == HttpRecorderMode.Record) { TaskStateMonitor monitor = context.BatchOMClient.Utilities.CreateTaskStateMonitor(); monitor.WaitAll(tasks.Select(t => t.omObject), TaskState.Completed, TimeSpan.FromMinutes(2), null); } }
/// <summary> /// Gets the id of a compute node in the specified pool /// </summary> public static string GetComputeNodeId(BatchController controller, BatchAccountContext context, string poolId) { RequestInterceptor interceptor = CreateHttpRecordingInterceptor(); BatchClientBehavior[] behaviors = new BatchClientBehavior[] { interceptor }; BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); ListComputeNodeOptions options = new ListComputeNodeOptions(context, poolId, null, behaviors); return client.ListComputeNodes(options).First().Id; }
/// <summary> /// Deletes an application package used in a Scenario test. /// </summary> public static void DeleteApplicationPackage(BatchController controller, BatchAccountContext context, string applicationId, string version) { BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); client.DeleteApplicationPackage(context.ResourceGroupName, context.AccountName, applicationId, version); }
/// <summary> /// Creates a test user for use in Scenario tests. /// </summary> public static void CreateComputeNodeUser(BatchController controller, BatchAccountContext context, string poolId, string computeNodeId, string computeNodeUserName) { RequestInterceptor interceptor = CreateHttpRecordingInterceptor(); BatchClientBehavior[] behaviors = new BatchClientBehavior[] { interceptor }; BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); NewComputeNodeUserParameters parameters = new NewComputeNodeUserParameters(context, poolId, computeNodeId, null, behaviors) { ComputeNodeUserName = computeNodeUserName, Password = "******", }; client.CreateComputeNodeUser(parameters); }
public static void EnableAutoScale(BatchController controller, BatchAccountContext context, string poolId) { RequestInterceptor interceptor = CreateHttpRecordingInterceptor(); BatchClientBehavior[] behaviors = new BatchClientBehavior[] { interceptor }; BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); string formula = "$TargetDedicated=2"; AutoScaleParameters parameters = new AutoScaleParameters(context, poolId, null, formula, behaviors); client.EnableAutoScale(parameters); }
/// <summary> /// Creates a test pool for use in Scenario tests. /// </summary> public static void CreateTestPool(BatchController controller, BatchAccountContext context, string poolId, int targetDedicated) { RequestInterceptor interceptor = CreateHttpRecordingInterceptor(); BatchClientBehavior[] behaviors = new BatchClientBehavior[] { interceptor }; BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); NewPoolParameters parameters = new NewPoolParameters(context, poolId, behaviors) { VirtualMachineSize = "small", OSFamily = "4", TargetOSVersion = "*", TargetDedicated = targetDedicated, }; client.CreatePool(parameters); }
/// <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) { BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient); X509Certificate2 cert = new X509Certificate2(filePath); ListCertificateOptions getParameters = new ListCertificateOptions(context) { 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); client.AddCertificate(parameters); return cert.Thumbprint; }