4
4
# Copyright(c) 2024-2025 Intel Corporation
5
5
# Media Communications Mesh
6
6
7
- import argparse
8
7
import logging
9
- import os
10
8
import subprocess
11
9
import sys
12
10
import tempfile
13
11
from pathlib import Path
14
12
13
+
15
14
# Create mock runner classes for testing
16
15
class 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
+ ):
19
30
self .host = host
20
31
self .test_repo_path = test_repo_path
21
32
self .src_url = src_url
@@ -33,10 +44,12 @@ def __init__(self, host, test_repo_path, src_url, out_name, sample_size=2, sampl
33
44
# Get the directory where this script is located
34
45
script_dir = Path (__file__ ).parent
35
46
self .integrity_path = str (script_dir / "audio_integrity.py" )
36
-
47
+
37
48
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
+
40
53
def run (self ):
41
54
cmd = " " .join (
42
55
[
@@ -56,15 +69,22 @@ def run(self):
56
69
"--delete_file" if self .delete_file else "--no_delete_file" ,
57
70
]
58
71
)
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
+ )
61
78
if result .return_code > 0 :
62
79
logging .error (f"Audio integrity check failed on { self .host .name } : { self .out_name } " )
63
80
logging .error (result .stdout )
64
81
return False
65
- logging .info (f"Audio integrity check completed successfully on { self .host .name } for { self .out_name } " )
82
+ logging .info (
83
+ f"Audio integrity check completed successfully on { self .host .name } for { self .out_name } "
84
+ )
66
85
return True
67
86
87
+
68
88
# Set up logging
69
89
logging .basicConfig (
70
90
level = logging .INFO ,
@@ -75,14 +95,17 @@ def run(self):
75
95
76
96
class LocalHost :
77
97
"""Simple host class for testing that mimics the expected interface"""
98
+
78
99
def __init__ (self , name = "localhost" ):
79
100
self .name = name
80
101
self .connection = self
81
102
82
103
def path (self , * args ):
83
104
return Path (* args )
84
105
85
- def execute_command (self , cmd , shell = False , stderr_to_stdout = False , expected_return_codes = None ):
106
+ def execute_command (
107
+ self , cmd , shell = False , stderr_to_stdout = False , expected_return_codes = None
108
+ ):
86
109
class CommandResult :
87
110
def __init__ (self , return_code , stdout ):
88
111
self .return_code = return_code
@@ -91,20 +114,19 @@ def __init__(self, return_code, stdout):
91
114
logger .info (f"Executing command: { cmd } " )
92
115
try :
93
116
result = subprocess .run (
94
- cmd ,
95
- shell = shell ,
96
- check = False ,
97
- text = True ,
98
- capture_output = True
117
+ cmd , shell = shell , check = False , text = True , capture_output = True
99
118
)
100
119
logger .info (f"Command output: { result .stdout } " )
101
120
if result .stderr :
102
121
logger .error (f"Command error: { result .stderr } " )
103
-
122
+
104
123
if expected_return_codes and result .returncode not in expected_return_codes :
105
124
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 )
125
+
126
+ return CommandResult (
127
+ result .returncode ,
128
+ result .stdout + result .stderr if stderr_to_stdout else result .stdout ,
129
+ )
108
130
except Exception as e :
109
131
logger .error (f"Exception running command: { e } " )
110
132
return CommandResult (1 , str (e ))
@@ -113,7 +135,7 @@ def __init__(self, return_code, stdout):
113
135
def create_test_files (test_dir , sample_size , sample_num , channel_num , frame_count ):
114
136
"""Create test PCM files for the integrity test"""
115
137
frame_size = sample_size * sample_num * channel_num
116
-
138
+
117
139
# Create source file
118
140
source_file = test_dir / "source.pcm"
119
141
with open (source_file , "wb" ) as f :
@@ -122,12 +144,12 @@ def create_test_files(test_dir, sample_size, sample_num, channel_num, frame_coun
122
144
# This example creates a pattern based on the frame number
123
145
pattern = bytes ([(i + j ) % 256 for j in range (frame_size )])
124
146
f .write (pattern )
125
-
147
+
126
148
# Create a matching destination file
127
149
dest_file = test_dir / "dest.pcm"
128
150
with open (source_file , "rb" ) as src , open (dest_file , "wb" ) as dst :
129
151
dst .write (src .read ())
130
-
152
+
131
153
# Create a corrupted file for additional testing (optional)
132
154
corrupt_file = test_dir / "corrupt.pcm"
133
155
with open (source_file , "rb" ) as src , open (corrupt_file , "wb" ) as dst :
@@ -139,36 +161,36 @@ def create_test_files(test_dir, sample_size, sample_num, channel_num, frame_coun
139
161
for i in range (min (10 , frame_size )):
140
162
corrupt_data [corrupt_pos + i ] = (corrupt_data [corrupt_pos + i ] + 123 ) % 256
141
163
dst .write (corrupt_data )
142
-
164
+
143
165
return source_file , dest_file
144
166
145
167
146
168
def main ():
147
169
# Create temporary directory for test files
148
170
with tempfile .TemporaryDirectory () as temp_dir :
149
171
test_dir = Path (temp_dir )
150
-
172
+
151
173
# Parameters for audio frames
152
174
sample_size = 2 # 16-bit samples (2 bytes)
153
175
sample_num = 480 # 480 samples per frame
154
176
channel_num = 2 # stereo
155
177
frame_count = 100 # generate 100 frames
156
-
178
+
157
179
# Get the path to the repo
158
180
repo_path = Path (__file__ ).parent .parent .parent
159
-
181
+
160
182
# Create test files
161
183
source_file , dest_file = create_test_files (
162
184
test_dir , sample_size , sample_num , channel_num , frame_count
163
185
)
164
-
186
+
165
187
logger .info (f"Created test files in { test_dir } " )
166
188
logger .info (f"Source file: { source_file } " )
167
189
logger .info (f"Destination file: { dest_file } " )
168
-
190
+
169
191
# Create local host for testing
170
192
host = LocalHost ()
171
-
193
+
172
194
# Test 1: Test the file audio integrity runner with valid file
173
195
logger .info ("Test 1: Testing FileAudioIntegrityRunner with valid file..." )
174
196
file_runner = FileAudioIntegrityRunner (
@@ -182,16 +204,16 @@ def main():
182
204
out_path = str (test_dir ),
183
205
delete_file = False ,
184
206
)
185
-
207
+
186
208
file_runner .setup ()
187
209
result = file_runner .run ()
188
-
210
+
189
211
if result :
190
212
logger .info ("✅ Test 1: FileAudioIntegrityRunner with valid file - PASSED" )
191
213
else :
192
214
logger .error ("❌ Test 1: FileAudioIntegrityRunner with valid file - FAILED" )
193
215
return 1
194
-
216
+
195
217
# Test 2: Test with corrupted file (should fail)
196
218
logger .info ("Test 2: Testing FileAudioIntegrityRunner with corrupted file..." )
197
219
corrupt_runner = FileAudioIntegrityRunner (
@@ -205,16 +227,20 @@ def main():
205
227
out_path = str (test_dir ),
206
228
delete_file = False ,
207
229
)
208
-
230
+
209
231
corrupt_runner .setup ()
210
232
corrupt_result = corrupt_runner .run ()
211
-
233
+
212
234
if not corrupt_result :
213
- logger .info ("✅ Test 2: FileAudioIntegrityRunner with corrupted file - PASSED (correctly detected corruption)" )
235
+ logger .info (
236
+ "✅ Test 2: FileAudioIntegrityRunner with corrupted file - PASSED (correctly detected corruption)"
237
+ )
214
238
else :
215
- logger .error ("❌ Test 2: FileAudioIntegrityRunner with corrupted file - FAILED (did not detect corruption)" )
239
+ logger .error (
240
+ "❌ Test 2: FileAudioIntegrityRunner with corrupted file - FAILED (did not detect corruption)"
241
+ )
216
242
return 1
217
-
243
+
218
244
return 0
219
245
220
246
0 commit comments