public boolean isChecksumEquals(String expectedSignature) { return matchChecksum(getLocalFile(), expectedSignature); }
private void cleanupAgentPluginsFile() throws IOException { FileUtils.deleteQuietly(AGENT_PLUGINS.getLocalFile()); }
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; }
@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); } } }
@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 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 shouldExtractPluginZip() throws Exception { agentPluginsInitializer.onApplicationEvent(null); verify(zipUtil).unzip(DownloadableFile.AGENT_PLUGINS.getLocalFile(), new File(SystemEnvironment.PLUGINS_PATH)); }
@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 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)); }
@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)); }