44# Copyright(c) 2024-2025 Intel Corporation
55# Media Communications Mesh
66
7- import argparse
87import logging
9- import os
108import subprocess
119import sys
1210import tempfile
1311from pathlib import Path
1412
13+
1514# Create mock runner classes for testing
1615class FileAudioIntegrityRunner :
17- def __init__ (self , host , test_repo_path , src_url , out_name , sample_size = 2 , sample_num = 480 ,
18- channel_num = 2 , out_path = "/mnt/ramdisk" , python_path = None , integrity_path = None , delete_file = True ):
16+ def __init__ (
17+ self ,
18+ host ,
19+ test_repo_path ,
20+ src_url ,
21+ out_name ,
22+ sample_size = 2 ,
23+ sample_num = 480 ,
24+ channel_num = 2 ,
25+ out_path = "/mnt/ramdisk" ,
26+ python_path = None ,
27+ integrity_path = None ,
28+ delete_file = True ,
29+ ):
1930 self .host = host
2031 self .test_repo_path = test_repo_path
2132 self .src_url = src_url
@@ -33,10 +44,12 @@ def __init__(self, host, test_repo_path, src_url, out_name, sample_size=2, sampl
3344 # Get the directory where this script is located
3445 script_dir = Path (__file__ ).parent
3546 self .integrity_path = str (script_dir / "audio_integrity.py" )
36-
47+
3748 def setup (self ):
38- logging .info (f"Setting up audio integrity check on { self .host .name } for { self .out_name } " )
39-
49+ logging .info (
50+ f"Setting up audio integrity check on { self .host .name } for { self .out_name } "
51+ )
52+
4053 def run (self ):
4154 cmd = " " .join (
4255 [
@@ -56,15 +69,24 @@ def run(self):
5669 "--delete_file" if self .delete_file else "--no_delete_file" ,
5770 ]
5871 )
59- logging .debug (f"Running audio integrity check on { self .host .name } for { self .out_name } with command: { cmd } " )
60- result = self .host .connection .execute_command (cmd , shell = True , stderr_to_stdout = True , expected_return_codes = (0 , 1 ))
72+ logging .debug (
73+ f"Running audio integrity check on { self .host .name } for { self .out_name } with command: { cmd } "
74+ )
75+ result = self .host .connection .execute_command (
76+ cmd , shell = True , stderr_to_stdout = True , expected_return_codes = (0 , 1 )
77+ )
6178 if result .return_code > 0 :
62- logging .error (f"Audio integrity check failed on { self .host .name } : { self .out_name } " )
79+ logging .error (
80+ f"Audio integrity check failed on { self .host .name } : { self .out_name } "
81+ )
6382 logging .error (result .stdout )
6483 return False
65- logging .info (f"Audio integrity check completed successfully on { self .host .name } for { self .out_name } " )
84+ logging .info (
85+ f"Audio integrity check completed successfully on { self .host .name } for { self .out_name } "
86+ )
6687 return True
6788
89+
6890# Set up logging
6991logging .basicConfig (
7092 level = logging .INFO ,
@@ -75,14 +97,17 @@ def run(self):
7597
7698class LocalHost :
7799 """Simple host class for testing that mimics the expected interface"""
100+
78101 def __init__ (self , name = "localhost" ):
79102 self .name = name
80103 self .connection = self
81104
82105 def path (self , * args ):
83106 return Path (* args )
84107
85- def execute_command (self , cmd , shell = False , stderr_to_stdout = False , expected_return_codes = None ):
108+ def execute_command (
109+ self , cmd , shell = False , stderr_to_stdout = False , expected_return_codes = None
110+ ):
86111 class CommandResult :
87112 def __init__ (self , return_code , stdout ):
88113 self .return_code = return_code
@@ -91,20 +116,19 @@ def __init__(self, return_code, stdout):
91116 logger .info (f"Executing command: { cmd } " )
92117 try :
93118 result = subprocess .run (
94- cmd ,
95- shell = shell ,
96- check = False ,
97- text = True ,
98- capture_output = True
119+ cmd , shell = shell , check = False , text = True , capture_output = True
99120 )
100121 logger .info (f"Command output: { result .stdout } " )
101122 if result .stderr :
102123 logger .error (f"Command error: { result .stderr } " )
103-
124+
104125 if expected_return_codes and result .returncode not in expected_return_codes :
105126 logger .error (f"Command failed with return code { result .returncode } " )
106-
107- return CommandResult (result .returncode , result .stdout + result .stderr if stderr_to_stdout else result .stdout )
127+
128+ return CommandResult (
129+ result .returncode ,
130+ result .stdout + result .stderr if stderr_to_stdout else result .stdout ,
131+ )
108132 except Exception as e :
109133 logger .error (f"Exception running command: { e } " )
110134 return CommandResult (1 , str (e ))
@@ -113,7 +137,7 @@ def __init__(self, return_code, stdout):
113137def create_test_files (test_dir , sample_size , sample_num , channel_num , frame_count ):
114138 """Create test PCM files for the integrity test"""
115139 frame_size = sample_size * sample_num * channel_num
116-
140+
117141 # Create source file
118142 source_file = test_dir / "source.pcm"
119143 with open (source_file , "wb" ) as f :
@@ -122,12 +146,12 @@ def create_test_files(test_dir, sample_size, sample_num, channel_num, frame_coun
122146 # This example creates a pattern based on the frame number
123147 pattern = bytes ([(i + j ) % 256 for j in range (frame_size )])
124148 f .write (pattern )
125-
149+
126150 # Create a matching destination file
127151 dest_file = test_dir / "dest.pcm"
128152 with open (source_file , "rb" ) as src , open (dest_file , "wb" ) as dst :
129153 dst .write (src .read ())
130-
154+
131155 # Create a corrupted file for additional testing (optional)
132156 corrupt_file = test_dir / "corrupt.pcm"
133157 with open (source_file , "rb" ) as src , open (corrupt_file , "wb" ) as dst :
@@ -139,36 +163,36 @@ def create_test_files(test_dir, sample_size, sample_num, channel_num, frame_coun
139163 for i in range (min (10 , frame_size )):
140164 corrupt_data [corrupt_pos + i ] = (corrupt_data [corrupt_pos + i ] + 123 ) % 256
141165 dst .write (corrupt_data )
142-
166+
143167 return source_file , dest_file
144168
145169
146170def main ():
147171 # Create temporary directory for test files
148172 with tempfile .TemporaryDirectory () as temp_dir :
149173 test_dir = Path (temp_dir )
150-
174+
151175 # Parameters for audio frames
152176 sample_size = 2 # 16-bit samples (2 bytes)
153177 sample_num = 480 # 480 samples per frame
154178 channel_num = 2 # stereo
155179 frame_count = 100 # generate 100 frames
156-
180+
157181 # Get the path to the repo
158182 repo_path = Path (__file__ ).parent .parent .parent
159-
183+
160184 # Create test files
161185 source_file , dest_file = create_test_files (
162186 test_dir , sample_size , sample_num , channel_num , frame_count
163187 )
164-
188+
165189 logger .info (f"Created test files in { test_dir } " )
166190 logger .info (f"Source file: { source_file } " )
167191 logger .info (f"Destination file: { dest_file } " )
168-
192+
169193 # Create local host for testing
170194 host = LocalHost ()
171-
195+
172196 # Test 1: Test the file audio integrity runner with valid file
173197 logger .info ("Test 1: Testing FileAudioIntegrityRunner with valid file..." )
174198 file_runner = FileAudioIntegrityRunner (
@@ -182,16 +206,16 @@ def main():
182206 out_path = str (test_dir ),
183207 delete_file = False ,
184208 )
185-
209+
186210 file_runner .setup ()
187211 result = file_runner .run ()
188-
212+
189213 if result :
190214 logger .info ("✅ Test 1: FileAudioIntegrityRunner with valid file - PASSED" )
191215 else :
192216 logger .error ("❌ Test 1: FileAudioIntegrityRunner with valid file - FAILED" )
193217 return 1
194-
218+
195219 # Test 2: Test with corrupted file (should fail)
196220 logger .info ("Test 2: Testing FileAudioIntegrityRunner with corrupted file..." )
197221 corrupt_runner = FileAudioIntegrityRunner (
@@ -205,16 +229,20 @@ def main():
205229 out_path = str (test_dir ),
206230 delete_file = False ,
207231 )
208-
232+
209233 corrupt_runner .setup ()
210234 corrupt_result = corrupt_runner .run ()
211-
235+
212236 if not corrupt_result :
213- logger .info ("✅ Test 2: FileAudioIntegrityRunner with corrupted file - PASSED (correctly detected corruption)" )
237+ logger .info (
238+ "✅ Test 2: FileAudioIntegrityRunner with corrupted file - PASSED (correctly detected corruption)"
239+ )
214240 else :
215- logger .error ("❌ Test 2: FileAudioIntegrityRunner with corrupted file - FAILED (did not detect corruption)" )
241+ logger .error (
242+ "❌ Test 2: FileAudioIntegrityRunner with corrupted file - FAILED (did not detect corruption)"
243+ )
216244 return 1
217-
245+
218246 return 0
219247
220248
0 commit comments