File: host.rb

package info (click to toggle)
grok 1.20110708.1-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,388 kB
  • sloc: ansic: 3,469; ruby: 987; makefile: 276; sh: 124; yacc: 106
file content (30 lines) | stat: -rw-r--r-- 863 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
require 'grok'
require 'test/unit'

class HostPattternTest < Test::Unit::TestCase
  def setup
    @grok = Grok.new
    path = "#{File.dirname(__FILE__)}/../../../patterns/base"
    @grok.add_patterns_from_file(path)
    @grok.compile("%{HOSTNAME}")
  end

  def test_hosts
    hosts = ["www.google.com", "foo-234.14.AAc5-2.foobar.net",
            "192-455.a.b.c.d."]
    hosts.each do |host|
      match = @grok.match(host)
      assert_not_equal(false, match, "Expected this to match: #{host}")
      assert_equal(host, match.captures["HOSTNAME"][0])
    end
  end

  def test_hosts_in_string
    @grok.compile("%{HOSTNAME =~ /\\./}")
    host = "www.google.com"
    line = "1 2 3 4 #{host} test"
    match = @grok.match(line)
    assert_not_equal(false, match, "Expected this to match: #{line}")
    assert_equal(host, match.captures["HOSTNAME"][0])
  end
end