Skip to content

Commit

Permalink
Updating Virtual Network Rules tests with automatic vnet creation (#3488
Browse files Browse the repository at this point in the history
)

* Updating Virtual Network Rules tests with automatic vnet creation

* Addressing comments to make the code more readable.
  • Loading branch information
dnayantara authored and shahabhijeet committed Jul 18, 2017
1 parent d3cc5d0 commit 3cd3bc6
Show file tree
Hide file tree
Showing 7 changed files with 1,408 additions and 414 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/SDKs/SqlManagement/Sql.Tests/Sql.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Azure.KeyVault" Version="2.0.6" />
<PackageReference Include="Microsoft.Azure.Management.KeyVault" Version="2.2.0-preview" />
<PackageReference Include="Microsoft.Azure.Management.Network" Version="11.1.0-preview" />
<PackageReference Include="Microsoft.Azure.Management.RecoveryServices" Version="4.2.0-preview" />
<PackageReference Include="Microsoft.Azure.Management.RecoveryServices.Backup" Version="1.2.0-preview" />
<PackageReference Include="Microsoft.Azure.Management.ResourceManager" Version="1.6.0-preview" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public ResourceGroup CreateResourceGroup(string location = SqlManagementTestUtil
rgName,
new ResourceGroup
{
Location = SqlManagementTestUtilities.DefaultLocation,
Location = location,
Tags = new Dictionary<string, string>() { { rgName, DateTime.UtcNow.ToString("u") } }
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public class SqlManagementTestUtilities

public const string DefaultEuapPrimaryLocation = "East US 2 EUAP";

public const string DefaultEuapPrimaryLocationId = "eastus2euap";

public const string DefaultLogin = "dummylogin";

public const string DefaultPassword = "Un53cuRE!";
Expand Down
171 changes: 130 additions & 41 deletions src/SDKs/SqlManagement/Sql.Tests/VirtualNetworkRulesCrudScenarioTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using Microsoft.Azure.Management.Resources;
using Microsoft.Azure.Management.Network;
using Microsoft.Azure.Management.Network.Models;
using Microsoft.Azure.Management.ResourceManager;
using Microsoft.Azure.Management.ResourceManager.Models;
using Microsoft.Azure.Management.Sql;
using Microsoft.Azure.Management.Sql.Models;
using System;
Expand All @@ -16,24 +19,39 @@ public class VirtualNetworkRulesCrudScenarioTests
[Fact]
public void TestGetAndListVirtualNetworkRule()
{
// TODO Update SQL VirtualNetworkRules test to be fully automated
// TODO https://github.com/Azure/azure-sdk-for-net/issues/3388

string testPrefix = "virtualnetworkrulescrudtest-";
string suiteName = this.GetType().FullName;
SqlManagementTestUtilities.RunTestInNewV12Server(suiteName, "TestGetAndListVirtualNetworkRule", testPrefix, (resClient, sqlClient, resourceGroup, server) =>

using (SqlManagementTestContext context = new SqlManagementTestContext(this))
{
// Create Firewall Rules
//
// Basic setup
var location = SqlManagementTestUtilities.DefaultEuapPrimaryLocationId;
ResourceGroup resourceGroup = context.CreateResourceGroup(location);
VirtualNetwork getVnetResponse = CreateVirtualNetwork(context, resourceGroup, subnetCount: 2, location: location);

Server server = context.CreateServer(resourceGroup, location);
SqlManagementClient sqlClient = context.GetClient<SqlManagementClient>();

// create virtual network rules
Dictionary<string, VirtualNetworkRule> rules = new Dictionary<string, VirtualNetworkRule>();

string vnetfirewallRuleName = SqlManagementTestUtilities.GenerateName(testPrefix);
VirtualNetworkRule rule = new VirtualNetworkRule()
//rule 1
string vnetfirewallRuleName1 = SqlManagementTestUtilities.GenerateName(testPrefix);

VirtualNetworkRule rule1 = new VirtualNetworkRule()
{
VirtualNetworkSubnetId = getVnetResponse.Subnets[0].Id.ToString()
};
sqlClient.VirtualNetworkRules.CreateOrUpdate(resourceGroup.Name, server.Name, vnetfirewallRuleName1, rule1);
rules.Add(vnetfirewallRuleName1, rule1);

//rule 2
string vnetfirewallRuleName2 = SqlManagementTestUtilities.GenerateName(testPrefix);
VirtualNetworkRule rule2 = new VirtualNetworkRule()
{
VirtualNetworkSubnetId = string.Format("/subscriptions/d513e2e9-97db-40f6-8d1a-ab3b340cc81a/resourceGroups/TestVnetSdk/providers/Microsoft.Network/virtualNetworks/vnetSdkTest/subnets/subnet1")
VirtualNetworkSubnetId = getVnetResponse.Subnets[1].Id.ToString()
};
sqlClient.VirtualNetworkRules.CreateOrUpdate(resourceGroup.Name, server.Name, vnetfirewallRuleName, rule);
rules.Add(vnetfirewallRuleName, rule);
sqlClient.VirtualNetworkRules.CreateOrUpdate(resourceGroup.Name, server.Name, vnetfirewallRuleName2, rule2);
rules.Add(vnetfirewallRuleName2, rule2);

foreach (var rul in rules)
{
Expand All @@ -53,63 +71,134 @@ public void TestGetAndListVirtualNetworkRule()
{
SqlManagementTestUtilities.ValidateVirtualNetworkRule(rul.Value, listResponse.Single(r => r.Name == rul.Key), rul.Key);
}
}, SqlManagementTestUtilities.DefaultEuapPrimaryLocation);

}
}

[Fact]
public void TestCreateAndDropVirtualNetworkRule()
{
// TODO Update SQL VirtualNetworkRules test to be fully automated
// TODO https://github.com/Azure/azure-sdk-for-net/issues/3388

string testPrefix = "virtualnetworkrulecrudtest-";
string suiteName = this.GetType().FullName;
SqlManagementTestUtilities.RunTestInNewV12Server(suiteName, "TestCreateAndDropVirtualNetworkRule", testPrefix, (resClient, sqlClient, resourceGroup, server) =>

using (SqlManagementTestContext context = new SqlManagementTestContext(this))
{
// Create and validate Firewall Rule
//
// Basic setup
var location = SqlManagementTestUtilities.DefaultEuapPrimaryLocationId;
ResourceGroup resourceGroup = context.CreateResourceGroup(location);
VirtualNetwork getVnetResponse = CreateVirtualNetwork(context, resourceGroup, subnetCount: 1, location: location);

Server server = context.CreateServer(resourceGroup, location);
SqlManagementClient sqlClient = context.GetClient<SqlManagementClient>();

// create virtual network rules
string vnetfirewallRuleName = SqlManagementTestUtilities.GenerateName(testPrefix);

VirtualNetworkRule toCreate = new VirtualNetworkRule()
VirtualNetworkRule rule = new VirtualNetworkRule()
{
VirtualNetworkSubnetId = string.Format("/subscriptions/d513e2e9-97db-40f6-8d1a-ab3b340cc81a/resourceGroups/TestVnetSdk/providers/Microsoft.Network/virtualNetworks/vnetSdkTest/subnets/subnet1")
VirtualNetworkSubnetId = getVnetResponse.Subnets[0].Id.ToString()
};
var vfr1 = sqlClient.VirtualNetworkRules.CreateOrUpdate(resourceGroup.Name, server.Name, vnetfirewallRuleName, toCreate);
SqlManagementTestUtilities.ValidateVirtualNetworkRule(toCreate, vfr1, vnetfirewallRuleName);
VirtualNetworkRule vfr = sqlClient.VirtualNetworkRules.CreateOrUpdate(resourceGroup.Name, server.Name, vnetfirewallRuleName, rule);
SqlManagementTestUtilities.ValidateVirtualNetworkRule(rule, vfr, vnetfirewallRuleName);

sqlClient.VirtualNetworkRules.Delete(resourceGroup.Name, server.Name, vfr1.Name);
}, SqlManagementTestUtilities.DefaultEuapPrimaryLocation);
// delete virtual network rules
sqlClient.VirtualNetworkRules.Delete(resourceGroup.Name, server.Name, vfr.Name);
}
}

[Fact]
public void TestCreateAndUpdateVirtualNetworkRule()
{
// TODO Update SQL VirtualNetworkRules test to be fully automated
// TODO https://github.com/Azure/azure-sdk-for-net/issues/3388

string testPrefix = "virtualnetworkrulecrudtest-";
string suiteName = this.GetType().FullName;
SqlManagementTestUtilities.RunTestInNewV12Server(suiteName, "TestCreateAndUpdateVirtualNetworkRule", testPrefix, (resClient, sqlClient, resourceGroup, server) =>

using (SqlManagementTestContext context = new SqlManagementTestContext(this))
{
// Create Firewall Rule and Validate
//
// Basic setup
var location = SqlManagementTestUtilities.DefaultEuapPrimaryLocationId;
ResourceGroup resourceGroup = context.CreateResourceGroup(location);
VirtualNetwork getVnetResponse = CreateVirtualNetwork(context, resourceGroup, subnetCount: 2, location: location);

Server server = context.CreateServer(resourceGroup, location);
SqlManagementClient sqlClient = context.GetClient<SqlManagementClient>();

// create virtual network rules
string vnetfirewallRuleName = SqlManagementTestUtilities.GenerateName(testPrefix);

VirtualNetworkRule toCreate = new VirtualNetworkRule()
VirtualNetworkRule rule = new VirtualNetworkRule()
{
VirtualNetworkSubnetId = string.Format("/subscriptions/d513e2e9-97db-40f6-8d1a-ab3b340cc81a/resourceGroups/TestVnetSdk/providers/Microsoft.Network/virtualNetworks/vnetSdkTest/subnets/subnet1")
VirtualNetworkSubnetId = getVnetResponse.Subnets[0].Id.ToString()
};
var vfr1 = sqlClient.VirtualNetworkRules.CreateOrUpdate(resourceGroup.Name, server.Name, vnetfirewallRuleName, toCreate);
SqlManagementTestUtilities.ValidateVirtualNetworkRule(toCreate, vfr1, vnetfirewallRuleName);
VirtualNetworkRule vfr = sqlClient.VirtualNetworkRules.CreateOrUpdate(resourceGroup.Name, server.Name, vnetfirewallRuleName, rule);
SqlManagementTestUtilities.ValidateVirtualNetworkRule(rule, vfr, vnetfirewallRuleName);

// Update Firewall Rule and Validate
toCreate = new VirtualNetworkRule()
rule = new VirtualNetworkRule()
{
VirtualNetworkSubnetId = string.Format("/subscriptions/d513e2e9-97db-40f6-8d1a-ab3b340cc81a/resourceGroups/TestVnetSdk/providers/Microsoft.Network/virtualNetworks/vnetSdkTest/subnets/subnet1")
VirtualNetworkSubnetId = getVnetResponse.Subnets[1].Id.ToString()
};
vfr1 = sqlClient.VirtualNetworkRules.CreateOrUpdate(resourceGroup.Name, server.Name, vnetfirewallRuleName, toCreate);
SqlManagementTestUtilities.ValidateVirtualNetworkRule(toCreate, vfr1, vnetfirewallRuleName);
}, SqlManagementTestUtilities.DefaultEuapPrimaryLocation);
vfr = sqlClient.VirtualNetworkRules.CreateOrUpdate(resourceGroup.Name, server.Name, vnetfirewallRuleName, rule);
SqlManagementTestUtilities.ValidateVirtualNetworkRule(rule, vfr, vnetfirewallRuleName);
}
}

