Skip to content

Commit c1169be

Browse files
author
Yi-Ting Lee
committed
validation logic clean on integ tests
1 parent 7cec38d commit c1169be

File tree

3 files changed

+95
-126
lines changed

3 files changed

+95
-126
lines changed

tests/integ/sagemaker/lineage/conftest.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import pytest
2020
import logging
2121
import uuid
22+
import json
2223
from sagemaker.lineage import (
2324
action,
2425
context,
@@ -891,3 +892,17 @@ def _deploy_static_endpoint(execution_arn, sagemaker_session):
891892
pass
892893
else:
893894
raise (e)
895+
896+
897+
@pytest.fixture
898+
def extract_data_from_html():
899+
def _method(data):
900+
start = data.find("[")
901+
end = data.find("]")
902+
res = data[start + 1 : end].split("}, ")
903+
res = [i + "}" for i in res]
904+
res[-1] = res[-1][:-1]
905+
data_dict = [json.loads(i) for i in res]
906+
return data_dict
907+
908+
return _method

tests/integ/sagemaker/lineage/helpers.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,27 +145,23 @@ def clean_all(self):
145145
for source, dest in self.associations:
146146
try:
147147
self.client.delete_association(SourceArn=source, DestinationArn=dest)
148-
time.sleep(0.5)
149148
except Exception as e:
150149
print("skipped " + str(e))
151150

152151
for artifact_arn in self.artifacts:
153152
try:
154153
self.client.delete_artifact(ArtifactArn=artifact_arn)
155-
time.sleep(0.5)
156154
except Exception as e:
157155
print("skipped " + str(e))
158156

159157
for action_arn in self.actions:
160158
try:
161159
self.client.delete_action(ActionArn=action_arn)
162-
time.sleep(0.5)
163160
except Exception as e:
164161
print("skipped " + str(e))
165162

166163
for context_arn in self.contexts:
167164
try:
168165
self.client.delete_context(ContextArn=context_arn)
169-
time.sleep(0.5)
170166
except Exception as e:
171167
print("skipped " + str(e))

tests/integ/sagemaker/lineage/test_lineage_visualize.py

Lines changed: 80 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
"""This module contains code to test SageMaker ``LineageQueryResult.visualize()``"""
1414
from __future__ import absolute_import
1515
import time
16-
import json
1716
import os
1817

1918
import pytest
@@ -47,26 +46,22 @@ def test_wide_graph_visualize(sagemaker_session):
4746
# \ \--> Artifact
4847
# \---> ...
4948
try:
50-
for i in range(10):
49+
for i in range(200):
5150
artifact_arn = lineage_resource_helper.create_artifact(artifact_name=name())
5251
lineage_resource_helper.create_association(
5352
source_arn=wide_graph_root_arn, dest_arn=artifact_arn
5453
)
55-
except Exception as e:
56-
print(e)
57-
lineage_resource_helper.clean_all()
58-
assert False
5954

60-
try:
6155
lq = sagemaker.lineage.query.LineageQuery(sagemaker_session)
6256
lq_result = lq.query(start_arns=[wide_graph_root_arn])
6357
lq_result.visualize(path="wideGraph.html")
58+
6459
except Exception as e:
6560
print(e)
66-
lineage_resource_helper.clean_all()
6761
assert False
6862

69-
lineage_resource_helper.clean_all()
63+
finally:
64+
lineage_resource_helper.clean_all()
7065

7166

7267
@pytest.mark.skip("visualizer load test")
@@ -84,27 +79,23 @@ def test_long_graph_visualize(sagemaker_session):
8479
source_arn=last_arn, dest_arn=new_artifact_arn
8580
)
8681
last_arn = new_artifact_arn
87-
except Exception as e:
88-
print(e)
89-
lineage_resource_helper.clean_all()
90-
assert False
9182

