Пример #1
0
        public void AzureSqlDatabaseEditionsTests()
        {
            // This test uses the https endpoint, setup the certificates.
            MockHttpServer.SetupCertificates();

            using (PowerShell powershell = PowerShell.Create())
            {
                // Setup the subscription used for the test
                WindowsAzureSubscription 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.");
                    Assert.IsTrue(
                        UnitTestHelper.GetUnitTestClientCertificate().Equals(actual.Certificate),
                        "Expected correct client certificate");
                });

                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[0]"));
                });

                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[0] -Force"));
                });

                Collection <PSObject> setDatabaseObjective2 = MockServerHelper.ExecuteWithMock(
                    testSession,
                    MockHttpServer.DefaultHttpsServerPrefixUri,
                    () =>
                {
                    return(powershell.InvokeBatchScript(
                               @"Set-AzureSqlDatabase -ServerName $serverName -DatabaseName $db3.Name -Edition Standard -MaxSizeGB 1 -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
                Database[] databases = new Database[] { newDatabaseResult1.Single().BaseObject as Database };
                Assert.AreEqual(1, databases.Length, "Expecting one database");
                Assert.IsNotNull(databases[0], "Expecting a Database object.");
                DatabaseTestHelper.ValidateDatabaseProperties(databases[0], "testdbeditions1", "Basic", 0, 104857600L, "SQL_Latin1_General_CP1_CI_AS", "Basic", false, DatabaseTestHelper.BasicSloGuid);

                databases = new Database[] { newDatabaseResult2.Single().BaseObject as Database };
                Assert.AreEqual(1, databases.Length, "Expecting one database");
                Assert.IsNotNull(databases[0], "Expecting a Database object.");
                DatabaseTestHelper.ValidateDatabaseProperties(databases[0], "testdbeditions2", "", -1, -1, "SQL_Latin1_General_CP1_CI_AS", "S1", false, DatabaseTestHelper.StandardS1SloGuid);

                databases = new Database[] { newDatabaseResult3.Single().BaseObject as Database };
                Assert.AreEqual(1, databases.Length, "Expecting one database");
                Assert.IsNotNull(databases[0], "Expecting a Database object.");
                DatabaseTestHelper.ValidateDatabaseProperties(databases[0], "testdbeditions3", "Basic", 0, 104857600L, "SQL_Latin1_General_CP1_CI_AS", "Basic", false, DatabaseTestHelper.BasicSloGuid);

                databases = new Database[] { newDatabaseResult4.Single().BaseObject as Database };
                Assert.AreEqual(1, databases.Length, "Expecting one database");
                Assert.IsNotNull(databases[0], "Expecting a Database object.");
                DatabaseTestHelper.ValidateDatabaseProperties(databases[0], "testdbeditions4", "", -1, -1, "SQL_Latin1_General_CP1_CI_AS", "P1", false, DatabaseTestHelper.PremiumP1SloGuid);

                databases = new Database[] { newDatabaseResult5.Single().BaseObject as Database };
                Assert.AreEqual(1, databases.Length, "Expecting one database");
                Assert.IsNotNull(databases[0], "Expecting a Database object.");
                DatabaseTestHelper.ValidateDatabaseProperties(databases[0], "testdbeditions5", "", -1, -1, "SQL_Latin1_General_CP1_CI_AS", "S2", false, DatabaseTestHelper.StandardS2SloGuid);


                // Validate Get-AzureSqlDatabase
                databases = getDatabaseResult.Select(r => r.BaseObject as 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[0], "master", "System", 5, 5368709120L, "SQL_Latin1_General_CP1_CI_AS", "System", true, DatabaseTestHelper.SystemSloGuid);
                DatabaseTestHelper.ValidateDatabaseProperties(databases[1], "testdbeditions1", "Basic", 0, 104857600L, "SQL_Latin1_General_CP1_CI_AS", "Basic", false, DatabaseTestHelper.BasicSloGuid);
                DatabaseTestHelper.ValidateDatabaseProperties(databases[2], "testdbeditions2", "Standard", 0, 524288000L, "SQL_Latin1_General_CP1_CI_AS", "S2", false, DatabaseTestHelper.StandardS2SloGuid);
                DatabaseTestHelper.ValidateDatabaseProperties(databases[3], "testdbeditions3", "Standard", 1, 1073741824L, "SQL_Latin1_General_CP1_CI_AS", "S1", false, DatabaseTestHelper.StandardS1SloGuid);
                DatabaseTestHelper.ValidateDatabaseProperties(databases[4], "testdbeditions4", "Premium", 10, 10737418240L, "SQL_Latin1_General_CP1_CI_AS", "P1", false, DatabaseTestHelper.PremiumP1SloGuid);
                DatabaseTestHelper.ValidateDatabaseProperties(databases[5], "testdbeditions5", "Standard", 2, 2147483648L, "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], "S2", "Standard S2 resource allocation.", 1, "Standard S2 resource allocation.");
                ValidateServiceObjectiveProperties(sos[1], "P1", "Premium P1 resource allocation.", 1, "Premium P1 resource allocation.");
                ValidateServiceObjectiveProperties(sos[2], "P2", "Premium P2 resource allocation.", 1, "Premium P2 resource allocation.");
                ValidateServiceObjectiveProperties(sos[3], "Basic", "Basic resource allocation.", 1, "Basic resource allocation.");
                ValidateServiceObjectiveProperties(sos[4], "S1", "Standard S1 resource allocation.", 1, "Standard S1 resource allocation.");
                ValidateServiceObjectiveProperties(sos[5], "P3 deprecated", "Premium P3 deprecated resource allocation.", 1, "Premium P3 deprecated resource allocation.");
                ValidateServiceObjectiveProperties(sos[6], "P3", "Premium P3 resource allocation.", 1, "Premium P3 resource allocation.");
                ValidateServiceObjectiveProperties(sos[7], "System", "Used for master database only.", 1, "Used for master database only.");
                ValidateServiceObjectiveProperties(sos[8], "System Standard", "Used for master database only.", 1, "Shared resource allocation.");
                ValidateServiceObjectiveProperties(sos[9], "Shared", "Shared resource allocation.", 1, "Shared resource allocation.");

                // Validate Remove-AzureSqlDatabase
                databases = new Database[] { removeDatabaseResult.Single().BaseObject as 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", "System", true, DatabaseTestHelper.SystemSloGuid);
            }
        }
Пример #2
0
        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);
                }
            }
        }