public VirtualNetwork CreateVirtualNetwork(SqlManagementTestContext context, ResourceGroup resourceGroup, int subnetCount = 1, string location = SqlManagementTestUtilities.DefaultLocationId)
{
NetworkManagementClient networkClient = context.GetClient<NetworkManagementClient>();

// Create vnet andinitialize subnets
string vnetName = SqlManagementTestUtilities.GenerateName();

List<PrivateAccessServicePropertiesFormat> SqlPrivateAccess = new List<PrivateAccessServicePropertiesFormat>()
{
new PrivateAccessServicePropertiesFormat("Microsoft.Sql")
};

List<Subnet> subnetList = new List<Subnet>();
for (int i = 0; i<subnetCount; i++)
{
string subnetName = SqlManagementTestUtilities.GenerateName();
String addressPrefix = "10.0." + (i + 1) + ".0/24";
Subnet subnet = new Subnet()
{
Name = subnetName,
AddressPrefix = addressPrefix,
PrivateAccessServices = SqlPrivateAccess,
};
subnetList.Add(subnet);
}

var vnet = new VirtualNetwork()
{
Location = location,

AddressSpace = new AddressSpace()
{
AddressPrefixes = new List<string>()
{
"10.0.0.0/16",
}
},
DhcpOptions = new DhcpOptions()
{
DnsServers = new List<string>()
{
"10.1.1.1",
"10.1.2.4"
}
},
Subnets = subnetList
};

// Put Vnet
var putVnetResponse = networkClient.VirtualNetworks.CreateOrUpdate(resourceGroup.Name, vnetName, vnet);
Assert.Equal("Succeeded", putVnetResponse.ProvisioningState);

// Get Vnet
var getVnetResponse = networkClient.VirtualNetworks.Get(resourceGroup.Name, vnetName);

return getVnetResponse;

}
}
}

0 comments on commit 3cd3bc6

Please sign in to comment.