-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathtest_analytics.py
More file actions
162 lines (122 loc) · 4.17 KB
/
test_analytics.py
File metadata and controls
162 lines (122 loc) · 4.17 KB
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
import json
import platform
import pytest
from voluptuous import Any, Schema
from dvc import analytics
from dvc.cli import parse_args
@pytest.fixture
def tmp_global_dir(mocker, tmp_path):
"""
Fixture to prevent modifying the actual global config
"""
def _user_config_dir(appname, *_args, **_kwargs):
return str(tmp_path / appname)
mocker.patch("iterative_telemetry.user_config_dir", _user_config_dir)
def test_collect_and_send_report(mocker, tmp_global_dir):
mock_json = mocker.patch("json.dump")
mock_daemon = mocker.patch("dvc.daemon._spawn")
analytics.collect_and_send_report()
report = mock_json.call_args[0][0]
assert not report.get("cmd_class")
assert not report.get("cmd_return_code")
args = parse_args(["add", "foo"])
return_code = 0
analytics.collect_and_send_report(args, return_code)
report = mock_json.call_args[0][0]
assert report["cmd_class"] == "CmdAdd"
assert report["cmd_return_code"] == return_code
assert mock_daemon.call_count == 2
def test_runtime_info(tmp_global_dir):
schema = Schema(
{
"dvc_version": str,
"is_binary": bool,
"scm_class": Any("Git", None),
"user_id": str,
"system_info": dict,
"group_id": Any(str, None),
},
required=True,
)
assert schema(analytics._runtime_info())
def test_send(mocker, tmp_path):
mock_post = mocker.patch("requests.post")
import requests
url = "https://analytics.dvc.org"
report = {"name": "dummy report"}
report_file = tmp_path / "report"
report_file.write_text(json.dumps(report))
mock_post.side_effect = requests.exceptions.RequestException
analytics.send(str(report_file))
assert mock_post.called
assert mock_post.call_args[0][0] == url
assert not report_file.exists()
@pytest.mark.parametrize(
"config, result",
[
({}, True),
({"analytics": "false"}, False),
({"analytics": "true"}, True),
({"unknown": "broken"}, True),
({"analytics": "false", "unknown": "broken"}, False),
],
)
def test_is_enabled(dvc, config, result, monkeypatch, tmp_global_dir):
with dvc.config.edit(validate=False) as conf:
conf["core"] = config
# reset DVC_TEST env var, which affects `is_enabled()`
monkeypatch.delenv("DVC_TEST")
monkeypatch.delenv("DVC_NO_ANALYTICS", raising=False)
assert result == analytics.is_enabled()
@pytest.mark.parametrize(
"config, env, result",
[
(None, None, True),
(None, "true", False),
(None, "false", False), # only checking if env is set
("false", None, False),
("false", "true", False),
("false", "false", False),
("true", None, True),
("true", "true", False),
("true", "false", False), # we checking if env is set
],
)
def test_is_enabled_env_neg(
dvc, config, env, result, monkeypatch, tmp_global_dir
):
# reset DVC_TEST env var, which affects `is_enabled()`
monkeypatch.delenv("DVC_TEST")
monkeypatch.delenv("DVC_NO_ANALYTICS", raising=False)
with dvc.config.edit() as conf:
conf["core"] = {}
assert analytics.is_enabled()
if config is not None:
with dvc.config.edit() as conf:
conf["core"] = {"analytics": config}
if env is not None:
monkeypatch.setenv("DVC_NO_ANALYTICS", env)
assert result == analytics.is_enabled()
def test_system_info():
schema = Schema({"os": Any("windows", "mac", "linux")}, required=True)
system = platform.system()
if system == "Windows":
schema = schema.extend(
{
"windows_version_build": int,
"windows_version_major": int,
"windows_version_minor": int,
"windows_version_service_pack": str,
}
)
if system == "Darwin":
schema = schema.extend({"mac_version": str})
if system == "Linux":
schema = schema.extend(
{
"linux_distro": str,
"linux_distro_like": str,
"linux_distro_version": str,
}
)
assert schema(analytics._system_info())