-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathsgcollect_info_test.py
120 lines (109 loc) · 3.81 KB
/
sgcollect_info_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
# 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 io
import unittest
import pytest
import sgcollect_info
@pytest.mark.parametrize(
"config",
[
'{{"logfilepath": "{tmpdir}"}}',
'{{"Logging": {{ "default": {{ "logfilepath": "{log_file}" }} }} }}',
'{{"logging": {{ "log_file_path": "{tmpdir}" }} }}',
],
)
def test_make_collect_logs_tasks(config, tmpdir):
log_file = tmpdir.join("sg_info.log")
log_file.write("foo")
with unittest.mock.patch(
"sgcollect_info.urlopen_with_basic_auth",
return_value=io.BytesIO(
config.format(
tmpdir=str(tmpdir).replace("\\", "\\\\"),
log_file=str(log_file).replace("\\", "\\\\"),
).encode("utf-8")
),
):
rotated_log_file = tmpdir.join("sg_info-01.log.gz")
rotated_log_file.write("foo")
tasks = sgcollect_info.make_collect_logs_tasks(
sg_url="fakeurl",
sg_config_file_path="",
sg_username="",
sg_password="",
)
assert [t.log_file for t in tasks] == [
log_file.basename,
rotated_log_file.basename,
]
def test_make_collect_logs_heap_profile(tmpdir):
with unittest.mock.patch(
"sgcollect_info.urlopen_with_basic_auth",
return_value=io.BytesIO(
'{{"logfilepath": "{logpath}"}}'.format(
logpath=str(tmpdir).replace("\\", "\\\\")
).encode("utf-8")
),
):
pprof_file = tmpdir.join("pprof_heap_high_01.pb.gz")
pprof_file.write("foo")
tasks = sgcollect_info.make_collect_logs_tasks(
sg_url="fakeurl",
sg_config_file_path="",
sg_username="",
sg_password="",
)
assert [tasks[0].log_file] == [pprof_file.basename]
# ensure that this is not redacted task
assert tasks[0].description.startswith("Contents of")
@pytest.mark.parametrize("should_redact", [True, False])
def test_make_collect_logs_tasks_duplicate_files(should_redact, tmp_path):
tmpdir1 = tmp_path / "tmpdir1"
tmpdir2 = tmp_path / "tmpdir2"
config = """
{{"logfilepath": "{tmpdir1}",
"logging": {{ "log_file_path": "{tmpdir2}" }}
}}
"""
for d in [tmpdir1, tmpdir2]:
d.mkdir()
(d / "sg_info.log").write_text("foo")
(d / "sg_info-01.log.gz").write_text("foo")
with unittest.mock.patch(
"sgcollect_info.urlopen_with_basic_auth",
return_value=io.BytesIO(
config.format(
tmpdir1=str(tmpdir1).replace("\\", "\\\\"),
tmpdir2=str(tmpdir2).replace("\\", "\\\\"),
).encode("utf-8")
),
):
tasks = sgcollect_info.make_collect_logs_tasks(
sg_url="fakeurl",
sg_config_file_path="",
sg_username="",
sg_password="",
)
# assert all tasks have unique log_file names
assert len(set(t.log_file for t in tasks)) == len(tasks)
assert set(t.log_file for t in tasks) == {
"sg_info.log",
"sg_info.log.1",
"sg_info-01.log.gz",
"sg_info-01.log.gz.1",
}
@pytest.mark.parametrize(
"basenames, filename, expected",
[
({}, "foo", "foo"),
({"foo"}, "foo", "foo.1"),
({"foo", "foo.1"}, "foo", "foo.2"),
],
)
def test_get_unique_filename(basenames, filename, expected):
assert sgcollect_info.get_unique_filename(basenames, filename) == expected