22# SPDX-License-Identifier: Unlicense OR CC0-1.0
33# !/usr/bin/env python3
44# this file defines some functions for testing cli and br under pytest framework
5+ import logging
56import os
67import re
78import socket
@@ -28,7 +29,7 @@ def wrapper(dut: IdfDut) -> str:
2829 try :
2930 result = dut .expect (pattern , timeout = 5 )[1 ].decode ()
3031 except Exception as e :
31- print (f'Error: { e } ' )
32+ logging . error (f'Error: { e } ' )
3233 return default_return
3334 return func (result )
3435
@@ -151,7 +152,7 @@ def getDeviceRole(dut: IdfDut) -> str:
151152 wait (dut , 1 )
152153 execute_command (dut , 'state' )
153154 role = dut .expect (r'\W+(\w+)\W+Done' , timeout = 5 )[1 ].decode ()
154- print (role )
155+ logging . info (role )
155156 return str (role )
156157
157158
@@ -310,17 +311,17 @@ def get_host_interface_name() -> str:
310311 interface_name = config .get ('interface_name' )
311312 if interface_name :
312313 if interface_name == 'eth0' :
313- print (
314+ logging . warning (
314315 f"Warning: 'eth0' is not recommended as a valid network interface. "
315316 f"Please check and update the 'interface_name' in the configuration file: "
316317 f'{ config_path } '
317318 )
318319 else :
319320 return str (interface_name )
320321 else :
321- print ("Warning: Configuration file found but 'interface_name' is not defined." )
322+ logging . warning ("Warning: Configuration file found but 'interface_name' is not defined." )
322323 except Exception as e :
323- print (f'Error: Failed to read or parse { config_path } . Details: { e } ' )
324+ logging . error (f'Error: Failed to read or parse { config_path } . Details: { e } ' )
324325 if 'eth1' in netifaces .interfaces ():
325326 return 'eth1'
326327
@@ -338,8 +339,8 @@ def check_if_host_receive_ra(br: IdfDut) -> bool:
338339 omrprefix = get_omrprefix (br )
339340 command = 'ip -6 route | grep ' + str (interface_name )
340341 out_str = subprocess .getoutput (command )
341- print ( 'br omrprefix: ' , str ( omrprefix ) )
342- print ( 'host route table:\n ' , str ( out_str ) )
342+ logging . info ( f 'br omrprefix: { omrprefix } ' )
343+ logging . info ( f 'host route table:\n { out_str } ' )
343344 return str (omrprefix ) in str (out_str )
344345
345346
@@ -404,7 +405,7 @@ def create_host_udp_server(myudp: udp_parameter) -> None:
404405 AF_INET = socket .AF_INET6
405406 else :
406407 AF_INET = socket .AF_INET
407- print ('The host start to create udp server!' )
408+ logging . info ('The host start to create udp server!' )
408409 if_index = socket .if_nametoindex (interface_name )
409410 sock = socket .socket (AF_INET , socket .SOCK_DGRAM )
410411 sock .bind ((myudp .addr , myudp .port ))
@@ -417,13 +418,14 @@ def create_host_udp_server(myudp: udp_parameter) -> None:
417418 )
418419 sock .settimeout (myudp .timeout )
419420 myudp .init_flag = True
420- print ('The host start to receive message!' )
421+ logging . info ('The host start to receive message!' )
421422 myudp .udp_bytes = (sock .recvfrom (1024 ))[0 ]
422- print ('The host has received message: ' , myudp .udp_bytes )
423+ udp_str = str (myudp .udp_bytes )
424+ logging .info (f'The host has received message: { udp_str } ' )
423425 except OSError :
424- print ('The host did not receive message!' )
426+ logging . error ('The host did not receive message!' )
425427 finally :
426- print ('Close the socket.' )
428+ logging . info ('Close the socket.' )
427429 sock .close ()
428430
429431
@@ -438,10 +440,10 @@ def host_udp_send_message(udp_target: udp_parameter) -> None:
438440 sock .bind (('::' , 12350 ))
439441 sock .setsockopt (socket .SOL_SOCKET , socket .SO_BINDTODEVICE , interface_name .encode ())
440442 sock .setsockopt (socket .IPPROTO_IPV6 , socket .IPV6_MULTICAST_HOPS , 32 )
441- print ('Host is sending message' )
443+ logging . info ('Host is sending message' )
442444 sock .sendto (udp_target .udp_bytes , (udp_target .addr , udp_target .port ))
443445 except OSError :
444- print ('Host cannot send message' )
446+ logging . error ('Host cannot send message' )
445447 finally :
446448 sock .close ()
447449
@@ -481,13 +483,13 @@ def host_close_service() -> None:
481483 command = 'ps auxww | grep avahi-publish-s'
482484 out_bytes = subprocess .check_output (command , shell = True , timeout = 5 )
483485 out_str = out_bytes .decode ('utf-8' )
484- print ( 'host close service avahi status:\n ' , out_str )
486+ logging . info ( f 'host close service avahi status:\n { out_str } ' )
485487 service_info = [line for line in out_str .splitlines () if 'testxxx _testxxx._udp' in line ]
486488 for line in service_info :
487- print ( 'Process:' , line )
489+ logging . info ( f 'Process:{ line } ' )
488490 pid = line .split ()[1 ]
489491 command = 'kill -9 ' + pid
490- print ( 'kill ' , pid )
492+ logging . info ( f 'kill { pid } ' )
491493 subprocess .call (command , shell = True , timeout = 5 )
492494 time .sleep (1 )
493495
@@ -520,33 +522,33 @@ def open_host_interface() -> None:
520522
521523def get_domain () -> str :
522524 hostname = socket .gethostname ()
523- print ( 'hostname is: ' , hostname )
525+ logging . info ( f 'hostname is: { hostname } ' )
524526 command = 'ps -auxww | grep avahi-daemon | grep running'
525527 out_str = subprocess .getoutput (command )
526- print ( 'avahi status:\n ' , out_str )
528+ logging . info ( f 'avahi status:\n { out_str } ' )
527529 role = re .findall (r'\[([\w\W]+)\.local\]' , str (out_str ))[0 ]
528- print ( 'active host is: ' , role )
530+ logging . info ( f 'active host is: { role } ' )
529531 return str (role )
530532
531533
532534def flush_ipv6_addr_by_interface () -> None :
533535 interface_name = get_host_interface_name ()
534- print (f'flush ipv6 addr : { interface_name } ' )
536+ logging . info (f'flush ipv6 addr : { interface_name } ' )
535537 command_show_addr = f'ip -6 addr show dev { interface_name } '
536538 command_show_route = f'ip -6 route show dev { interface_name } '
537539 addr_before = subprocess .getoutput (command_show_addr )
538540 route_before = subprocess .getoutput (command_show_route )
539- print (f'Before flush, IPv6 addresses: \n { addr_before } ' )
540- print (f'Before flush, IPv6 routes: \n { route_before } ' )
541+ logging . info (f'Before flush, IPv6 addresses: \n { addr_before } ' )
542+ logging . info (f'Before flush, IPv6 routes: \n { route_before } ' )
541543 subprocess .run (['ip' , 'link' , 'set' , interface_name , 'down' ])
542544 subprocess .run (['ip' , '-6' , 'addr' , 'flush' , 'dev' , interface_name ])
543545 subprocess .run (['ip' , '-6' , 'route' , 'flush' , 'dev' , interface_name ])
544546 subprocess .run (['ip' , 'link' , 'set' , interface_name , 'up' ])
545547 time .sleep (5 )
546548 addr_after = subprocess .getoutput (command_show_addr )
547549 route_after = subprocess .getoutput (command_show_route )
548- print (f'After flush, IPv6 addresses: \n { addr_after } ' )
549- print (f'After flush, IPv6 routes: \n { route_after } ' )
550+ logging . info (f'After flush, IPv6 addresses: \n { addr_after } ' )
551+ logging . info (f'After flush, IPv6 routes: \n { route_after } ' )
550552
551553
552554class tcp_parameter :
@@ -575,28 +577,29 @@ def create_host_tcp_server(mytcp: tcp_parameter) -> None:
575577 AF_INET = socket .AF_INET6
576578 else :
577579 AF_INET = socket .AF_INET
578- print ('The host start to create a tcp server!' )
580+ logging . info ('The host start to create a tcp server!' )
579581 sock = socket .socket (AF_INET , socket .SOCK_STREAM )
580582 sock .bind ((mytcp .addr , mytcp .port ))
581583 sock .listen (5 )
582584 mytcp .listen_flag = True
583585
584- print ('The tcp server is waiting for connection!' )
586+ logging . info ('The tcp server is waiting for connection!' )
585587 sock .settimeout (mytcp .timeout )
586588 connfd , addr = sock .accept ()
587- print ( 'The tcp server connected with ' , addr )
589+ logging . info ( f 'The tcp server connected with { addr } ' )
588590 mytcp .recv_flag = True
589591
590592 mytcp .tcp_bytes = connfd .recv (1024 )
591- print ('The tcp server has received message: ' , mytcp .tcp_bytes )
593+ tcp_str = str (mytcp .tcp_bytes )
594+ logging .info (f'The tcp server has received message: { tcp_str } ' )
592595
593596 except OSError :
594597 if mytcp .recv_flag :
595- print ('The tcp server did not receive message!' )
598+ logging . error ('The tcp server did not receive message!' )
596599 else :
597- print ('The tcp server fail to connect!' )
600+ logging . error ('The tcp server fail to connect!' )
598601 finally :
599- print ('Close the socket.' )
602+ logging . info ('Close the socket.' )
600603 sock .close ()
601604
602605
0 commit comments