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());
}
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;
}
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();
}
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));
}
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;
}
Aggregations