github.com/crowdsecurity/crowdsec@v1.6.1/test/bin/generate-hub-tests.py (about) 1 #!/usr/bin/env python3 2 3 import json 4 import pathlib 5 import os 6 import sys 7 import textwrap 8 9 test_header = """ 10 set -u 11 12 setup_file() { 13 load "../lib/setup_file.sh" 14 } 15 16 teardown_file() { 17 load "../lib/teardown_file.sh" 18 } 19 20 setup() { 21 load "../lib/setup.sh" 22 } 23 """ 24 25 26 def write_chunk(target_dir, n, chunk): 27 with open(target_dir / f"hub-{n}.bats", "w") as f: 28 f.write(test_header) 29 for test in chunk: 30 cscli = os.environ['CSCLI'] 31 crowdsec = os.environ['CROWDSEC'] 32 testname = test['Name'] 33 hubdir = os.environ['LOCAL_DIR'] + '/hub-tests' 34 f.write(textwrap.dedent(f""" 35 @test "{testname}" {{ 36 run "{cscli}" \\ 37 --crowdsec "{crowdsec}" \\ 38 --cscli "{cscli}" \\ 39 --hub "{hubdir}" \\ 40 hubtest run "{testname}" \\ 41 --clean 42 echo "$output" 43 assert_success 44 }} 45 """)) 46 47 48 def main(): 49 hubtests_json = sys.argv[1] 50 target_dir = sys.argv[2] 51 52 with open(hubtests_json) as f: 53 j = json.load(f) 54 chunk_size = len(j) // 3 + 1 55 n = 1 56 for i in range(0, len(j), chunk_size): 57 chunk = j[i:i + chunk_size] 58 write_chunk(pathlib.Path(target_dir), n, chunk) 59 n += 1 60 61 62 if __name__ == "__main__": 63 main()