test_dyncfg.rb 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #!/usr/bin/env ruby
  2. require 'json'
  3. require 'httparty'
  4. require 'pastel'
  5. require 'securerandom'
  6. ARGV.length == 1 or raise "Usage: #{$0} <config file>"
  7. config_file = ARGV[0]
  8. File.exist?(config_file) or raise "File not found: #{config_file}"
  9. $config = JSON.parse(File.read(config_file), symbolize_names: true)
  10. $plugin_name = $config[:global][:test_plugin_name]
  11. $pastel = Pastel.new
  12. class TestRunner
  13. attr_reader :stats
  14. def initialize
  15. @stats = {
  16. :suites => 0,
  17. :tests => 0,
  18. :assertions => 0
  19. }
  20. @test = nil
  21. end
  22. def add_assertion()
  23. @stats[:assertions] += 1
  24. end
  25. def FAIL(msg, exception = nil, loc = nil)
  26. puts $pastel.red.bold(" ✕ FAIL")
  27. STDERR.print " "
  28. if loc
  29. STDERR.print $pastel.yellow("@#{loc.path}:#{loc.lineno}: ")
  30. else
  31. STDERR.print $pastel.yellow("@#{caller_locations(1, 1).first.path}:#{caller_locations(1, 1).first.lineno}: ")
  32. end
  33. STDERR.puts msg
  34. STDERR.puts exception.full_message(:highlight => true) if exception
  35. STDERR.puts $pastel.yellow(" Backtrace:")
  36. caller.each do |line|
  37. STDERR.puts " #{line}"
  38. end
  39. exit 1
  40. end
  41. def PASS()
  42. STDERR.puts $pastel.green.bold(" ✓ PASS")
  43. @stats[:tests] += 1
  44. @test = nil
  45. end
  46. def TEST_SUITE(name)
  47. puts $pastel.bold("• TEST SUITE: \"#{name}\"")
  48. @stats[:suites] += 1
  49. end
  50. def assert_no_test_running()
  51. unless @test.nil?
  52. STDERR.puts $pastel.red("\nFATAL: Test \"#{@test}\" did not call PASS() or FAIL()!")
  53. exit 1
  54. end
  55. end
  56. def TEST(name, description = nil)
  57. assert_no_test_running()
  58. @test = name
  59. col = 0
  60. txt = " ├─ T: #{name} "
  61. col += txt.length
  62. print $pastel.bold(txt)
  63. tab = 50
  64. rem = tab - (col % tab)
  65. rem.times do putc ' ' end
  66. col += rem
  67. if (description)
  68. txt = " - #{description} "
  69. col += txt.length
  70. print txt
  71. tab = 180
  72. rem = tab - (col % tab)
  73. rem.times do putc '.' end
  74. end
  75. end
  76. def FINALIZE()
  77. assert_no_test_running()
  78. end
  79. end
  80. $test_runner = TestRunner.new
  81. def FAIL(msg, exception = nil, loc = nil)
  82. $test_runner.FAIL(msg, exception, loc)
  83. end
  84. def PASS()
  85. $test_runner.PASS()
  86. end
  87. def TEST_SUITE(name)
  88. $test_runner.TEST_SUITE(name)
  89. end
  90. def TEST(name, description = nil)
  91. $test_runner.TEST(name, description)
  92. end
  93. def assert_eq(got, expected, msg = nil)
  94. unless got == expected
  95. FAIL("Expected #{expected}, got #{got} #{msg ? "(#{msg})" : ""}", nil, caller_locations(1, 1).first)
  96. end
  97. $test_runner.add_assertion()
  98. end
  99. def assert_eq_http_code(got, expected, msg = nil)
  100. unless got.code == expected
  101. FAIL("Expected #{expected}, got #{got}. Server \"#{got.parsed_response}\" #{msg ? "(#{msg})" : ""}", nil, caller_locations(1, 1).first)
  102. end
  103. $test_runner.add_assertion()
  104. end
  105. def assert_eq_str(got, expected, msg = nil)
  106. unless got == expected
  107. FAIL("Strings do not match #{msg ? "(#{msg})" : ""}", nil, caller_locations(1, 1).first)
  108. end
  109. $test_runner.add_assertion()
  110. end
  111. def assert_not_eq_str(got, expected, msg = nil)
  112. unless got != expected
  113. FAIL("Strings shoud not match #{msg ? "(#{msg})" : ""}", nil, caller_locations(1, 1).first)
  114. end
  115. $test_runner.add_assertion()
  116. end
  117. def assert_nothing_raised()
  118. begin
  119. yield
  120. rescue Exception => e
  121. FAIL("Unexpected exception of type #{e.class} raised. Msg: \"#{e.message}\"", e, caller_locations(1, 1).first)
  122. end
  123. $test_runner.add_assertion()
  124. end
  125. def assert_has_key?(hash, key)
  126. unless hash.has_key?(key)
  127. FAIL("Expected key \"#{key}\" in hash", nil, caller_locations(1, 1).first)
  128. end
  129. $test_runner.add_assertion()
  130. end
  131. def assert_array_include?(array, value)
  132. unless array.include?(value)
  133. FAIL("Expected array to include \"#{value}\"", nil, caller_locations(1, 1).first)
  134. end
  135. $test_runner.add_assertion()
  136. end
  137. def assert_array_not_include?(array, value)
  138. if array.include?(value)
  139. FAIL("Expected array to not include \"#{value}\"", nil, caller_locations(1, 1).first)
  140. end
  141. $test_runner.add_assertion()
  142. end
  143. def assert_is_one_of(value, *values)
  144. unless values.include?(value)
  145. FAIL("Expected value to be one of #{values.join(", ")}", nil, caller_locations(1, 1).first)
  146. end
  147. $test_runner.add_assertion()
  148. end
  149. def assert_not_nil(value)
  150. if value.nil?
  151. FAIL("Expected value to not be nil", nil, caller_locations(1, 1).first)
  152. end
  153. $test_runner.add_assertion()
  154. end
  155. def assert_nil(value)
  156. unless value.nil?
  157. FAIL("Expected value to be nil", nil, caller_locations(1, 1).first)
  158. end
  159. $test_runner.add_assertion()
  160. end
  161. class DynCfgHttpClient
  162. def self.protocol(cfg)
  163. return cfg[:ssl] ? 'https://' : 'http://'
  164. end
  165. def self.url_base(host)
  166. return "#{protocol(host)}#{host[:host]}:#{host[:port]}"
  167. end
  168. def self.get_url_cfg_base(host, child = nil)
  169. url = url_base(host)
  170. url += "/host/#{child[:mguid]}" if child
  171. url += "/api/v2/config"
  172. return url
  173. end
  174. def self.get_url_cfg_plugin(host, plugin, child = nil)
  175. return get_url_cfg_base(host, child) + '/' + plugin
  176. end
  177. def self.get_url_cfg_module(host, plugin, mod, child = nil)
  178. return get_url_cfg_plugin(host, plugin, child) + '/' + mod
  179. end
  180. def self.get_url_cfg_job(host, plugin, mod, job_id, child = nil)
  181. return get_url_cfg_module(host, plugin, mod, child) + "/#{job_id}"
  182. end
  183. def self.get_plugin_list(host, child = nil)
  184. begin
  185. return HTTParty.get(get_url_cfg_base(host, child), verify: false, format: :plain)
  186. rescue => e
  187. FAIL(e.message, e)
  188. end
  189. end
  190. def self.get_plugin_config(host, plugin, child = nil)
  191. begin
  192. return HTTParty.get(get_url_cfg_plugin(host, plugin, child), verify: false)
  193. rescue => e
  194. FAIL(e.message, e)
  195. end
  196. end
  197. def self.set_plugin_config(host, plugin, cfg, child = nil)
  198. begin
  199. return HTTParty.put(get_url_cfg_plugin(host, plugin, child), verify: false, body: cfg)
  200. rescue => e
  201. FAIL(e.message, e)
  202. end
  203. end
  204. def self.get_plugin_module_list(host, plugin, child = nil)
  205. begin
  206. return HTTParty.get(get_url_cfg_plugin(host, plugin, child) + "/modules", verify: false, format: :plain)
  207. rescue => e
  208. FAIL(e.message, e)
  209. end
  210. end
  211. def self.get_job_list(host, plugin, mod, child = nil)
  212. begin
  213. return HTTParty.get(get_url_cfg_module(host, plugin, mod, child) + "/jobs", verify: false, format: :plain)
  214. rescue => e
  215. FAIL(e.message, e)
  216. end
  217. end
  218. def self.create_job(host, plugin, mod, job_id, job_cfg, child = nil)
  219. begin
  220. return HTTParty.post(get_url_cfg_job(host, plugin, mod, job_id, child), verify: false, body: job_cfg)
  221. rescue => e
  222. FAIL(e.message, e)
  223. end
  224. end
  225. def self.delete_job(host, plugin, mod, job_id, child = nil)
  226. begin
  227. return HTTParty.delete(get_url_cfg_job(host, plugin, mod, job_id, child), verify: false)
  228. rescue => e
  229. FAIL(e.message, e)
  230. end
  231. end
  232. def self.get_job_config(host, plugin, mod, job_id, child = nil)
  233. begin
  234. return HTTParty.get(get_url_cfg_job(host, plugin, mod, job_id, child), verify: false, format: :plain)
  235. rescue => e
  236. FAIL(e.message, e)
  237. end
  238. end
  239. def self.set_job_config(host, plugin, mod, job_id, job_cfg, child = nil)
  240. begin
  241. return HTTParty.put(get_url_cfg_job(host, plugin, mod, job_id, child), verify: false, body: job_cfg)
  242. rescue => e
  243. FAIL(e.message, e)
  244. end
  245. end
  246. end
  247. require_relative 'sub_tests/test_parent_child.rb'
  248. $test_runner.FINALIZE()
  249. puts $pastel.green.bold("All tests passed!")
  250. puts ("Total #{$test_runner.stats[:assertions]} assertions, #{$test_runner.stats[:tests]} tests in #{$test_runner.stats[:suites]} suites")
  251. exit 0