Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display AzurePowershell Survey url for Resolve-Error #11041

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/Accounts/Accounts.Test/ErrorResolutionScenarioTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.WindowsAzure.Commands.ScenarioTest;
using Xunit;
using Xunit.Abstractions;

namespace Microsoft.Azure.Commands.Profile.Test
{
public class ErrorResolutionScenarioTests : AccountsTestRunner
{
public ErrorResolutionScenarioTests(ITestOutputHelper output)
: base(output)
{
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void WriteInformationInResolveError()
{
TestRunner.RunTestScript("Test-WriteInformationInResolveError");
}
}
}
26 changes: 26 additions & 0 deletions src/Accounts/Accounts.Test/ErrorResolutionScenarioTests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -----------------------------------------------------------------------------------
#
# Copyright Microsoft Corporation
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ----------------------------------------------------------------------------------

<#
.SYNOPSIS
Tests cmdlets surrounding default resource group
#>

function Test-WriteInformationInResolveError
{
Assert-Throws { Connect-AzAccount -Wrong }
$err = Resolve-AzError -Last
Assert-AreEqual 1 $err.length
Assert-AreEqual "Microsoft.Azure.Commands.Profile.Errors.AzureExceptionRecord" $err[0].GetType().FullName
}
52 changes: 37 additions & 15 deletions src/Accounts/Accounts.Test/ErrorResolutionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// ----------------------------------------------------------------------------------

using Hyak.Common;
using Microsoft.Azure.Commands.Profile.Common;
using Microsoft.Azure.Commands.Profile.Errors;
using Microsoft.Azure.Commands.ScenarioTest;
using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
Expand All @@ -29,6 +30,20 @@ namespace Microsoft.Azure.Commands.Profile.Test
{
public class ErrorResolutionTests
{
[Cmdlet("Mock", "Resolve" + ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "Error", DefaultParameterSetName = ResolveError.AnyErrorParameterSet)]
class MockResolveError : ResolveError
{
protected override void WriteInformationWrapper(string text)
{
var informationMessage = new HostInformationMessage();
informationMessage.Message = $"{Environment.NewLine}{text}{Environment.NewLine}";
informationMessage.NoNewLine = false;

ICommandRuntime2 commandRuntime2 = CommandRuntime as ICommandRuntime2;
InformationRecord informationRecord = new InformationRecord(informationMessage, String.Empty);
commandRuntime2.WriteInformation(informationRecord);
}
}
class TestHyakException : CloudException
{
public TestHyakException(string message, CloudHttpRequestErrorInfo request, CloudHttpResponseErrorInfo response) : base(message)
Expand All @@ -43,12 +58,12 @@ public TestHyakException(string message, CloudHttpRequestErrorInfo request, Clou
public void DoesNotThrowWithNullError()
{
TestExecutionHelpers.SetUpSessionAndProfile();
var cmdlet = new ResolveError();
var output = cmdlet.ExecuteCmdletInPipeline<AzureErrorRecord>("Resolve-Error");
var cmdlet = new MockResolveError();
var output = cmdlet.ExecuteCmdletInPipeline<AzureErrorRecord>("Mock-ResolveAzError");
Assert.True(output == null || output.Count == 0);
output = cmdlet.ExecuteCmdletInPipeline<AzureErrorRecord>("Resolve-Error", new ErrorRecord[] { null, null });
output = cmdlet.ExecuteCmdletInPipeline<AzureErrorRecord>("Mock-ResolveAzError", new ErrorRecord[] { null, null });
Assert.True(output == null || output.Count == 0);
output = cmdlet.ExecuteCmdletInPipeline<AzureErrorRecord>("Resolve-Error", new ErrorRecord[] { null, new ErrorRecord(new Exception(null), null, ErrorCategory.AuthenticationError, null) });
output = cmdlet.ExecuteCmdletInPipeline<AzureErrorRecord>("Mock-ResolveAzError", new ErrorRecord[] { null, new ErrorRecord(new Exception(null), null, ErrorCategory.AuthenticationError, null) });
Assert.NotNull(output);
Assert.Single(output);
var record = output[0] as AzureExceptionRecord;
Expand All @@ -66,7 +81,7 @@ public void HandlesExceptionError()
var response = new HttpResponseMessage(HttpStatusCode.BadRequest);
var hyakException = new TestHyakException("exception message", CloudHttpRequestErrorInfo.Create(request), CloudHttpResponseErrorInfo.Create(response))
{
Error = new Hyak.Common.CloudError { Code="HyakCode", Message="HyakError"}
Error = new Hyak.Common.CloudError { Code = "HyakCode", Message = "HyakError" }
};

var autorestException = new Microsoft.Rest.Azure.CloudException("exception message")
Expand All @@ -77,17 +92,16 @@ public void HandlesExceptionError()
RequestId = "AutoRestRequestId"
};

var cmdlet = new ResolveError
var cmdlet = new MockResolveError
{
Error = new []
Error = new[]
{
new ErrorRecord(new Exception("exception message"), "errorCode", ErrorCategory.AuthenticationError, this),
new ErrorRecord(hyakException, "errorCode", ErrorCategory.ConnectionError, this),
new ErrorRecord(autorestException , "errorCode", ErrorCategory.InvalidOperation, this),
},
CommandRuntime = runtime
};

cmdlet.ExecuteCmdlet();
Assert.NotNull(runtime.OutputPipeline);
Assert.Equal(3, runtime.OutputPipeline.Count);
Expand Down Expand Up @@ -122,6 +136,11 @@ public void HandlesExceptionError()
Assert.Equal("AutoRestRequestId", autorestResult.RequestId);
Assert.Contains("AutorestCode", autorestResult.ServerMessage);
Assert.Contains("Autorest message", autorestResult.ServerMessage);

Assert.Single(runtime.informationStream);
char[] charsToTrim = Environment.NewLine.ToCharArray();
Assert.Equal(AzureProfileConstants.AzurePowerShellFeedbackMessage
, runtime.informationStream[0].MessageData.ToString().Trim(charsToTrim));
}

[Fact]
Expand All @@ -130,10 +149,10 @@ public void HandlesNullValuesInArmExceptions()
{
var runtime = new MockCommandRuntime();
var hyakException = new TestHyakException(null, null, null);

var autorestException = new Microsoft.Rest.Azure.CloudException();
var cmdlet = new ResolveError

var cmdlet = new MockResolveError
{
Error = new[]
{
Expand All @@ -143,7 +162,7 @@ public void HandlesNullValuesInArmExceptions()
},
CommandRuntime = runtime
};

cmdlet.ExecuteCmdlet();
Assert.NotNull(runtime.OutputPipeline);
Assert.Equal(3, runtime.OutputPipeline.Count);
Expand All @@ -162,6 +181,11 @@ public void HandlesNullValuesInArmExceptions()
Assert.Equal(ErrorCategory.InvalidOperation, autorestResult.ErrorCategory.Category);
Assert.NotNull(autorestResult.Exception);
Assert.Equal(typeof(Microsoft.Rest.Azure.CloudException), autorestResult.Exception.GetType());

Assert.Single(runtime.informationStream);
char[] charsToTrim = Environment.NewLine.ToCharArray();
Assert.Equal(AzureProfileConstants.AzurePowerShellFeedbackMessage
, runtime.informationStream[0].MessageData.ToString().Trim(charsToTrim));
}

[Fact]
Expand All @@ -173,7 +197,7 @@ public void LastParameterFindsLastError()
var cmdlet = new ResolveError { CommandRuntime = mock };
var message = "RuntimeErrorMessage";
var exception = new Exception(message);
cmdlet.ExecuteCmdletWithExceptionInPipeline<AzureErrorRecord>("Resolve-AzureRmError", exception, new KeyValuePair<string, object>("Last", null ) );
cmdlet.ExecuteCmdletWithExceptionInPipeline<AzureErrorRecord>("Mock-ResolveAzError", exception, new KeyValuePair<string, object>("Last", null));
Assert.NotNull(mock.ErrorStream);
Assert.Single(mock.ErrorStream);
Assert.NotNull(mock.OutputPipeline);
Expand All @@ -183,8 +207,6 @@ public void LastParameterFindsLastError()
Assert.NotNull(record.Exception);
Assert.Equal(typeof(Exception), record.Exception.GetType());
Assert.Equal(message, record.Message);


}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
{
"Entries": [
{
"RequestUri": "/tenants?api-version=2016-06-01",
"EncodedRequestUri": "L3RlbmFudHM/YXBpLXZlcnNpb249MjAxNi0wNi0wMQ==",
"RequestMethod": "GET",
"RequestBody": "",
"RequestHeaders": {
"x-ms-client-request-id": [
"5b28f448-239c-4ac6-ab5c-d706ee4ec51e"
],
"Accept-Language": [
"en-US"
],
"User-Agent": [
"FxVersion/4.6.28207.03",
"OSName/Windows",
"OSVersion/Microsoft.Windows.10.0.18363.",
"Microsoft.Azure.Internal.Subscriptions.SubscriptionClient/1.3.7"
]
},
"ResponseHeaders": {
"Cache-Control": [
"no-cache"
],
"Pragma": [
"no-cache"
],
"x-ms-ratelimit-remaining-tenant-reads": [
"11998"
],
"x-ms-request-id": [
"7762c02a-4ba5-43be-a37c-a6c802f5585f"
],
"x-ms-correlation-request-id": [
"7762c02a-4ba5-43be-a37c-a6c802f5585f"
],
"x-ms-routing-request-id": [
"EASTASIA:20200213T030718Z:7762c02a-4ba5-43be-a37c-a6c802f5585f"
],
"Strict-Transport-Security": [
"max-age=31536000; includeSubDomains"
],
"X-Content-Type-Options": [
"nosniff"
],
"Date": [
"Thu, 13 Feb 2020 03:07:18 GMT"
],
"Content-Type": [
"application/json; charset=utf-8"
],
"Expires": [
"-1"
],
"Content-Length": [
"116"
]
},
"ResponseBody": "{\r\n \"value\": [\r\n {\r\n \"id\": \"/tenants/54826b22-38d6-4fb2-bad9-b7b93a3e9c5a\",\r\n \"tenantId\": \"54826b22-38d6-4fb2-bad9-b7b93a3e9c5a\"\r\n }\r\n ]\r\n}",
"StatusCode": 200
},
{
"RequestUri": "/subscriptions?api-version=2016-06-01",
"EncodedRequestUri": "L3N1YnNjcmlwdGlvbnM/YXBpLXZlcnNpb249MjAxNi0wNi0wMQ==",
"RequestMethod": "GET",
"RequestBody": "",
"RequestHeaders": {
"x-ms-client-request-id": [
"b5c0f48f-43ab-4f48-a1e6-84b4a1892524"
],
"Accept-Language": [
"en-US"
],
"User-Agent": [
"FxVersion/4.6.28207.03",
"OSName/Windows",
"OSVersion/Microsoft.Windows.10.0.18363.",
"Microsoft.Azure.Internal.Subscriptions.SubscriptionClient/1.3.7"
]
},
"ResponseHeaders": {
"Cache-Control": [
"no-cache"
],
"Pragma": [
"no-cache"
],
"x-ms-ratelimit-remaining-tenant-reads": [
"11999"
],
"x-ms-request-id": [
"fe449bd7-d1f4-452d-9a58-84fee8efa1b7"
],
"x-ms-correlation-request-id": [
"fe449bd7-d1f4-452d-9a58-84fee8efa1b7"
],
"x-ms-routing-request-id": [
"EASTASIA:20200213T030719Z:fe449bd7-d1f4-452d-9a58-84fee8efa1b7"
],
"Strict-Transport-Security": [
"max-age=31536000; includeSubDomains"
],
"X-Content-Type-Options": [
"nosniff"
],
"Date": [
"Thu, 13 Feb 2020 03:07:18 GMT"
],
"Content-Type": [
"application/json; charset=utf-8"
],
"Expires": [
"-1"
],
"Content-Length": [
"332"
]
},
"ResponseBody": "{\r\n \"value\": [\r\n {\r\n \"id\": \"/subscriptions/0b1f6471-1bf0-4dda-aec3-cb9272f09590\",\r\n \"authorizationSource\": \"RoleBased\",\r\n \"subscriptionId\": \"0b1f6471-1bf0-4dda-aec3-cb9272f09590\",\r\n \"displayName\": \"AzureSDKTest\",\r\n \"state\": \"Enabled\",\r\n \"subscriptionPolicies\": {\r\n \"locationPlacementId\": \"Internal_2014-09-01\",\r\n \"quotaId\": \"Internal_2014-09-01\",\r\n \"spendingLimit\": \"Off\"\r\n }\r\n }\r\n ]\r\n}",
"StatusCode": 200
}
],
"Names": {},
"Variables": {
"SubscriptionId": "0b1f6471-1bf0-4dda-aec3-cb9272f09590"
}
}
1 change: 1 addition & 0 deletions src/Accounts/Accounts/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

## Version 1.7.2
* Added SubscriptionId, TenantId, and execution time into data of client side telemetry
* Display Azure PowerShell survey url to Resolve-Error.

## Version 1.7.1
* Disable context auto saving when AzureRmContext.json not available
Expand Down
10 changes: 5 additions & 5 deletions src/Accounts/Accounts/Common/AzureProfileConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.Commands.Profile.Properties;

namespace Microsoft.Azure.Commands.Profile.Common
{
public static class AzureProfileConstants
{
public const string AzureAutosaveVariable = "Azure_Profile_Autosave";

public const string AzureSurveyUrl = "https://aka.ms/azpssurvey";

public static readonly string AzurePowerShellFeedbackMessage = string.Format(Resources.AzurePowerShellFeedback, AzureProfileConstants.AzureSurveyUrl);
}
}
10 changes: 10 additions & 0 deletions src/Accounts/Accounts/Errors/ResolveError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System.Linq;
using System.Management.Automation;
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
using Microsoft.Azure.Commands.Profile.Common;
using System.Collections;
using Microsoft.Azure.Commands.ResourceManager.Common;
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
Expand Down Expand Up @@ -79,6 +80,15 @@ public override void ExecuteCmdlet()
HandleError(record);
}
}
WriteInformationWrapper(AzureProfileConstants.AzurePowerShellFeedbackMessage);
}

protected virtual void WriteInformationWrapper(string text)
{
var informationMessage = new HostInformationMessage();
informationMessage.Message = $"{Environment.NewLine}{text}{Environment.NewLine}";
informationMessage.NoNewLine = false;
WriteInformation(informationMessage, new string[] { "PSHOST" });
}

private IEnumerable<ErrorRecord> GetErrorVariable()
Expand Down
Loading