-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathprofiling_test.py
More file actions
147 lines (124 loc) · 4.15 KB
/
Copy pathprofiling_test.py
File metadata and controls
147 lines (124 loc) · 4.15 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
# Copyright 2024 Google LLC
#
# 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
#
# https://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.
from unittest import mock
from pathwaysutils import profiling
import requests
from absl.testing import absltest
from absl.testing import parameterized
class ProfilingTest(parameterized.TestCase):
"""Tests for Pathways on Cloud profiling."""
def setUp(self):
super().setUp()
self.mock_post = self.enter_context(
mock.patch.object(requests, "post", autospec=True)
)
@parameterized.parameters(8000, 1234)
def test_collect_profile_port(self, port):
profiling.collect_profile(
port=port,
duration_ms=1000,
host="127.0.0.1",
log_dir="gs://test_bucket/test_dir",
)
self.mock_post.assert_called_once_with(
f"http://127.0.0.1:{port}/profiling",
json={
"duration_ms": 1000,
"repository_path": "gs://test_bucket/test_dir",
},
)
@parameterized.parameters(1000, 1234)
def test_collect_profile_duration_ms(self, duration_ms):
profiling.collect_profile(
port=8000,
duration_ms=duration_ms,
host="127.0.0.1",
log_dir="gs://test_bucket/test_dir",
)
self.mock_post.assert_called_once_with(
"http://127.0.0.1:8000/profiling",
json={
"duration_ms": duration_ms,
"repository_path": "gs://test_bucket/test_dir",
},
)
@parameterized.parameters("127.0.0.1", "localhost", "192.168.1.1")
def test_collect_profile_host(self, host):
profiling.collect_profile(
port=8000,
duration_ms=1000,
host=host,
log_dir="gs://test_bucket/test_dir",
)
self.mock_post.assert_called_once_with(
f"http://{host}:8000/profiling",
json={
"duration_ms": 1000,
"repository_path": "gs://test_bucket/test_dir",
},
)
@parameterized.parameters(
"gs://test_bucket/test_log_dir",
"gs://test_bucket2",
"gs://test_bucket3/test/log/dir",
)
def test_collect_profile_log_dir(self, log_dir):
profiling.collect_profile(
port=8000, duration_ms=1000, host="127.0.0.1", log_dir=log_dir
)
self.mock_post.assert_called_once_with(
"http://127.0.0.1:8000/profiling",
json={
"duration_ms": 1000,
"repository_path": log_dir,
},
)
@parameterized.parameters("/logs/test_log_dir", "relative_path/my_log_dir")
def test_collect_profile_log_dir_error(self, log_dir):
with self.assertRaises(ValueError):
profiling.collect_profile(
port=8000, duration_ms=1000, host="127.0.0.1", log_dir=log_dir
)
@parameterized.parameters(
requests.exceptions.ConnectionError,
requests.exceptions.Timeout,
requests.exceptions.TooManyRedirects,
requests.exceptions.RequestException,
requests.exceptions.HTTPError,
)
def test_collect_profile_request_error(self, exception_type):
self.mock_post.side_effect = exception_type
result = profiling.collect_profile(
port=8000,
duration_ms=1000,
host="127.0.0.1",
log_dir="gs://test_bucket/test_dir",
)
self.assertFalse(result)
self.mock_post.assert_called_once()
def test_collect_profile_success(self):
mock_response = mock.Mock()
mock_response.raise_for_status.return_value = None
self.mock_post.return_value = mock_response
result = profiling.collect_profile(
port=8000,
duration_ms=1000,
host="127.0.0.1",
log_dir="gs://test_bucket/test_dir",
)
self.assertTrue(result)
self.mock_post.assert_called_once()
mock_response.raise_for_status.assert_called_once()
if __name__ == "__main__":
absltest.main()