@Test public void shouldReturnFalseIfTheServerDoesNotRespondWithEntity() throws Exception { GoAgentServerHttpClientBuilder builder = mock(GoAgentServerHttpClientBuilder.class); CloseableHttpClient closeableHttpClient = mock(CloseableHttpClient.class); when(builder.build()).thenReturn(closeableHttpClient); CloseableHttpResponse httpResponse = mock(CloseableHttpResponse.class); when(closeableHttpClient.execute(any(HttpRequestBase.class))).thenReturn(httpResponse); ServerBinaryDownloader downloader = new ServerBinaryDownloader(builder, ServerUrlGeneratorMother.generatorFor("localhost", server.getPort())); assertThat(downloader.download(DownloadableFile.AGENT), is(false)); }
@Test public void shouldSetMd5AndSSLPortHeaders() throws Exception { ServerBinaryDownloader downloader = new ServerBinaryDownloader(ServerUrlGeneratorMother.generatorFor("localhost", server.getPort()), null, SslVerificationMode.NONE); downloader.downloadIfNecessary(DownloadableFile.AGENT); MessageDigest digester = MessageDigest.getInstance("MD5"); try (BufferedInputStream stream = new BufferedInputStream(new FileInputStream(DownloadableFile.AGENT.getLocalFile()))) { try (DigestInputStream digest = new DigestInputStream(stream, digester)) { IOUtils.copy(digest, new NullOutputStream()); } assertThat(downloader.getMd5(), is(Hex.encodeHexString(digester.digest()).toLowerCase())); } assertThat(downloader.getSslPort(), is(String.valueOf(server.getSecurePort()))); }
@Test public void shouldConnectToAnSSLServerWithSelfSignedCertWhenInsecureModeIsNoVerifyHost() throws Exception { ServerBinaryDownloader downloader = new ServerBinaryDownloader(ServerUrlGeneratorMother.generatorFor("localhost", server.getPort()), new File("testdata/test_cert.pem"), SslVerificationMode.NO_VERIFY_HOST); downloader.download(DownloadableFile.AGENT); assertThat(DownloadableFile.AGENT.getLocalFile().exists(), is(true)); }
SslVerificationMode sslVerificationMode = backwardCompatibility.sslVerificationMode(); ServerBinaryDownloader agentDownloader = new ServerBinaryDownloader(urlGenerator, rootCertFile, sslVerificationMode); agentDownloader.downloadIfNecessary(DownloadableFile.AGENT); ServerBinaryDownloader pluginZipDownloader = new ServerBinaryDownloader(urlGenerator, rootCertFile, sslVerificationMode); pluginZipDownloader.downloadIfNecessary(DownloadableFile.AGENT_PLUGINS); ServerBinaryDownloader tfsImplDownloader = new ServerBinaryDownloader(urlGenerator, rootCertFile, sslVerificationMode); tfsImplDownloader.downloadIfNecessary(DownloadableFile.TFS_IMPL);
SslVerificationMode sslVerificationMode = backwardCompatibility.sslVerificationMode(); ServerBinaryDownloader launcherDownloader = new ServerBinaryDownloader(urlGenerator, rootCertFile, sslVerificationMode); if (launcherDownloader.downloadIfNecessary(DownloadableFile.LAUNCHER)) { return LAUNCHER_NOT_UP_TO_DATE; ServerBinaryDownloader agentDownloader = new ServerBinaryDownloader(urlGenerator, rootCertFile, sslVerificationMode); agentDownloader.downloadIfNecessary(DownloadableFile.AGENT);
@Test public void shouldRaiseExceptionWhenSelfSignedCertDoesNotMatchTheHostName() throws Exception { exception.expect(Exception.class); exception.expectMessage("Certificate for <localhost> doesn't match any of the subject alternative names: []"); ServerBinaryDownloader downloader = new ServerBinaryDownloader(ServerUrlGeneratorMother.generatorFor("https://localhost:" + server.getSecurePort() + "/go/hello"), new File("testdata/test_cert.pem"), SslVerificationMode.FULL); downloader.download(DownloadableFile.AGENT); }
@Test public void shouldDownloadAgentJarFile() { ServerBinaryDownloader downloader = new ServerBinaryDownloader(ServerUrlGeneratorMother.generatorFor("localhost", server.getPort()), null, SslVerificationMode.NONE); assertThat(DownloadableFile.AGENT.doesNotExist(), is(true)); downloader.downloadIfNecessary(DownloadableFile.AGENT); assertThat(DownloadableFile.AGENT.getLocalFile().exists(), is(true)); }
@Test public void shouldReturnTrueIfTheFileIsDownloaded() { ServerBinaryDownloader downloader = new ServerBinaryDownloader(ServerUrlGeneratorMother.generatorFor("localhost", server.getPort()), null, SslVerificationMode.NONE); assertThat(downloader.downloadIfNecessary(DownloadableFile.AGENT), is(true)); }
@Test(expected = Exception.class) public void shouldThrowExceptionIfTheServerIsDown() throws Exception { ServerBinaryDownloader downloader = new ServerBinaryDownloader(ServerUrlGeneratorMother.generatorFor("locahost", server.getPort()), null, SslVerificationMode.NONE); downloader.download(DownloadableFile.AGENT); }
private void assertExtraProperties(String valueToSet, Map<String, String> expectedValue) { ServerBinaryDownloader downloader = new ServerBinaryDownloader(ServerUrlGeneratorMother.generatorFor("localhost", server.getPort()), null, SslVerificationMode.NONE); try { server.setExtraPropertiesHeaderValue(valueToSet); downloader.downloadIfNecessary(DownloadableFile.AGENT); assertThat(downloader.getExtraProperties(), is(expectedValue)); } finally { server.setExtraPropertiesHeaderValue(null); } } }
@Test public void shouldThrowExceptionInCaseOf404() throws Exception { exception.expect(Exception.class); exception.expectMessage("This agent might be incompatible with your GoCD Server." + " Please fix the version mismatch between GoCD Server and GoCD Agent."); ServerBinaryDownloader downloader = new ServerBinaryDownloader(ServerUrlGeneratorMother.generatorWithoutSubPathFor("https://localhost:" + server.getSecurePort() + "/go/not-found"), null, SslVerificationMode.NONE); downloader.download(DownloadableFile.AGENT); }
@Test public void shouldFailIfMD5HeadersAreMissing() throws Exception { exception.expect(Exception.class); exception.expectMessage("Missing required headers 'Content-MD5' and 'Cruise-Server-Ssl-Port' in response."); ServerBinaryDownloader downloader = new ServerBinaryDownloader(ServerUrlGeneratorMother.generatorWithoutSubPathFor("https://localhost:" + server.getSecurePort() + "/go/hello"), null, SslVerificationMode.NONE); downloader.fetchUpdateCheckHeaders(DownloadableFile.AGENT); }
@Test public void shouldFailIfServerIsNotAvailable() throws Exception { exception.expect(UnknownHostException.class); exception.expectMessage("invalidserver"); ServerBinaryDownloader downloader = new ServerBinaryDownloader(ServerUrlGeneratorMother.generatorWithoutSubPathFor("https://invalidserver:" + server.getSecurePort() + "/go/hello"), null, SslVerificationMode.NONE); downloader.fetchUpdateCheckHeaders(DownloadableFile.AGENT); }