Search in sources :

Example 16 with PathsPromoteResult

use of org.commonjava.indy.promote.model.PathsPromoteResult in project indy by Commonjava.

the class PromotionManagerTest method promoteAllByPath_RaceToPromote_FirstLocksTargetStore.

@Test
public void promoteAllByPath_RaceToPromote_FirstLocksTargetStore() throws Exception {
    Random rand = new Random();
    final HostedRepository[] sources = { new HostedRepository(MAVEN_PKG_KEY, "source1"), new HostedRepository(MAVEN_PKG_KEY, "source2") };
    final String[] paths = { "/path/path1", "/path/path2", "/path3", "/path/path/4" };
    Stream.of(sources).forEach((source) -> {
        try {
            storeManager.storeArtifactStore(source, new ChangeSummary(ChangeSummary.SYSTEM_USER, "test setup"), false, true, new EventMetadata());
            Stream.of(paths).forEach((path) -> {
                byte[] buf = new byte[1024 * 1024 * 2];
                rand.nextBytes(buf);
                try {
                    contentManager.store(source, path, new ByteArrayInputStream(buf), TransferOperation.UPLOAD, new EventMetadata());
                } catch (IndyWorkflowException e) {
                    e.printStackTrace();
                    Assert.fail("failed to store generated file to: " + source + path);
                }
            });
        } catch (IndyDataException e) {
            e.printStackTrace();
            Assert.fail("failed to store hosted repository: " + source);
        }
    });
    final HostedRepository target = new HostedRepository(MAVEN_PKG_KEY, "target");
    storeManager.storeArtifactStore(target, new ChangeSummary(ChangeSummary.SYSTEM_USER, "test setup"), false, true, new EventMetadata());
    PathsPromoteResult[] results = new PathsPromoteResult[2];
    CountDownLatch cdl = new CountDownLatch(2);
    AtomicInteger counter = new AtomicInteger(0);
    Stream.of(sources).forEach((source) -> {
        int idx = counter.getAndIncrement();
        executor.execute(() -> {
            try {
                results[idx] = manager.promotePaths(new PathsPromoteRequest(source.getKey(), target.getKey(), paths), FAKE_BASE_URL);
            } catch (Exception e) {
                e.printStackTrace();
                Assert.fail("Promotion from source: " + source + " failed.");
            } finally {
                cdl.countDown();
            }
        });
        try {
            Thread.sleep(25);
        } catch (InterruptedException e) {
            Assert.fail("Test interrupted");
        }
    });
    assertThat("Promotions failed to finish.", cdl.await(30, TimeUnit.SECONDS), equalTo(true));
    // first one should succeed.
    PathsPromoteResult result = results[0];
    assertThat(result.getRequest().getSource(), equalTo(sources[0].getKey()));
    assertThat(result.getRequest().getTarget(), equalTo(target.getKey()));
    Set<String> pending = result.getPendingPaths();
    assertThat(pending == null || pending.isEmpty(), equalTo(true));
    Set<String> skipped = result.getSkippedPaths();
    assertThat(skipped == null || skipped.isEmpty(), equalTo(true));
    Set<String> completed = result.getCompletedPaths();
    assertThat(completed, notNullValue());
    assertThat(completed.size(), equalTo(paths.length));
    assertThat(result.getError(), nullValue());
    Stream.of(paths).forEach((path) -> {
        HostedRepository src = sources[0];
        Transfer sourceRef = downloadManager.getStorageReference(src, path);
        Transfer targetRef = downloadManager.getStorageReference(target, path);
        assertThat(targetRef.exists(), equalTo(true));
        try (InputStream sourceIn = sourceRef.openInputStream();
            InputStream targetIn = targetRef.openInputStream()) {
            int s = -1, t = -1;
            while ((s = sourceIn.read()) == (t = targetIn.read())) {
                if (s == -1) {
                    break;
                }
            }
            if (s != -1 && s != t) {
                Assert.fail(path + " doesn't match between source: " + src + " and target: " + target);
            }
        } catch (IOException e) {
            e.printStackTrace();
            Assert.fail("Failed to compare contents of: " + path + " between source: " + src + " and target: " + target);
        }
    });
    // second one should be completely skipped.
    result = results[1];
    assertThat(result.getRequest().getSource(), equalTo(sources[1].getKey()));
    assertThat(result.getRequest().getTarget(), equalTo(target.getKey()));
    pending = result.getPendingPaths();
    assertThat(pending == null || pending.isEmpty(), equalTo(true));
    skipped = result.getSkippedPaths();
    assertThat(skipped, notNullValue());
    assertThat(skipped.size(), equalTo(paths.length));
    completed = result.getCompletedPaths();
    assertThat(completed == null || completed.isEmpty(), equalTo(true));
    assertThat(result.getError(), nullValue());
}
Also used : PathsPromoteResult(org.commonjava.indy.promote.model.PathsPromoteResult) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PathsPromoteRequest(org.commonjava.indy.promote.model.PathsPromoteRequest) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) IndyDataException(org.commonjava.indy.data.IndyDataException) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) IOException(java.io.IOException) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) IndyDataException(org.commonjava.indy.data.IndyDataException) Random(java.util.Random) ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) Transfer(org.commonjava.maven.galley.model.Transfer) ChangeSummary(org.commonjava.indy.audit.ChangeSummary) HostedRepository(org.commonjava.indy.model.core.HostedRepository) Test(org.junit.Test)

