Module: Fluent::Test::Helpers

Defined in:
lib/fluent/test/helpers.rb

Instance Method Summary collapse

Instance Method Details

#capture_log(driver) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/fluent/test/helpers.rb', line 69

def capture_log(driver)
  tmp = driver.instance.log.out
  driver.instance.log.out = StringIO.new
  yield
  return driver.instance.log.out.string
ensure
  driver.instance.log.out = tmp
end

#config_element(name = 'test', argument = '', params = {}, elements = []) ⇒ Object



24
25
26
# File 'lib/fluent/test/helpers.rb', line 24

def config_element(name = 'test', argument = '', params = {}, elements = [])
  Fluent::Config::Element.new(name, argument, params, elements)
end

#event_time(str = nil, format: nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/fluent/test/helpers.rb', line 28

def event_time(str=nil, format: nil)
  if str
    if format
      Fluent::EventTime.from_time(Time.strptime(str, format))
    else
      Fluent::EventTime.parse(str)
    end
  else
    Fluent::EventTime.now
  end
end

#msgpack(type) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/fluent/test/helpers.rb', line 56

def msgpack(type)
  case type
  when :factory
    Fluent::MessagePackFactory.factory
  when :packer
    Fluent::MessagePackFactory.packer
  when :unpacker
    Fluent::MessagePackFactory.unpacker
  else
    raise ArgumentError, "unknown msgpack object type '#{type}'"
  end
end

#time2str(time, localtime: false, format: nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/fluent/test/helpers.rb', line 40

def time2str(time, localtime: false, format: nil)
  if format
    if localtime
      Time.at(time).strftime(format)
    else
      Time.at(time).utc.strftime(format)
    end
  else
    if localtime
      Time.at(time).iso8601
    else
      Time.at(time).utc.iso8601
    end
  end
end