public void CreatesNewPSResourceGroup() { Mock <ICommandRuntime> commandRuntimeMock = new Mock <ICommandRuntime>(); AddAzureASAccountCommand cmdlet = new AddAzureASAccountCommand() { CommandRuntime = commandRuntimeMock.Object }; // Setup cmdlet.RolloutEnvironment = testAsAzureEnvironment; var password = new SecureString(); var testpwd = testPassword; testpwd.All(c => { password.AppendChar(c); return(true); }); cmdlet.Credential = new PSCredential(testUser, password); cmdlet.InvokeBeginProcessing(); cmdlet.ExecuteCmdlet(); cmdlet.InvokeEndProcessing(); // Act Assert.NotEmpty(AsAzureClientSession.Instance.Profile.Environments); Assert.NotNull(AsAzureClientSession.Instance.Profile.Environments[testAsAzureEnvironment]); var environment = (AsAzureEnvironment)AsAzureClientSession.Instance.Profile.Environments[testAsAzureEnvironment]; Assert.Equal("", environment.Endpoints[AsAzureEnvironment.AsRolloutEndpoints.AdAuthorityBaseUrl]); Assert.NotNull(environment.Endpoints[AsAzureEnvironment.AsRolloutEndpoints.RestartEndpointFormat]); }
public void RestartAzureASInstance_Succeeds() { Mock <ICommandRuntime> commandRuntimeMock = new Mock <ICommandRuntime>(); // Setup // Clear the the current profile AsAzureClientSession.Instance.Profile.Environments.Clear(); var mockAuthenticationProvider = new Mock <IAsAzureAuthenticationProvider>(); mockAuthenticationProvider.Setup( authProvider => authProvider.GetAadAuthenticatedToken( It.IsAny <AsAzureContext>(), It.IsAny <SecureString>(), #if NETSTANDARD It.IsAny <Action <string> >(), #else It.IsAny <PromptBehavior>(), #endif It.IsAny <string>(), It.IsAny <string>(), It.IsAny <Uri>())).Returns(testToken); AsAzureClientSession.Instance.SetAsAzureAuthenticationProvider(mockAuthenticationProvider.Object); commandRuntimeMock.Setup(f => f.ShouldProcess(It.IsAny <string>(), It.IsAny <string>())).Returns(true); // Set up AsAzureHttpClient mock var mockAsAzureHttpClient = new Mock <IAsAzureHttpClient>(); mockAsAzureHttpClient .Setup(obj => obj.CallPostAsync(It.IsAny <Uri>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <HttpContent>())) .Returns(Task <HttpResponseMessage> .FromResult(new HttpResponseMessage(HttpStatusCode.OK))); var mockTokenCacheItemProvider = new Mock <ITokenCacheItemProvider>(); mockTokenCacheItemProvider .Setup(obj => obj.GetTokenFromTokenCache(It.IsAny <TokenCache>(), It.IsAny <string>())) .Returns(testToken); var restartCmdlet = new RestartAzureAnalysisServer(mockAsAzureHttpClient.Object, mockTokenCacheItemProvider.Object) { CommandRuntime = commandRuntimeMock.Object }; var addAmdlet = new AddAzureASAccountCommand() { CommandRuntime = commandRuntimeMock.Object }; DoLogin(addAmdlet); restartCmdlet.Instance = testServer; // Act restartCmdlet.InvokeBeginProcessing(); restartCmdlet.ExecuteCmdlet(); restartCmdlet.InvokeEndProcessing(); }
private void DoLogin(AddAzureASAccountCommand addCmdlet) { Mock <ICommandRuntime> commandRuntimeMock = new Mock <ICommandRuntime>(); addCmdlet.RolloutEnvironment = testAsAzureEnvironment; var password = new SecureString(); var testpwd = testPassword; testpwd.All(c => { password.AppendChar(c); return(true); }); addCmdlet.Credential = new PSCredential(testUser, password); commandRuntimeMock.Setup(f => f.ShouldProcess(It.IsAny <string>(), It.IsAny <string>())).Returns(true); // Act addCmdlet.InvokeBeginProcessing(); addCmdlet.ExecuteCmdlet(); AsAzureClientSession.TokenCache.Deserialize(Encoding.ASCII.GetBytes(testToken)); addCmdlet.InvokeEndProcessing(); }
public void TestAddAzureASAccountCommand() { Mock <ICommandRuntime> commandRuntimeMock = new Mock <ICommandRuntime>(); var addAmdlet = new AddAzureASAccountCommand() { CommandRuntime = commandRuntimeMock.Object }; var expectedProfile = new AsAzureProfile { Context = new AsAzureContext( new AsAzureAccount() { Id = testUser, Tenant = null }, new AsAzureEnvironment(testAsAzureEnvironment)) }; expectedProfile.Context.Environment.Endpoints.Add(AsAzureEnvironment.AsRolloutEndpoints.AdAuthorityBaseUrl, AsAzureClientSession.GetAuthorityUrlForEnvironment(expectedProfile.Context.Environment)); expectedProfile.Context.Environment.Endpoints.Add(AsAzureEnvironment.AsRolloutEndpoints.RestartEndpointFormat, AsAzureClientSession.RestartEndpointPathFormat); expectedProfile.Environments.Add(testAsAzureEnvironment, expectedProfile.Context.Environment); expectedProfile.Context.TokenCache = Encoding.ASCII.GetBytes(testToken); // Setup // Clear the the current profile AsAzureClientSession.Instance.Profile.Environments.Clear(); var mockAuthenticationProvider = new Mock <IAsAzureAuthenticationProvider>(); mockAuthenticationProvider.Setup( authProvider => authProvider.GetAadAuthenticatedToken( It.IsAny <AsAzureContext>(), It.IsAny <SecureString>(), #if NETSTANDARD It.IsAny <Action <string> >(), #else It.IsAny <PromptBehavior>(), #endif It.IsAny <string>(), It.IsAny <string>(), It.IsAny <Uri>())).Returns(testToken); AsAzureClientSession.Instance.SetAsAzureAuthenticationProvider(mockAuthenticationProvider.Object); addAmdlet.RolloutEnvironment = testAsAzureEnvironment; var password = new SecureString(); var testpwd = testPassword; testpwd.All(c => { password.AppendChar(c); return(true); }); addAmdlet.Credential = new PSCredential(testUser, password); commandRuntimeMock.Setup(f => f.ShouldProcess(It.IsAny <string>(), It.IsAny <string>())).Returns(true); commandRuntimeMock.Setup(f => f.WriteObject(expectedProfile)); // Act addAmdlet.InvokeBeginProcessing(); Assert.Empty(AsAzureClientSession.Instance.Profile.Environments); addAmdlet.ExecuteCmdlet(); Assert.True(AsAzureClientSession.Instance.Profile.Environments.Count == 1); Assert.NotNull(AsAzureClientSession.Instance.Profile.Environments[testAsAzureEnvironment]); // Call InvokeBeginProcessing again to get coverage. It should use the existing environment in memory not create a new one. addAmdlet.InvokeBeginProcessing(); Assert.True(AsAzureClientSession.Instance.Profile.Environments.Count == 1); Assert.NotNull(AsAzureClientSession.Instance.Profile.Environments[testAsAzureEnvironment]); addAmdlet.InvokeEndProcessing(); var environment = (AsAzureEnvironment)AsAzureClientSession.Instance.Profile.Environments[testAsAzureEnvironment]; Assert.Equal(environment.Endpoints[AsAzureEnvironment.AsRolloutEndpoints.AdAuthorityBaseUrl], AsAzureClientSession.GetAuthorityUrlForEnvironment(environment)); Assert.NotNull(environment.Endpoints[AsAzureEnvironment.AsRolloutEndpoints.RestartEndpointFormat]); commandRuntimeMock.Verify(f => f.WriteObject(AsAzureClientSession.Instance.Profile)); mockAuthenticationProvider.Verify(authProvider => authProvider.GetAadAuthenticatedToken(It.IsAny <AsAzureContext>(), It.IsAny <SecureString>(), #if NETSTANDARD It.IsAny <Action <string> >(), #else It.IsAny <PromptBehavior>(), #endif It.IsAny <string>(), It.IsAny <string>(), It.IsAny <Uri>()), Times.Once); }
public void SynchronizeAzureASInstance_FailsAfterTooManyRetries() { Mock <ICommandRuntime> commandRuntimeMock = new Mock <ICommandRuntime>(); // Setup // Clear the the current profile AsAzureClientSession.Instance.Profile.Environments.Clear(); var mockAuthenticationProvider = new Mock <IAsAzureAuthenticationProvider>(); mockAuthenticationProvider.Setup( authProvider => authProvider.GetAadAuthenticatedToken( It.IsAny <AsAzureContext>(), It.IsAny <SecureString>(), #if NETSTANDARD It.IsAny <Action <string> >(), #else It.IsAny <PromptBehavior>(), #endif It.IsAny <string>(), It.IsAny <string>(), It.IsAny <Uri>())).Returns(testToken); AsAzureClientSession.Instance.SetAsAzureAuthenticationProvider(mockAuthenticationProvider.Object); commandRuntimeMock.Setup(f => f.ShouldProcess(It.IsAny <string>(), It.IsAny <string>())).Returns(true); // Set up AsAzureHttpClient mock var mockAsAzureHttpClient = new Mock <IAsAzureHttpClient>(); // set up cluster resolve respnose ClusterResolutionResult resolveResult = new ClusterResolutionResult() { ClusterFQDN = "resolved.westcentralus.asazure.windows.net", CoreServerName = testServer + ":rw", TenantId = Guid.NewGuid().ToString() }; mockAsAzureHttpClient .Setup(obj => obj.CallPostAsync( It.IsAny <Uri>(), It.Is <string>(s => s.Contains("clusterResolve")), It.IsAny <string>(), It.IsAny <HttpContent>())) .Returns(Task <HttpResponseMessage> .FromResult( new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(JsonConvert.SerializeObject(resolveResult)) })); // set up sync respnose var postResponse = new HttpResponseMessage(HttpStatusCode.Accepted); postResponse.Headers.Location = new Uri("https://1"); postResponse.Headers.RetryAfter = new RetryConditionHeaderValue(TimeSpan.FromMilliseconds(500)); postResponse.Headers.Add("x-ms-root-activity-id", Guid.NewGuid().ToString()); postResponse.Headers.Add("x-ms-current-utc-date", Guid.NewGuid().ToString()); mockAsAzureHttpClient .Setup(obj => obj.CallPostAsync( It.IsAny <Uri>(), It.Is <string>(s => s.Contains("sync")), It.IsAny <string>(), It.IsAny <Guid>(), null)) .Returns(Task <Mock <HttpResponseMessage> > .FromResult(postResponse)); var getResponse1 = new HttpResponseMessage(HttpStatusCode.SeeOther); getResponse1.Headers.Location = new Uri("https://done"); getResponse1.Headers.RetryAfter = new RetryConditionHeaderValue(TimeSpan.FromMilliseconds(500)); getResponse1.Headers.Add("x-ms-root-activity-id", Guid.NewGuid().ToString()); getResponse1.Headers.Add("x-ms-current-utc-date", Guid.NewGuid().ToString()); mockAsAzureHttpClient .Setup(obj => obj.CallGetAsync( It.Is <Uri>(u => u.OriginalString.Contains("1")), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <Guid>())) .Returns(Task <HttpResponseMessage> .FromResult(getResponse1)); var getResponseSucceed = new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent( "{\n\"database\":\"db0\",\n\"syncstate\":\"Completed\"\n}", Encoding.UTF8, "application/json") }; getResponseSucceed.Headers.Add("x-ms-root-activity-id", Guid.NewGuid().ToString()); getResponseSucceed.Headers.Add("x-ms-current-utc-date", Guid.NewGuid().ToString()); var getResponseError = new HttpResponseMessage(HttpStatusCode.InternalServerError); getResponseError.Headers.Add("x-ms-root-activity-id", Guid.NewGuid().ToString()); getResponseError.Headers.Add("x-ms-current-utc-date", Guid.NewGuid().ToString()); var finalResponses = new Queue <HttpResponseMessage>(new[] { getResponseError, getResponseError, getResponseError, getResponseSucceed }); mockAsAzureHttpClient .Setup(obj => obj.CallGetAsync( It.Is <Uri>(u => u.OriginalString.Contains("done")), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <Guid>())) .Returns(() => Task.FromResult(finalResponses.Dequeue())); var mockTokenCacheItemProvider = new Mock <ITokenCacheItemProvider>(); mockTokenCacheItemProvider .Setup(obj => obj.GetTokenFromTokenCache(It.IsAny <TokenCache>(), It.IsAny <string>())) .Returns(testToken); var syncCmdlet = new SynchronizeAzureAzureAnalysisServer(mockAsAzureHttpClient.Object, mockTokenCacheItemProvider.Object) { CommandRuntime = commandRuntimeMock.Object }; var addAmdlet = new AddAzureASAccountCommand() { CommandRuntime = commandRuntimeMock.Object }; DoLogin(addAmdlet); syncCmdlet.Instance = testServer + ":rw"; syncCmdlet.Database = "db0"; // Act syncCmdlet.InvokeBeginProcessing(); Assert.Throws <SynchronizationFailedException>(() => syncCmdlet.ExecuteCmdlet()); syncCmdlet.InvokeEndProcessing(); }
public void ExportAzureASInstanceLogTest() { Mock <ICommandRuntime> commandRuntimeMock = new Mock <ICommandRuntime>(); // Setup // Clear the the current profile AsAzureClientSession.Instance.Profile.Environments.Clear(); var mockAuthenticationProvider = new Mock <IAsAzureAuthenticationProvider>(); mockAuthenticationProvider.Setup( authProvider => authProvider.GetAadAuthenticatedToken( It.IsAny <AsAzureContext>(), It.IsAny <SecureString>(), #if NETSTANDARD It.IsAny <Action <string> >(), #else It.IsAny <PromptBehavior>(), #endif It.IsAny <string>(), It.IsAny <string>(), It.IsAny <Uri>())).Returns(testToken); AsAzureClientSession.Instance.SetAsAzureAuthenticationProvider(mockAuthenticationProvider.Object); commandRuntimeMock.Setup(f => f.ShouldProcess(It.IsAny <string>(), It.IsAny <string>())).Returns(true); // Set up AsAzureHttpClient mock var mockAsAzureHttpClient = new Mock <IAsAzureHttpClient>(); mockAsAzureHttpClient .Setup(obj => obj.CallPostAsync( It.IsAny <Uri>(), It.Is <string>(s => s.Contains("clusterResolve")), It.IsAny <string>(), It.IsAny <HttpContent>())) .Returns(Task <HttpResponseMessage> .FromResult( new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("{\"clusterFQDN\": \"resolved.westcentralus.asazure.windows.net\"}") })); mockAsAzureHttpClient.Setup(obj => obj.CallGetAsync(It.IsAny <Uri>(), It.IsAny <string>(), It.IsAny <string>())).Returns( Task <HttpResponseMessage> .FromResult( new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("MOCKED STREAM CONTENT") })); var mockTokenCacheItemProvider = new Mock <ITokenCacheItemProvider>(); mockTokenCacheItemProvider .Setup(obj => obj.GetTokenFromTokenCache(It.IsAny <TokenCache>(), It.IsAny <string>())) .Returns(testToken); var exportLogCmdlet = new ExportAzureAnalysisServerLog(mockAsAzureHttpClient.Object, mockTokenCacheItemProvider.Object) { CommandRuntime = commandRuntimeMock.Object }; var addAmdlet = new AddAzureASAccountCommand() { CommandRuntime = commandRuntimeMock.Object }; DoLogin(addAmdlet); exportLogCmdlet.Instance = testServer; try { exportLogCmdlet.OutputPath = System.IO.Path.GetTempFileName(); exportLogCmdlet.InvokeBeginProcessing(); exportLogCmdlet.ExecuteCmdlet(); exportLogCmdlet.InvokeEndProcessing(); } finally { if (System.IO.File.Exists(exportLogCmdlet.OutputPath)) { System.IO.File.Delete(exportLogCmdlet.OutputPath); } } }