forked from open-telemetry/opentelemetry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_run.py
118 lines (100 loc) · 3.81 KB
/
test_run.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
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# type: ignore
from os import environ, getcwd
from os.path import abspath, dirname, pathsep
from unittest import TestCase
from unittest.mock import patch
from opentelemetry.environment_variables import OTEL_TRACES_EXPORTER
from opentelemetry.instrumentation import auto_instrumentation
class TestRun(TestCase):
auto_instrumentation_path = dirname(abspath(auto_instrumentation.__file__))
@classmethod
def setUpClass(cls):
cls.execl_patcher = patch(
"opentelemetry.instrumentation.auto_instrumentation.execl"
)
cls.which_patcher = patch(
"opentelemetry.instrumentation.auto_instrumentation.which"
)
cls.execl_patcher.start()
cls.which_patcher.start()
@classmethod
def tearDownClass(cls):
cls.execl_patcher.stop()
cls.which_patcher.stop()
@patch("sys.argv", ["instrument", ""])
@patch.dict("os.environ", {"PYTHONPATH": ""})
def test_empty(self):
auto_instrumentation.run()
self.assertEqual(
environ["PYTHONPATH"],
pathsep.join([self.auto_instrumentation_path, getcwd()]),
)
@patch("sys.argv", ["instrument", ""])
@patch.dict("os.environ", {"PYTHONPATH": "abc"})
def test_non_empty(self):
auto_instrumentation.run()
self.assertEqual(
environ["PYTHONPATH"],
pathsep.join([self.auto_instrumentation_path, getcwd(), "abc"]),
)
@patch("sys.argv", ["instrument", ""])
@patch.dict(
"os.environ",
{"PYTHONPATH": pathsep.join(["abc", auto_instrumentation_path])},
)
def test_after_path(self):
auto_instrumentation.run()
self.assertEqual(
environ["PYTHONPATH"],
pathsep.join([self.auto_instrumentation_path, getcwd(), "abc"]),
)
@patch("sys.argv", ["instrument", ""])
@patch.dict(
"os.environ",
{
"PYTHONPATH": pathsep.join(
[auto_instrumentation_path, "abc", auto_instrumentation_path]
)
},
)
def test_single_path(self):
auto_instrumentation.run()
self.assertEqual(
environ["PYTHONPATH"],
pathsep.join([self.auto_instrumentation_path, getcwd(), "abc"]),
)
class TestExecl(TestCase):
@patch("sys.argv", ["1", "2", "3"])
@patch("opentelemetry.instrumentation.auto_instrumentation.which")
@patch("opentelemetry.instrumentation.auto_instrumentation.execl")
def test_execl(
self, mock_execl, mock_which
): # pylint: disable=no-self-use
mock_which.configure_mock(**{"return_value": "python"})
auto_instrumentation.run()
mock_execl.assert_called_with("python", "python", "3")
class TestArgs(TestCase):
@patch("opentelemetry.instrumentation.auto_instrumentation.execl")
def test_exporter(self, _): # pylint: disable=no-self-use
with patch("sys.argv", ["instrument", "2"]):
auto_instrumentation.run()
self.assertIsNone(environ.get(OTEL_TRACES_EXPORTER))
with patch(
"sys.argv",
["instrument", "--traces_exporter", "jaeger", "1", "2"],
):
auto_instrumentation.run()
self.assertEqual(environ.get(OTEL_TRACES_EXPORTER), "jaeger")