Skip to content

Commit 202df13

Browse files
committedApr 16, 2024
Addressed PR comments and re-organized code structure.
1 parent c3dfaf7 commit 202df13

File tree

1 file changed

+85
-38
lines changed

1 file changed

+85
-38
lines changed
 

‎scripts/tests/run_tv_casting_test.py

+85-38
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@
2121

2222
import click
2323

24+
LINUX_TV_APP_LOGS = './scripts/tests/Linux-tv-app-logs.txt'
25+
LINUX_TV_CASTING_APP_LOGS = './scripts/tests/Linux-tv-casting-app-logs.txt'
26+
27+
RUN_INTERVAL = 5
28+
PARSE_INTERVAL = 15
29+
30+
VENDOR_ID = 65521
31+
PRODUCT_ID = 32769
32+
DEVICE_TYPE = 35
33+
2434

2535
# Dump the logs to the console in the case of an error.
2636
def dump_logs_to_console(log_file):
@@ -37,10 +47,46 @@ def remove_log_file(log_file):
3747
print("The file does not exist.")
3848

3949

50+
# Whenever a failure is discovered, we should print 'Discovery failed!',
51+
# dump the logs, clean up the log files, exit on error.
52+
def handle_discovery_failure():
53+
print('Discovery failed!')
54+
55+
dump_logs_to_console(LINUX_TV_CASTING_APP_LOGS)
56+
57+
remove_log_file(LINUX_TV_CASTING_APP_LOGS)
58+
remove_log_file(LINUX_TV_APP_LOGS)
59+
60+
sys.exit(1)
61+
62+
63+
# Helper function to extract the integer value from a string.
64+
def extract_value_from_string(line):
65+
value = line.split(":")[-1].strip().replace('\x1b[0m', '')
66+
value = int(value)
67+
68+
return value
69+
70+
71+
# Check if the discovered value matches the expected value.
72+
def check_expected_value(line, expected_value, value_name):
73+
# Extract the integer value from the string
74+
value = extract_value_from_string(line)
75+
76+
# If the discovered value does not match the expected value,
77+
# print the error and handle the discovery failure.
78+
if value != expected_value:
79+
print(f'{value_name} does not match the expected value!')
80+
print(f'Discovered {value_name}: {value}')
81+
print(f'Expected {value_name}: {expected_value}')
82+
83+
handle_discovery_failure()
84+
85+
4086
# Read the logs from the Linux-tv-casting-app-logs.txt file.
4187
# The discovered commissioner(s) will be stored in a list along with their
4288
# vendor ID, product ID, and device type.
43-
def read_linux_tv_casting_app_logs(log_file):
89+
def parse_linux_tv_casting_app_logs(log_file):
4490

4591
with open(log_file, 'r') as file:
4692
lines = file.readlines()
@@ -57,24 +103,11 @@ def read_linux_tv_casting_app_logs(log_file):
57103
print(line)
58104
print('Discovery success!')
59105

60-
remove_log_file('./scripts/tests/Linux-tv-casting-app-logs.txt')
61-
remove_log_file('./scripts/tests/Linux-tv-app-logs.txt')
106+
remove_log_file(LINUX_TV_CASTING_APP_LOGS)
107+
remove_log_file(LINUX_TV_APP_LOGS)
62108

63109
break
64110

65-
# If no commissioner was discovered, then something went wrong.
66-
# Exit on error.
67-
if "No commissioner discovered" in line:
68-
print(line)
69-
print('Discovery failed!')
70-
71-
dump_logs_to_console('./scripts/tests/Linux-tv-casting-app-logs.txt')
72-
73-
remove_log_file('./scripts/tests/Linux-tv-casting-app-logs.txt')
74-
remove_log_file('./scripts/tests/Linux-tv-app-logs.txt')
75-
76-
sys.exit(1)
77-
78111
# Look for "Discovered Commissioner"
79112
if "Discovered Commissioner" in line:
80113
print(line)
@@ -93,21 +126,15 @@ def read_linux_tv_casting_app_logs(log_file):
93126

94127
if "Vendor ID:" in next_line:
95128
print(next_line)
96-
97-
vendor_id = next_line.split(":")[-1].strip()
98-
vendor_id = vendor_id.replace('\x1b[0m', '')
129+
vendor_id = extract_value_from_string(next_line)
99130

100131
elif "Product ID:" in next_line:
101132
print(next_line)
102-
103-
product_id = next_line.split(":")[-1].strip()
104-
product_id = product_id.replace('\x1b[0m', '')
133+
product_id = extract_value_from_string(next_line)
105134

