Skip to content

Commit c60c467

Browse files
committed
Adding server context for v12 servers along with mock test framework and some tests.
1 parent dbaa515 commit c60c467

19 files changed

+3329
-97
lines changed

src/ServiceManagement/Sql/Commands.SqlDatabase.Test/Commands.SqlDatabase.Test.csproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
<Reference Include="System.Core">
9191
<RequiredTargetFramework>3.5</RequiredTargetFramework>
9292
</Reference>
93+
<Reference Include="System.Data" />
9394
<Reference Include="System.Data.Services.Client" />
9495
<Reference Include="System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
9596
<SpecificVersion>False</SpecificVersion>
@@ -132,6 +133,18 @@
132133
<Compile Include="UnitTests\Database\Cmdlet\NewAzureSqlPremiumDatabaseTests.cs" />
133134
<Compile Include="UnitTests\Database\Cmdlet\AzureSqlDatabaseCopyCertAuthTests.cs" />
134135
<Compile Include="UnitTests\Database\Cmdlet\RestoreDatabaseTests.cs" />
136+
<Compile Include="UnitTests\Database\Cmdlet\SqlAuthv12MockTests.cs" />
137+
<Compile Include="UnitTests\TSql\MockQueryResult.cs" />
138+
<Compile Include="UnitTests\TSql\MockSettings.cs" />
139+
<Compile Include="UnitTests\TSql\MockSqlCommand.cs">
140+
<SubType>Component</SubType>
141+
</Compile>
142+
<Compile Include="UnitTests\TSql\MockSqlConnection.cs">
143+
<SubType>Component</SubType>
144+
</Compile>
145+
<Compile Include="UnitTests\TSql\MockSqlParameter.cs" />
146+
<Compile Include="UnitTests\TSql\MockSqlParameterCollection.cs" />
147+
<Compile Include="UnitTests\TSql\RecordMockDataResultsAttribute.cs" />
135148
<Compile Include="Utilities\SqlCustomPsHostUserInterface.cs" />
136149
<Compile Include="Utilities\SqlDatabaseSettings.cs" />
137150
<Compile Include="UnitTests\MockServer\AsyncExceptionManager.cs" />
@@ -221,6 +234,9 @@
221234
<SubType>Designer</SubType>
222235
</Content>
223236
<None Include="TestScripts\Database\DeleteDatabase-ScenarioFunctions.ps1" />
237+
<Content Include="Resources\TSqlMockSessions\SqlAuthv12MockTests.xml">
238+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
239+
</Content>
224240
<Content Include="TestScripts\Database\ExpectedFormat.txt" />
225241
<Content Include="TestScripts\Server\ExpectedFormat.txt" />
226242
</ItemGroup>

src/ServiceManagement/Sql/Commands.SqlDatabase.Test/UnitTests/Database/Cmdlet/DatabaseTestHelper.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ public static class DatabaseTestHelper
4646
/// </summary>
4747
public static readonly Guid StandardS1SloGuid = new Guid("1b1ebd4d-d903-4baa-97f9-4ea675f5e928");
4848

49+
/// <summary>
50+
/// The unique GUID for identifying the Standard S0 SLO.
51+
/// </summary>
52+
public static readonly Guid StandardS0SloGuid = new Guid("f1173c43-91bd-4aaa-973c-54e79e15235b");
53+
4954
/// <summary>
5055
/// The unique GUID for identifying the Premium P1 SLO.
5156
/// </summary>

