etcd_volume_test.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import pytest
  2. from openshift_checks.etcd_volume import EtcdVolume, OpenShiftCheckException
  3. @pytest.mark.parametrize('ansible_mounts,extra_words', [
  4. ([], ['none']), # empty ansible_mounts
  5. ([{'mount': '/mnt'}], ['/mnt']), # missing relevant mount paths
  6. ])
  7. def test_cannot_determine_available_disk(ansible_mounts, extra_words):
  8. task_vars = dict(
  9. ansible_mounts=ansible_mounts,
  10. )
  11. check = EtcdVolume(execute_module=fake_execute_module)
  12. with pytest.raises(OpenShiftCheckException) as excinfo:
  13. check.run(tmp=None, task_vars=task_vars)
  14. for word in 'Unable to find etcd storage mount point'.split() + extra_words:
  15. assert word in str(excinfo.value)
  16. @pytest.mark.parametrize('size_limit,ansible_mounts', [
  17. (
  18. # if no size limit is specified, expect max usage
  19. # limit to default to 90% of size_total
  20. None,
  21. [{
  22. 'mount': '/',
  23. 'size_available': 40 * 10**9,
  24. 'size_total': 80 * 10**9
  25. }],
  26. ),
  27. (
  28. 1,
  29. [{
  30. 'mount': '/',
  31. 'size_available': 30 * 10**9,
  32. 'size_total': 30 * 10**9,
  33. }],
  34. ),
  35. (
  36. 20000000000,
  37. [{
  38. 'mount': '/',
  39. 'size_available': 20 * 10**9,
  40. 'size_total': 40 * 10**9,
  41. }],
  42. ),
  43. (
  44. 5000000000,
  45. [{
  46. # not enough space on / ...
  47. 'mount': '/',
  48. 'size_available': 0,
  49. 'size_total': 0,
  50. }, {
  51. # not enough space on /var/lib ...
  52. 'mount': '/var/lib',
  53. 'size_available': 2 * 10**9,
  54. 'size_total': 21 * 10**9,
  55. }, {
  56. # ... but enough on /var/lib/etcd
  57. 'mount': '/var/lib/etcd',
  58. 'size_available': 36 * 10**9,
  59. 'size_total': 40 * 10**9
  60. }],
  61. )
  62. ])
  63. def test_succeeds_with_recommended_disk_space(size_limit, ansible_mounts):
  64. task_vars = dict(
  65. etcd_device_usage_threshold_percent=size_limit,
  66. ansible_mounts=ansible_mounts,
  67. )
  68. if task_vars["etcd_device_usage_threshold_percent"] is None:
  69. task_vars.pop("etcd_device_usage_threshold_percent")
  70. check = EtcdVolume(execute_module=fake_execute_module)
  71. result = check.run(tmp=None, task_vars=task_vars)
  72. assert not result.get('failed', False)
  73. @pytest.mark.parametrize('size_limit_percent,ansible_mounts,extra_words', [
  74. (
  75. # if no size limit is specified, expect max usage
  76. # limit to default to 90% of size_total
  77. None,
  78. [{
  79. 'mount': '/',
  80. 'size_available': 1 * 10**9,
  81. 'size_total': 100 * 10**9,
  82. }],
  83. ['99.0%'],
  84. ),
  85. (
  86. 70.0,
  87. [{
  88. 'mount': '/',
  89. 'size_available': 1 * 10**6,
  90. 'size_total': 5 * 10**9,
  91. }],
  92. ['100.0%'],
  93. ),
  94. (
  95. 40.0,
  96. [{
  97. 'mount': '/',
  98. 'size_available': 2 * 10**9,
  99. 'size_total': 6 * 10**9,
  100. }],
  101. ['66.7%'],
  102. ),
  103. (
  104. None,
  105. [{
  106. # enough space on /var ...
  107. 'mount': '/var',
  108. 'size_available': 20 * 10**9,
  109. 'size_total': 20 * 10**9,
  110. }, {
  111. # .. but not enough on /var/lib
  112. 'mount': '/var/lib',
  113. 'size_available': 1 * 10**9,
  114. 'size_total': 20 * 10**9,
  115. }],
  116. ['95.0%'],
  117. ),
  118. ])
  119. def test_fails_with_insufficient_disk_space(size_limit_percent, ansible_mounts, extra_words):
  120. task_vars = dict(
  121. etcd_device_usage_threshold_percent=size_limit_percent,
  122. ansible_mounts=ansible_mounts,
  123. )
  124. if task_vars["etcd_device_usage_threshold_percent"] is None:
  125. task_vars.pop("etcd_device_usage_threshold_percent")
  126. check = EtcdVolume(execute_module=fake_execute_module)
  127. result = check.run(tmp=None, task_vars=task_vars)
  128. assert result['failed']
  129. for word in extra_words:
  130. assert word in result['msg']
  131. def fake_execute_module(*args):
  132. raise AssertionError('this function should not be called')