Пример #3
0
        public void AzureSqlDatabaseCertTests()
        {
            // This test uses the https endpoint, setup the certificates.
            MockHttpServer.SetupCertificates();

            using (PowerShell powershell = PowerShell.Create())
            {
                // Setup the subscription used for the test
                WindowsAzureSubscription 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.");
                    Assert.IsTrue(
                        UnitTestHelper.GetUnitTestClientCertificate().Equals(actual.Certificate),
                        "Expected correct client certificate");
                });

                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 (Web)
                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[2]",
                               @"$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> newPremiumP1DatabaseResult = MockServerHelper.ExecuteWithMock(
                    testSession,
                    MockHttpServer.DefaultHttpsServerPrefixUri,
                    () =>
                {
                    return(powershell.InvokeBatchScript(
                               @"New-AzureSqlDatabase" +
                               @" -ServerName $serverName" +
                               @" -DatabaseName ""testdbcertPremiumDBP1""" +
                               @" -Edition Premium" +
                               @" -ServiceObjective $P1"));
                });

                Collection <PSObject> newPremiumP2DatabaseResult = MockServerHelper.ExecuteWithMock(
                    testSession,
                    MockHttpServer.DefaultHttpsServerPrefixUri,
                    () =>
                {
                    return(powershell.InvokeBatchScript(
                               @"New-AzureSqlDatabase" +
                               @" -ServerName $serverName" +
                               @" -DatabaseName ""testdbcertPremiumDBP2""" +
                               @" -Edition Premium" +
                               @" -ServiceObjective $P2"));
                });

                // 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_551f9692-e85f-4dcd-9fe1-7e5c6af67a6f";
                }
                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> 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("premiumP1", newPremiumP1DatabaseResult.FirstOrDefault());
                    powershell.Runspace.SessionStateProxy.SetVariable("premiumP2", newPremiumP2DatabaseResult.FirstOrDefault());
                    powershell.Runspace.SessionStateProxy.SetVariable("operationDb", newOperationDbResult.FirstOrDefault());
                    powershell.InvokeBatchScript(@"$db1 | Remove-AzureSqlDatabase -Force");
                    powershell.InvokeBatchScript(@"$db2 | Remove-AzureSqlDatabase -Force");
                    powershell.InvokeBatchScript(@"$db3 | Remove-AzureSqlDatabase -Force");
                    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
                Database[] databases = new Database[] { newDatabaseResult1.Single().BaseObject as 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", "Web", 5, 5368709120L, "SQL_Latin1_General_CP1_CI_AS", "Shared", false, DatabaseTestHelper.SharedSloGuid);

                databases = new Database[] { newDatabaseResult2.Single().BaseObject as Database };
                Assert.AreEqual(1, databases.Length, "Expecting one database");
                Assert.IsNotNull(databases[0], "Expecting a Database object.");
                DatabaseTestHelper.ValidateDatabaseProperties(databases[0], "testdbcert2", "Business", 10, 10737418240L, "Japanese_CI_AS", "Shared", false, DatabaseTestHelper.SharedSloGuid);

                databases = new Database[] { newDatabaseResult3.Single().BaseObject as Database };
                Assert.AreEqual(1, databases.Length, "Expecting one database");
                Assert.IsNotNull(databases[0], "Expecting a Database object.");
                DatabaseTestHelper.ValidateDatabaseProperties(databases[0], "testdbcert4", "Web", 0, 104857600L, "SQL_Latin1_General_CP1_CI_AS", "Shared", false, DatabaseTestHelper.SharedSloGuid);

                // Validate Get-AzureSqlDatabase
                databases = getDatabaseResult.Select(r => r.BaseObject as 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[0], "master", "System", 5, 5368709120L, "SQL_Latin1_General_CP1_CI_AS", "System", true, DatabaseTestHelper.SystemSloGuid);
                DatabaseTestHelper.ValidateDatabaseProperties(databases[1], "testdbcert1", "Web", 1, 1073741824L, "SQL_Latin1_General_CP1_CI_AS", "Shared", false, DatabaseTestHelper.SharedSloGuid);
                DatabaseTestHelper.ValidateDatabaseProperties(databases[2], "testdbcert2", "Business", 10, 10737418240L, "Japanese_CI_AS", "Shared", false, DatabaseTestHelper.SharedSloGuid);
                DatabaseTestHelper.ValidateDatabaseProperties(databases[3], "testdbcert4", "Web", 0, 104857600L, "SQL_Latin1_General_CP1_CI_AS", "Shared", false, DatabaseTestHelper.SharedSloGuid);

                databases = new Database[] { getSingleDatabaseResult.Single().BaseObject as Database };
                Assert.AreEqual(1, databases.Length, "Expecting one database");
                Assert.IsNotNull(databases[0], "Expecting a Database object.");
                DatabaseTestHelper.ValidateDatabaseProperties(databases[0], "testdbcert1", "Web", 1, 1073741824L, "SQL_Latin1_General_CP1_CI_AS", "Shared", false, DatabaseTestHelper.SharedSloGuid);

                databases = new Database[] { getSingleDatabaseResult2.Single().BaseObject as Database };
                Assert.AreEqual(1, databases.Length, "Expecting one database");
                Assert.IsNotNull(databases[0], "Expecting a Database object.");
                DatabaseTestHelper.ValidateDatabaseProperties(databases[0], "testdbcert4", "Web", 0, 104857600L, "SQL_Latin1_General_CP1_CI_AS", "Shared", false, DatabaseTestHelper.SharedSloGuid);

                // Validate Set-AzureSqlDatabase
                databases = new Database[] { setDatabaseNameResult.Single().BaseObject as Database };
                Assert.AreEqual(1, databases.Length, "Expecting one database");
                Assert.IsNotNull(databases[0], "Expecting a Database object.");
                DatabaseTestHelper.ValidateDatabaseProperties(databases[0], "testdbcert3", "Web", 1, 1073741824L, "SQL_Latin1_General_CP1_CI_AS", "Shared", false, DatabaseTestHelper.SharedSloGuid);

                databases = new Database[] { setDatabaseSizeResult.Single().BaseObject as Database };
                Assert.AreEqual(1, databases.Length, "Expecting one database");
                Assert.IsNotNull(databases[0], "Expecting a Database object.");
                DatabaseTestHelper.ValidateDatabaseProperties(databases[0], "testdbcert3", "Web", 5, 5368709120L, "SQL_Latin1_General_CP1_CI_AS", "Shared", false, DatabaseTestHelper.SharedSloGuid);

                databases = new Database[] { setDatabaseSizeResult2.Single().BaseObject as Database };
                Assert.AreEqual(1, databases.Length, "Expecting one database");
                Assert.IsNotNull(databases[0], "Expecting a Database object.");
                DatabaseTestHelper.ValidateDatabaseProperties(databases[0], "testdbcert4", "Web", 1, 1073741824L, "SQL_Latin1_General_CP1_CI_AS", "Shared", false, DatabaseTestHelper.SharedSloGuid);

                databases = new Database[] { setDatabaseSlo.Single().BaseObject as Database };
                Assert.AreEqual(1, databases.Length, "Expecting one database");
                Assert.IsNotNull(databases[0], "Expecting a Database object.");
                DatabaseTestHelper.ValidateDatabaseProperties(databases[0], "testdbcert4", "Premium", 10, 10737418240L, "SQL_Latin1_General_CP1_CI_AS", "P2", false, DatabaseTestHelper.SharedSloGuid);

                // 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.AreEqual("Premium P1 resource allocation.", SLOP1.Description);
                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.AreEqual(SLOP2.Description, "Premium P2 resource allocation.");
                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 Database[] { removeDatabaseResult.Single().BaseObject as 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", "System", true, DatabaseTestHelper.SystemSloGuid);
            }
        }
