Advertisement
Guest User

Untitled

a guest
Dec 15th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.29 KB | None | 0 0
  1. require_relative '../models/manageiq/providers/hawkular/middleware_manager/hawkular_helper'
  2.  
  3. describe "Alert mgmt flow:" do
  4. let(:alert) { FactoryGirl.create(:miq_alert_mw_heap_used, :id => 201) }
  5. let(:vcr_record_mode) do
  6. :once #:new_episodes
  7. end
  8.  
  9. subject!(:ems) do
  10. ems = ems_hawkular_fixture
  11. ems.guid = "def"
  12. ems.save!
  13. ems
  14. end
  15.  
  16. before do
  17. MiqRegion.seed
  18. MiqRegion.my_region.guid = "abc"
  19. MiqRegion.my_region.save!
  20. @hwclient = nil
  21. end
  22.  
  23. around do |ex|
  24. vcr_opts = ex.metadata[:vcr]
  25. vcr_opts[:record] = vcr_record_mode
  26. vcr_opts[:allow_unused_http_interactions] = true
  27. vcr_opts[:decode_compressed_response] = true
  28. cassette_name = vcr_opts.delete(:cassette_name)
  29. cassette_name = "integration/alert_mgmt_flows/#{cassette_name}"
  30.  
  31. VCR.use_cassette(cassette_name, vcr_opts) do
  32. ex.run
  33. end
  34. end
  35.  
  36. def ems_class
  37. ManageIQ::Providers::Hawkular::MiddlewareManager
  38. end
  39.  
  40. def alert_manager_class
  41. ManageIQ::Providers::Hawkular::MiddlewareManager::AlertManager
  42. end
  43.  
  44. def hawkular_client
  45. @hclient ||= Hawkular::Client.new(
  46. :credentials => {
  47. :username => test_userid,
  48. :password => test_password
  49. },
  50. :options => { :tenant => 'hawkular' },
  51. :entrypoint => URI::HTTP.build(:host => test_hostname, :port => test_port)
  52. )
  53. end
  54.  
  55. def alerts_client
  56. hawkular_client.alerts
  57. end
  58.  
  59. describe "alerts" do
  60. it "CRUD flow is propagated to Hawkular", :vcr => { :cassette_name => 'alerts_crud_flow' } do
  61. # STAGE 1
  62. # Notify to EMS an alert was created
  63. ems_class.update_alert(:operation => :new, :alert => alert)
  64.  
  65. # Verify a trigger is in Hawkular
  66. hawkular_alert_id = alert_manager_class.build_hawkular_trigger_id(:ems => ems, :alert => alert)
  67. trigger = alerts_client.list_triggers(hawkular_alert_id)
  68. expect(trigger.count).to eq(1)
  69.  
  70. # STAGE 2
  71. # Update alert condition and notify to EMS
  72. alert.expression[:options][:value_mw_greater_than] = 50
  73. alert.save
  74. ems_class.update_alert(:operation => :update, :alert => alert)
  75.  
  76. # Verify trigger condition was updated in Hawkular
  77. trigger = alerts_client.get_single_trigger(hawkular_alert_id, true)
  78. expect(trigger.conditions.count).to eq(1)
  79. expect trigger.conditions[0].expression.include?('> 0.5')
  80.  
  81. # STAGE 3
  82. # Delete alert and notify to EMS
  83. alert.destroy
  84. ems_class.update_alert(:operation => :delete, :alert => alert)
  85.  
  86. # Verify trigger has been deleted in Hawkular
  87. trigger = alerts_client.list_triggers(hawkular_alert_id)
  88. expect(trigger.count).to be_zero
  89. end
  90.  
  91. it "should fallback to old alerts id format if an alert with the new id does not exist in Hawkular",
  92. :vcr => { :cassette_name => 'fallback_to_old_ids_format' } do
  93. # Temporarily mock construction of id
  94. allow(alert_manager_class).to receive(:build_hawkular_trigger_id).and_return("MiQ-#{alert.id}")
  95.  
  96. # Create alert in Hawkular with old id format
  97. ems_class.update_alert(:operation => :new, :alert => alert)
  98.  
  99. trigger = alerts_client.list_triggers("MiQ-#{alert.id}")
  100. expect(trigger.count).to eq(1)
  101.  
  102. # Remove mock
  103. allow(alert_manager_class).to receive(:build_hawkular_trigger_id).and_call_original
  104. expect(alert_manager_class.build_hawkular_trigger_id(:ems => ems, :alert => { :id => 1 })).to include('ems') # just to check mock is removed
  105.  
  106. # Delete alert and notify to EMS
  107. alert.destroy
  108. ems_class.update_alert(:operation => :delete, :alert => alert)
  109.  
  110. # Verify trigger has been deleted in Hawkular
  111. trigger = alerts_client.list_triggers("MiQ-#{alert.id}")
  112. expect(trigger.count).to be_zero
  113. end
  114. end
  115.  
  116. ems_refresh_request_matcher = lambda do |request_1, request_2|
  117. uri_1 = request_1.uri.gsub(/start=\d+/, 'start=123456')
  118. uri_2 = request_2.uri.gsub(/start=\d+/, 'start=123456')
  119. uri_1 == uri_2
  120. end
  121.  
  122. describe "alert profiles" do
  123. # This context assumes that there is a wildfly server
  124. # in domain mode (with the shipped sample domain configs)
  125. # connected to hawkular services. This means that hawkular
  126. # should have registered the relevant inventory entities.
  127.  
  128. let(:profile) { FactoryGirl.create(:miq_alert_set_mw, :id => 202) }
  129. let(:server_one) do
  130. s1 = ManageIQ::Providers::Hawkular::MiddlewareManager::
  131. MiddlewareServer.find_by(:name => 'server-one')
  132. s1.update_column(:id, 400)
  133. s1.reload
  134. end
  135.  
  136. before do
  137. VCR.use_cassette('integration/alert_mgmt_flows/profiles_hawkular_setup',
  138. :allow_unused_http_interactions => true,
  139. :decode_compressed_response => true,
  140. :match_requests_on => [:method, ems_refresh_request_matcher],
  141. :record => vcr_record_mode) do # , :record => :all) do :new_episode
  142. # Update MiQ inventory
  143. EmsRefresh.refresh(ems)
  144. ems.reload
  145.  
  146. # Place group trigger in Hawkular
  147. ems_class.update_alert(:operation => :new, :alert => alert)
  148. alert.reload
  149. end
  150. end
  151.  
  152. after do
  153. VCR.use_cassette('integration/alert_mgmt_flows/profiles_hawkular_cleanup',
  154. :allow_unused_http_interactions => true,
  155. :decode_compressed_response => true,
  156. :record => vcr_record_mode) do # , :record => :all) do
  157. # Cleanup group trigger in Hawkular
  158. ems_class.update_alert(:operation => :delete, :alert => alert)
  159. end
  160. end
  161.  
  162. it "without assigned servers shouldn't create members in Hawkular when adding alerts",
  163. :vcr => { :cassette_name => 'add_alerts_to_profile_with_no_servers' } do
  164. # Setup
  165. profile.add_member(alert)
  166.  
  167. ems_class.update_alert_profile(
  168. :operation => :update_alerts,
  169. :profile_id => profile.id,
  170. :old_alerts => [],
  171. :new_alerts => [alert.id],
  172. :old_assignments => [],
  173. :new_assignments => nil
  174. )
  175.  
  176. # Verify
  177. triggers = alerts_client.list_triggers
  178. expect(triggers.select { |t| t.type == 'MEMBER' }.count).to be_zero
  179. end
  180.  
  181. it "without alerts shouldn't create members in Hawkular when adding servers",
  182. :vcr => { :cassette_name => 'add_servers_to_profile_with_no_alerts' } do
  183. # Setup
  184. profile.assign_to_objects([server_one])
  185.  
  186. ems_class.update_alert_profile(
  187. :operation => :update_assignments,
  188. :profile_id => profile.id,
  189. :old_alerts => [],
  190. :new_alerts => [],
  191. :old_assignments => [],
  192. :new_assignments => {"objects" => [server_one.id], "assign_to" => server_one.class}
  193. )
  194.  
  195. # Verify
  196. triggers = alerts_client.list_triggers
  197. expect(triggers.select { |t| t.type == 'MEMBER' }.count).to be_zero
  198. end
  199.  
  200. it "with alerts should update members in Hawkular when assigning and unassigning a server",
  201. :vcr => { :cassette_name => 'assign_unassign_server_to_profile_with_alerts' } do
  202. # Setup
  203. profile.add_member(alert)
  204.  
  205. # Add the server
  206. profile.assign_to_objects([server_one])
  207.  
  208. ems_class.update_alert_profile(
  209. :operation => :update_assignments,
  210. :profile_id => profile.id,
  211. :old_alerts => [alert.id],
  212. :new_alerts => [],
  213. :old_assignments => [],
  214. :new_assignments => {"objects" => [server_one.id], "assign_to" => server_one.class}
  215. )
  216.  
  217. # Verify
  218. triggers = alerts_client.list_triggers
  219. expect(triggers.select { |t| t.type == 'MEMBER' }.count).to eq(1)
  220.  
  221. # Remove server
  222. profile.remove_all_assigned_tos
  223.  
  224. ems_class.update_alert_profile(
  225. :operation => :update_assignments,
  226. :profile_id => profile.id,
  227. :old_alerts => [alert.id],
  228. :new_alerts => [],
  229. :old_assignments => [server_one],
  230. :new_assignments => nil
  231. )
  232.  
  233. # Verify
  234. triggers = alerts_client.list_triggers
  235. expect(triggers.select { |t| t.type == 'MEMBER' }.count).to be_zero
  236. end
  237.  
  238. it "with servers should update members in Hawkular when assigning and unassigning an alert",
  239. :vcr => { :cassette_name => 'assign_unassign_alert_to_profile_with_servers' } do
  240. # Setup
  241. profile.assign_to_objects([server_one])
  242.  
  243. # Add the alert
  244. profile.add_member(alert)
  245. ems_class.update_alert_profile(
  246. :operation => :update_alerts,
  247. :profile_id => profile.id,
  248. :old_alerts => [],
  249. :new_alerts => [alert.id],
  250. :old_assignments => [server_one],
  251. :new_assignments => nil
  252. )
  253.  
  254. # Verify
  255. triggers = alerts_client.list_triggers
  256. expect(triggers.select { |t| t.type == 'MEMBER' }.count).to eq(1)
  257.  
  258. # Remove the alert
  259. profile.remove_member(alert)
  260.  
  261. ems_class.update_alert_profile(
  262. :operation => :update_alerts,
  263. :profile_id => profile.id,
  264. :old_alerts => [alert.id],
  265. :new_alerts => [],
  266. :old_assignments => [server_one],
  267. :new_assignments => nil
  268. )
  269.  
  270. # Verify
  271. triggers = alerts_client.list_triggers
  272. expect(triggers.select { |t| t.type == 'MEMBER' }.count).to be_zero
  273. end
  274. end
  275. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement