private void cleanupAgentPluginsFile() throws IOException { FileUtils.deleteQuietly(AGENT_PLUGINS.getLocalFile()); }
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; }
public boolean isChecksumEquals(String expectedSignature) { return matchChecksum(getLocalFile(), expectedSignature); }
protected synchronized boolean download(final DownloadableFile downloadableFile) throws Exception { File toDownload = downloadableFile.getLocalFile(); LOG.info("Downloading {}", toDownload); String url = downloadableFile.url(urlGenerator); final HttpRequestBase request = new HttpGet(url); request.setConfig(RequestConfig.custom().setConnectTimeout(HTTP_TIMEOUT_IN_MILLISECONDS).build()); try (CloseableHttpClient httpClient = httpClientBuilder.build(); CloseableHttpResponse response = httpClient.execute(request)) { LOG.info("Got server response"); if (response.getEntity() == null) { LOG.error("Unable to read file from the server response"); return false; } handleInvalidResponse(response, url); try (BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(downloadableFile.getLocalFile()))) { response.getEntity().writeTo(outStream); LOG.info("Piped the stream to {}", downloadableFile); } } return true; }
@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)); }
void fetchUpdateCheckHeaders(DownloadableFile downloadableFile) throws Exception { String url = downloadableFile.validatedUrl(urlGenerator); final HttpRequestBase request = new HttpHead(url); request.setConfig(RequestConfig.custom().setConnectTimeout(HTTP_TIMEOUT_IN_MILLISECONDS).build()); try ( CloseableHttpClient httpClient = httpClientBuilder.build(); CloseableHttpResponse response = httpClient.execute(request) ) { handleInvalidResponse(response, url); this.md5 = response.getFirstHeader(MD5_HEADER).getValue(); this.sslPort = response.getFirstHeader(SSL_PORT_HEADER).getValue(); this.extraProperties = HeaderUtil.parseExtraProperties(response.getFirstHeader(AGENT_EXTRA_PROPERTIES_HEADER)); } }
@Test public void shouldReturnFalseIfChecksumIsNotEqual() throws Exception { File inputFile = new File("src/test/resources/checksum.txt"); assertFalse(DownloadableFile.matchChecksum(inputFile, "nonmat")); }
@Test public void shouldCheckIfFileExists() throws Exception { assertTrue(DownloadableFile.AGENT.doesNotExist()); }
@Test public void shouldThrowExceptionIfUrlIsInvalid() throws Exception { ServerUrlGenerator serverUrlGenerator = mock(ServerUrlGenerator.class); when(serverUrlGenerator.serverUrlFor("admin/agent")).thenReturn("invalidUrl"); exception.expect(RuntimeException.class); exception.expectMessage("URL you provided to access Go Server: " + "invalidUrl" + " is not valid"); DownloadableFile.AGENT.validatedUrl(serverUrlGenerator); } }
@Test public void shouldReturnTrueIfChecksumIsEqual() throws Exception { File inputFile = new File("src/test/resources/checksum.txt"); assertTrue(DownloadableFile.matchChecksum(inputFile, "16508b3a80b828afd13318003b58626e")); }
@Override public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) { try { File pluginsFolder = new File(systemEnvironment.get(SystemEnvironment.AGENT_PLUGINS_PATH)); if (pluginsFolder.exists()) { FileUtils.forceDelete(pluginsFolder); } zipUtil.unzip(DownloadableFile.AGENT_PLUGINS.getLocalFile(), pluginsFolder); defaultPluginJarLocationMonitor.initialize(); pluginManager.startInfrastructure(false); } catch (IOException e) { LOG.warn("could not extract plugin zip", e); } catch (RuntimeException e) { LOG.warn("error while initializing agent plugins", e); } } }
@Test public void shouldValidateTheUrl() throws Exception { ServerUrlGenerator serverUrlGenerator = ServerUrlGeneratorMother.generatorFor("localhost", 9090); assertThat(DownloadableFile.AGENT.validatedUrl(serverUrlGenerator), is("http://localhost:9090/go/admin/agent")); }
@After public void tearDown() { FileUtils.deleteQuietly(new File(Downloader.AGENT_BINARY)); FileUtils.deleteQuietly(DownloadableFile.AGENT.getLocalFile()); }
private SetupOfAgentPluginsFile setupAgentsPluginFile() throws IOException { return new SetupOfAgentPluginsFile(AGENT_PLUGINS.getLocalFile()); }
@Test public void shouldAllExceptionsExceptionQuietly() throws Exception { doThrow(new IOException()).when(zipUtil).unzip(DownloadableFile.AGENT_PLUGINS.getLocalFile(), new File(SystemEnvironment.PLUGINS_PATH)); try { doThrow(new RuntimeException("message")).when(pluginJarLocationMonitor).initialize(); agentPluginsInitializer.onApplicationEvent(null); } catch (Exception e) { fail("should have handled IOException"); } } }
@Test public void shouldExtractPluginZip() throws Exception { agentPluginsInitializer.onApplicationEvent(null); verify(zipUtil).unzip(DownloadableFile.AGENT_PLUGINS.getLocalFile(), new File(SystemEnvironment.PLUGINS_PATH)); }
@Test public void shouldHandleIOExceptionQuietly() throws Exception { doThrow(new IOException()).when(zipUtil).unzip(DownloadableFile.AGENT_PLUGINS.getLocalFile(), new File(SystemEnvironment.PLUGINS_PATH)); try { agentPluginsInitializer.onApplicationEvent(null); } catch (Exception e) { fail("should have handled IOException"); } }
@Test public void shouldInitializePluginJarLocationMonitorAndStartPluginInfrastructureAfterPluginZipExtracted() throws Exception { InOrder inOrder = inOrder(zipUtil, pluginManager, pluginJarLocationMonitor); agentPluginsInitializer.onApplicationEvent(null); inOrder.verify(zipUtil).unzip(DownloadableFile.AGENT_PLUGINS.getLocalFile(), new File(SystemEnvironment.PLUGINS_PATH)); inOrder.verify(pluginJarLocationMonitor).initialize(); inOrder.verify(pluginManager).startInfrastructure(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)); }