|
| 1 | +#!/usr/bin/env -S python3 -B |
| 2 | + |
| 3 | +# Copyright (c) 2024 Project CHIP Authors |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +import os |
| 18 | +import subprocess |
| 19 | +import sys |
| 20 | +import time |
| 21 | + |
| 22 | +import click |
| 23 | + |
| 24 | + |
| 25 | +# Dump the logs to the console in the case of an error. |
| 26 | +def dump_logs_to_console(log_file): |
| 27 | + with open(log_file, 'r') as file: |
| 28 | + logs = file.read() |
| 29 | + print(logs) |
| 30 | + |
| 31 | + |
| 32 | +# Remove the log files once the script is done running. |
| 33 | +def remove_log_file(log_file): |
| 34 | + if os.path.exists(log_file): |
| 35 | + os.remove(log_file) |
| 36 | + else: |
| 37 | + print("The file does not exist.") |
| 38 | + |
| 39 | + |
| 40 | +# Read the logs from the Linux-tv-casting-app-logs.txt file. |
| 41 | +# The discovered commissioner(s) will be stored in a list along with their |
| 42 | +# vendor ID, product ID, and device type. |
| 43 | +def read_linux_tv_casting_app_logs(log_file): |
| 44 | + |
| 45 | + with open(log_file, 'r') as file: |
| 46 | + lines = file.readlines() |
| 47 | + |
| 48 | + discovered_commissioners = [] |
| 49 | + |
| 50 | + print('Reading from Linux-tv-casting-app-logs.txt') |
| 51 | + |
| 52 | + # Read through the Linux-tv-casting-app-logs.txt line by line |
| 53 | + for i, line in enumerate(lines): |
| 54 | + |
| 55 | + # If commissioner(s) are discovered, then the discovery process was successful. |
| 56 | + if "commissioner(s) discovered" in line: |
| 57 | + print(line) |
| 58 | + print('Discovery success!') |
| 59 | + |
| 60 | + remove_log_file('./scripts/tests/Linux-tv-casting-app-logs.txt') |
| 61 | + remove_log_file('./scripts/tests/Linux-tv-app-logs.txt') |
| 62 | + |
| 63 | + break |
| 64 | + |
| 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 | + # Look for "Discovered Commissioner" |
| 79 | + if "Discovered Commissioner" in line: |
| 80 | + print(line) |
| 81 | + |
| 82 | + # Extract the relevant part of the string |
| 83 | + commissioner = line.split("Discovered Commissioner")[-1].strip() |
| 84 | + commissioner = commissioner.replace('\x1b[0m', '') |
| 85 | + |
| 86 | + # Initialize variables for Vendor ID, Product ID, and Device Type |
| 87 | + vendor_id = None |
| 88 | + product_id = None |
| 89 | + device_type = None |
| 90 | + |
| 91 | + # Iterate through the subsequent lines to find the strings of interest |
| 92 | + for next_line in lines[i+1:]: |
| 93 | + |
| 94 | + if "Vendor ID:" in next_line: |
| 95 | + print(next_line) |
| 96 | + |
| 97 | + vendor_id = next_line.split(":")[-1].strip() |
| 98 | + vendor_id = vendor_id.replace('\x1b[0m', '') |
| 99 | + |
| 100 | + elif "Product ID:" in next_line: |
| 101 | + print(next_line) |
| 102 | + |
| 103 | + product_id = next_line.split(":")[-1].strip() |
| 104 | + product_id = product_id.replace('\x1b[0m', '') |
| 105 | + |
| 106 | + elif "Device Type:" in next_line: |
| 107 | + print(next_line) |
| 108 | + |
| 109 | + device_type = next_line.split(":")[-1].strip() |
| 110 | + device_type = device_type.replace('\x1b[0m', '') |
| 111 | + |
| 112 | + elif "commissioner(s) discovered" in next_line: |
| 113 | + break |
| 114 | + |
| 115 | + # If the next line starts with "Discovered Commissioner", break the loop |
| 116 | + if "Discovered Commissioner" in next_line: |
| 117 | + break |
| 118 | + |
| 119 | + # Append the extracted information to the devices list |
| 120 | + discovered_commissioners.append({ |
| 121 | + "discovered_commissioner": commissioner, |
| 122 | + "vendor_id": vendor_id, |
| 123 | + "product_id": product_id, |
| 124 | + "device_type": device_type |
| 125 | + }) |
| 126 | + |
| 127 | + # If the list of discovered commissioners is empty and we didn't find the "No commissioner discovered" string, |
| 128 | + # then something went wrong. Exit on error. |
| 129 | + 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) |
| 138 | + |
| 139 | + |
| 140 | +# Test if the Linux tv-casting-app is able to discover the Linux tv-app. |
| 141 | +# The Linux tv-casting-app and the tv-app will be run in separate processes. |
| 142 | +# Their corresponding output will be written to their respective log files. |
| 143 | +# The log file of the tv-casting-app will be parsed for strings of interest |
| 144 | +# which will be printed to the console. |
| 145 | +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: |
| 147 | + |
| 148 | + # Run the Linux tv-app and write the output to file |
| 149 | + tv_app_rel_path = 'out/tv-app/chip-tv-app' |
| 150 | + tv_app_abs_path = os.path.abspath(tv_app_rel_path) |
| 151 | + p1 = subprocess.Popen(tv_app_abs_path, stdout=fd1, stderr=subprocess.PIPE, text=True) |
| 152 | + |
| 153 | + time.sleep(5) |
| 154 | + |
| 155 | + # Run the Linux tv-casting-app and write the output to file |
| 156 | + tv_casting_app_rel_path = 'out/tv-casting-app/chip-tv-casting-app' |
| 157 | + tv_casting_app_abs_path = os.path.abspath(tv_casting_app_rel_path) |
| 158 | + p2 = subprocess.Popen(tv_casting_app_abs_path, stdout=fd2, stderr=subprocess.PIPE, text=True) |
| 159 | + |
| 160 | + # Wait for the processes to finish writing before attempting to read |
| 161 | + time.sleep(15) |
| 162 | + |
| 163 | + read_linux_tv_casting_app_logs('./scripts/tests/Linux-tv-casting-app-logs.txt') |
| 164 | + |
| 165 | + |
| 166 | +@click.group() |
| 167 | +def main(): |
| 168 | + pass |
| 169 | + |
| 170 | + |
| 171 | +@main.command('test-discovery', help='Test if the Linux tv-casting-app is able to discover the Linux tv-app.') |
| 172 | +def test_discovery(): |
| 173 | + test_discovery_fn() |
| 174 | + |
| 175 | + |
| 176 | +if __name__ == '__main__': |
| 177 | + |
| 178 | + main() |
0 commit comments