You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
3.5 KiB

  1. # Copyright 2018 Creu Blanca
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. import mock
  4. from odoo.tests.common import TransactionCase
  5. class Popen:
  6. def __init__(self, cmd, stdout, stdin, stderr):
  7. self.cmd = cmd
  8. self.stdout = stdout
  9. self.stdin = stdin
  10. self.stderr = stderr
  11. def communicate(input):
  12. return ['test']
  13. class TestNsca(TransactionCase):
  14. def test_nsca(self):
  15. server = self.env['nsca.server'].create({
  16. 'name': 'localhost',
  17. 'password': 'pass',
  18. 'encryption_method': '3',
  19. 'node_hostname': 'odoodev',
  20. })
  21. self.assertTrue(server.config_file_path)
  22. with mock.patch('subprocess.Popen') as post:
  23. post.return_value = Popen
  24. check = self.env['nsca.check'].create({
  25. 'server_id': server.id,
  26. 'service': 'test',
  27. 'nsca_model': 'nsca.server',
  28. 'nsca_function': 'current_status'
  29. })
  30. self.assertTrue(check.model_id)
  31. self.env['nsca.check']._cron_check(check.id,)
  32. def test_write(self):
  33. server = self.env['nsca.server'].create({
  34. 'name': 'localhost',
  35. 'password': 'pass',
  36. 'encryption_method': '3',
  37. 'node_hostname': 'odoodev',
  38. })
  39. self.assertTrue(server.config_file_path)
  40. check = self.env['nsca.check'].create({
  41. 'server_id': server.id,
  42. 'service': 'test',
  43. 'nsca_model': 'nsca.server',
  44. 'nsca_function': 'current_status'
  45. })
  46. check.cron_id.state = 'object_create'
  47. check.write({'interval_number': 1})
  48. self.assertEqual(check.cron_id.state, 'object_create')
  49. check.write({'service': 'change'})
  50. self.assertNotEqual(check.cron_id.state, 'object_create')
  51. def test_void_failure(self):
  52. server = self.env['nsca.server'].create({
  53. 'name': 'localhost',
  54. 'password': 'pass',
  55. 'encryption_method': '3',
  56. 'node_hostname': 'odoodev',
  57. })
  58. check = self.env['nsca.check'].create({
  59. 'server_id': server.id,
  60. 'service': 'test',
  61. 'nsca_model': 'nsca.server',
  62. 'allow_void_result': False,
  63. 'nsca_function': '_check_send_nsca_command'
  64. })
  65. with mock.patch('subprocess.Popen') as post:
  66. post.return_value = Popen
  67. self.env['nsca.check']._cron_check(check.id,)
  68. post.assert_called_once()
  69. def test_void_ok(self):
  70. server = self.env['nsca.server'].create({
  71. 'name': 'localhost',
  72. 'password': 'pass',
  73. 'encryption_method': '3',
  74. 'node_hostname': 'odoodev',
  75. })
  76. self.assertEqual(server.check_count, 0)
  77. check = self.env['nsca.check'].create({
  78. 'server_id': server.id,
  79. 'service': 'test',
  80. 'nsca_model': 'nsca.server',
  81. 'allow_void_result': True,
  82. 'nsca_function': '_check_send_nsca_command'
  83. })
  84. self.assertEqual(server.check_count, 1)
  85. action = server.show_checks()
  86. self.assertEqual(check, self.env['nsca.check'].browse(
  87. action['res_id']))
  88. self.assertEqual(check, self.env['nsca.check'].search(
  89. action['domain']))
  90. with mock.patch('subprocess.Popen') as post:
  91. post.return_value = Popen
  92. self.env['nsca.check']._cron_check(check.id,)
  93. post.assert_not_called()