@@ -1167,6 +1167,94 @@ def test_add_alerts() -> None:
11671167 )
11681168 for _id in _expected_alerts :
11691169 client .delete_alert (_id )
1170+
1171+ @pytest .mark .run
1172+ @pytest .mark .offline
1173+ def test_add_alerts_offline (monkeypatch ) -> None :
1174+ _uuid = f"{ uuid .uuid4 ()} " .split ("-" )[0 ]
1175+
1176+ temp_d = tempfile .TemporaryDirectory ()
1177+ monkeypatch .setenv ("SIMVUE_OFFLINE_DIRECTORY" , temp_d .name )
1178+
1179+ run = sv_run .Run (mode = "offline" )
1180+ run .init (
1181+ name = "test_add_alerts_offline" ,
1182+ folder = f"/simvue_unit_testing/{ _uuid } " ,
1183+ retention_period = os .environ .get ("SIMVUE_TESTING_RETENTION_PERIOD" , "2 mins" ),
1184+ tags = [platform .system (), "test_add_alerts" ],
1185+ visibility = "tenant" if os .environ .get ("CI" ) else None ,
1186+ )
1187+
1188+ _expected_alerts = []
1189+
1190+ # Create alerts, have them attach to run automatically
1191+ _id = run .create_event_alert (
1192+ name = f"event_alert_{ _uuid } " ,
1193+ pattern = "test" ,
1194+ )
1195+ _expected_alerts .append (_id )
1196+
1197+ # Create another alert and attach to run
1198+ _id = run .create_metric_range_alert (
1199+ name = f"metric_range_alert_{ _uuid } " ,
1200+ metric = "test" ,
1201+ range_low = 10 ,
1202+ range_high = 100 ,
1203+ rule = "is inside range" ,
1204+ )
1205+ _expected_alerts .append (_id )
1206+
1207+ # Create another alert, do not attach to run
1208+ _id = run .create_metric_threshold_alert (
1209+ name = f"metric_threshold_alert_{ _uuid } " ,
1210+ metric = "test" ,
1211+ threshold = 10 ,
1212+ rule = "is above" ,
1213+ attach_to_run = False ,
1214+ )
1215+
1216+ # Try redefining existing alert again
1217+ _id = run .create_metric_range_alert (
1218+ name = f"metric_range_alert_{ _uuid } " ,
1219+ metric = "test" ,
1220+ range_low = 10 ,
1221+ range_high = 100 ,
1222+ rule = "is inside range" ,
1223+ )
1224+
1225+ _id_mapping = sv_send .sender (os .environ ["SIMVUE_OFFLINE_DIRECTORY" ], 2 , 10 , throw_exceptions = True )
1226+ _online_run = RunObject (identifier = _id_mapping .get (run .id ))
1227+
1228+ # Check that there is no duplication
1229+ assert sorted (_online_run .alerts ) == sorted ([_id_mapping .get (_id ) for _id in _expected_alerts ])
1230+
1231+ # Create another run without adding to run
1232+ _id = run .create_user_alert (name = f"user_alert_{ _uuid } " , attach_to_run = False )
1233+ _id_mapping = sv_send .sender (os .environ ["SIMVUE_OFFLINE_DIRECTORY" ], 2 , 10 , throw_exceptions = True )
1234+
1235+ # Check alert is not added
1236+ _online_run .refresh ()
1237+ assert sorted (_online_run .alerts ) == sorted ([_id_mapping .get (_id ) for _id in _expected_alerts ])
1238+
1239+ # Try adding alerts with IDs, check there is no duplication
1240+ _expected_alerts .append (_id )
1241+ run .add_alerts (ids = _expected_alerts )
1242+ _id_mapping = sv_send .sender (os .environ ["SIMVUE_OFFLINE_DIRECTORY" ], 2 , 10 , throw_exceptions = True )
1243+
1244+ _online_run .refresh ()
1245+ assert sorted (_online_run .alerts ) == sorted ([_id_mapping .get (_id ) for _id in _expected_alerts ])
1246+
1247+ run .close ()
1248+
1249+ client = sv_cl .Client ()
1250+ with contextlib .suppress (ObjectNotFoundError ):
1251+ client .delete_folder (
1252+ f"/simvue_unit_testing/{ _uuid } " ,
1253+ remove_runs = True ,
1254+ recursive = True
1255+ )
1256+ for _id in [_id_mapping .get (_id ) for _id in _expected_alerts ]:
1257+ client .delete_alert (_id )
11701258
11711259
11721260@pytest .mark .run
@@ -1457,7 +1545,7 @@ def test_run_environment_metadata(environment: str, mocker: pytest_mock.MockerFi
14571545 _target_dir = _data_dir
14581546 if "python" in environment :
14591547 _target_dir = _data_dir .joinpath (environment )
1460- _config = SimvueConfiguration .fetch ()
1548+ _config = SimvueConfiguration .fetch (mode = "online" )
14611549
14621550 with sv_run .Run (server_token = _config .server .token , server_url = _config .server .url ) as run :
14631551 _uuid = f"{ uuid .uuid4 ()} " .split ("-" )[0 ]
0 commit comments