public void AzureSqlDatabaseServerV2Tests()
        {
            // This test uses the https endpoint, setup the certificates.
            MockHttpServer.SetupCertificates();

            using (System.Management.Automation.PowerShell powershell = System.Management.Automation.PowerShell.Create())
            {
                // Setup the subscription used for the test
                AzureSubscription subscription =
                    UnitTestHelper.SetupUnitTestSubscription(powershell);

                // Create a new V2 server
                HttpSession testSession = MockServerHelper.DefaultSessionCollection.GetSession(
                    "UnitTest.AzureSqlDatabaseServerV2Tests");
                ServerTestHelper.SetDefaultTestSessionSettings(testSession);
                testSession.RequestValidator =
                    new Action <HttpMessage, HttpMessage.Request>(
                        (expected, actual) =>
                {
                    Assert.AreEqual(expected.RequestInfo.Method, actual.Method);
                    Assert.IsTrue(
                        actual.UserAgent.Contains(ApiConstants.UserAgentHeaderValue),
                        "Missing proper UserAgent string.");
                });

                powershell.Runspace.SessionStateProxy.SetVariable("login", "mylogin");
                powershell.Runspace.SessionStateProxy.SetVariable("password", "Pa$$w0rd!");
                powershell.Runspace.SessionStateProxy.SetVariable("location", "East Asia");
                Collection <PSObject> newServerResult = MockServerHelper.ExecuteWithMock(
                    testSession,
                    MockHttpServer.DefaultHttpsServerPrefixUri,
                    () =>
                {
                    return(powershell.InvokeBatchScript(
                               @"New-AzureSqlDatabaseServer" +
                               @" -AdministratorLogin $login" +
                               @" -AdministratorLoginPassword $password" +
                               @" -Location $location" +
                               @" -Version 2"));
                });

                Collection <PSObject> getServerResult = MockServerHelper.ExecuteWithMock(
                    testSession,
                    MockHttpServer.DefaultHttpsServerPrefixUri,
                    () =>
                {
                    powershell.Runspace.SessionStateProxy.SetVariable("server", newServerResult);
                    return(powershell.InvokeBatchScript(
                               @"Get-AzureSqlDatabaseServer $server.ServerName"));
                });

                Collection <PSObject> removeServerResult = MockServerHelper.ExecuteWithMock(
                    testSession,
                    MockHttpServer.DefaultHttpsServerPrefixUri,
                    () =>
                {
                    powershell.Runspace.SessionStateProxy.SetVariable("server", newServerResult);
                    powershell.InvokeBatchScript(
                        @"$server | Remove-AzureSqlDatabaseServer" +
                        @" -Force");

                    return(powershell.InvokeBatchScript(
                               @"Get-AzureSqlDatabaseServer"));
                });

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

                // Validate New-AzureSqlDatabaseServer results
                SqlDatabaseServerContext server =
                    newServerResult.Single().BaseObject as SqlDatabaseServerContext;
                Assert.IsNotNull(server, "Expecting a SqlDatabaseServerContext object");
                VerifyServer(
                    server,
                    (string)powershell.Runspace.SessionStateProxy.GetVariable("login"),
                    (string)powershell.Runspace.SessionStateProxy.GetVariable("location"),
                    ServerVersion2,
                    "Ready");


                // Validate Get-AzureSqlDatabaseServer results
                server = getServerResult.Single().BaseObject as SqlDatabaseServerContext;
                Assert.IsNotNull(server, "Expecting a SqlDatabaseServerContext object");
                VerifyServer(
                    server,
                    (string)powershell.Runspace.SessionStateProxy.GetVariable("login"),
                    (string)powershell.Runspace.SessionStateProxy.GetVariable("location"),
                    ServerVersion2,
                    "Ready");

                powershell.Streams.ClearStreams();
            }
        }
        public void AzureSqlDatabaseServerTests()
        {
            // This test uses the https endpoint, setup the certificates.
            MockHttpServer.SetupCertificates();

            SqlTestPsHost host = new SqlTestPsHost();
            SqlCustomPsHostUserInterface ui = host.UI as SqlCustomPsHostUserInterface;

            using (Runspace space = RunspaceFactory.CreateRunspace(host))
            {
                space.Open();

                using (System.Management.Automation.PowerShell powershell = System.Management.Automation.PowerShell.Create())
                {
                    powershell.Runspace = space;

                    // Setup the subscription used for the test
                    AzureSubscription subscription =
                        UnitTestHelper.SetupUnitTestSubscription(powershell);

                    // Create a new server
                    HttpSession testSession = MockServerHelper.DefaultSessionCollection.GetSession(
                        "UnitTest.AzureSqlDatabaseServerTests");
                    ServerTestHelper.SetDefaultTestSessionSettings(testSession);
                    testSession.RequestValidator =
                        new Action <HttpMessage, HttpMessage.Request>(
                            (expected, actual) =>
                    {
                        Assert.AreEqual(expected.RequestInfo.Method, actual.Method);
                        Assert.IsTrue(
                            actual.UserAgent.Contains(ApiConstants.UserAgentHeaderValue),
                            "Missing proper UserAgent string.");
                    });

                    powershell.Runspace.SessionStateProxy.SetVariable("login", "mylogin");
                    powershell.Runspace.SessionStateProxy.SetVariable("password", "Pa$$w0rd!");
                    powershell.Runspace.SessionStateProxy.SetVariable("location", "East Asia");
                    Collection <PSObject> newServerResult = MockServerHelper.ExecuteWithMock(
                        testSession,
                        MockHttpServer.DefaultHttpsServerPrefixUri,
                        () =>
                    {
                        return(powershell.InvokeBatchScript(
                                   @"New-AzureSqlDatabaseServer" +
                                   @" -AdministratorLogin $login" +
                                   @" -AdministratorLoginPassword $password" +
                                   @" -Location $location"));
                    });

                    ui.PromptInputs = new PSObject[] { "mylogin", "Pa$$w0rd", "East Asia" };
                    Collection <PSObject> newServerResult2 = MockServerHelper.ExecuteWithMock(
                        testSession,
                        MockHttpServer.DefaultHttpsServerPrefixUri,
                        () =>
                    {
                        return(powershell.InvokeBatchScript(@"New-AzureSqlDatabaseServer"));
                    });
                    ui.PromptInputs = null;

                    Collection <PSObject> getServerResult = MockServerHelper.ExecuteWithMock(
                        testSession,
                        MockHttpServer.DefaultHttpsServerPrefixUri,
                        () =>
                    {
                        powershell.Runspace.SessionStateProxy.SetVariable("server", newServerResult);
                        return(powershell.InvokeBatchScript(
                                   @"Get-AzureSqlDatabaseServer $server.ServerName"));
                    });

                    Collection <PSObject> setServerResult = MockServerHelper.ExecuteWithMock(
                        testSession,
                        MockHttpServer.DefaultHttpsServerPrefixUri,
                        () =>
                    {
                        powershell.Runspace.SessionStateProxy.SetVariable("server", newServerResult);
                        powershell.Runspace.SessionStateProxy.SetVariable("password", "Pa$$w0rd2");
                        powershell.InvokeBatchScript(
                            @"$server | Set-AzureSqlDatabaseServer" +
                            @" -AdminPassword $password" +
                            @" -Force");
                        return(powershell.InvokeBatchScript(
                                   @"$server | Get-AzureSqlDatabaseServer"));
                    });

                    ui.PromptInputs = new PSObject[] { "Pa$$w0rd2" };
                    Collection <PSObject> setServerResult2 = MockServerHelper.ExecuteWithMock(
                        testSession,
                        MockHttpServer.DefaultHttpsServerPrefixUri,
                        () =>
                    {
                        powershell.Runspace.SessionStateProxy.SetVariable("server", newServerResult2);
                        powershell.InvokeBatchScript(@"$server | Set-AzureSqlDatabaseServer");
                        return(powershell.InvokeBatchScript(@"$server | Get-AzureSqlDatabaseServer"));
                    });
                    ui.PromptInputs = null;

                    Collection <PSObject> removeServerResult = MockServerHelper.ExecuteWithMock(
                        testSession,
                        MockHttpServer.DefaultHttpsServerPrefixUri,
                        () =>
                    {
                        powershell.Runspace.SessionStateProxy.SetVariable("server", newServerResult);
                        powershell.InvokeBatchScript(
                            @"$server | Remove-AzureSqlDatabaseServer" +
                            @" -Force");

                        return(powershell.InvokeBatchScript(
                                   @"Get-AzureSqlDatabaseServer"));
                    });

                    ui.PromptInputs = new PSObject[] { ((SqlDatabaseServerContext)newServerResult2[0].BaseObject).ServerName };
                    ui.PromptForChoiceInputIndex = 0;   //answer yes to delete database prompt
                    Collection <PSObject> removeServerResult2 = MockServerHelper.ExecuteWithMock(
                        testSession,
                        MockHttpServer.DefaultHttpsServerPrefixUri,
                        () =>
                    {
                        powershell.InvokeBatchScript(@"Remove-AzureSqlDatabaseServer");

                        return(powershell.InvokeBatchScript(
                                   @"Get-AzureSqlDatabaseServer"));
                    });
                    ui.PromptForChoiceInputIndex = -1;
                    ui.PromptInputs = null;

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

                    // Validate New-AzureSqlDatabaseServer results
                    SqlDatabaseServerContext server =
                        newServerResult.Single().BaseObject as SqlDatabaseServerContext;
                    Assert.IsNotNull(server, "Expecting a SqlDatabaseServerContext object");
                    VerifyServer(
                        server,
                        (string)powershell.Runspace.SessionStateProxy.GetVariable("login"),
                        (string)powershell.Runspace.SessionStateProxy.GetVariable("location"),
                        ServerVersion12,
                        "Ready");

                    SqlDatabaseServerContext server2 = newServerResult2.Single().BaseObject as SqlDatabaseServerContext;
                    Assert.IsNotNull(server2, "Expecting a SqlDatabaseServerContext object");
                    VerifyServer(
                        server,
                        (string)powershell.Runspace.SessionStateProxy.GetVariable("login"),
                        (string)powershell.Runspace.SessionStateProxy.GetVariable("location"),
                        ServerVersion12,
                        "Ready");

                    // Validate Get-AzureSqlDatabaseServer results
                    server = getServerResult.Single().BaseObject as SqlDatabaseServerContext;
                    Assert.IsNotNull(server, "Expecting a SqlDatabaseServerContext object");
                    VerifyServer(
                        server,
                        (string)powershell.Runspace.SessionStateProxy.GetVariable("login"),
                        (string)powershell.Runspace.SessionStateProxy.GetVariable("location"),
                        ServerVersion12,
                        "Ready");

                    server = setServerResult.Single().BaseObject as SqlDatabaseServerContext;
                    Assert.IsNotNull(server, "Expecting a SqlDatabaseServerContext object");
                    VerifyServer(
                        server,
                        (string)powershell.Runspace.SessionStateProxy.GetVariable("login"),
                        (string)powershell.Runspace.SessionStateProxy.GetVariable("location"),
                        ServerVersion12,
                        "Ready");

                    server2 = setServerResult2.Single().BaseObject as SqlDatabaseServerContext;
                    Assert.IsNotNull(server, "Expecting a SqlDatabaseServerContext object");
                    VerifyServer(
                        server2,
                        (string)powershell.Runspace.SessionStateProxy.GetVariable("login"),
                        (string)powershell.Runspace.SessionStateProxy.GetVariable("location"),
                        ServerVersion12,
                        "Ready");

                    // Validate Remove-AzureSqlDatabaseServer results
                    Assert.IsFalse(
                        removeServerResult.Any((o) => o.GetVariableValue <string>("ServerName") == server.ServerName),
                        "Server should have been removed.");

                    Assert.IsFalse(
                        removeServerResult2.Any((o) => o.GetVariableValue <string>("ServerName") == server2.ServerName),
                        "Server 2 should have been removed.");

                    powershell.Streams.ClearStreams();
                }

                space.Close();
            }
        }