src/ServiceManagement/Sql/Commands.SqlDatabase.Test/UnitTests/Database/Cmdlet/NewAzureSqlDatabaseServerContextTests.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,5 +339,47 @@ public static void CreateServerContextSqlAuth(
339339
contextPsObject.BaseObject is ServerDataServiceSqlAuth,
340340
"Expecting a ServerDataServiceSqlAuth object");
341341
}
342+
343+
/// <summary>
344+
/// Common helper method for other tests to create a context for ESA server.
345+
/// </summary>
346+
/// <param name="contextVariable">The variable name that will hold the new context.</param>
347+
public static void CreateServerContextSqlAuthV2(
348+
System.Management.Automation.PowerShell powershell,
349+
string manageUrl,
350+
string username,
351+
string password,
352+
string contextVariable)
353+
{
354+
UnitTestHelper.ImportAzureModule(powershell);
355+
UnitTestHelper.CreateTestCredential(
356+
powershell,
357+
username,
358+
password);
359+
360+
Collection<PSObject> serverContext;
361+
using (AsyncExceptionManager exceptionManager = new AsyncExceptionManager())
362+
{
363+
serverContext = powershell.InvokeBatchScript(
364+
string.Format(
365+
CultureInfo.InvariantCulture,
366+
@"{1} = New-AzureSqlDatabaseServerContext " +
367+
@"-ManageUrl {0} " +
368+
@"-Credential $credential " +
369+
@"-Version 12.0 ",
370+
manageUrl,
371+
contextVariable),
372+
contextVariable);
373+
}
374+
375+
Assert.AreEqual(0, powershell.Streams.Error.Count, "Errors during run!");
376+
Assert.AreEqual(0, powershell.Streams.Warning.Count, "Warnings during run!");
377+
powershell.Streams.ClearStreams();
378+
379+
PSObject contextPsObject = serverContext.Single();
380+
Assert.IsTrue(
381+
contextPsObject.BaseObject is TSqlConnectionContext,
382+
"Expecting a TSqlConnectionContext object");
383+
}
342384
}
343385
}

src/ServiceManagement/Sql/Commands.SqlDatabase.Test/UnitTests/Database/Cmdlet/NewAzureSqlPremiumDatabaseTests.cs

Lines changed: 71 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void CreatePremiumDatabasesWithSqlAuth()
4848
"$context");
4949
HttpSession testSession = MockServerHelper.DefaultSessionCollection.GetSession(
5050
"UnitTest.Common.CreatePremiumDatabasesWithSqlAuth");
51-
DatabaseTestHelper.SetDefaultTestSessionSettings(testSession);
51+
DatabaseTestHelper.SetDefaultTestSessionSettings(testSession);
5252
testSession.RequestValidator =
5353
new Action<HttpMessage, HttpMessage.Request>(
5454
(expected, actual) =>
@@ -57,69 +57,79 @@ public void CreatePremiumDatabasesWithSqlAuth()
5757
Assert.AreEqual(expected.RequestInfo.UserAgent, actual.UserAgent);
5858
});
5959