106135
elif "Device Type:" in next_line:
107136
print(next_line)
108-
109-
device_type = next_line.split(":")[-1].strip()
110-
device_type = device_type.replace('\x1b[0m', '')
137+
device_type = extract_value_from_string(next_line)
111138

112139
elif "commissioner(s) discovered" in next_line:
113140
break
@@ -127,14 +154,8 @@ def read_linux_tv_casting_app_logs(log_file):
127154
# If the list of discovered commissioners is empty and we didn't find the "No commissioner discovered" string,
128155
# then something went wrong. Exit on error.
129156
if len(discovered_commissioners) == 0:
130-
print('Discovery failed! No commissioner(s) discovered! The list of discovered commissioner(s) is empty!')
131-
132-
dump_logs_to_console('./scripts/tests/Linux-tv-casting-app-logs.txt')
133-
134-
remove_log_file('./scripts/tests/Linux-tv-casting-app-logs.txt')
135-
remove_log_file('./scripts/tests/Linux-tv-app-logs.txt')
136-
137-
sys.exit(1)
157+
print('No commissioner(s) discovered! The list of discovered commissioner(s) is empty!')
158+
handle_discovery_failure()
138159

139160

140161
# Test if the Linux tv-casting-app is able to discover the Linux tv-app.
@@ -143,24 +164,48 @@ def read_linux_tv_casting_app_logs(log_file):
143164
# The log file of the tv-casting-app will be parsed for strings of interest
144165
# which will be printed to the console.
145166
def test_discovery_fn():
146-
with open('./scripts/tests/Linux-tv-app-logs.txt', 'w') as fd1, open('./scripts/tests/Linux-tv-casting-app-logs.txt', 'w') as fd2:
167+
168+
with open(LINUX_TV_APP_LOGS, 'w') as fd1, open(LINUX_TV_CASTING_APP_LOGS, 'w') as fd2:
147169

148170
# Run the Linux tv-app and write the output to file
149171
tv_app_rel_path = 'out/tv-app/chip-tv-app'
150172
tv_app_abs_path = os.path.abspath(tv_app_rel_path)
151173
subprocess.Popen(tv_app_abs_path, stdout=fd1, stderr=subprocess.PIPE, text=True)
152174

153-
time.sleep(5)
175+
time.sleep(RUN_INTERVAL)
154176

155177
# Run the Linux tv-casting-app and write the output to file
156178
tv_casting_app_rel_path = 'out/tv-casting-app/chip-tv-casting-app'
157179
tv_casting_app_abs_path = os.path.abspath(tv_casting_app_rel_path)
158-
subprocess.Popen(tv_casting_app_abs_path, stdout=fd2, stderr=subprocess.PIPE, text=True)
180+
tv_casting_app_process = subprocess.Popen(
181+
tv_casting_app_abs_path, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
182+
183+
for line in tv_casting_app_process.stdout:
184+
# Write the line to the Linux tv-casting-app log file
185+
fd2.write(line)
186+
187+
# Fail fast if "No commissioner discovered" string found
188+
if "No commissioner discovered" in line:
189+
print(line)
190+
handle_discovery_failure()
191+
192+
# Check if the Vendor ID, Product ID, and Device Type match the expected values
193+
if "Vendor ID:" in line:
194+
check_expected_value(line, VENDOR_ID, "Vendor ID")
195+
196+
elif "Product ID:" in line:
197+
check_expected_value(line, PRODUCT_ID, "Product ID")
198+
199+
elif "Device Type:" in line:
200+
check_expected_value(line, DEVICE_TYPE, "Device Type")
201+
202+
if "commissioner(s) discovered" in line:
203+
break
159204

160205
# Wait for the processes to finish writing before attempting to read
161-
time.sleep(15)
206+
time.sleep(PARSE_INTERVAL)
162207

163-
read_linux_tv_casting_app_logs('./scripts/tests/Linux-tv-casting-app-logs.txt')
208+
parse_linux_tv_casting_app_logs(LINUX_TV_CASTING_APP_LOGS)
164209

165210

166211
@click.group()
@@ -174,5 +219,7 @@ def test_discovery():
174219

175220

176221
if __name__ == '__main__':
222+
# Start with a clean slate by removing any previously cached entries.
223+
os.system('rm -f /tmp/chip_*')
177224

178225
main()

0 commit comments

Comments
 (0)