Skip to content

Update permissive record matcher to allow ignoring api versions at resource type level. #9705

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

Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

using Microsoft.Azure.Commands.ScenarioTest.SqlTests;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using System.Collections.Generic;
using Xunit;
using Xunit.Abstractions;
using RestTestFramework = Microsoft.Rest.ClientRuntime.Azure.TestFramework;
Expand All @@ -32,6 +33,9 @@ protected override void SetupManagementClients(RestTestFramework.MockContext con

public AdvancedDataSecurityManagedInstanceTests(ITestOutputHelper output) : base(output)
{
base.resourceTypesToIgnoreApiVersion = new string[] {
"Microsoft.Sql/managedInstances"
};
}

[Fact]
Expand Down
3 changes: 3 additions & 0 deletions src/Sql/Sql.Test/ScenarioTests/DataClassificationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public class DataClassificationTests : SqlTestsBase
{
public DataClassificationTests(ITestOutputHelper output) : base(output)
{
base.resourceTypesToIgnoreApiVersion = new string[] {
"Microsoft.Sql/managedInstances"
};
}

protected override void SetupManagementClients(RestTestFramework.MockContext context)
Expand Down
3 changes: 3 additions & 0 deletions src/Sql/Sql.Test/ScenarioTests/ManagedDatabaseBackupTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ protected override void SetupManagementClients(RestTestFramework.MockContext con

public ManagedDatabaseBackupTests(ITestOutputHelper output) : base(output)
{
base.resourceTypesToIgnoreApiVersion = new string[] {
"Microsoft.Sql/managedInstances"
};
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ protected override void SetupManagementClients(RestTestFramework.MockContext con

public ManagedDatabaseCrudScenarioTests(ITestOutputHelper output) : base(output)
{
base.resourceTypesToIgnoreApiVersion = new string[] {
"Microsoft.Sql/managedInstances"
};
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ protected override void SetupManagementClients(RestTestFramework.MockContext con

public ManagedInstanceCrudScenarioTests(ITestOutputHelper output) : base(output)
{
base.resourceTypesToIgnoreApiVersion = new string[] { "Microsoft.Sql/managedInstances" };
Copy link
Contributor Author

@johnpaulkee johnpaulkee Jul 24, 2019

Choose a reason for hiding this comment

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

This will have new recordings as it's a top level resource test - this is stubbed here to test build.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In https://github.com/Azure/azure-powershell-cmdlet-review-pr/issues/329, I've added some new recordings that verifying server level communication with 2018-06-01-preview against managedInstances works after api version bump.

}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public class ManagedInstanceKeyVaultKeyTests : SqlTestsBase
{
public ManagedInstanceKeyVaultKeyTests(ITestOutputHelper output) : base(output)
{
base.resourceTypesToIgnoreApiVersion = new string[] {
"Microsoft.Sql/managedInstances"
};
}

protected override void SetupManagementClients(RestTestFramework.MockContext context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public class ManagedInstanceProtectorTests : SqlTestsBase
{
public ManagedInstanceProtectorTests(ITestOutputHelper output) : base(output)
{
base.resourceTypesToIgnoreApiVersion = new string[] {
"Microsoft.Sql/managedInstances"
};
}

protected override void SetupManagementClients(RestTestFramework.MockContext context)
Expand Down
7 changes: 4 additions & 3 deletions src/Sql/Sql.Test/ScenarioTests/SqlTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ namespace Microsoft.Azure.Commands.ScenarioTest.SqlTests
public class SqlTestsBase : RMTestBase
{
protected EnvironmentSetupHelper Helper;
protected string[] resourceTypesToIgnoreApiVersion;

protected SqlTestsBase(ITestOutputHelper output)
{
Expand Down Expand Up @@ -69,15 +70,15 @@ protected void RunPowerShellTest(params string[] scripts)
{"Microsoft.Features", null},
{"Microsoft.Authorization", null},
{"Microsoft.Network", null},
{"Microsoft.KeyVault", null}

{"Microsoft.KeyVault", null},
};

var providersToIgnore = new Dictionary<string, string>
{
{"Microsoft.Azure.Graph.RBAC.Version1_6.GraphRbacManagementClient", "1.42-previewInternal"},
{"Microsoft.Azure.Management.Resources.ResourceManagementClient", "2016-02-01"}
};
HttpMockServer.Matcher = new PermissiveRecordMatcherWithApiExclusion(true, d, providersToIgnore);
HttpMockServer.Matcher = new PermissiveRecordMatcherWithApiExclusion(true, d, providersToIgnore, resourceTypesToIgnoreApiVersion);
HttpMockServer.RecordsDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SessionRecords");

// Enable undo functionality as well as mock recording
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ protected override void SetupManagementClients(RestTestFramework.MockContext con

public VirtualClusterCrudScenarioTests(ITestOutputHelper output) : base(output)
{
base.resourceTypesToIgnoreApiVersion = new string[] {
"Microsoft.Sql/managedInstances"
};
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ protected override void SetupManagementClients(RestTestFramework.MockContext con
var newResourcesClient = GetResourcesClient(context);
var networkClient = GetNetworkClient(context);
Helper.SetupSomeOfManagementClients(sqlClient, storageV2Client, newResourcesClient, networkClient);

}

public VulnerabilityAssessmentMiTests(ITestOutputHelper output) : base(output)
{
base.resourceTypesToIgnoreApiVersion = new string[] {
"Microsoft.Sql/managedInstances"
};
}

#region Managed Instance Policy Tests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"Entries": [
{
"RequestUri": "/subscriptions/a8c9a924-06c0-4bde-9788-e7b1370969e1/resourceGroups/georestore-rg/providers/Microsoft.Sql/managedInstances/testinstance/recoverableDatabases/sourcedb?api-version=2017-10-01-preview",
"RequestUri": "/subscriptions/a8c9a924-06c0-4bde-9788-e7b1370969e1/resourceGroups/restore-rg/providers/Microsoft.Sql/managedInstances/testbrinstance/recoverableDatabases/sourcedb?api-version=2017-10-01-preview",
"EncodedRequestUri": "L3N1YnNjcmlwdGlvbnMvYThjOWE5MjQtMDZjMC00YmRlLTk3ODgtZTdiMTM3MDk2OWUxL3Jlc291cmNlR3JvdXBzL3Jlc3RvcmUtcmcvcHJvdmlkZXJzL01pY3Jvc29mdC5TcWwvbWFuYWdlZEluc3RhbmNlcy90ZXN0YnJpbnN0YW5jZS9yZWNvdmVyYWJsZURhdGFiYXNlcy9zb3VyY2VkYj9hcGktdmVyc2lvbj0yMDE3LTEwLTAxLXByZXZpZXc=",
"RequestMethod": "GET",
"RequestBody": "",
Expand Down Expand Up @@ -60,11 +60,11 @@
"-1"
]
},
"ResponseBody": "{\r\n \"properties\": {\r\n \"lastAvailableBackupDate\": \"2019-02-20T22:16:05.0539401Z\"\r\n },\r\n \"id\": \"/subscriptions/a8c9a924-06c0-4bde-9788-e7b1370969e1/resourceGroups/georestore-rg/providers/Microsoft.Sql/managedInstances/testinstance/recoverableDatabases/sourcedb\",\r\n \"name\": \"sourcedb\",\r\n \"type\": \"Microsoft.Sql/managedInstances/recoverableDatabases\"\r\n}",
"ResponseBody": "{\r\n \"properties\": {\r\n \"lastAvailableBackupDate\": \"2019-02-20T22:16:05.0539401Z\"\r\n },\r\n \"id\": \"/subscriptions/a8c9a924-06c0-4bde-9788-e7b1370969e1/resourceGroups/restore-rg/providers/Microsoft.Sql/managedInstances/testbrinstance/recoverableDatabases/sourcedb\",\r\n \"name\": \"sourcedb\",\r\n \"type\": \"Microsoft.Sql/managedInstances/recoverableDatabases\"\r\n}",
"StatusCode": 200
},
{
"RequestUri": "/subscriptions/a8c9a924-06c0-4bde-9788-e7b1370969e1/resourceGroups/georestore-rg/providers/Microsoft.Sql/managedInstances/testinstance/recoverableDatabases?api-version=2017-10-01-preview",
"RequestUri": "/subscriptions/a8c9a924-06c0-4bde-9788-e7b1370969e1/resourceGroups/restore-rg/providers/Microsoft.Sql/managedInstances/testbrinstance/recoverableDatabases?api-version=2017-10-01-preview",
"EncodedRequestUri": "L3N1YnNjcmlwdGlvbnMvYThjOWE5MjQtMDZjMC00YmRlLTk3ODgtZTdiMTM3MDk2OWUxL3Jlc291cmNlR3JvdXBzL3Jlc3RvcmUtcmcvcHJvdmlkZXJzL01pY3Jvc29mdC5TcWwvbWFuYWdlZEluc3RhbmNlcy90ZXN0YnJpbnN0YW5jZS9yZWNvdmVyYWJsZURhdGFiYXNlcz9hcGktdmVyc2lvbj0yMDE3LTEwLTAxLXByZXZpZXc=",
"RequestMethod": "GET",
"RequestBody": "",
Expand Down Expand Up @@ -123,7 +123,7 @@
"-1"
]
},
"ResponseBody": "{\r\n \"value\": [\r\n {\r\n \"properties\": {\r\n \"lastAvailableBackupDate\": \"2019-02-20T22:16:05.4133047Z\"\r\n },\r\n \"id\": \"/subscriptions/a8c9a924-06c0-4bde-9788-e7b1370969e1/resourceGroups/georestore-rg/providers/Microsoft.Sql/managedInstances/testinstance/recoverableDatabases/testrecovered1\",\r\n \"name\": \"testrecovered1\",\r\n \"type\": \"Microsoft.Sql/managedInstances/recoverableDatabases\"\r\n },\r\n {\r\n \"properties\": {\r\n \"lastAvailableBackupDate\": \"2019-02-20T22:16:05.4133047Z\"\r\n },\r\n \"id\": \"/subscriptions/a8c9a924-06c0-4bde-9788-e7b1370969e1/resourceGroups/georestore-rg/providers/Microsoft.Sql/managedInstances/testinstance/recoverableDatabases/sourcedb\",\r\n \"name\": \"sourcedb\",\r\n \"type\": \"Microsoft.Sql/managedInstances/recoverableDatabases\"\r\n },\r\n {\r\n \"properties\": {\r\n \"lastAvailableBackupDate\": \"2019-02-20T22:16:05.4133047Z\"\r\n },\r\n \"id\": \"/subscriptions/a8c9a924-06c0-4bde-9788-e7b1370969e1/resourceGroups/georestore-rg/providers/Microsoft.Sql/managedInstances/testinstance/recoverableDatabases/testbrdb\",\r\n \"name\": \"testbrdb\",\r\n \"type\": \"Microsoft.Sql/managedInstances/recoverableDatabases\"\r\n }\r\n ]\r\n}",
"ResponseBody": "{\r\n \"value\": [\r\n {\r\n \"properties\": {\r\n \"lastAvailableBackupDate\": \"2019-02-20T22:16:05.4133047Z\"\r\n },\r\n \"id\": \"/subscriptions/a8c9a924-06c0-4bde-9788-e7b1370969e1/resourceGroups/restore-rg/providers/Microsoft.Sql/managedInstances/testbrinstance/recoverableDatabases/testrecovered1\",\r\n \"name\": \"testrecovered1\",\r\n \"type\": \"Microsoft.Sql/managedInstances/recoverableDatabases\"\r\n },\r\n {\r\n \"properties\": {\r\n \"lastAvailableBackupDate\": \"2019-02-20T22:16:05.4133047Z\"\r\n },\r\n \"id\": \"/subscriptions/a8c9a924-06c0-4bde-9788-e7b1370969e1/resourceGroups/restore-rg/providers/Microsoft.Sql/managedInstances/testbrinstance/recoverableDatabases/sourcedb\",\r\n \"name\": \"sourcedb\",\r\n \"type\": \"Microsoft.Sql/managedInstances/recoverableDatabases\"\r\n },\r\n {\r\n \"properties\": {\r\n \"lastAvailableBackupDate\": \"2019-02-20T22:16:05.4133047Z\"\r\n },\r\n \"id\": \"/subscriptions/a8c9a924-06c0-4bde-9788-e7b1370969e1/resourceGroups/restore-rg/providers/Microsoft.Sql/managedInstances/testbrinstance/recoverableDatabases/testbrdb\",\r\n \"name\": \"testbrdb\",\r\n \"type\": \"Microsoft.Sql/managedInstances/recoverableDatabases\"\r\n }\r\n ]\r\n}",
"StatusCode": 200
}
],
Expand Down
4 changes: 2 additions & 2 deletions src/Sql/Sql.Test/Sql.Test.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<PsModuleName>Sql</PsModuleName>
Expand All @@ -19,7 +19,7 @@
<PackageReference Include="Microsoft.Azure.KeyVault.WebKey" Version="3.0.1" />
<PackageReference Include="Microsoft.Azure.Management.KeyVault" Version="2.4.2" />
<PackageReference Include="Microsoft.Azure.Management.OperationalInsights" Version="0.19.0-preview" />
<PackageReference Include="Microsoft.Azure.Management.Sql" Version="1.31.0-preview" />
<PackageReference Include="Microsoft.Azure.Management.Sql" Version="1.33.0-preview" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;

namespace Microsoft.Azure.Commands.Sql.Test.UnitTests
{
public class PermissiveRecordMatcherUnitTests
{
[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void PermissiveRecordMatcherWithApiExclusion_ContainsIgnoredProvider()
{
var testRequestUris = new string[]
{
"/subscriptions/a8c9a924-06c0-4bde-9788-e7b1370969e1/providers/Microsoft.Sql?api-version=2016-09-01",
"/subscriptions/a8c9a924-06c0-4bde-9788-e7b1370969e1/resourcegroups/ps8625?api-version=2016-09-01",
"/subscriptions/a8c9a924-06c0-4bde-9788-e7b1370969e1/providers/Microsoft.Sql/virtualClusters?api-version=2015-05-01-preview",
"/subscriptions/a8c9a924-06c0-4bde-9788-e7b1370969e1/resourceGroups/ps8625/providers/Microsoft.Network/virtualNetworks/cl_initial?api-version=2018-12-01",
"/subscriptions/a8c9a924-06c0-4bde-9788-e7b1370969e1/resourceGroups/ps8625/providers/Microsoft.Sql/managedInstances/ps8807?api-version=2015-05-01-preview",
"/subscriptions/a8c9a924-06c0-4bde-9788-e7b1370969e1/resourceGroups/ps8625/providers/Microsoft.Sql/servers/ps8807?api-version=2015-05-01-preview",
"/subscriptions/a8c9a924-06c0-4bde-9788-e7b1370969e1/resourceGroups/ps8625/providers/Microsoft.Sql/virtualClusters?api-version=2015-05-01-preview",
"/subscriptions/a8c9a924-06c0-4bde-9788-e7b1370969e1/resourceGroups/ps8625/providers/Microsoft.Sql/managedInstances/ps8807/managedDatabases/db1?api-version=2017-03-01-preview",
"/subscriptions/a8c9a924-06c0-4bde-9788-e7b1370969e1/resourceGroups/ps8625/providers/Microsoft.Sql/servers/ps8807/databases/testdb?api-version=2017-03-01-preview",
"/subscriptions/4cac86b0-1e56-48c2-9df2-669a6d2d87c5/providers/Microsoft.Sql/managedInstances",
"/subscriptions/4cac86b0-1e56-48c2-9df2-669a6d2d87c5/resourceGroups/ps8625/providers/Microsoft.Sql/managedInstances"
};

TestContainsIgnoredProvider(
requestUrisToTest: testRequestUris,
resourcesToIgnore: null,
expectedNumIgnored: 0);

TestContainsIgnoredProvider(
requestUrisToTest: testRequestUris,
resourcesToIgnore: new string[1]
{
"Microsoft.Sql/managedInstances",
},
expectedNumIgnored: 3);

TestContainsIgnoredProvider(
requestUrisToTest: testRequestUris,
resourcesToIgnore: new string[1]
{
"Microsoft.Sql/managedInstances/managedDatabases"
},
expectedNumIgnored: 1);
}

private void TestContainsIgnoredProvider(
IEnumerable<string> requestUrisToTest,
string[] resourcesToIgnore,
int expectedNumIgnored)
{
var numIgnored = 0;
foreach (var testUri in requestUrisToTest)
{
var result = PermissiveRecordMatcherWithApiExclusion.ContainsIgnoredProvider(
requestUri: testUri,
shouldIgnoreGenericResource: false,
providersToIgnore: new Dictionary<string, string>(),
resourcesToIgnore: resourcesToIgnore,
apiVersion: out var apiVersion);

Assert.Equal(String.Empty, apiVersion);
if (result) numIgnored++;
}
Assert.Equal(expectedNumIgnored, numIgnored);
}
}
}
6 changes: 3 additions & 3 deletions src/Sql/Sql/Sql.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<PsModuleName>Sql</PsModuleName>
Expand All @@ -21,10 +21,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Management.Sql" Version="1.31.0-preview" />
<PackageReference Include="Microsoft.Azure.Management.Sql" Version="1.33.0-preview" />
<PackageReference Include="System.Security.Permissions" Version="4.5.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Sql.LegacySdk\Sql.LegacySdk.csproj" />
</ItemGroup>
Expand Down
Loading