-
Notifications
You must be signed in to change notification settings - Fork 242
/
Copy pathtest_spark_tools.py
181 lines (168 loc) · 5.8 KB
/
test_spark_tools.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
import sys
from unittest import mock
import pytest
from paasta_tools import spark_tools
from paasta_tools.spark_tools import auto_add_timeout_for_spark_job
def test_get_webui_url():
with mock.patch("socket.getfqdn", return_value="1.2.3.4"):
assert spark_tools.get_webui_url("1234") == "http://1.2.3.4:1234"
@pytest.mark.parametrize(
"cmd,expected",
[
("spark-shell", "spark-shell --conf spark.max.cores=100"),
(
"/venv/bin/pyspark test.py",
"/venv/bin/pyspark --conf spark.max.cores=100 test.py",
),
(
"spark-submit script.py --other args",
"spark-submit --conf spark.max.cores=100 script.py --other args",
),
("history-server", "history-server"),
],
)
def test_inject_spark_conf_str(cmd, expected):
assert (
spark_tools.inject_spark_conf_str(cmd, "--conf spark.max.cores=100") == expected
)
@pytest.mark.parametrize(
"spark_conf,expected",
[
(
{
"spark.kubernetes.executor.volumes.hostPath.nailsrv-123.mount.path": "/nail/srv",
"spark.kubernetes.executor.volumes.hostPath.nailsrv-123.options.path": "/nail/srv",
"spark.kubernetes.executor.volumes.hostPath.nailsrv-123.mount.readOnly": "true",
"spark.kubernetes.executor.volumes.hostPath.123.mount.path": "/nail/123",
"spark.kubernetes.executor.volumes.hostPath.123.options.path": "/nail/123",
"spark.kubernetes.executor.volumes.hostPath.123.mount.readOnly": "false",
},
["/nail/srv:/nail/srv:ro", "/nail/123:/nail/123:rw"],
),
(
{
"spark.kubernetes.executor.volumes.hostPath.NAILsrv-123.mount.path": "/one/two",
"spark.kubernetes.executor.volumes.hostPath.NAILsrv-123.options.path": "/one/two",
"spark.kubernetes.executor.volumes.hostPath.NAILsrv-123.mount.readOnly": "true",
},
[""],
),
],
)
@mock.patch.object(sys, "exit")
def test_get_volumes_from_spark_k8s_configs(mock_sys, spark_conf, expected):
result = spark_tools.get_volumes_from_spark_k8s_configs(spark_conf)
if (
"spark.kubernetes.executor.volumes.hostPath.NAILsrv-123.mount.path"
in spark_conf
):
mock_sys.assert_called_with(1)
else:
assert result == expected
@pytest.mark.parametrize(
"spark_config,expected",
[
# Empty config
({}, {}),
# Only UI port specified
(
{"spark.ui.port": "4040"},
{
"prometheus.io/port": "4040",
"prometheus.io/path": "/metrics/prometheus",
},
),
# Only service and instance specified
(
{
"spark.kubernetes.executor.annotation.paasta.yelp.com/service": "my-service",
"spark.kubernetes.executor.annotation.paasta.yelp.com/instance": "my-instance",
},
{
"paasta.yelp.com/service": "my-service",
"paasta.yelp.com/instance": "my-instance",
},
),
# All annotations specified
(
{
"spark.ui.port": "4040",
"spark.kubernetes.executor.annotation.paasta.yelp.com/service": "my-service",
"spark.kubernetes.executor.annotation.paasta.yelp.com/instance": "my-instance",
},
{
"prometheus.io/port": "4040",
"prometheus.io/path": "/metrics/prometheus",
"paasta.yelp.com/service": "my-service",
"paasta.yelp.com/instance": "my-instance",
},
),
# Missing service
(
{
"spark.ui.port": "4040",
"spark.kubernetes.executor.annotation.paasta.yelp.com/instance": "my-instance",
},
{
"prometheus.io/port": "4040",
"prometheus.io/path": "/metrics/prometheus",
},
),
# Missing instance
(
{
"spark.ui.port": "4040",
"spark.kubernetes.executor.annotation.paasta.yelp.com/service": "my-service",
},
{
"prometheus.io/port": "4040",
"prometheus.io/path": "/metrics/prometheus",
},
),
],
)
def test_get_spark_driver_monitoring_annotations(spark_config, expected):
result = spark_tools.get_spark_driver_monitoring_annotations(spark_config)
assert result == expected
@pytest.mark.parametrize(
argnames=[
"cmd",
"timeout_duration",
"expected",
],
argvalues=[
pytest.param(
"spark-submit abc.py",
"4h",
"timeout 4h spark-submit abc.py",
id="No timeout",
),
pytest.param(
"timeout 2h spark-submit abc.py",
"12h",
"timeout 2h spark-submit abc.py",
id="Timeout without options",
),
pytest.param(
"timeout -v 2h spark-submit abc.py",
"12h",
"timeout -v 2h spark-submit abc.py",
id="Timeout with options",
),
pytest.param(
"timeout -v -s 1 2h spark-submit abc.py",
"12h",
"timeout -v -s 1 2h spark-submit abc.py",
id="Timeout with multiple options",
),
pytest.param(
"timeout -k 10m --signal=SIGKILL 2h spark-submit abc.py",
"12h",
"timeout -k 10m --signal=SIGKILL 2h spark-submit abc.py",
id="Timeout with double dash option",
),
],
)
def test_auto_add_timeout_for_spark_job(cmd, timeout_duration, expected):
result = auto_add_timeout_for_spark_job(cmd, timeout_duration)
assert result == expected