test_oc_csr_approve.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import os
  2. import sys
  3. import pytest
  4. from ansible.module_utils.basic import AnsibleModule
  5. try:
  6. # python3, mock is built in.
  7. from unittest.mock import patch
  8. except ImportError:
  9. # In python2, mock is installed via pip.
  10. from mock import patch
  11. MODULE_PATH = os.path.realpath(os.path.join(__file__, os.pardir, os.pardir, 'library'))
  12. sys.path.insert(1, MODULE_PATH)
  13. import oc_csr_approve # noqa
  14. # base path for text files with sample outputs.
  15. ASSET_PATH = os.path.realpath(os.path.join(__file__, os.pardir, 'test_data'))
  16. RUN_CMD_MOCK = 'ansible.module_utils.basic.AnsibleModule.run_command'
  17. class DummyModule(AnsibleModule):
  18. def _load_params(self):
  19. self.params = {}
  20. def exit_json(*args, **kwargs):
  21. return 0
  22. def fail_json(*args, **kwargs):
  23. raise Exception(kwargs['msg'])
  24. def test_parse_subject_cn():
  25. subject = 'subject=/C=US/CN=fedora1.openshift.io/L=Raleigh/O=Red Hat/ST=North Carolina/OU=OpenShift\n'
  26. assert oc_csr_approve.parse_subject_cn(subject) == 'fedora1.openshift.io'
  27. subject = 'subject=C = US, CN = test.io, L = City, O = Company, ST = State, OU = Dept\n'
  28. assert oc_csr_approve.parse_subject_cn(subject) == 'test.io'
  29. def test_get_ready_nodes():
  30. output_file = os.path.join(ASSET_PATH, 'oc_get_nodes.json')
  31. with open(output_file) as stdoutfile:
  32. oc_get_nodes_stdout = stdoutfile.read()
  33. module = DummyModule({})
  34. with patch(RUN_CMD_MOCK) as call_mock:
  35. call_mock.return_value = (0, oc_get_nodes_stdout, '')
  36. ready_nodes = oc_csr_approve.get_ready_nodes(module, 'oc', '/dev/null')
  37. print(ready_nodes)
  38. assert ready_nodes == ['fedora1.openshift.io', 'fedora3.openshift.io']
  39. def test_get_csrs():
  40. module = DummyModule({})
  41. output_file = os.path.join(ASSET_PATH, 'oc_csr_approve_pending.json')
  42. with open(output_file) as stdoutfile:
  43. oc_get_csr_out = stdoutfile.read()
  44. # mock oc get csr call to cluster
  45. with patch(RUN_CMD_MOCK) as call_mock:
  46. call_mock.return_value = (0, oc_get_csr_out, '')
  47. csrs = oc_csr_approve.get_csrs(module, 'oc', '/dev/null')
  48. assert csrs[0]['kind'] == "CertificateSigningRequest"
  49. output_file = os.path.join(ASSET_PATH, 'openssl1.txt')
  50. with open(output_file) as stdoutfile:
  51. openssl_out = stdoutfile.read()
  52. # mock openssl req call.
  53. node_list = ['fedora2.mguginolocal.com']
  54. with patch(RUN_CMD_MOCK) as call_mock:
  55. call_mock.return_value = (0, openssl_out, '')
  56. csr_dict = oc_csr_approve.process_csrs(module, csrs, node_list, "client")
  57. # actually run openssl req call.
  58. csr_dict = oc_csr_approve.process_csrs(module, csrs, node_list, "client")
  59. assert csr_dict['node-csr-TkefytQp8Dz4Xp7uzcw605MocvI0gWuEOGNrHhOjGNQ'] == 'fedora2.mguginolocal.com'
  60. def test_confirm_needed_requests_present():
  61. module = DummyModule({})
  62. csr_dict = {'some-csr': 'fedora1.openshift.io'}
  63. not_ready_nodes = ['host1']
  64. with pytest.raises(Exception) as err:
  65. oc_csr_approve.confirm_needed_requests_present(
  66. module, not_ready_nodes, csr_dict)
  67. assert 'Exception: Cound not find csr for nodes: host1' in str(err)
  68. not_ready_nodes = ['fedora1.openshift.io']
  69. # this should complete silently
  70. oc_csr_approve.confirm_needed_requests_present(
  71. module, not_ready_nodes, csr_dict)
  72. def test_approve_csrs():
  73. module = DummyModule({})
  74. oc_bin = 'oc'
  75. oc_conf = '/dev/null'
  76. csr_dict = {'csr-1': 'example.openshift.io'}
  77. with patch(RUN_CMD_MOCK) as call_mock:
  78. call_mock.return_value = (0, 'csr-1 ok', '')
  79. client_approve_results = oc_csr_approve.approve_csrs(
  80. module, oc_bin, oc_conf, csr_dict, 'client')
  81. assert client_approve_results == ['csr-1 ok']
  82. def test_get_ready_nodes_server():
  83. module = DummyModule({})
  84. oc_bin = 'oc'
  85. oc_conf = '/dev/null'
  86. nodes_list = ['fedora1.openshift.io']
  87. with patch(RUN_CMD_MOCK) as call_mock:
  88. call_mock.return_value = (0, 'ok', '')
  89. ready_nodes_server = oc_csr_approve.get_ready_nodes_server(
  90. module, oc_bin, oc_conf, nodes_list)
  91. assert ready_nodes_server == ['fedora1.openshift.io']
  92. def test_get_csrs_server():
  93. module = DummyModule({})
  94. output_file = os.path.join(ASSET_PATH, 'oc_csr_server_multiple_pends_one_host.json')
  95. with open(output_file) as stdoutfile:
  96. oc_get_csr_out = stdoutfile.read()
  97. # mock oc get csr call to cluster
  98. with patch(RUN_CMD_MOCK) as call_mock:
  99. call_mock.return_value = (0, oc_get_csr_out, '')
  100. csrs = oc_csr_approve.get_csrs(module, 'oc', '/dev/null')
  101. assert csrs[0]['kind'] == "CertificateSigningRequest"
  102. output_file = os.path.join(ASSET_PATH, 'openssl1.txt')
  103. with open(output_file) as stdoutfile:
  104. openssl_out = stdoutfile.read()
  105. node_list = ['fedora1.openshift.io']
  106. # mock openssl req call.
  107. with patch(RUN_CMD_MOCK) as call_mock:
  108. call_mock.return_value = (0, openssl_out, '')
  109. csr_dict = oc_csr_approve.process_csrs(module, csrs, node_list, "server")
  110. # actually run openssl req call.
  111. node_list = ['fedora2.mguginolocal.com']
  112. csr_dict = oc_csr_approve.process_csrs(module, csrs, node_list, "server")
  113. assert csr_dict['csr-2cxkp'] == 'fedora2.mguginolocal.com'
  114. if __name__ == '__main__':
  115. test_parse_subject_cn()
  116. test_get_ready_nodes()
  117. test_get_csrs()
  118. test_confirm_needed_requests_present()
  119. test_approve_csrs()
  120. test_get_ready_nodes_server()
  121. test_get_csrs_server()