Search in sources :

Example 46 with ChangeSummary

use of org.commonjava.indy.audit.ChangeSummary in project indy by Commonjava.

the class PromotionManagerTest method promoteAllByPath_CollidingPaths_VerifySecondSkipped.

/**
     * On collision, the promotion manager should skip the second file to be promoted (instead of overwriting the
     * existing one). This assumes no overwrite attribute is available for setting in the promotion request (or that
     * it is available but isn't set...and defaults to false).
     * @throws Exception
     */
@Test
public void promoteAllByPath_CollidingPaths_VerifySecondSkipped() throws Exception {
    final HostedRepository source1 = new HostedRepository(MAVEN_PKG_KEY, "source1");
    final HostedRepository source2 = new HostedRepository(MAVEN_PKG_KEY, "source2");
    storeManager.storeArtifactStore(source1, new ChangeSummary(ChangeSummary.SYSTEM_USER, "test setup"), false, true, new EventMetadata());
    storeManager.storeArtifactStore(source2, new ChangeSummary(ChangeSummary.SYSTEM_USER, "test setup"), false, true, new EventMetadata());
    String originalString = "This is a test";
    final String path = "/path/path";
    contentManager.store(source1, path, new ByteArrayInputStream(originalString.getBytes()), TransferOperation.UPLOAD, new EventMetadata());
    contentManager.store(source2, path, new ByteArrayInputStream("This is another test".getBytes()), TransferOperation.UPLOAD, new EventMetadata());
    final HostedRepository target = new HostedRepository(MAVEN_PKG_KEY, "target");
    storeManager.storeArtifactStore(target, new ChangeSummary(ChangeSummary.SYSTEM_USER, "test setup"), false, true, new EventMetadata());
    PathsPromoteResult result = manager.promotePaths(new PathsPromoteRequest(source1.getKey(), target.getKey(), path), FAKE_BASE_URL);
    assertThat(result.getRequest().getSource(), equalTo(source1.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(1));
    assertThat(result.getError(), nullValue());
    Transfer ref = downloadManager.getStorageReference(target, path);
    assertThat(ref.exists(), equalTo(true));
    try (InputStream in = ref.openInputStream()) {
        String value = IOUtils.toString(in);
        assertThat(value, equalTo(originalString));
    }
    result = manager.promotePaths(new PathsPromoteRequest(source1.getKey(), target.getKey(), path), FAKE_BASE_URL);
    assertThat(result.getRequest().getSource(), equalTo(source1.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(1));
    completed = result.getCompletedPaths();
    assertThat(completed == null || completed.isEmpty(), equalTo(true));
    assertThat(result.getError(), nullValue());
    ref = downloadManager.getStorageReference(target, path);
    assertThat(ref.exists(), equalTo(true));
    try (InputStream in = ref.openInputStream()) {
        String value = IOUtils.toString(in);
        assertThat(value, equalTo(originalString));
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) PathsPromoteResult(org.commonjava.indy.promote.model.PathsPromoteResult) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Transfer(org.commonjava.maven.galley.model.Transfer) PathsPromoteRequest(org.commonjava.indy.promote.model.PathsPromoteRequest) ChangeSummary(org.commonjava.indy.audit.ChangeSummary) HostedRepository(org.commonjava.indy.model.core.HostedRepository) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) Test(org.junit.Test)

Example 47 with ChangeSummary

use of org.commonjava.indy.audit.ChangeSummary in project indy by Commonjava.

the class PromotionManagerTest method promoteAllByPath_PushTwoArtifactsToHostedRepo_DryRun_VerifyPendingPathsPopulated.

@Test
public void promoteAllByPath_PushTwoArtifactsToHostedRepo_DryRun_VerifyPendingPathsPopulated() throws Exception {
    final HostedRepository source = new HostedRepository(MAVEN_PKG_KEY, "source");
    storeManager.storeArtifactStore(source, new ChangeSummary(ChangeSummary.SYSTEM_USER, "test setup"), false, true, new EventMetadata());
    final String first = "/first/path";
    final String second = "/second/path";
    contentManager.store(source, first, new ByteArrayInputStream("This is a test".getBytes()), TransferOperation.UPLOAD, new EventMetadata());
    contentManager.store(source, second, new ByteArrayInputStream("This is a test".getBytes()), TransferOperation.UPLOAD, new EventMetadata());
    final HostedRepository target = new HostedRepository(MAVEN_PKG_KEY, "target");
    storeManager.storeArtifactStore(target, new ChangeSummary(ChangeSummary.SYSTEM_USER, "test setup"), false, true, new EventMetadata());
    final PathsPromoteResult result = manager.promotePaths(new PathsPromoteRequest(source.getKey(), target.getKey()).setDryRun(true), FAKE_BASE_URL);
    assertThat(result.getRequest().getSource(), equalTo(source.getKey()));
    assertThat(result.getRequest().getTarget(), equalTo(target.getKey()));
    final Set<String> completed = result.getCompletedPaths();
    assertThat(completed == null || completed.isEmpty(), equalTo(true));
    final Set<String> pending = result.getPendingPaths();
    assertThat(pending, notNullValue());
    assertThat(pending.size(), equalTo(2));
    assertThat(result.getError(), nullValue());
    Transfer ref = downloadManager.getStorageReference(target, first);
    assertThat(ref.exists(), equalTo(false));
    ref = downloadManager.getStorageReference(target, second);
    assertThat(ref.exists(), equalTo(false));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) PathsPromoteResult(org.commonjava.indy.promote.model.PathsPromoteResult) Transfer(org.commonjava.maven.galley.model.Transfer) PathsPromoteRequest(org.commonjava.indy.promote.model.PathsPromoteRequest) ChangeSummary(org.commonjava.indy.audit.ChangeSummary) HostedRepository(org.commonjava.indy.model.core.HostedRepository) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) Test(org.junit.Test)

Example 48 with ChangeSummary

use of org.commonjava.indy.audit.ChangeSummary in project indy by Commonjava.

the class PromotionManagerTest method promoteAllByPath_PurgeSource_PushTwoArtifactsToHostedRepo_VerifyCopiedToOtherHostedRepo.

@Test
public void promoteAllByPath_PurgeSource_PushTwoArtifactsToHostedRepo_VerifyCopiedToOtherHostedRepo() throws Exception {
    final HostedRepository source = new HostedRepository(MAVEN_PKG_KEY, "source");
    storeManager.storeArtifactStore(source, new ChangeSummary(ChangeSummary.SYSTEM_USER, "test setup"), false, true, new EventMetadata());
    final String first = "/first/path";
    final String second = "/second/path";
    contentManager.store(source, first, new ByteArrayInputStream("This is a test".getBytes()), TransferOperation.UPLOAD, new EventMetadata());
    contentManager.store(source, second, new ByteArrayInputStream("This is a test".getBytes()), TransferOperation.UPLOAD, new EventMetadata());
    final HostedRepository target = new HostedRepository(MAVEN_PKG_KEY, "target");
    storeManager.storeArtifactStore(target, new ChangeSummary(ChangeSummary.SYSTEM_USER, "test setup"), false, true, new EventMetadata());
    final PathsPromoteResult result = manager.promotePaths(new PathsPromoteRequest(source.getKey(), target.getKey()).setPurgeSource(true), FAKE_BASE_URL);
    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());
    Transfer ref = downloadManager.getStorageReference(target, first);
    assertThat(ref.exists(), equalTo(true));
    ref = downloadManager.getStorageReference(target, second);
    assertThat(ref.exists(), equalTo(true));
    // source artifacts should be deleted.
    ref = downloadManager.getStorageReference(source, first);
    assertThat(ref.exists(), equalTo(false));
    ref = downloadManager.getStorageReference(source, second);
    assertThat(ref.exists(), equalTo(false));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) PathsPromoteResult(org.commonjava.indy.promote.model.PathsPromoteResult) Transfer(org.commonjava.maven.galley.model.Transfer) PathsPromoteRequest(org.commonjava.indy.promote.model.PathsPromoteRequest) ChangeSummary(org.commonjava.indy.audit.ChangeSummary) HostedRepository(org.commonjava.indy.model.core.HostedRepository) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) Test(org.junit.Test)

