1616 validate_python_activity
1717from chaoslib .provider .process import run_process_activity , \
1818 validate_process_activity
19- from chaoslib .types import Activity , Configuration , Experiment , Run , Secrets
19+ from chaoslib .types import Activity , Configuration , Experiment , Run , Secrets , \
20+ ValidationError
21+ from chaoslib .validation import Validation
2022
2123
2224__all__ = ["ensure_activity_is_valid" , "get_all_activities_in_experiment" ,
2325 "run_activities" ]
2426
2527
26- def ensure_activity_is_valid (activity : Activity ):
28+ def ensure_activity_is_valid (activity : Activity ) -> List [ ValidationError ] :
2729 """
2830 Goes through the activity and checks certain of its properties and raise
2931 :exc:`InvalidActivity` whenever one does not respect the expectations.
@@ -34,75 +36,80 @@ def ensure_activity_is_valid(activity: Activity):
3436
3537 Depending on the type, an activity requires a variety of other keys.
3638
37- In all failing cases, raises :exc:`InvalidActivity` .
39+ In all failing cases, returns a list of validation errors .
3840 """
39- errors = []
41+ v = Validation ()
4042 if not activity :
41- errors . append ( InvalidActivity ( " empty activity is no activity") )
42- return errors
43+ v . add_error ( "activity" , " empty activity is no activity" )
44+ return v . errors ()
4345
4446 # when the activity is just a ref, there is little to validate
4547 ref = activity .get ("ref" )
4648 if ref is not None :
4749 if not isinstance (ref , str ) or ref == '' :
48- errors . append ( InvalidActivity (
49- "reference to activity must be non-empty strings" ) )
50- return errors
50+ v . add_error (
51+ "ref" , " reference to activity must be non-empty strings" )
52+ return v . errors ()
5153
5254 activity_type = activity .get ("type" )
5355 if not activity_type :
54- errors . append ( InvalidActivity ( " an activity must have a type") )
56+ v . add_error ( "type" , " an activity must have a type" )
5557
5658 if activity_type not in ("probe" , "action" ):
57- errors . append ( InvalidActivity (
58- "'{t}' is not a supported activity type". format ( t = activity_type )) )
59+ msg = "'{t}' is not a supported activity type" . format ( t = activity_type )
60+ v . add_error ( " type", msg , value = activity_type )
5961
6062 if not activity .get ("name" ):
61- errors . append ( InvalidActivity ( " an activity must have a name") )
63+ v . add_error ( "name" , " an activity must have a name" )
6264
6365 provider = activity .get ("provider" )
6466 if not provider :
65- errors . append ( InvalidActivity ( " an activity requires a provider") )
67+ v . add_error ( "provider" , " an activity requires a provider" )
6668 provider_type = None
6769 else :
6870 provider_type = provider .get ("type" )
6971 if not provider_type :
70- errors . append ( InvalidActivity ( " a provider must have a type") )
72+ v . add_error ( "type" , " a provider must have a type" )
7173
7274 if provider_type not in ("python" , "process" , "http" ):
73- errors . append ( InvalidActivity (
74- "unknown provider type '{type}'" . format ( type = provider_type )) )
75+ msg = "unknown provider type '{type}'" . format ( type = provider_type )
76+ v . add_error ( " type" , msg , value = provider_type )
7577
7678 timeout = activity .get ("timeout" )
7779 if timeout is not None :
7880 if not isinstance (timeout , numbers .Number ):
79- errors . append (
80- InvalidActivity ( " activity timeout must be a number") )
81+ v . add_error (
82+ "timeout" , " activity timeout must be a number", value = timeout )
8183
8284 pauses = activity .get ("pauses" )
8385 if pauses is not None :
8486 before = pauses .get ("before" )
8587 if before is not None and not isinstance (before , numbers .Number ):
86- errors .append (
87- InvalidActivity ("activity before pause must be a number" ))
88+ v .add_error (
89+ "before" , "activity before pause must be a number" ,
90+ value = before
91+ )
8892 after = pauses .get ("after" )
8993 if after is not None and not isinstance (after , numbers .Number ):
90- errors .append (
91- InvalidActivity ("activity after pause must be a number" ))
94+ v .add_error (
95+ "after" , "activity after pause must be a number" ,
96+ value = after
97+ )
9298
9399 if "background" in activity :
94100 if not isinstance (activity ["background" ], bool ):
95- errors .append (
96- InvalidActivity ("activity background must be a boolean" ))
101+ v .add_error (
102+ "background" , "activity background must be a boolean" ,
103+ value = activity ["background" ])
97104
98105 if provider_type == "python" :
99- errors . extend (validate_python_activity (activity ))
106+ v . extend_errors (validate_python_activity (activity ))
100107 elif provider_type == "process" :
101- errors . extend (validate_process_activity (activity ))
108+ v . extend_errors (validate_process_activity (activity ))
102109 elif provider_type == "http" :
103- errors . extend (validate_http_activity (activity ))
110+ v . extend_errors (validate_http_activity (activity ))
104111
105- return errors
112+ return v . errors ()
106113
107114
108115def run_activities (experiment : Experiment , configuration : Configuration ,
0 commit comments