Skip to content

AzureCP PR - Fix Extension ID Issue #387 #1792

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

Merged
merged 39 commits into from
Feb 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
34b3c5b
Merge pull request #360 from Azure/dev
huangpf Jan 26, 2016
0fe7d69
Merge pull request #361 from Azure/dev
huangpf Jan 27, 2016
fc0027f
Merge pull request #363 from Azure/dev
huangpf Jan 27, 2016
1d42e41
Merge pull request #364 from Azure/dev
huangpf Jan 27, 2016
bf33468
Merge pull request #114 from huangpf/dev
huangpf Jan 27, 2016
d942e42
Merge pull request #365 from Azure/dev
huangpf Jan 29, 2016
bf8744b
Merge pull request #367 from Azure/dev
huangpf Feb 1, 2016
92b92e0
Merge pull request #368 from Azure/dev
huangpf Feb 2, 2016
555090d
Merge pull request #371 from Azure/dev
huangpf Feb 4, 2016
62134ac
Merge pull request #374 from Azure/dev
huangpf Feb 4, 2016
5aa7a89
Adding RunnerTests.cs to run Azure Tests in Record mode
khoatle Feb 4, 2016
6c3386c
Merge pull request #375 from Azure/dev
huangpf Feb 4, 2016
c8a6c1b
Adding Dynamic VM test to RunnerTests.csv file
khoatle Feb 4, 2016
74014ad
Merge pull request #117 from khoatle/dev
AzureRT Feb 5, 2016
f07dd07
Merge pull request #378 from Azure/dev
huangpf Feb 5, 2016
ca8b901
Update WXI
huangpf Feb 5, 2016
e47eed3
Merge pull request #381 from Azure/dev
huangpf Feb 5, 2016
98ac14b
Merge pull request #121 from huangpf/dev
huangpf Feb 5, 2016
95849b2
Merge branch 'dev' of https://github.com/Azure/azure-powershell into dev
huangpf Feb 6, 2016
59f49f1
Update
huangpf Feb 6, 2016
3b94797
Merge pull request #388 from Azure/dev
huangpf Feb 9, 2016
d07b1eb
Fix Ext ID
huangpf Feb 9, 2016
877045e
Merge branch 'dev' of https://github.com/huangpf/azure-powershell int…
huangpf Feb 9, 2016
343836e
Merge branch 'dev' of https://github.com/huangpf/azure-powershell int…
huangpf Feb 9, 2016
39d97c2
Merge pull request #124 from huangpf/dev
huangpf Feb 9, 2016
02c0288
Address CRs
huangpf Feb 9, 2016
31111f4
Merge pull request #393 from Azure/dev
huangpf Feb 9, 2016
8ae38ce
Merge pull request #396 from AzureRT/dev
huangpf Feb 9, 2016
1e67e6c
Merge pull request #125 from huangpf/dev
huangpf Feb 9, 2016
37faca8
Merge pull request #397 from Azure/dev
huangpf Feb 10, 2016
fbee436
Merge pull request #126 from huangpf/dev
huangpf Feb 10, 2016
f99c1fb
Remove tests from RunnerTests.csv to reduce run time
khoatle Feb 10, 2016
3118a0e
Merge pull request #128 from khoatle/dev
huangpf Feb 11, 2016
b7ecf11
Merge pull request #398 from Azure/dev
huangpf Feb 12, 2016
a42e515
Merge pull request #400 from AzureRT/dev
huangpf Feb 12, 2016
99e7994
Merge pull request #402 from Azure/dev
huangpf Feb 12, 2016
579cd16
Merge pull request #405 from Azure/dev
huangpf Feb 13, 2016
c03048b
wxi
huangpf Feb 15, 2016
c19a050
Merge branch 'dev' of https://github.com/Azure/azure-powershell into dev
huangpf Feb 17, 2016
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
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@
<Compile Include="ScenarioTests\ComputeCloudExceptionTests.cs" />
<Compile Include="ScenarioTests\DiagnosticsExtensionTests.cs" />
<Compile Include="ScenarioTests\DscExtensionTests.cs" />
<Compile Include="ScenarioTests\RunnerTests.cs" />
<Compile Include="ScenarioTests\VirtualMachineBootDiagnosticsTests.cs" />
<Compile Include="ScenarioTests\VMDynamicTests.cs" />
<Compile Include="ScenarioTests\VirtualMachineProfileTests.cs" />
Expand Down Expand Up @@ -233,13 +234,7 @@
<None Include="ScenarioTests\DummyConfig.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="ScenarioTests\Generated\VirtualMachineDynamicTest1.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="ScenarioTests\Generated\VirtualMachineDynamicTest2.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="ScenarioTests\Generated\VirtualMachineDynamicTest3.ps1">
<None Include="ScenarioTests\RunnerTests.csv">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="ScenarioTests\VirtualMachineBootDiagnosticsTests.ps1">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
using Xunit;

