sdn_tests.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import pytest
  2. from openshift_checks.sdn import SDNCheck
  3. def fake_execute_module(*args):
  4. raise AssertionError('this function should not be called')
  5. def test_check_nodes_missing_node_name():
  6. task_vars = dict(
  7. group_names=['oo_nodes_to_config'],
  8. )
  9. check = SDNCheck(fake_execute_module, task_vars)
  10. check.run()
  11. assert 1 == len(check.failures)
  12. assert 'Could not determine node name' in str(check.failures[0])
  13. def test_check_master():
  14. nodes = [
  15. {
  16. 'apiVersion': 'v1',
  17. 'kind': 'Node',
  18. 'metadata': {
  19. 'annotations': {'kubernetes.io/hostname': 'node1'},
  20. 'name': 'ip-172-31-50-1.ec2.internal'
  21. },
  22. 'status': {
  23. 'addresses': [
  24. {'address': '172.31.50.1', 'type': 'InternalIP'},
  25. {'address': '52.0.0.1', 'type': 'ExternalIP'},
  26. {
  27. 'address': 'ip-172-31-50-1.ec2.internal',
  28. 'type': 'Hostname'
  29. }
  30. ]
  31. }
  32. },
  33. {
  34. 'apiVersion': 'v1',
  35. 'kind': 'Node',
  36. 'metadata': {'name': 'ip-172-31-50-2.ec2.internal'},
  37. 'status': {
  38. 'addresses': [
  39. {'address': '172.31.50.2', 'type': 'InternalIP'},
  40. {'address': '52.0.0.2', 'type': 'ExternalIP'},
  41. {
  42. 'address': 'ip-172-31-50-2.ec2.internal',
  43. 'type': 'Hostname'
  44. }
  45. ]
  46. }
  47. }
  48. ]
  49. task_vars = dict(
  50. group_names=['oo_masters_to_config'],
  51. resources=dict(results=[
  52. dict(item='nodes', results=dict(results=[dict(items=nodes)])),
  53. dict(item='pods', results=dict(results=[dict(items={})])),
  54. dict(item='services', results=dict(results=[dict(items={})]))
  55. ])
  56. )
  57. node_addresses = {
  58. node['metadata']['name']: {
  59. address['type']: address['address']
  60. for address
  61. in node['status']['addresses']
  62. }
  63. for node in nodes
  64. }
  65. expected_hostnames = [addresses['Hostname']
  66. for addresses in node_addresses.values()]
  67. uri_hostnames = []
  68. resolve_address_hostnames = []
  69. def execute_module(module_name, args, *_):
  70. if module_name == 'uri':
  71. for hostname in expected_hostnames:
  72. if hostname in args['url']:
  73. uri_hostnames.append(hostname)
  74. return {}
  75. raise ValueError('unexpected url: %s' % args['url'])
  76. raise ValueError('not expecting module %s' % module_name)
  77. def resolve_address(address):
  78. for hostname in expected_hostnames:
  79. if address == hostname:
  80. resolve_address_hostnames.append(hostname)
  81. return node_addresses[hostname]['InternalIP']
  82. raise ValueError('unexpected address: %s' % hostname)
  83. check = SDNCheck(execute_module, task_vars)
  84. check.resolve_address = resolve_address
  85. check.run()
  86. assert 0 == len(check.failures)
  87. assert set(expected_hostnames) == set(uri_hostnames), 'should try to connect to the kubelet'
  88. assert set(expected_hostnames) == set(resolve_address_hostnames), 'should try to resolve the node\'s address'
  89. def test_check_nodes():
  90. nodes = [
  91. {
  92. 'apiVersion': 'v1',
  93. 'kind': 'Node',
  94. 'metadata': {
  95. 'annotations': {'kubernetes.io/hostname': 'node1'},
  96. 'name': 'ip-172-31-50-1.ec2.internal'
  97. },
  98. 'status': {
  99. 'addresses': [
  100. {'address': '172.31.50.1', 'type': 'InternalIP'},
  101. {'address': '52.0.0.1', 'type': 'ExternalIP'},
  102. {
  103. 'address': 'ip-172-31-50-1.ec2.internal',
  104. 'type': 'Hostname'
  105. }
  106. ]
  107. }
  108. },
  109. {
  110. 'apiVersion': 'v1',
  111. 'kind': 'Node',
  112. 'metadata': {'name': 'ip-172-31-50-2.ec2.internal'},
  113. 'status': {
  114. 'addresses': [
  115. {'address': '172.31.50.2', 'type': 'InternalIP'},
  116. {'address': '52.0.0.2', 'type': 'ExternalIP'},
  117. {
  118. 'address': 'ip-172-31-50-2.ec2.internal',
  119. 'type': 'Hostname'
  120. }
  121. ]
  122. }
  123. }
  124. ]
  125. hostsubnets = [
  126. {
  127. 'metadata': {
  128. 'name': 'ip-172-31-50-1.ec2.internal'
  129. },
  130. 'subnet': '10.128.0.1/23'
  131. },
  132. {
  133. 'metadata': {
  134. 'name': 'ip-172-31-50-2.ec2.internal'
  135. },
  136. 'subnet': '10.129.0.1/23'
  137. }
  138. ]
  139. task_vars = dict(
  140. group_names=['oo_nodes_to_config'],
  141. resources=dict(results=[
  142. dict(item='nodes', results=dict(results=[dict(items=nodes)])),
  143. dict(item='hostsubnets', results=dict(results=[dict(items=hostsubnets)]))
  144. ]),
  145. openshift=dict(node=dict(nodename='foo'))
  146. )
  147. def execute_module(module_name, args, *_):
  148. if module_name == 'command':
  149. return dict(stdout='bogus_container_id')
  150. raise ValueError('not expecting module %s' % module_name)
  151. SDNCheck(execute_module, task_vars).run()
  152. def test_no_nodes():
  153. task_vars = dict(
  154. group_names=['oo_masters_to_config'],
  155. resources=dict(results=[
  156. dict(item='nodes', results=dict(results=[dict(items={})])),
  157. dict(item='pods', results=dict(results=[dict(items={})])),
  158. dict(item='services', results=dict(results=[dict(items={})]))
  159. ])
  160. )
  161. check = SDNCheck(fake_execute_module, task_vars)
  162. check.run()
  163. assert 1 == len(check.failures)
  164. assert 'No nodes' in str(check.failures[0])
  165. @pytest.mark.parametrize('group_names,expected', [
  166. (['oo_masters_to_config'], True),
  167. (['oo_nodes_to_config'], True),
  168. (['oo_masters_to_config', 'oo_nodes_to_config'], True),
  169. (['oo_masters_to_config', 'oo_etcd_to_config'], True),
  170. ([], False),
  171. (['oo_etcd_to_config'], False),
  172. (['lb'], False),
  173. (['nfs'], False),
  174. ])
  175. def test_sdn_skip_when_not_master_nor_node(group_names, expected):
  176. task_vars = dict(
  177. group_names=group_names,
  178. openshift_is_atomic=True,
  179. )
  180. assert SDNCheck(None, task_vars).is_active() == expected