PageRenderTime 67ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/test/config/test_configure_proxy.rb

https://gitlab.com/vectorci/fluentd
Ruby | 386 lines | 343 code | 42 blank | 1 comment | 4 complexity | dac6e53226eb854eb90c1eed78991450 MD5 | raw file
  1. require_relative '../helper'
  2. require 'fluent/config/configure_proxy'
  3. module Fluent::Config
  4. class TestConfigureProxy < ::Test::Unit::TestCase
  5. setup do
  6. @type_lookup = ->(type) { Fluent::Configurable.lookup_type(type) }
  7. end
  8. sub_test_case 'to generate a instance' do
  9. sub_test_case '#initialize' do
  10. test 'has default values' do
  11. proxy = Fluent::Config::ConfigureProxy.new('section', type_lookup: @type_lookup)
  12. assert_equal(:section, proxy.name)
  13. proxy = Fluent::Config::ConfigureProxy.new(:section, type_lookup: @type_lookup)
  14. assert_equal(:section, proxy.name)
  15. assert_nil(proxy.param_name)
  16. assert_equal(:section, proxy.variable_name)
  17. assert_false(proxy.root?)
  18. assert_nil(proxy.init)
  19. assert_nil(proxy.required)
  20. assert_false(proxy.required?)
  21. assert_nil(proxy.multi)
  22. assert_true(proxy.multi?)
  23. end
  24. test 'can specify param_name/required/multi with optional arguments' do
  25. proxy = Fluent::Config::ConfigureProxy.new(:section, param_name: 'sections', init: true, required: false, multi: true, type_lookup: @type_lookup)
  26. assert_equal(:section, proxy.name)
  27. assert_equal(:sections, proxy.param_name)
  28. assert_equal(:sections, proxy.variable_name)
  29. assert_false(proxy.required)
  30. assert_false(proxy.required?)
  31. assert_true(proxy.multi)
  32. assert_true(proxy.multi?)
  33. proxy = Fluent::Config::ConfigureProxy.new(:section, param_name: :sections, init: false, required: true, multi: false, type_lookup: @type_lookup)
  34. assert_equal(:section, proxy.name)
  35. assert_equal(:sections, proxy.param_name)
  36. assert_equal(:sections, proxy.variable_name)
  37. assert_true(proxy.required)
  38. assert_true(proxy.required?)
  39. assert_false(proxy.multi)
  40. assert_false(proxy.multi?)
  41. end
  42. test 'raise error if both of init and required are true' do
  43. assert_raise "init and required are exclusive" do
  44. Fluent::Config::ConfigureProxy.new(:section, init: true, required: true, type_lookup: @type_lookup)
  45. end
  46. end
  47. end
  48. sub_test_case '#merge' do
  49. test 'generate a new instance which values are overwritten by the argument object' do
  50. proxy = p1 = Fluent::Config::ConfigureProxy.new(:section, type_lookup: @type_lookup)
  51. assert_equal(:section, proxy.name)
  52. assert_nil(proxy.param_name)
  53. assert_equal(:section, proxy.variable_name)
  54. assert_nil(proxy.init)
  55. assert_nil(proxy.required)
  56. assert_false(proxy.required?)
  57. assert_nil(proxy.multi)
  58. assert_true(proxy.multi?)
  59. assert_nil(proxy.configured_in_section)
  60. p2 = Fluent::Config::ConfigureProxy.new(:section, init: false, required: true, multi: false, type_lookup: @type_lookup)
  61. proxy = p1.merge(p2)
  62. assert_equal(:section, proxy.name)
  63. assert_nil(proxy.param_name)
  64. assert_equal(:section, proxy.variable_name)
  65. assert_false(proxy.init)
  66. assert_false(proxy.init?)
  67. assert_true(proxy.required)
  68. assert_true(proxy.required?)
  69. assert_false(proxy.multi)
  70. assert_false(proxy.multi?)
  71. assert_nil(proxy.configured_in_section)
  72. end
  73. test 'does not overwrite with argument object without any specifications of required/multi' do
  74. p1 = Fluent::Config::ConfigureProxy.new(:section1, param_name: :sections, type_lookup: @type_lookup)
  75. p1.configured_in_section = :subsection
  76. p2 = Fluent::Config::ConfigureProxy.new(:section2, init: false, required: true, multi: false, type_lookup: @type_lookup)
  77. p3 = Fluent::Config::ConfigureProxy.new(:section3, type_lookup: @type_lookup)
  78. proxy = p1.merge(p2).merge(p3)
  79. assert_equal(:section1, proxy.name)
  80. assert_equal(:sections, proxy.param_name)
  81. assert_equal(:sections, proxy.variable_name)
  82. assert_false(proxy.init)
  83. assert_false(proxy.init?)
  84. assert_true(proxy.required)
  85. assert_true(proxy.required?)
  86. assert_false(proxy.multi)
  87. assert_false(proxy.multi?)
  88. assert_equal :subsection, proxy.configured_in_section
  89. end
  90. test "does overwrite name of proxy for root sections which are used for plugins" do
  91. # latest plugin class shows actual plugin implementation
  92. p1 = Fluent::Config::ConfigureProxy.new('Fluent::Plugin::MyP1'.to_sym, root: true, required: true, multi: false, type_lookup: @type_lookup)
  93. p1.config_param :key1, :integer
  94. p2 = Fluent::Config::ConfigureProxy.new('Fluent::Plugin::MyP2'.to_sym, root: true, required: true, multi: false, type_lookup: @type_lookup)
  95. p2.config_param :key2, :string, default: "value2"
  96. merged = p1.merge(p2)
  97. assert_equal 'Fluent::Plugin::MyP2'.to_sym, merged.name
  98. assert_true merged.root?
  99. end
  100. end
  101. sub_test_case '#overwrite_defaults' do
  102. test 'overwrites only defaults with others defaults' do
  103. type_lookup = ->(type) { Fluent::Configurable.lookup_type(type) }
  104. p1 = Fluent::Config::ConfigureProxy.new(:mychild, type_lookup: type_lookup)
  105. p1.configured_in_section = :child
  106. p1.config_param(:k1a, :string)
  107. p1.config_param(:k1b, :string)
  108. p1.config_param(:k2a, :integer, default: 0)
  109. p1.config_param(:k2b, :integer, default: 0)
  110. p1.config_section(:sub1) do
  111. config_param :k3, :time, default: 30
  112. end
  113. p0 = Fluent::Config::ConfigureProxy.new(:myparent, type_lookup: type_lookup)
  114. p0.config_section(:child) do
  115. config_set_default :k1a, "v1a"
  116. config_param :k1b, :string, default: "v1b"
  117. config_set_default :k2a, 21
  118. config_param :k2b, :integer, default: 22
  119. config_section :sub1 do
  120. config_set_default :k3, 60
  121. end
  122. end
  123. p1.overwrite_defaults(p0.sections[:child])
  124. assert_equal "v1a", p1.defaults[:k1a]
  125. assert_equal "v1b", p1.defaults[:k1b]
  126. assert_equal 21, p1.defaults[:k2a]
  127. assert_equal 22, p1.defaults[:k2b]
  128. assert_equal 60, p1.sections[:sub1].defaults[:k3]
  129. end
  130. end
  131. sub_test_case '#configured_in' do
  132. test 'sets a section name which have configuration parameters of target plugin in owners configuration' do
  133. proxy = Fluent::Config::ConfigureProxy.new(:section, type_lookup: @type_lookup)
  134. proxy.configured_in(:mysection)
  135. assert_equal :mysection, proxy.configured_in_section
  136. end
  137. test 'do not permit to be called twice' do
  138. proxy = Fluent::Config::ConfigureProxy.new(:section, type_lookup: @type_lookup)
  139. proxy.configured_in(:mysection)
  140. assert_raise(ArgumentError) { proxy.configured_in(:myothersection) }
  141. end
  142. end
  143. sub_test_case '#config_param / #config_set_default / #config_argument' do
  144. setup do
  145. @proxy = Fluent::Config::ConfigureProxy.new(:section, type_lookup: @type_lookup)
  146. end
  147. test 'does not permit config_set_default for param w/ :default option' do
  148. @proxy.config_param(:name, :string, default: "name1")
  149. assert_raise(ArgumentError) { @proxy.config_set_default(:name, "name2") }
  150. end
  151. test 'does not permit default value specification twice' do
  152. @proxy.config_param(:name, :string)
  153. @proxy.config_set_default(:name, "name1")
  154. assert_raise(ArgumentError) { @proxy.config_set_default(:name, "name2") }
  155. end
  156. test 'does not permit default value specification twice, even on config_argument' do
  157. @proxy.config_param(:name, :string)
  158. @proxy.config_set_default(:name, "name1")
  159. @proxy.config_argument(:name)
  160. assert_raise(ArgumentError) { @proxy.config_argument(:name, default: "name2") }
  161. end
  162. end
  163. sub_test_case '#config_param without default values cause error if section is configured as init:true' do
  164. setup do
  165. @proxy = Fluent::Config::ConfigureProxy.new(:section, type_lookup: @type_lookup)
  166. end
  167. test 'with simple config_param with default value' do
  168. assert_nothing_raised do
  169. @proxy.config_section :subsection, init: true do
  170. config_param :param1, :integer, default: 1
  171. end
  172. end
  173. end
  174. test 'with simple config_param without default value' do
  175. assert_raise ArgumentError do
  176. @proxy.config_section :subsection, init: true do
  177. config_param :param1, :integer
  178. end
  179. end
  180. end
  181. test 'with config_param with config_set_default' do
  182. assert_nothing_raised do
  183. @proxy.config_section :subsection, init: true do
  184. config_param :param1, :integer
  185. config_set_default :param1, 1
  186. end
  187. end
  188. end
  189. test 'with config_argument' do
  190. assert_raise ArgumentError do
  191. @proxy.config_section :subsection, init: true do
  192. config_argument :param0, :string
  193. end
  194. end
  195. end
  196. test 'with config_argument with default value' do
  197. assert_nothing_raised do
  198. @proxy.config_section :subsection, init: true do
  199. config_argument :param0, :string, default: ''
  200. end
  201. end
  202. end
  203. end
  204. sub_test_case '#config_set_desc' do
  205. setup do
  206. @proxy = Fluent::Config::ConfigureProxy.new(:section, type_lookup: @type_lookup)
  207. end
  208. test 'does not permit description specification twice w/ :desc option' do
  209. @proxy.config_param(:name, :string, desc: "description")
  210. assert_raise(ArgumentError) { @proxy.config_set_desc(:name, "description2") }
  211. end
  212. test 'does not permit description specification twice' do
  213. @proxy.config_param(:name, :string)
  214. @proxy.config_set_desc(:name, "description")
  215. assert_raise(ArgumentError) { @proxy.config_set_desc(:name, "description2") }
  216. end
  217. end
  218. sub_test_case '#desc' do
  219. setup do
  220. @proxy = Fluent::Config::ConfigureProxy.new(:section, type_lookup: @type_lookup)
  221. end
  222. test 'permit to specify description twice' do
  223. @proxy.desc("description1")
  224. @proxy.desc("description2")
  225. @proxy.config_param(:name, :string)
  226. assert_equal("description2", @proxy.descriptions[:name])
  227. end
  228. test 'does not permit description specification twice' do
  229. @proxy.desc("description1")
  230. assert_raise(ArgumentError) do
  231. @proxy.config_param(:name, :string, desc: "description2")
  232. end
  233. end
  234. end
  235. sub_test_case '#dump' do
  236. setup do
  237. @proxy = Fluent::Config::ConfigureProxy.new(:section, type_lookup: @type_lookup)
  238. end
  239. test 'empty proxy' do
  240. assert_equal("", @proxy.dump)
  241. end
  242. test 'plain proxy w/o default value' do
  243. @proxy.config_param(:name, :string)
  244. assert_equal(<<CONFIG, @proxy.dump)
  245. name: string: <nil>
  246. CONFIG
  247. end
  248. test 'plain proxy w/ default value' do
  249. @proxy.config_param(:name, :string, default: "name1")
  250. assert_equal(<<CONFIG, @proxy.dump)
  251. name: string: <"name1">
  252. CONFIG
  253. end
  254. test 'plain proxy w/ default value using config_set_default' do
  255. @proxy.config_param(:name, :string)
  256. @proxy.config_set_default(:name, "name1")
  257. assert_equal(<<CONFIG, @proxy.dump)
  258. name: string: <"name1">
  259. CONFIG
  260. end
  261. test 'single sub proxy' do
  262. @proxy.config_section(:sub) do
  263. config_param(:name, :string, default: "name1")
  264. end
  265. assert_equal(<<CONFIG, @proxy.dump)
  266. sub
  267. name: string: <"name1">
  268. CONFIG
  269. end
  270. test 'nested sub proxy' do
  271. @proxy.config_section(:sub) do
  272. config_param(:name1, :string, default: "name1")
  273. config_param(:name2, :string, default: "name2")
  274. config_section(:sub2) do
  275. config_param(:name3, :string, default: "name3")
  276. config_param(:name4, :string, default: "name4")
  277. end
  278. end
  279. assert_equal(<<CONFIG, @proxy.dump)
  280. sub
  281. name1: string: <"name1">
  282. name2: string: <"name2">
  283. sub2
  284. name3: string: <"name3">
  285. name4: string: <"name4">
  286. CONFIG
  287. end
  288. sub_test_case 'w/ description' do
  289. test 'single proxy' do
  290. @proxy.config_param(:name, :string, desc: "description for name")
  291. assert_equal(<<CONFIG, @proxy.dump)
  292. name: string: <nil> # description for name
  293. CONFIG
  294. end
  295. test 'single proxy using config_set_desc' do
  296. @proxy.config_param(:name, :string)
  297. @proxy.config_set_desc(:name, "description for name")
  298. assert_equal(<<CONFIG, @proxy.dump)
  299. name: string: <nil> # description for name
  300. CONFIG
  301. end
  302. test 'sub proxy' do
  303. @proxy.config_section(:sub) do
  304. config_param(:name1, :string, default: "name1", desc: "desc1")
  305. config_param(:name2, :string, default: "name2", desc: "desc2")
  306. config_section(:sub2) do
  307. config_param(:name3, :string, default: "name3")
  308. config_param(:name4, :string, default: "name4", desc: "desc4")
  309. end
  310. end
  311. assert_equal(<<CONFIG, @proxy.dump)
  312. sub
  313. name1: string: <"name1"> # desc1
  314. name2: string: <"name2"> # desc2
  315. sub2
  316. name3: string: <"name3">
  317. name4: string: <"name4"> # desc4
  318. CONFIG
  319. end
  320. test 'sub proxy w/ desc method' do
  321. @proxy.config_section(:sub) do
  322. desc("desc1")
  323. config_param(:name1, :string, default: "name1")
  324. config_param(:name2, :string, default: "name2", desc: "desc2")
  325. config_section(:sub2) do
  326. config_param(:name3, :string, default: "name3")
  327. desc("desc4")
  328. config_param(:name4, :string, default: "name4")
  329. end
  330. end
  331. assert_equal(<<CONFIG, @proxy.dump)
  332. sub
  333. name1: string: <"name1"> # desc1
  334. name2: string: <"name2"> # desc2
  335. sub2
  336. name3: string: <"name3">
  337. name4: string: <"name4"> # desc4
  338. CONFIG
  339. end
  340. end
  341. end
  342. end
  343. end
  344. end