Skip to content

Instantly share code, notes, and snippets.

@tolleiv
Created November 17, 2015 15:54
Show Gist options
  • Save tolleiv/4c45f8fad9501ead77b3 to your computer and use it in GitHub Desktop.
Save tolleiv/4c45f8fad9501ead77b3 to your computer and use it in GitHub Desktop.
Logstash basic/sanity spec to make sure the testing system itself works.

Intend

This file shows a sample very basic test which mainly confirms that the rspec testframework works as expected.

Usage

Get Logstash and the developer plugins e.g. with:

wget https://download.elastic.co/logstash/logstash/logstash-2.0.0.tar.gz
tar -xzf logstash-2.0.0.tar.gz
cd logstash-2.0.0
bin/plugin install --development
export PATH=`pwd`/bin:$PATH

Then run:

rspec a_spec.rb

The output should look like:

Using Accessor#strict_set for specs
Run options: exclude {:redis=>true, :socket=>true, :performance=>true, :couchdb=>true, :elasticsearch=>true, :elasticsearch_secure=>true, :export_cypher=>true, :integration=>true, :windows=>true}
.

Finished in 0.172 seconds (files took 1.35 seconds to load)
1 example, 0 failures

Randomized with seed 54437
# encoding: UTF-8
###############################################
# a_spec.rb
# makes sure to logstash testing itself works
###############################################
require "logstash/devutils/rspec/spec_helper"
describe "Example tests" do
###############################################
# Configuration to test
###############################################
conf = %q(
filter {
grok {
pattern => [ "%{WORD:word1}" ]
break_on_match => false
singles => true
}
mutate {
add_field => { "word2" => "hello" }
}
}
)
# Either the configuration to test is everything in ./conf.d
#Dir.glob('./conf.d/[12]*.conf') do |conf_file|
# conf = conf + IO.read(conf_file)
#end
# or the configuration to test is a dedicated filter in ./conf.d
#conf = IO.read('./conf.d/11_xxx.conf')
# puts "-CONFIG-\n" + conf + "\n--"
config conf
###############################################
# Test data
###############################################
sample("message" => "hello world", "examplefield" => "12345") do
# puts "-RESULT-\n" + subject.to_json + "\n--"
###############################################
# Assertions
###############################################
#insist { subject["type"] } == ""
insist { subject["examplefield"] } == "12345"
insist { subject["word1"] } == "hello"
insist { subject["word2"] } == "hello"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment