Skip to content

Commit c43b59b

Browse files
Addressed @sharadb-amazon PR comments.
1 parent 0ac57cc commit c43b59b

File tree

1 file changed

+31
-30
lines changed

1 file changed

+31
-30
lines changed

scripts/tests/run_tv_casting_test.py

+31-30
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
TV_APP_MAX_START_WAIT_SEC = 2
3232

3333
# The maximum amount of time to commission the Linux tv-casting-app and the tv-app before timeout.
34-
COMMISSIONING_STAGE_MAX_WAIT_SEC = 3
34+
COMMISSIONING_STAGE_MAX_WAIT_SEC = 10
3535

3636
# File names of logs for the Linux tv-casting-app and the Linux tv-app.
3737
LINUX_TV_APP_LOGS = 'Linux-tv-app-logs.txt'
@@ -75,7 +75,7 @@ def dump_temporary_logs_to_console(log_file_path: str):
7575
print(line.rstrip())
7676

7777

78-
def handle_casting_state_failure(casting_state: str, log_file_paths: List[str]):
78+
def handle_casting_failure(casting_state: str, log_file_paths: List[str]):
7979
"""Log '{casting_state} failed!' as error, dump log files to console, exit on error."""
8080
logging.error(casting_state + ' failed!')
8181

@@ -88,10 +88,10 @@ def handle_casting_state_failure(casting_state: str, log_file_paths: List[str]):
8888
sys.exit(1)
8989

9090

