Search in sources :

Example 16 with ArtifactCache

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

the class CacheCommandTest method testRunCommandAndFetchArtifactsUnsuccessfully.

@Test
public void testRunCommandAndFetchArtifactsUnsuccessfully() throws IOException, InterruptedException {
    final String ruleKeyHash = "b64009ae3762a42a1651c139ec452f0d18f48e21";
    ArtifactCache cache = createMock(ArtifactCache.class);
    expect(cache.fetch(eq(new RuleKey(ruleKeyHash)), isA(LazyPath.class))).andReturn(CacheResult.miss());
    TestConsole console = new TestConsole();
    console.printErrorText("Failed to retrieve an artifact with id " + ruleKeyHash + ".");
    CommandRunnerParams commandRunnerParams = CommandRunnerParamsForTesting.builder().setConsole(console).setArtifactCache(cache).build();
    replayAll();
    CacheCommand cacheCommand = new CacheCommand();
    cacheCommand.setArguments(ImmutableList.of(ruleKeyHash));
    int exitCode = cacheCommand.run(commandRunnerParams);
    assertEquals(1, exitCode);
}
Also used : RuleKey(com.facebook.buck.rules.RuleKey) LazyPath(com.facebook.buck.io.LazyPath) TestConsole(com.facebook.buck.testutil.TestConsole) ArtifactCache(com.facebook.buck.artifact_cache.ArtifactCache) Test(org.junit.Test)

Example 17 with ArtifactCache

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

the class BuildInfoRecorderIntegrationTest method testPerformUploadToArtifactCache.

@Test
public void testPerformUploadToArtifactCache() throws IOException, InterruptedException {
    BuildInfoRecorder buildInfoRecorder = createBuildInfoRecorder(new FakeProjectFilesystem() {

        @Override
        public void createZip(Collection<Path> pathsToIncludeInZip, Path out) throws IOException {
            // For this test, nothing really cares about the content, so just write out the name.
            writeBytesToPath(out.toString().getBytes(), out);
        }
    });
    Path cacheDir = Files.createTempDirectory("root");
    ArtifactCache artifactCache = TestArtifactCaches.createDirCacheForTest(cacheDir, Paths.get("cache"));
    buildInfoRecorder.performUploadToArtifactCache(ImmutableSet.of(new RuleKey(RULE_KEY)), artifactCache, new BuckEventBus(new DefaultClock(), new BuildId()));
    assertTrue(cacheDir.resolve(DirArtifactCacheTestUtil.getPathForRuleKey(artifactCache, new RuleKey(RULE_KEY), Optional.empty())).toFile().exists());
}
Also used : Path(java.nio.file.Path) BuckEventBus(com.facebook.buck.event.BuckEventBus) BuildId(com.facebook.buck.model.BuildId) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) DefaultClock(com.facebook.buck.timing.DefaultClock) IOException(java.io.IOException) ArtifactCache(com.facebook.buck.artifact_cache.ArtifactCache) Test(org.junit.Test)

Example 18 with ArtifactCache

use of com.facebook.buck.artifact_cache.ArtifactCache 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)

Example 19 with ArtifactCache

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

the class ServedCacheIntegrationTest method testStoreDisabled.

