|
| 1 | +from pytest import fixture, mark, raises |
| 2 | + |
| 3 | +from firebolt.utils.exception import OperationalError |
| 4 | + |
| 5 | + |
| 6 | +@fixture(scope="module") |
| 7 | +def db_name(database_name): |
| 8 | + return database_name + "_system_test" |
| 9 | + |
| 10 | + |
| 11 | +@fixture(scope="module") |
| 12 | +def second_db_name(database_name): |
| 13 | + return database_name + "_system_test_two" |
| 14 | + |
| 15 | + |
| 16 | +@fixture(scope="module") |
| 17 | +def region(): |
| 18 | + return "us-east-1" |
| 19 | + |
| 20 | + |
| 21 | +@fixture(scope="module") |
| 22 | +def engine_name(engine_name): |
| 23 | + return engine_name + "_system_test" |
| 24 | + |
| 25 | + |
| 26 | +@fixture(scope="module") |
| 27 | +def setup_dbs(connection_system_engine, db_name, second_db_name, engine_name, region): |
| 28 | + with connection_system_engine.cursor() as cursor: |
| 29 | + |
| 30 | + cursor.execute(create_database(name=db_name)) |
| 31 | + |
| 32 | + cursor.execute(create_engine(engine_name, engine_specs(region))) |
| 33 | + |
| 34 | + cursor.execute( |
| 35 | + create_database(name=second_db_name, specs=db_specs(region, engine_name)) |
| 36 | + ) |
| 37 | + |
| 38 | + yield |
| 39 | + |
| 40 | + cursor.execute(f"DROP ENGINE IF EXISTS {engine_name}") |
| 41 | + cursor.execute(f"DROP DATABASE IF EXISTS {db_name}") |
| 42 | + cursor.execute(f"DROP DATABASE IF EXISTS {second_db_name}") |
| 43 | + |
| 44 | + |
| 45 | +def engine_specs(region): |
| 46 | + return f"REGION = '{region}' " "SPEC = 'B1' " "SCALE = 1" |
| 47 | + |
| 48 | + |
| 49 | +def create_database(name, specs=None): |
| 50 | + query = f"CREATE DATABASE {name}" |
| 51 | + query += f" WITH {specs}" if specs else "" |
| 52 | + return query |
| 53 | + |
| 54 | + |
| 55 | +def create_engine(name, specs=None): |
| 56 | + query = f"CREATE ENGINE {name}" |
| 57 | + query += f" WITH {specs}" if specs else "" |
| 58 | + return query |
| 59 | + |
| 60 | + |
| 61 | +def db_specs(region, attached_engine): |
| 62 | + return ( |
| 63 | + f"REGION = '{region}' " |
| 64 | + f"ATTACHED_ENGINES = ('{attached_engine}') " |
| 65 | + "DESCRIPTION = 'Sample description'" |
| 66 | + ) |
| 67 | + |
| 68 | + |
| 69 | +@mark.parametrize( |
| 70 | + "query", |
| 71 | + ["SELECT 1", "CREATE DIMENSION TABLE dummy(id INT)", "SHOW TABLES", "SHOW INDEXES"], |
| 72 | +) |
| 73 | +def test_query_errors(connection_system_engine, query): |
| 74 | + with connection_system_engine.cursor() as cursor: |
| 75 | + with raises(OperationalError): |
| 76 | + cursor.execute(query) |
| 77 | + |
| 78 | + |
| 79 | +@mark.xdist_group(name="system_engine") |
| 80 | +def test_show_databases(setup_dbs, connection_system_engine, db_name): |
| 81 | + with connection_system_engine.cursor() as cursor: |
| 82 | + |
| 83 | + cursor.execute("SHOW DATABASES") |
| 84 | + |
| 85 | + dbs = [row[0] for row in cursor.fetchall()] |
| 86 | + |
| 87 | + assert db_name in dbs |
| 88 | + assert f"{db_name}_two" in dbs |
| 89 | + |
| 90 | + |
| 91 | +@mark.xdist_group(name="system_engine") |
| 92 | +def test_detach_engine( |
| 93 | + setup_dbs, connection_system_engine, engine_name, second_db_name |
| 94 | +): |
| 95 | + def check_engine_exists(cursor, engine_name, db_name): |
| 96 | + cursor.execute("SHOW ENGINES") |
| 97 | + engines = cursor.fetchall() |
| 98 | + # Results have the following columns |
| 99 | + # engine_name, region, spec, scale, status, attached_to, version |
| 100 | + assert engine_name in [row[0] for row in engines] |
| 101 | + assert (engine_name, db_name) in [(row[0], row[5]) for row in engines] |
| 102 | + |
| 103 | + with connection_system_engine.cursor() as cursor: |
| 104 | + check_engine_exists(cursor, engine_name, db_name=second_db_name) |
| 105 | + cursor.execute(f"DETACH ENGINE {engine_name} FROM {second_db_name}") |
| 106 | + |
| 107 | + # When engine not attached db is - |
| 108 | + check_engine_exists(cursor, engine_name, db_name="-") |
| 109 | + |
| 110 | + cursor.execute(f"ATTACH ENGINE {engine_name} TO {second_db_name}") |
| 111 | + check_engine_exists(cursor, engine_name, db_name=second_db_name) |
| 112 | + |
| 113 | + |
| 114 | +@mark.xdist_group(name="system_engine") |
| 115 | +def test_alter_engine(setup_dbs, connection_system_engine, engine_name): |
| 116 | + with connection_system_engine.cursor() as cursor: |
| 117 | + cursor.execute(f"ALTER ENGINE {engine_name} SET SPEC = B2") |
| 118 | + |
| 119 | + cursor.execute("SHOW ENGINES") |
| 120 | + engines = cursor.fetchall() |
| 121 | + assert (engine_name, "B2") in [(row[0], row[2]) for row in engines] |
| 122 | + |
| 123 | + |
| 124 | +@mark.xdist_group(name="system_engine") |
| 125 | +def test_start_stop_engine(setup_dbs, connection_system_engine, engine_name): |
| 126 | + def check_engine_status(cursor, engine_name, status): |
| 127 | + cursor.execute("SHOW ENGINES") |
| 128 | + engines = cursor.fetchall() |
| 129 | + # Results have the following columns |
| 130 | + # engine_name, region, spec, scale, status, attached_to, version |
| 131 | + assert engine_name in [row[0] for row in engines] |
| 132 | + assert (engine_name, status) in [(row[0], row[4]) for row in engines] |
| 133 | + |
| 134 | + with connection_system_engine.cursor() as cursor: |
| 135 | + check_engine_status(cursor, engine_name, "Stopped") |
| 136 | + cursor.execute(f"START ENGINE {engine_name}") |
| 137 | + check_engine_status(cursor, engine_name, "Running") |
| 138 | + cursor.execute(f"STOP ENGINE {engine_name}") |
| 139 | + check_engine_status(cursor, engine_name, "Stopped") |
0 commit comments