60-
using (AsyncExceptionManager exceptionManager = new AsyncExceptionManager())
60+
TestCreatePremiumDatabase(powershell, testSession);
61+
}
62+
}
63+
64+
/// <summary>
65+
/// Helper function to create premium database in the powershell environment provided.
66+
/// </summary>
67+
/// <param name="powershell">The powershell environment</param>
68+
/// <param name="testSession">The test session</param>
69+
private static void TestCreatePremiumDatabase(System.Management.Automation.PowerShell powershell, HttpSession testSession)
70+
{
71+
using (AsyncExceptionManager exceptionManager = new AsyncExceptionManager())
72+
{
73+
Collection<PSObject> premiumDB_P1, PremiumDB_P2;
74+
using (new MockHttpServer(
75+
exceptionManager,
76+
MockHttpServer.DefaultServerPrefixUri,
77+
testSession))
6178
{
62-
Collection<PSObject> premiumDB_P1, PremiumDB_P2;
63-
using (new MockHttpServer(
64-
exceptionManager,
65-
MockHttpServer.DefaultServerPrefixUri,
66-
testSession))
67-
{
68-
powershell.InvokeBatchScript(
69-
@"$P1 = Get-AzureSqlDatabaseServiceObjective" +
70-
@" -Context $context" +
71-
@" -ServiceObjectiveName ""P1""");
72-
73-
powershell.InvokeBatchScript(
74-
@"$P2 = Get-AzureSqlDatabaseServiceObjective " +
75-
@"-Context $context" +
76-
@" -ServiceObjectiveName ""P2""");
77-
78-
premiumDB_P1 = powershell.InvokeBatchScript(
79-
@"$premiumDB_P1 = New-AzureSqlDatabase " +
80-
@"-Context $context " +
81-
@"-DatabaseName NewAzureSqlPremiumDatabaseTests_P1 " +
82-
@"-Edition Premium " +
83-
@"-ServiceObjective $P1 ");
84-
premiumDB_P1 = powershell.InvokeBatchScript("$PremiumDB_P1");
85-
86-
powershell.InvokeBatchScript(
87-
@"$PremiumDB_P2 = New-AzureSqlDatabase " +
88-
@"-Context $context " +
89-
@"-DatabaseName NewAzureSqlPremiumDatabaseTests_P2 " +
90-
@"-Collation Japanese_CI_AS " +
91-
@"-Edition Premium " +
92-
@"-ServiceObjective $P2 " +
93-
@"-MaxSizeGB 10 " +
94-
@"-Force");
95-
PremiumDB_P2 = powershell.InvokeBatchScript("$PremiumDB_P2");
96-
}
79+
powershell.InvokeBatchScript(
80+
@"$P1 = Get-AzureSqlDatabaseServiceObjective" +
81+
@" -Context $context" +
82+
@" -ServiceObjectiveName ""P1""");
9783

98-
Assert.AreEqual(0, powershell.Streams.Error.Count, "Errors during run!");
99-
Assert.AreEqual(0, powershell.Streams.Warning.Count, "Warnings during run!");
100-
powershell.Streams.ClearStreams();
101-
102-
Assert.IsTrue(
103-
premiumDB_P1.Single().BaseObject is Services.Server.Database,
104-
"Expecting a Database object");
105-
Services.Server.Database databaseP1 =
106-
(Services.Server.Database)premiumDB_P1.Single().BaseObject;
107-
Assert.AreEqual("NewAzureSqlPremiumDatabaseTests_P1", databaseP1.Name, "Expected db name to be NewAzureSqlPremiumDatabaseTests_P1");
108-
109-
Assert.IsTrue(
110-
PremiumDB_P2.Single().BaseObject is Services.Server.Database,
111-
"Expecting a Database object");
112-
Services.Server.Database databaseP2 =
113-
(Services.Server.Database)PremiumDB_P2.Single().BaseObject;
114-
Assert.AreEqual("NewAzureSqlPremiumDatabaseTests_P2", databaseP2.Name, "Expected db name to be NewAzureSqlPremiumDatabaseTests_P2");
115-
116-
Assert.AreEqual(
117-
"Japanese_CI_AS",
118-
databaseP2.CollationName,
119-
"Expected collation to be Japanese_CI_AS");
120-
Assert.AreEqual("Premium", databaseP2.Edition, "Expected edition to be Premium");
121-
Assert.AreEqual(10, databaseP2.MaxSizeGB, "Expected max size to be 10 GB");
84+
powershell.InvokeBatchScript(
85+
@"$P2 = Get-AzureSqlDatabaseServiceObjective " +
86+
@"-Context $context" +
87+
@" -ServiceObjectiveName ""P2""");
88+
89+
premiumDB_P1 = powershell.InvokeBatchScript(
90+
@"$premiumDB_P1 = New-AzureSqlDatabase " +
91+
@"-Context $context " +
92+
@"-DatabaseName NewAzureSqlPremiumDatabaseTests_P1 " +
93+
@"-Edition Premium " +
94+
@"-ServiceObjective $P1 ");
95+
premiumDB_P1 = powershell.InvokeBatchScript("$PremiumDB_P1");
96+
97+
powershell.InvokeBatchScript(
98+
@"$PremiumDB_P2 = New-AzureSqlDatabase " +
99+
@"-Context $context " +
100+
@"-DatabaseName NewAzureSqlPremiumDatabaseTests_P2 " +
101+
@"-Collation Japanese_CI_AS " +
102+
@"-Edition Premium " +
103+
@"-ServiceObjective $P2 " +
104+
@"-MaxSizeGB 10 " +
105+
@"-Force");
106+
PremiumDB_P2 = powershell.InvokeBatchScript("$PremiumDB_P2");
122107
}
108+
109+
Assert.AreEqual(0, powershell.Streams.Error.Count, "Errors during run!");
110+
Assert.AreEqual(0, powershell.Streams.Warning.Count, "Warnings during run!");
111+
powershell.Streams.ClearStreams();
112+
113+
Assert.IsTrue(
114+
premiumDB_P1.Single().BaseObject is Services.Server.Database,
115+
"Expecting a Database object");
116+
Services.Server.Database databaseP1 =
117+
(Services.Server.Database)premiumDB_P1.Single().BaseObject;
118+
Assert.AreEqual("NewAzureSqlPremiumDatabaseTests_P1", databaseP1.Name, "Expected db name to be NewAzureSqlPremiumDatabaseTests_P1");
119+
120+
Assert.IsTrue(
121+
PremiumDB_P2.Single().BaseObject is Services.Server.Database,
122+
"Expecting a Database object");
123+
Services.Server.Database databaseP2 =
124+
(Services.Server.Database)PremiumDB_P2.Single().BaseObject;
125+
Assert.AreEqual("NewAzureSqlPremiumDatabaseTests_P2", databaseP2.Name, "Expected db name to be NewAzureSqlPremiumDatabaseTests_P2");
126+
127+
Assert.AreEqual(
128+
"Japanese_CI_AS",
129+
databaseP2.CollationName,
130+
"Expected collation to be Japanese_CI_AS");
131+
Assert.AreEqual("Premium", databaseP2.Edition, "Expected edition to be Premium");
132+
Assert.AreEqual(10, databaseP2.MaxSizeGB, "Expected max size to be 10 GB");
123133
}
124134
}
125135

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using Microsoft.WindowsAzure.Commands.SqlDatabase.Services.Server;
3+
using Microsoft.WindowsAzure.Commands.SqlDatabase.Test.UnitTests.TSql;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Collections.ObjectModel;
7+
using System.Linq;
8+
using System.Management.Automation;
9+
using System.Text;
10+
using System.Threading.Tasks;
11+
12+
namespace Microsoft.WindowsAzure.Commands.SqlDatabase.Test.UnitTests.Database.Cmdlet
13+
{
14+
[RecordMockDataResults("./")]
15+
[TestClass]
16+
public class SqlAuthv12MockTests
17+
{
18+
public static string username = "testlogin";
19+
public static string password = "MyS3curePa$$w0rd";
20+
public static string manageUrl = "https://mysvr2.adamkr-vm04.onebox.xdb.mscds.com";
21+
22+
[TestInitialize]
23+
public void Setup()
24+
{
25+
var mockConn = new MockSqlConnection();
26+
TSqlConnectionContext.MockSqlConnection = mockConn;
27+
}
28+
29+
[TestCleanup]
30+
public void Cleanup()
31+
{
32+
// Do any test clean up here.
33+
}
34+
35+
[TestMethod]
36+
public void NewAzureSqlDatabaseWithSqlAuthv12()
37+
{
38+
39+
using (System.Management.Automation.PowerShell powershell =
40+
System.Management.Automation.PowerShell.Create())
41+
{
42+
43+
// Create a context
44+
NewAzureSqlDatabaseServerContextTests.CreateServerContextSqlAuthV2(
45+
powershell,
46+
manageUrl,
47+
username,
48+
password,
49+
"$context");
50+
51+
Collection<PSObject> database1, database2, database3, database4;
52+
53+
database1 = powershell.InvokeBatchScript(
54+
@"$testdb1 = New-AzureSqlDatabase " +
55+
@"-Context $context " +
56+
@"-DatabaseName testdb1 " +
57+
@"-Force",
58+
@"$testdb1");
59+
database2 = powershell.InvokeBatchScript(
60+
@"$testdb2 = New-AzureSqlDatabase " +
61+
@"-Context $context " +
62+
@"-DatabaseName testdb2 " +
63+
@"-Collation Japanese_CI_AS " +
64+
@"-Edition Basic " +
65+
@"-MaxSizeGB 2 " +
66+
@"-Force",
67+
@"$testdb2");
68+
database3 = powershell.InvokeBatchScript(
69+
@"$testdb3 = New-AzureSqlDatabase " +
70+
@"-Context $context " +
71+
@"-DatabaseName testdb3 " +
72+
@"-MaxSizeBytes 107374182400 " +
73+
@"-Force",
74+
@"$testdb3");
75+
var slo = powershell.InvokeBatchScript(
76+
@"$so = Get-AzureSqlDatabaseServiceObjective " +
77+
@"-Context $context " +
78+
@"-ServiceObjectiveName S2 ",
79+
@"$so");
80+
database4 = powershell.InvokeBatchScript(
81+
@"$testdb4 = New-AzureSqlDatabase " +
82+
@"-Context $context " +
83+
@"-DatabaseName testdb4 " +
84+
@"-Edition Standard " +
85+
@"-ServiceObjective $so " +
86+
@"-Force",
87+
@"$testdb4");
88+
89+
Assert.AreEqual(0, powershell.Streams.Error.Count, "Errors during run!");
90+
Assert.AreEqual(0, powershell.Streams.Warning.Count, "Warnings during run!");
91+
powershell.Streams.ClearStreams();
92+
93+
Services.Server.Database database = database1.Single().BaseObject as Services.Server.Database;
94+
Assert.IsTrue(database != null, "Expecting a Database object");
95+
ValidateDatabaseProperties(database, "testdb1", "Standard", 250, 268435456000L, "SQL_Latin1_General_CP1_CI_AS", false, DatabaseTestHelper.StandardS0SloGuid);
96+
97+
database = database2.Single().BaseObject as Services.Server.Database;
98+
Assert.IsTrue(database != null, "Expecting a Database object");
99+
ValidateDatabaseProperties(database, "testdb2", "Basic", 2, 2147483648L, "Japanese_CI_AS", false, DatabaseTestHelper.BasicSloGuid);
100+
101+
database = database3.Single().BaseObject as Services.Server.Database;
102+
Assert.IsTrue(database != null, "Expecting a Database object");
103+
ValidateDatabaseProperties(database, "testdb3", "Standard", 100, 107374182400L, "SQL_Latin1_General_CP1_CI_AS", false, DatabaseTestHelper.StandardS0SloGuid);
104+
105+
database = database4.Single().BaseObject as Services.Server.Database;
106+
Assert.IsTrue(database != null, "Expecting a Database object");
107+
ValidateDatabaseProperties(database, "testdb4", "Standard", 250, 268435456000L, "SQL_Latin1_General_CP1_CI_AS", false, DatabaseTestHelper.StandardS2SloGuid);
108+
}
109+
}
110+
111+
112+
/// <summary>
113+
/// Validate the properties of a database against the expected values supplied as input.
114+
/// </summary>
115+
/// <param name="database">The database object to validate</param>
116+
/// <param name="name">The expected name of the database</param>
117+
/// <param name="edition">The expected edition of the database</param>
118+
/// <param name="maxSizeGb">The expected max size of the database in GB</param>
119+
/// <param name="collation">The expected Collation of the database</param>
120+
/// <param name="isSystem">Whether or not the database is expected to be a system object.</param>
121+
internal static void ValidateDatabaseProperties(
122+
Services.Server.Database database,
123+
string name,
124+
string edition,
125+
int maxSizeGb,
126+
long maxSizeBytes,
127+
string collation,
128+
bool isSystem,
129+
Guid slo)
130+
{
131+
Assert.AreEqual(name, database.Name);
132+
Assert.AreEqual(edition, database.Edition);
133+
Assert.AreEqual(maxSizeGb, database.MaxSizeGB);
134+
Assert.AreEqual(maxSizeBytes, database.MaxSizeBytes);
135+
Assert.AreEqual(collation, database.CollationName);
136+
Assert.AreEqual(isSystem, database.IsSystemObject);
137+
// Assert.AreEqual(slo, database.ServiceObjectiveId);
138+
}
139+
}
140+
}

0 commit comments

Comments
 (0)