24
24
LINUX_TV_APP_LOGS = './scripts/tests/Linux-tv-app-logs.txt'
25
25
LINUX_TV_CASTING_APP_LOGS = './scripts/tests/Linux-tv-casting-app-logs.txt'
26
26
27
- RUN_INTERVAL = 5
28
- PARSE_INTERVAL = 15
27
+ RUN_INTERVAL = 2
29
28
30
29
VENDOR_ID = 65521
31
30
PRODUCT_ID = 32769
34
33
35
34
# Dump the logs to the console in the case of an error.
36
35
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
+
37
41
with open (log_file , 'r' ) as file :
38
42
logs = file .read ()
39
43
print (logs )
@@ -50,9 +54,10 @@ def remove_log_file(log_file):
50
54
# Whenever a failure is discovered, we should print 'Discovery failed!',
51
55
# dump the logs, clean up the log files, exit on error.
52
56
def handle_discovery_failure ():
53
- print ('Discovery failed!' )
57
+ print ('Discovery failed!\n ' )
54
58
55
59
dump_logs_to_console (LINUX_TV_CASTING_APP_LOGS )
60
+ dump_logs_to_console (LINUX_TV_APP_LOGS )
56
61
57
62
remove_log_file (LINUX_TV_CASTING_APP_LOGS )
58
63
remove_log_file (LINUX_TV_APP_LOGS )
@@ -69,152 +74,150 @@ def extract_value_from_string(line):
69
74
70
75
71
76
# 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 ):
73
79
# Extract the integer value from the string
74
80
value = extract_value_from_string (line )
75
81
76
82
# 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 .
78
84
if value != expected_value :
79
85
print (f'{ value_name } does not match the expected value!' )
80
- print (f'Discovered { value_name } : { value } ' )
81
86
print (f'Expected { value_name } : { expected_value } ' )
87
+ line = line .rstrip ('\n ' )
88
+ print (line )
82
89
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
108
91
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
159
94
160
95
161
96
# Test if the Linux tv-casting-app is able to discover the Linux tv-app.
162
97
# The Linux tv-casting-app and the tv-app will be run in separate processes.
163
98
# 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.
166
101
def test_discovery_fn ():
167
102
168
103
with open (LINUX_TV_APP_LOGS , 'w' ) as fd1 , open (LINUX_TV_CASTING_APP_LOGS , 'w' ) as fd2 :
169
104
170
105
# Run the Linux tv-app and write the output to file
171
106
tv_app_rel_path = 'out/tv-app/chip-tv-app'
172
107
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 )
174
109
175
110
time .sleep (RUN_INTERVAL )
176
111
177
- # Run the Linux tv-casting-app and write the output to file
112
+ # Run the Linux tv-casting-app
178
113
tv_casting_app_rel_path = 'out/tv-casting-app/chip-tv-casting-app'
179
114
tv_casting_app_abs_path = os .path .abspath (tv_casting_app_rel_path )
180
115
tv_casting_app_process = subprocess .Popen (
181
116
tv_casting_app_abs_path , stdout = subprocess .PIPE , stderr = subprocess .PIPE , text = True )
182
117
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
183
123
for line in tv_casting_app_process .stdout :
184
124
# Write the line to the Linux tv-casting-app log file
185
125
fd2 .write (line )
186
126
187
127
# Fail fast if "No commissioner discovered" string found
188
128
if "No commissioner discovered" in line :
129
+ line = line .rstrip ('\n ' )
189
130
print (line )
190
131
handle_discovery_failure ()
191
132
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" )
195
158
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
198
164
199
- elif "Device Type:" in line :
200
- check_expected_value (line , DEVICE_TYPE , "Device Type" )
165
+ elif "Product ID:" in line :
201
166
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
204
205
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 ()
207
209
208
- parse_linux_tv_casting_app_logs (LINUX_TV_CASTING_APP_LOGS )
210
+ tv_casting_app_process .terminate ()
211
+ tv_casting_app_process .wait ()
209
212
210
213
211
214
@click .group ()
212
215
def main ():
213
216
pass
214
217
215
218
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 ():
218
221
test_discovery_fn ()
219
222
220
223
0 commit comments