Example #1
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();
            }
        }
        /// <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");
        }
Example #4
0
        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");
                }
            }
        }
Example #5
0
        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");
                }
            }
        }
Example #6
0
        public void GetAzureSqlDatabaseServiceObjectiveWithSqlAuth()
        {
            using (System.Management.Automation.PowerShell powershell =
                       System.Management.Automation.PowerShell.Create())
            {
                // Create a context
                NewAzureSqlDatabaseServerContextTests.CreateServerContextSqlAuth(
                    powershell,
                    "$context");

                HttpSession testSession = DatabaseTestHelper.DefaultSessionCollection.GetSession(
                    "UnitTests.GetAzureSqlServiceObjectiveWithSqlAuth");
                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: Retrieving all (6) ServiceObjectives and DimensionSettings
                    case 0:
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                    case 5:
                    case 6:
                    // Request 7-8: Retrieving Reserved P1 ServiceObjectives and DimensionSettings
                    case 7:
                    case 8:
                        DatabaseTestHelper.ValidateHeadersForODataRequest(
                            expected.RequestInfo,
                            actual);
                        break;

                    default:
                        Assert.Fail("No more requests expected.");
                        break;
                    }
                });
                testSession.ResponseModifier =
                    new Action <HttpMessage>(
                        (message) =>
                {
                    DatabaseTestHelper.FixODataResponseUri(
                        message.ResponseInfo,
                        testSession.ServiceBaseUri,
                        MockHttpServer.DefaultServerPrefixUri);
                });

                using (AsyncExceptionManager exceptionManager = new AsyncExceptionManager())
                {
                    Collection <PSObject> objectives, objective1;
                    using (new MockHttpServer(
                               exceptionManager,
                               MockHttpServer.DefaultServerPrefixUri,
                               testSession))
                    {
                        objectives = powershell.InvokeBatchScript(
                            @"Get-AzureSqlDatabaseServiceObjective " +
                            @"-Context $context");

                        objective1 = powershell.InvokeBatchScript(
                            @"Get-AzureSqlDatabaseServiceObjective " +
                            @"-Context $context " +
                            @"-ServiceObjectiveName ""Reserved P1""");
                    }

                    Assert.AreEqual(0, powershell.Streams.Error.Count, "Errors during run!");
                    Assert.AreEqual(0, powershell.Streams.Warning.Count, "Warnings during run!");
                    powershell.Streams.ClearStreams();

                    Assert.AreEqual(6, objectives.Count, "Expecting 6 Objective objects");

                    Assert.IsTrue(
                        objective1.Single().BaseObject is Services.Server.ServiceObjective,
                        "Expecting a ServiceObjective object");
                    Services.Server.ServiceObjective objective1Obj =
                        (Services.Server.ServiceObjective)objective1.Single().BaseObject;
                    Assert.AreEqual("Reserved P1", objective1Obj.Name, "Expected objective name to be 'Reserved P1'");
                }
            }
        }
        /// <summary>
        /// Create a new database in the given server context along with a continuous copy at the specified partner server
        /// </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="databaseVariable">The variable name that holds the database object.</param>
        /// <param name="databaseName">The name of the database to create and to copy.</param>
        /// <param name="partnerServer">The name of the partner server where the continuous copy goes to</param>
        public static void CreateDatabaseWithCopy(
            System.Management.Automation.PowerShell powershell,
            string contextVariable,
            string databaseVariable,
            string databaseName,
            string partnerServer)
        {
            HttpSession testSession = DatabaseTestHelper.DefaultSessionCollection.GetSession(
                "UnitTest.Common.CreateDatabaseWithCopy");

            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: Create database with copy
                case 0:
                case 1:
                // Request 2-3: Get database copy
                case 2:
                case 3:
                    DatabaseTestHelper.ValidateHeadersForODataRequest(
                        expected.RequestInfo,
                        actual);
                    break;

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

            using (AsyncExceptionManager exceptionManager = new AsyncExceptionManager())
            {
                Collection <PSObject> database, databaseCopy;
                using (new MockHttpServer(
                           exceptionManager,
                           MockHttpServer.DefaultServerPrefixUri,
                           testSession))
                {
                    database = powershell.InvokeBatchScript(
                        string.Format(
                            CultureInfo.InvariantCulture,
                            @"{0} = New-AzureSqlDatabaseWithCopy " +
                            @"-Context {1} " +
                            @"-DatabaseName {2} " +
                            @"-PartnerServer {3} " +
                            @"-Collation Japanese_CI_AS " +
                            @"-Edition Web " +
                            @"-MaxSizeGB 5 " +
                            @"-MaxLagInMinutes 15 " +
                            @"-Force",
                            databaseVariable,
                            contextVariable,
                            databaseName,
                            partnerServer),
                        databaseVariable);
                    databaseCopy = powershell.InvokeBatchScript(
                        string.Format(
                            CultureInfo.InvariantCulture,
                            @"Get-AzureSqlDatabaseCopy " +
                            @"-Context {0} " +
                            @"-DatabaseName {1} " +
                            @"-PartnerServer {2}",
                            contextVariable,
                            databaseName,
                            partnerServer)
                        );
                }

                Assert.AreEqual(0, powershell.Streams.Error.Count, "Errors during run!");
                Assert.AreEqual(0, powershell.Streams.Warning.Count, "Warnings during run!");
                powershell.Streams.ClearStreams();

                // Validate Database object returned
                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(databaseName, databaseObj.Name, "Expected database name to match");
                Assert.AreEqual("Japanese_CI_AS", databaseObj.CollationName, "Expected collation to be Japanese_CI_AS");
                Assert.AreEqual("Web", databaseObj.Edition, "Expected edition to be Web");
                Assert.AreEqual(5, databaseObj.MaxSizeGB, "Expected max size to be 5 GB");

                // Validate DatabaseCopy object returned
                Assert.IsTrue(
                    databaseCopy.First().BaseObject is Services.Server.DatabaseCopy,
                    "Expected a DatabaseCopy object");
                Services.Server.DatabaseCopy databaseCopyObj =
                    (Services.Server.DatabaseCopy)databaseCopy.First().BaseObject;
                Assert.AreEqual("testserver", databaseCopyObj.SourceServerName, "Expected source server name to be testserver");
                Assert.AreEqual(
                    databaseName,
                    databaseCopyObj.SourceDatabaseName,
                    string.Format(
                        CultureInfo.InvariantCulture,
                        "Expected source database name to be {0}",
                        databaseName)
                    );
                Assert.AreEqual(
                    partnerServer,
                    databaseCopyObj.DestinationServerName,
                    string.Format(
                        CultureInfo.InvariantCulture,
                        "Expected destination server name to be {0}",
                        partnerServer)
                    );
                Assert.AreEqual(
                    databaseName,
                    databaseCopyObj.DestinationDatabaseName,
                    string.Format(
                        CultureInfo.InvariantCulture,
                        "Expected destination database name to be {0}",
                        databaseName)
                    );
                Assert.IsTrue(databaseCopyObj.IsContinuous, "Expected copy to be continuous");
                Assert.AreEqual(15, databaseCopyObj.MaximumLag, "Expected maximum lag to be 15 minutes");
                Assert.AreEqual("CATCH_UP", databaseCopyObj.ReplicationStateDescription, "Expected replication state to be CATCH_UP");
            }
        }
