fluentd_test.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import pytest
  2. import json
  3. from openshift_checks.logging.fluentd import Fluentd
  4. def canned_fluentd(exec_oc=None):
  5. """Create a Fluentd check object with canned exec_oc method"""
  6. check = Fluentd("dummy") # fails if a module is actually invoked
  7. if exec_oc:
  8. check._exec_oc = exec_oc
  9. return check
  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. fluentd_pod_node1 = {
  17. "metadata": {
  18. "labels": {"component": "fluentd", "deploymentconfig": "logging-fluentd"},
  19. "name": "logging-fluentd-1",
  20. },
  21. "spec": {"host": "node1", "nodeName": "node1"},
  22. "status": {
  23. "containerStatuses": [{"ready": True}],
  24. "conditions": [{"status": "True", "type": "Ready"}],
  25. }
  26. }
  27. fluentd_pod_node2_down = {
  28. "metadata": {
  29. "labels": {"component": "fluentd", "deploymentconfig": "logging-fluentd"},
  30. "name": "logging-fluentd-2",
  31. },
  32. "spec": {"host": "node2", "nodeName": "node2"},
  33. "status": {
  34. "containerStatuses": [{"ready": False}],
  35. "conditions": [{"status": "False", "type": "Ready"}],
  36. }
  37. }
  38. fluentd_node1 = {
  39. "metadata": {
  40. "labels": {"logging-infra-fluentd": "true", "kubernetes.io/hostname": "node1"},
  41. "name": "node1",
  42. },
  43. "status": {"addresses": [{"type": "InternalIP", "address": "10.10.1.1"}]},
  44. }
  45. fluentd_node2 = {
  46. "metadata": {
  47. "labels": {"logging-infra-fluentd": "true", "kubernetes.io/hostname": "hostname"},
  48. "name": "node2",
  49. },
  50. "status": {"addresses": [{"type": "InternalIP", "address": "10.10.1.2"}]},
  51. }
  52. fluentd_node3_unlabeled = {
  53. "metadata": {
  54. "labels": {"kubernetes.io/hostname": "hostname"},
  55. "name": "node3",
  56. },
  57. "status": {"addresses": [{"type": "InternalIP", "address": "10.10.1.3"}]},
  58. }
  59. @pytest.mark.parametrize('pods, nodes, expect_error', [
  60. (
  61. [],
  62. [],
  63. 'No nodes appear to be defined',
  64. ),
  65. (
  66. [],
  67. [fluentd_node3_unlabeled],
  68. 'There are no nodes with the fluentd label',
  69. ),
  70. (
  71. [],
  72. [fluentd_node1, fluentd_node3_unlabeled],
  73. 'Fluentd will not aggregate logs from these nodes.',
  74. ),
  75. (
  76. [],
  77. [fluentd_node2],
  78. "nodes are supposed to have a Fluentd pod but do not",
  79. ),
  80. (
  81. [fluentd_pod_node1, fluentd_pod_node1],
  82. [fluentd_node1],
  83. 'more Fluentd pods running than nodes labeled',
  84. ),
  85. (
  86. [fluentd_pod_node2_down],
  87. [fluentd_node2],
  88. "Fluentd pods are supposed to be running",
  89. ),
  90. (
  91. [fluentd_pod_node1],
  92. [fluentd_node1],
  93. None,
  94. ),
  95. ])
  96. def test_get_fluentd_pods(pods, nodes, expect_error):
  97. check = canned_fluentd(lambda cmd, args, task_vars: json.dumps(dict(items=nodes)))
  98. error = check.check_fluentd(pods, {})
  99. assert_error(error, expect_error)