| 1 | import sys |
| 2 | import signal |
| 3 | from random import randbytes |
| 4 | from pathlib import Path # if you haven't already done so |
| 5 | |
| 6 | file = Path(__file__).resolve() |
| 7 | parent, root = file.parent, file.parents[1] |
| 8 | sys.path.append(str(root)) |
| 9 | |
| 10 | # Additionally remove the current file's directory from sys.path |
| 11 | from contextlib import suppress |
| 12 | |
| 13 | with suppress(ValueError): |
| 14 | sys.path.remove(str(parent)) |
| 15 | |
| 16 | import logging |
| 17 | logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(message)s") |
| 18 | |
| 19 | import time |
| 20 | import os |
| 21 | from omnistrate_tests.classes.omnistrate_fleet_instance import OmnistrateFleetInstance |
| 22 | from omnistrate_tests.classes.omnistrate_fleet_api import OmnistrateFleetAPI |
| 23 | import argparse |
| 24 | |
| 25 | parser = argparse.ArgumentParser() |
| 26 | parser.add_argument("omnistrate_user") |
| 27 | parser.add_argument("omnistrate_password") |
| 28 | parser.add_argument("cloud_provider", choices=["aws", "gcp"]) |
| 29 | parser.add_argument("region") |
| 30 | |
| 31 | parser.add_argument( |
| 32 | "--subscription-id", required=False, default=os.getenv("SUBSCRIPTION_ID") |
| 33 | ) |
| 34 | parser.add_argument("--ref-name", required=False, default=os.getenv("REF_NAME")) |
| 35 | parser.add_argument("--service-id", required=True) |
| 36 | parser.add_argument("--environment-id", required=True) |
| 37 | parser.add_argument("--resource-key", required=True) |
| 38 | parser.add_argument("--replica-id", required=True) |
| 39 | |
| 40 | |
| 41 | parser.add_argument("--instance-name", required=True) |
| 42 | parser.add_argument("--instance-description", required=False, default="test-standalone") |
| 43 | parser.add_argument("--instance-type", required=True) |
| 44 | parser.add_argument("--storage-size", required=False, default="30") |
| 45 | parser.add_argument("--tls", action="store_true") |
| 46 | parser.add_argument("--rdb-config", required=False, default="medium") |
| 47 | parser.add_argument("--aof-config", required=False, default="always") |
| 48 | |
| 49 | parser.set_defaults(tls=False) |
| 50 | args = parser.parse_args() |
| 51 | |
| 52 | instance: OmnistrateFleetInstance = None |
| 53 | |
| 54 | # Intercept exit signals so we can delete the instance before exiting |
| 55 | def signal_handler(sig, frame): |
| 56 | if instance: |
| 57 | instance.delete(False) |
| 58 | sys.exit(0) |
| 59 | signal.signal(signal.SIGINT, signal_handler) |
| 60 | signal.signal(signal.SIGTERM, signal_handler) |
| 61 | |
| 62 | def test_standalone(): |
| 63 | global instance |
| 64 | |
| 65 | omnistrate = OmnistrateFleetAPI( |
| 66 | email=args.omnistrate_user, |
| 67 | password=args.omnistrate_password, |
| 68 | ) |
| 69 | |
| 70 | service = omnistrate.get_service(args.service_id) |
| 71 | product_tier = omnistrate.get_product_tier( |
| 72 | service_id=args.service_id, |
| 73 | environment_id=args.environment_id, |
| 74 | tier_name=args.ref_name, |
| 75 | ) |
| 76 | service_model = omnistrate.get_service_model( |
| 77 | args.service_id, product_tier.service_model_id |
| 78 | ) |
| 79 | |
| 80 | logging.info(f"Product tier id: {product_tier.product_tier_id} for {args.ref_name}") |
| 81 | |
| 82 | instance = omnistrate.instance( |
| 83 | service_id=args.service_id, |
| 84 | service_provider_id=service.service_provider_id, |
| 85 | service_key=service.key, |
| 86 | service_environment_id=args.environment_id, |
| 87 | service_environment_key=service.get_environment(args.environment_id).key, |
| 88 | service_model_key=service_model.key, |
| 89 | service_api_version="v1", |
| 90 | product_tier_key=product_tier.product_tier_key, |
| 91 | resource_key=args.resource_key, |
| 92 | subscription_id=args.subscription_id, |
| 93 | deployment_create_timeout_seconds=2400, |
| 94 | deployment_delete_timeout_seconds=2400, |
| 95 | deployment_failover_timeout_seconds=2400 |
| 96 | ) |
| 97 | |
| 98 | try: |
| 99 | instance.create( |
| 100 | wait_for_ready=True, |
| 101 | deployment_cloud_provider=args.cloud_provider, |
| 102 | deployment_region=args.region, |
| 103 | name=args.instance_name, |
| 104 | description=args.instance_description, |
| 105 | falkordb_user="falkordb", |
| 106 | falkordb_password=randbytes(16).hex(), |
| 107 | nodeInstanceType=args.instance_type, |
| 108 | storageSize=args.storage_size, |
| 109 | enableTLS=args.tls, |
| 110 | RDBPersistenceConfig=args.rdb_config, |
| 111 | AOFPersistenceConfig=args.aof_config, |
| 112 | ) |
| 113 | |
| 114 | # Test failover and data loss |
| 115 | test_failover(instance) |
| 116 | |
| 117 | # Test stop and start instance |
| 118 | test_stop_start(instance) |
| 119 | except Exception as e: |
| 120 | logging.exception(e) |
| 121 | instance.delete(False) |
| 122 | raise e |
| 123 | |
| 124 | # Delete instance |
| 125 | instance.delete(False) |
| 126 | |
| 127 | logging.info("Test passed") |
| 128 | |
| 129 | |
| 130 | def test_failover(instance: OmnistrateFleetInstance): |
| 131 | """This function should retrieve the instance host and port for connection, write some data to the DB, then trigger a failover. After X seconds, the instance should be back online and data should have persisted""" |
| 132 | |
| 133 | # Get instance host and port |
| 134 | db = instance.create_connection( |
| 135 | ssl=args.tls, |
| 136 | ) |
| 137 | |
| 138 | graph = db.select_graph("test") |
| 139 | |
| 140 | # Write some data to the DB |
| 141 | graph.query("CREATE (n:Person {name: 'Alice'})") |
| 142 | |
| 143 | # Trigger failover |
| 144 | instance.trigger_failover( |
| 145 | replica_id=args.replica_id, |
| 146 | wait_for_ready=True, |
| 147 | ) |
| 148 | |
| 149 | # Check if data is still there |
| 150 | |
| 151 | graph = db.select_graph("test") |
| 152 | |
| 153 | result = graph.query("MATCH (n:Person) RETURN n") |
| 154 | |
| 155 | if len(result.result_set) == 0: |
| 156 | raise Exception("Data lost after failover") |
| 157 | |
| 158 | logging.info("Data persisted after failover") |
| 159 | |
| 160 | graph.delete() |
| 161 | |
| 162 | |
| 163 | def test_stop_start(instance: OmnistrateFleetInstance): |
| 164 | """This function should stop the instance, check that it is stopped, then start it again and check that it is running""" |
| 165 | |
| 166 | # Get instance host and port |
| 167 | db = instance.create_connection( |
| 168 | ssl=args.tls, |
| 169 | ) |
| 170 | |
| 171 | graph = db.select_graph("test") |
| 172 | |
| 173 | # Write some data to the DB |
| 174 | graph.query("CREATE (n:Person {name: 'Alice'})") |
| 175 | |
| 176 | logging.info("Stopping instance") |
| 177 | |
| 178 | instance.stop(wait_for_ready=True) |
| 179 | |
| 180 | logging.info("Instance stopped") |
| 181 | |
| 182 | instance.start(wait_for_ready=True) |
| 183 | |
| 184 | graph = db.select_graph("test") |
| 185 | |
| 186 | result = graph.query("MATCH (n:Person) RETURN n") |
| 187 | |
| 188 | if len(result.result_set) == 0: |
| 189 | raise Exception("Data lost after stop/start") |
| 190 | |
| 191 | logging.info("Instance started") |
| 192 | |
| 193 | |
| 194 | if __name__ == "__main__": |
| 195 | test_standalone() |