Module: Fluent::Test::Helpers

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

Instance Method Summary collapse

Instance Method Details

#assert_equal_event_time(expected, actual, message = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fluent/test/helpers.rb', line 25

def assert_equal_event_time(expected, actual, message = nil)
  expected_s = "#{Time.at(expected.sec)} (nsec #{expected.nsec})"
  actual_s   = "#{Time.at(actual.sec)  } (nsec #{actual.nsec})"
  message = build_message(message, <<EOT, expected_s, actual_s)
<?> expected but was
<?>.
EOT
  assert_block(message) do
    expected.is_a?(Fluent::EventTime) && actual.is_a?(Fluent::EventTime) && expected.sec == actual.sec && expected.nsec == actual.nsec
  end
end

#capture_log(driver) ⇒ Object

Use this method with v0.12 compatibility layer.

For v0.14 API, use ‘driver.logs` instead.



115
116
117
118
119
120
121
122
# File 'lib/fluent/test/helpers.rb', line 115

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

#capture_stdoutObject



124
125
126
127
128
129
130
131
# File 'lib/fluent/test/helpers.rb', line 124

def capture_stdout
  out = StringIO.new
  $stdout = out
  yield
  out.string.force_encoding('utf-8')
ensure
  $stdout = STDOUT
end

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



37
38
39
# File 'lib/fluent/test/helpers.rb', line 37

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

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



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

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



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/fluent/test/helpers.rb', line 97

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



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/fluent/test/helpers.rb', line 81

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

#with_timezone(tz) ⇒ Object



53
54
55
56
57
58
# File 'lib/fluent/test/helpers.rb', line 53

def with_timezone(tz)
  oldtz, ENV['TZ'] = ENV['TZ'], tz
  yield
ensure
  ENV['TZ'] = oldtz
end

#with_worker_config(root_dir: nil, workers: nil, worker_id: nil, &block) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/fluent/test/helpers.rb', line 60

def with_worker_config(root_dir: nil, workers: nil, worker_id: nil, &block)
  if workers
    if worker_id
      if worker_id >= workers
        raise "worker_id must be between 0 and (workers - 1)"
      end
    else
      worker_id = 0
    end
  end

  opts = {}
  opts['root_dir'] = root_dir if root_dir
  opts['workers'] = workers if workers

  ENV['SERVERENGINE_WORKER_ID'] = worker_id.to_s
  Fluent::SystemConfig.overwrite_system_config(opts, &block)
ensure
  ENV.delete('SERVERENGINE_WORKER_ID')
end