Пример #4
0
        /// <summary>
        /// Helper function to create the test databases.
        /// </summary>
        public static void CreateTestDatabasesWithCertAuth(System.Management.Automation.PowerShell powershell)
        {
            HttpSession testSession = MockServerHelper.DefaultSessionCollection.GetSession(
                "UnitTest.Common.CreateTestDatabasesWithCertAuth");

            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())
            {
                Collection <PSObject> database1, database2, database3;
                using (new MockHttpServer(
                           exceptionManager,
                           MockHttpServer.DefaultHttpsServerPrefixUri,
                           testSession))
                {
                    database1 = powershell.InvokeBatchScript(
                        @"$testdb1 = New-AzureSqlDatabase " +
                        @"-ServerName $serverName " +
                        @"-DatabaseName testdb1 " +
                        @"-Force",
                        @"$testdb1");
                    database2 = powershell.InvokeBatchScript(
                        @"$testdb2 = New-AzureSqlDatabase " +
                        @"-ServerName $serverName " +
                        @"-DatabaseName testdb2 " +
                        @"-Collation Japanese_CI_AS " +
                        @"-Edition Standard " +
                        @"-MaxSizeGB 5 " +
                        @"-Force",
                        @"$testdb2");
                    database3 = powershell.InvokeBatchScript(
                        @"$testdb3 = New-AzureSqlDatabase " +
                        @"-ServerName $serverName " +
                        @"-DatabaseName testdb3 " +
                        @"-MaxSizeBytes 104857600 " +
                        @"-Force",
                        @"$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");
                DatabaseTestHelper.ValidateDatabaseProperties(database, "testdb1", "Standard", 250, 268435456000L, "SQL_Latin1_General_CP1_CI_AS", "S0", false, DatabaseTestHelper.StandardS0SloGuid);

                database = database2.Single().BaseObject as Services.Server.Database;
                Assert.IsTrue(database != null, "Expecting a Database object");
                DatabaseTestHelper.ValidateDatabaseProperties(database, "testdb2", "Standard", 250, 268435456000L, "Japanese_CI_AS", "S0", false, DatabaseTestHelper.StandardS0SloGuid);

                database = database3.Single().BaseObject as Services.Server.Database;
                Assert.IsTrue(database != null, "Expecting a Database object");
                DatabaseTestHelper.ValidateDatabaseProperties(database, "testdb3", "Standard", 0, 104857600L, "SQL_Latin1_General_CP1_CI_AS", "S0", false, DatabaseTestHelper.StandardS0SloGuid);
            }
        }
