Search in sources :

Example 21 with RuleKey

use of com.facebook.buck.rules.RuleKey in project buck by facebook.

the class HttpArtifactCacheBinaryProtocolTest method testStoreRequest.

@Test
public void testStoreRequest() throws IOException {
    final RuleKey ruleKey = new RuleKey("00000000010000000000008000000000");
    final RuleKey ruleKey2 = new RuleKey("90000000000000000000008000000005");
    final String data = "data";
    ImmutableMap<String, String> metadata = ImmutableMap.of("metaKey", "metaValue");
    HttpArtifactCacheBinaryProtocol.StoreRequest storeRequest = new HttpArtifactCacheBinaryProtocol.StoreRequest(ArtifactInfo.builder().addRuleKeys(ruleKey, ruleKey2).setMetadata(metadata).build(), new ByteSource() {

        @Override
        public InputStream openStream() throws IOException {
            return new ByteArrayInputStream(data.getBytes(Charsets.UTF_8));
        }
    });
    ByteArrayOutputStream storeRequestOutputStream = new ByteArrayOutputStream();
    storeRequest.write(storeRequestOutputStream);
    ByteArrayOutputStream storeRequestPayloadStream = new ByteArrayOutputStream();
    StoreResponseReadResult readStoreRequest = HttpArtifactCacheBinaryProtocol.readStoreRequest(new DataInputStream(new ByteArrayInputStream(storeRequestOutputStream.toByteArray())), storeRequestPayloadStream);
    assertThat(readStoreRequest.getRuleKeys(), Matchers.containsInAnyOrder(ruleKey, ruleKey2));
    assertThat(readStoreRequest.getMetadata(), Matchers.equalTo(metadata));
    assertThat(storeRequestPayloadStream.toByteArray(), Matchers.equalTo(data.getBytes(Charsets.UTF_8)));
}
Also used : RuleKey(com.facebook.buck.rules.RuleKey) DataInputStream(java.io.DataInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteSource(com.google.common.io.ByteSource) Test(org.junit.Test)

Example 22 with RuleKey

use of com.facebook.buck.rules.RuleKey in project buck by facebook.

the class HttpArtifactCacheBinaryProtocolTest method testReadFetchResponse.

@Test
public void testReadFetchResponse() throws IOException {
    final String base64EncodedData = "AAAALgAAAAEAIDAwMDAwMDAwMDEwMDAwMDAwMDAwMDA4MDAwMDAwMDAwAAAAANcwdr5kYXRh";
    final RuleKey ruleKey = new RuleKey("00000000010000000000008000000000");
    final String data = "data";
    byte[] expectedData;
    try (ByteArrayOutputStream out = new ByteArrayOutputStream();
        DataOutputStream dataOut = new DataOutputStream(out)) {
        byte[] metadata = HttpArtifactCacheBinaryProtocol.createMetadataHeader(ImmutableSet.of(ruleKey), ImmutableMap.of(), ByteSource.wrap(data.getBytes(Charsets.UTF_8)));
        dataOut.writeInt(metadata.length);
        dataOut.write(metadata);
        dataOut.write(data.getBytes(Charsets.UTF_8));
        expectedData = out.toByteArray();
    }
    assertThat(expectedData, Matchers.equalTo(BaseEncoding.base64().decode(base64EncodedData)));
    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(expectedData))) {
        FetchResponseReadResult result = HttpArtifactCacheBinaryProtocol.readFetchResponse(inputStream, outputStream);
        assertThat(result.getRuleKeys(), Matchers.contains(ruleKey));
        assertThat(outputStream.toByteArray(), Matchers.equalTo(data.getBytes(Charsets.UTF_8)));
        assertThat(result.getActualHashCode(), Matchers.equalTo(HashCode.fromString("d73076be")));
        assertThat(result.getExpectedHashCode(), Matchers.equalTo(HashCode.fromString("d73076be")));
        assertThat(result.getMetadata(), Matchers.anEmptyMap());
        assertThat(result.getResponseSizeBytes(), Matchers.equalTo(4L));
    }
}
Also used : RuleKey(com.facebook.buck.rules.RuleKey) ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) Test(org.junit.Test)

Example 23 with RuleKey

use of com.facebook.buck.rules.RuleKey in project buck by facebook.

the class HttpArtifactCacheTest method testFetchWrongKey.

