|
| 1 | +// ---------------------------------------------------------------------------------- |
| 2 | +// |
| 3 | +// Copyright Microsoft Corporation |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | +// ---------------------------------------------------------------------------------- |
| 14 | + |
| 15 | +using Hyak.Common; |
| 16 | +using Microsoft.Azure.Commands.Profile.Errors; |
| 17 | +using Microsoft.Azure.Commands.ScenarioTest; |
| 18 | +using Microsoft.WindowsAzure.Commands.Common.Test.Mocks; |
| 19 | +using Microsoft.WindowsAzure.Commands.ScenarioTest; |
| 20 | +using Microsoft.WindowsAzure.Commands.Utilities.Common; |
| 21 | +using System; |
| 22 | +using System.Collections.Generic; |
| 23 | +using System.Management.Automation; |
| 24 | +using System.Net; |
| 25 | +using System.Net.Http; |
| 26 | +using Xunit; |
| 27 | + |
| 28 | +namespace Microsoft.Azure.Commands.Profile.Test |
| 29 | +{ |
| 30 | + public class ErrorResolutionTests |
| 31 | + { |
| 32 | + class TestHyakException : CloudException |
| 33 | + { |
| 34 | + public TestHyakException(string message, CloudHttpRequestErrorInfo request, CloudHttpResponseErrorInfo response) : base(message) |
| 35 | + { |
| 36 | + Request = request; |
| 37 | + Response = response; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + [Fact] |
| 42 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 43 | + public void DoesNotThrowWithNullError() |
| 44 | + { |
| 45 | + TestExecutionHelpers.SetUpSessionAndProfile(); |
| 46 | + var cmdlet = new ResolveError(); |
| 47 | + var output = cmdlet.ExecuteCmdletInPipeline<AzureErrorRecord>("Resolve-Error"); |
| 48 | + Assert.True(output == null || output.Count == 0); |
| 49 | + output = cmdlet.ExecuteCmdletInPipeline<AzureErrorRecord>("Resolve-Error", new ErrorRecord[] { null, null }); |
| 50 | + Assert.True(output == null || output.Count == 0); |
| 51 | + output = cmdlet.ExecuteCmdletInPipeline<AzureErrorRecord>("Resolve-Error", new ErrorRecord[] { null, new ErrorRecord(new Exception(null), null, ErrorCategory.AuthenticationError, null) }); |
| 52 | + Assert.NotNull(output); |
| 53 | + Assert.Equal(1, output.Count); |
| 54 | + var record = output[0] as AzureExceptionRecord; |
| 55 | + Assert.NotNull(record); |
| 56 | + Assert.Equal(ErrorCategory.AuthenticationError, record.ErrorCategory.Category); |
| 57 | + } |
| 58 | + |
| 59 | + [Fact] |
| 60 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 61 | + public void HandlesExceptionError() |
| 62 | + { |
| 63 | + var runtime = new MockCommandRuntime(); |
| 64 | + var request = new HttpRequestMessage(HttpMethod.Get, new Uri("https://www.contoso.com/resource?api-version-1.0")); |
| 65 | + request.Headers.Add("x-ms-request-id", "HyakRequestId"); |
| 66 | + var response = new HttpResponseMessage(HttpStatusCode.BadRequest); |
| 67 | + var hyakException = new TestHyakException("exception message", CloudHttpRequestErrorInfo.Create(request), CloudHttpResponseErrorInfo.Create(response)) |
| 68 | + { |
| 69 | + Error = new Hyak.Common.CloudError { Code="HyakCode", Message="HyakError"} |
| 70 | + }; |
| 71 | + |
| 72 | + var autorestException = new Microsoft.Rest.Azure.CloudException("exception message") |
| 73 | + { |
| 74 | + Body = new Microsoft.Rest.Azure.CloudError { Code = "AutorestCode", Message = "Autorest message" }, |
| 75 | + Request = new Rest.HttpRequestMessageWrapper(request, ""), |
| 76 | + Response = new Rest.HttpResponseMessageWrapper(response, ""), |
| 77 | + RequestId = "AutoRestRequestId" |
| 78 | + }; |
| 79 | + |
| 80 | + var cmdlet = new ResolveError |
| 81 | + { |
| 82 | + Error = new [] |
| 83 | + { |
| 84 | + new ErrorRecord(new Exception("exception message"), "errorCode", ErrorCategory.AuthenticationError, this), |
| 85 | + new ErrorRecord(hyakException, "errorCode", ErrorCategory.ConnectionError, this), |
| 86 | + new ErrorRecord(autorestException , "errorCode", ErrorCategory.InvalidOperation, this), |
| 87 | + }, |
| 88 | + CommandRuntime = runtime |
| 89 | + }; |
| 90 | + |
| 91 | + cmdlet.ExecuteCmdlet(); |
| 92 | + Assert.NotNull(runtime.OutputPipeline); |
| 93 | + Assert.Equal(3, runtime.OutputPipeline.Count); |
| 94 | + var errorResult = runtime.OutputPipeline[0] as AzureExceptionRecord; |
| 95 | + Assert.NotNull(errorResult); |
| 96 | + Assert.Equal(ErrorCategory.AuthenticationError, errorResult.ErrorCategory.Category); |
| 97 | + Assert.NotNull(errorResult.Exception); |
| 98 | + Assert.Equal(errorResult.Exception.GetType(), typeof(Exception)); |
| 99 | + Assert.Equal("exception message", errorResult.Exception.Message); |
| 100 | + var hyakResult = runtime.OutputPipeline[1] as AzureRestExceptionRecord; |
| 101 | + Assert.NotNull(hyakResult); |
| 102 | + Assert.Equal(ErrorCategory.ConnectionError, hyakResult.ErrorCategory.Category); |
| 103 | + Assert.NotNull(errorResult.Exception); |
| 104 | + Assert.Equal(hyakResult.Exception.GetType(), typeof(TestHyakException)); |
| 105 | + Assert.Equal("exception message", hyakResult.Exception.Message); |
| 106 | + Assert.NotNull(hyakResult.RequestMessage); |
| 107 | + Assert.Equal(HttpMethod.Get.ToString(), hyakResult.RequestMessage.Verb); |
| 108 | + Assert.Equal(new Uri("https://www.contoso.com/resource?api-version-1.0"), hyakResult.RequestMessage.Uri); |
| 109 | + Assert.NotNull(hyakResult.ServerResponse); |
| 110 | + Assert.Equal(HttpStatusCode.BadRequest.ToString(), hyakResult.ServerResponse.ResponseStatusCode); |
| 111 | + var autorestResult = runtime.OutputPipeline[2] as AzureRestExceptionRecord; |
| 112 | + Assert.NotNull(autorestResult); |
| 113 | + Assert.Equal(ErrorCategory.InvalidOperation, autorestResult.ErrorCategory.Category); |
| 114 | + Assert.NotNull(autorestResult.Exception); |
| 115 | + Assert.Equal(autorestResult.Exception.GetType(), typeof(Microsoft.Rest.Azure.CloudException)); |
| 116 | + Assert.Equal("exception message", autorestResult.Exception.Message); |
| 117 | + Assert.NotNull(autorestResult.RequestMessage); |
| 118 | + Assert.Equal(HttpMethod.Get.ToString(), autorestResult.RequestMessage.Verb); |
| 119 | + Assert.Equal(new Uri("https://www.contoso.com/resource?api-version-1.0"), autorestResult.RequestMessage.Uri); |
| 120 | + Assert.NotNull(autorestResult.ServerResponse); |
| 121 | + Assert.Equal(HttpStatusCode.BadRequest.ToString(), autorestResult.ServerResponse.ResponseStatusCode); |
| 122 | + Assert.Equal("AutoRestRequestId", autorestResult.RequestId); |
| 123 | + Assert.Contains("AutorestCode", autorestResult.ServerMessage); |
| 124 | + Assert.Contains("Autorest message", autorestResult.ServerMessage); |
| 125 | + } |
| 126 | + |
| 127 | + [Fact] |
| 128 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 129 | + public void LastParameterFindsLastError() |
| 130 | + { |
| 131 | + TestExecutionHelpers.SetUpSessionAndProfile(); |
| 132 | + var mock = new MockCommandRuntime(); |
| 133 | + var cmdlet = new ResolveError { CommandRuntime = mock }; |
| 134 | + var message = "RuntimeErrorMessage"; |
| 135 | + var exception = new Exception(message); |
| 136 | + cmdlet.ExecuteCmdletWithExceptionInPipeline<AzureErrorRecord>("Resolve-AzureRmError", exception, new KeyValuePair<string, object>("Last", null ) ); |
| 137 | + Assert.NotNull(mock.ErrorStream); |
| 138 | + Assert.Equal(1, mock.ErrorStream.Count); |
| 139 | + Assert.NotNull(mock.OutputPipeline); |
| 140 | + Assert.Equal(1, mock.OutputPipeline.Count); |
| 141 | + var record = mock.OutputPipeline[0] as AzureExceptionRecord; |
| 142 | + Assert.NotNull(record); |
| 143 | + Assert.NotNull(record.Exception); |
| 144 | + Assert.Equal(typeof(Exception), record.Exception.GetType()); |
| 145 | + Assert.Equal(message, record.Message); |
| 146 | + |
| 147 | + |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments