|
| 1 | +// ---------------------------------------------------------------------------------- |
| 2 | +// |
| 3 | +// Copyright Microsoft Corporation |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | +// ---------------------------------------------------------------------------------- |
| 14 | + |
| 15 | +using System; |
| 16 | +using System.Collections.Generic; |
| 17 | +using System.Linq; |
| 18 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 19 | +using Microsoft.WindowsAzure.Commands.Test.HDInsight.CmdLetTests; |
| 20 | +using Microsoft.WindowsAzure.Management.HDInsight; |
| 21 | +using Microsoft.WindowsAzure.Management.HDInsight.Cmdlet.Commands.CommandInterfaces; |
| 22 | +using Microsoft.WindowsAzure.Management.HDInsight.Cmdlet.DataObjects; |
| 23 | +using Microsoft.WindowsAzure.Management.HDInsight.Cmdlet.GetAzureHDInsightClusters; |
| 24 | +using Microsoft.WindowsAzure.Management.HDInsight.Cmdlet.GetAzureHDInsightClusters.Extensions; |
| 25 | +using Microsoft.WindowsAzure.Management.HDInsight.Cmdlet.ServiceLocation; |
| 26 | + |
| 27 | +namespace Microsoft.WindowsAzure.Commands.Test.HDInsight.CommandTests |
| 28 | +{ |
| 29 | + [TestClass] |
| 30 | + public class ManageAzureHDInsightRdpAccessCommandTests : HDInsightTestCaseBase |
| 31 | + { |
| 32 | + [TestCleanup] |
| 33 | + public override void TestCleanup() |
| 34 | + { |
| 35 | + base.TestCleanup(); |
| 36 | + } |
| 37 | + |
| 38 | + [TestMethod] |
| 39 | + [TestCategory("Integration")] |
| 40 | + [TestCategory("CheckIn")] |
| 41 | + [TestCategory("Rdfe")] |
| 42 | + public void CanGrantHDInsightRdpAccess() |
| 43 | + { |
| 44 | + var rdpUserName = "rdpuser"; |
| 45 | + IHDInsightCertificateCredential creds = GetValidCredentials(); |
| 46 | + AzureHDInsightCluster testCluster = GetClusterWithRdpAccessDisabled(creds); |
| 47 | + AzureHDInsightCluster cluster = EnableRdpAccessToCluster( |
| 48 | + creds, testCluster, rdpUserName, TestCredentials.AzurePassword, DateTime.UtcNow.AddDays(6)); |
| 49 | + Assert.IsNotNull(cluster); |
| 50 | + Assert.AreEqual(cluster.RdpUserName, rdpUserName); |
| 51 | + } |
| 52 | + |
| 53 | + [TestMethod] |
| 54 | + [TestCategory("Integration")] |
| 55 | + [TestCategory("CheckIn")] |
| 56 | + [TestCategory("Rdfe")] |
| 57 | + public void CanRevokeAccessToRdpServices() |
| 58 | + { |
| 59 | + IHDInsightCertificateCredential creds = GetValidCredentials(); |
| 60 | + AzureHDInsightCluster testCluster = GetClusterWithRdpAccessDisabled(creds); |
| 61 | + EnableRdpAccessToCluster(creds, testCluster, TestCredentials.AzureUserName, TestCredentials.AzurePassword, |
| 62 | + DateTime.UtcNow.AddDays(6)); |
| 63 | + AzureHDInsightCluster cluster = DisableRdpAccessToCluster(creds, testCluster); |
| 64 | + Assert.IsNotNull(cluster); |
| 65 | + Assert.IsTrue(string.IsNullOrEmpty(cluster.RdpUserName)); |
| 66 | + } |
| 67 | + |
| 68 | + [TestInitialize] |
| 69 | + public override void Initialize() |
| 70 | + { |
| 71 | + base.Initialize(); |
| 72 | + } |
| 73 | + |
| 74 | + private static AzureHDInsightCluster GetClusterWithRdpAccessDisabled(IHDInsightCertificateCredential creds) |
| 75 | + { |
| 76 | + IGetAzureHDInsightClusterCommand client = ServiceLocator.Instance.Locate<IAzureHDInsightCommandFactory>().CreateGet(); |
| 77 | + client.CurrentSubscription = GetCurrentSubscription(); |
| 78 | + client.EndProcessing(); |
| 79 | + List<AzureHDInsightCluster> clusters = client.Output.ToList(); |
| 80 | + AzureHDInsightCluster containerWithRdpAccessDisabled = clusters.FirstOrDefault(cluster => cluster.RdpUserName.IsNullOrEmpty()); |
| 81 | + if (containerWithRdpAccessDisabled == null) |
| 82 | + { |
| 83 | + containerWithRdpAccessDisabled = clusters.Last(); |
| 84 | + DisableRdpAccessToCluster(creds, containerWithRdpAccessDisabled); |
| 85 | + } |
| 86 | + |
| 87 | + return containerWithRdpAccessDisabled; |
| 88 | + } |
| 89 | + |
| 90 | + private static AzureHDInsightCluster DisableRdpAccessToCluster( |
| 91 | + IHDInsightCertificateCredential creds, AzureHDInsightCluster containerWithRdpAccessDisabled) |
| 92 | + { |
| 93 | + IManageAzureHDInsightRdpAccessCommand rdpManagementClient = |
| 94 | + ServiceLocator.Instance.Locate<IAzureHDInsightCommandFactory>().CreateManageRdpAccess(); |
| 95 | + rdpManagementClient.CurrentSubscription = GetCurrentSubscription(); |
| 96 | + rdpManagementClient.RdpCredential = GetAzurePsCredentials(); |
| 97 | + rdpManagementClient.Name = containerWithRdpAccessDisabled.Name; |
| 98 | + rdpManagementClient.Location = containerWithRdpAccessDisabled.Location; |
| 99 | + rdpManagementClient.Enable = false; |
| 100 | + rdpManagementClient.EndProcessing(); |
| 101 | + return rdpManagementClient.Output.First(); |
| 102 | + } |
| 103 | + |
| 104 | + private static AzureHDInsightCluster EnableRdpAccessToCluster( |
| 105 | + IHDInsightCertificateCredential creds, AzureHDInsightCluster containerWithRdpAccessDisabled, string rdpUserName, string rdpPassword, DateTime expiry) |
| 106 | + { |
| 107 | + IManageAzureHDInsightRdpAccessCommand rdpManagementClient = |
| 108 | + ServiceLocator.Instance.Locate<IAzureHDInsightCommandFactory>().CreateManageRdpAccess(); |
| 109 | + rdpManagementClient.CurrentSubscription = GetCurrentSubscription(); |
| 110 | + rdpManagementClient.RdpCredential = GetPSCredential(rdpUserName, rdpPassword); |
| 111 | + rdpManagementClient.Name = containerWithRdpAccessDisabled.Name; |
| 112 | + rdpManagementClient.Location = containerWithRdpAccessDisabled.Location; |
| 113 | + rdpManagementClient.RdpAccessExpiry = expiry; |
| 114 | + rdpManagementClient.Enable = true; |
| 115 | + |
| 116 | + rdpManagementClient.EndProcessing(); |
| 117 | + return rdpManagementClient.Output.First(); |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments