public boolean downloadIfNecessary(final DownloadableFile downloadableFile) { boolean updated = false; boolean downloaded = false; while (!updated) try { fetchUpdateCheckHeaders(downloadableFile); if (downloadableFile.doesNotExist() || !downloadableFile.isChecksumEquals(getMd5())) { PerfTimer timer = PerfTimer.start("Downloading new " + downloadableFile + " with md5 signature: " + md5); downloaded = download(downloadableFile); timer.stop(); } updated = true; } catch (Exception e) { LOG.error("Couldn't update {}. Sleeping for 1m. Error: ", downloadableFile, e); try { int period = Integer.parseInt(System.getProperty("sleep.for.download", "60000")); Thread.sleep(period); } catch (InterruptedException ie) { /* we don't care. Stupid checked exception.*/ } } return downloaded; }
@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 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)); }
@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(expected = Exception.class) public void shouldThrowExceptionIfTheServerIsDown() throws Exception { ServerBinaryDownloader downloader = new ServerBinaryDownloader(ServerUrlGeneratorMother.generatorFor("locahost", server.getPort()), null, SslVerificationMode.NONE); downloader.download(DownloadableFile.AGENT); }
@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); }