14
14
# See the License for the specific language governing permissions and
15
15
# limitations under the License.
16
16
17
+ import logging
17
18
import os
18
19
import subprocess
19
20
import sys
20
21
import time
21
22
22
23
import click
23
24
25
+ # Configure logging format.
26
+ logging .basicConfig (level = logging .DEBUG , format = '%(levelname)s - %(message)s' )
27
+
24
28
LINUX_TV_APP_LOGS = './scripts/tests/Linux-tv-app-logs.txt'
25
29
LINUX_TV_CASTING_APP_LOGS = './scripts/tests/Linux-tv-casting-app-logs.txt'
26
30
33
37
34
38
# Dump the logs to the console in the case of an error.
35
39
def dump_logs_to_console (log_file ):
40
+
36
41
if log_file == LINUX_TV_CASTING_APP_LOGS :
37
- print ('Dumping Linux TV Casting App Logs to Console.' )
42
+ logging . debug ('Dumping Linux TV Casting App Logs to Console.' )
38
43
elif log_file == LINUX_TV_APP_LOGS :
39
- print ('Dumping Linux TV App Logs to Console.' )
44
+ logging . debug ('Dumping Linux TV App Logs to Console.' )
40
45
41
46
with open (log_file , 'r' ) as file :
42
47
logs = file .read ()
43
- print (logs )
48
+ logging . debug (logs )
44
49
45
50
46
51
# Remove the log files once the script is done running.
47
52
def remove_log_file (log_file ):
53
+
48
54
if os .path .exists (log_file ):
49
55
os .remove (log_file )
50
56
else :
51
- print ( " The file does not exist." )
57
+ logging . error ( ' The file does not exist.' )
52
58
53
59
54
60
# Whenever a failure is discovered, we should print 'Discovery failed!',
55
61
# dump the logs, clean up the log files, exit on error.
56
62
def handle_discovery_failure ():
57
- print ('Discovery failed!\n ' )
63
+
64
+ logging .error ('Discovery failed!\n ' )
58
65
59
66
dump_logs_to_console (LINUX_TV_CASTING_APP_LOGS )
60
67
dump_logs_to_console (LINUX_TV_APP_LOGS )
@@ -67,7 +74,8 @@ def handle_discovery_failure():
67
74
68
75
# Helper function to extract the integer value from a string.
69
76
def extract_value_from_string (line ):
70
- value = line .split (":" )[- 1 ].strip ().replace ('\x1b [0m' , '' )
77
+
78
+ value = line .split (':' )[- 1 ].strip ().replace ('\x1b [0m' , '' )
71
79
value = int (value )
72
80
73
81
return value
@@ -76,23 +84,32 @@ def extract_value_from_string(line):
76
84
# Check if the discovered value matches the expected value.
77
85
# Returns False if the value does not match, True otherwise.
78
86
def validate_value (expected_value , line , value_name ):
87
+
79
88
# Extract the integer value from the string
80
89
value = extract_value_from_string (line )
81
90
82
91
# If the discovered value does not match the expected value,
83
92
# print the error and return False.
84
93
if value != expected_value :
85
- print (f'{ value_name } does not match the expected value!' )
86
- print (f'Expected { value_name } : { expected_value } ' )
94
+ logging . error (f'{ value_name } does not match the expected value!' )
95
+ logging . error (f'Expected { value_name } : { expected_value } ' )
87
96
line = line .rstrip ('\n ' )
88
- print (line )
97
+ logging . error (line )
89
98
90
99
return False
91
100
92
101
# Return True if the value matches the expected value
93
102
return True
94
103
95
104
105
+ # Tear down the processes once done running.
106
+ def tear_down (processes ):
107
+
108
+ for process in processes :
109
+ process .terminate ()
110
+ process .wait ()
111
+
112
+
96
113
# Test if the Linux tv-casting-app is able to discover the Linux tv-app.
97
114
# The Linux tv-casting-app and the tv-app will be run in separate processes.
98
115
# Their corresponding output will be written to their respective log files.
@@ -121,17 +138,20 @@ def test_discovery_fn():
121
138
122
139
# Read the output as we get it from the tv-casting-app process
123
140
for line in tv_casting_app_process .stdout :
141
+
124
142
# Write the line to the Linux tv-casting-app log file
125
143
fd2 .write (line )
126
144
127
145
# Fail fast if "No commissioner discovered" string found
128
146
if "No commissioner discovered" in line :
147
+
129
148
line = line .rstrip ('\n ' )
130
- print (line )
149
+ logging . error (line )
131
150
handle_discovery_failure ()
132
151
133
152
# Look for 'Discovered Commissioner'
134
153
if "Discovered Commissioner" in line :
154
+
135
155
line = line .rstrip ('\n ' )
136
156
valid_discovered_commissioner_str = line
137
157
@@ -192,23 +212,20 @@ def test_discovery_fn():
192
212
# We only print the discovered commissioner that has valid vendor id, product id,
193
213
# and device type. Remove the log files once done.
194
214
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!' )
215
+
216
+ logging .info (valid_discovered_commissioner_str )
217
+ logging .info (valid_vendor_id_str )
218
+ logging .info (valid_product_id_str )
219
+ logging .info (valid_device_type_str )
220
+ logging .info ('Discovery success!' )
200
221
201
222
remove_log_file (LINUX_TV_CASTING_APP_LOGS )
202
223
remove_log_file (LINUX_TV_APP_LOGS )
203
224
204
225
break
205
226
206
227
# Tear down the processes.
207
- tv_app_process .terminate ()
208
- tv_app_process .wait ()
209
-
210
- tv_casting_app_process .terminate ()
211
- tv_casting_app_process .wait ()
228
+ tear_down ([tv_app_process , tv_casting_app_process ])
212
229
213
230
214
231
@click .group ()
0 commit comments