Exemple #3
0
        public void AzureSqlDatabaseServerQuotaCertAuthTest()
        {
            // 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.AzureSqlDatabaseServerQuotaCertAuthTest");
                ServerTestHelper.SetDefaultTestSessionSettings(testSession);

                testSession.RequestValidator =
                    new Action <HttpMessage, HttpMessage.Request>(
                        (expected, actual) =>
                {
                    Assert.AreEqual(expected.RequestInfo.Method, actual.Method);
                    Assert.IsTrue(
                        actual.UserAgent.Contains(ApiConstants.UserAgentHeaderValue),
                        "Missing proper UserAgent string.");
                    Assert.IsTrue(
                        UnitTestHelper.GetUnitTestClientCertificate().Equals(actual.Certificate),
                        "Expected correct client certificate");
                });

                Collection <PSObject> getQuota1 = MockServerHelper.ExecuteWithMock(
                    testSession,
                    MockHttpServer.DefaultHttpsServerPrefixUri,
                    () =>
                {
                    return(powershell.InvokeBatchScript(
                               @"Get-AzureSqlDatabaseServerQuota -ServerName $serverName"));
                });

                Collection <PSObject> getQuota2 = MockServerHelper.ExecuteWithMock(
                    testSession,
                    MockHttpServer.DefaultHttpsServerPrefixUri,
                    () =>
                {
                    return(powershell.InvokeBatchScript(
                               @"Get-AzureSqlDatabaseServerQuota -ServerName $serverName -QuotaName premium_databases"));
                });

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

                // Validate Get-AzureSqlDatabaseServerQuota
                var quotas = getQuota1.Select(x => ((IEnumerable)x.BaseObject).Cast <Model.SqlDatabaseServerQuotaContext>().Single()).ToArray();
                Assert.AreEqual(1, quotas.Length, "Expecting one server quota");
                Assert.IsNotNull(quotas[0], "Expecting a server quota.");
                Assert.AreEqual("premium_databases", quotas[0].Name);
                Assert.AreEqual(SqlDatabaseTestSettings.Instance.ServerV2, quotas[0].ServerName);
                Assert.AreEqual("Microsoft.SqlAzure.ServerQuota", quotas[0].Type);
                Assert.AreEqual("1000", quotas[0].Value);
                Assert.AreEqual("Normal", quotas[0].State);

                quotas = getQuota2.Select(x => ((IEnumerable)x.BaseObject).Cast <Model.SqlDatabaseServerQuotaContext>().Single()).ToArray();
                Assert.AreEqual(1, quotas.Length, "Expecting server quota");
                Assert.IsNotNull(quotas[0], "Expecting a server quota.");
                Assert.AreEqual("premium_databases", quotas[0].Name);
                Assert.AreEqual(SqlDatabaseTestSettings.Instance.ServerV2, quotas[0].ServerName);
                Assert.AreEqual("Microsoft.SqlAzure.ServerQuota", quotas[0].Type);
                Assert.AreEqual("1000", quotas[0].Value);
                Assert.AreEqual("Normal", quotas[0].State);
            }
        }
