Env.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. # Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http:#www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """ Test Env, manages DUT, App and EnvConfig, interface for test cases to access these components """
  15. import os
  16. import threading
  17. import functools
  18. import netifaces
  19. from . import EnvConfig
  20. def _synced(func):
  21. @functools.wraps(func)
  22. def decorator(self, *args, **kwargs):
  23. with self.lock:
  24. ret = func(self, *args, **kwargs)
  25. return ret
  26. decorator.__doc__ = func.__doc__
  27. return decorator
  28. class Env(object):
  29. """
  30. test env, manages DUTs and env configs.
  31. :keyword app: class for default application
  32. :keyword dut: class for default DUT
  33. :keyword env_tag: test env tag, used to select configs from env config file
  34. :keyword env_config_file: test env config file path
  35. :keyword test_name: test suite name, used when generate log folder name
  36. """
  37. def __init__(self,
  38. app=None,
  39. dut=None,
  40. env_tag=None,
  41. env_config_file=None,
  42. test_suite_name=None,
  43. **kwargs):
  44. self.app_cls = app
  45. self.default_dut_cls = dut
  46. self.config = EnvConfig.Config(env_config_file, env_tag)
  47. self.log_path = self.app_cls.get_log_folder(test_suite_name)
  48. if not os.path.exists(self.log_path):
  49. os.makedirs(self.log_path)
  50. self.allocated_duts = dict()
  51. self.lock = threading.RLock()
  52. @_synced
  53. def get_dut(self, dut_name, app_path, dut_class=None, app_class=None, app_config_name=None, **dut_init_args):
  54. """
  55. get_dut(dut_name, app_path, dut_class=None, app_class=None)
  56. :param dut_name: user defined name for DUT
  57. :param app_path: application path, app instance will use this path to process application info
  58. :param dut_class: dut class, if not specified will use default dut class of env
  59. :param app_class: app class, if not specified will use default app of env
  60. :param app_config_name: app build config
  61. :keyword dut_init_args: extra kwargs used when creating DUT instance
  62. :return: dut instance
  63. """
  64. if dut_name in self.allocated_duts:
  65. dut = self.allocated_duts[dut_name]["dut"]
  66. else:
  67. if dut_class is None:
  68. dut_class = self.default_dut_cls
  69. if app_class is None:
  70. app_class = self.app_cls
  71. detected_target = None
  72. try:
  73. port = self.config.get_variable(dut_name)
  74. except ValueError:
  75. # try to auto detect ports
  76. allocated_ports = [self.allocated_duts[x]["port"] for x in self.allocated_duts]
  77. available_ports = dut_class.list_available_ports()
  78. for port in available_ports:
  79. if port not in allocated_ports:
  80. result, detected_target = dut_class.confirm_dut(port)
  81. if result:
  82. break
  83. else:
  84. port = None
  85. app_target = dut_class.TARGET
  86. if not app_target:
  87. app_target = detected_target
  88. if not app_target:
  89. raise ValueError("DUT class doesn't specify the target, and autodetection failed")
  90. app_inst = app_class(app_path, app_config_name, app_target)
  91. if port:
  92. try:
  93. dut_config = self.get_variable(dut_name + "_port_config")
  94. except ValueError:
  95. dut_config = dict()
  96. dut_config.update(dut_init_args)
  97. dut = dut_class(dut_name, port,
  98. os.path.join(self.log_path, dut_name + ".log"),
  99. app_inst,
  100. **dut_config)
  101. self.allocated_duts[dut_name] = {"port": port, "dut": dut}
  102. else:
  103. raise ValueError("Failed to get DUT")
  104. return dut
  105. @_synced
  106. def close_dut(self, dut_name):
  107. """
  108. close_dut(dut_name)
  109. close one DUT by name if DUT name is valid (the name used by ``get_dut``). otherwise will do nothing.
  110. :param dut_name: user defined name for DUT
  111. :return: None
  112. """
  113. try:
  114. dut = self.allocated_duts.pop(dut_name)["dut"]
  115. dut.close()
  116. except KeyError:
  117. pass
  118. @_synced
  119. def get_variable(self, variable_name):
  120. """
  121. get_variable(variable_name)
  122. get variable from config file. If failed then try to auto-detected it.
  123. :param variable_name: name of the variable
  124. :return: value of variable if successfully found. otherwise None.
  125. """
  126. return self.config.get_variable(variable_name)
  127. PROTO_MAP = {
  128. "ipv4": netifaces.AF_INET,
  129. "ipv6": netifaces.AF_INET6,
  130. "mac": netifaces.AF_LINK,
  131. }
  132. @_synced
  133. def get_pc_nic_info(self, nic_name="pc_nic", proto="ipv4"):
  134. """
  135. get_pc_nic_info(nic_name="pc_nic")
  136. try to get info of a specified NIC and protocol.
  137. :param nic_name: pc nic name. allows passing variable name, nic name value.
  138. :param proto: "ipv4", "ipv6" or "mac"
  139. :return: a dict of nic info if successfully found. otherwise None.
  140. nic info keys could be different for different protocols.
  141. key "addr" is available for both mac, ipv4 and ipv6 pic info.
  142. """
  143. interfaces = netifaces.interfaces()
  144. if nic_name in interfaces:
  145. # the name is in the interface list, we regard it as NIC name
  146. if_addr = netifaces.ifaddresses(nic_name)
  147. else:
  148. # it's not in interface name list, we assume it's variable name
  149. _nic_name = self.get_variable(nic_name)
  150. if_addr = netifaces.ifaddresses(_nic_name)
  151. return if_addr[self.PROTO_MAP[proto]][0]
  152. @_synced
  153. def close(self, dut_debug=False):
  154. """
  155. close()
  156. close all DUTs of the Env.
  157. :param dut_debug: if dut_debug is True, then print all dut expect failures before close it
  158. :return: exceptions during close DUT
  159. """
  160. dut_close_errors = []
  161. for dut_name in self.allocated_duts:
  162. dut = self.allocated_duts[dut_name]["dut"]
  163. try:
  164. if dut_debug:
  165. dut.print_debug_info()
  166. dut.close()
  167. except Exception as e:
  168. dut_close_errors.append(e)
  169. self.allocated_duts = dict()
  170. return dut_close_errors