Example 17 with PathsPromoteResult

use of org.commonjava.indy.promote.model.PathsPromoteResult in project indy by Commonjava.

the class IndyPromoteClientModule method promoteByPath.

public PathsPromoteResult promoteByPath(final StoreKey src, final StoreKey target, final boolean purgeSource, final String... paths) throws IndyClientException {
    final PathsPromoteRequest req = new PathsPromoteRequest(src, target, new HashSet<String>(Arrays.asList(paths))).setPurgeSource(purgeSource);
    final PathsPromoteResult result = http.postWithResponse(PATHS_PROMOTE_PATH, req, PathsPromoteResult.class, HttpStatus.SC_OK);
    return result;
}
Also used : PathsPromoteResult(org.commonjava.indy.promote.model.PathsPromoteResult) PathsPromoteRequest(org.commonjava.indy.promote.model.PathsPromoteRequest) HashSet(java.util.HashSet)

Example 18 with PathsPromoteResult

use of org.commonjava.indy.promote.model.PathsPromoteResult in project indy by Commonjava.

the class IndyPromoteClientModule method getPromotablePaths.

public Set<String> getPromotablePaths(final StoreType type, final String name) throws IndyClientException {
    final StoreKey sk = new StoreKey(type, name);
    final PathsPromoteResult result = promoteByPath(new PathsPromoteRequest(sk, sk).setDryRun(true));
    return result.getPendingPaths();
}
Also used : PathsPromoteResult(org.commonjava.indy.promote.model.PathsPromoteResult) PathsPromoteRequest(org.commonjava.indy.promote.model.PathsPromoteRequest) StoreKey(org.commonjava.indy.model.core.StoreKey)

Example 19 with PathsPromoteResult

use of org.commonjava.indy.promote.model.PathsPromoteResult in project indy by Commonjava.

the class PromoteAllTest method run.

@Test
public void run() throws Exception {
    final PathsPromoteResult result = client.module(IndyPromoteClientModule.class).promoteByPath(new PathsPromoteRequest(source.getKey(), target.getKey()));
    assertThat(result.getRequest().getSource(), equalTo(source.getKey()));
    assertThat(result.getRequest().getTarget(), equalTo(target.getKey()));
    final Set<String> pending = result.getPendingPaths();
    assertThat(pending == null || pending.isEmpty(), equalTo(true));
    final Set<String> completed = result.getCompletedPaths();
    assertThat(completed, notNullValue());
    assertThat(completed.size(), equalTo(2));
    assertThat(result.getError(), nullValue());
    assertThat(client.content().exists(target.getKey().getType(), target.getName(), first), equalTo(true));
    assertThat(client.content().exists(target.getKey().getType(), target.getName(), second), equalTo(true));
}
Also used : PathsPromoteResult(org.commonjava.indy.promote.model.PathsPromoteResult) PathsPromoteRequest(org.commonjava.indy.promote.model.PathsPromoteRequest) IndyPromoteClientModule(org.commonjava.indy.promote.client.IndyPromoteClientModule) Test(org.junit.Test)