91-
def extract_value_from_string(line: str) -> Union[int, str]:
92-
"""Extract and return either an integer or substring from given input string.
91+
def extract_value_from_string(line: str) -> str:
92+
"""Extract and return value from given input string.
9393
94-
The input string is expected to be in the following format as it is received
94+
The string is expected to be in the following format as it is received
9595
from the Linux tv-casting-app output:
9696
\x1b[0;34m[1713741926895] [7276:9521344] [DIS] Vendor ID: 65521\x1b[0m
9797
The integer value to be extracted here is 65521.
@@ -101,22 +101,21 @@ def extract_value_from_string(line: str) -> Union[int, str]:
101101
"""
102102
value = line.split(':')[-1].strip().replace('\x1b[0m', '')
103103

104-
try:
105-
value = int(value)
106-
return value
107-
except ValueError:
108-
return value
104+
return value
109105

110106

111-
def validate_value(casting_state: str, expected_value: int, log_paths: List[str], line: str, value_name: str) -> Optional[str]:
107+
def validate_value(casting_state: str, expected_value: Union[str, int], log_paths: List[str], line: str, value_name: str) -> Optional[str]:
112108
"""Validate a value in a string against an expected value during a given casting state."""
113109
value = extract_value_from_string(line)
114110

111+
if isinstance(expected_value, int):
112+
value = int(value)
113+
115114
if value != expected_value:
116115
logging.error(f'{value_name} does not match the expected value!')
117116
logging.error(f'Expected {value_name}: {expected_value}')
118117
logging.error(line.rstrip('\n'))
119-
handle_casting_state_failure(casting_state, log_paths)
118+
handle_casting_failure(casting_state, log_paths)
120119

121120
# Return the line containing the valid value.
122121
return line.rstrip('\n')
@@ -153,7 +152,7 @@ def initiate_cast_request_success(tv_casting_app_info: Tuple[subprocess.Popen, T
153152
# Check if we exceeded the maximum wait time for initiating 'cast request' from the Linux tv-casting-app to the Linux tv-app.
154153
if time.time() - start_wait_time > COMMISSIONING_STAGE_MAX_WAIT_SEC:
155154
logging.error('The command `cast request ' + valid_discovered_commissioner_number +
156-
'` was not sent to the Linux tv-casting-app process within the timeout.')
155+
'` was not issued to the Linux tv-casting-app process within the timeout.')
157156
return False
158157

159158
tv_casting_app_output_line = tv_casting_app_process.stdout.readline()
@@ -172,7 +171,7 @@ def initiate_cast_request_success(tv_casting_app_info: Tuple[subprocess.Popen, T
172171
return True
173172

174173

175-
def extract_device_info(tv_casting_app_info: Tuple[subprocess.Popen, TextIO]) -> Tuple[Optional[str], Optional[str], Optional[str]]:
174+
def extract_device_info_from_tv_casting_app(tv_casting_app_info: Tuple[subprocess.Popen, TextIO]) -> Tuple[Optional[str], Optional[int], Optional[int]]:
176175
"""Extract device information from the 'Identification Declaration' block in the Linux tv-casting-app output."""
177176
tv_casting_app_process, linux_tv_casting_app_log_file = tv_casting_app_info
178177

@@ -188,16 +187,18 @@ def extract_device_info(tv_casting_app_info: Tuple[subprocess.Popen, TextIO]) ->
188187
device_name = extract_value_from_string(line)
189188
elif 'vendor id' in line:
190189
vendor_id = extract_value_from_string(line)
190+
vendor_id = int(vendor_id)
191191
elif 'product id' in line:
192192
product_id = extract_value_from_string(line)
193+
product_id = int(product_id)
193194

194195
if device_name and vendor_id and product_id:
195196
break
196197

197198
return device_name, vendor_id, product_id
198199

199200

200-
def validate_tv_app_device_info(tv_app_info, device_name, vendor_id, product_id, log_paths) -> bool:
201+
def validate_identification_declaration_message_on_tv_app(tv_app_info: Tuple[subprocess.Popen, TextIO], expected_device_name: str, expected_vendor_id: int, expected_product_id: int, log_paths: List[str]) -> bool:
201202
"""Validate device information from the 'Identification Declaration' block from the Linux tv-app output against the expected values."""
202203
tv_app_process, linux_tv_app_log_file = tv_app_info
203204

@@ -223,18 +224,18 @@ def validate_tv_app_device_info(tv_app_info, device_name, vendor_id, product_id,
223224
elif parsing_identification_block:
224225
logging.info(tv_app_line.rstrip('\n'))
225226
if 'device Name' in tv_app_line:
226-
validate_value('Commissioning', device_name, log_paths, tv_app_line, 'device Name')
227+
validate_value('Commissioning', expected_device_name, log_paths, tv_app_line, 'device Name')
227228
elif 'vendor id' in tv_app_line:
228-
validate_value('Commissioning', vendor_id, log_paths, tv_app_line, 'vendor id')
229+
validate_value('Commissioning', expected_vendor_id, log_paths, tv_app_line, 'vendor id')
229230
elif 'product id' in tv_app_line:
230-
validate_value('Commissioning', product_id, log_paths, tv_app_line, 'product id')
231+
validate_value('Commissioning', expected_product_id, log_paths, tv_app_line, 'product id')
231232
elif 'Identification Declaration End' in tv_app_line:
232233
parsing_identification_block = False
233234
return True
234235

235236

236-
def approve_tv_casting_request(tv_app_info: Tuple[subprocess.Popen, TextIO], log_paths: List[str]) -> bool:
237-
"""Approve the TV casting request from the Linux tv-casting-app to the Linux tv-app by sending `controller ux ok` via Linux tv-app process."""
237+
def validate_tv_casting_request_approval(tv_app_info: Tuple[subprocess.Popen, TextIO], log_paths: List[str]) -> bool:
238+
"""Validate that the TV casting request from the Linux tv-casting-app to the Linux tv-app is approved by sending `controller ux ok` via Linux tv-app process."""
238239
tv_app_process, linux_tv_app_log_file = tv_app_info
239240

240241
start_wait_time = time.time()
@@ -323,7 +324,7 @@ def test_discovery_fn(tv_casting_app_info: Tuple[subprocess.Popen, TextIO], log_
323324
# Fail fast if "No commissioner discovered" string found.
324325
if "No commissioner discovered" in line:
325326
logging.error(line.rstrip('\n'))
326-
handle_casting_state_failure('Discovery', log_paths)
327+
handle_casting_failure('Discovery', log_paths)
327328

328329
elif "Discovered Commissioner" in line:
329330
valid_discovered_commissioner = line.rstrip('\n')
@@ -356,19 +357,19 @@ def test_commissioning_fn(valid_discovered_commissioner_number, tv_casting_app_i
356357
"""Test commissioning between Linux tv-casting-app and Linux tv-app."""
357358

358359
if not initiate_cast_request_success(tv_casting_app_info, valid_discovered_commissioner_number):
359-
handle_casting_state_failure('Commissioning', log_paths)
360+
handle_casting_failure('Commissioning', log_paths)
360361

361362
# Extract the values from the 'Identification Declaration' block in the tv-casting-app output that we want to validate against.
362-
device_name, vendor_id, product_id = extract_device_info(tv_casting_app_info)
363+
expected_device_name, expected_vendor_id, expected_product_id = extract_device_info_from_tv_casting_app(tv_casting_app_info)
363364

364-
if not validate_tv_app_device_info(tv_app_info, device_name, vendor_id, product_id, log_paths):
365-
handle_casting_state_failure('Commissioning', log_paths)
365+
if not validate_identification_declaration_message_on_tv_app(tv_app_info, expected_device_name, expected_vendor_id, expected_product_id, log_paths):
366+
handle_casting_failure('Commissioning', log_paths)
366367

367-
if not approve_tv_casting_request(tv_app_info, log_paths):
368-
handle_casting_state_failure('Commissioning', log_paths)
368+
if not validate_tv_casting_request_approval(tv_app_info, log_paths):
369+
handle_casting_failure('Commissioning', log_paths)
369370

370371
if not validate_commissioning_success(tv_casting_app_info, tv_app_info, log_paths):
371-
handle_casting_state_failure('Commissioning', log_paths)
372+
handle_casting_failure('Commissioning', log_paths)
372373

373374

374375
@click.command()
@@ -399,7 +400,7 @@ def test_casting_fn(tv_app_rel_path, tv_casting_app_rel_path):
399400
with ProcessManager(disable_stdout_buffering_cmd + [tv_app_abs_path], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as tv_app_process:
400401

401402
if not start_up_tv_app_success(tv_app_process, linux_tv_app_log_file):
402-
handle_casting_state_failure('Discovery', [linux_tv_app_log_path])
403+
handle_casting_failure('Discovery', [linux_tv_app_log_path])
403404

404405
tv_casting_app_abs_path = os.path.abspath(tv_casting_app_rel_path)
405406
# Run the Linux tv-casting-app subprocess.
@@ -410,7 +411,7 @@ def test_casting_fn(tv_app_rel_path, tv_casting_app_rel_path):
410411
valid_discovered_commissioner = test_discovery_fn(tv_casting_app_info, log_paths)
411412

412413
if not valid_discovered_commissioner:
413-
handle_casting_state_failure('Discovery', log_paths)
414+
handle_casting_failure('Discovery', log_paths)
414415

415416
# We need the valid discovered commissioner number to continue with commissioning.
416417
# Example string: \x1b[0;32m[1714582264602] [77989:2286038] [SVR] Discovered Commissioner #0\x1b[0m

0 commit comments

Comments
 (0)