/** * Fetches and compares prefix file from Nx and the "original" from Central and compares the two: * they must be binary equal, Nexus must not modify the content at all. * * @throws IOException */ @Test public void prefixFileIsUnchanged() throws IOException { // central final Status centralStatus = whitelist().getWhitelistStatus("central"); // let's verify that Nexus did not modify the prefix file got from Central (req: Nexus must // publish prefix file // as-is, as it was received from remote). both should be equal on byte level. final InputStream nexusPrefixFile = getPrefixFileFrom(centralStatus.getPublishedUrl()); final InputStream centralPrefixFile = getPrefixFileFrom("http://repo1.maven.org/maven2/.meta/prefixes.txt"); try { ByteStreams.equal( new InputSupplier<InputStream>() { @Override public InputStream getInput() throws IOException { return nexusPrefixFile; } }, new InputSupplier<InputStream>() { @Override public InputStream getInput() throws IOException { return centralPrefixFile; } }); } finally { Closeables.closeQuietly(nexusPrefixFile); Closeables.closeQuietly(centralPrefixFile); } }
/** * Testing initial boot of Nexus with WL feature. Asserting that Central (and hence Public group, * that has Central and only one proxy member) has WL published, since Central discovery * succeeded. * * @throws Exception */ @Test public void whitelistLooksSane() throws Exception { // central Status centralStatus = whitelist().getWhitelistStatus("central"); assertThat(centralStatus.getPublishedStatus(), equalTo(Outcome.SUCCEEDED)); assertThat(centralStatus.getPublishedUrl(), is(notNullValue())); assertThat(centralStatus.getDiscoveryStatus(), is(notNullValue())); assertThat( centralStatus.getDiscoveryStatus().getDiscoveryLastStatus(), equalTo(Outcome.SUCCEEDED)); // let's check some sanity (just blindly check that some expected entries are present) final InputStream entityStream = getPrefixFileFrom(centralStatus.getPublishedUrl()); try { final LineNumberReader lnr = new LineNumberReader(new InputStreamReader(entityStream, "UTF-8")); boolean hasAbbot = false; boolean hasComApple = false; boolean hasOrgSonatype = false; String currentLine = lnr.readLine(); while (currentLine != null) { hasAbbot = hasAbbot || "/abbot".equals(currentLine); hasComApple = hasComApple || "/com/apple".equals(currentLine); hasOrgSonatype = hasOrgSonatype || "/org/sonatype".equals(currentLine); currentLine = lnr.readLine(); } // check is this what we think should be assertThat("Line /abbot is missing?", hasAbbot); assertThat("Line /com/apple is missing?", hasComApple); assertThat("Line /org/sonatype is missing?", hasOrgSonatype); // count lines lnr.skip(Long.MAX_VALUE); // 2013. 02. 08. Today, Nexus scraped prefix file with 5517 lines (depth=2) // So, safely assuming the prefix file MUST HAVE more than 5k lines // Naturally, if depth changes, making it lesser, this might fail. // 2012. 02. 14. Today the prefix file is deployed to Central, no more scraping // The prefix file has around 1600 entries. assertThat(lnr.getLineNumber() + 1, is(greaterThanOrEqualTo(1000))); } finally { Closeables.closeQuietly(entityStream); } }
/** * A proxy "transitions" from having prefixes file to not having prefixes file (and not being * scraped either). The test does two passes of requests. In first pass (with WL), the proxy will * forward only whitelisted requests to it's remote target. Then repository transitions into a * state of not having WL (like remote prefix file removed and will not be scraped as test Jetty * does not have index file), with nuked proxy caches same set of requests is repeated. This time, * it is validated that all requests are forwared to remote target (pre-WL behaviour of Nexus). * Simply put, proxy repository falls back to pre-WL (pre-2.4) behavior. * * @throws Exception */ @Test public void proxyWithAndWithoutWL() throws Exception { // where to put downloaded things final File remoteRepoRoot = testData().resolveFile("remote-repo"); final File downloadsDir = testIndex().getDirectory("downloads"); // bring up remote server using Jetty final PathRecorder recorder = new PathRecorder(); final PrefixesFile prefixesFile = new PrefixesFile(); // now set the prefixes file that contains /org/someorg prefix only, and repeat prefixesFile.setContent( Files.toString(testData().resolveFile("someorg-prefixes.txt"), Charset.forName("UTF-8"))); final Server server = Server.withPort(0) .serve("/*") .withBehaviours(recorder, prefixesFile, Behaviours.get(remoteRepoRoot)) .start(); // create the proxy final MavenProxyRepository proxyRepository = repositories() .create(MavenProxyRepository.class, repositoryIdForTest("someorgProxy1")) .asProxyOf(server.getUrl().toExternalForm()) .doNotDownloadRemoteIndexes() .save(); whitelistTest().waitForAllWhitelistUpdateJobToStop(); // waitForWLPublishingOutcomes( proxyRepository.id() ); client().getSubsystem(Scheduler.class).waitForAllTasksToStop(); try { // clear recorder recorder.clear(); // repeat the test with slightly different expectations { // check that newly added proxy is publishing whitelist assertThat( whitelist().getWhitelistStatus(proxyRepository.id()).getPublishedStatus(), equalTo(Outcome.SUCCEEDED)); // nuke the repo cache nukeProxyCaches(proxyRepository.id()); // and because we have WL, we cant fetch whatever we want (com and org) // only WL-enlisted of these will go remotely fetchAndAssert(downloadsDir, proxyRepository.id(), COM_SOMEORG_ARTIFACT_10_POM, false); fetchAndAssert(downloadsDir, proxyRepository.id(), COM_SOMEORG_ARTIFACT_10_JAR, false); fetchAndAssert(downloadsDir, proxyRepository.id(), ORG_SOMEORG_ARTIFACT_10_POM, true); fetchAndAssert(downloadsDir, proxyRepository.id(), ORG_SOMEORG_ARTIFACT_10_JAR, true); fetchAndAssert(downloadsDir, proxyRepository.id(), FLUKE_ARTIFACT_POM, false); fetchAndAssert(downloadsDir, proxyRepository.id(), FLUKE_ARTIFACT_JAR, false); // GET /org/someorg/artifact/1.0/artifact-1.0.jar.sha1, // GET /org/someorg/artifact/1.0/artifact-1.0.jar, // GET /org/someorg/artifact/1.0/artifact-1.0.pom.sha1, // GET /org/someorg/artifact/1.0/artifact-1.0.pom, final List<String> requests = recorder.getPathsForVerb("GET"); assertThat(requests.size(), is(4)); assertThat( requests, containsInAnyOrder( ORG_SOMEORG_ARTIFACT_10_POM, ORG_SOMEORG_ARTIFACT_10_POM + ".sha1", ORG_SOMEORG_ARTIFACT_10_JAR, ORG_SOMEORG_ARTIFACT_10_JAR + ".sha1")); } // now loose the prefixes file prefixesFile.setContent(null); // update the WL of proxy repo to have new prefixes file picked up whitelist().updateWhitelist(proxyRepository.id()); // wait for update to finish since it's async op, client above returned immediately // but update happens in a separate thread. Still this should be quick operation as prefix // file is used Status proxyStatus = whitelist().getWhitelistStatus(proxyRepository.id()); // sit and wait for remote discovery (or the timeout Junit @Rule will kill us) while (proxyStatus.getPublishedStatus() != Outcome.FAILED) { Thread.sleep(10000); proxyStatus = whitelist().getWhitelistStatus(proxyRepository.id()); } // clear recorder recorder.clear(); // remote repo lives without prefix file { // check that newly added proxy is not publishing whitelist assertThat( whitelist().getWhitelistStatus(proxyRepository.id()).getPublishedStatus(), equalTo(Outcome.FAILED)); // nuke the repo cache nukeProxyCaches(proxyRepository.id()); // and because no WL, we can fetch whatever we want (com and org) // all these will go remotely fetchAndAssert(downloadsDir, proxyRepository.id(), COM_SOMEORG_ARTIFACT_10_POM, true); fetchAndAssert(downloadsDir, proxyRepository.id(), COM_SOMEORG_ARTIFACT_10_JAR, true); fetchAndAssert(downloadsDir, proxyRepository.id(), ORG_SOMEORG_ARTIFACT_10_POM, true); fetchAndAssert(downloadsDir, proxyRepository.id(), ORG_SOMEORG_ARTIFACT_10_JAR, true); fetchAndAssert(downloadsDir, proxyRepository.id(), FLUKE_ARTIFACT_POM, false); fetchAndAssert(downloadsDir, proxyRepository.id(), FLUKE_ARTIFACT_JAR, false); // note: sha1 is asked for existing files only // GET /hu/fluke/artifact/1.0/artifact-1.0.jar, // GET /hu/fluke/artifact/1.0/artifact-1.0.pom, // GET /org/someorg/artifact/1.0/artifact-1.0.jar.sha1, // GET /org/someorg/artifact/1.0/artifact-1.0.jar, // GET /org/someorg/artifact/1.0/artifact-1.0.pom.sha1, // GET /org/someorg/artifact/1.0/artifact-1.0.pom, // GET /com/someorg/artifact/1.0/artifact-1.0.jar.sha1, // GET /com/someorg/artifact/1.0/artifact-1.0.jar, // GET /com/someorg/artifact/1.0/artifact-1.0.pom.sha1, // GET /com/someorg/artifact/1.0/artifact-1.0.pom, final List<String> requests = recorder.getPathsForVerb("GET"); assertThat(requests.size(), is(10)); assertThat( requests, containsInAnyOrder( COM_SOMEORG_ARTIFACT_10_POM, COM_SOMEORG_ARTIFACT_10_POM + ".sha1", COM_SOMEORG_ARTIFACT_10_JAR, COM_SOMEORG_ARTIFACT_10_JAR + ".sha1", ORG_SOMEORG_ARTIFACT_10_POM, ORG_SOMEORG_ARTIFACT_10_POM + ".sha1", ORG_SOMEORG_ARTIFACT_10_JAR, ORG_SOMEORG_ARTIFACT_10_JAR + ".sha1", FLUKE_ARTIFACT_POM, FLUKE_ARTIFACT_JAR)); } } finally { server.stop(); } }