21
21
22
22
import click
23
23
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
+
24
34
25
35
# Dump the logs to the console in the case of an error.
26
36
def dump_logs_to_console (log_file ):
@@ -37,10 +47,46 @@ def remove_log_file(log_file):
37
47
print ("The file does not exist." )
38
48
39
49
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
+
40
86
# Read the logs from the Linux-tv-casting-app-logs.txt file.
41
87
# The discovered commissioner(s) will be stored in a list along with their
42
88
# 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 ):
44
90
45
91
with open (log_file , 'r' ) as file :
46
92
lines = file .readlines ()
@@ -57,24 +103,11 @@ def read_linux_tv_casting_app_logs(log_file):
57
103
print (line )
58
104
print ('Discovery success!' )
59
105
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 )
62
108
63
109
break
64
110
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
-
78
111
# Look for "Discovered Commissioner"
79
112
if "Discovered Commissioner" in line :
80
113
print (line )
@@ -93,21 +126,15 @@ def read_linux_tv_casting_app_logs(log_file):
93
126
94
127
if "Vendor ID:" in next_line :
95
128
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 )
99
130
100
131
elif "Product ID:" in next_line :
101
132
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 )
105
134
106
135
elif "Device Type:" in next_line :
107
136
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 )
111
138
112
139
elif "commissioner(s) discovered" in next_line :
113
140
break
@@ -127,14 +154,8 @@ def read_linux_tv_casting_app_logs(log_file):
127
154
# If the list of discovered commissioners is empty and we didn't find the "No commissioner discovered" string,
128
155
# then something went wrong. Exit on error.
129
156
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 ()
138
159
139
160
140
161
# 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):
143
164
# The log file of the tv-casting-app will be parsed for strings of interest
144
165
# which will be printed to the console.
145
166
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 :
147
169
148
170
# Run the Linux tv-app and write the output to file
149
171
tv_app_rel_path = 'out/tv-app/chip-tv-app'
150
172
tv_app_abs_path = os .path .abspath (tv_app_rel_path )
151
173
subprocess .Popen (tv_app_abs_path , stdout = fd1 , stderr = subprocess .PIPE , text = True )
152
174
153
- time .sleep (5 )
175
+ time .sleep (RUN_INTERVAL )
154
176
155
177
# Run the Linux tv-casting-app and write the output to file
156
178
tv_casting_app_rel_path = 'out/tv-casting-app/chip-tv-casting-app'
157
179
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
159
204
160
205
# Wait for the processes to finish writing before attempting to read
161
- time .sleep (15 )
206
+ time .sleep (PARSE_INTERVAL )
162
207
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 )
164
209
165
210
166
211
@click .group ()
@@ -174,5 +219,7 @@ def test_discovery():
174
219
175
220
176
221
if __name__ == '__main__' :
222
+ # Start with a clean slate by removing any previously cached entries.
223
+ os .system ('rm -f /tmp/chip_*' )
177
224
178
225
main ()
0 commit comments