private void enqueueNotFoundDownload(String pluginKey, String pluginHash) { doReturn(Optional.empty()).when(pluginFiles).get(argThat(p -> pluginKey.equals(p.key) && pluginHash.equals(p.hash))); } }
private void enqueueDownload(String pluginKey, String pluginHash) throws IOException { File jar = temp.newFile(); Manifest manifest = new Manifest(); manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); manifest.getMainAttributes().putValue("Plugin-Key", pluginKey); try (JarOutputStream output = new JarOutputStream(FileUtils.openOutputStream(jar), manifest)) { } doReturn(Optional.of(jar)).when(pluginFiles).get(argThat(p -> pluginKey.equals(p.key) && pluginHash.equals(p.hash))); }
private Loaded loadPlugins(Map<String, ScannerPlugin> result) { for (InstalledPlugin plugin : listInstalledPlugins()) { if (pluginPredicate.apply(plugin.key)) { Optional<File> jarFile = pluginFiles.get(plugin); if (!jarFile.isPresent()) { return new Loaded(false, plugin.key); } PluginInfo info = PluginInfo.create(jarFile.get()); result.put(info.getKey(), new ScannerPlugin(plugin.key, plugin.updatedAt, info)); } } return new Loaded(true, null); }
@Test public void filter_blacklisted_plugins() throws IOException { WsTestUtil.mockReader(wsClient, "api/plugins/installed", new InputStreamReader(getClass().getResourceAsStream("ScannerPluginInstallerTest/installed-plugins-ws.json"))); enqueueDownload("scmgit", "abc"); enqueueDownload("java", "def"); when(pluginPredicate.apply("scmgit")).thenReturn(true); when(pluginPredicate.apply("java")).thenReturn(false); Map<String, ScannerPlugin> result = underTest.installRemotes(); assertThat(result.keySet()).containsExactlyInAnyOrder("scmgit"); verify(pluginFiles, times(1)).get(any()); }
@Test public void get_jar_from_cache_if_present() throws Exception { FileAndMd5 jar = createFileInCache("foo"); File result = underTest.get(newInstalledPlugin("foo", jar.md5)).get(); verifySameContent(result, jar); // no requests to server assertThat(server.getRequestCount()).isEqualTo(0); }
@Test public void download_and_add_jar_to_cache_if_missing() throws Exception { FileAndMd5 tempJar = new FileAndMd5(); enqueueDownload(tempJar); InstalledPlugin plugin = newInstalledPlugin("foo", tempJar.md5); File result = underTest.get(plugin).get(); verifySameContent(result, tempJar); HttpUrl requestedUrl = server.takeRequest().getRequestUrl(); assertThat(requestedUrl.encodedPath()).isEqualTo("/api/plugins/download"); assertThat(requestedUrl.encodedQuery()).isEqualTo("plugin=foo&acceptCompressions=pack200"); // get from cache on second call result = underTest.get(plugin).get(); verifySameContent(result, tempJar); assertThat(server.getRequestCount()).isEqualTo(1); }
@Test public void download_compressed_and_add_uncompressed_to_cache_if_missing() throws Exception { FileAndMd5 jar = new FileAndMd5(); enqueueCompressedDownload(jar, true); InstalledPlugin plugin = newInstalledPlugin("foo", jar.md5); File result = underTest.get(plugin).get(); verifySameContentAfterCompression(jar.file, result); RecordedRequest recordedRequest = server.takeRequest(); assertThat(recordedRequest.getRequestUrl().queryParameter("acceptCompressions")).isEqualTo("pack200"); // get from cache on second call result = underTest.get(plugin).get(); verifySameContentAfterCompression(jar.file, result); assertThat(server.getRequestCount()).isEqualTo(1); }
@Test public void download_a_new_version_of_plugin_during_blue_green_switch() throws IOException { FileAndMd5 tempJar = new FileAndMd5(); enqueueDownload(tempJar); // expecting to download plugin foo with checksum "abc" InstalledPlugin pluginV1 = newInstalledPlugin("foo", "abc"); File result = underTest.get(pluginV1).get(); verifySameContent(result, tempJar); // new version of downloaded jar is put in cache with the new md5 InstalledPlugin pluginV2 = newInstalledPlugin("foo", tempJar.md5); result = underTest.get(pluginV2).get(); verifySameContent(result, tempJar); assertThat(server.getRequestCount()).isEqualTo(1); // v1 still requests server and downloads v2 enqueueDownload(tempJar); result = underTest.get(pluginV1).get(); verifySameContent(result, tempJar); assertThat(server.getRequestCount()).isEqualTo(2); }
@Test public void fail_if_integrity_of_compressed_download_is_not_valid() throws Exception { FileAndMd5 jar = new FileAndMd5(); enqueueCompressedDownload(jar, false); expectISE("foo", "was expected to have checksum invalid_hash but had "); InstalledPlugin plugin = newInstalledPlugin("foo", jar.md5); underTest.get(plugin).get(); }
@Test public void return_empty_if_plugin_not_found_on_server() { server.enqueue(new MockResponse().setResponseCode(404)); InstalledPlugin plugin = newInstalledPlugin("foo", "abc"); Optional<File> result = underTest.get(plugin); assertThat(result).isEmpty(); }
@Test public void fail_if_md5_header_is_missing_from_response() throws IOException { File tempJar = temp.newFile(); enqueueDownload(tempJar, null); InstalledPlugin plugin = newInstalledPlugin("foo", "abc"); expectISE("foo", "did not return header Sonar-MD5"); underTest.get(plugin); }
@Test public void fail_if_integrity_of_download_is_not_valid() throws IOException { FileAndMd5 tempJar = new FileAndMd5(); enqueueDownload(tempJar.file, "invalid_hash"); InstalledPlugin plugin = newInstalledPlugin("foo", "abc"); expectISE("foo", "was expected to have checksum invalid_hash but had " + tempJar.md5); underTest.get(plugin); }
@Test public void fail_if_compressed_download_cannot_be_uncompressed() { MockResponse response = new MockResponse().setBody("not binary"); response.setHeader("Sonar-MD5", DigestUtils.md5Hex("not binary")); response.setHeader("Sonar-UncompressedMD5", "abc"); response.setHeader("Sonar-Compression", "pack200"); server.enqueue(response); expectISE("foo", "Pack200 error"); InstalledPlugin plugin = newInstalledPlugin("foo", "abc"); underTest.get(plugin).get(); }
@Test public void fail_if_cached_file_is_outside_cache_dir() throws IOException { FileAndMd5 tempJar = new FileAndMd5(); enqueueDownload(tempJar); InstalledPlugin plugin = newInstalledPlugin("foo/bar", "abc"); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("Fail to download plugin [foo/bar]. Key is not valid."); underTest.get(plugin); }
@Test public void fail_if_server_returns_error() { server.enqueue(new MockResponse().setResponseCode(500)); InstalledPlugin plugin = newInstalledPlugin("foo", "abc"); expectISE("foo", "returned code 500"); underTest.get(plugin); }
private Loaded loadPlugins(Map<String, ScannerPlugin> result) { for (InstalledPlugin plugin : listInstalledPlugins()) { if (pluginPredicate.apply(plugin.key)) { Optional<File> jarFile = pluginFiles.get(plugin); if (!jarFile.isPresent()) { return new Loaded(false, plugin.key); } PluginInfo info = PluginInfo.create(jarFile.get()); result.put(info.getKey(), new ScannerPlugin(plugin.key, plugin.updatedAt, info)); } } return new Loaded(true, null); }