namespace Microsoft.Azure.Commands.Compute.Test.ScenarioTests
{
public class RunnerTests
{
[Fact]
public void ExecuteRunnerTests()
{
var mode = Environment.GetEnvironmentVariable("AZURE_TEST_MODE");
var csmAuth = Environment.GetEnvironmentVariable("TEST_CSM_ORGID_AUTHENTICATION");

if (mode == null || csmAuth == null || mode.ToLower() != "record")
{
return;
}

Assert.False(string.IsNullOrEmpty(csmAuth));
Assert.True(csmAuth.Contains("AADTenant"));

var envDictionary = TestUtilities.ParseConnectionString(csmAuth);
var testEnv = new TestEnvironment(envDictionary);
Assert.NotNull(testEnv.Tenant);
Assert.NotNull(testEnv.SubscriptionId);
Assert.NotNull(testEnv.ClientId);
Assert.True(envDictionary.ContainsKey("ApplicationSecret"));

var authenticationContext = new AuthenticationContext("https://login.windows.net/" + testEnv.Tenant);
var credential = new ClientCredential(testEnv.ClientId, envDictionary["ApplicationSecret"]);

var result = authenticationContext.AcquireToken("https://management.core.windows.net/", clientCredential: credential);

Assert.NotNull(result.AccessToken);
envDictionary["RawToken"] = result.AccessToken;

FixCSMAuthEnvVariable(envDictionary);

Console.WriteLine(Environment.GetEnvironmentVariable("TEST_CSM_ORGID_AUTHENTICATION"));

var testFile = File.ReadAllLines("ScenarioTests\\RunnerTests.csv");
foreach (var line in testFile)
{
var tokens = line.Split(';');
var className = tokens[0];
var type = Type.GetType(className);
var constructorInfo = type.GetConstructor(Type.EmptyTypes);
for (int i = 1; i < tokens.Length; i++)
{
var method = tokens[i];
var testClassInstance = constructorInfo.Invoke(new object[] {});
var testMethod = type.GetMethod(method);

Console.WriteLine("Invoking method : " + testMethod);

testMethod.Invoke(testClassInstance, new object[] {});

Console.WriteLine("Method " + testMethod + " has finished");
}
}
}

private void FixCSMAuthEnvVariable(IDictionary<string, string> envDictionary)
{
var str = string.Empty;
foreach (var entry in envDictionary)
{
if (entry.Key != "AADClientId" && entry.Key != "ApplicationSecret")
{
str += string.Format("{0}={1};", entry.Key, entry.Value);
}
}

Environment.SetEnvironmentVariable("TEST_CSM_ORGID_AUTHENTICATION", str);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Microsoft.Azure.Commands.Compute.Test.ScenarioTests.VMDynamicTests;RunVMDynamicTests
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;
using System.IO;
using Microsoft.Azure.Test.HttpRecorder;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using Xunit;
Expand All @@ -24,7 +26,7 @@ public partial class VMDynamicTests
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void RunVMDynamicTests()
{
ComputeTestController.NewInstance.RunPsTest("Run-VMDynamicTests");
ComputeTestController.NewInstance.RunPsTest("Run-VMDynamicTests -num_total_generated_tests 1");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@
<Compile Include="Resources\ResourceLocator.cs" />
<Compile Include="Scheduler\SchedulerTests.cs" />
<Compile Include="ServiceBusTests\ServiceBusAuthorizationRuleTests.cs" />
<Compile Include="ServiceManagement\UnitTests.cs" />
<Compile Include="ServiceManagement\ScenarioTests.cs" />
<Compile Include="ServiceManagement\ServiceManagementTests.cs" />
<Compile Include="StorageTests\StorageContainerTest.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// ----------------------------------------------------------------------------------
//
// 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.ServiceManagement.Extensions;
using System;
using Xunit;

namespace Microsoft.WindowsAzure.Commands.ScenarioTest
{
public partial class ServiceManagementTests
{
[Fact]
[Trait(Category.Service, Category.ServiceManagement)]
[Trait(Category.AcceptanceType, Category.CheckIn)]
[Trait(Category.AcceptanceType, Category.BVT)]
public void TestExtensionRoleNames()
{
var roleNames = new string[]
{
"test Role Name",
"!!!!! _____ test Role Name ~~~",
"testRoleName",
" testRoleName",
"testRoleName ",
" testRoleName"
};
var expectedPrefixName = "testRoleName";
var expectedExtensionId = "testRoleName-test-test-Ext-0";
foreach (var roleName in roleNames)
{
ExtensionRole er = new ExtensionRole(roleName);
Assert.Equal(er.RoleName, roleName.Trim());
Assert.Equal(er.PrefixName, expectedPrefixName);
Assert.Equal(er.GetExtensionId("test", "test", 0), expectedExtensionId);
}

var longRoleNames = new string[]
{
"A123456789B123456789C123456789D123456789E123456789F123456789G123456789H123456789",
" A123456789B123456789C123456789D123456789E123456789F123456789G123456789H123456789 ~~~"
};

// PrefixName = A123456789B123456789C123456789D123456789E123456789F123456789G123456789H123456789
// Extension Name = test
// Slot = test
// Index = 0
// Extension ID Format = {prefix_name_part}-{extension_name_part}-{slot}-Ext-{index}
// Extenion ID's Max Length: 60 = 43 + 1 + 4 + 1 + 4 + 1 + 5
// i.e. 'A123...E123' + '-' + 'test' + '-' + 'test' + '-' + 'Ext-0'
// L=43 L=1 L=4 L=1 L=4 L=1 L=5
expectedPrefixName = longRoleNames[0];
expectedExtensionId = "A123456789B123456789C123456789D123456789E123-test-test-Ext-0";
foreach (var roleName in longRoleNames)
{
ExtensionRole er = new ExtensionRole(roleName);
Assert.Equal(er.RoleName, roleName.Trim());
Assert.Equal(er.PrefixName, expectedPrefixName);
Assert.Equal(er.GetExtensionId("test", "test", 0), expectedExtensionId);
}


var longExtensionNames = longRoleNames;
// PrefixName = Default
// Extension Name = A123456789B123456789C123456789D123456789E123456789F123456789G123456789H123456789
// Slot = test
// Index = 1
// Extension ID Format = {prefix_name_part}-{extension_name_part}-{slot}-Ext-{index}
// Extenion ID's Max Length: 60 = 1 + 1 + 47 + 1 + 4 + 1 + 5
// i.e. 'D' + '-' + 'A123...456' + '-' + 'test' + '-' + 'Ext-0'
// L=1 L=1 L=47 L=1 L=4 L=1 L=5
expectedExtensionId = "D-A123456789B123456789C123456789D123456789E123456-test-Ext-1";
foreach (var extensionName in longExtensionNames)
{
ExtensionRole er = new ExtensionRole();
Assert.Equal(er.RoleName, string.Empty);
Assert.Equal(er.PrefixName, "Default");
Assert.Equal(er.GetExtensionId(extensionName, "test", 1), expectedExtensionId);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

using System;
using System.Text;
using System.Text.RegularExpressions;

namespace Microsoft.WindowsAzure.Commands.ServiceManagement.Extensions
{
Expand All @@ -22,12 +23,21 @@ public class ExtensionRole
protected const string DefaultExtensionIdPrefixStr = "Default";
protected const string ExtensionIdSuffixTemplate = "-{0}-{1}-Ext-{2}";
protected const int MaxExtensionIdLength = 60;
protected const int MinRoleNamePartLength = 1;
protected const int MaxSuffixLength = MaxExtensionIdLength - MinRoleNamePartLength;

public string RoleName { get; private set; }
public string PrefixName { get; private set; }
public ExtensionRoleType RoleType { get; private set; }
public bool Default { get; private set; }

private static string RemoveDisallowedCharacters(string roleName)
{
// Remove characters that are not allowed in the extension id
var disallowedCharactersRegex = new Regex(@"[^A-Za-z0-9\-]");
return disallowedCharactersRegex.Replace(roleName, string.Empty);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this method seems too complex for the thing it is doing.
Just put following on line 37 and you should be good:

return disallowedCharactersRegex.Replace(roleName,"");


public ExtensionRole()
{
RoleName = string.Empty;
Expand All @@ -48,9 +58,7 @@ public ExtensionRole(string roleName)
else
{
PrefixName = RoleName = roleName.Trim();
PrefixName = PrefixName.Replace(".", string.Empty);
PrefixName = PrefixName.Replace(" ", string.Empty);
PrefixName = PrefixName.Replace("_", string.Empty);
PrefixName = RemoveDisallowedCharacters(PrefixName);
RoleType = ExtensionRoleType.NamedRoles;
Default = false;
}
Expand All @@ -63,13 +71,21 @@ public override string ToString()

public string GetExtensionId(string extensionName, string slot, int index)
{
var normalizedExtName = extensionName.Replace(".", string.Empty);
normalizedExtName = normalizedExtName.Replace(" ", string.Empty);
normalizedExtName = normalizedExtName.Replace("_", string.Empty);
var normalizedExtName = RemoveDisallowedCharacters(extensionName);

var suffix = new StringBuilder();
// Suffix format: -{extension_name_part}-{slot}-Ext-{index}
suffix.AppendFormat(ExtensionIdSuffixTemplate, normalizedExtName, slot, index);
if (suffix.Length > MaxSuffixLength)
{
// If the suffix is too long, truncate the {extension_name_part}
int lenDiff = suffix.Length - MaxSuffixLength;
int startIndex = 1; // Suffix starts with '-'
suffix.Remove(startIndex + normalizedExtName.Length - lenDiff, lenDiff);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you want to achieve here? string.Remove returns a new string which you do not use anywhere. The original string will remain unchanged.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, please cover this case in the tests as well ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's actually covered, :-)... -- The test case is to validate that a very long extension name will be truncated in the suffix, while the other parts, i.e. slot & number are preserved.

}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this for limiting the length of the string? If so, this should be enough

suffix = siffix.Substring(0, MaxSuffixLength);

right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually no... So the format of the extension ID is {role-name-part} + {suffix}, where {suffix} = -{extension-name-part}-{slot}-Ext-{num} (all from legacy design request).

I want to limit the suffix length, but I can't simply cut it off by MaxSuffixLength, and I have to truncate the {extension-name-part}.


// Calculate the prefix length by the difference between the suffix and the max ID length.
// The difference should always be at least 1.
int prefixSubStrLen = Math.Min(Math.Max(MaxExtensionIdLength - suffix.Length, 0), PrefixName.Length);

var result = new StringBuilder();
Expand Down