Exemplo n.º 1
0
  /**
   * 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();
    }
  }