@Test
public void testFetchWrongKey() throws Exception {
    FakeProjectFilesystem filesystem = new FakeProjectFilesystem();
    final RuleKey ruleKey = new RuleKey("00000000000000000000000000000000");
    final RuleKey otherRuleKey = new RuleKey("11111111111111111111111111111111");
    final String data = "data";
    argsBuilder.setFetchClient(withMakeRequest(((path, requestBuilder) -> {
        Request request = requestBuilder.url(SERVER + path).build();
        Response response = new Response.Builder().request(request).protocol(Protocol.HTTP_1_1).code(HttpURLConnection.HTTP_OK).body(createResponseBody(ImmutableSet.of(otherRuleKey), ImmutableMap.of(), ByteSource.wrap(data.getBytes(Charsets.UTF_8)), data)).build();
        return new OkHttpResponseWrapper(response);
    })));
    HttpArtifactCache cache = new HttpArtifactCache(argsBuilder.build());
    Path output = Paths.get("output/file");
    CacheResult result = cache.fetch(ruleKey, LazyPath.ofInstance(output));
    assertEquals(CacheResultType.ERROR, result.getType());
    assertEquals(Optional.empty(), filesystem.readFileIfItExists(output));
    cache.close();
}
Also used : Response(okhttp3.Response) HttpResponse(com.facebook.buck.slb.HttpResponse) Path(java.nio.file.Path) LazyPath(com.facebook.buck.io.LazyPath) RuleKey(com.facebook.buck.rules.RuleKey) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) Request(okhttp3.Request) OkHttpResponseWrapper(com.facebook.buck.slb.OkHttpResponseWrapper) Test(org.junit.Test)

Example 24 with RuleKey

use of com.facebook.buck.rules.RuleKey in project buck by facebook.

the class ArchiveTest method testThatInputChangesCauseRuleKeyChanges.

@Test
public void testThatInputChangesCauseRuleKeyChanges() {
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer()));
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    BuildTarget target = BuildTargetFactory.newInstance("//foo:bar");
    BuildRuleParams params = new FakeBuildRuleParamsBuilder(target).build();
    FakeFileHashCache hashCache = FakeFileHashCache.createFromStrings(ImmutableMap.<String, String>builder().put(AR.toString(), Strings.repeat("0", 40)).put(RANLIB.toString(), Strings.repeat("1", 40)).put("a.o", Strings.repeat("a", 40)).put("b.o", Strings.repeat("b", 40)).put("c.o", Strings.repeat("c", 40)).put(Paths.get("different").toString(), Strings.repeat("d", 40)).build());
    // Generate a rule key for the defaults.
    RuleKey defaultRuleKey = new DefaultRuleKeyFactory(0, hashCache, pathResolver, ruleFinder).build(Archive.from(target, params, ruleFinder, DEFAULT_ARCHIVER, ImmutableList.of(), DEFAULT_RANLIB, ImmutableList.of(), Archive.Contents.NORMAL, DEFAULT_OUTPUT, DEFAULT_INPUTS));
    // Verify that changing the archiver causes a rulekey change.
    RuleKey archiverChange = new DefaultRuleKeyFactory(0, hashCache, pathResolver, ruleFinder).build(Archive.from(target, params, ruleFinder, new GnuArchiver(new HashedFileTool(Paths.get("different"))), ImmutableList.of(), DEFAULT_RANLIB, ImmutableList.of(), Archive.Contents.NORMAL, DEFAULT_OUTPUT, DEFAULT_INPUTS));
    assertNotEquals(defaultRuleKey, archiverChange);
    // Verify that changing the output path causes a rulekey change.
    RuleKey outputChange = new DefaultRuleKeyFactory(0, hashCache, pathResolver, ruleFinder).build(Archive.from(target, params, ruleFinder, DEFAULT_ARCHIVER, ImmutableList.of(), DEFAULT_RANLIB, ImmutableList.of(), Archive.Contents.NORMAL, Paths.get("different"), DEFAULT_INPUTS));
    assertNotEquals(defaultRuleKey, outputChange);
    // Verify that changing the inputs causes a rulekey change.
    RuleKey inputChange = new DefaultRuleKeyFactory(0, hashCache, pathResolver, ruleFinder).build(Archive.from(target, params, ruleFinder, DEFAULT_ARCHIVER, ImmutableList.of(), DEFAULT_RANLIB, ImmutableList.of(), Archive.Contents.NORMAL, DEFAULT_OUTPUT, ImmutableList.of(new FakeSourcePath("different"))));
    assertNotEquals(defaultRuleKey, inputChange);
    // Verify that changing the type of archiver causes a rulekey change.
    RuleKey archiverTypeChange = new DefaultRuleKeyFactory(0, hashCache, pathResolver, ruleFinder).build(Archive.from(target, params, ruleFinder, new BsdArchiver(new HashedFileTool(AR)), ImmutableList.of(), DEFAULT_RANLIB, ImmutableList.of(), Archive.Contents.NORMAL, DEFAULT_OUTPUT, DEFAULT_INPUTS));
    assertNotEquals(defaultRuleKey, archiverTypeChange);
}
Also used : FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) DefaultRuleKeyFactory(com.facebook.buck.rules.keys.DefaultRuleKeyFactory) FakeFileHashCache(com.facebook.buck.testutil.FakeFileHashCache) RuleKey(com.facebook.buck.rules.RuleKey) FakeBuildRuleParamsBuilder(com.facebook.buck.rules.FakeBuildRuleParamsBuilder) Matchers.containsString(org.hamcrest.Matchers.containsString) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) HashedFileTool(com.facebook.buck.rules.HashedFileTool) BuildTarget(com.facebook.buck.model.BuildTarget) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) Test(org.junit.Test)