Exemple #4
0
        public void AzureSqlDatabaseServerFirewallTests()
        {
            // This test uses the https endpoint, setup the certificates.
            MockHttpServer.SetupCertificates();

            using (System.Management.Automation.PowerShell powershell = System.Management.Automation.PowerShell.Create())
            {
                // Setup the subscription used for the test
                AzureSubscription subscription =
                    UnitTestHelper.SetupUnitTestSubscription(powershell);

                powershell.Runspace.SessionStateProxy.SetVariable(
                    "serverName",
                    SqlDatabaseTestSettings.Instance.ServerName);

                // Create a new server
                HttpSession testSession = MockServerHelper.DefaultSessionCollection.GetSession(
                    "UnitTest.AzureSqlDatabaseServerFirewallTests");
                ServerTestHelper.SetDefaultTestSessionSettings(testSession);
                testSession.RequestValidator =
                    new Action <HttpMessage, HttpMessage.Request>(
                        (expected, actual) =>
                {
                    Assert.AreEqual(expected.RequestInfo.Method, actual.Method);
                    Assert.IsTrue(
                        actual.UserAgent.Contains(ApiConstants.UserAgentHeaderValue),
                        "Missing proper UserAgent string.");
                });

                Collection <PSObject> newFirewallRuleResult1 = MockServerHelper.ExecuteWithMock(
                    testSession,
                    MockHttpServer.DefaultHttpsServerPrefixUri,
                    () =>
                {
                    return(powershell.InvokeBatchScript(
                               @"New-AzureSqlDatabaseServerFirewallRule" +
                               @" -ServerName $serverName" +
                               @" -RuleName Rule1" +
                               @" -StartIpAddress 0.0.0.0" +
                               @" -EndIpAddress 1.1.1.1"));
                });

                Collection <PSObject> newFirewallRuleResult2 = MockServerHelper.ExecuteWithMock(
                    testSession,
                    MockHttpServer.DefaultHttpsServerPrefixUri,
                    () =>
                {
                    return(powershell.InvokeBatchScript(
                               @"New-AzureSqlDatabaseServerFirewallRule" +
                               @" -ServerName $serverName" +
                               @" -RuleName Rule2" +
                               @" -StartIpAddress 1.1.1.1" +
                               @" -EndIpAddress 2.2.2.2"));
                });

                Collection <PSObject> getFirewallRuleResult = MockServerHelper.ExecuteWithMock(
                    testSession,
                    MockHttpServer.DefaultHttpsServerPrefixUri,
                    () =>
                {
                    return(powershell.InvokeBatchScript(
                               @"Get-AzureSqlDatabaseServerFirewallRule $serverName"));
                });

                Collection <PSObject> setFirewallResult = MockServerHelper.ExecuteWithMock(
                    testSession,
                    MockHttpServer.DefaultHttpsServerPrefixUri,
                    () =>
                {
                    powershell.Runspace.SessionStateProxy.SetVariable("fw", newFirewallRuleResult1);
                    powershell.InvokeBatchScript(
                        @"$fw | Set-AzureSqlDatabaseServerFirewallRule" +
                        @" -StartIpAddress 2.2.2.2" +
                        @" -EndIpAddress 3.3.3.3");
                    return(powershell.InvokeBatchScript(
                               @"$fw | Get-AzureSqlDatabaseServerFirewallRule"));
                });

                Collection <PSObject> removeFirewallResult = MockServerHelper.ExecuteWithMock(
                    testSession,
                    MockHttpServer.DefaultHttpsServerPrefixUri,
                    () =>
                {
                    powershell.Runspace.SessionStateProxy.SetVariable("rules", getFirewallRuleResult);
                    powershell.InvokeBatchScript(
                        @"$rules | Remove-AzureSqlDatabaseServerFirewallRule" +
                        @" -Force");

                    return(powershell.InvokeBatchScript(
                               @"Get-AzureSqlDatabaseServerFirewallRule $serverName"));
                });

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

                // Validate New- and Get-AzureSqlDatabaseServerFirewallRule
                SqlDatabaseServerFirewallRuleContext[] firewallRules = getFirewallRuleResult
                                                                       .Select(r => r.BaseObject as SqlDatabaseServerFirewallRuleContext)
                                                                       .ToArray();
                Assert.AreEqual(2, firewallRules.Length, "Expecting two firewall rules");
                Assert.IsNotNull(firewallRules[0],
                                 "Expecting a SqlDatabaseServerFirewallRuleContext object.");
                Assert.IsNotNull(firewallRules[1],
                                 "Expecting a SqlDatabaseServerFirewallRuleContext object.");

                Assert.AreEqual(SqlDatabaseTestSettings.Instance.ServerName, firewallRules[0].ServerName);
                Assert.AreEqual("Rule1", firewallRules[0].RuleName);
                Assert.AreEqual("0.0.0.0", firewallRules[0].StartIpAddress);
                Assert.AreEqual("1.1.1.1", firewallRules[0].EndIpAddress);
                Assert.AreEqual("Success", firewallRules[0].OperationStatus);

                Assert.AreEqual(SqlDatabaseTestSettings.Instance.ServerName, firewallRules[1].ServerName);
                Assert.AreEqual("Rule2", firewallRules[1].RuleName);
                Assert.AreEqual("1.1.1.1", firewallRules[1].StartIpAddress);
                Assert.AreEqual("2.2.2.2", firewallRules[1].EndIpAddress);
                Assert.AreEqual("Success", firewallRules[1].OperationStatus);

                // Validate Set-AzureSqlDatabaseServerFirewallRule
                SqlDatabaseServerFirewallRuleContext firewallRule =
                    setFirewallResult.Single().BaseObject as SqlDatabaseServerFirewallRuleContext;
                Assert.IsNotNull(firewallRule, "Expecting a SqlDatabaseServerFirewallRuleContext object");
                Assert.AreEqual(SqlDatabaseTestSettings.Instance.ServerName, firewallRule.ServerName);
                Assert.AreEqual("Rule1", firewallRule.RuleName);
                Assert.AreEqual("2.2.2.2", firewallRule.StartIpAddress);
                Assert.AreEqual("3.3.3.3", firewallRule.EndIpAddress);
                Assert.AreEqual("Success", firewallRule.OperationStatus);

                // Validate Remove-AzureSqlDatabaseServerFirewallRule
                Assert.AreEqual(0, removeFirewallResult.Count,
                                "Expect all firewall rules are dropped.");
            }
        }
