logging_index_time_test.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import json
  2. import pytest
  3. from openshift_checks.logging.logging_index_time import LoggingIndexTime, OpenShiftCheckException
  4. SAMPLE_UUID = "unique-test-uuid"
  5. def canned_loggingindextime(exec_oc=None):
  6. """Create a check object with a canned exec_oc method"""
  7. check = LoggingIndexTime() # fails if a module is actually invoked
  8. if exec_oc:
  9. check.exec_oc = exec_oc
  10. return check
  11. plain_running_elasticsearch_pod = {
  12. "metadata": {
  13. "labels": {"component": "es", "deploymentconfig": "logging-es-data-master"},
  14. "name": "logging-es-data-master-1",
  15. },
  16. "status": {
  17. "containerStatuses": [{"ready": True}, {"ready": True}],
  18. "phase": "Running",
  19. }
  20. }
  21. plain_running_kibana_pod = {
  22. "metadata": {
  23. "labels": {"component": "kibana", "deploymentconfig": "logging-kibana"},
  24. "name": "logging-kibana-1",
  25. },
  26. "status": {
  27. "containerStatuses": [{"ready": True}, {"ready": True}],
  28. "phase": "Running",
  29. }
  30. }
  31. not_running_kibana_pod = {
  32. "metadata": {
  33. "labels": {"component": "kibana", "deploymentconfig": "logging-kibana"},
  34. "name": "logging-kibana-2",
  35. },
  36. "status": {
  37. "containerStatuses": [{"ready": True}, {"ready": False}],
  38. "conditions": [{"status": "True", "type": "Ready"}],
  39. "phase": "pending",
  40. }
  41. }
  42. @pytest.mark.parametrize('pods, expect_pods', [
  43. (
  44. [not_running_kibana_pod],
  45. [],
  46. ),
  47. (
  48. [plain_running_kibana_pod],
  49. [plain_running_kibana_pod],
  50. ),
  51. (
  52. [],
  53. [],
  54. )
  55. ])
  56. def test_check_running_pods(pods, expect_pods):
  57. check = canned_loggingindextime()
  58. pods = check.running_pods(pods)
  59. assert pods == expect_pods
  60. @pytest.mark.parametrize('name, json_response, uuid, timeout, extra_words', [
  61. (
  62. 'valid count in response',
  63. {
  64. "count": 1,
  65. },
  66. SAMPLE_UUID,
  67. 0.001,
  68. [],
  69. ),
  70. ], ids=lambda argval: argval[0])
  71. def test_wait_until_cmd_or_err_succeeds(name, json_response, uuid, timeout, extra_words):
  72. check = canned_loggingindextime(lambda *_: json.dumps(json_response))
  73. check.wait_until_cmd_or_err(plain_running_elasticsearch_pod, uuid, timeout)
  74. @pytest.mark.parametrize('name, json_response, uuid, timeout, extra_words', [
  75. (
  76. 'invalid json response',
  77. {
  78. "invalid_field": 1,
  79. },
  80. SAMPLE_UUID,
  81. 0.001,
  82. ["invalid response", "Elasticsearch"],
  83. ),
  84. (
  85. 'empty response',
  86. {},
  87. SAMPLE_UUID,
  88. 0.001,
  89. ["invalid response", "Elasticsearch"],
  90. ),
  91. (
  92. 'valid response but invalid match count',
  93. {
  94. "count": 0,
  95. },
  96. SAMPLE_UUID,
  97. 0.005,
  98. ["expecting match", SAMPLE_UUID, "0.005s"],
  99. )
  100. ], ids=lambda argval: argval[0])
  101. def test_wait_until_cmd_or_err(name, json_response, uuid, timeout, extra_words):
  102. check = canned_loggingindextime(lambda *_: json.dumps(json_response))
  103. with pytest.raises(OpenShiftCheckException) as error:
  104. check.wait_until_cmd_or_err(plain_running_elasticsearch_pod, uuid, timeout)
  105. for word in extra_words:
  106. assert word in str(error)
  107. @pytest.mark.parametrize('name, json_response, uuid, extra_words', [
  108. (
  109. 'correct response code, found unique id is returned',
  110. {
  111. "statusCode": 404,
  112. },
  113. "sample unique id",
  114. ["sample unique id"],
  115. ),
  116. ], ids=lambda argval: argval[0])
  117. def test_curl_kibana_with_uuid(name, json_response, uuid, extra_words):
  118. check = canned_loggingindextime(lambda *_: json.dumps(json_response))
  119. check.generate_uuid = lambda: uuid
  120. result = check.curl_kibana_with_uuid(plain_running_kibana_pod)
  121. for word in extra_words:
  122. assert word in result
  123. @pytest.mark.parametrize('name, json_response, uuid, extra_words', [
  124. (
  125. 'invalid json response',
  126. {
  127. "invalid_field": "invalid",
  128. },
  129. SAMPLE_UUID,
  130. ["invalid response returned", 'Missing "statusCode" key'],
  131. ),
  132. (
  133. 'wrong error code in response',
  134. {
  135. "statusCode": 500,
  136. },
  137. SAMPLE_UUID,
  138. ["Expecting error code", "500"],
  139. ),
  140. ], ids=lambda argval: argval[0])
  141. def test_failed_curl_kibana_with_uuid(name, json_response, uuid, extra_words):
  142. check = canned_loggingindextime(lambda *_: json.dumps(json_response))
  143. check.generate_uuid = lambda: uuid
  144. with pytest.raises(OpenShiftCheckException) as error:
  145. check.curl_kibana_with_uuid(plain_running_kibana_pod)
  146. for word in extra_words:
  147. assert word in str(error)