@@ -1153,6 +1153,94 @@ def test_add_alerts() -> None:
11531153 )
11541154 for _id in _expected_alerts :
11551155 client .delete_alert (_id )
1156+
1157+ @pytest .mark .run
1158+ @pytest .mark .offline
1159+ def test_add_alerts_offline (monkeypatch ) -> None :
1160+ _uuid = f"{ uuid .uuid4 ()} " .split ("-" )[0 ]
1161+
1162+ temp_d = tempfile .TemporaryDirectory ()
1163+ monkeypatch .setenv ("SIMVUE_OFFLINE_DIRECTORY" , temp_d .name )
1164+
1165+ run = sv_run .Run (mode = "offline" )
1166+ run .init (
1167+ name = "test_add_alerts_offline" ,
1168+ folder = f"/simvue_unit_testing/{ _uuid } " ,
1169+ retention_period = os .environ .get ("SIMVUE_TESTING_RETENTION_PERIOD" , "2 mins" ),
1170+ tags = [platform .system (), "test_add_alerts" ],
1171+ visibility = "tenant" if os .environ .get ("CI" ) else None ,
1172+ )
1173+
1174+ _expected_alerts = []
1175+
1176+ # Create alerts, have them attach to run automatically
1177+ _id = run .create_event_alert (
1178+ name = f"event_alert_{ _uuid } " ,
1179+ pattern = "test" ,
1180+ )
1181+ _expected_alerts .append (_id )
1182+
1183+ # Create another alert and attach to run
1184+ _id = run .create_metric_range_alert (
1185+ name = f"metric_range_alert_{ _uuid } " ,
1186+ metric = "test" ,
1187+ range_low = 10 ,
1188+ range_high = 100 ,
1189+ rule = "is inside range" ,
1190+ )
1191+ _expected_alerts .append (_id )
1192+
1193+ # Create another alert, do not attach to run
1194+ _id = run .create_metric_threshold_alert (
1195+ name = f"metric_threshold_alert_{ _uuid } " ,
1196+ metric = "test" ,
1197+ threshold = 10 ,
1198+ rule = "is above" ,
1199+ attach_to_run = False ,
1200+ )
1201+
1202+ # Try redefining existing alert again
1203+ _id = run .create_metric_range_alert (
1204+ name = f"metric_range_alert_{ _uuid } " ,
1205+ metric = "test" ,
1206+ range_low = 10 ,
1207+ range_high = 100 ,
1208+ rule = "is inside range" ,
1209+ )
1210+
1211+ _id_mapping = sv_send .sender (os .environ ["SIMVUE_OFFLINE_DIRECTORY" ], 2 , 10 , throw_exceptions = True )
1212+ _online_run = RunObject (identifier = _id_mapping .get (run .id ))
1213+
1214+ # Check that there is no duplication
1215+ assert sorted (_online_run .alerts ) == sorted ([_id_mapping .get (_id ) for _id in _expected_alerts ])
1216+
1217+ # Create another run without adding to run
1218+ _id = run .create_user_alert (name = f"user_alert_{ _uuid } " , attach_to_run = False )
1219+ _id_mapping = sv_send .sender (os .environ ["SIMVUE_OFFLINE_DIRECTORY" ], 2 , 10 , throw_exceptions = True )
1220+
1221+ # Check alert is not added
1222+ _online_run .refresh ()
1223+ assert sorted (_online_run .alerts ) == sorted ([_id_mapping .get (_id ) for _id in _expected_alerts ])
1224+
1225+ # Try adding alerts with IDs, check there is no duplication
1226+ _expected_alerts .append (_id )
1227+ run .add_alerts (ids = _expected_alerts )
1228+ _id_mapping = sv_send .sender (os .environ ["SIMVUE_OFFLINE_DIRECTORY" ], 2 , 10 , throw_exceptions = True )
1229+
1230+ _online_run .refresh ()
1231+ assert sorted (_online_run .alerts ) == sorted ([_id_mapping .get (_id ) for _id in _expected_alerts ])
1232+
1233+ run .close ()
1234+
1235+ client = sv_cl .Client ()
1236+ with contextlib .suppress (ObjectNotFoundError ):
1237+ client .delete_folder (
1238+ f"/simvue_unit_testing/{ _uuid } " ,
1239+ remove_runs = True ,
1240+ recursive = True
1241+ )
1242+ for _id in [_id_mapping .get (_id ) for _id in _expected_alerts ]:
1243+ client .delete_alert (_id )
11561244
11571245
11581246@pytest .mark .run
@@ -1439,7 +1527,7 @@ def test_run_environment_metadata(environment: str, mocker: pytest_mock.MockerFi
14391527 _target_dir = _data_dir
14401528 if "python" in environment :
14411529 _target_dir = _data_dir .joinpath (environment )
1442- _config = SimvueConfiguration .fetch ()
1530+ _config = SimvueConfiguration .fetch (mode = "online" )
14431531
14441532 with sv_run .Run (server_token = _config .server .token , server_url = _config .server .url ) as run :
14451533 _uuid = f"{ uuid .uuid4 ()} " .split ("-" )[0 ]
0 commit comments