Example #8
0
        public void RemoveAzureSqlDatabaseWithSqlAuth()
        {
            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.RemoveAzureSqlDatabaseWithSqlAuth");
                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 < 8)
                    {
                        // Request 0-5: Remove database requests
                        // Request 6-7: Get all database request
                        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> databases;
                    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");

                        databases = powershell.InvokeBatchScript(
                            @"Get-AzureSqlDatabase " +
                            @"-Context $context");
                    }

                    Assert.AreEqual(0, powershell.Streams.Error.Count, "Errors during run!");
                    Assert.AreEqual(0, powershell.Streams.Warning.Count, "Warnings during run!");
                    powershell.Streams.ClearStreams();

                    Assert.AreEqual(1, databases.Count, "Expecting only master database object");
                }
            }
        }
Example #9
0
        /// <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 = DatabaseTestHelper.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.ImportSqlDatabaseModule(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");
        }
Example #10
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 = DatabaseTestHelper.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);
                    switch (expected.Index)
                    {
                    // Request 0-3: Get all databases + ServiceObjective lookup for each database
                    case 0:
                    case 1:
                    case 2:
                    case 3:
                    // Request 4-7: Get database requests, 2 requests per get
                    case 4:
                    case 5:
                    case 6:
                    case 7:
                        DatabaseTestHelper.ValidateHeadersForODataRequest(
                            expected.RequestInfo,
                            actual);
                        break;

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

                using (AsyncExceptionManager exceptionManager = new AsyncExceptionManager())
                {
                    Collection <PSObject> databases, database1, database2;
                    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");
                    }

                    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");

                    Assert.IsTrue(
                        database1.Single().BaseObject is Services.Server.Database,
                        "Expecting a Database object");
                    Services.Server.Database database1Obj =
                        (Services.Server.Database)database1.Single().BaseObject;
                    Assert.AreEqual(
                        "testdb1",
                        database1Obj.Name,
                        "Expected db name to be testdb1");

                    Assert.IsTrue(
                        database2.Single().BaseObject is Services.Server.Database,
                        "Expecting a Database object");
                    Services.Server.Database database2Obj =
                        (Services.Server.Database)database2.Single().BaseObject;
                    Assert.AreEqual(
                        "testdb2",
                        database2Obj.Name,
                        "Expected db name to be testdb2");
                    Assert.AreEqual(
                        "Japanese_CI_AS",
                        database2Obj.CollationName,
                        "Expected collation to be Japanese_CI_AS");
                    Assert.AreEqual("Web", database2Obj.Edition, "Expected edition to be Web");
                    Assert.AreEqual(5, database2Obj.MaxSizeGB, "Expected max size to be 5 GB");
                }
            }
        }
