-
Notifications
You must be signed in to change notification settings - Fork 126
/
test_armada_deferrable_operator.py
171 lines (143 loc) · 5.02 KB
/
test_armada_deferrable_operator.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
import copy
import pytest
from armada_client.armada import submit_pb2
from armada_client.k8s.io.api.core.v1 import generated_pb2 as core_v1
from armada_client.k8s.io.apimachinery.pkg.api.resource import (
generated_pb2 as api_resource,
)
from armada.operators.armada_deferrable import ArmadaDeferrableOperator
from armada.operators.grpc import CredentialsCallback
def test_serialize_armada_deferrable():
grpc_chan_args = {
"target": "localhost:443",
"credentials_callback_args": {
"module_name": "channel_test",
"function_name": "get_credentials",
"function_kwargs": {
"example_arg": "test",
},
},
}
pod = core_v1.PodSpec(
containers=[
core_v1.Container(
name="sleep",
image="busybox",
args=["sleep", "10s"],
securityContext=core_v1.SecurityContext(runAsUser=1000),
resources=core_v1.ResourceRequirements(
requests={
"cpu": api_resource.Quantity(string="120m"),
"memory": api_resource.Quantity(string="510Mi"),
},
limits={
"cpu": api_resource.Quantity(string="120m"),
"memory": api_resource.Quantity(string="510Mi"),
},
),
)
],
)
job_requests = [
submit_pb2.JobSubmitRequestItem(
priority=1,
pod_spec=pod,
namespace="personal-anonymous",
annotations={"armadaproject.io/hello": "world"},
)
]
source = ArmadaDeferrableOperator(
task_id="test_task_id",
name="test task",
armada_channel_args=grpc_chan_args,
job_service_channel_args=grpc_chan_args,
armada_queue="test-queue",
job_request_items=job_requests,
lookout_url_template="https://lookout.test.domain/",
poll_interval=5,
)
serialized = source.serialize()
assert serialized["name"] == source.name
reconstituted = ArmadaDeferrableOperator(**serialized)
assert reconstituted == source
get_lookout_url_test_cases = [
(
"http://localhost:8089/jobs?job_id=<job_id>",
"test_id",
"http://localhost:8089/jobs?job_id=test_id",
),
(
"https://lookout.armada.domain/jobs?job_id=<job_id>",
"test_id",
"https://lookout.armada.domain/jobs?job_id=test_id",
),
("", "test_id", ""),
(None, "test_id", ""),
]
@pytest.mark.parametrize(
"lookout_url_template, job_id, expected_url", get_lookout_url_test_cases
)
def test_get_lookout_url(lookout_url_template, job_id, expected_url):
armada_channel_args = {"target": "127.0.0.1:50051"}
job_service_channel_args = {"target": "127.0.0.1:60003"}
operator = ArmadaDeferrableOperator(
task_id="test_task_id",
name="test_task",
armada_channel_args=armada_channel_args,
job_service_channel_args=job_service_channel_args,
armada_queue="test_queue",
job_request_items=[],
lookout_url_template=lookout_url_template,
)
assert operator._get_lookout_url(job_id) == expected_url
def test_deepcopy_operator():
armada_channel_args = {"target": "127.0.0.1:50051"}
job_service_channel_args = {"target": "127.0.0.1:60003"}
operator = ArmadaDeferrableOperator(
task_id="test_task_id",
name="test_task",
armada_channel_args=armada_channel_args,
job_service_channel_args=job_service_channel_args,
armada_queue="test_queue",
job_request_items=[],
lookout_url_template="http://localhost:8089/jobs?job_id=<job_id>",
)
try:
copy.deepcopy(operator)
except Exception as e:
assert False, f"{e}"
def test_deepcopy_operator_with_grpc_credentials_callback():
armada_channel_args = {
"target": "127.0.0.1:50051",
"credentials_callback_args": {
"module_name": "tests.unit.test_armada_operator",
"function_name": "__example_test_callback",
"function_kwargs": {
"test_arg": "fake_arg",
},
},
}
job_service_channel_args = {"target": "127.0.0.1:60003"}
operator = ArmadaDeferrableOperator(
task_id="test_task_id",
name="test_task",
armada_channel_args=armada_channel_args,
job_service_channel_args=job_service_channel_args,
armada_queue="test_queue",
job_request_items=[],
lookout_url_template="http://localhost:8089/jobs?job_id=<job_id>",
)
try:
copy.deepcopy(operator)
except Exception as e:
assert False, f"{e}"
def __example_test_callback(foo=None):
return f"fake_cred {foo}"
def test_credentials_callback():
callback = CredentialsCallback(
module_name="test_armada_operator",
function_name="__example_test_callback",
function_kwargs={"foo": "bar"},
)
result = callback.call()
assert result == "fake_cred bar"