Skip to content

Commit e7fb2fe

Browse files
Addressed PR comments - Changed workflow step name and command name, updated validate_value function, and added succeeding fast logic.
1 parent 202df13 commit e7fb2fe

File tree

2 files changed

+106
-102
lines changed

2 files changed

+106
-102
lines changed

.github/workflows/examples-linux-tv-casting-app.yaml

+4-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ name: Test TV Casting Example
1717
on:
1818
push:
1919
branches-ignore:
20-
- 'dependabot/**'
20+
- "dependabot/**"
2121
pull_request:
2222
merge_group:
2323

@@ -63,10 +63,11 @@ jobs:
6363
./scripts/run_in_build_env.sh \
6464
"scripts/examples/gn_build_example.sh examples/tv-casting-app/linux/ out/tv-casting-app"
6565
66-
- name: Test Discovery between Linux tv-casting-app and Linux tv-app
66+
- name: Test casting from Linux tv-casting-app to Linux tv-app
6767
run: |
6868
./scripts/run_in_build_env.sh \
69-
"python3 ./scripts/tests/run_tv_casting_test.py test-discovery"
69+
"python3 ./scripts/tests/run_tv_casting_test.py test-casting"
70+
timeout-minutes: 1
7071

7172
- name: Uploading Size Reports
7273
uses: ./.github/actions/upload-size-reports

scripts/tests/run_tv_casting_test.py

+102-99
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
LINUX_TV_APP_LOGS = './scripts/tests/Linux-tv-app-logs.txt'
2525
LINUX_TV_CASTING_APP_LOGS = './scripts/tests/Linux-tv-casting-app-logs.txt'
2626

27-
RUN_INTERVAL = 5
28-
PARSE_INTERVAL = 15
27+
RUN_INTERVAL = 2
2928

3029
VENDOR_ID = 65521
3130
PRODUCT_ID = 32769
@@ -34,6 +33,11 @@
3433

3534
# Dump the logs to the console in the case of an error.
3635
def dump_logs_to_console(log_file):
36+
if log_file == LINUX_TV_CASTING_APP_LOGS:
37+
print('Dumping Linux TV Casting App Logs to Console.')
38+
elif log_file == LINUX_TV_APP_LOGS:
39+
print('Dumping Linux TV App Logs to Console.')
40+
3741
with open(log_file, 'r') as file:
3842
logs = file.read()
3943
print(logs)
@@ -50,9 +54,10 @@ def remove_log_file(log_file):
5054
# Whenever a failure is discovered, we should print 'Discovery failed!',
5155
# dump the logs, clean up the log files, exit on error.
5256
def handle_discovery_failure():
53-
print('Discovery failed!')
57+
print('Discovery failed!\n')
5458

5559
dump_logs_to_console(LINUX_TV_CASTING_APP_LOGS)
60+
dump_logs_to_console(LINUX_TV_APP_LOGS)
5661

5762
remove_log_file(LINUX_TV_CASTING_APP_LOGS)
5863
remove_log_file(LINUX_TV_APP_LOGS)
@@ -69,152 +74,150 @@ def extract_value_from_string(line):
6974

7075

7176
# Check if the discovered value matches the expected value.
72-
def check_expected_value(line, expected_value, value_name):
77+
# Returns False if the value does not match, True otherwise.
78+
def validate_value(expected_value, line, value_name):
7379
# Extract the integer value from the string
7480
value = extract_value_from_string(line)
7581

7682
# If the discovered value does not match the expected value,
77-
# print the error and handle the discovery failure.
83+
# print the error and return False.
7884
if value != expected_value:
7985
print(f'{value_name} does not match the expected value!')
80-
print(f'Discovered {value_name}: {value}')
8186
print(f'Expected {value_name}: {expected_value}')
87+
line = line.rstrip('\n')
88+
print(line)
8289