Example #11
0
        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 = DatabaseTestHelper.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 'testserver.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();
            }
        }
Example #12
0
        public void NewAzureSqlDatabaseWithSqlAuthDuplicateName()
        {
            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");

                // Issue another create testdb1, causing a failure
                HttpSession testSession = DatabaseTestHelper.DefaultSessionCollection.GetSession(
                    "UnitTest.NewAzureSqlDatabaseWithSqlAuthDuplicateName");
                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: 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;
                        powershell.InvokeBatchScript(
                            @"New-AzureSqlDatabase " +
                            @"-Context $context " +
                            @"-DatabaseName testdb1 " +
                            @"-Force");
                    }
                }

                Assert.AreEqual(1, powershell.Streams.Error.Count, "Expecting errors");
                Assert.AreEqual(2, powershell.Streams.Warning.Count, "Expecting tracing IDs");
                Assert.AreEqual(
                    "Database 'testdb1' already exists. Choose a different database name.",
                    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();
            }
        }
Example #13
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 = DatabaseTestHelper.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-1: Create testdb1
                // Request 2-3: Create testdb2
                case 0:
                case 1:
                case 2:
                case 3:
                    DatabaseTestHelper.ValidateHeadersForODataRequest(
                        expected.RequestInfo,
                        actual);
                    break;

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

            using (AsyncExceptionManager exceptionManager = new AsyncExceptionManager())
            {
                Collection <PSObject> database1, database2;
                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");
                }

                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(
                    database1.Single().BaseObject is Services.Server.Database,
                    "Expecting a Database object");
                Services.Server.Database database1Obj =
                    (Services.Server.Database)database1.Single().BaseObject;
                Assert.AreEqual("testdb1", database1Obj.Name, "Expected db name to be testdb1");

                Assert.IsTrue(
                    database2.Single().BaseObject is Services.Server.Database,
                    "Expecting a Database object");
                Services.Server.Database database2Obj =
                    (Services.Server.Database)database2.Single().BaseObject;
                Assert.AreEqual("testdb2", database2Obj.Name, "Expected db name to be testdb2");
                Assert.AreEqual(
                    "Japanese_CI_AS",
                    database2Obj.CollationName,
                    "Expected collation to be Japanese_CI_AS");
                Assert.AreEqual("Web", database2Obj.Edition, "Expected edition to be Web");
                Assert.AreEqual(5, database2Obj.MaxSizeGB, "Expected max size to be 5 GB");
            }
        }
        public void GetAzureSqlDatabaseContinuousCopyWithSqlAuth()
        {
            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");
                // Start two continuous database copy operation
                StartAzureSqlDatabaseCopyTests.StartDatabaseContinuousCopyWithSqlAuth(
                    powershell,
                    "$context",
                    "$copy1",
                    "testdb1");
                StartAzureSqlDatabaseCopyTests.StartDatabaseContinuousCopyWithSqlAuth(
                    powershell,
                    "$context",
                    "$copy2",
                    "testdb2");

                HttpSession testSession = DatabaseTestHelper.DefaultSessionCollection.GetSession(
                    "UnitTest.GetAzureSqlDatabaseContinuousCopyWithSqlAuth");
                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 all database copies request
                    case 0:
                    case 1:
                    // Request 2-3: Get database copies for testdb1
                    case 2:
                    case 3:
                    // Request 4-5: Get database copies for testdb2
                    case 4:
                    case 5:
                        DatabaseTestHelper.ValidateHeadersForODataRequest(
                            expected.RequestInfo,
                            actual);
                        break;

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

                using (AsyncExceptionManager exceptionManager = new AsyncExceptionManager())
                {
                    Collection <PSObject> databaseCopies, databaseCopy1, databaseCopy2;
                    using (new MockHttpServer(
                               exceptionManager,
                               MockHttpServer.DefaultServerPrefixUri,
                               testSession))
                    {
                        databaseCopies = powershell.InvokeBatchScript(
                            @"Get-AzureSqlDatabaseCopy " +
                            @"-Context $context");
                        databaseCopy1 = powershell.InvokeBatchScript(
                            @"Get-AzureSqlDatabaseCopy " +
                            @"-Context $context " +
                            @"-DatabaseName testdb1");
                        databaseCopy2 = powershell.InvokeBatchScript(
                            @"Get-AzureSqlDatabaseCopy " +
                            @"-Context $context " +
                            @"-DatabaseName testdb2");
                    }

                    Assert.AreEqual(0, powershell.Streams.Error.Count, "Errors during run!");
                    Assert.AreEqual(0, powershell.Streams.Warning.Count, "Warnings during run!");
                    powershell.Streams.ClearStreams();

                    Assert.AreEqual(2, databaseCopies.Count, "Expecting 2 Database Copy objects");
                    Assert.IsTrue(
                        databaseCopy1.First().BaseObject is Services.Server.DatabaseCopy,
                        "Expecting a Database Copy object");
                    Services.Server.DatabaseCopy databaseCopyObj =
                        (Services.Server.DatabaseCopy)databaseCopy1.Single().BaseObject;
                    Assert.AreEqual(
                        "testserver",
                        databaseCopyObj.SourceServerName,
                        "Expected source server name to be testserver");
                    Assert.AreEqual(
                        "testdb1",
                        databaseCopyObj.SourceDatabaseName,
                        "Expected source database name to be testdb1");
                    Assert.AreEqual(
                        "partnersrv",
                        databaseCopyObj.DestinationServerName,
                        "Expected destination server name to be partnersrv");
                    Assert.AreEqual(
                        "testdb1",
                        databaseCopyObj.DestinationDatabaseName,
                        "Expected destination database name to be testdb1");
                    Assert.IsTrue(
                        databaseCopyObj.IsContinuous,
                        "Expected copy to be continuous");

                    Assert.IsTrue(
                        databaseCopy2.First().BaseObject is Services.Server.DatabaseCopy,
                        "Expecting a Database Copy object");
                    databaseCopyObj =
                        (Services.Server.DatabaseCopy)databaseCopy2.Single().BaseObject;
                    Assert.AreEqual(
                        "testserver",
                        databaseCopyObj.SourceServerName,
                        "Expected source server name to be testserver");
                    Assert.AreEqual(
                        "testdb2",
                        databaseCopyObj.SourceDatabaseName,
                        "Expected source database name to be testdb2");
                    Assert.AreEqual(
                        "partnersrv",
                        databaseCopyObj.DestinationServerName,
                        "Expected destination server name to be partnersrv");
                    Assert.AreEqual(
                        "testdb2",
                        databaseCopyObj.DestinationDatabaseName,
                        "Expected destination database name to be testdb2");
                    Assert.IsTrue(
                        databaseCopyObj.IsContinuous,
                        "Expected copy to be continuous");
                }
            }
        }
        /// <summary>
        /// Create continuous copy of testdb1 to partnersrv.
        /// </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="copyVariable">The variable name that holds the copy object.</param>
        /// <param name="databaseName">The name of the database to copy.</param>
        public static void StartDatabaseContinuousCopyWithSqlAuth(
            System.Management.Automation.PowerShell powershell,
            string contextVariable,
            string copyVariable,
            string databaseName)
        {
            HttpSession testSession = DatabaseTestHelper.DefaultSessionCollection.GetSession(
                "UnitTest.Common.StartAzureSqlDatabaseContinuousCopyWithSqlAuth." + databaseName);

            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: Start database copy request
                case 0:
                case 1:
                    DatabaseTestHelper.ValidateHeadersForODataRequest(
                        expected.RequestInfo,
                        actual);
                    break;

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

            using (AsyncExceptionManager exceptionManager = new AsyncExceptionManager())
            {
                Collection <PSObject> databaseCopy;
                using (new MockHttpServer(
                           exceptionManager,
                           MockHttpServer.DefaultServerPrefixUri,
                           testSession))
                {
                    databaseCopy = powershell.InvokeBatchScript(
                        string.Format(
                            CultureInfo.InvariantCulture,
                            @"{0} = Start-AzureSqlDatabaseCopy " +
                            @"-Context $context " +
                            @"-DatabaseName {1} " +
                            @"-PartnerServer partnersrv " +
                            @"-ContinuousCopy",
                            copyVariable,
                            databaseName),
                        copyVariable);
                }

                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.Single().BaseObject is Services.Server.DatabaseCopy,
                    "Expecting a Database Copy object");
                Services.Server.DatabaseCopy databaseCopyObj =
                    (Services.Server.DatabaseCopy)databaseCopy.Single().BaseObject;
                Assert.AreEqual(
                    "testserver",
                    databaseCopyObj.SourceServerName,
                    "Unexpected source server name");
                Assert.AreEqual(
                    databaseName,
                    databaseCopyObj.SourceDatabaseName,
                    "Unexpected source database name");
                Assert.AreEqual(
                    "partnersrv",
                    databaseCopyObj.DestinationServerName,
                    "Unexpected destination server name");
                Assert.AreEqual(
                    databaseName,
                    databaseCopyObj.DestinationDatabaseName,
                    "Unexpected destination database name");
                Assert.IsTrue(
                    databaseCopyObj.IsContinuous,
                    "Expected copy to be continuous");
            }
        }
        public void StopAzureSqlDatabaseContinuousCopyWithSqlAuth()
        {
            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");
                // Start two continuous database copy operation
                StartAzureSqlDatabaseCopyTests.StartDatabaseContinuousCopyWithSqlAuth(
                    powershell,
                    "$context",
                    "$copy1",
                    "testdb1");
                GetAzureSqlDatabaseCopyTests.WaitContinuousCopyCatchup(
                    powershell,
                    "$context",
                    "$copy1");

                HttpSession testSession = DatabaseTestHelper.DefaultSessionCollection.GetSession(
                    "UnitTest.StopAzureSqlDatabaseContinuousCopyWithSqlAuth");
                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: Stop Database Copy
                    case 0:
                    case 1:
                    case 2:
                    // Request 3-4: Retrieve all copies
                    case 3:
                    case 4:
                        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(
                            @"Stop-AzureSqlDatabaseCopy " +
                            @"-Context $context " +
                            @"-DatabaseName testdb1");
                    }
                }

                Assert.AreEqual(0, powershell.Streams.Error.Count, "Errors during run!");
                Assert.AreEqual(0, powershell.Streams.Warning.Count, "Warnings during run!");
                powershell.Streams.ClearStreams();

                WaitForCopyTermination(powershell, "$context");
            }
        }