-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathtasks_test.py
238 lines (207 loc) · 6.26 KB
/
tasks_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# Copyright 2024-Present Couchbase, Inc.
#
# Use of this software is governed by the Business Source License included
# in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
# in that file, in accordance with the Business Source License, use of this
# software will be governed by the Apache License, Version 2.0, included in
# the file licenses/APL2.txt.
import gzip
import json
import os
import pathlib
import sys
import unittest
import password_remover
import pytest
import tasks
VERBOSE = 2
INPUT_CONFIG = """\
{
"password": "password",
"server": "http://localhost:4984/db",
"databases": {
"db" : {
"users" : {
"foo" : "bar"
}
}
}
}"""
REDACTED_OUTPUT = """\
{
"password": "******",
"server": "http://localhost:4984/db",
"databases": {
"db": {
"users": {
"<ud>foo</ud>": "bar"
}
}
}
}"""
@pytest.mark.parametrize("tag_userdata", [True, False])
def test_add_file_task(tmp_path, tag_userdata):
if tag_userdata:
expected = REDACTED_OUTPUT
else:
expected = REDACTED_OUTPUT.replace("<ud>foo</ud>", "foo")
filename = "config.json"
config_file = tmp_path / filename
config_file.write_text(INPUT_CONFIG)
postprocessors = [password_remover.remove_passwords]
if tag_userdata:
postprocessors.append(password_remover.tag_userdata_in_server_config)
task = tasks.add_file_task(
sourcefile_path=config_file,
output_path=config_file.name,
content_postprocessors=postprocessors,
)
output_dir = tmp_path / "output"
output_dir.mkdir()
runner = tasks.TaskRunner(
verbosity=VERBOSE,
default_name="sg.log",
tmp_dir=output_dir,
)
runner.run(task)
runner.close_all_files()
with open(pathlib.Path(runner.tmpdir) / filename) as fh:
assert expected in fh.read()
def test_make_curl_task(tmpdir, httpserver):
output = "curltask"
httpserver.expect_request("/").respond_with_json(json.loads(INPUT_CONFIG))
task = tasks.make_curl_task(
"curltask",
httpserver.url_for("/"),
content_postprocessors=[
password_remover.remove_passwords,
password_remover.tag_userdata_in_server_config,
],
log_file=output,
)
output_dir = tmpdir.mkdir("output")
runner = tasks.TaskRunner(
verbosity=VERBOSE,
default_name="sg.log",
tmp_dir=output_dir,
)
runner.run(task)
runner.close_all_files()
with open(pathlib.Path(runner.tmpdir) / output) as fh:
assert REDACTED_OUTPUT in fh.read()
httpserver.check()
def test_task_print_literal(tmp_path):
task = tasks.AllOsTask("test_task", ["notacommand"], literal="literal")
runner = tasks.TaskRunner(tmp_dir=tmp_path)
runner.run(task)
with open(pathlib.Path(runner.tmpdir) / runner.default_name) as fh:
assert "literal" in fh.read()
def test_task_timeout(tmp_path):
task = tasks.AllOsTask("test_task", ["sleep", "5"], timeout=0.01)
runner = tasks.TaskRunner(tmp_dir=tmp_path)
runner.run(task)
with open(pathlib.Path(runner.tmpdir) / runner.default_name) as fh:
assert "`['sleep', '5']` timed out after 0.01 seconds" in fh.read()
def test_task_popen_exception(tmp_path):
task = tasks.AllOsTask("test_task", ["notacommand"], timeout=0.01)
runner = tasks.TaskRunner(tmp_dir=tmp_path)
with unittest.mock.patch("subprocess.Popen") as popen:
popen.side_effect = OSError("Boom!")
runner.run(task)
with open(pathlib.Path(runner.tmpdir) / runner.default_name) as fh:
assert "Failed to execute ['notacommand']: Boom!" in fh.read()
@pytest.mark.parametrize("verbosity", [0, 1, 2])
@pytest.mark.parametrize("task_platform", [sys.platform, "fakeplatform"])
def test_task_logging(verbosity, task_platform, tmp_path):
taskrunner = tasks.TaskRunner(verbosity=verbosity, tmp_dir=tmp_path)
task = tasks.AllOsTask("echo", "echo")
# fake the platform for a test
task.platforms = [task_platform]
taskrunner.run(task)
@pytest.mark.parametrize(
"filename,redactable",
[
(
"sync_gateway",
False,
),
(
"sync_gateway.exe",
False,
),
(
"/abs/path/sync_gateway",
False,
),
(
"/abs/path/sync_gateway.exe",
False,
),
(
"pprof_heap_high_01.pb.gz",
False,
),
(
"pprof.pb",
False,
),
(
"/abs/path/pprof.pb",
False,
),
(
"sg_info.log",
True,
),
(
"sg_info-01.log.gz",
True,
),
(
"/abs/path/sg_info.log",
True,
),
(
"/abs/path/sg_info-01.log.gz",
True,
),
(
"expvars.json",
False,
),
(
"/abs/path/expvars.json",
False,
),
],
)
@pytest.mark.parametrize("use_pathlib", [True, False])
def test_redactable_filename(use_pathlib, filename, redactable):
if use_pathlib:
filename = pathlib.Path(filename)
assert tasks.redactable_file(filename) is redactable
def test_log_redact_file(tmp_path):
log_file = tmp_path / "foo.log.gz"
input_log_lines = [
"logline1: foo",
"logline2: <ud>password</ud>",
"logline3: log-redaction-salt=AAA",
"logline4: bar",
]
with gzip.open(log_file, "wt") as fh:
for line in input_log_lines:
fh.write(line + "\n")
salt = "AA"
redactor = tasks.LogRedactor(salt, tmp_path)
redacted_file = redactor.redact_file(log_file.name, log_file)
output_log_lines = [
"RedactLevel:partial,HashOfSalt:e2512172abf8cc9f67fdd49eb6cacf2df71bbad3",
"logline1: foo",
"logline2: <ud>1700bc8ae71605063ae83d80837fa53988c635ef</ud>",
"logline3: log-redaction-salt <redacted>",
"logline4: bar",
"", # file has trailing newline
]
updated_text = os.linesep.join(output_log_lines).encode("utf-8")
redacted_text = gzip.open(redacted_file).read()
assert redacted_text == updated_text