forked from open-telemetry/opentelemetry-python-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_utils.py
188 lines (171 loc) · 8.42 KB
/
test_utils.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
# 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.
import unittest
from http import HTTPStatus
from opentelemetry.instrumentation.utils import (
_add_sql_comment,
_python_path_without_directory,
http_status_to_status_code,
)
from opentelemetry.trace import StatusCode
class TestUtils(unittest.TestCase):
# See https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#status
def test_http_status_to_status_code(self):
for status_code, expected in (
(HTTPStatus.OK, StatusCode.UNSET),
(HTTPStatus.ACCEPTED, StatusCode.UNSET),
(HTTPStatus.IM_USED, StatusCode.UNSET),
(HTTPStatus.MULTIPLE_CHOICES, StatusCode.UNSET),
(HTTPStatus.BAD_REQUEST, StatusCode.ERROR),
(HTTPStatus.UNAUTHORIZED, StatusCode.ERROR),
(HTTPStatus.FORBIDDEN, StatusCode.ERROR),
(HTTPStatus.NOT_FOUND, StatusCode.ERROR),
(
HTTPStatus.UNPROCESSABLE_ENTITY,
StatusCode.ERROR,
),
(
HTTPStatus.TOO_MANY_REQUESTS,
StatusCode.ERROR,
),
(HTTPStatus.NOT_IMPLEMENTED, StatusCode.ERROR),
(HTTPStatus.SERVICE_UNAVAILABLE, StatusCode.ERROR),
(
HTTPStatus.GATEWAY_TIMEOUT,
StatusCode.ERROR,
),
(
HTTPStatus.HTTP_VERSION_NOT_SUPPORTED,
StatusCode.ERROR,
),
(600, StatusCode.ERROR),
(99, StatusCode.ERROR),
):
with self.subTest(status_code=status_code):
actual = http_status_to_status_code(int(status_code))
self.assertEqual(actual, expected, status_code)
def test_http_status_to_status_code_none(self):
for status_code, expected in ((None, StatusCode.UNSET),):
with self.subTest(status_code=status_code):
actual = http_status_to_status_code(status_code)
self.assertEqual(actual, expected, status_code)
def test_http_status_to_status_code_redirect(self):
for status_code, expected in (
(HTTPStatus.MULTIPLE_CHOICES, StatusCode.ERROR),
(HTTPStatus.MOVED_PERMANENTLY, StatusCode.ERROR),
(HTTPStatus.TEMPORARY_REDIRECT, StatusCode.ERROR),
(HTTPStatus.PERMANENT_REDIRECT, StatusCode.ERROR),
):
with self.subTest(status_code=status_code):
actual = http_status_to_status_code(
int(status_code), allow_redirect=False
)
self.assertEqual(actual, expected, status_code)
def test_http_status_to_status_code_server(self):
for status_code, expected in (
(HTTPStatus.OK, StatusCode.UNSET),
(HTTPStatus.ACCEPTED, StatusCode.UNSET),
(HTTPStatus.IM_USED, StatusCode.UNSET),
(HTTPStatus.MULTIPLE_CHOICES, StatusCode.UNSET),
(HTTPStatus.BAD_REQUEST, StatusCode.UNSET),
(HTTPStatus.UNAUTHORIZED, StatusCode.UNSET),
(HTTPStatus.FORBIDDEN, StatusCode.UNSET),
(HTTPStatus.NOT_FOUND, StatusCode.UNSET),
(
HTTPStatus.UNPROCESSABLE_ENTITY,
StatusCode.UNSET,
),
(
HTTPStatus.TOO_MANY_REQUESTS,
StatusCode.UNSET,
),
(HTTPStatus.NOT_IMPLEMENTED, StatusCode.ERROR),
(HTTPStatus.SERVICE_UNAVAILABLE, StatusCode.ERROR),
(
HTTPStatus.GATEWAY_TIMEOUT,
StatusCode.ERROR,
),
(
HTTPStatus.HTTP_VERSION_NOT_SUPPORTED,
StatusCode.ERROR,
),
(600, StatusCode.ERROR),
(99, StatusCode.ERROR),
):
with self.subTest(status_code=status_code):
actual = http_status_to_status_code(
int(status_code), server_span=True
)
self.assertEqual(actual, expected, status_code)
def test_remove_current_directory_from_python_path_windows(self):
directory = r"c:\users\Trayvon Martin\workplace\opentelemetry-python-contrib\opentelemetry-instrumentation\src\opentelemetry\instrumentation\auto_instrumentation"
path_separator = r";"
python_path = r"c:\users\Trayvon Martin\workplace\opentelemetry-python-contrib\opentelemetry-instrumentation\src\opentelemetry\instrumentation\auto_instrumentation;C:\Users\trayvonmartin\workplace"
actual_python_path = _python_path_without_directory(
python_path, directory, path_separator
)
expected_python_path = r"C:\Users\trayvonmartin\workplace"
self.assertEqual(actual_python_path, expected_python_path)
def test_remove_current_directory_from_python_path_linux(self):
directory = r"/home/georgefloyd/workplace/opentelemetry-python-contrib/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation"
path_separator = r":"
python_path = r"/home/georgefloyd/workplace/opentelemetry-python-contrib/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation:/home/georgefloyd/workplace"
actual_python_path = _python_path_without_directory(
python_path, directory, path_separator
)
expected_python_path = r"/home/georgefloyd/workplace"
self.assertEqual(actual_python_path, expected_python_path)
def test_remove_current_directory_from_python_path_windows_only_path(self):
directory = r"c:\users\Charleena Lyles\workplace\opentelemetry-python-contrib\opentelemetry-instrumentation\src\opentelemetry\instrumentation\auto_instrumentation"
path_separator = r";"
python_path = r"c:\users\Charleena Lyles\workplace\opentelemetry-python-contrib\opentelemetry-instrumentation\src\opentelemetry\instrumentation\auto_instrumentation"
actual_python_path = _python_path_without_directory(
python_path, directory, path_separator
)
self.assertEqual(actual_python_path, python_path)
def test_remove_current_directory_from_python_path_linux_only_path(self):
directory = r"/home/SandraBland/workplace/opentelemetry-python-contrib/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation"
path_separator = r":"
python_path = r"/home/SandraBland/workplace/opentelemetry-python-contrib/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation"
actual_python_path = _python_path_without_directory(
python_path, directory, path_separator
)
self.assertEqual(actual_python_path, python_path)
def test_add_sql_comments_with_semicolon(self):
sql_query_without_semicolon = "Select 1;"
comments = {"comment_1": "value 1", "comment 2": "value 3"}
commented_sql_without_semicolon = _add_sql_comment(
sql_query_without_semicolon, **comments
)
self.assertEqual(
commented_sql_without_semicolon,
"Select 1 /*comment%%202='value%%203',comment_1='value%%201'*/;",
)
def test_add_sql_comments_without_semicolon(self):
sql_query_without_semicolon = "Select 1"
comments = {"comment_1": "value 1", "comment 2": "value 3"}
commented_sql_without_semicolon = _add_sql_comment(
sql_query_without_semicolon, **comments
)
self.assertEqual(
commented_sql_without_semicolon,
"Select 1 /*comment%%202='value%%203',comment_1='value%%201'*/",
)
def test_add_sql_comments_without_comments(self):
sql_query_without_semicolon = "Select 1"
comments = {}
commented_sql_without_semicolon = _add_sql_comment(
sql_query_without_semicolon, **comments
)
self.assertEqual(commented_sql_without_semicolon, "Select 1")