|
13 | 13 | # See the License for the specific language governing permissions and |
14 | 14 | # limitations under the License. |
15 | 15 | # |
16 | | -import common |
17 | 16 | import pytest |
| 17 | +import common |
| 18 | +from unittest.mock import patch, MagicMock |
18 | 19 | from splunklib import binding |
19 | 20 |
|
20 | 21 | from solnlib import server_info |
@@ -72,3 +73,91 @@ def _mock_get(self, path_segment, owner=None, app=None, sharing=None, **query): |
72 | 73 |
|
73 | 74 | si = server_info.ServerInfo(common.SESSION_KEY) |
74 | 75 | assert si.is_captain_ready() |
| 76 | + |
| 77 | + @patch("solnlib.server_info.os.environ", autospec=True, return_value="$SPLUNK_HOME") |
| 78 | + @patch( |
| 79 | + "solnlib.server_info.get_splunkd_access_info", |
| 80 | + autospec=True, |
| 81 | + return_value=("https", "127.0.0.1", "8089"), |
| 82 | + ) |
| 83 | + @patch("solnlib.server_info.rest_client", autospec=True) |
| 84 | + @patch("solnlib.server_info.getWebCertFile", return_value=None) |
| 85 | + @patch("solnlib.server_info.getWebKeyFile", return_value=None) |
| 86 | + def test_server_info_object_with_no_certs( |
| 87 | + self, mock_web_key, mock_web_cert, mock_rest_client, mock_splunkd, mock_os_env |
| 88 | + ): |
| 89 | + mock_rest_client.SplunkRestClient = MagicMock() |
| 90 | + |
| 91 | + server_info.ServerInfo(common.SESSION_KEY) |
| 92 | + |
| 93 | + for call_arg in mock_rest_client.SplunkRestClient.call_args_list: |
| 94 | + _, kwargs = call_arg |
| 95 | + assert kwargs.get("cert_file") is None |
| 96 | + assert kwargs.get("key_file") is None |
| 97 | + assert kwargs.get("verify") is None |
| 98 | + |
| 99 | + @patch("solnlib.server_info.os.environ", autospec=True, return_value="$SPLUNK_HOME") |
| 100 | + @patch( |
| 101 | + "solnlib.server_info.get_splunkd_access_info", |
| 102 | + autospec=True, |
| 103 | + return_value=("https", "127.0.0.1", "8089"), |
| 104 | + ) |
| 105 | + @patch("solnlib.server_info.rest_client", autospec=True) |
| 106 | + @patch("solnlib.server_info.getWebCertFile", return_value="/path/cert/pem") |
| 107 | + @patch("solnlib.server_info.getWebKeyFile", return_value="/path/key/pem") |
| 108 | + def test_server_info_object_with_both_certs( |
| 109 | + self, mock_web_key, mock_web_cert, mock_rest_client, mock_splunkd, mock_os_env |
| 110 | + ): |
| 111 | + mock_rest_client.SplunkRestClient = MagicMock() |
| 112 | + |
| 113 | + server_info.ServerInfo(common.SESSION_KEY) |
| 114 | + |
| 115 | + for call_arg in mock_rest_client.SplunkRestClient.call_args_list: |
| 116 | + _, kwargs = call_arg |
| 117 | + assert kwargs.get("cert_file") == "/path/cert/pem" |
| 118 | + assert kwargs.get("key_file") == "/path/key/pem" |
| 119 | + assert kwargs.get("verify") is False |
| 120 | + |
| 121 | + @patch("solnlib.server_info.os.environ", autospec=True, return_value="$SPLUNK_HOME") |
| 122 | + @patch( |
| 123 | + "solnlib.server_info.get_splunkd_access_info", |
| 124 | + autospec=True, |
| 125 | + return_value=("https", "127.0.0.1", "8089"), |
| 126 | + ) |
| 127 | + @patch("solnlib.server_info.rest_client", autospec=True) |
| 128 | + @patch("solnlib.server_info.getWebCertFile", return_value="/path/cert/pem") |
| 129 | + @patch("solnlib.server_info.getWebKeyFile", return_value=None) |
| 130 | + def test_server_info_object_with_cert_file( |
| 131 | + self, mock_web_key, mock_web_cert, mock_rest_client, mock_splunkd, mock_os_env |
| 132 | + ): |
| 133 | + mock_rest_client.SplunkRestClient = MagicMock() |
| 134 | + |
| 135 | + server_info.ServerInfo(common.SESSION_KEY) |
| 136 | + |
| 137 | + for call_arg in mock_rest_client.SplunkRestClient.call_args_list: |
| 138 | + _, kwargs = call_arg |
| 139 | + assert kwargs.get("cert_file") == "/path/cert/pem" |
| 140 | + assert kwargs.get("key_file") is None |
| 141 | + assert kwargs.get("verify") is False |
| 142 | + |
| 143 | + @patch("solnlib.server_info.os.environ", autospec=True, return_value="$SPLUNK_HOME") |
| 144 | + @patch( |
| 145 | + "solnlib.server_info.get_splunkd_access_info", |
| 146 | + autospec=True, |
| 147 | + return_value=("https", "127.0.0.1", "8089"), |
| 148 | + ) |
| 149 | + @patch("solnlib.server_info.rest_client", autospec=True) |
| 150 | + @patch("solnlib.server_info.getWebCertFile", return_value=None) |
| 151 | + @patch("solnlib.server_info.getWebKeyFile", return_value="/path/key/pem") |
| 152 | + def test_server_info_object_with_key_file( |
| 153 | + self, mock_web_key, mock_web_cert, mock_rest_client, mock_splunkd, mock_os_env |
| 154 | + ): |
| 155 | + mock_rest_client.SplunkRestClient = MagicMock() |
| 156 | + |
| 157 | + server_info.ServerInfo(common.SESSION_KEY) |
| 158 | + |
| 159 | + for call_arg in mock_rest_client.SplunkRestClient.call_args_list: |
| 160 | + _, kwargs = call_arg |
| 161 | + assert kwargs.get("cert_file") is None |
| 162 | + assert kwargs.get("key_file") is None |
| 163 | + assert kwargs.get("verify") is None |
0 commit comments