@Test
public void testStoreDisabled() throws Exception {
    webServer = new WebServer(/* port */
    0, projectFilesystem, "/static/", MAPPER);
    webServer.updateAndStartIfNeeded(ArtifactCaches.newServedCache(createMockLocalConfig("[cache]", "dir = test-cache", "serve_local_cache = true", "served_local_cache_mode = readonly"), projectFilesystem));
    ArtifactCache serverBackedCache = createArtifactCache(createMockLocalHttpCacheConfig(webServer.getPort().get()));
    RuleKey ruleKey = new RuleKey("00111222333444");
    ImmutableMap<String, String> metadata = ImmutableMap.of("some key", "some value");
    Path originalDataPath = tmpDir.newFile();
    String data = "you won't believe this!";
    projectFilesystem.writeContentsToPath(data, originalDataPath);
    LazyPath fetchedContents = LazyPath.ofInstance(tmpDir.newFile());
    CacheResult cacheResult = serverBackedCache.fetch(ruleKey, fetchedContents);
    assertThat(cacheResult.getType().isSuccess(), Matchers.is(false));
    serverBackedCache.store(ArtifactInfo.builder().addRuleKeys(ruleKey).setMetadata(metadata).build(), BorrowablePath.notBorrowablePath(originalDataPath));
    cacheResult = serverBackedCache.fetch(ruleKey, fetchedContents);
    assertThat(cacheResult.getType().isSuccess(), Matchers.is(false));
}
Also used : Path(java.nio.file.Path) BorrowablePath(com.facebook.buck.io.BorrowablePath) LazyPath(com.facebook.buck.io.LazyPath) RuleKey(com.facebook.buck.rules.RuleKey) CacheResult(com.facebook.buck.artifact_cache.CacheResult) LazyPath(com.facebook.buck.io.LazyPath) ArtifactCache(com.facebook.buck.artifact_cache.ArtifactCache) Test(org.junit.Test)

Example 20 with ArtifactCache

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

the class ServedCacheIntegrationTest method testMalformedDirCacheMetaData.

@Test
public void testMalformedDirCacheMetaData() throws Exception {
    ArtifactCache cache = TestArtifactCaches.createDirCacheForTest(projectFilesystem.getRootPath(), Paths.get("test-cache"));
    Path cacheFilePath = DirArtifactCacheTestUtil.getPathForRuleKey(cache, A_FILE_RULE_KEY, Optional.of(".metadata"));
    assertThat(projectFilesystem.exists(cacheFilePath), Matchers.is(true));
    try (DataOutputStream outputStream = new DataOutputStream(projectFilesystem.newFileOutputStream(cacheFilePath))) {
        outputStream.writeInt(1024);
    }
    webServer = new WebServer(/* port */
    0, projectFilesystem, "/static/", MAPPER);
    webServer.updateAndStartIfNeeded(Optional.of(dirCache));
    ArtifactCache serverBackedCache = createArtifactCache(createMockLocalHttpCacheConfig(webServer.getPort().get()));
    LazyPath fetchedContents = LazyPath.ofInstance(tmpDir.newFile());
    CacheResult cacheResult = serverBackedCache.fetch(A_FILE_RULE_KEY, fetchedContents);
    assertThat(cacheResult.getType(), Matchers.equalTo(CacheResultType.MISS));
}
Also used : Path(java.nio.file.Path) BorrowablePath(com.facebook.buck.io.BorrowablePath) LazyPath(com.facebook.buck.io.LazyPath) DataOutputStream(java.io.DataOutputStream) CacheResult(com.facebook.buck.artifact_cache.CacheResult) LazyPath(com.facebook.buck.io.LazyPath) ArtifactCache(com.facebook.buck.artifact_cache.ArtifactCache) Test(org.junit.Test)

Aggregations

ArtifactCache (com.facebook.buck.artifact_cache.ArtifactCache)21 Test (org.junit.Test)16 LazyPath (com.facebook.buck.io.LazyPath)13 Path (java.nio.file.Path)13 BorrowablePath (com.facebook.buck.io.BorrowablePath)9 RuleKey (com.facebook.buck.rules.RuleKey)9 CacheResult (com.facebook.buck.artifact_cache.CacheResult)7 BuckEventBus (com.facebook.buck.event.BuckEventBus)6 TestConsole (com.facebook.buck.testutil.TestConsole)5 NoopArtifactCache (com.facebook.buck.artifact_cache.NoopArtifactCache)4 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)4 Cell (com.facebook.buck.rules.Cell)4 IOException (java.io.IOException)4 FakeAndroidDirectoryResolver (com.facebook.buck.android.FakeAndroidDirectoryResolver)3 FakeJavaPackageFinder (com.facebook.buck.jvm.java.FakeJavaPackageFinder)3 BuildId (com.facebook.buck.model.BuildId)3 TestCellBuilder (com.facebook.buck.rules.TestCellBuilder)3 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)3 Before (org.junit.Before)3 AndroidDirectoryResolver (com.facebook.buck.android.AndroidDirectoryResolver)2