83-
handle_discovery_failure()
84-
85-
86-
# Read the logs from the Linux-tv-casting-app-logs.txt file.
87-
# The discovered commissioner(s) will be stored in a list along with their
88-
# vendor ID, product ID, and device type.
89-
def parse_linux_tv_casting_app_logs(log_file):
90-
91-
with open(log_file, 'r') as file:
92-
lines = file.readlines()
93-
94-
discovered_commissioners = []
95-
96-
print('Reading from Linux-tv-casting-app-logs.txt')
97-
98-
# Read through the Linux-tv-casting-app-logs.txt line by line
99-
for i, line in enumerate(lines):
100-
101-
# If commissioner(s) are discovered, then the discovery process was successful.
102-
if "commissioner(s) discovered" in line:
103-
print(line)
104-
print('Discovery success!')
105-
106-
remove_log_file(LINUX_TV_CASTING_APP_LOGS)
107-
remove_log_file(LINUX_TV_APP_LOGS)
90+
return False
10891

109-
break
110-
111-
# Look for "Discovered Commissioner"
112-
if "Discovered Commissioner" in line:
113-
print(line)
114-
115-
# Extract the relevant part of the string
116-
commissioner = line.split("Discovered Commissioner")[-1].strip()
117-
commissioner = commissioner.replace('\x1b[0m', '')
118-
119-
# Initialize variables for Vendor ID, Product ID, and Device Type
120-
vendor_id = None
121-
product_id = None
122-
device_type = None
123-
124-
# Iterate through the subsequent lines to find the strings of interest
125-
for next_line in lines[i+1:]:
126-
127-
if "Vendor ID:" in next_line:
128-
print(next_line)
129-
vendor_id = extract_value_from_string(next_line)
130-
131-
elif "Product ID:" in next_line:
132-
print(next_line)
133-
product_id = extract_value_from_string(next_line)
134-
135-
elif "Device Type:" in next_line:
136-
print(next_line)
137-
device_type = extract_value_from_string(next_line)
138-
139-
elif "commissioner(s) discovered" in next_line:
140-
break
141-
142-
# If the next line starts with "Discovered Commissioner", break the loop
143-
if "Discovered Commissioner" in next_line:
144-
break
145-
146-
# Append the extracted information to the devices list
147-
discovered_commissioners.append({
148-
"discovered_commissioner": commissioner,
149-
"vendor_id": vendor_id,
150-
"product_id": product_id,
151-
"device_type": device_type
152-
})
153-
154-
# If the list of discovered commissioners is empty and we didn't find the "No commissioner discovered" string,
155-
# then something went wrong. Exit on error.
156-
if len(discovered_commissioners) == 0:
157-
print('No commissioner(s) discovered! The list of discovered commissioner(s) is empty!')
158-
handle_discovery_failure()
92+
# Return True if the value matches the expected value
93+
return True
15994

16095

16196
# Test if the Linux tv-casting-app is able to discover the Linux tv-app.
16297
# The Linux tv-casting-app and the tv-app will be run in separate processes.
16398
# Their corresponding output will be written to their respective log files.
164-
# The log file of the tv-casting-app will be parsed for strings of interest
165-
# which will be printed to the console.
99+
# The output of the tv-casting-app will be parsed in realtime for strings of
100+
# interest which will be printed to the console.
166101
def test_discovery_fn():
167102

168103
with open(LINUX_TV_APP_LOGS, 'w') as fd1, open(LINUX_TV_CASTING_APP_LOGS, 'w') as fd2:
169104

170105
# Run the Linux tv-app and write the output to file
171106
tv_app_rel_path = 'out/tv-app/chip-tv-app'
172107
tv_app_abs_path = os.path.abspath(tv_app_rel_path)
173-
subprocess.Popen(tv_app_abs_path, stdout=fd1, stderr=subprocess.PIPE, text=True)
108+
tv_app_process = subprocess.Popen(tv_app_abs_path, stdout=fd1, stderr=subprocess.PIPE, text=True)
174109

175110
time.sleep(RUN_INTERVAL)
176111

