2222import re
2323import argparse
2424
25- import yaml
26-
2725test_fw_path = os .getenv ("TEST_FW_PATH" )
2826if test_fw_path :
2927 sys .path .insert (0 , test_fw_path )
3028
31- from Utility import CaseConfig , SearchCases , GitlabCIJob
32-
33-
34- class Group (object ):
35-
36- MAX_EXECUTION_TIME = 30
37- MAX_CASE = 15
38- SORT_KEYS = ["env_tag" ]
39-
40- def __init__ (self , case ):
41- self .execution_time = 0
42- self .case_list = [case ]
43- self .filters = dict (zip (self .SORT_KEYS , [case .case_info [x ] for x in self .SORT_KEYS ]))
44-
45- def accept_new_case (self ):
46- """
47- check if allowed to add any case to this group
48-
49- :return: True or False
50- """
51- max_time = (sum ([x .case_info ["execution_time" ] for x in self .case_list ]) < self .MAX_EXECUTION_TIME )
52- max_case = (len (self .case_list ) < self .MAX_CASE )
53- return max_time and max_case
54-
55- def add_case (self , case ):
56- """
57- add case to current group
58-
59- :param case: test case
60- :return: True if add succeed, else False
61- """
62- added = False
63- if self .accept_new_case ():
64- for key in self .filters :
65- if case .case_info [key ] != self .filters [key ]:
66- break
67- else :
68- self .case_list .append (case )
69- added = True
70- return added
71-
72- def output (self ):
73- """
74- output data for job configs
75-
76- :return: {"Filter": case filter, "CaseConfig": list of case configs for cases in this group}
77- """
78- output_data = {
79- "Filter" : self .filters ,
80- "CaseConfig" : [{"name" : x .case_info ["name" ]} for x in self .case_list ],
81- }
82- return output_data
83-
29+ from Utility .CIAssignTest import AssignTest
8430
85- class AssignTest (object ):
86- """
87- Auto assign tests to CI jobs.
88-
89- :param test_case: path of test case file(s)
90- :param ci_config_file: path of ``.gitlab-ci.yml``
91- """
9231
32+ class CIExampleAssignTest (AssignTest ):
9333 CI_TEST_JOB_PATTERN = re .compile (r"^example_test_.+" )
9434
95- def __init__ (self , test_case , ci_config_file ):
96- self .test_cases = self ._search_cases (test_case )
97- self .jobs = self ._parse_gitlab_ci_config (ci_config_file )
98-
99- def _parse_gitlab_ci_config (self , ci_config_file ):
100-
101- with open (ci_config_file , "r" ) as f :
102- ci_config = yaml .load (f )
103-
104- job_list = list ()
105- for job_name in ci_config :
106- if self .CI_TEST_JOB_PATTERN .search (job_name ) is not None :
107- job_list .append (GitlabCIJob .Job (ci_config [job_name ], job_name ))
108- return job_list
109-
110- @staticmethod
111- def _search_cases (test_case , case_filter = None ):
112- """
113- :param test_case: path contains test case folder
114- :param case_filter: filter for test cases
115- :return: filtered test case list
116- """
117- test_methods = SearchCases .Search .search_test_cases (test_case )
118- return CaseConfig .filter_test_cases (test_methods , case_filter if case_filter else dict ())
119-
120- def _group_cases (self ):
121- """
122- separate all cases into groups according group rules. each group will be executed by one CI job.
123-
124- :return: test case groups.
125- """
126- groups = []
127- for case in self .test_cases :
128- for group in groups :
129- # add to current group
130- if group .add_case (case ):
131- break
132- else :
133- # create new group
134- groups .append (Group (case ))
135- return groups
136-
137- def assign_cases (self ):
138- """
139- separate test cases to groups and assign test cases to CI jobs.
140-
141- :raise AssertError: if failed to assign any case to CI job.
142- :return: None
143- """
144- failed_to_assign = []
145- test_groups = self ._group_cases ()
146- for group in test_groups :
147- for job in self .jobs :
148- if job .match_group (group ):
149- job .assign_group (group )
150- break
151- else :
152- failed_to_assign .append (group )
153- assert not failed_to_assign
154-
155- def output_configs (self , output_path ):
156- """
157-
158- :param output_path: path to output config files for each CI job
159- :return: None
160- """
161- if not os .path .exists (output_path ):
162- os .makedirs (output_path )
163- for job in self .jobs :
164- job .output_config (output_path )
165-
16635
16736if __name__ == '__main__' :
16837 parser = argparse .ArgumentParser ()
@@ -174,6 +43,6 @@ def output_configs(self, output_path):
17443 help = "output path of config files" )
17544 args = parser .parse_args ()
17645
177- assign_test = AssignTest (args .test_case , args .ci_config_file )
46+ assign_test = CIExampleAssignTest (args .test_case , args .ci_config_file )
17847 assign_test .assign_cases ()
17948 assign_test .output_configs (args .output_path )
0 commit comments