class testcase(unittest.TestCase): logger = set_logger(__name__) sdk = tools.get_api() model = tools.get_model() filters = tools.get_filter() filter_count = int(tools.read_job_config()['filter_count']) unfilter_count = int(tools.read_job_config()['unfilter_count']) timeout = tools.read_job_config()['case_timeout'] def setUp(self): self.timeout_flag = None self.flag1 = 0 self.flag2 = 0 self.key=None self.logger.info('X/E 测试chip0 filter uuid被动扫描 chip1 filter uuid 主动扫描,S chip0 filter uuid 主动扫描') self.timer = Timer(self.timeout, self.set_timeout) self.timer.start() def tearDown(self): self.timer.cancel() # 测试方法 def test_scan_filter_uuid(self): if self.model.startswith('S') or self.model.startswith('s'): a = threading.Thread(target=self.chip0_scan, args=(0, self.filters['filter_uuid'])) a.setDaemon(True) a.start() while True: if self.flag1 == 1: self.assertTrue(True) self.logger.info('pass\n') break elif self.timeout_flag: self.logger.info('fail\n') self.fail('Case failed,start scan timeout.') self.logger.error("Case failed,start scan timeout.") break else: a = threading.Thread(target=self.chip0_scan, args=(1, self.filters['filter_uuid'])) b = threading.Thread(target=self.chip1_scan, args=(0, self.filters['filter_uuid'])) a.setDaemon(True) b.setDaemon(True) b.start() a.start() while True: if self.flag1==1 and self.flag2==1: self.assertTrue(True) self.logger.info('pass\n') break elif self.timeout_flag: self.logger.info('fail\n') self.fail('Case failed,case timeout.') self.logger.error("Case failed,start scan timeout.") # break sys.exit(1)
def set_header(): config = read_job_config() use_info = config['user'] + ':' + config['pwd'] # 编码开发者帐号 encode_info = base64.b64encode(use_info.encode('utf-8')) head = { 'Content-Type': 'application/json', 'Authorization': 'Basic ' + encode_info.decode("utf-8") } data = {'grant_type': 'client_credentials'} try: # 发起请求 res = requests.post(config['host'] + '/oauth2/token', data=json.dumps(data), headers=head) # print(res.text,res.status_code) if res.status_code == 200: res_body = json.loads(res.text) # print(res_body.get("access_token")) TOKEN = res_body.get("access_token") elif res.status_code == 401: print('开发帐号错误') elif res.status_code == 400: print('API路径错误') except Exception as e: print(e) headers = { 'Content-Type': 'application/json', 'version': '1', 'Authorization': 'Bearer ' + TOKEN } return headers
class testcase(unittest.TestCase): logger = set_logger(__name__) sdk = tools.get_api() model = tools.get_model() filter_count = int(tools.read_job_config()['filter_count']) unfilter_count = int(tools.read_job_config()['unfilter_count']) timeout = tools.read_job_config()['case_timeout'] def setUp(self): self.timeout_flag = None self.flag1 = 0 self.flag2 = 0 self.key=None self.logger.info('X/E 测试chip0 filter duplicate主动扫描 chip1 不加filter duplicate主动扫描,S chip0 filter duplicate 被动扫描') self.timer = Timer(self.timeout, self.set_timeout) self.timer.start() def tearDown(self): self.timer.cancel()
class testcase(unittest.TestCase): logger = set_logger(__name__) sdk = tools.get_api() model = tools.get_model() filters = tools.get_filter() filter_count = int(tools.read_job_config()['filter_count']) unfilter_count = tools.read_job_config()['unfilter_count'] timeout = tools.read_job_config()['case_timeout'] def setUp(self): self.timeout_flag = None self.flag1 = 0 self.flag2 = 0 self.logger.info('测试X/E chip0 不加filter rssi被动扫描 chip1 filter uuid 被动扫描,S chip0 不加filter uuid 主动扫描') self.timer = Timer(10, self.set_timeout) self.timer.start() def tearDown(self): self.timer.cancel()
def get_online_hubs(): config = read_job_config() headers = set_header() res = requests.get(config['host'] + '/cassia/hubs', headers=headers) res_hub_info = json.loads(res.text) hubs = [] for i in res_hub_info: hubs.append(i['mac']) return hubs
class testcase(unittest.TestCase): logger = set_logger(__name__) sdk = tools.get_api() model = tools.get_model() filter_count = int(tools.read_job_config()['filter_count']) unfilter_count = int(tools.read_job_config()['unfilter_count']) timeout = tools.read_job_config()['case_timeout'] local=tools.read_job_config()["local"] def setUp(self): self.timeout_flag = None self.flag1 = 0 self.flag2 = 0 self.key=None self.logger.info('X/E duration 参数测试chip0 主动扫 chip1 被动扫d=5.58s ,S chip0 主动扫d=7.8s ') self.timer = Timer(self.timeout, self.set_timeout) self.timer.start() def tearDown(self): self.timer.cancel()
class testcase(unittest.TestCase): logger = set_logger(__name__) sdk = tools.get_api() model = tools.get_model() filters = tools.get_filter() timeout = tools.read_job_config()['case_timeout'] filter_count = int(tools.read_job_config()['filter_count']) unfilter_count = int(tools.read_job_config()['unfilter_count']) def setUp(self): self.timeout_flag = None self.flag1 = 0 self.flag2 = 0 self.logger.info('测试chip0 filter name主动扫描,chip1 不加过虑主动扫描') self.timer = Timer(self.timeout, self.set_timeout) self.timer.start() def tearDown(self): self.timer.cancel() #测试方法 # noinspection PyUnreachableCode,PyUnreachableCode def test_scan_filter_name(self): if self.model.startswith('S') or self.model.startswith('s'): a = threading.Thread(target=self.chip0_scan, args=(1, self.filters['filter_name'])) b = threading.Thread(target=self.chip0_scan, args=(0, )) a.setDaemon(True) b.setDaemon(True) b.start() a.start() while True: if self.flag1 == 2: self.assertTrue(True) self.logger.info('pass\n') break elif self.timeout_flag: self.logger.info('fail\n') self.fail('Case failed,start scan timeout.') self.logger.error("Case failed,start scan timeout.") break else: a = threading.Thread(target=self.chip0_scan, args=(1, self.filters['filter_name'])) b = threading.Thread(target=self.chip1_scan, args=(0, )) a.setDaemon(True) b.setDaemon(True) b.start() a.start() while True: if self.flag1 == 1 and self.flag2 == 1: self.assertTrue(True) self.logger.info('pass\n') break elif self.timeout_flag: self.logger.info('fail\n') self.fail('Case failed,start scan timeout.') self.logger.error("Case failed,start scan timeout.") break # noinspection PyUnreachableCode def chip0_scan(self, active=0, filter_name=None): #step1:chip 1 start passive scan,then start chip0 scan. with closing( self.sdk.scan(chip=0, active=active, filter_name=filter_name)) as self.sse1: count = 0 for message in self.sse1: if message.startswith('data'): msg = json.loads(message[5:]) if filter_name: #进入开启过滤的扫描结果判断流程 if count < self.filter_count: print('chip0', count, message) name = msg['name'] if name != self.filters['filter_name']: self.fail('filter name failed.') self.logger.debug('filter name failed.') break else: count += 1 else: self.flag1 += 1 self.logger.debug( 'Step 1:chip0 start scan with filter name success.' ) break else: #进入不开启过滤的扫描结果判断流程 if count < 300: print('chip0', count, message) count += 1 else: self.flag1 += 1 self.logger.debug( 'Step 1:chip0 start scan with no filter name success.' ) break # noinspection PyUnreachableCode def chip1_scan(self, active=0, filter_name=None): #step2:start chip0 scan. with closing( self.sdk.scan(chip=1, active=active, filter_name=filter_name)) as self.sse2: count = 0 tmp_name = [] for message in self.sse2: if message.startswith('data'): msg = json.loads(message[5:]) if filter_name: if count < self.filter_count: print('chip1', count, message) name = msg['name'] if name != filter_name: self.fail('filter name failed.') self.logger.debug('filter name failed.') break else: count += 1 else: self.flag2 += 1 self.logger.debug( 'Step 2:chip1 start scan with filter name success.' ) break else: #进入不开启过滤的扫描结果判断流程 if count < self.unfilter_count: print('chip1', count, message) count += 1 else: self.flag2 += 1 self.logger.debug( 'Step 2:chip1 start scan with no filter name success.' ) break def set_timeout(self): self.timeout_flag = True
class testcase(unittest.TestCase): logger = set_logger(__name__) local_sdk, cloud_sdk = tools.get_all_api() model = tools.get_model() filters = tools.get_filter() timeout = tools.read_job_config()['case_timeout'] def setUp(self): self.timeout_flag = None self.flag1 = None self.flag2 = None self.logger.info('测试点:测试chip0 local扫描的同时,chip1进行cloud扫描') self.timer = Timer(self.timeout, self.set_timeout) self.timer.start() def tearDown(self): self.timer.cancel() def test_scan_on_cloud_local(self): a = threading.Thread(target=self.local_scan) b = threading.Thread(target=self.cloud_scan) a.setDaemon(True) b.setDaemon(True) b.start() a.start() while True: if self.flag1 and self.flag2: self.assertTrue(True) self.logger.info('pass\n') break elif self.timeout_flag: self.logger.info('fail\n') self.fail('Case failed,start scan timeout.') self.logger.error("Case failed,start scan timeout.") break def test_step(self): self.flag = None #step1:start local scan. with closing(self.local_sdk.scan(chip=1)) as self.sse2: count = 0 for message in self.sse2: if message.startswith('data'): if count < 100: print('local', count, message) count += 1 else: self.flag = True self.logger.debug('Step 1: start local scan success.') break #step3:停止本地扫描 time.sleep(3) self.logger.debug('Step 2: stop local scan success.') #step2:start cloud scan. with closing(self.cloud_sdk.scan(chip=0)) as self.sse1: count = 0 for message in self.sse1: if message.startswith('data'): if count < 100: print('cloud', count, message) count += 1 else: self.flag = True self.logger.debug('Step 3:start cloud scan success.') break def set_timeout(self): self.timeout_flag = True
class testcase(unittest.TestCase): logger = set_logger(__name__) sdk = tools.get_api() model = tools.get_model() filter_count = int(tools.read_job_config()['filter_count']) unfilter_count = int(tools.read_job_config()['unfilter_count']) timeout = tools.read_job_config()['case_timeout'] local = tools.read_job_config()["local"] def setUp(self): self.timeout_flag = None self.flag1 = 0 self.flag2 = 0 self.key = None self.logger.info( 'X/E duration 参数测试chip0 被动扫 chip1 主动扫 d=9.99s,S chip0 被动扫d=9.99s ' ) self.timer = Timer(self.timeout, self.set_timeout) self.timer.start() def tearDown(self): self.timer.cancel() # 测试方法 # noinspection PyUnreachableCode def test_scan_filter_duplicates(self): # duration1=2 if self.local == "True": if self.model.startswith('S') or self.model.startswith('s'): a = threading.Thread(target=self.chip0_scan, args=(1, 9.99)) a.setDaemon(True) a.start() while True: if self.flag1 == 1: self.logger.info('pass\n') break elif self.timeout_flag: self.logger.info('fail\n') self.fail('Case failed,start scan timeout.') self.logger.error("Case failed,start scan timeout.") break else: a = threading.Thread(target=self.chip0_scan, args=(1, )) b = threading.Thread(target=self.chip1_scan, args=(0, 9.99)) a.setDaemon(True) b.setDaemon(True) b.start() a.start() while True: if self.flag1 == 1 and self.flag2 == 1: self.logger.info('pass\n') break elif self.timeout_flag: self.logger.info('fail\n') self.fail('Case failed,case timeout.') self.logger.error("Case failed,start scan timeout.") break sys.exit(1) else: self.logger.info("AC不需要测试duration参数,duration参数只对AP端生效") pass def chip0_scan(self, active=0, duration=None): # step1:chip 1 start passive scan,then start chip0 scan. if not duration is None: if isinstance(duration, str): duration1 = 5 print("字符串duration=", duration1) # 负数和0 扫描5s后停止 elif duration <= 0: duration1 = 5 print("负数和0 duration=", duration1) # 小数位数小于等于3时,会按照设置的时间停止扫描;小数位数大于3时,参数不起效,一直扫描数据 elif duration > float(str(duration)[:5]): duration1 = None print("小数位数大于3 duration=", duration1) else: duration1 = duration else: duration1 = None with closing(self.sdk.scan(chip=0, active=active, duration=duration1)) as self.sse1: if duration1: # time.sleep(duration1+0.5) d1 = datetime.datetime.now() print("start time at", datetime.datetime.now()) for message in self.sse1: continue d2 = datetime.datetime.now() print("end time at ", datetime.datetime.now()) t = float(((d2 - d1).seconds)) + float( (d2 - d1).microseconds / 1000 / 1000) - 1 print("second", t) if t <= duration1: self.flag1 += 1 self.logger.info( "Step 1:chip0 start scan with duration success.") else: self.fail("chip 0 duration failed") self.logger.debug("chip 0 duration failed") else: count = 0 for message in self.sse1: if message.startswith("data"): msg = json.loads(message[5:]) count += 1 print("chip 0 ", count, message) if count > self.unfilter_count: self.flag1 += 1 self.logger.info( "Step 1:chip0 start scan with no duration success." ) break # noinspection PyUnreachableCode def chip1_scan(self, active=0, duration=None): # step2:start chip1 scan. # 字符串 扫描5s后停止 if not duration is None: if isinstance(duration, str): duration1 = 5 print("字符串duration=", duration1) # 负数和0 扫描5s后停止 elif duration <= 0: duration1 = 5 print("负数和0 duration=", duration1) # 小数位数小于等于3时,会按照设置的时间停止扫描;小数位数大于3时,参数不起效,一直扫描数据 elif duration > float(str(duration)[:5]): duration1 = None print("小数位数大于3 duration=", duration1) else: duration1 = duration else: duration1 = None with closing(self.sdk.scan(chip=1, active=active, duration=duration1)) as self.sse1: if duration1: # time.sleep(duration1+0.5) d1 = datetime.datetime.now() print("start time at", datetime.datetime.now()) for message in self.sse1: continue d2 = datetime.datetime.now() print("end time at ", datetime.datetime.now()) t = float(((d2 - d1).seconds)) + float( (d2 - d1).microseconds / 1000 / 1000) - 0.5 print("second", t) if t <= duration1: self.flag2 += 1 self.logger.info( "Step 1:chip1 start scan with duration success.") else: self.fail("chip 0 duration failed") self.logger.debug("chip 1 duration failed") else: count = 0 for message in self.sse1: if message.startswith("data"): msg = json.loads(message[5:]) count += 1 print("chip 0 ", count, message) if count > self.unfilter_count: self.flag2 += 1 self.logger.info( "Step 1:chip1 start scan with no duration success." ) break def set_timeout(self): self.timeout_flag = True
class testcase(unittest.TestCase): logger = set_logger(__name__) sdk = get_api() model = get_model() timeout = read_job_config()['case_timeout'] def setUp(self): self.logger.info('测试在不同芯片之间,按先后顺序,能否成功开启扫描') self.timer = Timer(self.timeout,self.close) self.timer.start() def tearDown(self): self.timer.cancel() def test_start_scan_in_diff_chip(self): flag = None if self.model.startswith('S')or self.model.startswith('s'): self.logger.debug('if model is s1000系列的产品,直接pass') #if model is s1000系列的产品,直接pass self.assertTrue(True) else: #step1:chip 0 start scan,then stop scan with closing(self.sdk.scan(chip = 0)) as self.sse: for message in self.sse: if message.startswith('data'): flag = True self.logger.debug("step1:chip 0 start scan,then stop scan success") self.sse.close() elif 'keep-alive' in message: pass else: flag = False self.logger.error('start scan fail,%s'%message) #step2:chip 1 start scan,then stop scan. with closing(self.sdk.scan(chip = 1)) as self.sse: for message in self.sse: if message.startswith('data'): flag = True self.logger.debug("step2:chip 1 start scan,then stop scan success") self.sse.close() elif 'keep-alive' in message: pass else: flag = False self.logger.error('start scan fail,%s'%message) #step3:chip 0 start scan,then change to chip1 scan. with closing(self.sdk.scan(chip = 0)) as self.sse: for message in self.sse: if message.startswith('data'): flag = True self.logger.debug("step3:chip 0 start scan,then change to chip1 scan success") # self.change_scan(flag) self.sse.close() elif 'keep-alive' in message: pass else: flag = False self.logger.error('start scan fail,%s'%message) #step4:chip 1 start scan,then change to chip0 scan. with closing(self.sdk.scan(chip = 1)) as self.sse: for message in self.sse: if message.startswith('data'): flag = True self.logger.debug("step4:chip 1 start scan,then change to chip0 scan success") # self.change_scan2(flag) self.sse.close() elif 'keep-alive' in message: pass else: flag = False self.logger.error('start scan fail,%s'%message) #step5:chip 0 start scan,then change to chip1 scan. with closing(self.sdk.scan(chip = 1)) as self.sse: for message in self.sse: if message.startswith('data'): flag = True self.logger.debug("step5:chip 0 start scan,then change to chip1 scan success.") self.sse.close() elif 'keep-alive' in message: pass else: flag = False self.logger.error('start scan fail,%s'%message) self.assertTrue(flag) if flag: self.logger.info('case pass\n') else: self.logger.info('case fail\n')
import json