Example 25 with RuleKey

use of com.facebook.buck.rules.RuleKey in project buck by facebook.

the class CxxPreprocessAndCompileTest method inputChangesCauseRuleKeyChangesForCompilation.

@Test
public void inputChangesCauseRuleKeyChangesForCompilation() throws Exception {
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer()));
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    BuildTarget target = BuildTargetFactory.newInstance("//foo:bar");
    BuildRuleParams params = new FakeBuildRuleParamsBuilder(target).build();
    FakeFileHashCache hashCache = FakeFileHashCache.createFromStrings(ImmutableMap.<String, String>builder().put("preprocessor", Strings.repeat("a", 40)).put("compiler", Strings.repeat("a", 40)).put("test.o", Strings.repeat("b", 40)).put("test.cpp", Strings.repeat("c", 40)).put("different", Strings.repeat("d", 40)).put("foo/test.h", Strings.repeat("e", 40)).put("path/to/a/plugin.so", Strings.repeat("f", 40)).put("path/to/a/different/plugin.so", Strings.repeat("a0", 40)).build());
    // Generate a rule key for the defaults.
    RuleKey defaultRuleKey = new DefaultRuleKeyFactory(0, hashCache, pathResolver, ruleFinder).build(CxxPreprocessAndCompile.compile(params, new CompilerDelegate(pathResolver, CxxPlatformUtils.DEFAULT_COMPILER_DEBUG_PATH_SANITIZER, DEFAULT_COMPILER, DEFAULT_TOOL_FLAGS), DEFAULT_OUTPUT, DEFAULT_INPUT, DEFAULT_INPUT_TYPE, CxxPlatformUtils.DEFAULT_COMPILER_DEBUG_PATH_SANITIZER, CxxPlatformUtils.DEFAULT_ASSEMBLER_DEBUG_PATH_SANITIZER, Optional.empty()));
    // Verify that changing the compiler causes a rulekey change.
    RuleKey compilerChange = new DefaultRuleKeyFactory(0, hashCache, pathResolver, ruleFinder).build(CxxPreprocessAndCompile.compile(params, new CompilerDelegate(pathResolver, CxxPlatformUtils.DEFAULT_COMPILER_DEBUG_PATH_SANITIZER, new GccCompiler(new HashedFileTool(Paths.get("different"))), DEFAULT_TOOL_FLAGS), DEFAULT_OUTPUT, DEFAULT_INPUT, DEFAULT_INPUT_TYPE, CxxPlatformUtils.DEFAULT_COMPILER_DEBUG_PATH_SANITIZER, CxxPlatformUtils.DEFAULT_ASSEMBLER_DEBUG_PATH_SANITIZER, Optional.empty()));
    assertNotEquals(defaultRuleKey, compilerChange);
    // Verify that changing the operation causes a rulekey change.
    RuleKey operationChange = new DefaultRuleKeyFactory(0, hashCache, pathResolver, ruleFinder).build(CxxPreprocessAndCompile.preprocessAndCompile(params, new PreprocessorDelegate(pathResolver, CxxPlatformUtils.DEFAULT_COMPILER_DEBUG_PATH_SANITIZER, CxxPlatformUtils.DEFAULT_PLATFORM.getHeaderVerification(), DEFAULT_WORKING_DIR, DEFAULT_PREPROCESSOR, PreprocessorFlags.builder().build(), DEFAULT_FRAMEWORK_PATH_SEARCH_PATH_FUNCTION, Optional.empty(), /* leadingIncludePaths */
    Optional.empty()), new CompilerDelegate(pathResolver, CxxPlatformUtils.DEFAULT_COMPILER_DEBUG_PATH_SANITIZER, DEFAULT_COMPILER, DEFAULT_TOOL_FLAGS), DEFAULT_OUTPUT, DEFAULT_INPUT, DEFAULT_INPUT_TYPE, Optional.empty(), CxxPlatformUtils.DEFAULT_COMPILER_DEBUG_PATH_SANITIZER, CxxPlatformUtils.DEFAULT_ASSEMBLER_DEBUG_PATH_SANITIZER, Optional.empty()));
    assertNotEquals(defaultRuleKey, operationChange);
    // Verify that changing the platform flags causes a rulekey change.
    RuleKey platformFlagsChange = new DefaultRuleKeyFactory(0, hashCache, pathResolver, ruleFinder).build(CxxPreprocessAndCompile.compile(params, new CompilerDelegate(pathResolver, CxxPlatformUtils.DEFAULT_COMPILER_DEBUG_PATH_SANITIZER, DEFAULT_COMPILER, CxxToolFlags.explicitBuilder().addPlatformFlags("-different").setRuleFlags(DEFAULT_TOOL_FLAGS.getRuleFlags()).build()), DEFAULT_OUTPUT, DEFAULT_INPUT, DEFAULT_INPUT_TYPE, CxxPlatformUtils.DEFAULT_COMPILER_DEBUG_PATH_SANITIZER, CxxPlatformUtils.DEFAULT_ASSEMBLER_DEBUG_PATH_SANITIZER, Optional.empty()));
    assertNotEquals(defaultRuleKey, platformFlagsChange);
    // Verify that changing the rule flags causes a rulekey change.
    RuleKey ruleFlagsChange = new DefaultRuleKeyFactory(0, hashCache, pathResolver, ruleFinder).build(CxxPreprocessAndCompile.compile(params, new CompilerDelegate(pathResolver, CxxPlatformUtils.DEFAULT_COMPILER_DEBUG_PATH_SANITIZER, DEFAULT_COMPILER, CxxToolFlags.explicitBuilder().setPlatformFlags(DEFAULT_TOOL_FLAGS.getPlatformFlags()).addRuleFlags("-other", "flags").build()), DEFAULT_OUTPUT, DEFAULT_INPUT, DEFAULT_INPUT_TYPE, CxxPlatformUtils.DEFAULT_COMPILER_DEBUG_PATH_SANITIZER, CxxPlatformUtils.DEFAULT_ASSEMBLER_DEBUG_PATH_SANITIZER, Optional.empty()));
    assertNotEquals(defaultRuleKey, ruleFlagsChange);
    // Verify that changing the input causes a rulekey change.
    RuleKey inputChange = new DefaultRuleKeyFactory(0, hashCache, pathResolver, ruleFinder).build(CxxPreprocessAndCompile.compile(params, new CompilerDelegate(pathResolver, CxxPlatformUtils.DEFAULT_COMPILER_DEBUG_PATH_SANITIZER, DEFAULT_COMPILER, DEFAULT_TOOL_FLAGS), DEFAULT_OUTPUT, new FakeSourcePath("different"), DEFAULT_INPUT_TYPE, CxxPlatformUtils.DEFAULT_COMPILER_DEBUG_PATH_SANITIZER, CxxPlatformUtils.DEFAULT_ASSEMBLER_DEBUG_PATH_SANITIZER, Optional.empty()));
    assertNotEquals(defaultRuleKey, inputChange);
}
Also used : FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) DefaultRuleKeyFactory(com.facebook.buck.rules.keys.DefaultRuleKeyFactory) FakeFileHashCache(com.facebook.buck.testutil.FakeFileHashCache) RuleKey(com.facebook.buck.rules.RuleKey) FakeBuildRuleParamsBuilder(com.facebook.buck.rules.FakeBuildRuleParamsBuilder) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) HashedFileTool(com.facebook.buck.rules.HashedFileTool) BuildTarget(com.facebook.buck.model.BuildTarget) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) Test(org.junit.Test)

Aggregations

RuleKey (com.facebook.buck.rules.RuleKey)149 Test (org.junit.Test)112 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)71 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)71 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)71 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)70 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)57 Path (java.nio.file.Path)57 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)42 BuildTarget (com.facebook.buck.model.BuildTarget)42 BuildRule (com.facebook.buck.rules.BuildRule)42 LazyPath (com.facebook.buck.io.LazyPath)34 DefaultRuleKeyFactory (com.facebook.buck.rules.keys.DefaultRuleKeyFactory)34 PathSourcePath (com.facebook.buck.rules.PathSourcePath)33 AddToRuleKey (com.facebook.buck.rules.AddToRuleKey)30 SourcePath (com.facebook.buck.rules.SourcePath)28 FakeFileHashCache (com.facebook.buck.testutil.FakeFileHashCache)27 FakeBuildRule (com.facebook.buck.rules.FakeBuildRule)25 IOException (java.io.IOException)25 StackedFileHashCache (com.facebook.buck.util.cache.StackedFileHashCache)23