Debian Codegraph @

testing/mozbase/mozcrash/tests/conftest.py

raw
1 # coding=UTF-8
2
3 import uuid
4
5 import mozcrash
6 import pytest
7 from py._path.common import fspath
8
9
10 @pytest.fixture(scope="session")
11 def stackwalk(tmpdir_factory):
12 stackwalk = tmpdir_factory.mktemp("stackwalk_binary").join("stackwalk")
13 stackwalk.write("fake binary")
14 stackwalk.chmod(0o744)
15 return stackwalk
16
17
18 @pytest.fixture
19 def check_for_crashes(tmpdir, stackwalk, monkeypatch):
20 monkeypatch.delenv("MINIDUMP_SAVE_PATH", raising=False)
21
22 def wrapper(
23 dump_directory=fspath(tmpdir),
24 symbols_path="symbols_path",
25 stackwalk_binary=fspath(stackwalk),
26 dump_save_path=None,
27 test_name=None,
28 quiet=True,
29 keep=None,
30 ):
31 return mozcrash.check_for_crashes(
32 dump_directory,
33 symbols_path,
34 stackwalk_binary,
35 dump_save_path,
36 test_name,
37 quiet,
38 keep,
39 )
40
41 return wrapper
42
43
44 @pytest.fixture
45 def check_for_java_exception():
46 def wrapper(logcat=None, test_name=None, quiet=True):
47 return mozcrash.check_for_java_exception(logcat, test_name, quiet)
48
49 return wrapper
50
51
52 def minidump_files(request, tmpdir):
53 files = []
54
55 for i in range(getattr(request, "param", 1)):
56 name = uuid.uuid4()
57
58 dmp = tmpdir.join(f"{name}.dmp")
59 dmp.write("foo")
60
61 extra = tmpdir.join(f"{name}.extra")
62
63 extra.write_text(
64 """
65 {
66 "ContentSandboxLevel":"2",
67 "TelemetryEnvironment":"{🍪}",
68 "EMCheckCompatibility":"true",
69 "ProductName":"Firefox",
70 "ContentSandboxCapabilities":"119",
71 "TelemetryClientId":"",
72 "Vendor":"Mozilla",
73 "InstallTime":"1000000000",
74 "Theme":"classic/1.0",
75 "ReleaseChannel":"default",
76 "ServerURL":"https://crash-reports.mozilla.com",
77 "SafeMode":"0",
78 "ContentSandboxCapable":"1",
79 "useragent_locale":"en-US",
80 "Version":"55.0a1",
81 "BuildID":"20170512114708",
82 "ProductID":"{ec8030f7-c20a-464f-9b0e-13a3a9e97384}",
83 "MozCrashReason": "MOZ_CRASH()",
84 "TelemetryServerURL":"",
85 "DOMIPCEnabled":"1",
86 "Add-ons":"",
87 "CrashTime":"1494582646",
88 "UptimeTS":"14.9179586",
89 "ContentSandboxEnabled":"1",
90 "ProcessType":"content",
91 "StartupTime":"1000000000",
92 "URL":"about:home"
93 }
94
95 """,
96 encoding="utf-8",
97 )
98
99 files.append({"dmp": dmp, "extra": extra})
100
101 return files
102
103
104 @pytest.fixture(name="minidump_files")
105 def minidump_files_fixture(request, tmpdir):
106 return minidump_files(request, tmpdir)
107
108
109 @pytest.fixture(autouse=True)
110 def mock_popen(monkeypatch):
111 """Generate a class that can mock subprocess.Popen.
112
113 :param stdouts: Iterable that should return an iterable for the
114 stdout of each process in turn.
115 """
116
117 class MockPopen:
118 def __init__(self, args, *args_rest, **kwargs):
119 # all_popens.append(self)
120 self.args = args
121 self.returncode = 0
122
123 def communicate(self):
124 return ("Stackwalk command: {}".format(" ".join(self.args)), "")
125
126 def wait(self):
127 return self.returncode
128
129 monkeypatch.setattr(mozcrash.mozcrash.subprocess, "Popen", MockPopen)