PageRenderTime 52ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/overview_with_alerts/test.rb

https://gitlab.com/sgruhier/scout-plugins
Ruby | 343 lines | 303 code | 32 blank | 8 comment | 0 complexity | 0f57ab272f2eebed5ae94d74a07c4a93 MD5 | raw file
  1. require File.expand_path('../../test_helper.rb', __FILE__)
  2. require File.expand_path('../overview_with_alerts.rb', __FILE__)
  3. class OverviewWithAlertsTest < Test::Unit::TestCase
  4. def setup
  5. @options=parse_defaults("overview_with_alerts")
  6. end
  7. def teardown
  8. end
  9. def test_clean_run
  10. # Stub the plugin instance where necessary and run
  11. # @plugin=PluginName.new(last_run, memory, options)
  12. # date hash hash
  13. @plugin=OverviewWithAlerts.new(nil,{},@options)
  14. @plugin.stubs(:shell).with("df -h").returns(FIXTURES[:df]).once
  15. @plugin.stubs(:shell).with("cat /proc/meminfo").returns(FIXTURES[:meminfo]).once
  16. @plugin.stubs(:shell).with("uptime").returns(FIXTURES[:uptime]).once
  17. res= @plugin.run()
  18. # assertions
  19. assert res[:alerts].empty?
  20. assert res[:errors].empty?
  21. assert_equal 1, res[:reports].size
  22. end
  23. def test_memory_alert
  24. @plugin=OverviewWithAlerts.new(nil,{},@options)
  25. @plugin.stubs(:shell).with("df -h").returns(FIXTURES[:df]).once
  26. @plugin.stubs(:shell).with("cat /proc/meminfo").returns(FIXTURES[:meminfo_alert]).once
  27. @plugin.stubs(:shell).with("uptime").returns(FIXTURES[:uptime]).once
  28. res= @plugin.run()
  29. assert_equal 1, res[:alerts].size
  30. assert_equal "Memory Usage Alert", res[:alerts].first[:subject]
  31. @memory=res[:memory] # to be used in tests that run this as a precursor
  32. end
  33. def test_memory_alert_repeats
  34. test_memory_alert
  35. last_run=Time.now
  36. Timecop.travel(Time.now+60*60*13) do # 13 hours in future
  37. @plugin=OverviewWithAlerts.new(last_run,@memory,@options)
  38. @plugin.stubs(:shell).with("df -h").returns(FIXTURES[:df]).once
  39. @plugin.stubs(:shell).with("cat /proc/meminfo").returns(FIXTURES[:meminfo_alert]).once
  40. @plugin.stubs(:shell).with("uptime").returns(FIXTURES[:uptime]).once
  41. res = @plugin.run()
  42. assert_equal 1, res[:alerts].size
  43. assert res[:alerts].first[:body].include?("Duration")
  44. # This nested block tests that a second violation after the first alert (but not yet time for another notification)
  45. second_run_memory=res[:memory]
  46. last_run=Time.now
  47. Timecop.travel(Time.now+60*1) do # another 1 minute in the future
  48. @plugin=OverviewWithAlerts.new(last_run,second_run_memory,@options)
  49. @plugin.stubs(:shell).with("df -h").returns(FIXTURES[:df]).once
  50. @plugin.stubs(:shell).with("cat /proc/meminfo").returns(FIXTURES[:meminfo_alert]).once
  51. @plugin.stubs(:shell).with("uptime").returns(FIXTURES[:uptime]).once
  52. res= @plugin.run()
  53. assert_equal 0, res[:alerts].size
  54. # This nested block tests that another violation after the notification period sends another alert
  55. third_run_memory=res[:memory]
  56. last_run=Time.now
  57. Timecop.travel(Time.now+60*60*13) do # another 13 hours in the future
  58. @plugin=OverviewWithAlerts.new(last_run,third_run_memory,@options)
  59. @plugin.stubs(:shell).with("df -h").returns(FIXTURES[:df]).once
  60. @plugin.stubs(:shell).with("cat /proc/meminfo").returns(FIXTURES[:meminfo_alert]).once
  61. @plugin.stubs(:shell).with("uptime").returns(FIXTURES[:uptime]).once
  62. res= @plugin.run()
  63. assert_equal 1, res[:alerts].size
  64. end
  65. end
  66. end
  67. end
  68. def test_memory_alert_does_not_repeat_too_soon
  69. test_memory_alert
  70. last_run=Time.now
  71. Timecop.travel(Time.now+60*5) do # five minutes in future
  72. @plugin=OverviewWithAlerts.new(last_run,@memory,@options)
  73. @plugin.stubs(:shell).with("df -h").returns(FIXTURES[:df]).once
  74. @plugin.stubs(:shell).with("cat /proc/meminfo").returns(FIXTURES[:meminfo_alert]).once
  75. @plugin.stubs(:shell).with("uptime").returns(FIXTURES[:uptime]).once
  76. res= @plugin.run()
  77. assert_equal 0, res[:alerts].size
  78. end
  79. end
  80. def test_memory_back_to_normal
  81. test_memory_alert
  82. last_run=Time.now
  83. Timecop.travel(Time.now+60*5) do # five minutes in future
  84. @plugin=OverviewWithAlerts.new(last_run,@memory,@options)
  85. @plugin.stubs(:shell).with("df -h").returns(FIXTURES[:df]).once
  86. @plugin.stubs(:shell).with("cat /proc/meminfo").returns(FIXTURES[:meminfo]).once
  87. @plugin.stubs(:shell).with("uptime").returns(FIXTURES[:uptime]).once
  88. res= @plugin.run()
  89. assert_equal 1, res[:alerts].size
  90. assert_equal "Memory Usage OK", res[:alerts].first[:subject]
  91. end
  92. end
  93. def test_disk_alert
  94. @plugin=OverviewWithAlerts.new(nil,{},@options)
  95. @plugin.stubs(:shell).with("df -h").returns(FIXTURES[:df_alert]).once
  96. @plugin.stubs(:shell).with("cat /proc/meminfo").returns(FIXTURES[:meminfo]).once
  97. @plugin.stubs(:shell).with("uptime").returns(FIXTURES[:uptime]).once
  98. res= @plugin.run()
  99. assert_equal 1, res[:alerts].size
  100. assert_equal "Disk Usage Alert", res[:alerts].first[:subject]
  101. @memory=res[:memory] # to be used in tests that run this as a precursor
  102. end
  103. def test_disk_alert_repeats
  104. test_disk_alert
  105. last_run=Time.now
  106. Timecop.travel(Time.now+60*60*13) do # 13 hours in future
  107. @plugin=OverviewWithAlerts.new(last_run,@memory,@options)
  108. @plugin.stubs(:shell).with("df -h").returns(FIXTURES[:df_alert]).once
  109. @plugin.stubs(:shell).with("cat /proc/meminfo").returns(FIXTURES[:meminfo]).once
  110. @plugin.stubs(:shell).with("uptime").returns(FIXTURES[:uptime]).once
  111. res = @plugin.run()
  112. assert_equal 1, res[:alerts].size
  113. assert res[:alerts].first[:body].include?("Duration")
  114. # This nested block tests that a second violation after the first alert (but not yet time for another notification)
  115. second_run_memory=res[:memory]
  116. last_run=Time.now
  117. Timecop.travel(Time.now+60*1) do # another 1 minute in the future
  118. @plugin=OverviewWithAlerts.new(last_run,second_run_memory,@options)
  119. @plugin.stubs(:shell).with("df -h").returns(FIXTURES[:df_alert]).once
  120. @plugin.stubs(:shell).with("cat /proc/meminfo").returns(FIXTURES[:meminfo]).once
  121. @plugin.stubs(:shell).with("uptime").returns(FIXTURES[:uptime]).once
  122. res= @plugin.run()
  123. assert_equal 0, res[:alerts].size
  124. # This nested block tests that another violation after the notification period sends another alert
  125. third_run_memory=res[:memory]
  126. last_run=Time.now
  127. Timecop.travel(Time.now+60*60*13) do # another 13 hours in the future
  128. @plugin=OverviewWithAlerts.new(last_run,third_run_memory,@options)
  129. @plugin.stubs(:shell).with("df -h").returns(FIXTURES[:df_alert]).once
  130. @plugin.stubs(:shell).with("cat /proc/meminfo").returns(FIXTURES[:meminfo]).once
  131. @plugin.stubs(:shell).with("uptime").returns(FIXTURES[:uptime]).once
  132. res= @plugin.run()
  133. assert_equal 1, res[:alerts].size
  134. end
  135. end
  136. end
  137. end
  138. def test_disk_alert_does_not_repeat_too_soon
  139. test_disk_alert
  140. last_run=Time.now
  141. Timecop.travel(Time.now+60*15) do # 15 minutes in future
  142. @plugin=OverviewWithAlerts.new(last_run,@memory,@options)
  143. @plugin.stubs(:shell).with("df -h").returns(FIXTURES[:df_alert]).once
  144. @plugin.stubs(:shell).with("cat /proc/meminfo").returns(FIXTURES[:meminfo]).once
  145. @plugin.stubs(:shell).with("uptime").returns(FIXTURES[:uptime]).once
  146. res= @plugin.run()
  147. assert_equal 0, res[:alerts].size
  148. end
  149. end
  150. def test_disk_back_to_normal
  151. test_disk_alert
  152. last_run=Time.now
  153. Timecop.travel(Time.now+60*5) do # five minutes in future
  154. @plugin=OverviewWithAlerts.new(last_run,@memory,@options)
  155. @plugin.stubs(:shell).with("df -h").returns(FIXTURES[:df]).once
  156. @plugin.stubs(:shell).with("cat /proc/meminfo").returns(FIXTURES[:meminfo]).once
  157. @plugin.stubs(:shell).with("uptime").returns(FIXTURES[:uptime]).once
  158. res= @plugin.run()
  159. assert_equal 1, res[:alerts].size
  160. assert_equal "Disk Usage OK", res[:alerts].first[:subject]
  161. end
  162. end
  163. def test_vps
  164. File.stubs(:exist?).with('/proc/user_beancounters').returns(true).once
  165. @plugin=OverviewWithAlerts.new(nil,{},@options)
  166. @plugin.stubs(:shell).with("df -h").returns(FIXTURES[:df]).once
  167. @plugin.stubs(:shell).with("sudo cat /proc/user_beancounters").returns(FIXTURES[:beancounters]).once
  168. @plugin.stubs(:shell).with("uptime").returns(FIXTURES[:uptime]).once
  169. res= @plugin.run()
  170. assert_equal 512, res[:reports].first[:mem_total]
  171. assert_equal 47, res[:reports].first[:mem_used]
  172. assert_equal 0, res[:reports].first[:mem_swap_total]
  173. assert_equal 0, res[:reports].first[:mem_swap_used]
  174. assert_equal 0, res[:reports].first[:mem_swap_percent]
  175. assert_equal 0, res[:alerts].size
  176. end
  177. def test_vps_host
  178. File.stubs(:exist?).with('/proc/user_beancounters').returns(true).once
  179. @plugin=OverviewWithAlerts.new(nil,{},@options)
  180. @plugin.stubs(:shell).with("df -h").returns(FIXTURES[:df]).once
  181. @plugin.stubs(:shell).with("sudo cat /proc/user_beancounters").returns(FIXTURES[:beancounters_host]).once
  182. @plugin.stubs(:shell).with("cat /proc/meminfo").returns(FIXTURES[:meminfo]).once
  183. @plugin.stubs(:shell).with("uptime").returns(FIXTURES[:uptime]).once
  184. res= @plugin.run()
  185. assert_equal 1024, res[:reports].first[:mem_total]
  186. assert_equal 421, res[:reports].first[:mem_used]
  187. assert_equal 0, res[:alerts].size
  188. end
  189. FIXTURES=YAML.load(<<-EOS)
  190. :meminfo: |
  191. MemTotal: 1048796 kB
  192. MemFree: 220928 kB
  193. Buffers: 105780 kB
  194. Cached: 291456 kB
  195. SwapCached: 43864 kB
  196. Active: 422088 kB
  197. Inactive: 304300 kB
  198. SwapTotal: 2097144 kB
  199. SwapFree: 2029200 kB
  200. Dirty: 148 kB
  201. Writeback: 0 kB
  202. AnonPages: 328480 kB
  203. Mapped: 13732 kB
  204. Slab: 59796 kB
  205. SReclaimable: 9748 kB
  206. SUnreclaim: 50048 kB
  207. PageTables: 5244 kB
  208. NFS_Unstable: 0 kB
  209. Bounce: 0 kB
  210. CommitLimit: 2621540 kB
  211. Committed_AS: 1209676 kB
  212. VmallocTotal: 34359738367 kB
  213. VmallocUsed: 1220 kB
  214. VmallocChunk: 34359737147 kB
  215. :meminfo_alert: |
  216. MemTotal: 708796 kB
  217. MemFree: 2000 kB
  218. Buffers: 0 kB
  219. Cached: 0 kB
  220. SwapCached: 43864 kB
  221. Active: 422088 kB
  222. Inactive: 304300 kB
  223. SwapTotal: 0 kB
  224. SwapFree: 0 kB
  225. Dirty: 148 kB
  226. Writeback: 0 kB
  227. AnonPages: 328480 kB
  228. Mapped: 13732 kB
  229. Slab: 59796 kB
  230. SReclaimable: 9748 kB
  231. SUnreclaim: 50048 kB
  232. PageTables: 5244 kB
  233. NFS_Unstable: 0 kB
  234. Bounce: 0 kB
  235. CommitLimit: 2621540 kB
  236. Committed_AS: 1209676 kB
  237. VmallocTotal: 34359738367 kB
  238. VmallocUsed: 1220 kB
  239. VmallocChunk: 34359737147 kB
  240. :df: |
  241. Filesystem Size Used Avail Use% Mounted on
  242. /dev/sda1 38G 14G 22G 39% /
  243. varrun 513M 48K 513M 1% /var/run
  244. varlock 513M 0 513M 0% /var/lock
  245. udev 513M 16K 513M 1% /dev
  246. devshm 513M 0 513M 0% /dev/shm
  247. :df_alert: |
  248. Filesystem Size Used Avail Use% Mounted on
  249. /dev/sda1 38G 34G 4G 90% /
  250. varrun 513M 48K 513M 1% /var/run
  251. varlock 513M 0 513M 0% /var/lock
  252. udev 513M 16K 513M 1% /dev
  253. devshm 513M 0 513M 0% /dev/shm
  254. :uptime: |
  255. 15:36 up 33 days, 3:29, 4 users, load averages: 1.77 0.90 0.64
  256. :beancounters: |
  257. Version: 2.5
  258. uid resource held maxheld barrier limit failcnt
  259. 9833: kmemsize 3318833 4634057 33554432 37748736 0
  260. lockedpages 0 0 32 32 0
  261. privvmpages 12032 26321 262144 294912 0
  262. shmpages 641 1297 65536 65536 0
  263. dummy 0 0 0 0 0
  264. numproc 16 23 512 512 0
  265. physpages 4626 15124 131072 9223372036854775807 0
  266. vmguarpages 0 0 262144 9223372036854775807 0
  267. oomguarpages 4627 15125 131072 9223372036854775807 0
  268. numtcpsock 4 7 512 512 0
  269. numflock 3 5 512 512 0
  270. numpty 1 2 32 32 0
  271. numsiginfo 0 6 256 256 0
  272. tcpsndbuf 69888 1266992 5242880 7864320 0
  273. tcprcvbuf 65536 291584 5242880 7864320 0
  274. othersockbuf 9280 284768 1048576 2097152 0
  275. dgramrcvbuf 0 16928 131072 131072 0
  276. numothersock 13 23 512 512 0
  277. dcachesize 282639 307992 1048576 1179648 0
  278. numfile 618 943 5120 5120 0
  279. dummy 0 0 0 0 0
  280. dummy 0 0 0 0 0
  281. dummy 0 0 0 0 0
  282. numiptent 14 14 128 128 0
  283. :beancounters_host: |
  284. Version: 2.5
  285. uid resource held maxheld barrier limit failcnt
  286. 0: kmemsize 29696618 72254634 9223372036854775807 9223372036854775807 0
  287. lockedpages 5847 5855 9223372036854775807 9223372036854775807 0
  288. privvmpages 469383 633537 9223372036854775807 9223372036854775807 0
  289. shmpages 842 2778 9223372036854775807 9223372036854775807 0
  290. dummy 0 0 9223372036854775807 9223372036854775807 0
  291. numproc 152 290 9223372036854775807 9223372036854775807 0
  292. physpages 155069 298835 9223372036854775807 9223372036854775807 0
  293. vmguarpages 0 0 9223372036854775807 9223372036854775807 0
  294. oomguarpages 155069 298835 9223372036854775807 9223372036854775807 0
  295. numtcpsock 13 98 9223372036854775807 9223372036854775807 0
  296. numflock 4 11 9223372036854775807 9223372036854775807 0
  297. numpty 1 4 9223372036854775807 9223372036854775807 0
  298. numsiginfo 1 27 9223372036854775807 9223372036854775807 0
  299. tcpsndbuf 227552 764272 9223372036854775807 9223372036854775807 0
  300. tcprcvbuf 212992 684584 9223372036854775807 9223372036854775807 0
  301. othersockbuf 305912 555576 9223372036854775807 9223372036854775807 0
  302. dgramrcvbuf 0 18248 9223372036854775807 9223372036854775807 0
  303. numothersock 237 428 9223372036854775807 9223372036854775807 0
  304. dcachesize 0 0 9223372036854775807 9223372036854775807 0
  305. numfile 5967 8144 9223372036854775807 9223372036854775807 0
  306. dummy 0 0 9223372036854775807 9223372036854775807 0
  307. dummy 0 0 9223372036854775807 9223372036854775807 0
  308. dummy 0 0 9223372036854775807 9223372036854775807 0
  309. numiptent 73 73 9223372036854775807 9223372036854775807 0
  310. EOS
  311. end