|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +import pathlib |
| 5 | +import sys |
| 6 | +import os |
| 7 | +import json |
| 8 | +import urllib.parse |
| 9 | +import time |
| 10 | + |
| 11 | +cur_dir = os.path.dirname(os.path.abspath(__file__)) |
| 12 | +cli_dir = os.path.join(cur_dir, '..', '..', 'cli') |
| 13 | +sys.path.append(cli_dir) |
| 14 | +from raft_sdk.raft_service import RaftCLI, RaftJobConfig |
| 15 | + |
| 16 | +def find_files(): |
| 17 | + configs = {} |
| 18 | + fs = ['dredd', 'zap', 'compile', 'test', 'fuzz'] |
| 19 | + for root, _, files in os.walk(cli_dir): |
| 20 | + for f in fs: |
| 21 | + j = f + '.json' |
| 22 | + if j in files: |
| 23 | + if not configs.get(root): |
| 24 | + configs[root] = {} |
| 25 | + configs[root][f] = os.path.join(root, j) |
| 26 | + |
| 27 | + y = f + '.yaml' |
| 28 | + if y in files: |
| 29 | + if not configs.get(root): |
| 30 | + configs[root] = {} |
| 31 | + configs[root][f] = os.path.join(root, y) |
| 32 | + |
| 33 | + return configs |
| 34 | + |
| 35 | +def wait(configs, count, task_name, job_id_key): |
| 36 | + completed_count = 0 |
| 37 | + while completed_count < count: |
| 38 | + for c in configs: |
| 39 | + if configs[c].get(task_name): |
| 40 | + status = cli.job_status(configs[c][job_id_key]) |
| 41 | + completed, _ = cli.is_completed(status) |
| 42 | + if completed: |
| 43 | + completed_count += 1 |
| 44 | + cli.print_status(status) |
| 45 | + for _ in range(1,9) : |
| 46 | + sys.stdout.write('.') |
| 47 | + sys.stdout.flush() |
| 48 | + time.sleep(1) |
| 49 | + print('.') |
| 50 | + |
| 51 | + |
| 52 | +def compile_and_dredd(cli, configs): |
| 53 | + compile_count = 0 |
| 54 | + dredd_count = 0 |
| 55 | + for c in configs: |
| 56 | + if configs[c].get('compile'): |
| 57 | + compile_job_config = RaftJobConfig(file_path=configs[c]['compile'], substitutions=subs) |
| 58 | + compile_job = cli.new_job(compile_job_config) |
| 59 | + configs[c]['compile_job_id'] = compile_job['jobId'] |
| 60 | + compile_count = compile_count + 1 |
| 61 | + |
| 62 | + if configs[c].get('dredd'): |
| 63 | + dredd_job_config = RaftJobConfig(file_path=configs[c]['dredd'], substitutions=subs) |
| 64 | + dredd_job = cli.new_job(dredd_job_config) |
| 65 | + configs[c]['dredd_job_id'] = dredd_job['jobId'] |
| 66 | + dredd_count = dredd_count + 1 |
| 67 | + |
| 68 | + print('Compiling all ' + str(compile_count) + ' and running Dredd on ' + str(dredd_count) + ' samples ...') |
| 69 | + wait(configs, compile_count, 'compile', 'compile_job_id') |
| 70 | + wait(configs, dredd_count, 'dredd', 'dredd_job_id') |
| 71 | + |
| 72 | + |
| 73 | +def test(cli, configs): |
| 74 | + test_count = 0 |
| 75 | + for c in configs: |
| 76 | + if configs[c].get('test'): |
| 77 | + subs['{compile.jobId}'] = configs[c]['compile_job_id'] |
| 78 | + test_job_config = RaftJobConfig(file_path=configs[c]['test'], substitutions=subs) |
| 79 | + test_job = cli.new_job(test_job_config) |
| 80 | + configs[c]['test_job_id'] = test_job['jobId'] |
| 81 | + test_count = test_count + 1 |
| 82 | + print('Testing all ' + str(test_count) + ' samples ...') |
| 83 | + wait(configs, test_count, 'test', 'test_job_id') |
| 84 | + |
| 85 | + |
| 86 | +def fuzz_and_zap(cli, configs): |
| 87 | + fuzz_count = 0 |
| 88 | + zap_count = 0 |
| 89 | + for c in configs: |
| 90 | + if configs[c].get('fuzz'): |
| 91 | + subs['{compile.jobId}'] = configs[c]['compile_job_id'] |
| 92 | + fuzz_job_config = RaftJobConfig(file_path=configs[c]['fuzz'], substitutions=subs) |
| 93 | + fuzz_job = cli.new_job(fuzz_job_config) |
| 94 | + configs[c]['fuzz_job_id'] = fuzz_job['jobId'] |
| 95 | + fuzz_count = fuzz_count + 1 |
| 96 | + |
| 97 | + if configs[c].get('zap'): |
| 98 | + zap_job_config = RaftJobConfig(file_path=configs[c]['zap'], substitutions=subs) |
| 99 | + zap_job = cli.new_job(zap_job_config) |
| 100 | + configs[c]['zap_job_id'] = zap_job['jobId'] |
| 101 | + zap_count = zap_count + 1 |
| 102 | + |
| 103 | + print('Fuzzing all ' + str(fuzz_count) + ' and ZAP: ' + str(zap_count) + ' samples ...') |
| 104 | + wait(configs, fuzz_count, 'fuzz', 'fuzz_job_id') |
| 105 | + wait(configs, zap_count, 'zap', 'zap_job_id') |
| 106 | + |
| 107 | +if __name__ == "__main__": |
| 108 | + if len(sys.argv) == 1: |
| 109 | + print('Please provide a host on command line that does not require authentication. Something like: test-host.com') |
| 110 | + |
| 111 | + sample_host = sys.argv[1] |
| 112 | + |
| 113 | + cli = RaftCLI() |
| 114 | + subs = { |
| 115 | + '{sample.host}' : sample_host, |
| 116 | + '{defaults.deploymentName}' : cli.definitions.deployment, |
| 117 | + '{ci-run}': 'all-samples' |
| 118 | + } |
| 119 | + configs = find_files() |
| 120 | + compile_and_dredd(cli, configs) |
| 121 | + test(cli, configs) |
| 122 | + fuzz_and_zap(cli, configs) |
0 commit comments