12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
+ import logging
15
16
from unittest import mock
16
17
17
18
from pathwaysutils import profiling
@@ -32,13 +33,14 @@ def setUp(self):
32
33
33
34
@parameterized .parameters (8000 , 1234 )
34
35
def test_collect_profile_port (self , port ):
35
- profiling .collect_profile (
36
+ result = profiling .collect_profile (
36
37
port = port ,
37
38
duration_ms = 1000 ,
38
39
host = "127.0.0.1" ,
39
40
log_dir = "gs://test_bucket/test_dir" ,
40
41
)
41
42
43
+ self .assertTrue (result )
42
44
self .mock_post .assert_called_once_with (
43
45
f"http://127.0.0.1:{ port } /profiling" ,
44
46
json = {
@@ -49,13 +51,14 @@ def test_collect_profile_port(self, port):
49
51
50
52
@parameterized .parameters (1000 , 1234 )
51
53
def test_collect_profile_duration_ms (self , duration_ms ):
52
- profiling .collect_profile (
54
+ result = profiling .collect_profile (
53
55
port = 8000 ,
54
56
duration_ms = duration_ms ,
55
57
host = "127.0.0.1" ,
56
58
log_dir = "gs://test_bucket/test_dir" ,
57
59
)
58
60
61
+ self .assertTrue (result )
59
62
self .mock_post .assert_called_once_with (
60
63
"http://127.0.0.1:8000/profiling" ,
61
64
json = {
@@ -66,13 +69,14 @@ def test_collect_profile_duration_ms(self, duration_ms):
66
69
67
70
@parameterized .parameters ("127.0.0.1" , "localhost" , "192.168.1.1" )
68
71
def test_collect_profile_host (self , host ):
69
- profiling .collect_profile (
72
+ result = profiling .collect_profile (
70
73
port = 8000 ,
71
74
duration_ms = 1000 ,
72
75
host = host ,
73
76
log_dir = "gs://test_bucket/test_dir" ,
74
77
)
75
78
79
+ self .assertTrue (result )
76
80
self .mock_post .assert_called_once_with (
77
81
f"http://{ host } :8000/profiling" ,
78
82
json = {
@@ -87,10 +91,11 @@ def test_collect_profile_host(self, host):
87
91
"gs://test_bucket3/test/log/dir" ,
88
92
)
89
93
def test_collect_profile_log_dir (self , log_dir ):
90
- profiling .collect_profile (
94
+ result = profiling .collect_profile (
91
95
port = 8000 , duration_ms = 1000 , host = "127.0.0.1" , log_dir = log_dir
92
96
)
93
97
98
+ self .assertTrue (result )
94
99
self .mock_post .assert_called_once_with (
95
100
"http://127.0.0.1:8000/profiling" ,
96
101
json = {
@@ -107,22 +112,27 @@ def test_collect_profile_log_dir_error(self, log_dir):
107
112
)
108
113
109
114
@parameterized .parameters (
110
- requests .exceptions .ConnectionError ,
111
- requests .exceptions .Timeout ,
112
- requests .exceptions .TooManyRedirects ,
113
- requests .exceptions .RequestException ,
114
- requests .exceptions .HTTPError ,
115
+ requests .exceptions .ConnectionError ( "Connection error" ) ,
116
+ requests .exceptions .Timeout ( "Timeout" ) ,
117
+ requests .exceptions .TooManyRedirects ( "Too many redirects" ) ,
118
+ requests .exceptions .RequestException ( "Request exception" ) ,
119
+ requests .exceptions .HTTPError ( "HTTP error" ) ,
115
120
)
116
- def test_collect_profile_request_error (self , exception_type ):
117
- self .mock_post .side_effect = exception_type
121
+ def test_collect_profile_request_error (self , exception ):
122
+ self .mock_post .side_effect = exception
123
+
124
+ with self .assertLogs (profiling ._logger , level = logging .ERROR ) as logs :
125
+ result = profiling .collect_profile (
126
+ port = 8000 ,
127
+ duration_ms = 1000 ,
128
+ host = "127.0.0.1" ,
129
+ log_dir = "gs://test_bucket/test_dir" ,
130
+ )
118
131
119
- result = profiling .collect_profile (
120
- port = 8000 ,
121
- duration_ms = 1000 ,
122
- host = "127.0.0.1" ,
123
- log_dir = "gs://test_bucket/test_dir" ,
132
+ self .assertLen (logs .output , 1 )
133
+ self .assertIn (
134
+ f"Failed to collect profiling data: { exception } " , logs .output [0 ]
124
135
)
125
-
126
136
self .assertFalse (result )
127
137
self .mock_post .assert_called_once ()
128
138
0 commit comments