Exemple #5
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);
            }
        }
Exemple #6
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);
            }
        }
        public void ImportExportAzureSqlDatabaseTests()
        {
            using (PowerShell powershell = PowerShell.Create())
            {
                // Setup the subscription used for the test
                WindowsAzureSubscription subscription =
                    UnitTestHelper.SetupUnitTestSubscription(powershell);

                // Set the necessary session variables
                powershell.Runspace.SessionStateProxy.SetVariable(
                    "serverName",
                    SqlDatabaseTestSettings.Instance.ServerName);
                powershell.Runspace.SessionStateProxy.SetVariable(
                    "sourceDB",
                    SqlDatabaseTestSettings.Instance.SourceDatabaseName);
                powershell.Runspace.SessionStateProxy.SetVariable(
                    "targetDB",
                    SqlDatabaseTestSettings.Instance.TargetDatabaseName);
                powershell.Runspace.SessionStateProxy.SetVariable(
                    "credential",
                    new PSCredential(
                        SqlDatabaseTestSettings.Instance.UserName,
                        SqlDatabaseTestSettings.Instance.SecurePassword));
                powershell.Runspace.SessionStateProxy.SetVariable(
                    "storageAccountName",
                    SqlDatabaseTestSettings.Instance.StorageName);
                powershell.Runspace.SessionStateProxy.SetVariable(
                    "storageAccountKey",
                    SqlDatabaseTestSettings.Instance.AccessKey);
                powershell.Runspace.SessionStateProxy.SetVariable(
                    "storageContainerName",
                    SqlDatabaseTestSettings.Instance.ContainerName);

                // Create a new server
                HttpSession testSession = MockServerHelper.DefaultSessionCollection.GetSession(
                    "UnitTest.ImportExportAzureSqlDatabaseTests");
                ServerTestHelper.SetDefaultTestSessionSettings(testSession);
                //testSession.ServiceBaseUri = new Uri("https://management.core.windows.net");
                testSession.RequestValidator =
                    new Action <HttpMessage, HttpMessage.Request>(
                        (expected, actual) =>
                {
                    Assert.AreEqual(expected.RequestInfo.Method, actual.Method);
                    string expectedRequestText = RequestTextToString(expected.RequestInfo);
                    string actualRequestText   = RequestTextToString(actual);
                    // When checking out from GitHub, different new line setting may lead to different char \r\n or \n
                    // Replace them with String.Empty before comparison
                    Assert.AreEqual(
                        Regex.Replace(expectedRequestText, @"\s+", String.Empty),
                        Regex.Replace(actualRequestText, @"\s+", String.Empty));
                    Assert.IsTrue(
                        actual.UserAgent.Contains(ApiConstants.UserAgentHeaderValue),
                        "Missing proper UserAgent string.");
                });

                PSObject storageContext = MockServerHelper.ExecuteWithMock(
                    testSession,
                    MockHttpServer.DefaultHttpsServerPrefixUri,
                    () =>
                {
                    return(powershell.InvokeBatchScript(
                               @"New-AzureStorageContext" +
                               @" -StorageAccountName $storageAccountName" +
                               @" -StorageAccountKey $storageAccountKey"));
                }).FirstOrDefault();

                //testSession.ServiceBaseUri = new Uri("https://lqtqbo6kkp.database.windows.net");
                Collection <PSObject> databaseContext = MockServerHelper.ExecuteWithMock(
                    testSession,
                    MockHttpServer.DefaultServerPrefixUri,
                    () =>
                {
                    powershell.Runspace.SessionStateProxy.SetVariable(
                        "manageUrl",
                        MockHttpServer.DefaultServerPrefixUri.AbsoluteUri);
                    return(powershell.InvokeBatchScript(
                               @"New-AzureSqlDatabaseServerContext" +
                               @" -ServerName $serverName" +
                               @" -ManageUrl $manageUrl" +
                               @" -Credential $credential"));
                });

                //testSession.ServiceBaseUri = new Uri("https://management.core.windows.net");
                Collection <PSObject> startExportResult = MockServerHelper.ExecuteWithMock(
                    testSession,
                    MockHttpServer.DefaultHttpsServerPrefixUri,
                    () =>
                {
                    powershell.Runspace.SessionStateProxy.SetVariable("storageContext", storageContext);
                    powershell.Runspace.SessionStateProxy.SetVariable(
                        "databaseContext",
                        databaseContext.FirstOrDefault());
                    return(powershell.InvokeBatchScript(
                               @"Start-AzureSqlDatabaseExport" +
                               @" -SqlConnectionContext $databaseContext" +
                               @" -DatabaseName $sourceDB" +
                               @" -StorageContext $storageContext" +
                               @" -StorageContainerName $storageContainerName" +
                               @" -BlobName backup1"));
                });

                Collection <PSObject> getExportStatusResult = MockServerHelper.ExecuteWithMock(
                    testSession,
                    MockHttpServer.DefaultHttpsServerPrefixUri,
                    () =>
                {
                    powershell.Runspace.SessionStateProxy.SetVariable(
                        "exportResult",
                        startExportResult.FirstOrDefault());
                    return(powershell.InvokeBatchScript(
                               @"Get-AzureSqlDatabaseImportExportStatus" +
                               @" -ServerName $serverName" +
                               @" -RequestId $exportResult.RequestGuid" +
                               @" -Username $exportResult.SqlCredentials.UserName" +
                               @" -Password $exportResult.SqlCredentials.Password"));
                });

                Collection <PSObject> startImportResult = MockServerHelper.ExecuteWithMock(
                    testSession,
                    MockHttpServer.DefaultHttpsServerPrefixUri,
                    () =>
                {
                    return(powershell.InvokeBatchScript(
                               @"Start-AzureSqlDatabaseImport" +
                               @" -SqlConnectionContext $databaseContext" +
                               @" -DatabaseName $targetDB" +
                               @" -Edition Business" +
                               @" -DatabaseMaxSize 10" +
                               @" -StorageContext $storageContext" +
                               @" -StorageContainerName $storageContainerName" +
                               @" -BlobName backup1"));
                });

                Collection <PSObject> getImportStatusResult = MockServerHelper.ExecuteWithMock(
                    testSession,
                    MockHttpServer.DefaultHttpsServerPrefixUri,
                    () =>
                {
                    powershell.Runspace.SessionStateProxy.SetVariable(
                        "importResult",
                        startImportResult.FirstOrDefault());
                    return(powershell.InvokeBatchScript(
                               @"Get-AzureSqlDatabaseImportExportStatus" +
                               @" -ServerName $serverName" +
                               @" -RequestId $importResult.RequestGuid" +
                               @" -Username $importResult.SqlCredentials.UserName" +
                               @" -Password $importResult.SqlCredentials.Password"));
                });

                Assert.AreEqual(0, powershell.Streams.Error.Count, "Unexpected Errors during run!");
                Assert.AreEqual(0, powershell.Streams.Warning.Count, "Unexpected Warnings during run!");
            }
        }
