public void AzureImsResponseTimesOut() { string testPath = "timeOut"; using (new AzureInstanceMetadataServiceMock( AzureInstanceMetadataEndToEndTests.MockTestUri, testPath, (response) => { // wait for longer than the request timeout Thread.Sleep(TimeSpan.FromSeconds(5)); response.StatusCode = (int)HttpStatusCode.OK; var jsonStream = this.GetTestMetadataStream(); response.SetContentLength(jsonStream.Length); response.ContentType = "application/json"; response.SetContentEncoding(Encoding.UTF8); response.WriteStreamToBody(jsonStream); })) { var azureIms = new AzureMetadataRequestor { BaseAimsUri = string.Concat(AzureInstanceMetadataEndToEndTests.MockTestUri, testPath, "/"), AzureImsRequestTimeout = TimeSpan.FromSeconds(1) }; var azureIMSData = azureIms.GetAzureComputeMetadataAsync(); azureIMSData.Wait(); Assert.Null(azureIMSData.Result); } }
public void AzureIMSGetFailsWithException() { var requestor = new AzureMetadataRequestor(makeAzureIMSRequestor: (string uri) => { throw new HttpRequestException("MaxResponseContentLength exceeded"); }); try { var result = requestor.GetAzureComputeMetadataAsync(); Assert.Null(result.GetAwaiter().GetResult()); } catch { Assert.True(false, "Expectation is that exceptions will be handled within AzureMetadataRequestor, not the calling code."); } }
public void AzureImsResponseTooLargeStopsCollection() { string testPath = "tooLarge"; using (new AzureInstanceMetadataServiceMock( AzureInstanceMetadataEndToEndTests.MockTestUri, testPath, (response) => { response.StatusCode = (int)HttpStatusCode.OK; var tester = this.GetTestMetadata(); // ensure we will be outside the max allowed content size by setting a single text field to max length + 1 var testStuff = new char[AzureMetadataRequestor.AzureImsMaxResponseBufferSize + 1]; for (int i = 0; i < (AzureMetadataRequestor.AzureImsMaxResponseBufferSize + 1); ++i) { testStuff[i] = (char)((int)'a' + (i % 26)); } tester.Publisher = new string(testStuff); var jsonStream = this.GetTestMetadataStream(tester); response.SetContentLength(3 * jsonStream.Length); response.ContentType = "application/json"; response.SetContentEncoding(Encoding.UTF8); response.WriteStreamToBody(jsonStream); })) { var azureIms = new AzureMetadataRequestor { BaseAimsUri = string.Concat(AzureInstanceMetadataEndToEndTests.MockTestUri, testPath, "/") }; var azureIMSData = azureIms.GetAzureComputeMetadataAsync(); azureIMSData.Wait(); Assert.Null(azureIMSData.Result); } }
public void SpoofedResponseFromAzureIMSDoesntCrash() { var testMetadata = this.GetTestMetadata(); string testPath = "spoofedResponse"; using (new AzureInstanceMetadataServiceMock( AzureInstanceMetadataEndToEndTests.MockTestUri, testPath, (response) => { response.StatusCode = (int)HttpStatusCode.OK; var jsonStream = this.GetTestMetadataStream(testMetadata); response.SetContentLength(jsonStream.Length); response.ContentType = "application/json"; response.SetContentEncoding(Encoding.UTF8); response.WriteStreamToBody(jsonStream); })) { var azureIms = new AzureMetadataRequestor { BaseAimsUri = string.Concat(AzureInstanceMetadataEndToEndTests.MockTestUri, testPath, "/") }; var azureImsProps = new AzureComputeMetadataHeartbeatPropertyProvider(); var azureIMSData = azureIms.GetAzureComputeMetadataAsync(); azureIMSData.Wait(); foreach (string fieldName in azureImsProps.ExpectedAzureImsFields) { string fieldValue = azureIMSData.Result.GetValueForField(fieldName); Assert.NotNull(fieldValue); Assert.Equal(fieldValue, testMetadata.GetValueForField(fieldName)); } } }
public void AzureImsResponseUnsuccessful() { string testPath = "errorForbidden"; using (new AzureInstanceMetadataServiceMock( AzureInstanceMetadataEndToEndTests.MockTestUri, testPath, (response) => { // don't send anything in content at all, or the context defaults to 200 OK response.StatusCode = (int)HttpStatusCode.Forbidden; })) { var azureIms = new AzureMetadataRequestor { BaseAimsUri = string.Concat(AzureInstanceMetadataEndToEndTests.MockTestUri, testPath, "/") }; var azureIMSData = azureIms.GetAzureComputeMetadataAsync(); azureIMSData.Wait(); Assert.Null(azureIMSData.Result); } }