92-
try:
9383
lq = sagemaker.lineage.query.LineageQuery(sagemaker_session)
9484
lq_result = lq.query(
9585
start_arns=[long_graph_root_arn], direction=LineageQueryDirectionEnum.DESCENDANTS
9686
)
9787
# max depth = 10 -> graph rendered only has length of ten (in DESCENDANTS direction)
9888
lq_result.visualize(path="longGraph.html")
89+
9990
except Exception as e:
10091
print(e)
101-
lineage_resource_helper.clean_all()
10292
assert False
10393

104-
lineage_resource_helper.clean_all()
94+
finally:
95+
lineage_resource_helper.clean_all()
10596

10697

107-
def test_graph_visualize(sagemaker_session):
98+
def test_graph_visualize(sagemaker_session, extract_data_from_html):
10899
lineage_resource_helper = LineageResourceHelper(sagemaker_session=sagemaker_session)
109100

110101
# create lineage data
@@ -141,24 +132,14 @@ def test_graph_visualize(sagemaker_session):
141132
dest_arn=endpoint_context,
142133
association_type="AssociatedWith",
143134
)
144-
time.sleep(1)
145-
except Exception as e:
146-
print(e)
147-
lineage_resource_helper.clean_all()
148-
assert False
135+
time.sleep(3)
149136

150-
# visualize
151-
try:
137+
# visualize
152138
lq = sagemaker.lineage.query.LineageQuery(sagemaker_session)
153139
lq_result = lq.query(start_arns=[graph_startarn])
154140
lq_result.visualize(path="testGraph.html")
155-
except Exception as e:
156-
print(e)
157-
lineage_resource_helper.clean_all()
158-
assert False
159141

160-
# check generated graph info
161-
try:
142+
# check generated graph info
162143
fo = open("testGraph.html", "r")
163144
lines = fo.readlines()
164145
for line in lines:
@@ -167,108 +148,85 @@ def test_graph_visualize(sagemaker_session):
167148
if "edges = " in line:
168149
edge = line
169150

170-
# extract node data
171-
start = node.find("[")
172-
end = node.find("]")
173-
res = node[start + 1 : end].split("}, ")
174-
res = [i + "}" for i in res]
175-
res[-1] = res[-1][:-1]
176-
node_dict = [json.loads(i) for i in res]
177-
178-
# extract edge data
179-
start = edge.find("[")
180-
end = edge.find("]")
181-
res = edge[start + 1 : end].split("}, ")
182-
res = [i + "}" for i in res]
183-
res[-1] = res[-1][:-1]
184-
edge_dict = [json.loads(i) for i in res]
151+
node_dict = extract_data_from_html(node)
152+
edge_dict = extract_data_from_html(edge)
185153

186154
# check node number
187155
assert len(node_dict) == 5
188156

