public static void Build() { var builder = new ConfigurationBuilder() .AddJsonFile(ConfigFile); IConfiguration configuration = builder.Build(); logging = configuration.GetSection("Logging"); var children = logging.GetChildren(); vstsConfig = new VstsConfig(); configuration.Bind("Vsts", vstsConfig); }
public void GetEnvironmentId_AuthenticationFails_ThrowsException() { IVstsReleaseClient releaseClient = Substitute.For <IVstsReleaseClient>(); IAuthenticator authenticator = Substitute.For <IAuthenticator>(); VstsConfig vstsConfig = new VstsConfig(); authenticator.Authenticate().Returns(new AuthenticationResult()); this.sut = new VstsReleaseRepository(releaseClient, authenticator, vstsConfig); Assert.Throws <AuthenticationException>(() => this.sut.GetReleaseEnvironmentId("999", "someEnvironment")); }
public void Deploy_DoesntGetReleaseId_DoesntGetEnvironmentId() { IReleaseRepository releaseRepository = Substitute.For <IReleaseRepository>(); VstsConfig vstsConfig = new VstsConfig(); releaseRepository.GetReleaseId(Arg.Any <string>(), Arg.Any <string>()).Returns((string)null); this.sut = new VstsDeploymentExecutor(releaseRepository, vstsConfig); sut.Deploy("someService", environment: "someEnvironment", version: "someVersion"); releaseRepository.DidNotReceiveWithAnyArgs().GetReleaseEnvironmentId( default(string), default(string)); }
public void Deploy_UpdatesEnvironment_ReturnsTrue() { IReleaseRepository releaseRepository = Substitute.For <IReleaseRepository>(); VstsConfig vstsConfig = new VstsConfig(); releaseRepository.GetReleaseId(Arg.Any <string>(), Arg.Any <string>()).Returns("123"); releaseRepository.GetReleaseEnvironmentId(Arg.Any <string>(), Arg.Any <string>()).Returns("123"); this.sut = new VstsDeploymentExecutor(releaseRepository, vstsConfig); bool retval = sut.Deploy("someService", environment: "someEnvironment", version: "someVersion"); Assert.True(retval); }
public void GetDeploymentStatus_DoesntGetReleaseId_ReturnsUnknown() { IReleaseRepository releaseRepository = Substitute.For <IReleaseRepository>(); VstsConfig vstsConfig = new VstsConfig(); releaseRepository.GetReleaseId(Arg.Any <string>(), Arg.Any <string>()) .Returns((string)null); this.sut = new VstsDeploymentExecutor(releaseRepository, vstsConfig); DeploymentStatus result = this.sut.GetDeploymentStatus("someService", "someEnvironment", "someVersion"); Assert.Equal(DeploymentStatus.Unknown, result); }
public void GetDeploymentStatus_GetsReleaseId_GetsEnvironmentId() { IReleaseRepository releaseRepository = Substitute.For <IReleaseRepository>(); VstsConfig vstsConfig = new VstsConfig(); releaseRepository.GetReleaseId(Arg.Any <string>(), Arg.Any <string>()) .Returns("123"); this.sut = new VstsDeploymentExecutor(releaseRepository, vstsConfig); this.sut.GetDeploymentStatus("someService", "someEnvironment", "someVersion"); releaseRepository.ReceivedWithAnyArgs().GetReleaseEnvironmentId(default(string), default(string)); }
public void GetReleaseId_NoReleaseDefintions_ReturnsNull() { IVstsReleaseClient releaseClient = Substitute.For <IVstsReleaseClient>(); IAuthenticator authenticator = Substitute.For <IAuthenticator>(); VstsConfig vstsConfig = new VstsConfig(); authenticator.Authenticate().Returns(new AuthenticationResult { Success = true }); this.sut = new VstsReleaseRepository(releaseClient, authenticator, vstsConfig); string result = this.sut.GetReleaseId("someService", version: "someVersion"); Assert.Null(result); }
public void GetReleaseId_AuthenticationSucceeds_GetsReleaseDefinitions() { IVstsReleaseClient releaseClient = Substitute.For <IVstsReleaseClient>(); IAuthenticator authenticator = Substitute.For <IAuthenticator>(); VstsConfig vstsConfig = new VstsConfig(); authenticator.Authenticate().Returns(new AuthenticationResult { Success = true }); this.sut = new VstsReleaseRepository(releaseClient, authenticator, vstsConfig); this.sut.GetReleaseId("someService", version: "someVersion"); releaseClient.ReceivedWithAnyArgs().GetReleaseDefinitions(default(string), default(string)); }
public void GetEnvironmentId_NoRelease_ReturnsNull() { IVstsReleaseClient releaseClient = Substitute.For <IVstsReleaseClient>(); IAuthenticator authenticator = Substitute.For <IAuthenticator>(); VstsConfig vstsConfig = new VstsConfig(); authenticator.Authenticate().Returns(new AuthenticationResult { Success = true }); this.sut = new VstsReleaseRepository(releaseClient, authenticator, vstsConfig); string result = this.sut.GetReleaseEnvironmentId("999", "someEnvironment"); Assert.Null(result); }
public void GetOAuthAccessTokens_RequestsTokens() { IHttpClient httpClient = Substitute.For <IHttpClient>(); VstsConfig vstsConfig = new VstsConfig { TokenUrl = @"https://app.vssps.visualstudio.com/oauth2/token", AuthorizationUrl = @"http://localhost:5000/api/auth", TokenBodyTemplate = "client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer&client_assertion={0}&grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion={1}&redirect_uri={2}" }; this.sut = new TestableVstsOAuthAuthorizationService(httpClient, vstsConfig); this.sut.GetOAuthAccessTokens("someCode", "someState|someSecret"); httpClient.Received().Execute(Arg.Is <HttpWebRequest>(r => r != null)); }
public void Deploy_WhatIf_DoesntUpdateEnvironment() { IReleaseRepository releaseRepository = Substitute.For <IReleaseRepository>(); VstsConfig vstsConfig = new VstsConfig(); releaseRepository.GetReleaseId(Arg.Any <string>(), Arg.Any <string>()).Returns("123"); releaseRepository.GetReleaseEnvironmentId(Arg.Any <string>(), Arg.Any <string>()).Returns("123"); this.sut = new VstsDeploymentExecutor(releaseRepository, vstsConfig, new Options { WhatIf = true }); sut.Deploy("someService", environment: "someEnvironment", version: "someVersion"); releaseRepository.DidNotReceiveWithAnyArgs().UpdateReleaseEnvironment(default(string), default(string)); }
public void GetReleaseEnvironmentStatus_AuthenticationSucceeds_GetsRelease() { IVstsReleaseClient releaseClient = Substitute.For <IVstsReleaseClient>(); IAuthenticator authenticator = Substitute.For <IAuthenticator>(); VstsConfig vstsConfig = new VstsConfig(); authenticator.Authenticate().Returns(new AuthenticationResult { Success = true }); this.sut = new VstsReleaseRepository(releaseClient, authenticator, vstsConfig); this.sut.GetReleaseEnvironmentStatus("123", "999"); releaseClient.ReceivedWithAnyArgs().GetRelease(default(int), default(string)); }
private static IDeploymentService BuildService() { VstsConfig vstsConfig = GetVstsConfig(); IFileSystem fileSystem = new FileSystem(); IHttpClient httpClient = new HttpClient(); IProductManifestRepository manifestRepository = new JsonFileProductManifestRepository(fileSystem); ITokenRepository tokenRepository = new JsonFileTokenRepository(fileSystem); IVstsReleaseClient releaseClient = new VstsSyncReleaseClient(vstsConfig); IAuthenticator authenticator = new VstsOAuthAuthenticator(httpClient, tokenRepository, vstsConfig); IReleaseRepository releaseRepository = new VstsReleaseRepository(releaseClient, authenticator, vstsConfig); IServiceDeploymentExecutor deploymentExecutor = new VstsDeploymentExecutor(releaseRepository, vstsConfig); IServiceDeploymentHandler deploymentHandler = new SequentialDeploymentHandler(deploymentExecutor, 10); IDeploymentService service = new DeploymentService(manifestRepository, deploymentHandler); return(service); }
public void GetReleaseEnvironmentStatus_ReleaseInProgress_ChecksForApprovals() { IVstsReleaseClient releaseClient = Substitute.For <IVstsReleaseClient>(); IAuthenticator authenticator = Substitute.For <IAuthenticator>(); VstsConfig vstsConfig = new VstsConfig(); authenticator.Authenticate().Returns(new AuthenticationResult { Success = true }); releaseClient.GetRelease(Arg.Any <int>(), Arg.Any <string>()).Returns(GetTestRelease(EnvironmentStatus.InProgress)); this.sut = new VstsReleaseRepository(releaseClient, authenticator, vstsConfig); SolutionDeploy.Core.DeploymentStatus result = this.sut.GetReleaseEnvironmentStatus("123", "123"); releaseClient.ReceivedWithAnyArgs().GetApprovals(default(int), default(string)); }
public void GetReleaseId_WithBranch_GetsBuilds() { IVstsReleaseClient releaseClient = Substitute.For <IVstsReleaseClient>(); IAuthenticator authenticator = Substitute.For <IAuthenticator>(); VstsConfig vstsConfig = new VstsConfig(); authenticator.Authenticate().Returns(new AuthenticationResult { Success = true }); releaseClient.GetReleaseDefinitions(Arg.Any <string>(), Arg.Any <string>()).Returns(GetTestReleaseDefinitions()); this.sut = new VstsReleaseRepository(releaseClient, authenticator, vstsConfig); this.sut.GetReleaseId("someService", branch: "someBranch"); releaseClient.ReceivedWithAnyArgs().GetBuilds(default(int), default(string), default(string), default(string)); }
public void GetReleaseId_WithPrereqEnv_GetsReleaseWithPrereq() { IVstsReleaseClient releaseClient = Substitute.For <IVstsReleaseClient>(); IAuthenticator authenticator = Substitute.For <IAuthenticator>(); VstsConfig vstsConfig = new VstsConfig(); authenticator.Authenticate().Returns(new AuthenticationResult { Success = true }); releaseClient.GetReleaseDefinitions(Arg.Any <string>(), Arg.Any <string>()).Returns(GetTestReleaseDefinitions()); this.sut = new VstsReleaseRepository(releaseClient, authenticator, vstsConfig); this.sut.GetReleaseId("someService", version: null, prereqEnvironment: "someEnvironment"); releaseClient.Received().GetReleases(Arg.Any <int>(), Arg.Any <string>(), null, Arg.Any <int>()); }
public void VstsConfig_loads_data_async() { var clientMock = new Mock <IVstsClient>(); clientMock.Setup(x => x.GetAsync()).ReturnsAsync(new Dictionary <string, string> { { "Feature1", "true" }, { "Feature2", "false" } }); var sut = new VstsConfig(new VstsSettings(), clientMock.Object); sut.SetupAsync().GetAwaiter().GetResult(); sut.Cache.Count.Should().Be(2); }
public void UpdateApproval_AuthenticationSucceeds_GetsApprovals() { IVstsReleaseClient releaseClient = Substitute.For <IVstsReleaseClient>(); IAuthenticator authenticator = Substitute.For <IAuthenticator>(); VstsConfig vstsConfig = new VstsConfig(); authenticator.Authenticate().Returns(new AuthenticationResult() { Success = true }); this.sut = new VstsReleaseRepository(releaseClient, authenticator, vstsConfig); this.sut.UpdateApproval("123"); releaseClient.ReceivedWithAnyArgs().GetApprovals(Arg.Any <int>(), Arg.Any <string>()); }
public void Authenticate_RefreshTokens_Succeeds() { IHttpClient httpClient = new HttpClient(); ITokenRepository tokenRepository = new JsonFileTokenRepository(new FileSystem()); VstsConfig vstsConfig = GetVstsConfig(); File.Copy($"..//..//..//testdata//{TokenFilename}", fullTokenFilename, true); this.sut = new VstsOAuthAuthenticator(httpClient, tokenRepository, vstsConfig); AuthenticationResult result = this.sut.Authenticate(); Assert.NotNull(result); Assert.NotNull(result.AccessToken); File.Copy(fullTokenFilename, $"..//..//..//testdata//{TokenFilename}", true); }
public void GetReleaseEnvironmentStatus_DoesntGetRelease_ReturnsUnknown() { IVstsReleaseClient releaseClient = Substitute.For <IVstsReleaseClient>(); IAuthenticator authenticator = Substitute.For <IAuthenticator>(); VstsConfig vstsConfig = new VstsConfig(); authenticator.Authenticate().Returns(new AuthenticationResult { Success = true }); releaseClient.GetRelease(Arg.Any <int>(), Arg.Any <string>()).Returns((Release)null); this.sut = new VstsReleaseRepository(releaseClient, authenticator, vstsConfig); SolutionDeploy.Core.DeploymentStatus result = this.sut.GetReleaseEnvironmentStatus("123", "999"); Assert.Equal(SolutionDeploy.Core.DeploymentStatus.Unknown, result); }
public VstsDeploymentExecutor( IReleaseRepository releaseRepository, VstsConfig vstsConfig, Options options = null) { if (releaseRepository == null) { throw new ArgumentNullException(nameof(releaseRepository)); } if (vstsConfig == null) { throw new ArgumentNullException(nameof(vstsConfig)); } this.releaseRepository = releaseRepository; this.vstsConfig = vstsConfig; this.options = options; }
public void GetReleaseEnvironmentStatus_ReleaseHasNoApprovals_ReturnsInProgress() { IVstsReleaseClient releaseClient = Substitute.For <IVstsReleaseClient>(); IAuthenticator authenticator = Substitute.For <IAuthenticator>(); VstsConfig vstsConfig = new VstsConfig(); authenticator.Authenticate().Returns(new AuthenticationResult { Success = true }); releaseClient.GetRelease(Arg.Any <int>(), Arg.Any <string>()).Returns(GetTestRelease(EnvironmentStatus.InProgress)); releaseClient.GetApprovals(Arg.Any <int>(), Arg.Any <string>()).Returns(new List <ReleaseApproval>()); this.sut = new VstsReleaseRepository(releaseClient, authenticator, vstsConfig); SolutionDeploy.Core.DeploymentStatus result = this.sut.GetReleaseEnvironmentStatus("123", "123"); Assert.Equal(SolutionDeploy.Core.DeploymentStatus.InProgress, result); }
public void GetEnvironmentId_GetsEnvironment_ReturnsEnvironmentId() { IVstsReleaseClient releaseClient = Substitute.For <IVstsReleaseClient>(); IAuthenticator authenticator = Substitute.For <IAuthenticator>(); VstsConfig vstsConfig = new VstsConfig(); authenticator.Authenticate().Returns(new AuthenticationResult { Success = true }); releaseClient.GetRelease(Arg.Any <int>(), Arg.Any <string>()) .Returns(GetTestRelease(EnvironmentStatus.Succeeded)); this.sut = new VstsReleaseRepository(releaseClient, authenticator, vstsConfig); string result = this.sut.GetReleaseEnvironmentId("999", "Environment1"); Assert.NotNull(result); }
public void GetReleaseId_GetsReleases_ReturnsReleaseId() { IVstsReleaseClient releaseClient = Substitute.For <IVstsReleaseClient>(); IAuthenticator authenticator = Substitute.For <IAuthenticator>(); VstsConfig vstsConfig = new VstsConfig(); authenticator.Authenticate().Returns(new AuthenticationResult { Success = true }); releaseClient.GetReleaseDefinitions(Arg.Any <string>(), Arg.Any <string>()).Returns(GetTestReleaseDefinitions()); releaseClient.GetBuilds(Arg.Any <int>(), Arg.Any <string>(), Arg.Any <string>()).Returns(GetTestBuilds()); releaseClient.GetReleases(Arg.Any <int>(), Arg.Any <string>(), Arg.Any <int>()).Returns(GetTestReleases()); this.sut = new VstsReleaseRepository(releaseClient, authenticator, vstsConfig); string result = this.sut.GetReleaseId("someService", version: "someVersion"); Assert.NotNull(result); }
public void GetDeploymentStatus_GetsEnvironmentStatus_ReturnsStatus() { IReleaseRepository releaseRepository = Substitute.For <IReleaseRepository>(); VstsConfig vstsConfig = new VstsConfig(); releaseRepository.GetReleaseId(Arg.Any <string>(), Arg.Any <string>()) .Returns("123"); releaseRepository.GetReleaseEnvironmentId(Arg.Any <string>(), Arg.Any <string>()) .Returns("123"); releaseRepository.GetReleaseEnvironmentStatus(Arg.Any <string>(), Arg.Any <string>()) .Returns(DeploymentStatus.Succeeded); this.sut = new VstsDeploymentExecutor(releaseRepository, vstsConfig); DeploymentStatus result = this.sut.GetDeploymentStatus( "someService", "someEnvironment", "someVersion"); Assert.Equal(DeploymentStatus.Succeeded, result); }
public VstsOAuthAuthenticator( IHttpClient httpClient, ITokenRepository tokenRepository, VstsConfig vstsConfig) { if (httpClient == null) { throw new ArgumentNullException(nameof(httpClient)); } if (tokenRepository == null) { throw new ArgumentNullException(nameof(tokenRepository)); } if (vstsConfig == null) { throw new ArgumentNullException(nameof(vstsConfig)); } this.httpClient = httpClient; this.tokenRepository = tokenRepository; this.vstsConfig = vstsConfig; }
internal void GetReleaseEnvironmentStatus_GetsEnvironment_ReturnsStatus( EnvironmentStatus envStatus, SolutionDeploy.Core.DeploymentStatus deployStatus) { IVstsReleaseClient releaseClient = Substitute.For <IVstsReleaseClient>(); IAuthenticator authenticator = Substitute.For <IAuthenticator>(); VstsConfig vstsConfig = new VstsConfig(); authenticator.Authenticate().Returns(new AuthenticationResult { Success = true }); releaseClient.GetRelease(Arg.Any <int>(), Arg.Any <string>()) .Returns(this.GetTestRelease(envStatus)); this.sut = new VstsReleaseRepository(releaseClient, authenticator, vstsConfig); SolutionDeploy.Core.DeploymentStatus result = this.sut.GetReleaseEnvironmentStatus("123", "123"); Assert.Equal(deployStatus, result); }
public void GetReleaseId__Succeeds() { VstsConfig vstsConfig = GetVstsConfig(); IVstsReleaseClient releaseClient = new VstsSyncReleaseClient(vstsConfig); IHttpClient httpClient = new HttpClient(); ITokenRepository tokenRepository = new JsonFileTokenRepository(new FileSystem()); IAuthenticator authenticator = new VstsOAuthAuthenticator(httpClient, tokenRepository, vstsConfig); File.Copy($"..//..//..//testdata//{TokenFilename}", fullTokenFilename, true); this.sut = new VstsReleaseRepository(releaseClient, authenticator, vstsConfig); string result = this.sut.GetReleaseId("samplepackage", "1.1.11"); File.Copy(fullTokenFilename, $"..//..//..//testdata//{TokenFilename}", true); Assert.NotNull(result); }
public void VstsConfig_IsEnabled_returns_value_from_cache() { var clientMock = new Mock <IVstsClient>(); clientMock.Setup(x => x.GetAsync()).ReturnsAsync(new Dictionary <string, string> { { "Feature1", "true" }, { "Feature2", "false" } }); var sut = new VstsConfig(new VstsSettings(), clientMock.Object); sut.SetupAsync().GetAwaiter().GetResult(); Feature.Name feature1 = new Feature.Name(typeof(Feature1), "Feature1"); Feature.Name feature2 = new Feature.Name(typeof(Feature2), "Feature2"); sut.IsEnabled(feature1).Should().BeTrue(); sut.IsEnabled(feature2).Should().BeFalse(); }
public void VstsConfig_IsEnabled_creates_missing_feature() { var clientMock = new Mock <IVstsClient>(); clientMock.Setup(x => x.GetAsync()).ReturnsAsync(new Dictionary <string, string> { { "Feature1", "true" } }); clientMock.Setup(x => x.PutAsync("Feature2", "False")).ReturnsAsync(true); var sut = new VstsConfig(new VstsSettings(), clientMock.Object); sut.SetupAsync().GetAwaiter().GetResult(); Feature.Name feature2 = new Feature.Name(typeof(Feature2), "Feature2"); sut.IsEnabled(feature2).Should().BeFalse(); clientMock.Verify(x => x.PutAsync("Feature2", "False")); }