test_oc_csr_approve.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import os
  2. import sys
  3. from unittest.mock import patch
  4. from ansible.module_utils.basic import AnsibleModule
  5. MODULE_PATH = os.path.realpath(os.path.join(__file__, os.pardir, os.pardir, 'library'))
  6. sys.path.insert(1, MODULE_PATH)
  7. import oc_csr_approve # noqa
  8. from oc_csr_approve import CSRapprove # noqa
  9. # base path for text files with sample outputs.
  10. ASSET_PATH = os.path.realpath(os.path.join(__file__, os.pardir, 'test_data'))
  11. RUN_CMD_MOCK = 'ansible.module_utils.basic.AnsibleModule.run_command'
  12. class DummyModule(AnsibleModule):
  13. def _load_params(self):
  14. self.params = {}
  15. def exit_json(*args, **kwargs):
  16. return 0
  17. def fail_json(*args, **kwargs):
  18. raise Exception(kwargs['msg'])
  19. def test_parse_subject_cn():
  20. subject = 'subject=/C=US/CN=fedora1.openshift.io/L=Raleigh/O=Red Hat/ST=North Carolina/OU=OpenShift\n'
  21. assert oc_csr_approve.parse_subject_cn(subject) == 'fedora1.openshift.io'
  22. subject = 'subject=C = US, CN = test.io, L = City, O = Company, ST = State, OU = Dept\n'
  23. assert oc_csr_approve.parse_subject_cn(subject) == 'test.io'
  24. def test_csr_present_check():
  25. csr_dict = {'csr-1': 'fedora1.openshift.io'}
  26. nodename = 'fedora1.openshift.io'
  27. assert oc_csr_approve.csr_present_check(nodename, csr_dict) is True
  28. nodename = 'fedora2.openshift.io'
  29. assert oc_csr_approve.csr_present_check(nodename, csr_dict) is False
  30. def test_get_nodes():
  31. output_file = os.path.join(ASSET_PATH, 'oc_get_nodes.json')
  32. with open(output_file) as stdoutfile:
  33. oc_get_nodes_stdout = stdoutfile.read()
  34. module = DummyModule({})
  35. approver = CSRapprove(module, 'oc', '/dev/null', [])
  36. with patch(RUN_CMD_MOCK) as call_mock:
  37. call_mock.return_value = (0, oc_get_nodes_stdout, '')
  38. all_nodes = approver.get_nodes()
  39. assert all_nodes == ['fedora1.openshift.io', 'fedora2.openshift.io', 'fedora3.openshift.io']
  40. def test_get_csrs_client():
  41. module = DummyModule({})
  42. approver = CSRapprove(module, 'oc', '/dev/null', [])
  43. output_file = os.path.join(ASSET_PATH, 'oc_csr_approve_pending.json')
  44. with open(output_file) as stdoutfile:
  45. oc_get_csr_out = stdoutfile.read()
  46. # mock oc get csr call to cluster
  47. with patch(RUN_CMD_MOCK) as call_mock:
  48. call_mock.return_value = (0, oc_get_csr_out, '')
  49. csrs = approver.get_csrs()
  50. assert csrs[0]['kind'] == "CertificateSigningRequest"
  51. output_file = os.path.join(ASSET_PATH, 'openssl1.txt')
  52. with open(output_file) as stdoutfile:
  53. openssl_out = stdoutfile.read()
  54. # mock openssl req call.
  55. nodename = 'fedora1.openshift.io'
  56. approver = CSRapprove(module, 'oc', '/dev/null', nodename)
  57. with patch(RUN_CMD_MOCK) as call_mock:
  58. call_mock.return_value = (0, openssl_out, '')
  59. csr_dict = approver.process_csrs(csrs, "client")
  60. # actually run openssl req call.
  61. csr_dict = approver.process_csrs(csrs, "client")
  62. assert csr_dict['node-csr-TkefytQp8Dz4Xp7uzcw605MocvI0gWuEOGNrHhOjGNQ'] == 'fedora1.openshift.io'
  63. def test_get_csrs_server():
  64. module = DummyModule({})
  65. output_file = os.path.join(ASSET_PATH, 'oc_csr_server_multiple_pends_one_host.json')
  66. with open(output_file) as stdoutfile:
  67. oc_get_csr_out = stdoutfile.read()
  68. approver = CSRapprove(module, 'oc', '/dev/null', [])
  69. # mock oc get csr call to cluster
  70. with patch(RUN_CMD_MOCK) as call_mock:
  71. call_mock.return_value = (0, oc_get_csr_out, '')
  72. csrs = approver.get_csrs()
  73. assert csrs[0]['kind'] == "CertificateSigningRequest"
  74. output_file = os.path.join(ASSET_PATH, 'openssl1.txt')
  75. with open(output_file) as stdoutfile:
  76. openssl_out = stdoutfile.read()
  77. nodename = 'fedora1.openshift.io'
  78. approver = CSRapprove(module, 'oc', '/dev/null', nodename)
  79. # mock openssl req call.
  80. with patch(RUN_CMD_MOCK) as call_mock:
  81. call_mock.return_value = (0, openssl_out, '')
  82. csr_dict = approver.process_csrs(csrs, "server")
  83. # actually run openssl req call.
  84. nodename = 'fedora1.openshift.io'
  85. approver = CSRapprove(module, 'oc', '/dev/null', nodename)
  86. csr_dict = approver.process_csrs(csrs, "server")
  87. assert csr_dict['csr-2cxkp'] == 'fedora1.openshift.io'
  88. def test_process_csrs():
  89. module = DummyModule({})
  90. approver = CSRapprove(module, 'oc', '/dev/null', 'fedora1.openshift.io')
  91. output_file = os.path.join(ASSET_PATH, 'oc_csr_approve_pending.json')
  92. with open(output_file) as stdoutfile:
  93. oc_get_csr_out = stdoutfile.read()
  94. # mock oc get csr call to cluster
  95. with patch(RUN_CMD_MOCK) as call_mock:
  96. call_mock.return_value = (0, oc_get_csr_out, '')
  97. csrs = approver.get_csrs()
  98. csr_dict = approver.process_csrs(csrs, "client")
  99. assert csr_dict == {'node-csr-TkefytQp8Dz4Xp7uzcw605MocvI0gWuEOGNrHhOjGNQ': 'fedora1.openshift.io'}
  100. def test_approve_csrs():
  101. module = DummyModule({})
  102. csr_dict = {'csr-1': 'fedora1.openshift.io'}
  103. approver = CSRapprove(module, 'oc', '/dev/null', '')
  104. with patch(RUN_CMD_MOCK) as call_mock:
  105. call_mock.return_value = (0, 'csr-1 ok', '')
  106. approver.approve_csrs(csr_dict, 'client')
  107. assert approver.result['client_approve_results'] == ['fedora1.openshift.io: csr-1 ok']
  108. def test_node_is_ready():
  109. module = DummyModule({})
  110. nodename = 'fedora1.openshift.io'
  111. approver = CSRapprove(module, 'oc', '/dev/null', nodename)
  112. with patch(RUN_CMD_MOCK) as call_mock:
  113. call_mock.return_value = (0, 'ok', '')
  114. result = approver.node_is_ready(nodename)
  115. assert result is True
  116. with patch(RUN_CMD_MOCK) as call_mock:
  117. call_mock.return_value = (1, 'stdout fail', 'stderr fail')
  118. result = approver.node_is_ready(nodename)
  119. assert result is False