Example 49 with ChangeSummary

use of org.commonjava.indy.audit.ChangeSummary 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 50 with ChangeSummary

use of org.commonjava.indy.audit.ChangeSummary in project indy by Commonjava.

the class PromoteValidationsManagerTest method testRuleSetParseAndMatchOnStoreKey.

@Test
public void testRuleSetParseAndMatchOnStoreKey() throws Exception {
    DataFile dataFile = fileManager.getDataFile("promote/rule-sets/test.json");
    dataFile.writeString("{\"name\":\"test\",\"storeKeyPattern\":\".*\"}", new ChangeSummary(ChangeSummary.SYSTEM_USER, "writing test data"));
    promoteValidations = new PromoteValidationsManager(fileManager, config, parser);
    ValidationRuleSet ruleSet = promoteValidations.getRuleSetMatching(new StoreKey(StoreType.hosted, "repo"));
    assertThat(ruleSet, notNullValue());
    assertThat(ruleSet.matchesKey("hosted:repo"), equalTo(true));
}
Also used : DataFile(org.commonjava.indy.subsys.datafile.DataFile) ChangeSummary(org.commonjava.indy.audit.ChangeSummary) ValidationRuleSet(org.commonjava.indy.promote.model.ValidationRuleSet) StoreKey(org.commonjava.indy.model.core.StoreKey) Test(org.junit.Test)

Aggregations

ChangeSummary (org.commonjava.indy.audit.ChangeSummary)68 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)37 Test (org.junit.Test)29 IndyDataException (org.commonjava.indy.data.IndyDataException)24 DataFile (org.commonjava.indy.subsys.datafile.DataFile)21 RemoteRepository (org.commonjava.indy.model.core.RemoteRepository)17 StoreKey (org.commonjava.indy.model.core.StoreKey)14 IOException (java.io.IOException)11 HostedRepository (org.commonjava.indy.model.core.HostedRepository)11 Group (org.commonjava.indy.model.core.Group)10 File (java.io.File)8 ArtifactStore (org.commonjava.indy.model.core.ArtifactStore)8 PathsPromoteRequest (org.commonjava.indy.promote.model.PathsPromoteRequest)8 Transfer (org.commonjava.maven.galley.model.Transfer)8 Logger (org.slf4j.Logger)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 PathsPromoteResult (org.commonjava.indy.promote.model.PathsPromoteResult)7 ArrayList (java.util.ArrayList)6 IndyLifecycleException (org.commonjava.indy.action.IndyLifecycleException)5 BMRules (org.jboss.byteman.contrib.bmunit.BMRules)5