James Kuszmaul | 5c56ed3 | 2022-03-30 15:10:07 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # This script is meant to act as a test to confirm that our log_to_mcap converter produces |
| 3 | # a valid MCAP file. To do so, it first generates an AOS log, then converts it to MCAP, and |
| 4 | # then runs the "mcap doctor" tool on it to confirm compliance with the standard. |
| 5 | import argparse |
| 6 | import subprocess |
| 7 | import sys |
| 8 | import tempfile |
| 9 | import time |
| 10 | from typing import Sequence, Text |
| 11 | |
| 12 | |
James Kuszmaul | 5ab990d | 2022-11-07 16:35:49 -0800 | [diff] [blame] | 13 | def make_permutations(options): |
| 14 | if len(options) == 0: |
| 15 | return [[]] |
| 16 | permutations = [] |
| 17 | for option in options[0]: |
| 18 | for sub_permutations in make_permutations(options[1:]): |
| 19 | permutations.append([option] + sub_permutations) |
| 20 | return permutations |
| 21 | |
| 22 | |
| 23 | def generate_argument_permutations(): |
| 24 | arg_sets = [["--compress", "--nocompress"], |
| 25 | ["--mode=flatbuffer", "--mode=json"], |
| 26 | ["--canonical_channel_names", "--nocanonical_channel_names"], |
| 27 | ["--mcap_chunk_size=1000", "--mcap_chunk_size=10000000"], |
| 28 | ["--fetch", "--nofetch"]] |
| 29 | permutations = make_permutations(arg_sets) |
| 30 | print(permutations) |
| 31 | return permutations |
| 32 | |
| 33 | |
James Kuszmaul | 5c56ed3 | 2022-03-30 15:10:07 -0700 | [diff] [blame] | 34 | def main(argv: Sequence[Text]): |
| 35 | parser = argparse.ArgumentParser() |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 36 | parser.add_argument("--log_to_mcap", |
| 37 | required=True, |
| 38 | help="Path to log_to_mcap binary.") |
James Kuszmaul | 5c56ed3 | 2022-03-30 15:10:07 -0700 | [diff] [blame] | 39 | parser.add_argument("--mcap", required=True, help="Path to mcap binary.") |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 40 | parser.add_argument("--generate_log", |
| 41 | required=True, |
| 42 | help="Path to logfile generator.") |
James Kuszmaul | 5c56ed3 | 2022-03-30 15:10:07 -0700 | [diff] [blame] | 43 | args = parser.parse_args(argv) |
James Kuszmaul | 5ab990d | 2022-11-07 16:35:49 -0800 | [diff] [blame] | 44 | log_to_mcap_argument_permutations = generate_argument_permutations() |
| 45 | for log_to_mcap_args in log_to_mcap_argument_permutations: |
| 46 | with tempfile.TemporaryDirectory() as tmpdir: |
| 47 | log_name = tmpdir + "/test_log/" |
| 48 | mcap_name = tmpdir + "/log.mcap" |
| 49 | subprocess.run([args.generate_log, "--output_folder", |
| 50 | log_name]).check_returncode() |
| 51 | # Run with a really small chunk size, to force a multi-chunk file. |
| 52 | subprocess.run([ |
| 53 | args.log_to_mcap, "--output_path", mcap_name, |
| 54 | "--mcap_chunk_size", "1000", "--mode", "json", log_name |
| 55 | ] + log_to_mcap_args).check_returncode() |
| 56 | # MCAP attempts to find $HOME/.mcap.yaml, and dies on $HOME not existing. So |
| 57 | # give it an arbitrary config location (it seems to be fine with a non-existent config). |
| 58 | doctor_result = subprocess.run([ |
| 59 | args.mcap, "doctor", mcap_name, "--config", |
| 60 | tmpdir + "/.mcap.yaml" |
| 61 | ], |
| 62 | stdout=subprocess.PIPE, |
| 63 | stderr=subprocess.PIPE, |
| 64 | encoding='utf-8') |
| 65 | print(doctor_result.stdout) |
| 66 | print(doctor_result.stderr) |
| 67 | # mcap doctor doesn't actually return a non-zero exit code on certain failures... |
| 68 | # See https://github.com/foxglove/mcap/issues/356 |
| 69 | if len(doctor_result.stderr) != 0: |
| 70 | print("Didn't expect any stderr output.") |
| 71 | return 1 |
| 72 | if doctor_result.stdout != f"Examining {mcap_name}\nHeader.profile field \"x-aos\" is not a well-known profile.\n": |
| 73 | print("Only expected two lines of stdout.") |
| 74 | return 1 |
| 75 | doctor_result.check_returncode() |
James Kuszmaul | 5c56ed3 | 2022-03-30 15:10:07 -0700 | [diff] [blame] | 76 | return 0 |
| 77 | |
| 78 | |
| 79 | if __name__ == '__main__': |
| 80 | sys.exit(main(sys.argv[1:])) |