public void CanCreatePoolWithTwoAppPkgRefAndVersion() { var poolId = Guid.NewGuid().ToString(); var client = new BatchServiceClient(new Uri(Url), new BatchSharedKeyCredential(AccountName, AccountKey)); var poolAddParameter = new PoolAddParameter(poolId, "small", "4"); poolAddParameter.ApplicationPackageReferences = new[] { new ApplicationPackageReference { ApplicationId = AppPackageIdOne, Version = "1.0" }, new ApplicationPackageReference { ApplicationId = AppPackageIdTwo }, }; try { var response = client.Pool.AddWithHttpMessagesAsync(poolAddParameter).Result; Assert.Equal(HttpStatusCode.Created, response.Response.StatusCode); } finally { TestUtilities.DeletePoolIfExistsNoThrow(client, poolId, output); } }
public void CanPatchPoolByDeletingAppPkgRefs() { var poolId = Guid.NewGuid().ToString(); var client = new BatchServiceClient(new Uri(Url), new BatchSharedKeyCredential(AccountName, AccountKey)); var poolAddParameter = new PoolAddParameter(poolId, "small", "4"); poolAddParameter.ApplicationPackageReferences = new[] { new ApplicationPackageReference { ApplicationId = AppPackageIdOne, Version = "1.0" }, new ApplicationPackageReference { ApplicationId = AppPackageIdTwo }, }; try { client.Pool.AddAsync(poolAddParameter).Wait(); var patchParams = new PoolPatchParameter(); patchParams.ApplicationPackageReferences = new ApplicationPackageReference[] { }; var updateResponse = client.Pool.PatchWithHttpMessagesAsync(poolId, patchParams).Result; Assert.Equal(HttpStatusCode.OK, updateResponse.Response.StatusCode); var pool = client.Pool.GetAsync(poolId).Result; Assert.Equal(poolId, pool.Id); Assert.Null(pool.ApplicationPackageReferences); } finally { TestUtilities.DeletePoolIfExistsNoThrow(client, poolId, output); } }
public void CanCreatePoolAndRetrieveAppPkgRefs() { var poolId = Guid.NewGuid().ToString(); var client = new BatchServiceClient(new Uri(Url), new BatchSharedKeyCredential(AccountName, AccountKey)); var poolAddParameter = new PoolAddParameter(poolId, "small", "4"); poolAddParameter.ApplicationPackageReferences = new[] { new ApplicationPackageReference { ApplicationId = AppPackageIdOne, Version = "1.0" }, new ApplicationPackageReference { ApplicationId = AppPackageIdTwo }, }; try { var addResponse = client.Pool.AddAsync(poolAddParameter).Result; var pool = client.Pool.GetAsync(poolId).Result; Assert.Equal(poolId, pool.Id); Assert.Equal(2, pool.ApplicationPackageReferences.Count); Assert.Equal(AppPackageIdOne, pool.ApplicationPackageReferences[0].ApplicationId); Assert.Equal("1.0", pool.ApplicationPackageReferences[0].Version); Assert.Equal(AppPackageIdTwo, pool.ApplicationPackageReferences[1].ApplicationId); Assert.Null(pool.ApplicationPackageReferences[1].Version); } finally { TestUtilities.DeletePoolIfExistsNoThrow(client, poolId, output); } }
public async Task BatchAuthTokenIsSentToTheService_ClientCreatedWithToken() { var tokenCredentials = new TokenCredentials("foo"); HttpRequestMessage capturedRequest = null; var fakeHttpClientHandler = new FakeHttpClientHandler(req => { capturedRequest = req; return(new HttpResponseMessage(HttpStatusCode.Accepted)); }); using (var restClient = new BatchServiceClient(tokenCredentials, fakeHttpClientHandler) { BatchUrl = @"https://foo.microsoft.test" }) { using (var client = BatchClient.Open(restClient)) { await client.PoolOperations.DeletePoolAsync("bar"); } } Assert.Equal("foo", capturedRequest.Headers.Authorization.Parameter); Assert.Equal("Bearer", capturedRequest.Headers.Authorization.Scheme); }
public void MultipleAppPackageReferenceWithNoDefaultFails() { string poolId = Guid.NewGuid().ToString(); var client = new BatchServiceClient(new Uri(Url), new BatchSharedKeyCredential(AccountName, AccountKey)); var poolAddParameter = new PoolAddParameter(poolId, "small", "4"); poolAddParameter.ApplicationPackageReferences = new[] { new ApplicationPackageReference { ApplicationId = AppPackageIdOne }, // valid pkg, but has no default set new ApplicationPackageReference { ApplicationId = AppPackageIdTwo } }; var exception = Assert.Throws <AggregateException>(() => client.Pool.AddAsync(poolAddParameter).Result); var batchException = (BatchErrorException)exception.InnerException; Assert.NotNull(batchException); Assert.Equal("InvalidApplicationPackageReferences", batchException.Body.Code); Assert.Equal(1, batchException.Body.Values.Count); Assert.Equal(AppPackageIdOne, batchException.Body.Values[0].Key); Assert.Equal("The specified application package does not have a default version set.", batchException.Body.Values[0].Value); }
public async Task AuthTokenIsBeingSentOnEveryCallToTheService() { int count = 0; var tokenCredentials = new TokenCredentials(new BatchTokenProvider(() => { count++; return(Task.FromResult("foo")); })); using (var restClient = new BatchServiceClient( tokenCredentials, new FakeHttpClientHandler(req => new HttpResponseMessage(HttpStatusCode.Accepted))) { BatchUrl = @"https://foo.microsoft.test" }) { var client = BatchClient.Open(restClient); await client.PoolOperations.DeletePoolAsync("bar", new List <BatchClientBehavior>()); Assert.Equal(1, count); await client.PoolOperations.DeletePoolAsync("bar", new List <BatchClientBehavior>()); Assert.Equal(2, count); } }
protected override BatchServiceClient CreateBatchRestClient(string url, ServiceClientCredentials credentials, DelegatingHandler handler = default(DelegatingHandler)) { // Add HTTP recorder to the BatchRestClient HttpMockServer mockServer = HttpMockServer.CreateInstance(); mockServer.InnerHandler = new HttpClientHandler(); BatchServiceClient restClient = base.CreateBatchRestClient(url, credentials, mockServer); return(restClient); }
public static void DeletePoolIfExistsNoThrow(BatchServiceClient client, string poolId, ITestOutputHelper output) { try { client.Pool.Delete(poolId); } catch (BatchErrorException e) { output.WriteLine("Pool failed to delete: {0}", e); } }
public static void DeleteJobIfExistsNoThrow(BatchServiceClient client, string jobId, ITestOutputHelper output) { try { client.Job.Delete(jobId); } catch (BatchErrorException e) { output.WriteLine("Job failed to delete: {0}", e); } }
protected virtual BatchServiceClient CreateBatchRestClient(string url, ServiceClientCredentials creds, DelegatingHandler handler = default(DelegatingHandler)) { BatchServiceClient restClient = handler == null ? new BatchServiceClient(new Uri(url), creds) : new BatchServiceClient(new Uri(url), creds, handler); restClient.HttpClient.DefaultRequestHeaders.UserAgent.Add(Microsoft.WindowsAzure.Commands.Common.AzurePowerShell.UserAgentValue); restClient.SetRetryPolicy(null); //Force there to be no retries restClient.HttpClient.Timeout = Timeout.InfiniteTimeSpan; //Client side timeout will be set per-request return(restClient); }
private string WaitForAutoPool(BatchServiceClient client, string jobId) { var stopwatch = Stopwatch.StartNew(); string poolId = null; while (true) { if (stopwatch.Elapsed > TimeSpan.FromMinutes(1)) { throw new Exception("Timeout waiting for auto pool for job " + jobId); } var job = client.Job.GetAsync(jobId).Result; if (!string.IsNullOrEmpty(job.ExecutionInfo.PoolId)) { poolId = job.ExecutionInfo.PoolId; break; } Thread.Sleep(1000); } stopwatch.Restart(); while (true) { if (stopwatch.Elapsed > TimeSpan.FromMinutes(1)) { throw new Exception("Timeout waiting for auto pool for job " + jobId); } try { var response = client.Pool.GetAsync(poolId).Result; return(poolId); } catch (Exception) { // ignored } Thread.Sleep(1000); } }
public async Task AutoPoolApplicationPackagesFlowThroughToPool() { var jobId = Guid.NewGuid().ToString(); var client = new BatchServiceClient(new Uri(Url), new BatchSharedKeyCredential(AccountName, AccountKey)); var poolInfo = new PoolInformation { AutoPoolSpecification = new AutoPoolSpecification(poolLifetimeOption: PoolLifetimeOption.Job, pool: new PoolSpecification { TargetDedicated = 0, ApplicationPackageReferences = new[] { new ApplicationPackageReference { ApplicationId = AppPackageName, Version = Version }, }, CloudServiceConfiguration = new CloudServiceConfiguration("4"), VmSize = "small", }) }; try { AzureOperationHeaderResponse <JobAddHeaders> addResponse = await client.Job.AddWithHttpMessagesAsync(new JobAddParameter(jobId, poolInfo : poolInfo)).ConfigureAwait(false); Assert.Equal(HttpStatusCode.Created, addResponse.Response.StatusCode); var autoPoolId = WaitForAutoPool(client, jobId); var poolResponse = await client.Pool.GetWithHttpMessagesAsync(autoPoolId).ConfigureAwait(false); Assert.Equal(HttpStatusCode.OK, poolResponse.Response.StatusCode); Assert.NotNull(poolResponse.Body); Assert.Equal(1, poolResponse.Body.ApplicationPackageReferences.Count); Assert.Equal(AppPackageName, poolResponse.Body.ApplicationPackageReferences[0].ApplicationId); Assert.Equal(Version, poolResponse.Body.ApplicationPackageReferences[0].Version); } finally { TestUtilities.DeleteJobIfExistsNoThrow(client, jobId, output); } }
public void BadAppPackageReferenceAndVersionFails() { var poolId = Guid.NewGuid().ToString(); var client = new BatchServiceClient(new Uri(Url), new BatchSharedKeyCredential(AccountName, AccountKey)); var poolAddParameter = new PoolAddParameter(poolId, "small", "4"); poolAddParameter.ApplicationPackageReferences = new[] { new ApplicationPackageReference { ApplicationId = "bad", Version = "999" } }; var exception = Assert.Throws <AggregateException>(() => client.Pool.AddAsync(poolAddParameter).Result); var batchException = (BatchErrorException)exception.InnerException; Assert.NotNull(batchException); Assert.Equal("InvalidApplicationPackageReferences", batchException.Body.Code); Assert.Equal(1, batchException.Body.Values.Count()); Assert.Equal("bad:999", batchException.Body.Values[0].Key); Assert.Equal("The specified application package does not exist.", batchException.Body.Values[0].Value); }
public void CanUpdatePoolByAddingAppPkgRefs() { var poolId = Guid.NewGuid().ToString(); var client = new BatchServiceClient(new Uri(Url), new BatchSharedKeyCredential(AccountName, AccountKey)); var poolAddParameter = new PoolAddParameter(poolId, "small", "4"); try { var addResponse = client.Pool.AddAsync(poolAddParameter).Result; var appRefs = new List <ApplicationPackageReference> { new ApplicationPackageReference { ApplicationId = AppPackageIdOne, Version = "1.0" }, new ApplicationPackageReference { ApplicationId = AppPackageIdTwo }, }; var updateParams = new PoolUpdatePropertiesParameter(new List <CertificateReference>(), appRefs, new List <MetadataItem>()); var updateResponse = client.Pool.UpdatePropertiesWithHttpMessagesAsync(poolId, updateParams).Result; Assert.Equal(HttpStatusCode.NoContent, updateResponse.Response.StatusCode); var pool = client.Pool.GetAsync(poolId).Result; Assert.Equal(poolId, pool.Id); Assert.Equal(2, pool.ApplicationPackageReferences.Count); Assert.Equal(AppPackageIdOne, pool.ApplicationPackageReferences[0].ApplicationId); Assert.Equal("1.0", pool.ApplicationPackageReferences[0].Version); Assert.Equal(AppPackageIdTwo, pool.ApplicationPackageReferences[1].ApplicationId); Assert.Null(pool.ApplicationPackageReferences[1].Version); } finally { TestUtilities.DeletePoolIfExistsNoThrow(client, poolId, output); } }