177-
# Run the Linux tv-casting-app and write the output to file
112+
# Run the Linux tv-casting-app
178113
tv_casting_app_rel_path = 'out/tv-casting-app/chip-tv-casting-app'
179114
tv_casting_app_abs_path = os.path.abspath(tv_casting_app_rel_path)
180115
tv_casting_app_process = subprocess.Popen(
181116
tv_casting_app_abs_path, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
182117

118+
# Initialize variables
119+
continue_parsing = False
120+
valid_discovered_commissioner_str = ''
121+
122+
# Read the output as we get it from the tv-casting-app process
183123
for line in tv_casting_app_process.stdout:
184124
# Write the line to the Linux tv-casting-app log file
185125
fd2.write(line)
186126

187127
# Fail fast if "No commissioner discovered" string found
188128
if "No commissioner discovered" in line:
129+
line = line.rstrip('\n')
189130
print(line)
190131
handle_discovery_failure()
191132

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")
133+
# Look for 'Discovered Commissioner'
134+
if "Discovered Commissioner" in line:
135+
line = line.rstrip('\n')
136+
valid_discovered_commissioner_str = line
137+
138+
# Continue parsing the content that belongs to the "Discovered Commissioner"
139+
continue_parsing = True
140+
141+
# Initialize variables to store the information of interest
142+
valid_vendor_id = False
143+
valid_product_id = False
144+
valid_device_type = False
145+
146+
valid_vendor_id_str = ''
147+
valid_product_id_str = ''
148+
valid_device_type_str = ''
149+
150+
if continue_parsing:
151+
152+
# Check if the Vendor ID, Product ID, and Device Type match the expected values
153+
if "Vendor ID:" in line:
154+
155+
# If the value of the Vendor ID does not match the expected value, then
156+
# handle the discovery failure.
157+
valid_vendor_id = validate_value(VENDOR_ID, line, "Vendor ID")
195158

196-
elif "Product ID:" in line:
197-
check_expected_value(line, PRODUCT_ID, "Product ID")
159+
if not valid_vendor_id:
160+
handle_discovery_failure()
161+
else:
162+
line = line.rstrip('\n')
163+
valid_vendor_id_str = line
198164

199-
elif "Device Type:" in line:
200-
check_expected_value(line, DEVICE_TYPE, "Device Type")
165+
elif "Product ID:" in line:
201166

202-
if "commissioner(s) discovered" in line:
203-
break
167+
# If the value of Product ID does not match the expected value, then
168+
# handle the discovery failure.
169+
valid_product_id = validate_value(PRODUCT_ID, line, "Product ID")
170+
171+
if not valid_product_id:
172+
handle_discovery_failure()
173+
else:
174+
line = line.rstrip('\n')
175+
valid_product_id_str = line
176+
177+
elif "Device Type:" in line:
178+
179+
# If the value of Device Type does not match the expected value, then
180+
# handle the discovery failure.
181+
valid_device_type = validate_value(DEVICE_TYPE, line, "Device Type")
182+
183+
if not valid_device_type:
184+
handle_discovery_failure()
185+
else:
186+
line = line.rstrip('\n')
187+
valid_device_type_str = line
188+
189+
# At this point, all values of interest are valid, so we stop parsing.
190+
continue_parsing = False
191+
192+
# We only print the discovered commissioner that has valid vendor id, product id,
193+
# and device type. Remove the log files once done.
194+
if valid_vendor_id and valid_product_id and valid_device_type:
195+
print(valid_discovered_commissioner_str)
196+
print(valid_vendor_id_str)
197+
print(valid_product_id_str)
198+
print(valid_device_type_str)
199+
print('Discovery success!')
200+
201+
remove_log_file(LINUX_TV_CASTING_APP_LOGS)
202+
remove_log_file(LINUX_TV_APP_LOGS)
203+
204+
break
204205

205-
# Wait for the processes to finish writing before attempting to read
206-
time.sleep(PARSE_INTERVAL)
206+
# Tear down the processes.
207+
tv_app_process.terminate()
208+
tv_app_process.wait()
207209

208-
parse_linux_tv_casting_app_logs(LINUX_TV_CASTING_APP_LOGS)
210+
tv_casting_app_process.terminate()
211+
tv_casting_app_process.wait()
209212

210213

211214
@click.group()
212215
def main():
213216
pass
214217

215218

216-
@main.command('test-discovery', help='Test if the Linux tv-casting-app is able to discover the Linux tv-app.')
217-
def test_discovery():
219+
@main.command('test-casting', help='Test casting from Linux tv-casting-app to Linux tv-app.')
220+
def test_casting():
218221
test_discovery_fn()
219222

220223

0 commit comments

Comments
 (0)