Example 20 with PathsPromoteResult

use of org.commonjava.indy.promote.model.PathsPromoteResult in project indy by Commonjava.

the class PromoteResource method promotePaths.

@ApiOperation("Promote paths from a source repository into a target repository/group (subject to validation).")
@ApiResponse(code = 200, message = "Promotion operation finished (consult response content for success/failure).", response = PathsPromoteResult.class)
@ApiImplicitParam(name = "body", paramType = "body", value = "JSON request specifying source and target, with other configuration options", allowMultiple = false, required = true, dataType = "org.commonjava.indy.promote.model.PathsPromoteRequest")
@Path("/paths/promote")
@POST
@Consumes(ApplicationContent.application_json)
public Response promotePaths(@Context final HttpServletRequest request, @Context final UriInfo uriInfo) {
    PathsPromoteRequest req = null;
    Response response = null;
    try {
        final String json = IOUtils.toString(request.getInputStream());
        logger.info("Got promotion request:\n{}", json);
        req = mapper.readValue(json, PathsPromoteRequest.class);
    } catch (final IOException e) {
        response = formatResponse(e, "Failed to read DTO from request body.");
    }
    if (response != null) {
        return response;
    }
    try {
        PackageTypeDescriptor packageTypeDescriptor = PackageTypes.getPackageTypeDescriptor(req.getSource().getPackageType());
        final String baseUrl = uriInfo.getBaseUriBuilder().path(packageTypeDescriptor.getContentRestBasePath()).build(req.getSource().getType().singularEndpointName(), req.getSource().getName()).toString();
        final PathsPromoteResult result = manager.promotePaths(req, baseUrl);
        // TODO: Amend response status code based on presence of error? This would have consequences for client API...
        response = formatOkResponseWithJsonEntity(result, mapper);
    } catch (PromotionException | IndyWorkflowException e) {
        logger.error(e.getMessage(), e);
        response = formatResponse(e);
    }
    return response;
}
Also used : ResponseUtils.formatResponse(org.commonjava.indy.bind.jaxrs.util.ResponseUtils.formatResponse) Response(javax.ws.rs.core.Response) ApiResponse(io.swagger.annotations.ApiResponse) PathsPromoteResult(org.commonjava.indy.promote.model.PathsPromoteResult) PackageTypeDescriptor(org.commonjava.indy.model.core.PackageTypeDescriptor) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) PathsPromoteRequest(org.commonjava.indy.promote.model.PathsPromoteRequest) IOException(java.io.IOException) PromotionException(org.commonjava.indy.promote.data.PromotionException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) ApiOperation(io.swagger.annotations.ApiOperation) ApiImplicitParam(io.swagger.annotations.ApiImplicitParam) ApiResponse(io.swagger.annotations.ApiResponse)

Aggregations

PathsPromoteResult (org.commonjava.indy.promote.model.PathsPromoteResult)23 PathsPromoteRequest (org.commonjava.indy.promote.model.PathsPromoteRequest)18 Test (org.junit.Test)14 Transfer (org.commonjava.maven.galley.model.Transfer)10 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)9 ByteArrayInputStream (java.io.ByteArrayInputStream)7 ChangeSummary (org.commonjava.indy.audit.ChangeSummary)7 HostedRepository (org.commonjava.indy.model.core.HostedRepository)7 IOException (java.io.IOException)6 InputStream (java.io.InputStream)5 IndyWorkflowException (org.commonjava.indy.IndyWorkflowException)5 StoreKey (org.commonjava.indy.model.core.StoreKey)5 IndyPromoteClientModule (org.commonjava.indy.promote.client.IndyPromoteClientModule)5 HashSet (java.util.HashSet)4 ValidationResult (org.commonjava.indy.promote.model.ValidationResult)4 ApiImplicitParam (io.swagger.annotations.ApiImplicitParam)3 ApiOperation (io.swagger.annotations.ApiOperation)3 ApiResponse (io.swagger.annotations.ApiResponse)3 Consumes (javax.ws.rs.Consumes)3 POST (javax.ws.rs.POST)3