|
| 1 | +import os |
| 2 | +import string |
| 3 | +import json |
| 4 | +import string |
| 5 | +from time import sleep |
| 6 | +from minio import Minio |
| 7 | +import psycopg2 |
| 8 | +import random |
| 9 | + |
| 10 | + |
| 11 | +def do_test(config, N, n, prefix): |
| 12 | + conn = psycopg2.connect( |
| 13 | + host="localhost", |
| 14 | + port="4566", |
| 15 | + user="root", |
| 16 | + database="dev" |
| 17 | + ) |
| 18 | + |
| 19 | + # Open a cursor to execute SQL statements |
| 20 | + cur = conn.cursor() |
| 21 | + cur.execute(f'''CREATE TABLE s3_test_csv_without_headers( |
| 22 | + a int, |
| 23 | + b int, |
| 24 | + c int, |
| 25 | + ) WITH ( |
| 26 | + connector = 's3', |
| 27 | + match_pattern = '{prefix}_data_without_headers.csv', |
| 28 | + s3.region_name = '{config['S3_REGION']}', |
| 29 | + s3.bucket_name = '{config['S3_BUCKET']}', |
| 30 | + s3.credentials.access = '{config['S3_ACCESS_KEY']}', |
| 31 | + s3.credentials.secret = '{config['S3_SECRET_KEY']}', |
| 32 | + s3.endpoint_url = 'https://{config['S3_ENDPOINT']}' |
| 33 | + ) ROW FORMAT CSV WITHOUT HEADER DELIMITED BY ',';''') |
| 34 | + |
| 35 | + cur.execute(f'''CREATE TABLE s3_test_csv_with_headers( |
| 36 | + a int, |
| 37 | + b int, |
| 38 | + c int, |
| 39 | + ) WITH ( |
| 40 | + connector = 's3', |
| 41 | + match_pattern = '{prefix}_data_with_headers.csv', |
| 42 | + s3.region_name = '{config['S3_REGION']}', |
| 43 | + s3.bucket_name = '{config['S3_BUCKET']}', |
| 44 | + s3.credentials.access = '{config['S3_ACCESS_KEY']}', |
| 45 | + s3.credentials.secret = '{config['S3_SECRET_KEY']}', |
| 46 | + s3.endpoint_url = 'https://{config['S3_ENDPOINT']}' |
| 47 | + ) ROW FORMAT CSV DELIMITED BY ',';''') |
| 48 | + |
| 49 | + total_row = int(N * n) |
| 50 | + sleep(60) |
| 51 | + while True: |
| 52 | + sleep(60) |
| 53 | + cur.execute('select count(*) from s3_test_csv_with_headers') |
| 54 | + result_with_headers = cur.fetchone() |
| 55 | + cur.execute('select count(*) from s3_test_csv_without_headers') |
| 56 | + result_without_headers = cur.fetchone() |
| 57 | + if result_with_headers[0] == total_row and result_without_headers[0] == total_row: |
| 58 | + break |
| 59 | + print( |
| 60 | + f"Now got {result_with_headers[0]} rows in table, {total_row} expected, wait 60s") |
| 61 | + |
| 62 | + cur.execute( |
| 63 | + 'select count(*), sum(a), sum(b), sum(c) from s3_test_csv_with_headers') |
| 64 | + result_with_headers = cur.fetchone() |
| 65 | + |
| 66 | + cur.execute( |
| 67 | + 'select count(*), sum(a), sum(b), sum(c) from s3_test_csv_without_headers') |
| 68 | + s3_test_csv_without_headers = cur.fetchone() |
| 69 | + |
| 70 | + print(result_with_headers, s3_test_csv_without_headers, |
| 71 | + int(((N - 1) * N / 2) * n), int(N*n / 2)) |
| 72 | + |
| 73 | + assert s3_test_csv_without_headers[0] == total_row |
| 74 | + assert s3_test_csv_without_headers[1] == int(((N - 1) * N / 2) * n) |
| 75 | + assert s3_test_csv_without_headers[2] == int(N*n / 2) |
| 76 | + assert s3_test_csv_without_headers[3] == 0 |
| 77 | + |
| 78 | + assert result_with_headers[0] == total_row |
| 79 | + assert result_with_headers[1] == 0 |
| 80 | + assert result_with_headers[2] == int(N*n / 2) |
| 81 | + assert result_with_headers[3] == int(((N - 1) * N / 2) * n) |
| 82 | + |
| 83 | + cur.execute('drop table s3_test_csv_with_headers') |
| 84 | + cur.execute('drop table s3_test_csv_without_headers') |
| 85 | + |
| 86 | + cur.close() |
| 87 | + conn.close() |
| 88 | + |
| 89 | + |
| 90 | +if __name__ == "__main__": |
| 91 | + config = json.loads(os.environ["S3_SOURCE_TEST_CONF"]) |
| 92 | + run_id = str(random.randint(1000, 9999)) |
| 93 | + N = 10000 |
| 94 | + # do_test(config, N, 0, run_id) |
| 95 | + items = [",".join([str(j), str(j % 2), str(-1 if j % 2 else 1)]) |
| 96 | + for j in range(N) |
| 97 | + ] |
| 98 | + |
| 99 | + data = "\n".join(items) + "\n" |
| 100 | + n = 10 |
| 101 | + with open("data_without_headers.csv", "w") as f: |
| 102 | + for _ in range(10): |
| 103 | + f.write(data) |
| 104 | + os.fsync(f.fileno()) |
| 105 | + |
| 106 | + with open("data_with_headers.csv", "w") as f: |
| 107 | + f.write("c,b,a\n") |
| 108 | + for _ in range(10): |
| 109 | + f.write(data) |
| 110 | + os.fsync(f.fileno()) |
| 111 | + |
| 112 | + client = Minio( |
| 113 | + config["S3_ENDPOINT"], |
| 114 | + access_key=config["S3_ACCESS_KEY"], |
| 115 | + secret_key=config["S3_SECRET_KEY"], |
| 116 | + secure=True |
| 117 | + ) |
| 118 | + |
| 119 | + try: |
| 120 | + client.fput_object( |
| 121 | + config["S3_BUCKET"], |
| 122 | + f"{run_id}_data_without_headers.csv", |
| 123 | + f"data_without_headers.csv" |
| 124 | + |
| 125 | + ) |
| 126 | + client.fput_object( |
| 127 | + config["S3_BUCKET"], |
| 128 | + f"{run_id}_data_with_headers.csv", |
| 129 | + f"data_with_headers.csv" |
| 130 | + ) |
| 131 | + print( |
| 132 | + f"Uploaded {run_id}_data_with_headers.csv & {run_id}_data_with_headers.csv to S3") |
| 133 | + os.remove(f"data_with_headers.csv") |
| 134 | + os.remove(f"data_without_headers.csv") |
| 135 | + except Exception as e: |
| 136 | + print(f"Error uploading test files") |
| 137 | + |
| 138 | + return_code = 0 |
| 139 | + try: |
| 140 | + do_test(config, N, n, run_id) |
| 141 | + except Exception as e: |
| 142 | + print("Test failed", e) |
| 143 | + return_code = 1 |
| 144 | + |
| 145 | + # Clean up |
| 146 | + for i in range(20): |
| 147 | + try: |
| 148 | + client.remove_object( |
| 149 | + config["S3_BUCKET"], f"{run_id}_data_with_headers.csv") |
| 150 | + client.remove_object( |
| 151 | + config["S3_BUCKET"], f"{run_id}_data_without_headers.csv") |
| 152 | + except Exception as e: |
| 153 | + print(f"Error removing testing files {e}") |
| 154 | + |
| 155 | + exit(return_code) |
0 commit comments