22A class to execute the given scenario for Logstash Health Report integration test
33"""
44import time
5+ import re
56from logstash_health_report import LogstashHealthReport
67
78
@@ -11,39 +12,38 @@ class ScenarioExecutor:
1112 def __init__ (self ):
1213 pass
1314
14- def __has_intersection (self , expects , results ):
15- # TODO: this logic is aligned on current Health API response
16- # there is no guarantee that method correctly runs if provided multi expects and results
17- # we expect expects to be existing in results
18- for expect in expects :
19- for result in results :
20- if result .get ('help_url' ) and "health-report-pipeline-" not in result .get ('help_url' ):
21- return False
22- if not all (key in result and result [key ] == value for key , value in expect .items ()):
23- return False
24- return True
15+ def __get_difference (self , expect : Any , actual : Any , path : str | None = None ) -> list :
2516
26- def __get_difference ( self , differences : list , expectations : dict , reports : dict ) -> dict :
27- for key in expectations . keys ():
17+ path = path or ""
18+ differences = []
2819
29- if type (expectations .get (key )) != type (reports .get (key )):
30- differences .append (f"Scenario expectation and Health API report structure differs for { key } ." )
31- return differences
20+ match expect :
21+ case {"$include" : inclusion } if isinstance (expect , dict ) and len (expect ) == 1 and isinstance (actual , str ):
22+ if inclusion not in actual :
23+ differences .append (f"Value at path `{ path } ` does not include:`{ inclusion } `; got:`{ actual } `" )
24+ case dict ():
25+ if not isinstance (actual , dict ):
26+ differences .append (f"Structure differs at `{ path } `, expected:`{ expect } ` got:`{ actual } `" )
27+ else :
28+ for key in expect .keys ():
29+ differences .extend (self .__get_difference (expect .get (key ), actual .get (key ), f"{ path } .{ key } " ))
30+ case list ():
31+ if not isinstance (actual , list ):
32+ differences .append (f"Structure differs at `{ path } `, expected:`{ expect } ` got:`{ actual } `" )
33+ else :
34+ for index , (expectEntry , actualEntry ) in enumerate (zip (expect , actual )):
35+ differences .extend (self .__get_difference (expectEntry , actualEntry , f"{ path } [{ index } ]" ))
36+ if len (actual ) < len (expect ):
37+ differences .append (f"Missing entries at path `{ path } `, expected:`{ len (expect )} `, got:`{ len (actual )} `" )
38+ case _:
39+ if expect != actual :
40+ differences .append (f"Value not match at path `{ path } `; expected:`{ expect } `, got:`{ actual } `" )
3241
33- if isinstance (expectations .get (key ), str ):
34- if expectations .get (key ) != reports .get (key ):
35- differences .append ({key : {"expected" : expectations .get (key ), "got" : reports .get (key )}})
36- continue
37- elif isinstance (expectations .get (key ), dict ):
38- self .__get_difference (differences , expectations .get (key ), reports .get (key ))
39- elif isinstance (expectations .get (key ), list ):
40- if not self .__has_intersection (expectations .get (key ), reports .get (key )):
41- differences .append ({key : {"expected" : expectations .get (key ), "got" : reports .get (key )}})
4242 return differences
4343
4444 def __is_expected (self , expectations : dict ) -> None :
4545 reports = self .logstash_health_report_api .get ()
46- differences = self .__get_difference ([], expectations , reports )
46+ differences = self .__get_difference (expect = expectations , actual = reports )
4747 if differences :
4848 print ("Differences found in 'expectation' section between YAML content and stats:" )
4949 for diff in differences :
0 commit comments