PageRenderTime 24ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/test/helper.rb

https://gitlab.com/wilane/fluentd
Ruby | 141 lines | 123 code | 13 blank | 5 comment | 6 complexity | c46fe8f9ef1d6891068647dba6b42e56 MD5 | raw file
  1. # simplecov must be loaded before any of target code
  2. if ENV['SIMPLE_COV']
  3. require 'simplecov'
  4. if defined?(SimpleCov::SourceFile)
  5. mod = SimpleCov::SourceFile
  6. def mod.new(*args, &block)
  7. m = allocate
  8. m.instance_eval do
  9. begin
  10. initialize(*args, &block)
  11. rescue Encoding::UndefinedConversionError
  12. @src = "".force_encoding('UTF-8')
  13. end
  14. end
  15. m
  16. end
  17. end
  18. unless SimpleCov.running
  19. SimpleCov.start do
  20. add_filter '/test/'
  21. add_filter '/gems/'
  22. end
  23. end
  24. end
  25. # Some tests use Hash instead of Element for configure.
  26. # We should rewrite these tests in the future and remove this ad-hoc code
  27. class Hash
  28. def corresponding_proxies
  29. @corresponding_proxies ||= []
  30. end
  31. def to_masked_element
  32. self
  33. end
  34. end
  35. require 'rr'
  36. require 'test/unit'
  37. require 'test/unit/rr'
  38. require 'fileutils'
  39. require 'fluent/config/element'
  40. require 'fluent/log'
  41. require 'fluent/test'
  42. require 'fluent/plugin/base'
  43. require 'fluent/log'
  44. require 'fluent/plugin_id'
  45. require 'fluent/plugin_helper'
  46. require 'fluent/msgpack_factory'
  47. require 'fluent/time'
  48. require 'serverengine'
  49. module Fluent
  50. module Plugin
  51. class TestBase < Base
  52. # a base plugin class, but not input nor output
  53. # mainly for helpers and owned plugins
  54. include PluginId
  55. include PluginLoggerMixin
  56. include PluginHelper::Mixin
  57. end
  58. end
  59. end
  60. unless defined?(Test::Unit::AssertionFailedError)
  61. class Test::Unit::AssertionFailedError < StandardError
  62. end
  63. end
  64. def config_element(name = 'test', argument = '', params = {}, elements = [])
  65. Fluent::Config::Element.new(name, argument, params, elements)
  66. end
  67. def event_time(str=nil)
  68. if str
  69. Fluent::EventTime.parse(str)
  70. else
  71. Fluent::EventTime.now
  72. end
  73. end
  74. def msgpack(type)
  75. case type
  76. when :factory
  77. Fluent::MessagePackFactory.factory
  78. when :packer
  79. Fluent::MessagePackFactory.packer
  80. when :unpacker
  81. Fluent::MessagePackFactory.unpacker
  82. else
  83. raise ArgumentError, "unknown msgpack object type '#{type}'"
  84. end
  85. end
  86. def unused_port(num = 1)
  87. ports = []
  88. sockets = []
  89. num.times do
  90. s = TCPServer.open(0)
  91. sockets << s
  92. ports << s.addr[1]
  93. end
  94. sockets.each{|s| s.close }
  95. if num == 1
  96. return ports.first
  97. else
  98. return *ports
  99. end
  100. end
  101. def waiting(seconds, logs: nil, plugin: nil)
  102. begin
  103. Timeout.timeout(seconds) do
  104. yield
  105. end
  106. rescue Timeout::Error
  107. if logs
  108. STDERR.print(*logs)
  109. elsif plugin
  110. STDERR.print(*plugin.log.out.logs)
  111. end
  112. raise
  113. end
  114. end
  115. def ipv6_enabled?
  116. require 'socket'
  117. begin
  118. TCPServer.open("::1", 0)
  119. true
  120. rescue
  121. false
  122. end
  123. end
  124. dl_opts = {}
  125. dl_opts[:log_level] = ServerEngine::DaemonLogger::WARN
  126. logdev = Fluent::Test::DummyLogDevice.new
  127. logger = ServerEngine::DaemonLogger.new(logdev, dl_opts)
  128. $log ||= Fluent::Log.new(logger)