Exemple #8
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);

                // Uncomment one of these two when testing against onebox or production
                // When testing production use RDFE
                // testSession.ServiceBaseUri = new Uri("https://management.core.windows.net");
                // When testing onebox use Mock RDFE
                // testSession.ServiceBaseUri = new Uri("https://management.dev.mscds.com:12346/MockRDFE/");

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

                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> 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> P1 = MockServerHelper.ExecuteWithMock(
                    testSession,
                    MockHttpServer.DefaultHttpsServerPrefixUri,
                    () =>
                {
                    return(powershell.InvokeBatchScript(
                               @"$P1 = Get-AzureSqlDatabaseServiceObjective" +
                               @" -Server $serverName" +
                               @" -ServiceObjectiveName ""Reserved 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[1]",
                               @"$P2"));
                });

                Collection <PSObject> newPremiumP1DatabaseResult = MockServerHelper.ExecuteWithMock(
                    testSession,
                    MockHttpServer.DefaultHttpsServerPrefixUri,
                    () =>
                {
                    return(powershell.InvokeBatchScript(
                               string.Format(
                                   CultureInfo.InvariantCulture,
                                   @"New-AzureSqlDatabase" +
                                   @" -ServerName {0}" +
                                   @" -DatabaseName ""testdbcertPremiumDBP1""" +
                                   @" -Edition Premium" +
                                   @" -ServiceObjective $P1",
                                   "testserver")));
                });

                Collection <PSObject> newPremiumP2DatabaseResult = MockServerHelper.ExecuteWithMock(
                    testSession,
                    MockHttpServer.DefaultHttpsServerPrefixUri,
                    () =>
                {
                    return(powershell.InvokeBatchScript(
                               string.Format(
                                   CultureInfo.InvariantCulture,
                                   @"New-AzureSqlDatabase" +
                                   @" -ServerName {0}" +
                                   @" -DatabaseName ""testdbcertPremiumDBP2""" +
                                   @" -Edition Premium" +
                                   @" -ServiceObjective $P2",
                                   "testserver")));
                });
                // 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 = "testdbcertGetOperationDbName_" + Guid.NewGuid().ToString();
                string getOperationDbName = "testdbcertGetOperationDbName_5d8b5785-0490-402c-b42f-6a5f5d6fbed8";
                Collection <PSObject> newOperationDbResult = MockServerHelper.ExecuteWithMock(
                    testSession,
                    MockHttpServer.DefaultHttpsServerPrefixUri,
                    () =>
                {
                    return(powershell.InvokeBatchScript(
                               string.Format(
                                   CultureInfo.InvariantCulture,
                                   @"$getOperationDb = New-AzureSqlDatabase" +
                                   @" -ServerName testserver" +
                                   @" -DatabaseName ""{0}""",
                                   getOperationDbName),
                               @"$getOperationDb"));
                });

                Collection <PSObject> getDatabaseOperationByDbResult = MockServerHelper.ExecuteWithMock(
                    testSession,
                    MockHttpServer.DefaultHttpsServerPrefixUri,
                    () =>
                {
                    return(powershell.InvokeBatchScript(
                               string.Format(
                                   CultureInfo.InvariantCulture,
                                   @"Get-AzureSqlDatabaseOperation" +
                                   @" -ServerName testserver" +
                                   @" -Database $getOperationDb")));
                });

                Collection <PSObject> getDatabaseOperationByNameResult = MockServerHelper.ExecuteWithMock(
                    testSession,
                    MockHttpServer.DefaultHttpsServerPrefixUri,
                    () =>
                {
                    return(powershell.InvokeBatchScript(
                               string.Format(
                                   CultureInfo.InvariantCulture,
                                   @"$getOperation = Get-AzureSqlDatabaseOperation" +
                                   @" -ServerName testserver" +
                                   @" -DatabaseName ""{0}""",
                                   getOperationDbName),
                               @"$getOperation"));
                });

                Collection <PSObject> getDatabaseOperationByIdResult = MockServerHelper.ExecuteWithMock(
                    testSession,
                    MockHttpServer.DefaultHttpsServerPrefixUri,
                    () =>
                {
                    return(powershell.InvokeBatchScript(
                               string.Format(
                                   CultureInfo.InvariantCulture,
                                   @"Get-AzureSqlDatabaseOperation" +
                                   @" -ServerName testserver" +
                                   @" -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("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(
                        @"$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.
                Assert.AreEqual("testdbcert3", databases[0].Name);
                Assert.AreEqual("Web", databases[0].Edition);
                Assert.AreEqual(5, databases[0].MaxSizeGB);
                Assert.AreEqual("SQL_Latin1_General_CP1_CI_AS", databases[0].CollationName);

                databases = new Database[] { newDatabaseResult2.Single().BaseObject as Database };
                Assert.AreEqual(1, databases.Length, "Expecting one database");
                Assert.IsNotNull(databases[0],
                                 "Expecting a Database object.");
                Assert.AreEqual("testdbcert2", databases[0].Name);
                Assert.AreEqual("Business", databases[0].Edition);
                Assert.AreEqual(10, databases[0].MaxSizeGB);
                Assert.AreEqual("Japanese_CI_AS", databases[0].CollationName);

                // Validate Get-AzureSqlDatabase
                databases = getDatabaseResult.Select(r => r.BaseObject as Database).ToArray();
                Assert.AreEqual(3, 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.AreEqual("master", databases[0].Name);
                Assert.AreEqual("Web", databases[0].Edition);
                Assert.AreEqual(5, databases[0].MaxSizeGB);
                Assert.AreEqual("SQL_Latin1_General_CP1_CI_AS", databases[0].CollationName);
                Assert.AreEqual(true, databases[0].IsSystemObject);
                Assert.AreEqual("testdbcert1", databases[1].Name);
                Assert.AreEqual("Web", databases[1].Edition);
                Assert.AreEqual(1, databases[1].MaxSizeGB);
                Assert.AreEqual("SQL_Latin1_General_CP1_CI_AS", databases[1].CollationName);
                Assert.AreEqual(false, databases[1].IsSystemObject);
                Assert.AreEqual("testdbcert2", databases[2].Name);
                Assert.AreEqual("Business", databases[2].Edition);
                Assert.AreEqual(10, databases[2].MaxSizeGB);
                Assert.AreEqual("Japanese_CI_AS", databases[2].CollationName);
                Assert.AreEqual(false, databases[2].IsSystemObject);

                databases = new Database[] { getSingleDatabaseResult.Single().BaseObject as Database };
                Assert.AreEqual(1, databases.Length, "Expecting one database");
                Assert.IsNotNull(databases[0],
                                 "Expecting a Database object.");
                Assert.AreEqual("testdbcert1", databases[0].Name);
                Assert.AreEqual("Web", databases[0].Edition);
                Assert.AreEqual(1, databases[0].MaxSizeGB);
                Assert.AreEqual("SQL_Latin1_General_CP1_CI_AS", databases[0].CollationName);

                // 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.");
                Assert.AreEqual("testdbcert3", databases[0].Name);
                Assert.AreEqual("Web", databases[0].Edition);
                Assert.AreEqual(1, databases[0].MaxSizeGB);
                Assert.AreEqual("SQL_Latin1_General_CP1_CI_AS", databases[0].CollationName);

                databases = new Database[] { setDatabaseSizeResult.Single().BaseObject as Database };
                Assert.AreEqual(1, databases.Length, "Expecting one database");
                Assert.IsNotNull(databases[0],
                                 "Expecting a Database object.");
                Assert.AreEqual("testdbcert3", databases[0].Name);
                Assert.AreEqual("Web", databases[0].Edition);
                Assert.AreEqual(5, databases[0].MaxSizeGB);
                Assert.AreEqual("SQL_Latin1_General_CP1_CI_AS", databases[0].CollationName);

                // 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(SLOP1.Name, "Reserved P1");
                Assert.AreEqual(SLOP1.Description, "Resource capacity is reserved.");
                Assert.IsNotNull(SLOP1.DimensionSettings, "Expecting some Dimension Setting objects.");
                Assert.AreEqual(SLOP1.DimensionSettings.Count(), 1, "Expecting 1 Dimension Setting.");
                Assert.AreEqual(SLOP1.DimensionSettings[0].Description, "Resource capacity is reserved.", "Expecting Dimension Setting description as Resource capacity is reserved.");

                var SLOP2 = P2.Single().BaseObject as ServiceObjective;
                Assert.AreEqual(SLOP2.Name, "Reserved P2");
                Assert.AreEqual(SLOP2.Description, "Resource capacity is reserved.");
                Assert.IsNotNull(SLOP2.DimensionSettings, "Expecting some Dimension Setting objects.");
                Assert.AreEqual(SLOP2.DimensionSettings.Count(), 1, "Expecting 1 Dimension Setting.");
                Assert.AreEqual(SLOP2.DimensionSettings[0].Description, "Resource capacity is reserved.", "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.");
                Assert.AreEqual("master", databases[0].Name);
                Assert.AreEqual("Web", databases[0].Edition);
                Assert.AreEqual(5, databases[0].MaxSizeGB);
                Assert.AreEqual("SQL_Latin1_General_CP1_CI_AS", databases[0].CollationName);
            }
        }
        public void AzureSqlDatabaseServerTests()
        {
            // 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);

                // Create a new server
                HttpSession testSession = MockServerHelper.DefaultSessionCollection.GetSession(
                    "UnitTest.AzureSqlDatabaseServerTests");
                ServerTestHelper.SetDefaultTestSessionSettings(testSession);
                testSession.RequestValidator =
                    new Action <HttpMessage, HttpMessage.Request>(
                        (expected, actual) =>
                {
                    Assert.AreEqual(expected.RequestInfo.Method, actual.Method);
                    Assert.IsTrue(
                        actual.UserAgent.Contains(ApiConstants.UserAgentHeaderValue),
                        "Missing proper UserAgent string.");
                    Assert.IsTrue(
                        UnitTestHelper.GetUnitTestClientCertificate().Equals(actual.Certificate),
                        "Expected correct client certificate");
                });

                powershell.Runspace.SessionStateProxy.SetVariable("login", "mylogin");
                powershell.Runspace.SessionStateProxy.SetVariable("password", "Pa$$w0rd!");
                powershell.Runspace.SessionStateProxy.SetVariable("location", "East Asia");
                Collection <PSObject> newServerResult = MockServerHelper.ExecuteWithMock(
                    testSession,
                    MockHttpServer.DefaultHttpsServerPrefixUri,
                    () =>
                {
                    return(powershell.InvokeBatchScript(
                               @"New-AzureSqlDatabaseServer" +
                               @" -AdministratorLogin $login" +
                               @" -AdministratorLoginPassword $password" +
                               @" -Location $location"));
                });

                Collection <PSObject> getServerResult = MockServerHelper.ExecuteWithMock(
                    testSession,
                    MockHttpServer.DefaultHttpsServerPrefixUri,
                    () =>
                {
                    powershell.Runspace.SessionStateProxy.SetVariable("server", newServerResult);
                    return(powershell.InvokeBatchScript(
                               @"Get-AzureSqlDatabaseServer $server.ServerName"));
                });

                Collection <PSObject> setServerResult = MockServerHelper.ExecuteWithMock(
                    testSession,
                    MockHttpServer.DefaultHttpsServerPrefixUri,
                    () =>
                {
                    powershell.Runspace.SessionStateProxy.SetVariable("server", newServerResult);
                    powershell.Runspace.SessionStateProxy.SetVariable("password", "Pa$$w0rd2");
                    powershell.InvokeBatchScript(
                        @"$server | Set-AzureSqlDatabaseServer" +
                        @" -AdminPassword $password" +
                        @" -Force");
                    return(powershell.InvokeBatchScript(
                               @"$server | Get-AzureSqlDatabaseServer"));
                });

                Collection <PSObject> removeServerResult = MockServerHelper.ExecuteWithMock(
                    testSession,
                    MockHttpServer.DefaultHttpsServerPrefixUri,
                    () =>
                {
                    powershell.Runspace.SessionStateProxy.SetVariable("server", newServerResult);
                    powershell.InvokeBatchScript(
                        @"$server | Remove-AzureSqlDatabaseServer" +
                        @" -Force");

                    return(powershell.InvokeBatchScript(
                               @"Get-AzureSqlDatabaseServer"));
                });

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

                // Validate New-AzureSqlDatabaseServer results
                SqlDatabaseServerContext server =
                    newServerResult.Single().BaseObject as SqlDatabaseServerContext;
                Assert.IsNotNull(server, "Expecting a SqlDatabaseServerContext object");

                Assert.AreEqual(
                    (string)powershell.Runspace.SessionStateProxy.GetVariable("login"),
                    server.AdministratorLogin,
                    ignoreCase: true,
                    message: "Expecting matching login.");
                Assert.AreEqual(
                    (string)powershell.Runspace.SessionStateProxy.GetVariable("location"),
                    server.Location,
                    ignoreCase: true,
                    message: "Expecting matching location.");
                Assert.AreEqual(10, server.ServerName.Length, "Expecting a valid server name.");

                // Validate Get-AzureSqlDatabaseServer results
                server = getServerResult.Single().BaseObject as SqlDatabaseServerContext;
                Assert.IsNotNull(server, "Expecting a SqlDatabaseServerContext object");
                Assert.AreEqual(
                    (string)powershell.Runspace.SessionStateProxy.GetVariable("login"),
                    server.AdministratorLogin,
                    ignoreCase: true,
                    message: "Expecting matching login.");
                Assert.AreEqual(
                    (string)powershell.Runspace.SessionStateProxy.GetVariable("location"),
                    server.Location,
                    ignoreCase: true,
                    message: "Expecting matching location.");
                Assert.AreEqual(10, server.ServerName.Length, "Expecting a valid server name.");

                server = setServerResult.Single().BaseObject as SqlDatabaseServerContext;
                Assert.IsNotNull(server, "Expecting a SqlDatabaseServerContext object");
                Assert.AreEqual(
                    (string)powershell.Runspace.SessionStateProxy.GetVariable("login"),
                    server.AdministratorLogin,
                    ignoreCase: true,
                    message: "Expecting matching login.");
                Assert.AreEqual(
                    (string)powershell.Runspace.SessionStateProxy.GetVariable("location"),
                    server.Location,
                    ignoreCase: true,
                    message: "Expecting matching location.");
                Assert.AreEqual(10, server.ServerName.Length, "Expecting a valid server name.");

                // Validate Remove-AzureSqlDatabaseServer results
                Assert.IsFalse(
                    removeServerResult.Any((o) => o.GetVariableValue <string>("ServerName") == server.ServerName),
                    "Server should have been removed.");

                powershell.Streams.ClearStreams();
            }
        }
Exemple #10
0
        public void ImportExportAzureSqlDatabaseTestv12()
        {
            using (System.Management.Automation.PowerShell powershell =
                       System.Management.Automation.PowerShell.Create())
            {
                // Setup the subscription used for the test
                AzureSubscription subscription =
                    UnitTestHelper.SetupUnitTestSubscription(powershell);

                // Set the necessary session variables
                powershell.Runspace.SessionStateProxy.SetVariable(
                    "serverName",
                    SqlDatabaseTestSettings.Instance.ServerName);
                powershell.Runspace.SessionStateProxy.SetVariable(
                    "sourceDB",
                    SqlDatabaseTestSettings.Instance.SourceDatabaseName);
                powershell.Runspace.SessionStateProxy.SetVariable(
                    "targetDB",
                    SqlDatabaseTestSettings.Instance.TargetDatabaseName);
                powershell.Runspace.SessionStateProxy.SetVariable(
                    "credential",
                    new PSCredential(
                        SqlDatabaseTestSettings.Instance.UserName,
                        SqlDatabaseTestSettings.Instance.SecurePassword));
                powershell.Runspace.SessionStateProxy.SetVariable(
                    "storageAccountName",
                    SqlDatabaseTestSettings.Instance.StorageName);
                powershell.Runspace.SessionStateProxy.SetVariable(
                    "storageAccountKey",
                    SqlDatabaseTestSettings.Instance.AccessKey);
                powershell.Runspace.SessionStateProxy.SetVariable(
                    "storageContainerName",
                    SqlDatabaseTestSettings.Instance.ContainerName);

                // Create a new server
                HttpSession testSession = MockServerHelper.DefaultSessionCollection.GetSession(
                    "UnitTest.ImportExportAzureSqlDatabaseTestsv12");
                ServerTestHelper.SetDefaultTestSessionSettings(testSession);

                testSession.RequestValidator =
                    new Action <HttpMessage, HttpMessage.Request>(
                        (expected, actual) =>
                {
                    Assert.AreEqual(expected.RequestInfo.Method, actual.Method);
                    string expectedRequestText = RequestTextToString(expected.RequestInfo);
                    string actualRequestText   = RequestTextToString(actual);

                    // When checking out from GitHub, different new line setting may lead to different char \r\n or \n
                    // Replace them with String.Empty before comparison
                    Assert.AreEqual(
                        Regex.Replace(expectedRequestText, @"\s+", String.Empty),
                        Regex.Replace(actualRequestText, @"\s+", String.Empty));
                    Assert.IsTrue(
                        actual.UserAgent.Contains(ApiConstants.UserAgentHeaderValue),
                        "Missing proper UserAgent string.");
                });

                StorageCredentials  credential     = new StorageCredentials(SqlDatabaseTestSettings.Instance.StorageName, SqlDatabaseTestSettings.Instance.AccessKey);
                string              blobEndpoint   = String.Format("https://{0}.blob.{1}/", SqlDatabaseTestSettings.Instance.StorageName, "core.windows.net");
                string              tableEndpoint  = String.Format("https://{0}.table.{1}/", SqlDatabaseTestSettings.Instance.StorageName, "core.windows.net");
                string              queueEndpoint  = String.Format("http://{0}.queue.{1}/", SqlDatabaseTestSettings.Instance.StorageName, "core.windows.net");
                string              fileEndpoint   = String.Format("https://{0}.file.{1}/", SqlDatabaseTestSettings.Instance.StorageName, "core.windows.net");
                CloudStorageAccount account        = new CloudStorageAccount(credential, new Uri(blobEndpoint), new Uri(queueEndpoint), new Uri(tableEndpoint), new Uri(fileEndpoint));
                AzureStorageContext storageContext = new AzureStorageContext(account);

                // Tell the sql auth factory to create a v12 context (skip checking sql version using select query).
                //
                SqlAuthContextFactory.sqlVersion = SqlAuthContextFactory.SqlVersion.v12;

                Collection <PSObject> databaseContext = MockServerHelper.ExecuteWithMock(
                    testSession,
                    MockHttpServer.DefaultServerPrefixUri,
                    () =>
                {
                    powershell.Runspace.SessionStateProxy.SetVariable(
                        "manageUrl",
                        MockHttpServer.DefaultServerPrefixUri.AbsoluteUri);
                    return(powershell.InvokeBatchScript(
                               @"New-AzureSqlDatabaseServerContext" +
                               @" -ServerName $serverName" +
                               @" -ManageUrl $manageUrl" +
                               @" -Credential $credential"));
                });

                Collection <PSObject> startExportResult = MockServerHelper.ExecuteWithMock(
                    testSession,
                    MockHttpServer.DefaultHttpsServerPrefixUri,
                    () =>
                {
                    powershell.Runspace.SessionStateProxy.SetVariable("storageContext", storageContext);
                    powershell.Runspace.SessionStateProxy.SetVariable(
                        "databaseContext",
                        databaseContext.FirstOrDefault());
                    return(powershell.InvokeBatchScript(
                               @"Start-AzureSqlDatabaseExport" +
                               @" -SqlConnectionContext $databaseContext" +
                               @" -DatabaseName $sourceDB" +
                               @" -StorageContext $storageContext" +
                               @" -StorageContainerName $storageContainerName" +
                               @" -BlobName backup1"));
                });

                // Wait here for operation to finish

                Collection <PSObject> getExportStatusResult = MockServerHelper.ExecuteWithMock(
                    testSession,
                    MockHttpServer.DefaultHttpsServerPrefixUri,
                    () =>
                {
                    powershell.Runspace.SessionStateProxy.SetVariable(
                        "exportResult",
                        startExportResult.FirstOrDefault());
                    return(powershell.InvokeBatchScript(
                               @"Get-AzureSqlDatabaseImportExportStatus" +
                               @" -ServerName $serverName" +
                               @" -RequestId $exportResult.RequestGuid" +
                               @" -Username $exportResult.SqlCredentials.UserName" +
                               @" -Password $exportResult.SqlCredentials.Password"));
                });

                Collection <PSObject> startImportResult = MockServerHelper.ExecuteWithMock(
                    testSession,
                    MockHttpServer.DefaultHttpsServerPrefixUri,
                    () =>
                {
                    return(powershell.InvokeBatchScript(
                               @"Start-AzureSqlDatabaseImport" +
                               @" -SqlConnectionContext $databaseContext" +
                               @" -DatabaseName $targetDB" +
                               @" -Edition Business" +
                               @" -DatabaseMaxSize 10" +
                               @" -StorageContext $storageContext" +
                               @" -StorageContainerName $storageContainerName" +
                               @" -BlobName backup1"));
                });

                // Wait here for import to finish

                Collection <PSObject> getImportStatusResult = MockServerHelper.ExecuteWithMock(
                    testSession,
                    MockHttpServer.DefaultHttpsServerPrefixUri,
                    () =>
                {
                    powershell.Runspace.SessionStateProxy.SetVariable(
                        "importResult",
                        startImportResult.FirstOrDefault());
                    return(powershell.InvokeBatchScript(
                               @"Get-AzureSqlDatabaseImportExportStatus" +
                               @" -ServerName $serverName" +
                               @" -RequestId $importResult.RequestGuid" +
                               @" -Username $importResult.SqlCredentials.UserName" +
                               @" -Password $importResult.SqlCredentials.Password"));
                });

                Assert.AreEqual(0, powershell.Streams.Error.Count, "Unexpected Errors during run!");
                Assert.AreEqual(0, powershell.Streams.Warning.Count, "Unexpected Warnings during run!");
            }
        }
Exemple #11
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.ServiceBaseUri = new Uri("https://management.core.windows.net");
                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"));
                });

                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> 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> removeDatabaseResult = MockServerHelper.ExecuteWithMock(
                    testSession,
                    MockHttpServer.DefaultHttpsServerPrefixUri,
                    () =>
                {
                    powershell.Runspace.SessionStateProxy.SetVariable("db1", newDatabaseResult1.FirstOrDefault());
                    powershell.Runspace.SessionStateProxy.SetVariable("db2", newDatabaseResult2.FirstOrDefault());
                    powershell.InvokeBatchScript(
                        @"$db1 | Remove-AzureSqlDatabase" +
                        @" -Force");
                    powershell.InvokeBatchScript(
                        @"$db2 | 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.
                Assert.AreEqual("testdbcert3", databases[0].Name);
                Assert.AreEqual("Web", databases[0].Edition);
                Assert.AreEqual(5, databases[0].MaxSizeGB);
                Assert.AreEqual("SQL_Latin1_General_CP1_CI_AS", databases[0].CollationName);

                databases = new Database[] { newDatabaseResult2.Single().BaseObject as Database };
                Assert.AreEqual(1, databases.Length, "Expecting one database");
                Assert.IsNotNull(databases[0],
                                 "Expecting a Database object.");
                Assert.AreEqual("testdbcert2", databases[0].Name);
                Assert.AreEqual("Business", databases[0].Edition);
                Assert.AreEqual(10, databases[0].MaxSizeGB);
                Assert.AreEqual("Japanese_CI_AS", databases[0].CollationName);

                // Validate Get-AzureSqlDatabase
                databases = getDatabaseResult.Single().BaseObject as Database[];
                Assert.AreEqual(3, databases.Length, "Expecting three 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.AreEqual("master", databases[0].Name);
                Assert.AreEqual("Web", databases[0].Edition);
                Assert.AreEqual(5, databases[0].MaxSizeGB);
                Assert.AreEqual("SQL_Latin1_General_CP1_CI_AS", databases[0].CollationName);
                Assert.AreEqual(true, databases[0].IsSystemObject);
                Assert.AreEqual("testdbcert1", databases[1].Name);
                Assert.AreEqual("Web", databases[1].Edition);
                Assert.AreEqual(1, databases[1].MaxSizeGB);
                Assert.AreEqual("SQL_Latin1_General_CP1_CI_AS", databases[1].CollationName);
                Assert.AreEqual(false, databases[1].IsSystemObject);
                Assert.AreEqual("testdbcert2", databases[2].Name);
                Assert.AreEqual("Business", databases[2].Edition);
                Assert.AreEqual(10, databases[2].MaxSizeGB);
                Assert.AreEqual("Japanese_CI_AS", databases[2].CollationName);
                Assert.AreEqual(false, databases[2].IsSystemObject);

                databases = new Database[] { getSingleDatabaseResult.Single().BaseObject as Database };
                Assert.AreEqual(1, databases.Length, "Expecting one database");
                Assert.IsNotNull(databases[0],
                                 "Expecting a Database object.");
                Assert.AreEqual("testdbcert1", databases[0].Name);
                Assert.AreEqual("Web", databases[0].Edition);
                Assert.AreEqual(1, databases[0].MaxSizeGB);
                Assert.AreEqual("SQL_Latin1_General_CP1_CI_AS", databases[0].CollationName);

                // 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.");
                Assert.AreEqual("testdbcert3", databases[0].Name);
                Assert.AreEqual("Web", databases[0].Edition);
                Assert.AreEqual(1, databases[0].MaxSizeGB);
                Assert.AreEqual("SQL_Latin1_General_CP1_CI_AS", databases[0].CollationName);

                databases = new Database[] { setDatabaseSizeResult.Single().BaseObject as Database };
                Assert.AreEqual(1, databases.Length, "Expecting one database");
                Assert.IsNotNull(databases[0],
                                 "Expecting a Database object.");
                Assert.AreEqual("testdbcert3", databases[0].Name);
                Assert.AreEqual("Web", databases[0].Edition);
                Assert.AreEqual(5, databases[0].MaxSizeGB);
                Assert.AreEqual("SQL_Latin1_General_CP1_CI_AS", databases[0].CollationName);

                // Validate Remove-AzureSqlDatabase
                databases = removeDatabaseResult.Single().BaseObject as Database[];
                Assert.AreEqual(1, databases.Length, "Expecting no databases");
                Assert.IsNotNull(databases[0], "Expecting a Database object.");
                Assert.AreEqual("master", databases[0].Name);
                Assert.AreEqual("Web", databases[0].Edition);
                Assert.AreEqual(5, databases[0].MaxSizeGB);
                Assert.AreEqual("SQL_Latin1_General_CP1_CI_AS", databases[0].CollationName);
            }
        }