disk_availability_test.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import pytest
  2. from openshift_checks.disk_availability import DiskAvailability, OpenShiftCheckException
  3. @pytest.mark.parametrize('group_names,is_active', [
  4. (['masters'], True),
  5. (['nodes'], True),
  6. (['etcd'], True),
  7. (['masters', 'nodes'], True),
  8. (['masters', 'etcd'], True),
  9. ([], False),
  10. (['lb'], False),
  11. (['nfs'], False),
  12. ])
  13. def test_is_active(group_names, is_active):
  14. task_vars = dict(
  15. group_names=group_names,
  16. )
  17. assert DiskAvailability(None, task_vars).is_active() == is_active
  18. @pytest.mark.parametrize('ansible_mounts,extra_words', [
  19. ([], ['none']), # empty ansible_mounts
  20. ([{'mount': '/mnt'}], ['/mnt']), # missing relevant mount paths
  21. ([{'mount': '/var'}], ['/var']), # missing size_available
  22. ])
  23. def test_cannot_determine_available_disk(ansible_mounts, extra_words):
  24. task_vars = dict(
  25. group_names=['masters'],
  26. ansible_mounts=ansible_mounts,
  27. )
  28. with pytest.raises(OpenShiftCheckException) as excinfo:
  29. DiskAvailability(fake_execute_module, task_vars).run()
  30. for word in 'determine disk availability'.split() + extra_words:
  31. assert word in str(excinfo.value)
  32. @pytest.mark.parametrize('group_names,configured_min,ansible_mounts', [
  33. (
  34. ['masters'],
  35. 0,
  36. [{
  37. 'mount': '/',
  38. 'size_available': 40 * 10**9 + 1,
  39. }],
  40. ),
  41. (
  42. ['nodes'],
  43. 0,
  44. [{
  45. 'mount': '/',
  46. 'size_available': 15 * 10**9 + 1,
  47. }],
  48. ),
  49. (
  50. ['etcd'],
  51. 0,
  52. [{
  53. 'mount': '/',
  54. 'size_available': 20 * 10**9 + 1,
  55. }],
  56. ),
  57. (
  58. ['etcd'],
  59. 1, # configure lower threshold
  60. [{
  61. 'mount': '/',
  62. 'size_available': 1 * 10**9 + 1, # way smaller than recommended
  63. }],
  64. ),
  65. (
  66. ['etcd'],
  67. 0,
  68. [{
  69. # not enough space on / ...
  70. 'mount': '/',
  71. 'size_available': 2 * 10**9,
  72. }, {
  73. # ... but enough on /var
  74. 'mount': '/var',
  75. 'size_available': 20 * 10**9 + 1,
  76. }],
  77. ),
  78. ])
  79. def test_succeeds_with_recommended_disk_space(group_names, configured_min, ansible_mounts):
  80. task_vars = dict(
  81. group_names=group_names,
  82. openshift_check_min_host_disk_gb=configured_min,
  83. ansible_mounts=ansible_mounts,
  84. )
  85. result = DiskAvailability(fake_execute_module, task_vars).run()
  86. assert not result.get('failed', False)
  87. @pytest.mark.parametrize('name,group_names,configured_min,ansible_mounts,extra_words', [
  88. (
  89. 'test with no space available',
  90. ['masters'],
  91. 0,
  92. [{
  93. 'mount': '/',
  94. 'size_available': 1,
  95. }],
  96. ['0.0 GB'],
  97. ),
  98. (
  99. 'test with a higher configured required value',
  100. ['masters'],
  101. 100, # set a higher threshold
  102. [{
  103. 'mount': '/',
  104. 'size_available': 50 * 10**9, # would normally be enough...
  105. }],
  106. ['100.0 GB'],
  107. ),
  108. (
  109. 'test with 1GB available, but "0" GB space requirement',
  110. ['nodes'],
  111. 0,
  112. [{
  113. 'mount': '/',
  114. 'size_available': 1 * 10**9,
  115. }],
  116. ['1.0 GB'],
  117. ),
  118. (
  119. 'test with no space available, but "0" GB space requirement',
  120. ['etcd'],
  121. 0,
  122. [{
  123. 'mount': '/',
  124. 'size_available': 1,
  125. }],
  126. ['0.0 GB'],
  127. ),
  128. (
  129. 'test with enough space for a node, but not for a master',
  130. ['nodes', 'masters'],
  131. 0,
  132. [{
  133. 'mount': '/',
  134. 'size_available': 15 * 10**9 + 1,
  135. }],
  136. ['15.0 GB'],
  137. ),
  138. (
  139. 'test failure with enough space on "/", but not enough on "/var"',
  140. ['etcd'],
  141. 0,
  142. [{
  143. # enough space on / ...
  144. 'mount': '/',
  145. 'size_available': 20 * 10**9 + 1,
  146. }, {
  147. # .. but not enough on /var
  148. 'mount': '/var',
  149. 'size_available': 0,
  150. }],
  151. ['0.0 GB'],
  152. ),
  153. ], ids=lambda argval: argval[0])
  154. def test_fails_with_insufficient_disk_space(name, group_names, configured_min, ansible_mounts, extra_words):
  155. task_vars = dict(
  156. group_names=group_names,
  157. openshift_check_min_host_disk_gb=configured_min,
  158. ansible_mounts=ansible_mounts,
  159. )
  160. result = DiskAvailability(fake_execute_module, task_vars).run()
  161. assert result['failed']
  162. for word in 'below recommended'.split() + extra_words:
  163. assert word in result.get('msg', '')
  164. @pytest.mark.parametrize('name,group_names,context,ansible_mounts,failed,extra_words', [
  165. (
  166. 'test without enough space for master under "upgrade" context',
  167. ['nodes', 'masters'],
  168. "upgrade",
  169. [{
  170. 'mount': '/',
  171. 'size_available': 1 * 10**9 + 1,
  172. 'size_total': 21 * 10**9 + 1,
  173. }],
  174. True,
  175. ["1.0 GB"],
  176. ),
  177. (
  178. 'test with enough space for master under "upgrade" context',
  179. ['nodes', 'masters'],
  180. "upgrade",
  181. [{
  182. 'mount': '/',
  183. 'size_available': 10 * 10**9 + 1,
  184. 'size_total': 21 * 10**9 + 1,
  185. }],
  186. False,
  187. [],
  188. ),
  189. (
  190. 'test with not enough space for master, and non-upgrade context',
  191. ['nodes', 'masters'],
  192. "health",
  193. [{
  194. 'mount': '/',
  195. # not enough space for a master,
  196. # "health" context should not lower requirement
  197. 'size_available': 20 * 10**9 + 1,
  198. }],
  199. True,
  200. ["20.0 GB", "below minimum"],
  201. ),
  202. ], ids=lambda argval: argval[0])
  203. def test_min_required_space_changes_with_upgrade_context(name, group_names, context, ansible_mounts, failed, extra_words):
  204. task_vars = dict(
  205. r_openshift_health_checker_playbook_context=context,
  206. group_names=group_names,
  207. ansible_mounts=ansible_mounts,
  208. )
  209. check = DiskAvailability(fake_execute_module, task_vars)
  210. result = check.run()
  211. assert result.get("failed", False) == failed
  212. for word in extra_words:
  213. assert word in result.get('msg', '')
  214. def fake_execute_module(*args):
  215. raise AssertionError('this function should not be called')