test-remote-image 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. #!/usr/bin/env python
  2. # Copyright (c) 2014 Intel Corporation
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License version 2 as
  6. # published by the Free Software Foundation.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License along
  14. # with this program; if not, write to the Free Software Foundation, Inc.,
  15. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. # DESCRIPTION
  17. # This script is used to test public autobuilder images on remote hardware.
  18. # The script is called from a machine that is able download the images from the remote images repository and to connect to the test hardware.
  19. #
  20. # test-remote-image --image-type core-image-sato --repo-link http://192.168.10.2/images --required-packages rpm psplash
  21. #
  22. # Translation: Build the 'rpm' and 'pslash' packages and test a remote core-image-sato image using the http://192.168.10.2/images repository.
  23. #
  24. # You can also use the '-h' option to see some help information.
  25. import os
  26. import sys
  27. import argparse
  28. import logging
  29. import shutil
  30. from abc import ABCMeta, abstractmethod
  31. # Add path to scripts/lib in sys.path;
  32. scripts_path = os.path.abspath(os.path.dirname(os.path.abspath(sys.argv[0])))
  33. lib_path = scripts_path + '/lib'
  34. sys.path = sys.path + [lib_path]
  35. import scriptpath
  36. import argparse_oe
  37. # Add meta/lib to sys.path
  38. scriptpath.add_oe_lib_path()
  39. import oeqa.utils.ftools as ftools
  40. from oeqa.utils.commands import runCmd, bitbake, get_bb_var
  41. # Add all lib paths relative to BBPATH to sys.path; this is used to find and import the target controllers.
  42. for path in get_bb_var('BBPATH').split(":"):
  43. sys.path.insert(0, os.path.abspath(os.path.join(path, 'lib')))
  44. # In order to import modules that contain target controllers, we need the bitbake libraries in sys.path .
  45. bitbakepath = scriptpath.add_bitbake_lib_path()
  46. if not bitbakepath:
  47. sys.stderr.write("Unable to find bitbake by searching parent directory of this script or PATH\n")
  48. sys.exit(1)
  49. # create a logger
  50. def logger_create():
  51. log = logging.getLogger('hwauto')
  52. log.setLevel(logging.DEBUG)
  53. fh = logging.FileHandler(filename='hwauto.log', mode='w')
  54. fh.setLevel(logging.DEBUG)
  55. ch = logging.StreamHandler(sys.stdout)
  56. ch.setLevel(logging.INFO)
  57. formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  58. fh.setFormatter(formatter)
  59. ch.setFormatter(formatter)
  60. log.addHandler(fh)
  61. log.addHandler(ch)
  62. return log
  63. # instantiate the logger
  64. log = logger_create()
  65. # Define and return the arguments parser for the script
  66. def get_args_parser():
  67. description = "This script is used to run automated runtime tests using remotely published image files. You should prepare the build environment just like building local images and running the tests."
  68. parser = argparse_oe.ArgumentParser(description=description)
  69. parser.add_argument('--image-types', required=True, action="store", nargs='*', dest="image_types", default=None, help='The image types to test(ex: core-image-minimal).')
  70. parser.add_argument('--repo-link', required=True, action="store", type=str, dest="repo_link", default=None, help='The link to the remote images repository.')
  71. parser.add_argument('--required-packages', required=False, action="store", nargs='*', dest="required_packages", default=None, help='Required packages for the tests. They will be built before the testing begins.')
  72. parser.add_argument('--targetprofile', required=False, action="store", nargs=1, dest="targetprofile", default='AutoTargetProfile', help='The target profile to be used.')
  73. parser.add_argument('--repoprofile', required=False, action="store", nargs=1, dest="repoprofile", default='PublicAB', help='The repo profile to be used.')
  74. parser.add_argument('--skip-download', required=False, action="store_true", dest="skip_download", default=False, help='Skip downloading the images completely. This needs the correct files to be present in the directory specified by the target profile.')
  75. return parser
  76. class BaseTargetProfile(object):
  77. """
  78. This class defines the meta profile for a specific target (MACHINE type + image type).
  79. """
  80. __metaclass__ = ABCMeta
  81. def __init__(self, image_type):
  82. self.image_type = image_type
  83. self.kernel_file = None
  84. self.rootfs_file = None
  85. self.manifest_file = None
  86. self.extra_download_files = [] # Extra files (full name) to be downloaded. They should be situated in repo_link
  87. # This method is used as the standard interface with the target profile classes.
  88. # It returns a dictionary containing a list of files and their meaning/description.
  89. def get_files_dict(self):
  90. files_dict = {}
  91. if self.kernel_file:
  92. files_dict['kernel_file'] = self.kernel_file
  93. else:
  94. log.error('The target profile did not set a kernel file.')
  95. sys.exit(1)
  96. if self.rootfs_file:
  97. files_dict['rootfs_file'] = self.rootfs_file
  98. else:
  99. log.error('The target profile did not set a rootfs file.')
  100. sys.exit(1)
  101. if self.manifest_file:
  102. files_dict['manifest_file'] = self.manifest_file
  103. else:
  104. log.error('The target profile did not set a manifest file.')
  105. sys.exit(1)
  106. for idx, f in enumerate(self.extra_download_files):
  107. files_dict['extra_download_file' + str(idx)] = f
  108. return files_dict
  109. class AutoTargetProfile(BaseTargetProfile):
  110. def __init__(self, image_type):
  111. super(AutoTargetProfile, self).__init__(image_type)
  112. self.image_name = get_bb_var('IMAGE_LINK_NAME', target=image_type)
  113. self.kernel_type = get_bb_var('KERNEL_IMAGETYPE', target=image_type)
  114. self.controller = self.get_controller()
  115. self.set_kernel_file()
  116. self.set_rootfs_file()
  117. self.set_manifest_file()
  118. self.set_extra_download_files()
  119. # Get the controller object that will be used by bitbake.
  120. def get_controller(self):
  121. from oeqa.controllers.testtargetloader import TestTargetLoader
  122. target_controller = get_bb_var('TEST_TARGET')
  123. bbpath = get_bb_var('BBPATH').split(':')
  124. if target_controller == "qemu":
  125. from oeqa.targetcontrol import QemuTarget
  126. controller = QemuTarget
  127. else:
  128. testtargetloader = TestTargetLoader()
  129. controller = testtargetloader.get_controller_module(target_controller, bbpath)
  130. return controller
  131. def set_kernel_file(self):
  132. postconfig = "QA_GET_MACHINE = \"${MACHINE}\""
  133. machine = get_bb_var('QA_GET_MACHINE', postconfig=postconfig)
  134. self.kernel_file = self.kernel_type + '-' + machine + '.bin'
  135. def set_rootfs_file(self):
  136. image_fstypes = get_bb_var('IMAGE_FSTYPES').split(' ')
  137. # Get a matching value between target's IMAGE_FSTYPES and the image fstypes suppoerted by the target controller.
  138. fstype = self.controller.match_image_fstype(d=None, image_fstypes=image_fstypes)
  139. if fstype:
  140. self.rootfs_file = self.image_name + '.' + fstype
  141. else:
  142. log.error("Could not get a compatible image fstype. Check that IMAGE_FSTYPES and the target controller's supported_image_fstypes fileds have common values.")
  143. sys.exit(1)
  144. def set_manifest_file(self):
  145. self.manifest_file = self.image_name + ".manifest"
  146. def set_extra_download_files(self):
  147. self.extra_download_files = self.get_controller_extra_files()
  148. if not self.extra_download_files:
  149. self.extra_download_files = []
  150. def get_controller_extra_files(self):
  151. controller = self.get_controller()
  152. return controller.get_extra_files()
  153. class BaseRepoProfile(object):
  154. """
  155. This class defines the meta profile for an images repository.
  156. """
  157. __metaclass__ = ABCMeta
  158. def __init__(self, repolink, localdir):
  159. self.localdir = localdir
  160. self.repolink = repolink
  161. # The following abstract methods are the interfaces to the repository profile classes derived from this abstract class.
  162. # This method should check the file named 'file_name' if it is different than the upstream one.
  163. # Should return False if the image is the same as the upstream and True if it differs.
  164. @abstractmethod
  165. def check_old_file(self, file_name):
  166. pass
  167. # This method should fetch file_name and create a symlink to localname if set.
  168. @abstractmethod
  169. def fetch(self, file_name, localname=None):
  170. pass
  171. class PublicAB(BaseRepoProfile):
  172. def __init__(self, repolink, localdir=None):
  173. super(PublicAB, self).__init__(repolink, localdir)
  174. if localdir is None:
  175. self.localdir = os.path.join(os.environ['BUILDDIR'], 'PublicABMirror')
  176. # Not yet implemented. Always returning True.
  177. def check_old_file(self, file_name):
  178. return True
  179. def get_repo_path(self):
  180. path = '/machines/'
  181. postconfig = "QA_GET_MACHINE = \"${MACHINE}\""
  182. machine = get_bb_var('QA_GET_MACHINE', postconfig=postconfig)
  183. if 'qemu' in machine:
  184. path += 'qemu/'
  185. postconfig = "QA_GET_DISTRO = \"${DISTRO}\""
  186. distro = get_bb_var('QA_GET_DISTRO', postconfig=postconfig)
  187. path += distro.replace('poky', machine) + '/'
  188. return path
  189. def fetch(self, file_name, localname=None):
  190. repo_path = self.get_repo_path()
  191. link = self.repolink + repo_path + file_name
  192. self.wget(link, self.localdir, localname)
  193. def wget(self, link, localdir, localname=None, extraargs=None):
  194. wget_cmd = '/usr/bin/env wget -t 2 -T 30 -nv --passive-ftp --no-check-certificate '
  195. if localname:
  196. wget_cmd += ' -O ' + localname + ' '
  197. if extraargs:
  198. wget_cmd += ' ' + extraargs + ' '
  199. wget_cmd += " -P %s '%s'" % (localdir, link)
  200. runCmd(wget_cmd)
  201. class HwAuto():
  202. def __init__(self, image_types, repolink, required_packages, targetprofile, repoprofile, skip_download):
  203. log.info('Initializing..')
  204. self.image_types = image_types
  205. self.repolink = repolink
  206. self.required_packages = required_packages
  207. self.targetprofile = targetprofile
  208. self.repoprofile = repoprofile
  209. self.skip_download = skip_download
  210. self.repo = self.get_repo_profile(self.repolink)
  211. # Get the repository profile; for now we only look inside this module.
  212. def get_repo_profile(self, *args, **kwargs):
  213. repo = getattr(sys.modules[__name__], self.repoprofile)(*args, **kwargs)
  214. log.info("Using repo profile: %s" % repo.__class__.__name__)
  215. return repo
  216. # Get the target profile; for now we only look inside this module.
  217. def get_target_profile(self, *args, **kwargs):
  218. target = getattr(sys.modules[__name__], self.targetprofile)(*args, **kwargs)
  219. log.info("Using target profile: %s" % target.__class__.__name__)
  220. return target
  221. # Run the testimage task on a build while redirecting DEPLOY_DIR_IMAGE to repo.localdir, where the images are downloaded.
  222. def runTestimageBuild(self, image_type):
  223. log.info("Running the runtime tests for %s.." % image_type)
  224. postconfig = "DEPLOY_DIR_IMAGE = \"%s\"" % self.repo.localdir
  225. result = bitbake("%s -c testimage" % image_type, ignore_status=True, postconfig=postconfig)
  226. testimage_results = ftools.read_file(os.path.join(get_bb_var("T", image_type), "log.do_testimage"))
  227. log.info('Runtime tests results for %s:' % image_type)
  228. print testimage_results
  229. return result
  230. # Start the procedure!
  231. def run(self):
  232. if self.required_packages:
  233. # Build the required packages for the tests
  234. log.info("Building the required packages: %s ." % ', '.join(map(str, self.required_packages)))
  235. result = bitbake(self.required_packages, ignore_status=True)
  236. if result.status != 0:
  237. log.error("Could not build required packages: %s. Output: %s" % (self.required_packages, result.output))
  238. sys.exit(1)
  239. # Build the package repository meta data.
  240. log.info("Building the package index.")
  241. result = bitbake("package-index", ignore_status=True)
  242. if result.status != 0:
  243. log.error("Could not build 'package-index'. Output: %s" % result.output)
  244. sys.exit(1)
  245. # Create the directory structure for the images to be downloaded
  246. log.info("Creating directory structure %s" % self.repo.localdir)
  247. if not os.path.exists(self.repo.localdir):
  248. os.makedirs(self.repo.localdir)
  249. # For each image type, download the needed files and run the tests.
  250. noissuesfound = True
  251. for image_type in self.image_types:
  252. if self.skip_download:
  253. log.info("Skipping downloading the images..")
  254. else:
  255. target = self.get_target_profile(image_type)
  256. files_dict = target.get_files_dict()
  257. log.info("Downloading files for %s" % image_type)
  258. for f in files_dict:
  259. if self.repo.check_old_file(files_dict[f]):
  260. filepath = os.path.join(self.repo.localdir, files_dict[f])
  261. if os.path.exists(filepath):
  262. os.remove(filepath)
  263. self.repo.fetch(files_dict[f])
  264. result = self.runTestimageBuild(image_type)
  265. if result.status != 0:
  266. noissuesfound = False
  267. if noissuesfound:
  268. log.info('Finished. No issues found.')
  269. else:
  270. log.error('Finished. Some runtime tests have failed. Returning non-0 status code.')
  271. sys.exit(1)
  272. def main():
  273. parser = get_args_parser()
  274. args = parser.parse_args()
  275. hwauto = HwAuto(image_types=args.image_types, repolink=args.repo_link, required_packages=args.required_packages, targetprofile=args.targetprofile, repoprofile=args.repoprofile, skip_download=args.skip_download)
  276. hwauto.run()
  277. if __name__ == "__main__":
  278. try:
  279. ret = main()
  280. except Exception:
  281. ret = 1
  282. import traceback
  283. traceback.print_exc()
  284. sys.exit(ret)