# Gem to install logstash/event for test/unit

**URL:** <https://discuss.elastic.co/t/gem-to-install-logstash-event-for-test-unit/128388>\
**Category:** Logstash\
**Created:** [April 17, 2018, 2:52pm UTC](https://discuss.elastic.co/t/gem-to-install-logstash-event-for-test-unit/128388 "2018-04-17T14:52:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![camminati](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/camminati/32/19440_2.png) [@camminati](https://discuss.elastic.co/u/camminati)\
**Post date:** [April 17, 2018, 2:52pm UTC](https://discuss.elastic.co/t/gem-to-install-logstash-event-for-test-unit/128388/1 "2018-04-17T14:52:55Z")

</div>

Hello,  
in order to debug/test ruby filters I wrote some tests with `test/unit`. I have my test-data in json files and would like it to load it into an event (something like `LogStash::Event.new(data = json)`).

This is the test file, and the filter is in `map_wildfly_data.rb`

```auto
load "map_wildfly_data.rb"
require "json"
require "test/unit"
require "logstash/event"

class TestFilter < Test::Unit::TestCase
  def test_xa
    file = File.read("wf_data_xa.json")
    json = JSON.parse(file)
    event = LogStash::Event.new(data = json)
    filter(event)
    assert_equal(event.get("wf_data.datasource.type"), "xa")
  end
end

```

I tested it so far with `gem install logstash-event`, but it found an old version `logstash-event-1.2.02.gem`, from before the Event-API changed, so is no good, I get:

```auto
Error: test_xa(TestFilter):
  NoMethodError: undefined method `set' for #<LogStash::Event:0x00000000a253f0>

```

Is there a gem that includes the new Event-API?

I am learning ruby as I go, so I have no real idea (other than installing gems) of how to make libraries accessible from the test class (quasi jars in CP-like), but I guess with some pointers I can do that too.

---

<div class="post-metadata">

**Author:** ![camminati](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/camminati/32/19440_2.png) [@camminati](https://discuss.elastic.co/u/camminati)\
**Post date:** [April 18, 2018, 9:44am UTC](https://discuss.elastic.co/t/gem-to-install-logstash-event-for-test-unit/128388/2 "2018-04-18T09:44:26Z")

</div>

So I recently learnt that I need jruby (yea.. newbee). So I will try to build and install it from github (logstash 6.2).

---

<div class="post-metadata">

**Author:** ![camminati](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/camminati/32/19440_2.png) [@camminati](https://discuss.elastic.co/u/camminati)\
**Post date:** [April 18, 2018, 3:13pm UTC](https://discuss.elastic.co/t/gem-to-install-logstash-event-for-test-unit/128388/3 "2018-04-18T15:13:52Z")

</div>

Well, that didn't help much. After some hours of rubying and rvming I got the library installed, but it does not provide `logstash/event`

```auto
➜ ✗ gem list |grep logstash-core
logstash-core (6.2.5 java)
➜ ✗ ruby map_wildfly_data.test.rb
/usr/share/rvm/rubies/jruby-9.1.13.0/lib/ruby/stdlib/power_assert.rb:7: warning: tracing (e.g. set_trace_func) will not capture all events without --debug flag
LoadError: no such file to load -- logstash/event
  require at org/jruby/RubyKernel.java:955
  require at /usr/share/rvm/rubies/jruby-9.1.13.0/lib/ruby/stdlib/rubygems/core_ext/kernel_require.rb:59
   <main> at map_wildfly_data.test.rb:4

```

Is it somewhere else? Or are my gems not working like they should?

---

<div class="post-metadata">

**Author:** ![camminati](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/camminati/32/19440_2.png) [@camminati](https://discuss.elastic.co/u/camminati)\
**Post date:** [April 19, 2018, 10:18am UTC](https://discuss.elastic.co/t/gem-to-install-logstash-event-for-test-unit/128388/4 "2018-04-19T10:18:56Z")

</div>

So, finally solved it. It was a long journey. I wish the documentation would be more specific on how to use the libraries for development (or provide a working `dev-utils` gem for that matter). For many ruby-newbies (i found in forums) it is not really clear how this stuff works at all. I understand teaching ruby from scratch is not your mission, but since you provide this all-to-powerful filter, making it easier to use should be.

The final combo was:

```auto
$LOAD_PATH.unshift File.expand_path("/dev/logstash/logstash/logstash-core/lib", __FILE__ )
require "forwardable"
require "json"
require "test/unit"
require "logstash/event"
require_relative "./map_wildfly_data.rb"

class TestFilter < Test::Unit::TestCase
  def test_xa
    file = File.read("wf_data_xa.json")
    json = JSON.parse(file)
    event = LogStash::Event.new(data = json)
    filter(event)
    assert_equal(event.get("wf_data.datasource.type"), "nonxa")
  end
end

```

Somehow the `require "forwardable"` is what makes it work.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 17, 2018, 10:19am UTC](https://discuss.elastic.co/t/gem-to-install-logstash-event-for-test-unit/128388/5 "2018-05-17T10:19:11Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
