test_parent_child.rb 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. class ParentChildTest
  2. @@plugin_cfg = <<~HEREDOC
  3. { "test" : "true" }
  4. HEREDOC
  5. @@plugin_cfg2 = <<~HEREDOC
  6. { "asdfgh" : "asdfgh" }
  7. HEREDOC
  8. @@job_cfg = <<~HEREDOC
  9. { "i am newly created job" : "true" }
  10. HEREDOC
  11. def initialize
  12. @parent = $config[:http_endpoints][:parent]
  13. @child = $config[:http_endpoints][:child]
  14. @plugin = $config[:global][:test_plugin_name]
  15. @arry_mod = $config[:global][:test_array_module_name]
  16. @single_mod = $config[:global][:test_single_module_name]
  17. @test_job = $config[:global][:test_job_name]
  18. end
  19. def check_test_plugin_modules_list(host, child = nil)
  20. rc = DynCfgHttpClient.get_plugin_module_list(host, @plugin, child)
  21. assert_eq(rc.code, 200, "as HTTP code for get_module_list request on plugin \"#{@plugin}\"")
  22. modules = nil
  23. assert_nothing_raised do
  24. modules = JSON.parse(rc.parsed_response, symbolize_names: true)
  25. end
  26. assert_has_key?(modules, :modules)
  27. assert_eq(modules[:modules].count, 2, "as number of modules in plugin \"#{@plugin}\"")
  28. modules[:modules].each do |m|
  29. assert_has_key?(m, :name)
  30. assert_has_key?(m, :type)
  31. assert_is_one_of(m[:type], "job_array", "single")
  32. end
  33. assert_eq_str(modules[:modules][0][:name], @arry_mod, "name of first module in plugin \"#{@plugin}\"")
  34. assert_eq_str(modules[:modules][1][:name], @single_mod, "name of second module in plugin \"#{@plugin}\"")
  35. end
  36. def run
  37. TEST_SUITE("Parent/Child plugin config")
  38. TEST("parent/child/get_plugin_list", "Get child (hops:1) plugin list trough parent")
  39. plugins = DynCfgHttpClient.get_plugin_list(@parent, @child)
  40. assert_eq(plugins.code, 200, "as HTTP code for get_plugin_list request")
  41. assert_nothing_raised do
  42. plugins = JSON.parse(plugins.parsed_response, symbolize_names: true)
  43. end
  44. assert_has_key?(plugins, :configurable_plugins)
  45. assert_array_include?(plugins[:configurable_plugins], @plugin)
  46. PASS()
  47. TEST("parent/child/(set/get)plugin_config", "Set then get and compare child (hops:1) plugin config trough parent")
  48. rc = DynCfgHttpClient.set_plugin_config(@parent, @plugin, @@plugin_cfg, @child)
  49. assert_eq(rc.code, 200, "as HTTP code for set_plugin_config request")
  50. rc = DynCfgHttpClient.get_plugin_config(@parent, @plugin, @child)
  51. assert_eq(rc.code, 200, "as HTTP code for get_plugin_config request")
  52. assert_eq_str(rc.parsed_response.chomp!, @@plugin_cfg, "as plugin config")
  53. # We do this twice with different configs to ensure first config was not loaded from persistent storage (from previous tests)
  54. rc = DynCfgHttpClient.set_plugin_config(@parent, @plugin, @@plugin_cfg2, @child)
  55. assert_eq(rc.code, 200, "as HTTP code for set_plugin_config request 2")
  56. rc = DynCfgHttpClient.get_plugin_config(@parent, @plugin, @child)
  57. assert_eq(rc.code, 200, "as HTTP code for get_plugin_config request 2")
  58. assert_eq_str(rc.parsed_response.chomp!, @@plugin_cfg2, "set/get plugin config 2")
  59. PASS()
  60. TEST("child/get_plugin_config", "Get child (hops:0) plugin config and compare with what we got trough parent (set_plugin_config from previous test)")
  61. rc = DynCfgHttpClient.get_plugin_config(@child, @plugin, nil)
  62. assert_eq(rc.code, 200, "as HTTP code for get_plugin_config request")
  63. assert_eq_str(rc.parsed_response.chomp!, @@plugin_cfg2.chomp, "as plugin config")
  64. PASS()
  65. TEST("parent/child/plugin_module_list", "Get child (hops:1) plugin module list trough parent and check its contents")
  66. check_test_plugin_modules_list(@parent, @child)
  67. PASS()
  68. TEST("child/plugin_module_list", "Get child (hops:0) plugin module list directly and check its contents")
  69. check_test_plugin_modules_list(@child, nil)
  70. PASS()
  71. TEST("parent/child/module/jobs", "Get list of jobs from child (hops:1) trough parent and check its contents, check job updates")
  72. rc = DynCfgHttpClient.get_job_list(@parent, @plugin, @arry_mod, @child)
  73. assert_eq(rc.code, 200, "as HTTP code for get_jobs request")
  74. jobs = nil
  75. assert_nothing_raised do
  76. jobs = JSON.parse(rc.parsed_response, symbolize_names: true)
  77. end
  78. assert_has_key?(jobs, :jobs)
  79. new_job = jobs[:jobs].find {|i| i[:name] == @test_job}
  80. assert_not_nil(new_job)
  81. assert_has_key?(new_job, :status)
  82. assert_not_eq_str(new_job[:status], "unknown", "job status is other than unknown")
  83. assert_has_key?(new_job, :flags)
  84. assert_array_include?(new_job[:flags], "JOB_FLG_STREAMING_PUSHED")
  85. PASS()
  86. TEST("child/module/jobs", "Get list of jobs direct from child (hops:0) and check its contents, check job updates")
  87. rc = DynCfgHttpClient.get_job_list(@child, @plugin, @arry_mod, nil)
  88. assert_eq(rc.code, 200, "as HTTP code for get_jobs request")
  89. jobs = nil
  90. assert_nothing_raised do
  91. jobs = JSON.parse(rc.parsed_response, symbolize_names: true)
  92. end
  93. assert_has_key?(jobs, :jobs)
  94. new_job = jobs[:jobs].find {|i| i[:name] == @test_job}
  95. assert_not_nil(new_job)
  96. assert_has_key?(new_job, :status)
  97. assert_not_eq_str(new_job[:status], "unknown", "job status is other than unknown")
  98. assert_has_key?(new_job, :flags)
  99. assert_array_not_include?(new_job[:flags], "JOB_FLG_STREAMING_PUSHED") # this is plugin directly at child so it should not show this flag
  100. PASS()
  101. TEST("parent/child/single_module/jobs", "Attempt getting list of jobs from child (hops:1) trough parent on single module. Check it fails properly")
  102. rc = DynCfgHttpClient.get_job_list(@parent, @plugin, @single_mod, @child)
  103. assert_eq(rc.code, 400, "as HTTP code for get_jobs request")
  104. assert_eq_str(rc.parsed_response, '400 - this module is not array type', "as HTTP code for get_jobs request on single module")
  105. PASS()
  106. created_job = SecureRandom.uuid
  107. TEST("parent/child/module/cr_del_job", "Create and delete job on child (hops:1) trough parent")
  108. # create new job
  109. rc = DynCfgHttpClient.create_job(@parent, @plugin, @arry_mod, created_job, @@job_cfg, @child)
  110. assert_eq_http_code(rc, 200, "as HTTP code for create_job request")
  111. # check this job is in job list @parent
  112. rc = DynCfgHttpClient.get_job_list(@parent, @plugin, @arry_mod, @child)
  113. assert_eq(rc.code, 200, "as HTTP code for get_jobs request")
  114. jobs = nil
  115. assert_nothing_raised do
  116. jobs = JSON.parse(rc.parsed_response, symbolize_names: true)
  117. end
  118. assert_has_key?(jobs, :jobs)
  119. new_job = jobs[:jobs].find {|i| i[:name] == created_job}
  120. assert_not_nil(new_job)
  121. # check this job is in job list @child
  122. rc = DynCfgHttpClient.get_job_list(@child, @plugin, @arry_mod, nil)
  123. assert_eq(rc.code, 200, "as HTTP code for get_jobs request")
  124. jobs = nil
  125. assert_nothing_raised do
  126. jobs = JSON.parse(rc.parsed_response, symbolize_names: true)
  127. end
  128. assert_has_key?(jobs, :jobs)
  129. new_job = jobs[:jobs].find {|i| i[:name] == created_job}
  130. assert_not_nil(new_job)
  131. # check we can get job config back
  132. rc = DynCfgHttpClient.get_job_config(@parent, @plugin, @arry_mod, created_job, @child)
  133. assert_eq(rc.code, 200, "as HTTP code for get_job_config request")
  134. assert_eq_str(rc.parsed_response.chomp!, @@job_cfg, "as job config")
  135. # delete job
  136. rc = DynCfgHttpClient.delete_job(@parent, @plugin, @arry_mod, created_job, @child)
  137. assert_eq(rc.code, 200, "as HTTP code for delete_job request")
  138. # Check it is not in parents job list anymore
  139. rc = DynCfgHttpClient.get_job_list(@parent, @plugin, @arry_mod, @child)
  140. assert_eq(rc.code, 200, "as HTTP code for get_jobs request")
  141. jobs = nil
  142. assert_nothing_raised do
  143. jobs = JSON.parse(rc.parsed_response, symbolize_names: true)
  144. end
  145. assert_has_key?(jobs, :jobs)
  146. new_job = jobs[:jobs].find {|i| i[:name] == created_job}
  147. assert_nil(new_job)
  148. # Check it is not in childs job list anymore
  149. rc = DynCfgHttpClient.get_job_list(@child, @plugin, @arry_mod, nil)
  150. assert_eq(rc.code, 200, "as HTTP code for get_jobs request")
  151. jobs = nil
  152. assert_nothing_raised do
  153. jobs = JSON.parse(rc.parsed_response, symbolize_names: true)
  154. end
  155. assert_has_key?(jobs, :jobs)
  156. new_job = jobs[:jobs].find {|i| i[:name] == created_job}
  157. assert_nil(new_job)
  158. PASS()
  159. TEST("parent/child/module/del_undeletable_job", "Try delete job on child (child rejects), check failure case works (hops:1)")
  160. # test if plugin rejects job deletion the job still remains in list as it should
  161. rc = DynCfgHttpClient.delete_job(@parent, @plugin, @arry_mod, @test_job, @child)
  162. assert_eq(rc.code, 500, "as HTTP code for delete_job request")
  163. rc = DynCfgHttpClient.get_job_list(@parent, @plugin, @arry_mod, @child)
  164. assert_eq(rc.code, 200, "as HTTP code for get_jobs request")
  165. jobs = nil
  166. assert_nothing_raised do
  167. jobs = JSON.parse(rc.parsed_response, symbolize_names: true)
  168. end
  169. assert_has_key?(jobs, :jobs)
  170. job = jobs[:jobs].find {|i| i[:name] == @test_job}
  171. assert_not_nil(job)
  172. PASS()
  173. end
  174. end
  175. ParentChildTest.new.run()