Search in sources :

Example 1 with ArtifactInfo

use of com.facebook.buck.artifact_cache.ArtifactInfo in project buck by facebook.

the class BuildInfoRecorderTest method testPerformUploadToArtifactCache.

@Test
public void testPerformUploadToArtifactCache() throws IOException, InterruptedException {
    FakeProjectFilesystem filesystem = new FakeProjectFilesystem();
    BuildInfoRecorder buildInfoRecorder = createBuildInfoRecorder(filesystem);
    BuckEventBus bus = new BuckEventBus(new FakeClock(0), new BuildId("BUILD"));
    final byte[] contents = "contents".getBytes();
    Path file = Paths.get("file");
    filesystem.writeBytesToPath(contents, file);
    buildInfoRecorder.recordArtifact(file);
    Path dir = Paths.get("dir");
    filesystem.mkdirs(dir);
    filesystem.writeBytesToPath(contents, dir.resolve("file"));
    buildInfoRecorder.recordArtifact(dir);
    // Record some metadata.
    buildInfoRecorder.addMetadata("metadata", "metadata");
    // Record some build metadata.
    buildInfoRecorder.addBuildMetadata("build-metadata", "build-metadata");
    buildInfoRecorder.writeMetadataToDisk(true);
    final AtomicBoolean stored = new AtomicBoolean(false);
    final ArtifactCache cache = new NoopArtifactCache() {

        @Override
        public boolean isStoreSupported() {
            return true;
        }

        @Override
        public ListenableFuture<Void> store(ArtifactInfo info, BorrowablePath output) {
            stored.set(true);
            // Verify the build metadata.
            assertThat(info.getMetadata().get("build-metadata"), Matchers.equalTo("build-metadata"));
            // Verify zip contents
            try (Zip zip = new Zip(output.getPath(), /* forWriting */
            false)) {
                assertEquals(ImmutableSet.of("", "dir/", "buck-out/", "buck-out/bin/", "buck-out/bin/foo/", "buck-out/bin/foo/.bar/", "buck-out/bin/foo/.bar/metadata/"), zip.getDirNames());
                assertEquals(ImmutableSet.of("dir/file", "file", "buck-out/bin/foo/.bar/metadata/metadata"), zip.getFileNames());
                assertArrayEquals(contents, zip.readFully("file"));
                assertArrayEquals(contents, zip.readFully("dir/file"));
            } catch (IOException e) {
                Throwables.throwIfUnchecked(e);
                throw new RuntimeException(e);
            }
            return Futures.immediateFuture(null);
        }
    };
    buildInfoRecorder.performUploadToArtifactCache(ImmutableSet.of(new RuleKey("aa")), cache, bus);
    assertTrue(stored.get());
}
Also used : BuckEventBus(com.facebook.buck.event.BuckEventBus) Path(java.nio.file.Path) BorrowablePath(com.facebook.buck.io.BorrowablePath) Zip(com.facebook.buck.testutil.Zip) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) FakeClock(com.facebook.buck.timing.FakeClock) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArtifactInfo(com.facebook.buck.artifact_cache.ArtifactInfo) BuildId(com.facebook.buck.model.BuildId) NoopArtifactCache(com.facebook.buck.artifact_cache.NoopArtifactCache) BorrowablePath(com.facebook.buck.io.BorrowablePath) ArtifactCache(com.facebook.buck.artifact_cache.ArtifactCache) NoopArtifactCache(com.facebook.buck.artifact_cache.NoopArtifactCache) Test(org.junit.Test)

Example 2 with ArtifactInfo

use of com.facebook.buck.artifact_cache.ArtifactInfo in project buck by facebook.

the class LocalFsContentsProvider method writeFileAndGetInputStream.

public void writeFileAndGetInputStream(BuildJobStateFileHashEntry entry, Path absPath) throws IOException {
    RuleKey key = new RuleKey(entry.getHashCode());
    ArtifactInfo artifactInfo = ArtifactInfo.builder().setRuleKeys(ImmutableList.of(key)).build();
    BorrowablePath nonBorrowablePath = BorrowablePath.notBorrowablePath(absPath);
    try {
        dirCache.store(artifactInfo, nonBorrowablePath).get();
    } catch (InterruptedException | ExecutionException e) {
        throw new IOException("Failed to store artifact to DirCache.", e);
    }
}
Also used : ArtifactInfo(com.facebook.buck.artifact_cache.ArtifactInfo) RuleKey(com.facebook.buck.rules.RuleKey) BorrowablePath(com.facebook.buck.io.BorrowablePath) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 3 with ArtifactInfo

