kibana_test.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import pytest
  2. import json
  3. try:
  4. import urllib2
  5. from urllib2 import HTTPError, URLError
  6. except ImportError:
  7. from urllib.error import HTTPError, URLError
  8. import urllib.request as urllib2
  9. from openshift_checks.logging.kibana import Kibana
  10. def assert_error(error, expect_error):
  11. if expect_error:
  12. assert error
  13. assert expect_error in error
  14. else:
  15. assert not error
  16. plain_kibana_pod = {
  17. "metadata": {
  18. "labels": {"component": "kibana", "deploymentconfig": "logging-kibana"},
  19. "name": "logging-kibana-1",
  20. },
  21. "status": {
  22. "containerStatuses": [{"ready": True}, {"ready": True}],
  23. "conditions": [{"status": "True", "type": "Ready"}],
  24. }
  25. }
  26. not_running_kibana_pod = {
  27. "metadata": {
  28. "labels": {"component": "kibana", "deploymentconfig": "logging-kibana"},
  29. "name": "logging-kibana-2",
  30. },
  31. "status": {
  32. "containerStatuses": [{"ready": True}, {"ready": False}],
  33. "conditions": [{"status": "True", "type": "Ready"}],
  34. }
  35. }
  36. @pytest.mark.parametrize('pods, expect_error', [
  37. (
  38. [],
  39. "There are no Kibana pods deployed",
  40. ),
  41. (
  42. [plain_kibana_pod],
  43. None,
  44. ),
  45. (
  46. [not_running_kibana_pod],
  47. "No Kibana pod is in a running state",
  48. ),
  49. (
  50. [plain_kibana_pod, not_running_kibana_pod],
  51. "The following Kibana pods are not currently in a running state",
  52. ),
  53. ])
  54. def test_check_kibana(pods, expect_error):
  55. check = Kibana()
  56. error = check.check_kibana(pods)
  57. assert_error(error, expect_error)
  58. @pytest.mark.parametrize('route, expect_url, expect_error', [
  59. (
  60. None,
  61. None,
  62. 'no_route_exists',
  63. ),
  64. # test route with no ingress
  65. (
  66. {
  67. "metadata": {
  68. "labels": {"component": "kibana", "deploymentconfig": "logging-kibana"},
  69. "name": "logging-kibana",
  70. },
  71. "status": {
  72. "ingress": [],
  73. },
  74. "spec": {
  75. "host": "hostname",
  76. }
  77. },
  78. None,
  79. 'route_not_accepted',
  80. ),
  81. # test route with no host
  82. (
  83. {
  84. "metadata": {
  85. "labels": {"component": "kibana", "deploymentconfig": "logging-kibana"},
  86. "name": "logging-kibana",
  87. },
  88. "status": {
  89. "ingress": [{
  90. "status": True,
  91. }],
  92. },
  93. "spec": {},
  94. },
  95. None,
  96. 'route_missing_host',
  97. ),
  98. # test route that looks fine
  99. (
  100. {
  101. "metadata": {
  102. "labels": {"component": "kibana", "deploymentconfig": "logging-kibana"},
  103. "name": "logging-kibana",
  104. },
  105. "status": {
  106. "ingress": [{
  107. "status": True,
  108. }],
  109. },
  110. "spec": {
  111. "host": "hostname",
  112. },
  113. },
  114. "https://hostname/",
  115. None,
  116. ),
  117. ])
  118. def test_get_kibana_url(route, expect_url, expect_error):
  119. check = Kibana()
  120. check.exec_oc = lambda ns, cmd, args: json.dumps(route) if route else ""
  121. url, error = check._get_kibana_url()
  122. if expect_url:
  123. assert url == expect_url
  124. else:
  125. assert not url
  126. if expect_error:
  127. assert error == expect_error
  128. else:
  129. assert not error
  130. @pytest.mark.parametrize('exec_result, expect', [
  131. (
  132. 'urlopen error [Errno 111] Connection refused',
  133. 'at least one router routing to it?',
  134. ),
  135. (
  136. 'urlopen error [Errno -2] Name or service not known',
  137. 'DNS configured for the Kibana hostname?',
  138. ),
  139. (
  140. 'Status code was not [302]: HTTP Error 500: Server error',
  141. 'did not return the correct status code',
  142. ),
  143. (
  144. 'bork bork bork',
  145. 'bork bork bork', # should pass through
  146. ),
  147. ])
  148. def test_verify_url_internal_failure(exec_result, expect):
  149. check = Kibana(execute_module=lambda *_: dict(failed=True, msg=exec_result))
  150. check._get_kibana_url = lambda: ('url', None)
  151. error = check._check_kibana_route()
  152. assert_error(error, expect)
  153. @pytest.mark.parametrize('lib_result, expect', [
  154. (
  155. HTTPError('url', 500, "it broke", hdrs=None, fp=None),
  156. 'it broke',
  157. ),
  158. (
  159. URLError('it broke'),
  160. 'it broke',
  161. ),
  162. (
  163. 302,
  164. 'returned the wrong error code',
  165. ),
  166. (
  167. 200,
  168. None,
  169. ),
  170. ])
  171. def test_verify_url_external_failure(lib_result, expect, monkeypatch):
  172. class _http_return:
  173. def __init__(self, code):
  174. self.code = code
  175. def getcode(self):
  176. return self.code
  177. def urlopen(url, context):
  178. if type(lib_result) is int:
  179. return _http_return(lib_result)
  180. raise lib_result
  181. monkeypatch.setattr(urllib2, 'urlopen', urlopen)
  182. check = Kibana()
  183. check._get_kibana_url = lambda: ('url', None)
  184. check._verify_url_internal = lambda url: None
  185. error = check._check_kibana_route()
  186. assert_error(error, expect)