Skip to content

Commit 5fc0f97

Browse files
Addressed PR comments - Created def tear_down and utilized logging module instead of print statements.
1 parent ea608c5 commit 5fc0f97

File tree

1 file changed

+37
-20
lines changed

1 file changed

+37
-20
lines changed

scripts/tests/run_tv_casting_test.py

+37-20
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17+
import logging
1718
import os
1819
import subprocess
1920
import sys
2021
import time
2122

2223
import click
2324

25+
# Configure logging format.
26+
logging.basicConfig(level=logging.DEBUG, format='%(levelname)s - %(message)s')
27+
2428
LINUX_TV_APP_LOGS = './scripts/tests/Linux-tv-app-logs.txt'
2529
LINUX_TV_CASTING_APP_LOGS = './scripts/tests/Linux-tv-casting-app-logs.txt'
2630

@@ -33,28 +37,31 @@
3337

3438
# Dump the logs to the console in the case of an error.
3539
def dump_logs_to_console(log_file):
40+
3641
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.')
3843
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.')
4045

4146
with open(log_file, 'r') as file:
4247
logs = file.read()
43-
print(logs)
48+
logging.debug(logs)
4449

4550

4651
# Remove the log files once the script is done running.
4752
def remove_log_file(log_file):
53+
4854
if os.path.exists(log_file):
4955
os.remove(log_file)
5056
else:
51-
print("The file does not exist.")
57+
logging.error('The file does not exist.')
5258

5359

5460
# Whenever a failure is discovered, we should print 'Discovery failed!',
5561
# dump the logs, clean up the log files, exit on error.
5662
def handle_discovery_failure():
57-
print('Discovery failed!\n')
63+
64+
logging.error('Discovery failed!\n')
5865

5966
dump_logs_to_console(LINUX_TV_CASTING_APP_LOGS)
6067
dump_logs_to_console(LINUX_TV_APP_LOGS)
@@ -67,7 +74,8 @@ def handle_discovery_failure():
6774

6875
# Helper function to extract the integer value from a string.
6976
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', '')
7179
value = int(value)
7280

7381
return value
@@ -76,23 +84,32 @@ def extract_value_from_string(line):
7684
# Check if the discovered value matches the expected value.
7785
# Returns False if the value does not match, True otherwise.
7886
def validate_value(expected_value, line, value_name):
87+
7988
# Extract the integer value from the string
8089
value = extract_value_from_string(line)
8190

8291
# If the discovered value does not match the expected value,
8392
# print the error and return False.
8493
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}')
8796
line = line.rstrip('\n')
88-
print(line)
97+
logging.error(line)
8998

9099
return False
91100

92101
# Return True if the value matches the expected value
93102
return True
94103

95104

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+
96113
# Test if the Linux tv-casting-app is able to discover the Linux tv-app.
97114
# The Linux tv-casting-app and the tv-app will be run in separate processes.
98115
# Their corresponding output will be written to their respective log files.
@@ -121,17 +138,20 @@ def test_discovery_fn():
121138

122139
# Read the output as we get it from the tv-casting-app process
123140
for line in tv_casting_app_process.stdout:
141+
124142
# Write the line to the Linux tv-casting-app log file
125143
fd2.write(line)
126144

127145
# Fail fast if "No commissioner discovered" string found
128146
if "No commissioner discovered" in line:
147+
129148
line = line.rstrip('\n')
130-
print(line)
149+
logging.error(line)
131150
handle_discovery_failure()
132151

133152
# Look for 'Discovered Commissioner'
134153
if "Discovered Commissioner" in line:
154+
135155
line = line.rstrip('\n')
136156
valid_discovered_commissioner_str = line
137157

@@ -192,23 +212,20 @@ def test_discovery_fn():
192212
# We only print the discovered commissioner that has valid vendor id, product id,
193213
# and device type. Remove the log files once done.
194214
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!')
200221

201222
remove_log_file(LINUX_TV_CASTING_APP_LOGS)
202223
remove_log_file(LINUX_TV_APP_LOGS)
203224

204225
break
205226

206227
# 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])
212229

213230

214231
@click.group()

0 commit comments

Comments
 (0)