]>
git.ipfire.org Git - thirdparty/openembedded/openembedded-core.git/blob - scripts/patchtest
2 # ex:ts=4:sw=4:sts=4:et
3 # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
5 # patchtest: execute all unittest test cases discovered for a single patch
7 # Copyright (C) 2016 Intel Corporation
9 # SPDX-License-Identifier: GPL-2.0-only
19 # Include current path so test cases can see it
20 sys
. path
. insert ( 0 , os
. path
. dirname ( os
. path
. realpath ( __file__
)))
22 # Include patchtest library
23 sys
. path
. insert ( 0 , os
. path
. join ( os
. path
. dirname ( os
. path
. realpath ( __file__
)), '../meta/lib/patchtest' ))
25 from data
import PatchTestInput
26 from repo
import PatchTestRepo
29 logger
= utils
. logger_create ( 'patchtest' )
35 def getResult ( patch
, mergepatch
, logfile
= None ):
37 class PatchTestResult ( unittest
. TextTestResult
):
38 """ Patchtest TextTestResult """
46 def startTestRun ( self
):
47 # let's create the repo already, it can be used later on
49 'repodir' : PatchTestInput
. repodir
,
50 'commit' : PatchTestInput
. basecommit
,
51 'branch' : PatchTestInput
. basebranch
,
55 self
. repo_error
= False
56 self
. test_error
= False
57 self
. test_failure
= False
60 self
. repo
= PatchTestInput
. repo
= PatchTestRepo (** repoargs
)
62 logger
. error ( traceback
. print_exc ())
63 self
. repo_error
= True
70 def addError ( self
, test
, err
):
71 self
. test_error
= True
73 logger
. error ( traceback
. print_exc ())
75 def addFailure ( self
, test
, err
):
76 test_description
= test
. id (). split ( '.' )[- 1 ]. replace ( '_' , ' ' ). replace ( "cve" , "CVE" ). replace ( "signed off by" ,
77 "Signed-off-by" ). replace ( "upstream status" ,
78 "Upstream-Status" ). replace ( "non auh" ,
79 "non-AUH" ). replace ( "presence format" , "presence" )
80 self
. test_failure
= True
81 fail_str
= '{}: {}: {} ({})' . format ( self
. fail
,
82 test_description
, json
. loads ( str ( err
[ 1 ]))[ "issue" ],
86 with
open ( logfile
, "a" ) as f
:
87 f
. write ( fail_str
+ " \n " )
89 def addSuccess ( self
, test
):
90 test_description
= test
. id (). split ( '.' )[- 1 ]. replace ( '_' , ' ' ). replace ( "cve" , "CVE" ). replace ( "signed off by" ,
91 "Signed-off-by" ). replace ( "upstream status" ,
92 "Upstream-Status" ). replace ( "non auh" ,
93 "non-AUH" ). replace ( "presence format" , "presence" )
94 success_str
= '{}: {} ({})' . format ( self
. success
,
95 test_description
, test
. id ())
98 with
open ( logfile
, "a" ) as f
:
99 f
. write ( success_str
+ " \n " )
101 def addSkip ( self
, test
, reason
):
102 test_description
= test
. id (). split ( '.' )[- 1 ]. replace ( '_' , ' ' ). replace ( "cve" , "CVE" ). replace ( "signed off by" ,
103 "Signed-off-by" ). replace ( "upstream status" ,
104 "Upstream-Status" ). replace ( "non auh" ,
105 "non-AUH" ). replace ( "presence format" , "presence" )
106 skip_str
= '{}: {}: {} ({})' . format ( self
. skip
,
107 test_description
, json
. loads ( str ( reason
))[ "issue" ],
111 with
open ( logfile
, "a" ) as f
:
112 f
. write ( skip_str
+ " \n " )
114 def stopTestRun ( self
):
116 # in case there was an error on repo object creation, just return
122 return PatchTestResult
124 def _runner ( resultklass
, prefix
= None ):
125 # load test with the corresponding prefix
126 loader
= unittest
. TestLoader ()
128 loader
. testMethodPrefix
= prefix
130 # create the suite with discovered tests and the corresponding runner
131 suite
= loader
. discover ( start_dir
= PatchTestInput
. testdir
, pattern
= PatchTestInput
. pattern
, top_level_dir
= PatchTestInput
. topdir
)
132 ntc
= suite
. countTestCases ()
134 # if there are no test cases, just quit
137 runner
= unittest
. TextTestRunner ( resultclass
= resultklass
, verbosity
= 0 )
140 result
= runner
. run ( suite
)
142 logger
. error ( traceback
. print_exc ())
143 logger
. error ( 'patchtest: something went wrong' )
148 def run ( patch
, logfile
= None ):
149 """ Load, setup and run pre and post-merge tests """
150 # Get the result class and install the control-c handler
151 unittest
. installHandler ()
153 # run pre-merge tests, meaning those methods with 'pretest' as prefix
154 premerge_resultklass
= getResult ( patch
, False , logfile
)
155 premerge_result
= _runner ( premerge_resultklass
, 'pretest' )
157 # run post-merge tests, meaning those methods with 'test' as prefix
158 postmerge_resultklass
= getResult ( patch
, True , logfile
)
159 postmerge_result
= _runner ( postmerge_resultklass
, 'test' )
161 if premerge_result
== 2 and postmerge_result
== 2 :
162 logger
. error ( 'patchtest: any test cases found - did you specify the correct suite directory?' )
164 return premerge_result
or postmerge_result
168 patch_path
= PatchTestInput
. patch_path
169 log_results
= PatchTestInput
. log_results
173 git_status
= os
. popen ( "(cd %s && git status)" % PatchTestInput
. repodir
). read ()
174 status_matches
= [ "Changes not staged for commit" , "Changes to be committed" ]
175 if any ([ match
in git_status
for match
in status_matches
]):
176 logger
. error ( "patchtest: there are uncommitted changes in the target repo that would be overwritten. Please commit or restore them before running patchtest" )
179 if os
. path
. isdir ( patch_path
):
180 patch_list
= [ os
. path
. join ( patch_path
, filename
) for filename
in sorted ( os
. listdir ( patch_path
))]
182 patch_list
= [ patch_path
]
184 for patch
in patch_list
:
185 if os
. path
. getsize ( patch
) == 0 :
186 logger
. error ( 'patchtest: patch is empty' )
189 logger
. info ( 'Testing patch %s ' % patch
)
192 log_path
= patch
+ ".testresult"
193 with
open ( log_path
, "a" ) as f
:
194 f
. write ( "Patchtest results for patch ' %s ': \n\n " % patch
)
205 if __name__
== '__main__' :
208 # Parse the command line arguments and store it on the PatchTestInput namespace
209 PatchTestInput
. set_namespace ()
211 # set debugging level
212 if PatchTestInput
. debug
:
213 logger
. setLevel ( logging
. DEBUG
)
215 # if topdir not define, default it to testdir
216 if not PatchTestInput
. topdir
:
217 PatchTestInput
. topdir
= PatchTestInput
. testdir
223 traceback
. print_exc ( 5 )