|
14 | 14 | # limitations under the License. |
15 | 15 | # |
16 | 16 |
|
| 17 | +import os |
17 | 18 | import os.path as op |
18 | 19 | import socket |
19 | | -import subprocess |
| 20 | + |
| 21 | +from configparser import ConfigParser |
| 22 | +from io import StringIO |
20 | 23 |
|
21 | 24 | from splunklib import binding, client |
22 | 25 | from splunklib.data import record |
|
32 | 35 |
|
33 | 36 |
|
34 | 37 | def mock_splunkhome(monkeypatch): |
35 | | - class MockPopen: |
36 | | - def __init__( |
37 | | - self, |
38 | | - args, |
39 | | - bufsize=0, |
40 | | - executable=None, |
41 | | - stdin=None, |
42 | | - stdout=None, |
43 | | - stderr=None, |
44 | | - preexec_fn=None, |
45 | | - close_fds=False, |
46 | | - shell=False, |
47 | | - cwd=None, |
48 | | - env=None, |
49 | | - universal_newlines=False, |
50 | | - startupinfo=None, |
51 | | - creationflags=0, |
52 | | - ): |
53 | | - self._conf = args[3] |
54 | | - |
55 | | - def communicate(self, input=None): |
56 | | - if self._conf == "server": |
57 | | - file_path = op.sep.join( |
58 | | - [cur_dir, "data/mock_splunk/etc/system/default/server.conf"] |
59 | | - ) |
60 | | - elif self._conf == "inputs": |
61 | | - file_path = op.sep.join( |
62 | | - [ |
63 | | - cur_dir, |
64 | | - "data/mock_splunk/etc/apps/splunk_httpinput/local/inputs.conf", |
65 | | - ] |
66 | | - ) |
67 | | - else: |
68 | | - file_path = op.sep.join( |
69 | | - [cur_dir, "data/mock_splunk/etc/system/default/web.conf"] |
| 38 | + def get_app_conf(confName, app, use_btool, app_path): |
| 39 | + if confName == "server": |
| 40 | + file_path = op.sep.join( |
| 41 | + [cur_dir, "data/mock_splunk/etc/system/default/server.conf"] |
| 42 | + ) |
| 43 | + elif confName == "inputs": |
| 44 | + file_path = op.sep.join( |
| 45 | + [ |
| 46 | + cur_dir, |
| 47 | + "data/mock_splunk/etc/apps/splunk_httpinput/local/inputs.conf", |
| 48 | + ] |
| 49 | + ) |
| 50 | + else: |
| 51 | + file_path = op.sep.join( |
| 52 | + [cur_dir, "data/mock_splunk/etc/system/default/web.conf"] |
| 53 | + ) |
| 54 | + |
| 55 | + with open(file_path) as fp: |
| 56 | + data = fp.read(), None |
| 57 | + |
| 58 | + parser = ConfigParser(**{"strict": False}) |
| 59 | + parser.optionxform = str |
| 60 | + parser.read_file(StringIO(data[0])) |
| 61 | + |
| 62 | + out = {} |
| 63 | + for section in parser.sections(): |
| 64 | + out[section] = { |
| 65 | + item[0]: item[1] for item in parser.items(section, raw=True) |
| 66 | + } |
| 67 | + return out |
| 68 | + |
| 69 | + def make_splunk_gome(parts): |
| 70 | + relpath = os.path.normpath(os.path.join(*parts)) |
| 71 | + basepath = "" |
| 72 | + etc_with_trailing_sep = os.path.join("etc", "") |
| 73 | + if (relpath == "etc") or relpath.startswith(etc_with_trailing_sep): |
| 74 | + try: |
| 75 | + basepath = os.environ["SPLUNK_ETC"] |
| 76 | + except KeyError: |
| 77 | + basepath = op.join(os.path.normpath(os.environ["SPLUNK_HOME"]), "etc") |
| 78 | + relpath = relpath[4:] |
| 79 | + else: |
| 80 | + basepath = os.path.normpath(os.environ["SPLUNK_HOME"]) |
| 81 | + fullpath = os.path.normpath(os.path.join(basepath, relpath)) |
| 82 | + if os.path.relpath(fullpath, basepath)[0:2] == "..": |
| 83 | + raise ValueError( |
| 84 | + 'Illegal escape from parent directory "{}": {}'.format( |
| 85 | + basepath, fullpath |
70 | 86 | ) |
| 87 | + ) |
71 | 88 |
|
72 | | - with open(file_path) as fp: |
73 | | - return fp.read(), None |
| 89 | + return fullpath |
74 | 90 |
|
75 | 91 | splunk_home = op.join(cur_dir, "data/mock_splunk/") |
76 | 92 | monkeypatch.setenv("SPLUNK_HOME", splunk_home) |
77 | 93 | monkeypatch.setenv("SPLUNK_ETC", op.join(splunk_home, "etc")) |
78 | | - monkeypatch.setattr(subprocess, "Popen", MockPopen) |
| 94 | + monkeypatch.setattr("solnlib.splunkenv.getAppConf", get_app_conf) |
| 95 | + monkeypatch.setattr("solnlib.splunkenv.mksplhomepath", make_splunk_gome) |
79 | 96 |
|
80 | 97 |
|
81 | 98 | def mock_serverinfo(monkeypatch): |
|
0 commit comments