public void RecoverAzureSqlDatabaseWithDatabaseName() { var testSession = MockServerHelper.DefaultSessionCollection.GetSession( "UnitTests.RecoverAzureSqlDatabaseWithDatabaseName"); ServerTestHelper.SetDefaultTestSessionSettings(testSession); testSession.RequestValidator = new Action <HttpMessage, HttpMessage.Request>( (expected, actual) => { Assert.AreEqual(expected.RequestInfo.Method, actual.Method); Assert.IsTrue( actual.UserAgent.Contains(ApiConstants.UserAgentHeaderValue), "Missing proper UserAgent string."); }); using (var exceptionManager = new AsyncExceptionManager()) { Collection <PSObject> operation; using (new MockHttpServer( exceptionManager, MockHttpServer.DefaultHttpsServerPrefixUri, testSession)) { operation = powershell.InvokeBatchScript( @"Start-AzureSqlDatabaseRecovery " + @"-SourceServerName $serverName " + @"-SourceDatabaseName testdb1 " + @"-TargetDatabaseName testdb1-restored"); } Assert.AreEqual(0, powershell.Streams.Error.Count, "Errors during run!"); Assert.AreEqual(0, powershell.Streams.Warning.Count, "Warnings during run!"); powershell.Streams.ClearStreams(); // Expecting one operation object Assert.AreEqual(1, operation.Count, "Expecting one operation object"); Assert.IsInstanceOfType( operation[0].BaseObject, typeof(RecoverDatabaseOperation), "Expecting a RecoverDatabaseOperation object"); var operationObject = (RecoverDatabaseOperation)operation[0].BaseObject; Guid operationId; Assert.IsTrue( Guid.TryParse(operationObject.Id, out operationId), "Expecting a operation ID that's a GUID"); Assert.AreNotEqual( Guid.Empty, operationId, "Expecting an operation ID that's not an empty GUID"); Assert.AreEqual( operationObject.SourceDatabaseName, "testdb1", "Source database name mismatch"); Assert.AreEqual( operationObject.TargetServerName, serverName, "Target server name mismatch"); Assert.AreEqual( operationObject.TargetDatabaseName, "testdb1-restored", "Target database name mismatch"); } }
/// <summary> /// Imports the Azure Manifest to the given <paramref name="powershell"/> instance. /// </summary> /// <param name="powershell">An instance of the <see cref="PowerShell"/> object.</param> public static void ImportAzureModule(System.Management.Automation.PowerShell powershell) { // Import the test manifest file powershell.InvokeBatchScript(@"Import-Module ..\..\..\..\Package\Debug\ResourceManager\AzureResourceManager\AzureRM.Profile\AzureRM.Profile.psd1"); powershell.InvokeBatchScript(@"Import-Module ..\..\..\..\Package\Debug\Storage\Azure.Storage\Azure.Storage.psd1"); powershell.InvokeBatchScript(@"Import-Module ..\..\..\..\Package\Debug\ServiceManagement\Azure\Azure.psd1"); Assert.IsTrue(powershell.Streams.Error.Count == 0); }
/// <summary> /// Removes $testdb1 and $testdb2 on the given context. /// </summary> /// <param name="powershell">The powershell instance containing the context.</param> /// <param name="contextVariable">The variable name that holds the server context.</param> public static void RemoveTestDatabasesWithSqlAuth( System.Management.Automation.PowerShell powershell, string contextVariable) { HttpSession testSession = DatabaseTestHelper.DefaultSessionCollection.GetSession( "UnitTest.Common.RemoveTestDatabasesWithSqlAuth"); DatabaseTestHelper.SetDefaultTestSessionSettings(testSession); testSession.RequestValidator = new Action <HttpMessage, HttpMessage.Request>( (expected, actual) => { Assert.AreEqual(expected.RequestInfo.Method, actual.Method); Assert.AreEqual(expected.RequestInfo.UserAgent, actual.UserAgent); switch (expected.Index) { // Request 0-5: Remove database requests case 0: case 1: case 2: case 3: case 4: case 5: DatabaseTestHelper.ValidateHeadersForODataRequest( expected.RequestInfo, actual); break; default: Assert.Fail("No more requests expected."); break; } }); using (AsyncExceptionManager exceptionManager = new AsyncExceptionManager()) { using (new MockHttpServer( exceptionManager, MockHttpServer.DefaultServerPrefixUri, testSession)) { powershell.InvokeBatchScript( @"Remove-AzureSqlDatabase " + @"-Context $context " + @"-DatabaseName testdb1 " + @"-Force"); powershell.InvokeBatchScript( @"Remove-AzureSqlDatabase " + @"-Context $context " + @"-DatabaseName testdb2 " + @"-Force"); } Assert.AreEqual(0, powershell.Streams.Error.Count, "Errors during run!"); Assert.AreEqual(0, powershell.Streams.Warning.Count, "Warnings during run!"); powershell.Streams.ClearStreams(); } }
public void GetAzureSqlDatabaseWithSqlAuthv12() { var mockConn = new MockSqlConnection(); TSqlConnectionContext.MockSqlConnection = mockConn; using (System.Management.Automation.PowerShell powershell = System.Management.Automation.PowerShell.Create()) { // Create a context NewAzureSqlDatabaseServerContextTests.CreateServerContextSqlAuthV12( powershell, manageUrl, username, password, "$context"); Collection <PSObject> database1, database2, database3; database1 = powershell.InvokeBatchScript( @"$testdb1 = Get-AzureSqlDatabase " + @"-Context $context " + @"-DatabaseName testdb1 ", @"$testdb1"); database2 = powershell.InvokeBatchScript( @"$testdb2 = Get-AzureSqlDatabase " + @"-Context $context " + @"-Database $testdb1 ", @"$testdb2"); database3 = powershell.InvokeBatchScript( @"$testdb3 = Get-AzureSqlDatabase " + @"-Context $context ", @"$testdb3"); Assert.AreEqual(0, powershell.Streams.Error.Count, "Errors during run!"); Assert.AreEqual(0, powershell.Streams.Warning.Count, "Warnings during run!"); powershell.Streams.ClearStreams(); Services.Server.Database database = database1.Single().BaseObject as Services.Server.Database; Assert.IsTrue(database != null, "Expecting a Database object"); ValidateDatabaseProperties(database, "testdb1", "Standard", 250, 268435456000L, "SQL_Latin1_General_CP1_CI_AS", false, DatabaseTestHelper.StandardS0SloGuid); database = database2.Single().BaseObject as Services.Server.Database; Assert.IsTrue(database != null, "Expecting a Database object"); ValidateDatabaseProperties(database, "testdb1", "Standard", 250, 268435456000L, "SQL_Latin1_General_CP1_CI_AS", false, DatabaseTestHelper.StandardS0SloGuid); Assert.IsTrue(database3.Count == 5); foreach (var entry in database3) { var db = entry.BaseObject as Services.Server.Database; Assert.IsTrue(db != null, "Expecting a Database object"); } } }
/// <summary> /// Imports the Azure Manifest to the given <paramref name="powershell"/> instance. /// </summary> /// <param name="powershell">An instance of the <see cref="PowerShell"/> object.</param> public static void ImportAzureModule(System.Management.Automation.PowerShell powershell) { // Import the test manifest file powershell.InvokeBatchScript( string.Format(@"Import-Module .\{0}", SqlDatabaseTestManifest)); Assert.IsTrue(powershell.Streams.Error.Count == 0); }
/// <summary> /// Creates the $credential object in the given <paramref name="powershell"/> instance with /// the given user name and password. /// </summary> /// <param name="powershell">An instance of the <see cref="PowerShell"/> object.</param> public static void CreateTestCredential(System.Management.Automation.PowerShell powershell, string username, string password) { // Create the test credential powershell.InvokeBatchScript( string.Format(@"$user = ""{0}""", username), string.Format(@"$pass = ""{0}"" | ConvertTo-SecureString -asPlainText -Force", password), @"$credential = New-Object System.Management.Automation.PSCredential($user, $pass)"); Assert.IsTrue(powershell.Streams.Error.Count == 0); }
/// <summary> /// Helper function to remove the test databases. /// </summary> public static void RemoveTestDatabasesWithCertAuth(System.Management.Automation.PowerShell powershell) { HttpSession testSession = MockServerHelper.DefaultSessionCollection.GetSession( "UnitTest.Common.RemoveTestDatabasesWithCertAuth"); ServerTestHelper.SetDefaultTestSessionSettings(testSession); testSession.RequestValidator = new Action <HttpMessage, HttpMessage.Request>( (expected, actual) => { Assert.AreEqual(expected.RequestInfo.Method, actual.Method); Assert.IsTrue( actual.UserAgent.Contains(ApiConstants.UserAgentHeaderValue), "Missing proper UserAgent string."); }); using (AsyncExceptionManager exceptionManager = new AsyncExceptionManager()) { using (new MockHttpServer( exceptionManager, MockHttpServer.DefaultHttpsServerPrefixUri, testSession)) { powershell.InvokeBatchScript( @"Remove-AzureSqlDatabase " + @"-ServerName $serverName " + @"-DatabaseName testdb1 " + @"-Force"); powershell.InvokeBatchScript( @"Remove-AzureSqlDatabase " + @"-ServerName $serverName " + @"-DatabaseName testdb2 " + @"-Force"); powershell.InvokeBatchScript( @"Remove-AzureSqlDatabase " + @"-ServerName $serverName " + @"-DatabaseName testdb3 " + @"-Force"); } powershell.Streams.ClearStreams(); } }
public void NewAzureSqlDatabaseServerContextWithSqlAuth() { // Create standard context with both ManageUrl and ServerName overridden using (System.Management.Automation.PowerShell powershell = System.Management.Automation.PowerShell.Create()) { NewAzureSqlDatabaseServerContextTests.CreateServerContextSqlAuth( powershell, "$context"); } // Create context with just ManageUrl and a derived servername HttpSession testSession = MockServerHelper.DefaultSessionCollection.GetSession( "UnitTests.NewAzureSqlDatabaseServerContextWithSqlAuthDerivedName"); testSession.ServiceBaseUri = MockServerHelper.CommonServiceBaseUri; using (System.Management.Automation.PowerShell powershell = System.Management.Automation.PowerShell.Create()) { UnitTestHelper.ImportAzureModule(powershell); UnitTestHelper.CreateTestCredential(powershell); // Tell the sql auth factory to create a v2 context (skip checking sql version using select query). // SqlAuthContextFactory.sqlVersion = SqlAuthContextFactory.SqlVersion.v2; using (AsyncExceptionManager exceptionManager = new AsyncExceptionManager()) { Collection <PSObject> serverContext; using (new MockHttpServer( exceptionManager, MockHttpServer.DefaultServerPrefixUri, testSession)) { serverContext = powershell.InvokeBatchScript( string.Format( CultureInfo.InvariantCulture, @"$context = New-AzureSqlDatabaseServerContext " + @"-ManageUrl {0} " + @"-Credential $credential", MockHttpServer.DefaultServerPrefixUri.AbsoluteUri), @"$context"); } Assert.AreEqual(0, powershell.Streams.Error.Count, "Errors during run!"); powershell.Streams.ClearStreams(); PSObject contextPsObject = serverContext.Single(); Assert.IsTrue( contextPsObject.BaseObject is ServerDataServiceSqlAuth, "Expecting a ServerDataServiceSqlAuth object"); } } }
/// <summary> /// Wait for Continuous Copy operation to become catchup. /// </summary> /// <param name="powershell">The powershell instance containing the context.</param> /// <param name="contextVariable">The variable name that holds the server context.</param> /// <param name="dbCopyVariable">The variable name that holds the db copy object.</param> public static void WaitContinuousCopyCatchup( System.Management.Automation.PowerShell powershell, string contextVariable, string dbCopyVariable) { if (DatabaseTestHelper.CommonServiceBaseUri == null) { // Don't need to wait during playback return; } HttpSession testSession = new HttpSession(); testSession.Messages = new HttpMessageCollection(); DatabaseTestHelper.SetDefaultTestSessionSettings(testSession); using (AsyncExceptionManager exceptionManager = new AsyncExceptionManager()) { using (new MockHttpServer( exceptionManager, MockHttpServer.DefaultServerPrefixUri, testSession)) { for (int i = 0; i < 20; i++) { Collection <PSObject> databaseCopy = powershell.InvokeBatchScript( string.Format( CultureInfo.InvariantCulture, @"{0} | Get-AzureSqlDatabaseCopy " + @"-Context $context", dbCopyVariable)); Assert.AreEqual(0, powershell.Streams.Error.Count, "Errors during run!"); Assert.AreEqual(0, powershell.Streams.Warning.Count, "Warnings during run!"); powershell.Streams.ClearStreams(); Assert.IsTrue( databaseCopy.First().BaseObject is Services.Server.DatabaseCopy, "Expecting a Database Copy object"); Services.Server.DatabaseCopy databaseCopyObj = (Services.Server.DatabaseCopy)databaseCopy.Single().BaseObject; if (databaseCopyObj.ReplicationStateDescription == "CATCH_UP") { break; } Thread.Sleep(TimeSpan.FromSeconds(1)); } } } }
/// <summary> /// Helper method for wait for all copy to terminate. /// </summary> /// <param name="powershell">The powershell instance containing the context.</param> /// <param name="contextVariable">The variable name that will hold the new context.</param> public static void WaitForCopyTermination( System.Management.Automation.PowerShell powershell, string contextVariable) { if (DatabaseTestHelper.CommonServiceBaseUri == null) { // Don't need to wait during playback return; } HttpSession testSession = new HttpSession(); testSession.Messages = new HttpMessageCollection(); DatabaseTestHelper.SetDefaultTestSessionSettings(testSession); Collection <PSObject> databaseCopies = null; using (AsyncExceptionManager exceptionManager = new AsyncExceptionManager()) { using (new MockHttpServer( exceptionManager, MockHttpServer.DefaultServerPrefixUri, testSession)) { for (int i = 0; i < 20; i++) { databaseCopies = powershell.InvokeBatchScript( string.Format( CultureInfo.InvariantCulture, @"Get-AzureSqlDatabaseCopy " + @"-Context {0}", contextVariable)); if (databaseCopies.Count == 0) { break; } Thread.Sleep(TimeSpan.FromSeconds(1)); } } } Assert.AreEqual(0, databaseCopies.Count, "Expecting 0 Database Copy objects"); }
/// <summary> /// Common helper method for other tests to create a context for ESA server. /// </summary> /// <param name="contextVariable">The variable name that will hold the new context.</param> public static void CreateServerContextSqlAuthV12( System.Management.Automation.PowerShell powershell, string manageUrl, string username, string password, string contextVariable) { UnitTestHelper.ImportAzureModule(powershell); UnitTestHelper.CreateTestCredential( powershell, username, password); // Tell the sql auth factory to create a v22 context (skip checking sql version using select query). // SqlAuthContextFactory.sqlVersion = SqlAuthContextFactory.SqlVersion.v12; Collection <PSObject> serverContext; using (AsyncExceptionManager exceptionManager = new AsyncExceptionManager()) { serverContext = powershell.InvokeBatchScript( string.Format( CultureInfo.InvariantCulture, @"{1} = New-AzureSqlDatabaseServerContext " + @"-ManageUrl {0} " + @"-Credential $credential ", manageUrl, contextVariable), contextVariable); } Assert.AreEqual(0, powershell.Streams.Error.Count, "Errors during run!"); Assert.AreEqual(0, powershell.Streams.Warning.Count, "Warnings during run!"); powershell.Streams.ClearStreams(); PSObject contextPsObject = serverContext.Single(); Assert.IsTrue( contextPsObject.BaseObject is TSqlConnectionContext, "Expecting a TSqlConnectionContext object"); }
public void RestoreAzureSqlDatabaseWithDatabaseNameWithCertAuth() { var testSession = MockServerHelper.DefaultSessionCollection.GetSession( "UnitTests.RestoreAzureSqlDatabaseWithDatabaseNameWithCertAuth"); ServerTestHelper.SetDefaultTestSessionSettings(testSession); testSession.RequestValidator = new Action <HttpMessage, HttpMessage.Request>( (expected, actual) => { Assert.AreEqual(expected.RequestInfo.Method, actual.Method); Assert.IsTrue( actual.UserAgent.Contains(ApiConstants.UserAgentHeaderValue), "Missing proper UserAgent string."); }); using (var exceptionManager = new AsyncExceptionManager()) { Collection <PSObject> operation; using (new MockHttpServer( exceptionManager, MockHttpServer.DefaultHttpsServerPrefixUri, testSession)) { operation = powershell.InvokeBatchScript( @"Start-AzureSqlDatabaseRestore " + @"-SourceServerName $serverName " + @"-SourceDatabaseName testdb1 " + @"-TargetDatabaseName testdb1-restored"); } Assert.AreEqual(0, powershell.Streams.Error.Count, "Errors during run!"); Assert.AreEqual(0, powershell.Streams.Warning.Count, "Warnings during run!"); powershell.Streams.ClearStreams(); // Expecting one operation object Assert.AreEqual(1, operation.Count, "Expecting one operation object"); Assert.IsTrue( operation[0].BaseObject is RestoreDatabaseOperation, "Expecting a RestoreDatabaseOperation object"); var operationObject = (RestoreDatabaseOperation)operation[0].BaseObject; Assert.IsTrue( operationObject.RequestID != Guid.Empty, "Expecting a non-empty operation ID"); } }
public void GetAzureSqlDatabaseWithSqlAuthNonExistentDb() { using (System.Management.Automation.PowerShell powershell = System.Management.Automation.PowerShell.Create()) { // Create a context NewAzureSqlDatabaseServerContextTests.CreateServerContextSqlAuth( powershell, "$context"); // Query a non-existent test database HttpSession testSession = MockServerHelper.DefaultSessionCollection.GetSession( "UnitTests.GetAzureSqlDatabaseWithSqlAuthNonExistentDb"); DatabaseTestHelper.SetDefaultTestSessionSettings(testSession); testSession.RequestValidator = new Action <HttpMessage, HttpMessage.Request>( (expected, actual) => { Assert.AreEqual(expected.RequestInfo.Method, actual.Method); Assert.AreEqual(expected.RequestInfo.UserAgent, actual.UserAgent); switch (expected.Index) { // Request 0-2: Get database requests case 0: DatabaseTestHelper.ValidateHeadersForODataRequest( expected.RequestInfo, actual); break; default: Assert.Fail("No more requests expected."); break; } }); using (AsyncExceptionManager exceptionManager = new AsyncExceptionManager()) { using (new MockHttpServer( exceptionManager, MockHttpServer.DefaultServerPrefixUri, testSession)) { powershell.InvokeBatchScript( @"Get-AzureSqlDatabase " + @"-Context $context " + @"-DatabaseName testdb3"); } } Assert.AreEqual(1, powershell.Streams.Error.Count, "Expecting errors"); Assert.AreEqual(2, powershell.Streams.Warning.Count, "Expecting tracing IDs"); Assert.AreEqual( "Database 'myserver01.testdb3' not found.", powershell.Streams.Error.First().Exception.Message, "Unexpected error message"); Assert.IsTrue( powershell.Streams.Warning.Any(w => w.Message.StartsWith("Client Session Id")), "Expecting Client Session Id"); Assert.IsTrue( powershell.Streams.Warning.Any(w => w.Message.StartsWith("Client Request Id")), "Expecting Client Request Id"); powershell.Streams.ClearStreams(); } }
public void CreatePremiumDatabasesWithSqlAuth() { using (System.Management.Automation.PowerShell powershell = System.Management.Automation.PowerShell.Create()) { NewAzureSqlDatabaseServerContextTests.CreateServerContextSqlAuth( powershell, "$context"); HttpSession testSession = MockServerHelper.DefaultSessionCollection.GetSession( "UnitTest.Common.CreatePremiumDatabasesWithSqlAuth"); DatabaseTestHelper.SetDefaultTestSessionSettings(testSession); testSession.RequestValidator = new Action <HttpMessage, HttpMessage.Request>( (expected, actual) => { Assert.AreEqual(expected.RequestInfo.Method, actual.Method); Assert.AreEqual(expected.RequestInfo.UserAgent, actual.UserAgent); }); using (AsyncExceptionManager exceptionManager = new AsyncExceptionManager()) { Collection <PSObject> premiumDB_P1, PremiumDB_P2; using (new MockHttpServer( exceptionManager, MockHttpServer.DefaultServerPrefixUri, testSession)) { powershell.InvokeBatchScript( @"$P1 = Get-AzureSqlDatabaseServiceObjective" + @" -Context $context" + @" -ServiceObjectiveName ""P1"""); powershell.InvokeBatchScript( @"$P2 = Get-AzureSqlDatabaseServiceObjective " + @"-Context $context" + @" -ServiceObjectiveName ""P2"""); premiumDB_P1 = powershell.InvokeBatchScript( @"$premiumDB_P1 = New-AzureSqlDatabase " + @"-Context $context " + @"-DatabaseName NewAzureSqlPremiumDatabaseTests_P1 " + @"-Edition Premium " + @"-ServiceObjective $P1 "); premiumDB_P1 = powershell.InvokeBatchScript("$PremiumDB_P1"); powershell.InvokeBatchScript( @"$PremiumDB_P2 = New-AzureSqlDatabase " + @"-Context $context " + @"-DatabaseName NewAzureSqlPremiumDatabaseTests_P2 " + @"-Collation Japanese_CI_AS " + @"-Edition Premium " + @"-ServiceObjective $P2 " + @"-MaxSizeGB 10 " + @"-Force"); PremiumDB_P2 = powershell.InvokeBatchScript("$PremiumDB_P2"); } Assert.AreEqual(0, powershell.Streams.Error.Count, "Errors during run!"); Assert.AreEqual(0, powershell.Streams.Warning.Count, "Warnings during run!"); powershell.Streams.ClearStreams(); Assert.IsTrue( premiumDB_P1.Single().BaseObject is Services.Server.Database, "Expecting a Database object"); Services.Server.Database databaseP1 = (Services.Server.Database)premiumDB_P1.Single().BaseObject; Assert.AreEqual("NewAzureSqlPremiumDatabaseTests_P1", databaseP1.Name, "Expected db name to be NewAzureSqlPremiumDatabaseTests_P1"); Assert.IsTrue( PremiumDB_P2.Single().BaseObject is Services.Server.Database, "Expecting a Database object"); Services.Server.Database databaseP2 = (Services.Server.Database)PremiumDB_P2.Single().BaseObject; Assert.AreEqual("NewAzureSqlPremiumDatabaseTests_P2", databaseP2.Name, "Expected db name to be NewAzureSqlPremiumDatabaseTests_P2"); Assert.AreEqual( "Japanese_CI_AS", databaseP2.CollationName, "Expected collation to be Japanese_CI_AS"); Assert.AreEqual("Premium", databaseP2.Edition, "Expected edition to be Premium"); Assert.AreEqual(10, databaseP2.MaxSizeGB, "Expected max size to be 10 GB"); } } }
public void AzureSqlDatabaseServerQuotaCertAuthTest() { // This test uses the https endpoint, setup the certificates. MockHttpServer.SetupCertificates(); using (System.Management.Automation.PowerShell powershell = System.Management.Automation.PowerShell.Create()) { // Setup the subscription used for the test AzureSubscription subscription = UnitTestHelper.SetupUnitTestSubscription(powershell); powershell.Runspace.SessionStateProxy.SetVariable( "serverName", SqlDatabaseTestSettings.Instance.ServerV2); // Create a new server HttpSession testSession = MockServerHelper.DefaultSessionCollection.GetSession( "UnitTest.AzureSqlDatabaseServerQuotaCertAuthTest"); ServerTestHelper.SetDefaultTestSessionSettings(testSession); testSession.RequestValidator = new Action <HttpMessage, HttpMessage.Request>( (expected, actual) => { Assert.AreEqual(expected.RequestInfo.Method, actual.Method); Assert.IsTrue( actual.UserAgent.Contains(ApiConstants.UserAgentHeaderValue), "Missing proper UserAgent string."); }); Collection <PSObject> getQuota1 = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { return(powershell.InvokeBatchScript( @"Get-AzureSqlDatabaseServerQuota -ServerName $serverName")); }); Collection <PSObject> getQuota2 = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { return(powershell.InvokeBatchScript( @"Get-AzureSqlDatabaseServerQuota -ServerName $serverName -QuotaName premium_databases")); }); Assert.AreEqual(0, powershell.Streams.Error.Count, "Unexpected Errors during run!"); Assert.AreEqual(0, powershell.Streams.Warning.Count, "Unexpected Warnings during run!"); // Validate Get-AzureSqlDatabaseServerQuota var quotas = getQuota1.Select(x => ((IEnumerable)x.BaseObject).Cast <Model.SqlDatabaseServerQuotaContext>().Single()).ToArray(); Assert.AreEqual(1, quotas.Length, "Expecting one server quota"); Assert.IsNotNull(quotas[0], "Expecting a server quota."); Assert.AreEqual("premium_databases", quotas[0].Name); Assert.AreEqual(SqlDatabaseTestSettings.Instance.ServerV2, quotas[0].ServerName); Assert.AreEqual("Microsoft.SqlAzure.ServerQuota", quotas[0].Type); Assert.AreEqual("100", quotas[0].Value); Assert.AreEqual("Normal", quotas[0].State); quotas = getQuota2.Select(x => ((IEnumerable)x.BaseObject).Cast <Model.SqlDatabaseServerQuotaContext>().Single()).ToArray(); Assert.AreEqual(1, quotas.Length, "Expecting server quota"); Assert.IsNotNull(quotas[0], "Expecting a server quota."); Assert.AreEqual("premium_databases", quotas[0].Name); Assert.AreEqual(SqlDatabaseTestSettings.Instance.ServerV2, quotas[0].ServerName); Assert.AreEqual("Microsoft.SqlAzure.ServerQuota", quotas[0].Type); Assert.AreEqual("100", quotas[0].Value); Assert.AreEqual("Normal", quotas[0].State); } }
public void AzureSqlDatabaseEditionsTests() { // This test uses the https endpoint, setup the certificates. MockHttpServer.SetupCertificates(); using (System.Management.Automation.PowerShell powershell = System.Management.Automation.PowerShell.Create()) { // Setup the subscription used for the test AzureSubscription subscription = UnitTestHelper.SetupUnitTestSubscription(powershell); powershell.Runspace.SessionStateProxy.SetVariable( "serverName", SqlDatabaseTestSettings.Instance.ServerV2); // Create a new server HttpSession testSession = MockServerHelper.DefaultSessionCollection.GetSession( "UnitTest.AzureSqlDatabaseEditionsTests"); ServerTestHelper.SetDefaultTestSessionSettings(testSession); testSession.RequestValidator = new Action <HttpMessage, HttpMessage.Request>( (expected, actual) => { Assert.AreEqual(expected.RequestInfo.Method, actual.Method); Assert.IsTrue( actual.UserAgent.Contains(ApiConstants.UserAgentHeaderValue), "Missing proper UserAgent string."); }); Collection <PSObject> newDatabaseResult1 = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { return(powershell.InvokeBatchScript( @"($db = New-AzureSqlDatabase -ServerName $serverName -DatabaseName testdbeditions1)")); }); Collection <PSObject> newDatabaseResult2 = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { return(powershell.InvokeBatchScript( @"New-AzureSqlDatabase" + @" -ServerName $serverName" + @" -DatabaseName testdbeditions2" + @" -Edition Standard")); }); Collection <PSObject> newDatabaseResult3 = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { return(powershell.InvokeBatchScript( @"($db3 = New-AzureSqlDatabase" + @" -ServerName $serverName" + @" -DatabaseName testdbeditions3" + @" -Edition Basic)")); }); Collection <PSObject> newDatabaseResult4 = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { return(powershell.InvokeBatchScript( @"New-AzureSqlDatabase" + @" -ServerName $serverName" + @" -DatabaseName testdbeditions4" + @" -Edition Premium")); }); Collection <PSObject> serviceObjectives = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { return(powershell.InvokeBatchScript( @"($so = Get-AzureSqlDatabaseServiceObjective -Server $serverName)")); }); Collection <PSObject> newDatabaseResult5 = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { return(powershell.InvokeBatchScript( @"New-AzureSqlDatabase" + @" -ServerName $serverName" + @" -DatabaseName testdbeditions5" + @" -Edition Standard" + @" -ServiceObjective ($so | where-object { $_.name -match ""S2"" })")); }); Collection <PSObject> getSingleDatabaseResult1 = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { return(powershell.InvokeBatchScript( @"Get-AzureSqlDatabase $serverName -DatabaseName testdbeditions1")); }); Collection <PSObject> getSingleDatabaseResult2 = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { return(powershell.InvokeBatchScript( @"$db | Get-AzureSqlDatabase")); }); Collection <PSObject> setDatabaseObjective1 = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { return(powershell.InvokeBatchScript( @"Set-AzureSqlDatabase -ServerName $serverName -DatabaseName testdbeditions2 -ServiceObjective ($so | where-object { $_.name -match ""S2"" }) -Force")); }); Collection <PSObject> setDatabaseObjective2 = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { return(powershell.InvokeBatchScript( @"Set-AzureSqlDatabase -ServerName $serverName -DatabaseName $db3.Name -Edition Standard -MaxSizeGB 1 -ServiceObjective ($so | where-object { $_.name -match ""S1"" }) -Force")); }); Collection <PSObject> getDatabaseResult = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { return(powershell.InvokeBatchScript( @"Get-AzureSqlDatabase" + @" $serverName")); }); Collection <PSObject> removeDatabaseResult = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { powershell.Runspace.SessionStateProxy.SetVariable("db1", newDatabaseResult1.FirstOrDefault()); powershell.Runspace.SessionStateProxy.SetVariable("db2", newDatabaseResult2.FirstOrDefault()); powershell.Runspace.SessionStateProxy.SetVariable("db3", newDatabaseResult3.FirstOrDefault()); powershell.Runspace.SessionStateProxy.SetVariable("db4", newDatabaseResult4.FirstOrDefault()); powershell.Runspace.SessionStateProxy.SetVariable("db5", newDatabaseResult5.FirstOrDefault()); powershell.InvokeBatchScript(@"$db1 | Remove-AzureSqlDatabase -Force"); powershell.InvokeBatchScript(@"$db2 | Remove-AzureSqlDatabase -Force"); powershell.InvokeBatchScript(@"$db3 | Remove-AzureSqlDatabase -Force"); powershell.InvokeBatchScript(@"$db4 | Remove-AzureSqlDatabase -Force"); powershell.InvokeBatchScript(@"$db5 | Remove-AzureSqlDatabase -Force"); return(powershell.InvokeBatchScript(@"Get-AzureSqlDatabase $serverName")); }); Assert.AreEqual(0, powershell.Streams.Error.Count, "Unexpected Errors during run!"); Assert.AreEqual(0, powershell.Streams.Warning.Count, "Unexpected Warnings during run!"); // Validate New-AzureSqlDatabase Services.Server.Database[] databases = new Services.Server.Database[] { newDatabaseResult1.Single().BaseObject as Services.Server.Database }; Assert.AreEqual(1, databases.Length, "Expecting one database"); Assert.IsNotNull(databases[0], "Expecting a Database object."); DatabaseTestHelper.ValidateDatabaseProperties(databases[0], "testdbeditions1", "Standard", 250, 268435456000L, "SQL_Latin1_General_CP1_CI_AS", "S0", false, DatabaseTestHelper.StandardS0SloGuid); databases = new Services.Server.Database[] { newDatabaseResult2.Single().BaseObject as Services.Server.Database }; Assert.AreEqual(1, databases.Length, "Expecting one database"); Assert.IsNotNull(databases[0], "Expecting a Database object."); DatabaseTestHelper.ValidateDatabaseProperties(databases[0], "testdbeditions2", "Standard", 250, 268435456000L, "SQL_Latin1_General_CP1_CI_AS", "S0", false, DatabaseTestHelper.StandardS0SloGuid); databases = new Services.Server.Database[] { newDatabaseResult3.Single().BaseObject as Services.Server.Database }; Assert.AreEqual(1, databases.Length, "Expecting one database"); Assert.IsNotNull(databases[0], "Expecting a Database object."); DatabaseTestHelper.ValidateDatabaseProperties(databases[0], "testdbeditions3", "Basic", 2, 2147483648L, "SQL_Latin1_General_CP1_CI_AS", "Basic", false, DatabaseTestHelper.BasicSloGuid); databases = new Services.Server.Database[] { newDatabaseResult4.Single().BaseObject as Services.Server.Database }; Assert.AreEqual(1, databases.Length, "Expecting one database"); Assert.IsNotNull(databases[0], "Expecting a Database object."); DatabaseTestHelper.ValidateDatabaseProperties(databases[0], "testdbeditions4", "Premium", 500, 536870912000L, "SQL_Latin1_General_CP1_CI_AS", "P1", false, DatabaseTestHelper.PremiumP1SloGuid); databases = new Services.Server.Database[] { newDatabaseResult5.Single().BaseObject as Services.Server.Database }; Assert.AreEqual(1, databases.Length, "Expecting one database"); Assert.IsNotNull(databases[0], "Expecting a Database object."); DatabaseTestHelper.ValidateDatabaseProperties(databases[0], "testdbeditions5", "Standard", 250, 268435456000L, "SQL_Latin1_General_CP1_CI_AS", "S2", false, DatabaseTestHelper.StandardS2SloGuid); // Validate Get-AzureSqlDatabase databases = getDatabaseResult.Select(r => r.BaseObject as Services.Server.Database).ToArray(); Assert.AreEqual(6, databases.Length, "Expecting 3 databases"); Assert.IsNotNull(databases[0], "Expecting a Database object."); Assert.IsNotNull(databases[1], "Expecting a Database object."); Assert.IsNotNull(databases[2], "Expecting a Database object."); Assert.IsNotNull(databases[3], "Expecting a Database object."); Assert.IsNotNull(databases[4], "Expecting a Database object."); Assert.IsNotNull(databases[5], "Expecting a Database object."); DatabaseTestHelper.ValidateDatabaseProperties(databases[1], "master", "System", 5, 5368709120L, "SQL_Latin1_General_CP1_CI_AS", "System2", true, DatabaseTestHelper.System2SloGuid); DatabaseTestHelper.ValidateDatabaseProperties(databases[3], "testdbeditions1", "Standard", 250, 268435456000L, "SQL_Latin1_General_CP1_CI_AS", "S0", false, DatabaseTestHelper.StandardS0SloGuid); DatabaseTestHelper.ValidateDatabaseProperties(databases[4], "testdbeditions2", "Standard", 250, 268435456000L, "SQL_Latin1_General_CP1_CI_AS", "S0", false, DatabaseTestHelper.StandardS2SloGuid); DatabaseTestHelper.ValidateDatabaseProperties(databases[5], "testdbeditions3", "Basic", 2, 2147483648L, "SQL_Latin1_General_CP1_CI_AS", "Basic", false, DatabaseTestHelper.StandardS1SloGuid); DatabaseTestHelper.ValidateDatabaseProperties(databases[2], "testdbeditions4", "Premium", 500, 536870912000L, "SQL_Latin1_General_CP1_CI_AS", "P1", false, DatabaseTestHelper.PremiumP1SloGuid); DatabaseTestHelper.ValidateDatabaseProperties(databases[0], "testdbeditions5", "Standard", 250, 268435456000L, "SQL_Latin1_General_CP1_CI_AS", "S2", false, DatabaseTestHelper.StandardS2SloGuid); // Validate Get-AzureSqlDatabaseServiceObjective var sos = serviceObjectives.Select(x => x.BaseObject as ServiceObjective).ToArray(); Assert.AreEqual(10, sos.Count()); ValidateServiceObjectiveProperties(sos[0], "System", "", 1, "Used for master database only."); ValidateServiceObjectiveProperties(sos[1], "System2", "", 1, ""); ValidateServiceObjectiveProperties(sos[2], "Basic", "", 1, "Basic resource allocation."); ValidateServiceObjectiveProperties(sos[3], "S0", "", 1, "Standard S0 resource allocation."); ValidateServiceObjectiveProperties(sos[4], "S1", "", 1, "Standard S1 resource allocation."); ValidateServiceObjectiveProperties(sos[5], "S2", "", 1, "Standard S2 resource allocation."); ValidateServiceObjectiveProperties(sos[6], "S3", "", 1, "Standard S3 resource allocation."); ValidateServiceObjectiveProperties(sos[7], "P1", "", 1, "Premium P1 resource allocation."); ValidateServiceObjectiveProperties(sos[8], "P2", "", 1, "Premium P2 resource allocation."); ValidateServiceObjectiveProperties(sos[9], "P3", "", 1, "Premium P3 resource allocation."); // Validate Remove-AzureSqlDatabase databases = new Services.Server.Database[] { removeDatabaseResult.Single().BaseObject as Services.Server.Database }; Assert.AreEqual(1, databases.Length, "Expecting no databases"); Assert.IsNotNull(databases[0], "Expecting a Database object."); DatabaseTestHelper.ValidateDatabaseProperties(databases[0], "master", "System", 5, 5368709120L, "SQL_Latin1_General_CP1_CI_AS", "System2", true, DatabaseTestHelper.System2SloGuid); } }
public void AzureSqlDatabaseCertTests() { // This test uses the https endpoint, setup the certificates. MockHttpServer.SetupCertificates(); using (System.Management.Automation.PowerShell powershell = System.Management.Automation.PowerShell.Create()) { // Setup the subscription used for the test AzureSubscription subscription = UnitTestHelper.SetupUnitTestSubscription(powershell); powershell.Runspace.SessionStateProxy.SetVariable( "serverName", SqlDatabaseTestSettings.Instance.ServerName); // Create a new server HttpSession testSession = MockServerHelper.DefaultSessionCollection.GetSession( "UnitTest.AzureSqlDatabaseCertTests"); ServerTestHelper.SetDefaultTestSessionSettings(testSession); testSession.RequestValidator = new Action <HttpMessage, HttpMessage.Request>( (expected, actual) => { Assert.AreEqual(expected.RequestInfo.Method, actual.Method); Assert.IsTrue( actual.UserAgent.Contains(ApiConstants.UserAgentHeaderValue), "Missing proper UserAgent string."); }); Collection <PSObject> newDatabaseResult1 = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { return(powershell.InvokeBatchScript( @"New-AzureSqlDatabase" + @" -ServerName $serverName" + @" -DatabaseName testdbcert1")); }); Collection <PSObject> newDatabaseResult2 = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { return(powershell.InvokeBatchScript( @"New-AzureSqlDatabase" + @" -ServerName $serverName" + @" -DatabaseName testdbcert2" + @" -Edition Business" + @" -MaxSizeGB 10" + @" -Collation Japanese_CI_AS")); }); // Create a database of size 100MB Default Edition (Standard) Collection <PSObject> newDatabaseResult3 = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { return(powershell.InvokeBatchScript( @"New-AzureSqlDatabase" + @" -ServerName $serverName" + @" -DatabaseName testdbcert4" + @" -MaxSizeBytes 104857600")); }); Collection <PSObject> getDatabaseResult = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { return(powershell.InvokeBatchScript( @"Get-AzureSqlDatabase" + @" $serverName")); }); Collection <PSObject> getSingleDatabaseResult = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { return(powershell.InvokeBatchScript( @"Get-AzureSqlDatabase" + @" $serverName" + @" -DatabaseName testdbcert1")); }); Collection <PSObject> getSingleDatabaseResult2 = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { return(powershell.InvokeBatchScript( @"Get-AzureSqlDatabase" + @" $serverName" + @" -DatabaseName testdbcert4")); }); Collection <PSObject> setDatabaseNameResult = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { powershell.Runspace.SessionStateProxy.SetVariable("db", newDatabaseResult1.FirstOrDefault()); return(powershell.InvokeBatchScript( @"$db | Set-AzureSqlDatabase" + @" -NewDatabaseName testdbcert3" + @" -PassThru")); }); Collection <PSObject> setDatabaseSizeResult = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { powershell.Runspace.SessionStateProxy.SetVariable("db", newDatabaseResult1.FirstOrDefault()); return(powershell.InvokeBatchScript( @"$db | Set-AzureSqlDatabase" + @" -MaxSizeGB 5" + @" -PassThru")); }); Collection <PSObject> setDatabaseSizeResult2 = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { return(powershell.InvokeBatchScript( @"Set-AzureSqlDatabase" + @" -ServerName $serverName" + @" -DatabaseName testdbcert4" + @" -MaxSizeBytes 1073741824" + @" -passthru")); }); Collection <PSObject> P1 = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { return(powershell.InvokeBatchScript( @"$P1 = Get-AzureSqlDatabaseServiceObjective" + @" -Server $serverName" + @" -ServiceObjectiveName ""P1""", @"$P1")); }); Collection <PSObject> P2 = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { powershell.InvokeBatchScript( @"$SLO = Get-AzureSqlDatabaseServiceObjective" + @" -Server $serverName"); return(powershell.InvokeBatchScript( @"$P2 = Get-AzureSqlDatabaseServiceObjective" + @" -Server $serverName" + @" -ServiceObjective ($slo | where-object { $_.name -match ""P2"" })", @"$P2")); }); Collection <PSObject> setDatabaseSlo = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { return(powershell.InvokeBatchScript( @"Set-AzureSqlDatabase" + @" -ServerName $serverName" + @" -DatabaseName testdbcert4" + @" -Edition Premium" + @" -MaxSizeGb 10" + @" -ServiceObjective $P2" + @" -passthru" + @" -Force")); }); Collection <PSObject> removeDatabaseResult1 = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { powershell.Runspace.SessionStateProxy.SetVariable("db1", newDatabaseResult1.FirstOrDefault()); powershell.Runspace.SessionStateProxy.SetVariable("db2", newDatabaseResult2.FirstOrDefault()); powershell.Runspace.SessionStateProxy.SetVariable("db3", newDatabaseResult3.FirstOrDefault()); powershell.InvokeBatchScript(@"$db1 | Remove-AzureSqlDatabase -Force"); powershell.InvokeBatchScript(@"$db2 | Remove-AzureSqlDatabase -Force"); powershell.InvokeBatchScript(@"$db3 | Remove-AzureSqlDatabase -Force"); return(powershell.InvokeBatchScript(@"Get-AzureSqlDatabase $serverName")); }); Collection <PSObject> newPremiumP1DatabaseResult = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { return(powershell.InvokeBatchScript( @"New-AzureSqlDatabase" + @" -ServerName $serverName" + @" -DatabaseName ""testdbcertPremiumDBP1""" + @" -Edition Premium" + @" -ServiceObjective $P1" + @" -MaxSizeGb 10")); }); Collection <PSObject> newPremiumP2DatabaseResult = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { return(powershell.InvokeBatchScript( @"New-AzureSqlDatabase" + @" -ServerName $serverName" + @" -DatabaseName ""testdbcertPremiumDBP2""" + @" -Edition Premium" + @" -ServiceObjective $P2" + @" -MaxSizeGb 10")); }); // There is a known issue about the Get-AzureSqlDatabaseOperation that it returns all // operations which has the required database name no matter it's been deleted and recreated. // So when run it against the mock session, please use the hard coded testsDBName. // Run against onebox, please use the one with NewGuid(). // This unit test should be updated once that behavior get changed which was already been // created as a task. string getOperationDbName = null; if (testSession.ServiceBaseUri == null) { getOperationDbName = "testdbcertGetOperationDbName_08abc738-1381-4164-ae5e-03a4fe59b6d2"; } else { getOperationDbName = "testdbcertGetOperationDbName_" + Guid.NewGuid().ToString(); } Collection <PSObject> newOperationDbResult = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { return(powershell.InvokeBatchScript( string.Format( CultureInfo.InvariantCulture, @"$getOperationDb = New-AzureSqlDatabase" + @" -ServerName $serverName" + @" -DatabaseName ""{0}""", getOperationDbName), @"$getOperationDb")); }); Collection <PSObject> getDatabaseOperationByDbResult = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { return(powershell.InvokeBatchScript( string.Format( CultureInfo.InvariantCulture, @"Get-AzureSqlDatabaseOperation" + @" -ServerName $serverName" + @" -Database $getOperationDb"))); }); Collection <PSObject> getDatabaseOperationByNameResult = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { return(powershell.InvokeBatchScript( string.Format( CultureInfo.InvariantCulture, @"$getOperation = Get-AzureSqlDatabaseOperation" + @" -ServerName $serverName" + @" -DatabaseName ""{0}""", getOperationDbName), @"$getOperation")); }); Collection <PSObject> getDatabaseOperationByIdResult = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { return(powershell.InvokeBatchScript( string.Format( CultureInfo.InvariantCulture, @"Get-AzureSqlDatabaseOperation" + @" -ServerName $serverName" + @" -OperationGuid $getOperation[0].Id"))); }); Collection <PSObject> removeDatabaseResult2 = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { powershell.Runspace.SessionStateProxy.SetVariable("premiumP1", newPremiumP1DatabaseResult.FirstOrDefault()); powershell.Runspace.SessionStateProxy.SetVariable("premiumP2", newPremiumP2DatabaseResult.FirstOrDefault()); powershell.Runspace.SessionStateProxy.SetVariable("operationDb", newOperationDbResult.FirstOrDefault()); powershell.InvokeBatchScript(@"$premiumP1 | Remove-AzureSqlDatabase -Force"); powershell.InvokeBatchScript(@"$premiumP2 | Remove-AzureSqlDatabase -Force"); powershell.InvokeBatchScript(@"$operationDb | Remove-AzureSqlDatabase -Force"); return(powershell.InvokeBatchScript(@"Get-AzureSqlDatabase $serverName")); }); Assert.AreEqual(0, powershell.Streams.Error.Count, "Unexpected Errors during run!"); Assert.AreEqual(0, powershell.Streams.Warning.Count, "Unexpected Warnings during run!"); // Validate New-AzureSqlDatabase Services.Server.Database[] databases = new Services.Server.Database[] { newDatabaseResult1.Single().BaseObject as Services.Server.Database }; Assert.AreEqual(1, databases.Length, "Expecting one database"); Assert.IsNotNull(databases[0], "Expecting a Database object."); // Note: Because the object is piped, this is the final state of the // database object, after all the Set- cmdlet has run. DatabaseTestHelper.ValidateDatabaseProperties(databases[0], "testdbcert3", "Standard", 5, 5368709120L, "SQL_Latin1_General_CP1_CI_AS", "S0", false, DatabaseTestHelper.StandardS0SloGuid); databases = new Services.Server.Database[] { newDatabaseResult2.Single().BaseObject as Services.Server.Database }; Assert.AreEqual(1, databases.Length, "Expecting one database"); Assert.IsNotNull(databases[0], "Expecting a Database object."); //TODO: change below to business DatabaseTestHelper.ValidateDatabaseProperties(databases[0], "testdbcert2", "Business", 10, 10737418240L, "Japanese_CI_AS", "Business", false, DatabaseTestHelper.BusinessSloGuid); databases = new Services.Server.Database[] { newDatabaseResult3.Single().BaseObject as Services.Server.Database }; Assert.AreEqual(1, databases.Length, "Expecting one database"); Assert.IsNotNull(databases[0], "Expecting a Database object."); DatabaseTestHelper.ValidateDatabaseProperties(databases[0], "testdbcert4", "Standard", 0, 104857600L, "SQL_Latin1_General_CP1_CI_AS", "S0", false, DatabaseTestHelper.StandardS0SloGuid); // Validate Get-AzureSqlDatabase databases = getDatabaseResult.Select(r => r.BaseObject as Services.Server.Database).ToArray(); Assert.AreEqual(4, databases.Length, "Expecting 4 databases"); Assert.IsNotNull(databases[0], "Expecting a Database object."); Assert.IsNotNull(databases[1], "Expecting a Database object."); Assert.IsNotNull(databases[2], "Expecting a Database object."); Assert.IsNotNull(databases[3], "Expecting a Database object."); DatabaseTestHelper.ValidateDatabaseProperties(databases[1], "master", "System", 5, 5368709120L, "SQL_Latin1_General_CP1_CI_AS", "System2", true, DatabaseTestHelper.System2SloGuid); DatabaseTestHelper.ValidateDatabaseProperties(databases[3], "testdbcert1", "Standard", 250, 268435456000L, "SQL_Latin1_General_CP1_CI_AS", "S0", false, DatabaseTestHelper.StandardS0SloGuid); DatabaseTestHelper.ValidateDatabaseProperties(databases[2], "testdbcert2", "Business", 10, 10737418240L, "Japanese_CI_AS", "Business", false, DatabaseTestHelper.BusinessSloGuid); DatabaseTestHelper.ValidateDatabaseProperties(databases[0], "testdbcert4", "Standard", 0, 104857600L, "SQL_Latin1_General_CP1_CI_AS", "S0", false, DatabaseTestHelper.StandardS0SloGuid); databases = new Services.Server.Database[] { getSingleDatabaseResult.Single().BaseObject as Services.Server.Database }; Assert.AreEqual(1, databases.Length, "Expecting one database"); Assert.IsNotNull(databases[0], "Expecting a Database object."); DatabaseTestHelper.ValidateDatabaseProperties(databases[0], "testdbcert1", "Standard", 250, 268435456000L, "SQL_Latin1_General_CP1_CI_AS", "S0", false, DatabaseTestHelper.StandardS0SloGuid); databases = new Services.Server.Database[] { getSingleDatabaseResult2.Single().BaseObject as Services.Server.Database }; Assert.AreEqual(1, databases.Length, "Expecting one database"); Assert.IsNotNull(databases[0], "Expecting a Database object."); DatabaseTestHelper.ValidateDatabaseProperties(databases[0], "testdbcert4", "Standard", 0, 104857600L, "SQL_Latin1_General_CP1_CI_AS", "S0", false, DatabaseTestHelper.StandardS0SloGuid); // Validate Set-AzureSqlDatabase databases = new Services.Server.Database[] { setDatabaseNameResult.Single().BaseObject as Services.Server.Database }; Assert.AreEqual(1, databases.Length, "Expecting one database"); Assert.IsNotNull(databases[0], "Expecting a Database object."); DatabaseTestHelper.ValidateDatabaseProperties(databases[0], "testdbcert3", "Standard", 250, 268435456000L, "SQL_Latin1_General_CP1_CI_AS", "S0", false, DatabaseTestHelper.StandardS0SloGuid); databases = new Services.Server.Database[] { setDatabaseSizeResult.Single().BaseObject as Services.Server.Database }; Assert.AreEqual(1, databases.Length, "Expecting one database"); Assert.IsNotNull(databases[0], "Expecting a Database object."); DatabaseTestHelper.ValidateDatabaseProperties(databases[0], "testdbcert3", "Standard", 5, 5368709120L, "SQL_Latin1_General_CP1_CI_AS", "S0", false, DatabaseTestHelper.StandardS0SloGuid); databases = new Services.Server.Database[] { setDatabaseSizeResult2.Single().BaseObject as Services.Server.Database }; Assert.AreEqual(1, databases.Length, "Expecting one database"); Assert.IsNotNull(databases[0], "Expecting a Database object."); DatabaseTestHelper.ValidateDatabaseProperties(databases[0], "testdbcert4", "Standard", 1, 1073741824L, "SQL_Latin1_General_CP1_CI_AS", "S0", false, DatabaseTestHelper.StandardS0SloGuid); databases = new Services.Server.Database[] { setDatabaseSlo.Single().BaseObject as Services.Server.Database }; Assert.AreEqual(1, databases.Length, "Expecting one database"); Assert.IsNotNull(databases[0], "Expecting a Database object."); DatabaseTestHelper.ValidateDatabaseProperties(databases[0], "testdbcert4", "Standard", 1, 1073741824L, "SQL_Latin1_General_CP1_CI_AS", "S0", false, DatabaseTestHelper.PremiumP2SloGuid); // Validate New-AzureSqlDatabase for Premium Edition Database VerifyCreatePremiumDb(newPremiumP1DatabaseResult, "testdbcertPremiumDBP1", (P1.Single().BaseObject as ServiceObjective).Id.ToString()); VerifyCreatePremiumDb(newPremiumP2DatabaseResult, "testdbcertPremiumDBP2", (P2.Single().BaseObject as ServiceObjective).Id.ToString()); // Validate Get-AzureSqlDatabaseServiceObjective var SLOP1 = P1.Single().BaseObject as ServiceObjective; Assert.AreEqual("P1", SLOP1.Name); Assert.IsNotNull(SLOP1.DimensionSettings, "Expecting some Dimension Setting objects."); Assert.AreEqual(1, SLOP1.DimensionSettings.Count(), "Expecting 1 Dimension Setting."); Assert.AreEqual("Premium P1 resource allocation.", SLOP1.DimensionSettings[0].Description, "Expecting Dimension Setting description as Resource capacity is reserved."); var SLOP2 = P2.Single().BaseObject as ServiceObjective; Assert.AreEqual("P2", SLOP2.Name); Assert.IsNotNull(SLOP2.DimensionSettings, "Expecting some Dimension Setting objects."); Assert.AreEqual(1, SLOP2.DimensionSettings.Count(), "Expecting 1 Dimension Setting."); Assert.AreEqual("Premium P2 resource allocation.", SLOP2.DimensionSettings[0].Description, "Expecting Dimension Setting description as Resource capacity is reserved."); // Validate Get-AzureSqlDatabaseOperation VerifyGetAzureSqlDatabaseOperation(getOperationDbName, getDatabaseOperationByDbResult); VerifyGetAzureSqlDatabaseOperation(getOperationDbName, getDatabaseOperationByNameResult); VerifyGetAzureSqlDatabaseOperation(getOperationDbName, getDatabaseOperationByIdResult); // Validate Remove-AzureSqlDatabase databases = new Services.Server.Database[] { removeDatabaseResult1.Single().BaseObject as Services.Server.Database }; Assert.AreEqual(1, databases.Length, "Expecting no databases"); Assert.IsNotNull(databases[0], "Expecting a Database object."); DatabaseTestHelper.ValidateDatabaseProperties(databases[0], "master", "System", 5, 5368709120L, "SQL_Latin1_General_CP1_CI_AS", "System2", true, DatabaseTestHelper.System2SloGuid); databases = new Services.Server.Database[] { removeDatabaseResult2.Single().BaseObject as Services.Server.Database }; Assert.AreEqual(1, databases.Length, "Expecting no databases"); Assert.IsNotNull(databases[0], "Expecting a Database object."); DatabaseTestHelper.ValidateDatabaseProperties(databases[0], "master", "System", 5, 5368709120L, "SQL_Latin1_General_CP1_CI_AS", "System2", true, DatabaseTestHelper.System2SloGuid); } }
/// <summary> /// Common helper method for other tests to create a context. /// </summary> /// <param name="contextVariable">The variable name that will hold the new context.</param> public static void CreateServerContextSqlAuth( System.Management.Automation.PowerShell powershell, string contextVariable) { HttpSession testSession = MockServerHelper.DefaultSessionCollection.GetSession( "UnitTest.Common.NewAzureSqlDatabaseServerContextWithSqlAuth"); DatabaseTestHelper.SetDefaultTestSessionSettings(testSession); testSession.RequestValidator = new Action <HttpMessage, HttpMessage.Request>( (expected, actual) => { Assert.AreEqual(expected.RequestInfo.Method, actual.Method); Assert.AreEqual(expected.RequestInfo.UserAgent, actual.UserAgent); switch (expected.Index) { // Request 0-2: Create context with both ManageUrl and ServerName overriden case 0: // GetAccessToken call DatabaseTestHelper.ValidateGetAccessTokenRequest( expected.RequestInfo, actual); break; case 1: // Get server call DatabaseTestHelper.ValidateHeadersForODataRequest( expected.RequestInfo, actual); break; case 2: // $metadata call Assert.IsTrue( actual.RequestUri.AbsoluteUri.EndsWith("$metadata"), "Incorrect Uri specified for $metadata"); DatabaseTestHelper.ValidateHeadersForServiceRequest( expected.RequestInfo, actual); Assert.AreEqual( expected.RequestInfo.Headers[DataServiceConstants.AccessTokenHeader], actual.Headers[DataServiceConstants.AccessTokenHeader], "AccessToken header does not match"); Assert.AreEqual( expected.RequestInfo.Cookies[DataServiceConstants.AccessCookie], actual.Cookies[DataServiceConstants.AccessCookie], "AccessCookie does not match"); break; default: Assert.Fail("No more requests expected."); break; } }); UnitTestHelper.ImportAzureModule(powershell); UnitTestHelper.CreateTestCredential( powershell, testSession.SessionProperties["Username"], testSession.SessionProperties["Password"]); Collection <PSObject> serverContext; using (AsyncExceptionManager exceptionManager = new AsyncExceptionManager()) { using (new MockHttpServer( exceptionManager, MockHttpServer.DefaultServerPrefixUri, testSession)) { serverContext = powershell.InvokeBatchScript( string.Format( CultureInfo.InvariantCulture, @"{1} = New-AzureSqlDatabaseServerContext " + @"-ServerName {2} " + @"-ManageUrl {0} " + @"-Credential $credential", MockHttpServer.DefaultServerPrefixUri.AbsoluteUri, contextVariable, testSession.SessionProperties["Servername"]), contextVariable); } } Assert.AreEqual(0, powershell.Streams.Error.Count, "Errors during run!"); Assert.AreEqual(0, powershell.Streams.Warning.Count, "Warnings during run!"); powershell.Streams.ClearStreams(); PSObject contextPsObject = serverContext.Single(); Assert.IsTrue( contextPsObject.BaseObject is ServerDataServiceSqlAuth, "Expecting a ServerDataServiceSqlAuth object"); }
public void NewAzureSqlDatabaseServerContextWithSqlAuthNegativeCases() { HttpSession testSession = MockServerHelper.DefaultSessionCollection.GetSession( "UnitTests.NewAzureSqlDatabaseServerContextWithSqlAuthNegativeCases"); using (System.Management.Automation.PowerShell powershell = System.Management.Automation.PowerShell.Create()) { UnitTestHelper.ImportAzureModule(powershell); UnitTestHelper.CreateTestCredential(powershell); using (AsyncExceptionManager exceptionManager = new AsyncExceptionManager()) { // Test warning when different $metadata is received. Collection <PSObject> serverContext; using (new MockHttpServer( exceptionManager, MockHttpServer.DefaultServerPrefixUri, testSession)) { serverContext = powershell.InvokeBatchScript( string.Format( CultureInfo.InvariantCulture, @"$context = New-AzureSqlDatabaseServerContext " + @"-ServerName testserver " + @"-ManageUrl {0} " + @"-Credential $credential", MockHttpServer.DefaultServerPrefixUri.AbsoluteUri), @"$context"); } Assert.AreEqual(0, powershell.Streams.Error.Count, "Errors during run!"); Assert.AreEqual(1, powershell.Streams.Warning.Count, "Should have warning!"); Assert.AreEqual( Resources.WarningModelOutOfDate, powershell.Streams.Warning.First().Message); powershell.Streams.ClearStreams(); PSObject contextPsObject = serverContext.Single(); Assert.IsTrue( contextPsObject.BaseObject is ServerDataServiceSqlAuth, "Expecting a ServerDataServiceSqlAuth object"); // Test error case using (new MockHttpServer( exceptionManager, MockHttpServer.DefaultServerPrefixUri, testSession)) { powershell.InvokeBatchScript( string.Format( CultureInfo.InvariantCulture, @"$context = New-AzureSqlDatabaseServerContext " + @"-ServerName testserver " + @"-ManageUrl {0} " + @"-Credential $credential", MockHttpServer.DefaultServerPrefixUri.AbsoluteUri), @"$context"); } Assert.AreEqual(1, powershell.Streams.Error.Count, "Should have errors!"); Assert.AreEqual(2, powershell.Streams.Warning.Count, "Should have warning!"); Assert.AreEqual( "Test error message", powershell.Streams.Error.First().Exception.Message); Assert.IsTrue( powershell.Streams.Warning.Any( (w) => w.Message.StartsWith("Client Session Id:")), "Client session Id not written to warning"); Assert.IsTrue( powershell.Streams.Warning.Any( (w) => w.Message.StartsWith("Client Request Id:")), "Client request Id not written to warning"); powershell.Streams.ClearStreams(); } } }
public void GetRestorableDroppedDatabaseWithCertAuth() { var testSession = MockServerHelper.DefaultSessionCollection.GetSession( "UnitTest.GetRestorableDroppedDatabaseWithCertAuth"); ServerTestHelper.SetDefaultTestSessionSettings(testSession); testSession.RequestValidator = new Action <HttpMessage, HttpMessage.Request>( (expected, actual) => { Assert.AreEqual(expected.RequestInfo.Method, actual.Method); Assert.IsTrue( actual.UserAgent.Contains(ApiConstants.UserAgentHeaderValue), "Missing proper UserAgent string."); }); using (var exceptionManager = new AsyncExceptionManager()) { Collection <PSObject> databases, database1, database2; using (new MockHttpServer(exceptionManager, MockHttpServer.DefaultHttpsServerPrefixUri, testSession)) { databases = powershell.InvokeBatchScript( @"Get-AzureSqlDatabase -RestorableDropped -ServerName $serverName"); } Assert.AreEqual(0, powershell.Streams.Error.Count, "Errors during run!"); Assert.AreEqual(0, powershell.Streams.Warning.Count, "Warnings during run!"); powershell.Streams.ClearStreams(); // Expecting testdb1, testdb2, possibly dropped databases from previous runs Assert.IsTrue( databases.Count >= 2, "Expecting at-least two RestorableDroppedDatabase objects"); Assert.IsTrue( databases[0].BaseObject is RestorableDroppedDatabase, "Expecting a RestorableDroppedDatabase object"); Assert.IsTrue( databases[1].BaseObject is RestorableDroppedDatabase, "Expecting a RestorableDroppedDatabase object"); var database1Object = (RestorableDroppedDatabase)databases[0].BaseObject; var database1DeletionDate = database1Object.DeletionDate.ToUniversalTime().ToString(deletionDateStringFormat); var database2Object = (RestorableDroppedDatabase)databases[1].BaseObject; var database2DeletionDate = database2Object.DeletionDate.ToUniversalTime().ToString(deletionDateStringFormat); using (new MockHttpServer( exceptionManager, MockHttpServer.DefaultHttpsServerPrefixUri, testSession)) { database1 = powershell.InvokeBatchScript( @"Get-AzureSqlDatabase -RestorableDropped " + @"-ServerName $serverName " + @"-DatabaseName " + database1Object.Name + @" " + @"-DatabaseDeletionDate " + database1DeletionDate); database2 = powershell.InvokeBatchScript( @"Get-AzureSqlDatabase -RestorableDropped " + @"-ServerName $serverName " + @"-DatabaseName " + database2Object.Name + @" " + @"-DatabaseDeletionDate " + database2DeletionDate); } Assert.IsTrue( database1.Single().BaseObject is RestorableDroppedDatabase, "Expecting a RestorableDroppedDatabase object"); var refreshedDatabase1Object = (RestorableDroppedDatabase)database1.Single().BaseObject; Assert.AreEqual( database1Object.Name, refreshedDatabase1Object.Name, "Expected db name to be " + database1Object.Name); Assert.IsTrue( database2.Single().BaseObject is RestorableDroppedDatabase, "Expecting a RestorableDroppedDatabase object"); var refreshedDatabase2Object = (RestorableDroppedDatabase)database2.Single().BaseObject; Assert.AreEqual( database2Object.Name, refreshedDatabase2Object.Name, "Expected db name to be " + database2Object.Name); Assert.AreEqual( database2Object.Edition, refreshedDatabase2Object.Edition, "Expected edition to be " + database2Object.Edition); Assert.AreEqual( database2Object.MaxSizeBytes, refreshedDatabase2Object.MaxSizeBytes, "Expected max size to be " + database2Object.MaxSizeBytes); } }
public void GetAzureSqlDatabaseServerQuotaSqlAuthTest() { using (System.Management.Automation.PowerShell powershell = System.Management.Automation.PowerShell.Create()) { // Create a context NewAzureSqlDatabaseServerContextTests.CreateServerContextSqlAuth( powershell, "$context"); // Issue another create testdb1, causing a failure HttpSession testSession = MockServerHelper.DefaultSessionCollection.GetSession( "UnitTest.GetAzureSqlDatabaseServerQuotaSqlAuthTest"); DatabaseTestHelper.SetDefaultTestSessionSettings(testSession); testSession.RequestValidator = new Action <HttpMessage, HttpMessage.Request>( (expected, actual) => { Assert.AreEqual(expected.RequestInfo.Method, actual.Method); Assert.IsNotNull(actual.UserAgent); switch (expected.Index) { // Request 0-1: Create testdb1 case 0: case 1: DatabaseTestHelper.ValidateHeadersForODataRequest( expected.RequestInfo, actual); break; default: Assert.Fail("No more requests expected."); break; } }); using (AsyncExceptionManager exceptionManager = new AsyncExceptionManager()) { Services.Server.ServerDataServiceSqlAuth context; using (new MockHttpServer( exceptionManager, MockHttpServer.DefaultServerPrefixUri, testSession)) { Collection <PSObject> ctxPsObject = powershell.InvokeBatchScript("$context"); context = (Services.Server.ServerDataServiceSqlAuth)ctxPsObject.First().BaseObject; Collection <PSObject> q1, q2; q1 = powershell.InvokeBatchScript( @"$context | Get-AzureSqlDatabaseServerQuota"); q2 = powershell.InvokeBatchScript( @"$context | Get-AzureSqlDatabaseServerQuota -QuotaName ""Premium_Databases"""); ServerQuota quota1 = q1.FirstOrDefault().BaseObject as ServerQuota; ServerQuota quota2 = q2.FirstOrDefault().BaseObject as ServerQuota; Assert.AreEqual( "premium_databases", quota1.Name, "Unexpected quota name"); Assert.AreEqual( "premium_databases", quota2.Name, "Unexpected quota name"); } } Assert.AreEqual(0, powershell.Streams.Error.Count, "There were errors while running the tests!"); Assert.AreEqual(0, powershell.Streams.Warning.Count, "There were warnings while running the tests!"); } }
public void AzureSqlDatabaseServerFirewallTests() { // This test uses the https endpoint, setup the certificates. MockHttpServer.SetupCertificates(); using (System.Management.Automation.PowerShell powershell = System.Management.Automation.PowerShell.Create()) { // Setup the subscription used for the test AzureSubscription subscription = UnitTestHelper.SetupUnitTestSubscription(powershell); powershell.Runspace.SessionStateProxy.SetVariable( "serverName", SqlDatabaseTestSettings.Instance.ServerName); // Create a new server HttpSession testSession = MockServerHelper.DefaultSessionCollection.GetSession( "UnitTest.AzureSqlDatabaseServerFirewallTests"); ServerTestHelper.SetDefaultTestSessionSettings(testSession); testSession.RequestValidator = new Action <HttpMessage, HttpMessage.Request>( (expected, actual) => { Assert.AreEqual(expected.RequestInfo.Method, actual.Method); Assert.IsTrue( actual.UserAgent.Contains(ApiConstants.UserAgentHeaderValue), "Missing proper UserAgent string."); }); Collection <PSObject> newFirewallRuleResult1 = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { return(powershell.InvokeBatchScript( @"New-AzureSqlDatabaseServerFirewallRule" + @" -ServerName $serverName" + @" -RuleName Rule1" + @" -StartIpAddress 0.0.0.0" + @" -EndIpAddress 1.1.1.1")); }); Collection <PSObject> newFirewallRuleResult2 = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { return(powershell.InvokeBatchScript( @"New-AzureSqlDatabaseServerFirewallRule" + @" -ServerName $serverName" + @" -RuleName Rule2" + @" -StartIpAddress 1.1.1.1" + @" -EndIpAddress 2.2.2.2")); }); Collection <PSObject> getFirewallRuleResult = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { return(powershell.InvokeBatchScript( @"Get-AzureSqlDatabaseServerFirewallRule $serverName")); }); Collection <PSObject> setFirewallResult = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { powershell.Runspace.SessionStateProxy.SetVariable("fw", newFirewallRuleResult1); powershell.InvokeBatchScript( @"$fw | Set-AzureSqlDatabaseServerFirewallRule" + @" -StartIpAddress 2.2.2.2" + @" -EndIpAddress 3.3.3.3"); return(powershell.InvokeBatchScript( @"$fw | Get-AzureSqlDatabaseServerFirewallRule")); }); Collection <PSObject> removeFirewallResult = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { powershell.Runspace.SessionStateProxy.SetVariable("rules", getFirewallRuleResult); powershell.InvokeBatchScript( @"$rules | Remove-AzureSqlDatabaseServerFirewallRule" + @" -Force"); return(powershell.InvokeBatchScript( @"Get-AzureSqlDatabaseServerFirewallRule $serverName")); }); Assert.AreEqual(0, powershell.Streams.Error.Count, "Unexpected Errors during run!"); Assert.AreEqual(0, powershell.Streams.Warning.Count, "Unexpected Warnings during run!"); // Validate New- and Get-AzureSqlDatabaseServerFirewallRule SqlDatabaseServerFirewallRuleContext[] firewallRules = getFirewallRuleResult .Select(r => r.BaseObject as SqlDatabaseServerFirewallRuleContext) .ToArray(); Assert.AreEqual(2, firewallRules.Length, "Expecting two firewall rules"); Assert.IsNotNull(firewallRules[0], "Expecting a SqlDatabaseServerFirewallRuleContext object."); Assert.IsNotNull(firewallRules[1], "Expecting a SqlDatabaseServerFirewallRuleContext object."); Assert.AreEqual(SqlDatabaseTestSettings.Instance.ServerName, firewallRules[0].ServerName); Assert.AreEqual("Rule1", firewallRules[0].RuleName); Assert.AreEqual("0.0.0.0", firewallRules[0].StartIpAddress); Assert.AreEqual("1.1.1.1", firewallRules[0].EndIpAddress); Assert.AreEqual("Success", firewallRules[0].OperationStatus); Assert.AreEqual(SqlDatabaseTestSettings.Instance.ServerName, firewallRules[1].ServerName); Assert.AreEqual("Rule2", firewallRules[1].RuleName); Assert.AreEqual("1.1.1.1", firewallRules[1].StartIpAddress); Assert.AreEqual("2.2.2.2", firewallRules[1].EndIpAddress); Assert.AreEqual("Success", firewallRules[1].OperationStatus); // Validate Set-AzureSqlDatabaseServerFirewallRule SqlDatabaseServerFirewallRuleContext firewallRule = setFirewallResult.Single().BaseObject as SqlDatabaseServerFirewallRuleContext; Assert.IsNotNull(firewallRule, "Expecting a SqlDatabaseServerFirewallRuleContext object"); Assert.AreEqual(SqlDatabaseTestSettings.Instance.ServerName, firewallRule.ServerName); Assert.AreEqual("Rule1", firewallRule.RuleName); Assert.AreEqual("2.2.2.2", firewallRule.StartIpAddress); Assert.AreEqual("3.3.3.3", firewallRule.EndIpAddress); Assert.AreEqual("Success", firewallRule.OperationStatus); // Validate Remove-AzureSqlDatabaseServerFirewallRule Assert.AreEqual(0, removeFirewallResult.Count, "Expect all firewall rules are dropped."); } }
public void GetAzureSqlDatabaseWithSqlAuthByPipe() { using (System.Management.Automation.PowerShell powershell = System.Management.Automation.PowerShell.Create()) { // Create a context NewAzureSqlDatabaseServerContextTests.CreateServerContextSqlAuth( powershell, "$context"); // Query the created test databases HttpSession testSession = MockServerHelper.DefaultSessionCollection.GetSession( "UnitTests.GetAzureSqlDatabaseWithSqlAuthByPipe"); DatabaseTestHelper.SetDefaultTestSessionSettings(testSession); testSession.RequestValidator = new Action <HttpMessage, HttpMessage.Request>( (expected, actual) => { Assert.AreEqual(expected.RequestInfo.Method, actual.Method); Assert.AreEqual(expected.RequestInfo.UserAgent, actual.UserAgent); if (expected.Index < 12) { // Request 0-3: Get all databases + ServiceObjectives requests // Request 4-11: 4 Get databases, 2 requests per get call DatabaseTestHelper.ValidateHeadersForODataRequest( expected.RequestInfo, actual); } else { Assert.Fail("No more requests expected."); } }); using (AsyncExceptionManager exceptionManager = new AsyncExceptionManager()) { Collection <PSObject> databases, database1, database2; using (new MockHttpServer( exceptionManager, MockHttpServer.DefaultServerPrefixUri, testSession)) { databases = powershell.InvokeBatchScript( @"Get-AzureSqlDatabase " + @"-Context $context"); powershell.InvokeBatchScript( @"$testdb1 = Get-AzureSqlDatabase $context -DatabaseName testdb1"); powershell.InvokeBatchScript( @"$testdb2 = Get-AzureSqlDatabase $context -DatabaseName testdb2"); database1 = powershell.InvokeBatchScript( @"$testdb1 | Get-AzureSqlDatabase"); database2 = powershell.InvokeBatchScript( @"$testdb2 | Get-AzureSqlDatabase"); } Assert.AreEqual(0, powershell.Streams.Error.Count, "Errors during run!"); Assert.AreEqual(0, powershell.Streams.Warning.Count, "Warnings during run!"); powershell.Streams.ClearStreams(); // Expecting master, testdb1, testdb2 Assert.AreEqual( 3, databases.Count, "Expecting three Database objects"); Services.Server.Database database1Obj = database1.Single().BaseObject as Services.Server.Database; Assert.IsNotNull(database1Obj, "Expecting a Database object"); DatabaseTestHelper.ValidateDatabaseProperties(database1Obj, "testdb1", "Web", 1, 1073741824L, "SQL_Latin1_General_CP1_CI_AS", "Shared", false, DatabaseTestHelper.SharedSloGuid); Services.Server.Database database2Obj = database2.Single().BaseObject as Services.Server.Database; Assert.IsNotNull(database2Obj, "Expecting a Database object"); DatabaseTestHelper.ValidateDatabaseProperties(database2Obj, "testdb2", "Web", 5, 5368709120L, "Japanese_CI_AS", "Shared", false, DatabaseTestHelper.SharedSloGuid); } } }
public void SetAzureSqlDatabaseServiceObjectiveWithSqlAuth() { using (System.Management.Automation.PowerShell powershell = System.Management.Automation.PowerShell.Create()) { // Create a context NewAzureSqlDatabaseServerContextTests.CreateServerContextSqlAuth( powershell, "$context"); HttpSession testSession = DatabaseTestHelper.DefaultSessionCollection.GetSession( "UnitTests.SetAzureSqlDatabaseServiceObjectiveWithSqlAuth"); DatabaseTestHelper.SetDefaultTestSessionSettings(testSession); testSession.RequestValidator = new Action <HttpMessage, HttpMessage.Request>( (expected, actual) => { Assert.AreEqual(expected.RequestInfo.Method, actual.Method); Assert.AreEqual(expected.RequestInfo.UserAgent, actual.UserAgent); switch (expected.Index) { // Request 0-1: Get Service Objective case 0: case 1: // Request 2-7: Get/Update/Re-Get testdb2 case 2: case 3: case 4: case 5: case 6: DatabaseTestHelper.ValidateHeadersForODataRequest( expected.RequestInfo, actual); break; default: Assert.Fail("No more requests expected."); break; } }); using (AsyncExceptionManager exceptionManager = new AsyncExceptionManager()) { // Create context with both ManageUrl and ServerName overriden Collection <PSObject> database; using (new MockHttpServer( exceptionManager, MockHttpServer.DefaultServerPrefixUri, testSession)) { powershell.InvokeBatchScript( @"$slo = Get-AzureSqlDatabaseServiceObjective " + @"-Context $context " + @"-ServiceObjectiveName ""Reserved P1"""); database = powershell.InvokeBatchScript( @"Set-AzureSqlDatabase " + @"-Context $context " + @"-DatabaseName testdb2 " + @"-ServiceObjective $slo " + @"-Force " + @"-PassThru"); } Assert.AreEqual(0, powershell.Streams.Error.Count, "Errors during run!"); Assert.AreEqual(0, powershell.Streams.Warning.Count, "Warnings during run!"); powershell.Streams.ClearStreams(); Assert.IsTrue( database.Single().BaseObject is Services.Server.Database, "Expecting a Database object"); Services.Server.Database databaseObj = (Services.Server.Database)database.Single().BaseObject; databaseObj = (Services.Server.Database)database.Single().BaseObject; Assert.AreEqual("testdb2", databaseObj.Name, "Expected db name to be testdb2"); Assert.AreEqual((byte)0, databaseObj.ServiceObjectiveAssignmentState, "Expected assignment state to be pending"); //Assert.AreEqual("Reserved P1", databaseObj.ServiceObjective.Name, "Expected Reserved P1"); //Assert.AreEqual("Reserved P1", databaseObj.ServiceObjectiveName, "Expected Reserved P1"); } } }
public void SetAzureSqlDatabaseSizeWithSqlAuth() { using (System.Management.Automation.PowerShell powershell = System.Management.Automation.PowerShell.Create()) { // Create a context NewAzureSqlDatabaseServerContextTests.CreateServerContextSqlAuth( powershell, "$context"); // Create 2 test databases NewAzureSqlDatabaseTests.CreateTestDatabasesWithSqlAuth( powershell, "$context"); HttpSession testSession = DatabaseTestHelper.DefaultSessionCollection.GetSession( "UnitTests.SetAzureSqlDatabaseSizeWithSqlAuth"); DatabaseTestHelper.SetDefaultTestSessionSettings(testSession); testSession.RequestValidator = new Action <HttpMessage, HttpMessage.Request>( (expected, actual) => { Assert.AreEqual(expected.RequestInfo.Method, actual.Method); Assert.AreEqual(expected.RequestInfo.UserAgent, actual.UserAgent); switch (expected.Index) { // Request 0-1: Set testdb1 with new MaxSize case 0: case 1: // Request 2: Get updated testdb1 case 2: DatabaseTestHelper.ValidateHeadersForODataRequest( expected.RequestInfo, actual); break; default: Assert.Fail("No more requests expected."); break; } }); using (AsyncExceptionManager exceptionManager = new AsyncExceptionManager()) { // Create context with both ManageUrl and ServerName overriden Collection <PSObject> database; using (new MockHttpServer( exceptionManager, MockHttpServer.DefaultServerPrefixUri, testSession)) { database = powershell.InvokeBatchScript( @"Set-AzureSqlDatabase " + @"-Context $context " + @"-DatabaseName testdb1 " + @"-MaxSizeGB 5 " + @"-Force " + @"-PassThru"); } Assert.AreEqual(0, powershell.Streams.Error.Count, "Errors during run!"); Assert.AreEqual(0, powershell.Streams.Warning.Count, "Warnings during run!"); powershell.Streams.ClearStreams(); Assert.IsTrue( database.Single().BaseObject is Services.Server.Database, "Expecting a Database object"); Services.Server.Database databaseObj = (Services.Server.Database)database.Single().BaseObject; Assert.AreEqual("testdb1", databaseObj.Name, "Expected db name to be testdb1"); Assert.AreEqual("Web", databaseObj.Edition, "Expected edition to be Web"); Assert.AreEqual(5, databaseObj.MaxSizeGB, "Expected max size to be 5 GB"); } } }
public void GetAzureSqlDatabaseWithSqlAuth() { using (System.Management.Automation.PowerShell powershell = System.Management.Automation.PowerShell.Create()) { // Create a context NewAzureSqlDatabaseServerContextTests.CreateServerContextSqlAuth( powershell, "$context"); // Query the created test databases HttpSession testSession = MockServerHelper.DefaultSessionCollection.GetSession( "UnitTests.GetAzureSqlDatabaseWithSqlAuth"); DatabaseTestHelper.SetDefaultTestSessionSettings(testSession); testSession.RequestValidator = new Action <HttpMessage, HttpMessage.Request>( (expected, actual) => { Assert.AreEqual(expected.RequestInfo.Method, actual.Method); Assert.AreEqual(expected.RequestInfo.UserAgent, actual.UserAgent); // 0 - 5 // Get all databases + ServiceObjective lookup // 6 - 11 // get database requests, 2 requests per get. if (expected.Index > 11) { Assert.Fail("No More Requests Expected"); } else { DatabaseTestHelper.ValidateHeadersForODataRequest( expected.RequestInfo, actual); } }); using (AsyncExceptionManager exceptionManager = new AsyncExceptionManager()) { // Retrieve all databases then each individual ones Collection <PSObject> databases, database1, database2, database3; using (new MockHttpServer( exceptionManager, MockHttpServer.DefaultServerPrefixUri, testSession)) { databases = powershell.InvokeBatchScript( @"Get-AzureSqlDatabase " + @"-Context $context"); database1 = powershell.InvokeBatchScript( @"Get-AzureSqlDatabase " + @"-Context $context " + @"-DatabaseName testdb1"); database2 = powershell.InvokeBatchScript( @"Get-AzureSqlDatabase " + @"-Context $context " + @"-DatabaseName testdb2"); database3 = powershell.InvokeBatchScript( @"Get-AzureSqlDatabase " + @"-Context $context " + @"-DatabaseName testdb3"); } Assert.AreEqual(0, powershell.Streams.Error.Count, "Errors during run!"); Assert.AreEqual(0, powershell.Streams.Warning.Count, "Warnings during run!"); powershell.Streams.ClearStreams(); // Expecting master, testdb1, testdb2, testdb3 Assert.AreEqual(4, databases.Count, "Expecting four Database objects"); Services.Server.Database database = database1.Single().BaseObject as Services.Server.Database; Assert.IsNotNull(database, "Expecting a Database object"); DatabaseTestHelper.ValidateDatabaseProperties(database, "testdb1", "Web", 1, 1073741824L, "SQL_Latin1_General_CP1_CI_AS", "Shared", false, DatabaseTestHelper.SharedSloGuid); database = database2.Single().BaseObject as Services.Server.Database; Assert.IsTrue(database != null, "Expecting a Database object"); DatabaseTestHelper.ValidateDatabaseProperties(database, "testdb2", "Web", 5, 5368709120L, "Japanese_CI_AS", "Shared", false, DatabaseTestHelper.SharedSloGuid); database = database3.Single().BaseObject as Services.Server.Database; Assert.IsTrue(database != null, "Expecting a Database object"); DatabaseTestHelper.ValidateDatabaseProperties(database, "testdb3", "Web", 0, 104857600L, "SQL_Latin1_General_CP1_CI_AS", "Shared", false, DatabaseTestHelper.SharedSloGuid); } } }
public void AzureSqlDatabaseServerV2Tests() { // This test uses the https endpoint, setup the certificates. MockHttpServer.SetupCertificates(); using (System.Management.Automation.PowerShell powershell = System.Management.Automation.PowerShell.Create()) { // Setup the subscription used for the test AzureSubscription subscription = UnitTestHelper.SetupUnitTestSubscription(powershell); // Create a new V2 server HttpSession testSession = MockServerHelper.DefaultSessionCollection.GetSession( "UnitTest.AzureSqlDatabaseServerV2Tests"); ServerTestHelper.SetDefaultTestSessionSettings(testSession); testSession.RequestValidator = new Action <HttpMessage, HttpMessage.Request>( (expected, actual) => { Assert.AreEqual(expected.RequestInfo.Method, actual.Method); Assert.IsTrue( actual.UserAgent.Contains(ApiConstants.UserAgentHeaderValue), "Missing proper UserAgent string."); }); powershell.Runspace.SessionStateProxy.SetVariable("login", "mylogin"); powershell.Runspace.SessionStateProxy.SetVariable("password", "Pa$$w0rd!"); powershell.Runspace.SessionStateProxy.SetVariable("location", "East Asia"); Collection <PSObject> newServerResult = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { return(powershell.InvokeBatchScript( @"New-AzureSqlDatabaseServer" + @" -AdministratorLogin $login" + @" -AdministratorLoginPassword $password" + @" -Location $location" + @" -Version 2")); }); Collection <PSObject> getServerResult = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { powershell.Runspace.SessionStateProxy.SetVariable("server", newServerResult); return(powershell.InvokeBatchScript( @"Get-AzureSqlDatabaseServer $server.ServerName")); }); Collection <PSObject> removeServerResult = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { powershell.Runspace.SessionStateProxy.SetVariable("server", newServerResult); powershell.InvokeBatchScript( @"$server | Remove-AzureSqlDatabaseServer" + @" -Force"); return(powershell.InvokeBatchScript( @"Get-AzureSqlDatabaseServer")); }); Assert.AreEqual(0, powershell.Streams.Error.Count, "Unexpected Errors during run!"); Assert.AreEqual(0, powershell.Streams.Warning.Count, "Unexpected Warnings during run!"); // Validate New-AzureSqlDatabaseServer results SqlDatabaseServerContext server = newServerResult.Single().BaseObject as SqlDatabaseServerContext; Assert.IsNotNull(server, "Expecting a SqlDatabaseServerContext object"); VerifyServer( server, (string)powershell.Runspace.SessionStateProxy.GetVariable("login"), (string)powershell.Runspace.SessionStateProxy.GetVariable("location"), ServerVersion2, "Ready"); // Validate Get-AzureSqlDatabaseServer results server = getServerResult.Single().BaseObject as SqlDatabaseServerContext; Assert.IsNotNull(server, "Expecting a SqlDatabaseServerContext object"); VerifyServer( server, (string)powershell.Runspace.SessionStateProxy.GetVariable("login"), (string)powershell.Runspace.SessionStateProxy.GetVariable("location"), ServerVersion2, "Ready"); powershell.Streams.ClearStreams(); } }
public void CreatePremiumDatabasesWithSqlAuth() { using (System.Management.Automation.PowerShell powershell = System.Management.Automation.PowerShell.Create()) { NewAzureSqlDatabaseServerContextTests.CreateServerContextSqlAuth( powershell, "$context"); HttpSession testSession = MockServerHelper.DefaultSessionCollection.GetSession( "UnitTest.Common.CreatePremiumDatabasesWithSqlAuth"); DatabaseTestHelper.SetDefaultTestSessionSettings(testSession); testSession.RequestValidator = new Action <HttpMessage, HttpMessage.Request>( (expected, actual) => { Assert.AreEqual(expected.RequestInfo.Method, actual.Method); Assert.AreEqual(expected.RequestInfo.UserAgent, actual.UserAgent); switch (expected.Index) { // Request 0-6: Query P1 and P2 Service Objective case 0: case 1: case 2: case 3: case 4: case 5: case 6: // Request 7-9: Create NewAzureSqlPremiumDatabaseTests_P1 case 7: case 8: case 9: // Request 10-12: Create NewAzureSqlPremiumDatabaseTests_P2 case 10: case 11: case 12: DatabaseTestHelper.ValidateHeadersForODataRequest( expected.RequestInfo, actual); break; default: Assert.Fail("No more requests expected."); break; } }); using (AsyncExceptionManager exceptionManager = new AsyncExceptionManager()) { Collection <PSObject> premiumDB_P1, PremiumDB_P2; using (new MockHttpServer( exceptionManager, MockHttpServer.DefaultServerPrefixUri, testSession)) { powershell.InvokeBatchScript( @"$P1 = Get-AzureSqlDatabaseServiceObjective" + @" -Context $context" + @" -ServiceObjectiveName ""Reserved P1"""); powershell.InvokeBatchScript( @"$P2 = Get-AzureSqlDatabaseServiceObjective " + @"-Context $context" + @" -ServiceObjectiveName ""Reserved P2"""); premiumDB_P1 = powershell.InvokeBatchScript( @"$premiumDB_P1 = New-AzureSqlDatabase " + @"-Context $context " + @"-DatabaseName NewAzureSqlPremiumDatabaseTests_P1 " + @"-Edition Premium " + @"-ServiceObjective $P1 "); premiumDB_P1 = powershell.InvokeBatchScript("$PremiumDB_P1"); powershell.InvokeBatchScript( @"$PremiumDB_P2 = New-AzureSqlDatabase " + @"-Context $context " + @"-DatabaseName NewAzureSqlPremiumDatabaseTests_P2 " + @"-Collation Japanese_CI_AS " + @"-Edition Premium " + @"-ServiceObjective $P2 " + @"-MaxSizeGB 10 " + @"-Force"); PremiumDB_P2 = powershell.InvokeBatchScript("$PremiumDB_P2"); } Assert.AreEqual(0, powershell.Streams.Error.Count, "Errors during run!"); Assert.AreEqual(0, powershell.Streams.Warning.Count, "Warnings during run!"); powershell.Streams.ClearStreams(); Assert.IsTrue( premiumDB_P1.Single().BaseObject is Services.Server.Database, "Expecting a Database object"); Services.Server.Database databaseP1 = (Services.Server.Database)premiumDB_P1.Single().BaseObject; Assert.AreEqual("NewAzureSqlPremiumDatabaseTests_P1", databaseP1.Name, "Expected db name to be NewAzureSqlPremiumDatabaseTests_P1"); Assert.IsTrue( PremiumDB_P2.Single().BaseObject is Services.Server.Database, "Expecting a Database object"); Services.Server.Database databaseP2 = (Services.Server.Database)PremiumDB_P2.Single().BaseObject; Assert.AreEqual("NewAzureSqlPremiumDatabaseTests_P2", databaseP2.Name, "Expected db name to be NewAzureSqlPremiumDatabaseTests_P2"); Assert.AreEqual( "Japanese_CI_AS", databaseP2.CollationName, "Expected collation to be Japanese_CI_AS"); /* SQL Server: Defect 1655888: When creating a premium database, * the immediate returned value do not have valid Edition and Max Database Size info * We should active the following asserts once the defect is fixed. * Assert.AreEqual("Premium", database2Obj.Edition, "Expected edition to be Premium"); * Assert.AreEqual(10, database2Obj.MaxSizeGB, "Expected max size to be 10 GB"); */ } } }
public void AzureSqlDatabaseServerTests() { // This test uses the https endpoint, setup the certificates. MockHttpServer.SetupCertificates(); SqlTestPsHost host = new SqlTestPsHost(); SqlCustomPsHostUserInterface ui = host.UI as SqlCustomPsHostUserInterface; using (Runspace space = RunspaceFactory.CreateRunspace(host)) { space.Open(); using (System.Management.Automation.PowerShell powershell = System.Management.Automation.PowerShell.Create()) { powershell.Runspace = space; // Setup the subscription used for the test AzureSubscription subscription = UnitTestHelper.SetupUnitTestSubscription(powershell); // Create a new server HttpSession testSession = MockServerHelper.DefaultSessionCollection.GetSession( "UnitTest.AzureSqlDatabaseServerTests"); ServerTestHelper.SetDefaultTestSessionSettings(testSession); testSession.RequestValidator = new Action <HttpMessage, HttpMessage.Request>( (expected, actual) => { Assert.AreEqual(expected.RequestInfo.Method, actual.Method); Assert.IsTrue( actual.UserAgent.Contains(ApiConstants.UserAgentHeaderValue), "Missing proper UserAgent string."); }); powershell.Runspace.SessionStateProxy.SetVariable("login", "mylogin"); powershell.Runspace.SessionStateProxy.SetVariable("password", "Pa$$w0rd!"); powershell.Runspace.SessionStateProxy.SetVariable("location", "East Asia"); Collection <PSObject> newServerResult = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { return(powershell.InvokeBatchScript( @"New-AzureSqlDatabaseServer" + @" -AdministratorLogin $login" + @" -AdministratorLoginPassword $password" + @" -Location $location")); }); ui.PromptInputs = new PSObject[] { "mylogin", "Pa$$w0rd", "East Asia" }; Collection <PSObject> newServerResult2 = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { return(powershell.InvokeBatchScript(@"New-AzureSqlDatabaseServer")); }); ui.PromptInputs = null; Collection <PSObject> getServerResult = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { powershell.Runspace.SessionStateProxy.SetVariable("server", newServerResult); return(powershell.InvokeBatchScript( @"Get-AzureSqlDatabaseServer $server.ServerName")); }); Collection <PSObject> setServerResult = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { powershell.Runspace.SessionStateProxy.SetVariable("server", newServerResult); powershell.Runspace.SessionStateProxy.SetVariable("password", "Pa$$w0rd2"); powershell.InvokeBatchScript( @"$server | Set-AzureSqlDatabaseServer" + @" -AdminPassword $password" + @" -Force"); return(powershell.InvokeBatchScript( @"$server | Get-AzureSqlDatabaseServer")); }); ui.PromptInputs = new PSObject[] { "Pa$$w0rd2" }; Collection <PSObject> setServerResult2 = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { powershell.Runspace.SessionStateProxy.SetVariable("server", newServerResult2); powershell.InvokeBatchScript(@"$server | Set-AzureSqlDatabaseServer"); return(powershell.InvokeBatchScript(@"$server | Get-AzureSqlDatabaseServer")); }); ui.PromptInputs = null; Collection <PSObject> removeServerResult = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { powershell.Runspace.SessionStateProxy.SetVariable("server", newServerResult); powershell.InvokeBatchScript( @"$server | Remove-AzureSqlDatabaseServer" + @" -Force"); return(powershell.InvokeBatchScript( @"Get-AzureSqlDatabaseServer")); }); ui.PromptInputs = new PSObject[] { ((SqlDatabaseServerContext)newServerResult2[0].BaseObject).ServerName }; ui.PromptForChoiceInputIndex = 0; //answer yes to delete database prompt Collection <PSObject> removeServerResult2 = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { powershell.InvokeBatchScript(@"Remove-AzureSqlDatabaseServer"); return(powershell.InvokeBatchScript( @"Get-AzureSqlDatabaseServer")); }); ui.PromptForChoiceInputIndex = -1; ui.PromptInputs = null; Assert.AreEqual(0, powershell.Streams.Error.Count, "Unexpected Errors during run!"); Assert.AreEqual(0, powershell.Streams.Warning.Count, "Unexpected Warnings during run!"); // Validate New-AzureSqlDatabaseServer results SqlDatabaseServerContext server = newServerResult.Single().BaseObject as SqlDatabaseServerContext; Assert.IsNotNull(server, "Expecting a SqlDatabaseServerContext object"); VerifyServer( server, (string)powershell.Runspace.SessionStateProxy.GetVariable("login"), (string)powershell.Runspace.SessionStateProxy.GetVariable("location"), ServerVersion12, "Ready"); SqlDatabaseServerContext server2 = newServerResult2.Single().BaseObject as SqlDatabaseServerContext; Assert.IsNotNull(server2, "Expecting a SqlDatabaseServerContext object"); VerifyServer( server, (string)powershell.Runspace.SessionStateProxy.GetVariable("login"), (string)powershell.Runspace.SessionStateProxy.GetVariable("location"), ServerVersion12, "Ready"); // Validate Get-AzureSqlDatabaseServer results server = getServerResult.Single().BaseObject as SqlDatabaseServerContext; Assert.IsNotNull(server, "Expecting a SqlDatabaseServerContext object"); VerifyServer( server, (string)powershell.Runspace.SessionStateProxy.GetVariable("login"), (string)powershell.Runspace.SessionStateProxy.GetVariable("location"), ServerVersion12, "Ready"); server = setServerResult.Single().BaseObject as SqlDatabaseServerContext; Assert.IsNotNull(server, "Expecting a SqlDatabaseServerContext object"); VerifyServer( server, (string)powershell.Runspace.SessionStateProxy.GetVariable("login"), (string)powershell.Runspace.SessionStateProxy.GetVariable("location"), ServerVersion12, "Ready"); server2 = setServerResult2.Single().BaseObject as SqlDatabaseServerContext; Assert.IsNotNull(server, "Expecting a SqlDatabaseServerContext object"); VerifyServer( server2, (string)powershell.Runspace.SessionStateProxy.GetVariable("login"), (string)powershell.Runspace.SessionStateProxy.GetVariable("location"), ServerVersion12, "Ready"); // Validate Remove-AzureSqlDatabaseServer results Assert.IsFalse( removeServerResult.Any((o) => o.GetVariableValue <string>("ServerName") == server.ServerName), "Server should have been removed."); Assert.IsFalse( removeServerResult2.Any((o) => o.GetVariableValue <string>("ServerName") == server2.ServerName), "Server 2 should have been removed."); powershell.Streams.ClearStreams(); } space.Close(); } }
public void ImportExportAzureSqlDatabaseTests() { if (AzureRmProfileProvider.Instance != null) { AzureRmProfileProvider.Instance.Profile = null; } using (System.Management.Automation.PowerShell powershell = System.Management.Automation.PowerShell.Create()) { // Setup the subscription used for the test AzureSubscription subscription = UnitTestHelper.SetupUnitTestSubscription(powershell); // Set the necessary session variables powershell.Runspace.SessionStateProxy.SetVariable( "serverName", SqlDatabaseTestSettings.Instance.ServerName); powershell.Runspace.SessionStateProxy.SetVariable( "sourceDB", SqlDatabaseTestSettings.Instance.SourceDatabaseName); powershell.Runspace.SessionStateProxy.SetVariable( "targetDB", SqlDatabaseTestSettings.Instance.TargetDatabaseName); powershell.Runspace.SessionStateProxy.SetVariable( "credential", new PSCredential( SqlDatabaseTestSettings.Instance.UserName, SqlDatabaseTestSettings.Instance.SecurePassword)); powershell.Runspace.SessionStateProxy.SetVariable( "storageAccountName", SqlDatabaseTestSettings.Instance.StorageName); powershell.Runspace.SessionStateProxy.SetVariable( "storageAccountKey", SqlDatabaseTestSettings.Instance.AccessKey); powershell.Runspace.SessionStateProxy.SetVariable( "storageContainerName", SqlDatabaseTestSettings.Instance.ContainerName); // Create a new server HttpSession testSession = MockServerHelper.DefaultSessionCollection.GetSession( "UnitTest.ImportExportAzureSqlDatabaseTests"); ServerTestHelper.SetDefaultTestSessionSettings(testSession); //testSession.ServiceBaseUri = new Uri("https://management.core.windows.net"); testSession.RequestValidator = new Action <HttpMessage, HttpMessage.Request>( (expected, actual) => { Assert.AreEqual(expected.RequestInfo.Method, actual.Method); string expectedRequestText = RequestTextToString(expected.RequestInfo); string actualRequestText = RequestTextToString(actual); // When checking out from GitHub, different new line setting may lead to different char \r\n or \n // Replace them with String.Empty before comparison Assert.AreEqual( Regex.Replace(expectedRequestText, @"\s+", String.Empty), Regex.Replace(actualRequestText, @"\s+", String.Empty)); Assert.IsTrue( actual.UserAgent.Contains(ApiConstants.UserAgentHeaderValue), "Missing proper UserAgent string."); }); PSObject storageContext = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { return(powershell.InvokeBatchScript( @"New-AzureStorageContext" + @" -StorageAccountName $storageAccountName" + @" -StorageAccountKey $storageAccountKey")); }).FirstOrDefault(); // Tell the sql auth factory to create a v2 context (skip checking sql version using select query). // SqlAuthContextFactory.sqlVersion = SqlAuthContextFactory.SqlVersion.v2; //testSession.ServiceBaseUri = new Uri("https://lqtqbo6kkp.database.windows.net"); Collection <PSObject> databaseContext = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultServerPrefixUri, () => { powershell.Runspace.SessionStateProxy.SetVariable( "manageUrl", MockHttpServer.DefaultServerPrefixUri.AbsoluteUri); return(powershell.InvokeBatchScript( @"New-AzureSqlDatabaseServerContext" + @" -ServerName $serverName" + @" -ManageUrl $manageUrl" + @" -Credential $credential")); }); //testSession.ServiceBaseUri = new Uri("https://management.core.windows.net"); Collection <PSObject> startExportResult = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { powershell.Runspace.SessionStateProxy.SetVariable("storageContext", storageContext); powershell.Runspace.SessionStateProxy.SetVariable( "databaseContext", databaseContext.FirstOrDefault()); return(powershell.InvokeBatchScript( @"Start-AzureSqlDatabaseExport" + @" -SqlConnectionContext $databaseContext" + @" -DatabaseName $sourceDB" + @" -StorageContext $storageContext" + @" -StorageContainerName $storageContainerName" + @" -BlobName backup1")); }); Collection <PSObject> getExportStatusResult = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { powershell.Runspace.SessionStateProxy.SetVariable( "exportResult", startExportResult.FirstOrDefault()); return(powershell.InvokeBatchScript( @"Get-AzureSqlDatabaseImportExportStatus" + @" -ServerName $serverName" + @" -RequestId $exportResult.RequestGuid" + @" -Username $exportResult.SqlCredentials.UserName" + @" -Password $exportResult.SqlCredentials.Password")); }); Collection <PSObject> startImportResult = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { return(powershell.InvokeBatchScript( @"Start-AzureSqlDatabaseImport" + @" -SqlConnectionContext $databaseContext" + @" -DatabaseName $targetDB" + @" -Edition Business" + @" -DatabaseMaxSize 10" + @" -StorageContext $storageContext" + @" -StorageContainerName $storageContainerName" + @" -BlobName backup1")); }); Collection <PSObject> getImportStatusResult = MockServerHelper.ExecuteWithMock( testSession, MockHttpServer.DefaultHttpsServerPrefixUri, () => { powershell.Runspace.SessionStateProxy.SetVariable( "importResult", startImportResult.FirstOrDefault()); return(powershell.InvokeBatchScript( @"Get-AzureSqlDatabaseImportExportStatus" + @" -ServerName $serverName" + @" -RequestId $importResult.RequestGuid" + @" -Username $importResult.SqlCredentials.UserName" + @" -Password $importResult.SqlCredentials.Password")); }); Assert.AreEqual(0, powershell.Streams.Error.Count, "Unexpected Errors during run!"); Assert.AreEqual(0, powershell.Streams.Warning.Count, "Unexpected Warnings during run!"); } }