Пример #5
0
        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);
                }
            }
        }
Пример #6
0
        /// <summary>
        /// Create $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 CreateTestDatabasesWithSqlAuth(
            System.Management.Automation.PowerShell powershell,
            string contextVariable)
        {
            HttpSession testSession = MockServerHelper.DefaultSessionCollection.GetSession(
                "UnitTest.Common.CreateTestDatabasesWithSqlAuth");

            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 testdb1
                // Request 3-5: Create testdb2
                // Request 6-8: Create testdb3
                case 0:
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                case 7:
                case 8:
                    DatabaseTestHelper.ValidateHeadersForODataRequest(
                        expected.RequestInfo,
                        actual);
                    break;

                default:
                    Assert.Fail("No more requests expected.");
                    break;
                }
            });

            using (AsyncExceptionManager exceptionManager = new AsyncExceptionManager())
            {
                Collection <PSObject> database1, database2, database3;
                using (new MockHttpServer(
                           exceptionManager,
                           MockHttpServer.DefaultServerPrefixUri,
                           testSession))
                {
                    database1 = powershell.InvokeBatchScript(
                        @"$testdb1 = New-AzureSqlDatabase " +
                        @"-Context $context " +
                        @"-DatabaseName testdb1 " +
                        @"-Force",
                        @"$testdb1");
                    database2 = powershell.InvokeBatchScript(
                        @"$testdb2 = New-AzureSqlDatabase " +
                        @"-Context $context " +
                        @"-DatabaseName testdb2 " +
                        @"-Collation Japanese_CI_AS " +
                        @"-Edition Web " +
                        @"-MaxSizeGB 5 " +
                        @"-Force",
                        @"$testdb2");
                    database3 = powershell.InvokeBatchScript(
                        @"$testdb3 = New-AzureSqlDatabase " +
                        @"-Context $context " +
                        @"-DatabaseName testdb3 " +
                        @"-MaxSizeBytes 104857600 " +
                        @"-Force",
                        @"$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");
                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 SetAzureSqlDatabaseSizeWithSqlAuth()
        {
            using (System.Management.Automation.PowerShell powershell =
                       System.Management.Automation.PowerShell.Create())
            {
                // Create a context
                NewAzureSqlDatabaseServerContextTests.CreateServerContextSqlAuth(
                    powershell,
                    "$context");

                HttpSession testSession = MockServerHelper.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);
                    if (expected.Index < 10)
                    {
                        // Request 0-2: Set testdb1 with new MaxSize
                        // Request 3-5: Set testdb2 with new MaxSize
                        // Request 6-7: Get updated testdb1
                        // Request 8-9: Get updated testdb2
                        DatabaseTestHelper.ValidateHeadersForODataRequest(
                            expected.RequestInfo,
                            actual);
                    }
                    else
                    {
                        Assert.Fail("No more requests expected.");
                    }
                });

                using (AsyncExceptionManager exceptionManager = new AsyncExceptionManager())
                {
                    // Create context with both ManageUrl and ServerName overriden
                    Collection <PSObject> database1, database2;
                    using (new MockHttpServer(
                               exceptionManager,
                               MockHttpServer.DefaultServerPrefixUri,
                               testSession))
                    {
                        database1 = powershell.InvokeBatchScript(
                            @"Set-AzureSqlDatabase " +
                            @"-Context $context " +
                            @"-DatabaseName testdb1 " +
                            @"-MaxSizeGB 5 " +
                            @"-Force " +
                            @"-PassThru");

                        // Set the database to 100MB
                        database2 = powershell.InvokeBatchScript(
                            @"Set-AzureSqlDatabase " +
                            @"-Context $context " +
                            @"-DatabaseName testdb2 " +
                            @"-MaxSizeBytes 104857600 " +
                            @"-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();

                    Database database = database1.Single().BaseObject as Services.Server.Database;
                    Assert.IsTrue(database != null, "Expecting a Database object");
                    DatabaseTestHelper.ValidateDatabaseProperties(database, "testdb1", "Web", 5, 5368709120L, "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", 0, 104857600L, "Japanese_CI_AS", "Shared", false, DatabaseTestHelper.SharedSloGuid);
                }
            }
        }
        public void SetAzureSqlPremiumDatabaseServiceObjectiveWithSqlAuth()
        {
            using (System.Management.Automation.PowerShell powershell =
                       System.Management.Automation.PowerShell.Create())
            {
                // Create a context
                NewAzureSqlDatabaseServerContextTests.CreateServerContextSqlAuth(
                    powershell,
                    "$context");
                HttpSession testSession = MockServerHelper.DefaultSessionCollection.GetSession(
                    "UnitTests.SetAzureSqlPremiumDatabaseServiceObjectiveWithSqlAuth");
                DatabaseTestHelper.SetDefaultTestSessionSettings(testSession);

                using (AsyncExceptionManager exceptionManager = new AsyncExceptionManager())
                {
                    // Create context with both ManageUrl and ServerName overriden
                    Collection <PSObject> premiumDB;
                    using (new MockHttpServer(
                               exceptionManager,
                               MockHttpServer.DefaultServerPrefixUri,
                               testSession))
                    {
                        powershell.InvokeBatchScript(
                            @"$P1 = Get-AzureSqlDatabaseServiceObjective" +
                            @" -Context $context" +
                            @" -ServiceObjectiveName ""P1""");

                        powershell.InvokeBatchScript(
                            @"$premiumDB_P1 = New-AzureSqlDatabase " +
                            @"-Context $context " +
                            @"-DatabaseName SetAzureSqlPremiumDatabaseTests_P1 " +
                            @"-Edition Premium " +
                            @"-ServiceObjective $P1 ");

                        premiumDB = powershell.InvokeBatchScript(
                            @"Set-AzureSqlDatabase " +
                            @"-Context $context " +
                            @"-DatabaseName SetAzureSqlPremiumDatabaseTests_P1 " +
                            @"-Edition Business " +
                            @"-Force " +
                            @"-PassThru");

                        powershell.InvokeBatchScript(
                            @"Remove-AzureSqlDatabase " +
                            @"-Context $context " +
                            @"-DatabaseName SetAzureSqlPremiumDatabaseTests_P1 " +
                            @"-Force ");
                    }

                    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 premiumDBObj = premiumDB.Single().BaseObject as Services.Server.Database;
                    Assert.IsNotNull(premiumDBObj, "Expecting a Database object");

                    DatabaseTestHelper.ValidateDatabaseProperties(premiumDBObj, "SetAzureSqlPremiumDatabaseTests_P1", "Premium", 10, 10737418240L, "SQL_Latin1_General_CP1_CI_AS", "P1", false, DatabaseTestHelper.PremiumP1SloGuid);
                }
            }
        }
        public void SetAzureSqlDatabaseServiceObjectiveWithSqlAuth()
        {
            using (System.Management.Automation.PowerShell powershell =
                       System.Management.Automation.PowerShell.Create())
            {
                // Create a context
                NewAzureSqlDatabaseServerContextTests.CreateServerContextSqlAuth(
                    powershell,
                    "$context");

                HttpSession testSession = MockServerHelper.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 ""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();

                    Services.Server.Database databaseObj = database.Single().BaseObject as Services.Server.Database;
                    Assert.IsNotNull(databaseObj, "Expecting a Database object");
                    Assert.AreEqual("testdb2", databaseObj.Name, "Expected db name to be testdb2");
                    Assert.AreEqual((byte)0, databaseObj.ServiceObjectiveAssignmentState, "Expected assignment state to be complete");
                    DatabaseTestHelper.ValidateDatabaseProperties(databaseObj, "testdb2", "Web", 5, 5368709120L, "Japanese_CI_AS", "Shared", false, DatabaseTestHelper.PremiumP1SloGuid);
                }
            }
        }
        public void SetAzureSqlDatabaseNameWithSqlAuth()
        {
            using (System.Management.Automation.PowerShell powershell =
                       System.Management.Automation.PowerShell.Create())
            {
                // Create a context
                NewAzureSqlDatabaseServerContextTests.CreateServerContextSqlAuth(
                    powershell,
                    "$context");
                NewAzureSqlDatabaseServerContextTests.CreateServerContextSqlAuth(
                    powershell,
                    "$contextCleanup");

                HttpSession testSession = MockServerHelper.DefaultSessionCollection.GetSession(
                    "UnitTests.SetAzureSqlDatabaseNameWithSqlAuth");
                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 < 10)
                    {
                        // Request 0-4: Set testdb1 with new name of new_testdb1
                        // Request 5-9: Set new_testdb1 with new name of testdb1
                        DatabaseTestHelper.ValidateHeadersForODataRequest(
                            expected.RequestInfo,
                            actual);
                    }
                    else
                    {
                        Assert.Fail("No more requests expected.");
                    }
                });

                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 " +
                            @"-NewName new_testdb1 " +
                            @"-Force " +
                            @"-PassThru");
                        powershell.InvokeBatchScript(
                            @"Set-AzureSqlDatabase " +
                            @"-Context $contextCleanup " +
                            @"-DatabaseName new_testdb1 " +
                            @"-NewName testdb1 " +
                            @"-Force");
                    }

                    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 databaseObj = database.Single().BaseObject as Services.Server.Database;
                    Assert.IsNotNull(databaseObj, "Expecting a Database object");
                    DatabaseTestHelper.ValidateDatabaseProperties(databaseObj, "new_testdb1", "Web", 1, 1073741824L, "SQL_Latin1_General_CP1_CI_AS", "Shared", false, DatabaseTestHelper.SharedSloGuid);
                }
            }
        }