use of com.facebook.buck.artifact_cache.ArtifactInfo in project buck by facebook.

the class ServedCacheIntegrationTest method testMultipleNamedCaches.

@Test
public void testMultipleNamedCaches() throws Exception {
    LazyPath fetchedContents = LazyPath.ofInstance(tmpDir.newFile());
    final RuleKey bFileRuleKey = new RuleKey("baadbeef");
    webServer = new WebServer(/* port */
    0, projectFilesystem, "/static/", MAPPER);
    webServer.updateAndStartIfNeeded(Optional.of(dirCache));
    ArtifactCache secondCache = new ArtifactCache() {

        @Override
        public CacheResult fetch(RuleKey ruleKey, LazyPath output) {
            if (ruleKey.equals(bFileRuleKey)) {
                try {
                    projectFilesystem.writeContentsToPath("second", output.get());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                return CacheResult.hit("secondCache");
            }
            return CacheResult.miss();
        }

        @Override
        public ListenableFuture<Void> store(ArtifactInfo info, BorrowablePath output) {
            return Futures.immediateFuture(null);
        }

        @Override
        public boolean isStoreSupported() {
            return true;
        }

        @Override
        public void close() {
        // Intentional no-op.
        }
    };
    assertThat(secondCache.fetch(A_FILE_RULE_KEY, fetchedContents).getType(), Matchers.equalTo(CacheResultType.MISS));
    assertThat(secondCache.fetch(bFileRuleKey, fetchedContents).getType(), Matchers.equalTo(CacheResultType.HIT));
    WebServer secondWebServer = new WebServer(/* port */
    0, projectFilesystem, "/static/", MAPPER);
    try {
        secondWebServer.updateAndStartIfNeeded(Optional.of(secondCache));
        ArtifactCacheBuckConfig mutltiCacheConfig = createMockLocalConfig("[cache]", "mode = http", "http_cache_names = one, two", "[cache#two]", String.format("http_url = http://127.0.0.1:%d/", secondWebServer.getPort().get()), "[cache#one]", String.format("http_url = http://127.0.0.1:%d/", webServer.getPort().get()));
        ArtifactCache serverBackedCache = createArtifactCache(mutltiCacheConfig);
        assertThat(serverBackedCache.fetch(A_FILE_RULE_KEY, fetchedContents).getType(), Matchers.equalTo(CacheResultType.HIT));
        assertThat(serverBackedCache.fetch(bFileRuleKey, fetchedContents).getType(), Matchers.equalTo(CacheResultType.HIT));
    } finally {
        secondWebServer.stop();
    }
}
Also used : ArtifactInfo(com.facebook.buck.artifact_cache.ArtifactInfo) ArtifactCacheBuckConfig(com.facebook.buck.artifact_cache.ArtifactCacheBuckConfig) RuleKey(com.facebook.buck.rules.RuleKey) BorrowablePath(com.facebook.buck.io.BorrowablePath) LazyPath(com.facebook.buck.io.LazyPath) IOException(java.io.IOException) ArtifactCache(com.facebook.buck.artifact_cache.ArtifactCache) Test(org.junit.Test)

Aggregations

ArtifactInfo (com.facebook.buck.artifact_cache.ArtifactInfo)3 BorrowablePath (com.facebook.buck.io.BorrowablePath)3 IOException (java.io.IOException)3 ArtifactCache (com.facebook.buck.artifact_cache.ArtifactCache)2 RuleKey (com.facebook.buck.rules.RuleKey)2 Test (org.junit.Test)2 ArtifactCacheBuckConfig (com.facebook.buck.artifact_cache.ArtifactCacheBuckConfig)1 NoopArtifactCache (com.facebook.buck.artifact_cache.NoopArtifactCache)1 BuckEventBus (com.facebook.buck.event.BuckEventBus)1 LazyPath (com.facebook.buck.io.LazyPath)1 BuildId (com.facebook.buck.model.BuildId)1 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)1 Zip (com.facebook.buck.testutil.Zip)1 FakeClock (com.facebook.buck.timing.FakeClock)1 Path (java.nio.file.Path)1 ExecutionException (java.util.concurrent.ExecutionException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1