Skip to content

Commit

Permalink
Properly hash when no original source is present.
Browse files Browse the repository at this point in the history
This corrects a mistake where the digest of the string source should be
used when present, and the hash of the graph should be used when not.

Fixes elastic#7467
  • Loading branch information
andrewvc committed Jun 15, 2017
1 parent 50cbaf4 commit 6b53790
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
4 changes: 4 additions & 0 deletions logstash-core/spec/logstash/compiler/compiler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ def j

subject(:pipeline) { described_class.compile_sources(*sources_with_metadata) }

it "should generate a hash" do
expect(pipeline.unique_hash).to be_a(String)
end

it "should compile cleanly" do
expect(pipeline).to be_a(org.logstash.config.ir.PipelineIR)
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.logstash.config.ir;

import org.logstash.common.Util;
import org.logstash.config.ir.graph.Graph;
import org.logstash.config.ir.graph.PluginVertex;
import org.logstash.config.ir.graph.QueueVertex;
Expand Down Expand Up @@ -51,6 +52,8 @@ public PipelineIR(Graph inputSection, Graph filterSection, Graph outputSection,
this.graph.validate();

if (this.getOriginalSource() != null && this.getOriginalSource().matches("^\\S+$")) {
uniqueHash = Util.digest(this.getOriginalSource());
} else {
uniqueHash = this.graph.uniqueHash();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package org.logstash.config.ir;

import org.junit.Test;
import org.logstash.common.Util;
import org.logstash.config.ir.graph.Graph;

import java.nio.channels.Pipe;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.logstash.config.ir.DSL.*;
import static org.logstash.config.ir.PluginDefinition.Type.*;
Expand All @@ -12,21 +16,42 @@
* Created by andrewvc on 9/20/16.
*/
public class PipelineIRTest {
@Test
public void testPipelineCreation() throws InvalidIRException {
Graph inputSection = iComposeParallel(iPlugin(INPUT, "generator"), iPlugin(INPUT, "stdin")).toGraph();
Graph filterSection = iIf(eEq(eEventValue("[foo]"), eEventValue("[bar]")),
public Graph makeInputSection() throws InvalidIRException {
return iComposeParallel(iPlugin(INPUT, "generator"), iPlugin(INPUT, "stdin")).toGraph();
}

public Graph makeFilterSection() throws InvalidIRException {
return iIf(eEq(eEventValue("[foo]"), eEventValue("[bar]")),
iPlugin(FILTER, "grok"),
iPlugin(FILTER, "kv")).toGraph();
Graph outputSection = iIf(eGt(eEventValue("[baz]"), eValue(1000)),
}

public Graph makeOutputSection() throws InvalidIRException {
return iIf(eGt(eEventValue("[baz]"), eValue(1000)),
iComposeParallel(
iPlugin(OUTPUT, "s3"),
iPlugin(OUTPUT, "elasticsearch")),
iPlugin(OUTPUT, "stdout")).toGraph();
}

PipelineIR pipelineIR = new PipelineIR(inputSection, filterSection, outputSection);
@Test
public void testPipelineCreation() throws InvalidIRException {
PipelineIR pipelineIR = new PipelineIR(makeInputSection(), makeFilterSection(), makeOutputSection());
assertEquals(2, pipelineIR.getInputPluginVertices().size());
assertEquals(2, pipelineIR.getFilterPluginVertices().size());
assertEquals(3, pipelineIR.getOutputPluginVertices().size());
}

@Test
public void hashingWithoutOriginalSource() throws InvalidIRException {
PipelineIR pipelineIR = new PipelineIR(makeInputSection(), makeFilterSection(), makeOutputSection());
assertEquals(pipelineIR.uniqueHash(), pipelineIR.getGraph().uniqueHash());
}

@Test
public void hashingWithOriginalSource() throws InvalidIRException {
String source = "mysource";
PipelineIR pipelineIR = new PipelineIR(makeInputSection(), makeFilterSection(), makeOutputSection(), source);
assertEquals(pipelineIR.uniqueHash(), Util.digest(source));
}
}

0 comments on commit 6b53790

Please sign in to comment.