189-
# check startarn
190-
found_value = next(
191-
dictionary for dictionary in node_dict if dictionary["id"] == graph_startarn
192-
)
193-
assert found_value["color"] == "#146eb4"
194-
assert found_value["label"] == "Model"
195-
assert found_value["shape"] == "star"
196-
assert found_value["title"] == "Artifact"
197-
198-
# check image artifact
199-
found_value = next(
200-
dictionary for dictionary in node_dict if dictionary["id"] == image_artifact
201-
)
202-
assert found_value["color"] == "#146eb4"
203-
assert found_value["label"] == "Image"
204-
assert found_value["shape"] == "dot"
205-
assert found_value["title"] == "Artifact"
206-
207-
# check dataset artifact
208-
found_value = next(
209-
dictionary for dictionary in node_dict if dictionary["id"] == dataset_artifact
210-
)
211-
assert found_value["color"] == "#146eb4"
212-
assert found_value["label"] == "DataSet"
213-
assert found_value["shape"] == "dot"
214-
assert found_value["title"] == "Artifact"
215-
216-
# check modeldeploy action
217-
found_value = next(
218-
dictionary for dictionary in node_dict if dictionary["id"] == modeldeploy_action
219-
)
220-
assert found_value["color"] == "#88c396"
221-
assert found_value["label"] == "ModelDeploy"
222-
assert found_value["shape"] == "dot"
223-
assert found_value["title"] == "Action"
224-
225-
# check endpoint context
226-
found_value = next(
227-
dictionary for dictionary in node_dict if dictionary["id"] == endpoint_context
228-
)
229-
assert found_value["color"] == "#ff9900"
230-
assert found_value["label"] == "Endpoint"
231-
assert found_value["shape"] == "dot"
232-
assert found_value["title"] == "Context"
157+
expected_nodes = {
158+
graph_startarn: {
159+
"color": "#146eb4",
160+
"label": "Model",
161+
"shape": "star",
162+
"title": "Artifact",
163+
},
164+
image_artifact: {
165+
"color": "#146eb4",
166+
"label": "Image",
167+
"shape": "dot",
168+
"title": "Artifact",
169+
},
170+
dataset_artifact: {
171+
"color": "#146eb4",
172+
"label": "DataSet",
173+
"shape": "dot",
174+
"title": "Artifact",
175+
},
176+
modeldeploy_action: {
177+
"color": "#88c396",
178+
"label": "ModelDeploy",
179+
"shape": "dot",
180+
"title": "Action",
181+
},
182+
endpoint_context: {
183+
"color": "#ff9900",
184+
"label": "Endpoint",
185+
"shape": "dot",
186+
"title": "Context",
187+
},
188+
}
189+
190+
# check node properties
191+
for node in node_dict:
192+
for label, val in expected_nodes[node["id"]].items():
193+
assert node[label] == val
233194

234195
# check edge number
235196
assert len(edge_dict) == 4
236197

237-
# check image_artifact -> model_artifact(startarn) edge
238-
found_value = next(
239-
dictionary for dictionary in edge_dict if dictionary["from"] == image_artifact
240-
)
241-
assert found_value["to"] == graph_startarn
242-
assert found_value["title"] == "ContributedTo"
243-
244-
# check dataset_artifact -> model_artifact(startarn) edge
245-
found_value = next(
246-
dictionary for dictionary in edge_dict if dictionary["from"] == dataset_artifact
247-
)
248-
assert found_value["to"] == graph_startarn
249-
assert found_value["title"] == "AssociatedWith"
250-
251-
# check model_artifact(startarn) -> modeldeploy_action edge
252-
found_value = next(
253-
dictionary for dictionary in edge_dict if dictionary["from"] == graph_startarn
254-
)
255-
assert found_value["to"] == modeldeploy_action
256-
assert found_value["title"] == "ContributedTo"
257-
258-
# check modeldeploy_action -> endpoint_context edge
259-
found_value = next(
260-
dictionary for dictionary in edge_dict if dictionary["from"] == modeldeploy_action
261-
)
262-
assert found_value["to"] == endpoint_context
263-
assert found_value["title"] == "AssociatedWith"
198+
expected_edges = {
199+
image_artifact: {
200+
"from": image_artifact,
201+
"to": graph_startarn,
202+
"title": "ContributedTo",
203+
},
204+
dataset_artifact: {
205+
"from": dataset_artifact,
206+
"to": graph_startarn,
207+
"title": "AssociatedWith",
208+
},
209+
graph_startarn: {
210+
"from": graph_startarn,
211+
"to": modeldeploy_action,
212+
"title": "ContributedTo",
213+
},
214+
modeldeploy_action: {
215+
"from": modeldeploy_action,
216+
"to": endpoint_context,
217+
"title": "AssociatedWith",
218+
},
219+
}
220+
221+
# check edge properties
222+
for edge in edge_dict:
223+
for label, val in expected_edges[edge["from"]].items():
224+
assert edge[label] == val
264225

265226
except Exception as e:
266227
print(e)
267-
lineage_resource_helper.clean_all()
268-
os.remove("testGraph.html")
269228
assert False
270229

271-
# delete generated test graph
272-
os.remove("testGraph.html")
273-
# clean lineage data
274-
lineage_resource_helper.clean_all()
230+
finally:
231+
lineage_resource_helper.clean_all()
232+
os.remove("testGraph.html")

0 commit comments

Comments
 (0)