| 1 | import sys |
| 2 | import signal |
| 3 | from random import randbytes |
| 4 | from pathlib import Path # if you haven't already done so |
| 5 | import threading |
| 6 | |
| 7 | file = Path(__file__).resolve() |
| 8 | parent, root = file.parent, file.parents[1] |
| 9 | sys.path.append(str(root)) |
| 10 | |
| 11 | # Additionally remove the current file's directory from sys.path |
| 12 | from contextlib import suppress |
| 13 | |
| 14 | with suppress(ValueError): |
| 15 | sys.path.remove(str(parent)) |
| 16 | |
| 17 | import logging |
| 18 | |
| 19 | logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(message)s") |
| 20 | |
| 21 | import time |
| 22 | import os |
| 23 | from omnistrate_tests.classes.omnistrate_fleet_instance import OmnistrateFleetInstance |
| 24 | from omnistrate_tests.classes.omnistrate_fleet_api import OmnistrateFleetAPI |
| 25 | from omnistrate_tests.classes.omnistrate_types import TierVersionStatus |
| 26 | import argparse |
| 27 | |
| 28 | parser = argparse.ArgumentParser() |
| 29 | parser.add_argument("omnistrate_user") |
| 30 | parser.add_argument("omnistrate_password") |
| 31 | parser.add_argument("cloud_provider", choices=["aws", "gcp"]) |
| 32 | parser.add_argument("region") |
| 33 | |
| 34 | parser.add_argument( |
| 35 | "--subscription-id", required=False, default=os.getenv("SUBSCRIPTION_ID") |
| 36 | ) |
| 37 | parser.add_argument("--ref-name", required=False, default=os.getenv("REF_NAME")) |
| 38 | parser.add_argument("--service-id", required=True) |
| 39 | parser.add_argument("--environment-id", required=True) |
| 40 | parser.add_argument("--resource-key", required=True) |
| 41 | |
| 42 | |
| 43 | parser.add_argument("--instance-name", required=True) |
| 44 | parser.add_argument( |
| 45 | "--instance-description", required=False, default="test-upgrade-version" |
| 46 | ) |
| 47 | parser.add_argument("--instance-type", required=True) |
| 48 | parser.add_argument("--storage-size", required=False, default="30") |
| 49 | parser.add_argument("--tls", action="store_true") |
| 50 | parser.add_argument("--rdb-config", required=False, default="medium") |
| 51 | parser.add_argument("--aof-config", required=False, default="always") |
| 52 | parser.add_argument("--host-count", required=False, default="6") |
| 53 | parser.add_argument("--cluster-replicas", required=False, default="1") |
| 54 | |
| 55 | |
| 56 | parser.set_defaults(tls=False) |
| 57 | args = parser.parse_args() |
| 58 | |
| 59 | instance: OmnistrateFleetInstance = None |
| 60 | |
| 61 | |
| 62 | # Intercept exit signals so we can delete the instance before exiting |
| 63 | def signal_handler(sig, frame): |
| 64 | if instance: |
| 65 | instance.delete(False) |
| 66 | sys.exit(0) |
| 67 | |
| 68 | |
| 69 | signal.signal(signal.SIGINT, signal_handler) |
| 70 | signal.signal(signal.SIGTERM, signal_handler) |
| 71 | |
| 72 | |
| 73 | def test_upgrade_version(): |
| 74 | global instance |
| 75 | |
| 76 | omnistrate = OmnistrateFleetAPI( |
| 77 | email=args.omnistrate_user, |
| 78 | password=args.omnistrate_password, |
| 79 | ) |
| 80 | |
| 81 | service = omnistrate.get_service(args.service_id) |
| 82 | product_tier = omnistrate.get_product_tier( |
| 83 | service_id=args.service_id, |
| 84 | environment_id=args.environment_id, |
| 85 | tier_name=args.ref_name, |
| 86 | ) |
| 87 | service_model = omnistrate.get_service_model( |
| 88 | args.service_id, product_tier.service_model_id |
| 89 | ) |
| 90 | |
| 91 | logging.info(f"Product tier id: {product_tier.product_tier_id} for {args.ref_name}") |
| 92 | |
| 93 | # 1. List product tier versions |
| 94 | tiers = omnistrate.list_tier_versions( |
| 95 | service_id=args.service_id, tier_id=product_tier.product_tier_id |
| 96 | ) |
| 97 | |
| 98 | preferred_tier = next( |
| 99 | (tier for tier in tiers if tier.status == TierVersionStatus.PREFERRED), None |
| 100 | ) |
| 101 | if preferred_tier is None: |
| 102 | raise ValueError("No preferred tier found") |
| 103 | |
| 104 | last_tier = next( |
| 105 | (tier for tier in tiers if tier.status == TierVersionStatus.ACTIVE), None |
| 106 | ) |
| 107 | |
| 108 | if last_tier is None: |
| 109 | raise ValueError("No last tier found") |
| 110 | |
| 111 | logging.info(f"Preferred tier: {preferred_tier.version}") |
| 112 | logging.info(f"Last tier: {last_tier.version}") |
| 113 | |
| 114 | # 2. Create omnistrate instance with previous version |
| 115 | instance = omnistrate.instance( |
| 116 | service_id=args.service_id, |
| 117 | service_provider_id=service.service_provider_id, |
| 118 | service_key=service.key, |
| 119 | service_environment_id=args.environment_id, |
| 120 | service_environment_key=service.get_environment(args.environment_id).key, |
| 121 | service_model_key=service_model.key, |
| 122 | service_api_version="v1", |
| 123 | product_tier_key=product_tier.product_tier_key, |
| 124 | resource_key=args.resource_key, |
| 125 | subscription_id=args.subscription_id, |
| 126 | deployment_create_timeout_seconds=2400, |
| 127 | deployment_delete_timeout_seconds=2400, |
| 128 | deployment_failover_timeout_seconds=2400 |
| 129 | ) |
| 130 | try: |
| 131 | instance.create( |
| 132 | wait_for_ready=True, |
| 133 | deployment_cloud_provider=args.cloud_provider, |
| 134 | deployment_region=args.region, |
| 135 | name=args.instance_name, |
| 136 | description=args.instance_description, |
| 137 | falkordb_user="falkordb", |
| 138 | falkordb_password=randbytes(16).hex(), |
| 139 | nodeInstanceType=args.instance_type, |
| 140 | storageSize=args.storage_size, |
| 141 | enableTLS=args.tls, |
| 142 | RDBPersistenceConfig=args.rdb_config, |
| 143 | AOFPersistenceConfig=args.aof_config, |
| 144 | hostCount=args.host_count, |
| 145 | clusterReplicas=args.cluster_replicas, |
| 146 | product_tier_version=last_tier.version, |
| 147 | ) |
| 148 | |
| 149 | # 3. Add data to the instance |
| 150 | add_data(instance) |
| 151 | |
| 152 | thread_signal = None |
| 153 | error_signal = None |
| 154 | thread = None |
| 155 | if "standalone" not in args.instance_name: |
| 156 | thread_signal = threading.Event() |
| 157 | error_signal = threading.Event() |
| 158 | thread = threading.Thread( |
| 159 | target=test_zero_downtime, |
| 160 | args=(thread_signal, error_signal, instance, args.tls), |
| 161 | ) |
| 162 | thread.start() |
| 163 | |
| 164 | # 4. Upgrade version for the omnistrate instance |
| 165 | upgrade_timer = time.time() |
| 166 | instance.upgrade( |
| 167 | service_id=args.service_id, |
| 168 | product_tier_id=product_tier.product_tier_id, |
| 169 | source_version=last_tier.version, |
| 170 | target_version=preferred_tier.version, |
| 171 | wait_until_ready=True, |
| 172 | ) |
| 173 | |
| 174 | if "standalone" not in args.instance_name: |
| 175 | thread_signal.set() |
| 176 | thread.join() |
| 177 | |
| 178 | logging.info(f"Upgrade time: {(time.time() - upgrade_timer):.2f}s") |
| 179 | |
| 180 | # 6. Verify the upgrade was successful |
| 181 | query_data(instance) |
| 182 | except Exception as e: |
| 183 | logging.exception(e) |
| 184 | instance.delete(False) |
| 185 | raise e |
| 186 | |
| 187 | # 7. Delete the instance |
| 188 | instance.delete(False) |
| 189 | |
| 190 | if "standalone" not in args.instance_name and error_signal.is_set(): |
| 191 | raise ValueError("Test failed") |
| 192 | else: |
| 193 | logging.info("Test passed") |
| 194 | |
| 195 | |
| 196 | def add_data(instance: OmnistrateFleetInstance): |
| 197 | |
| 198 | # Get instance host and port |
| 199 | db = instance.create_connection(ssl=args.tls) |
| 200 | |
| 201 | graph = db.select_graph("test") |
| 202 | |
| 203 | # Write some data to the DB |
| 204 | graph.query("CREATE (n:Person {name: 'Alice'})") |
| 205 | |
| 206 | |
| 207 | def query_data(instance: OmnistrateFleetInstance): |
| 208 | |
| 209 | # Get instance host and port |
| 210 | db = instance.create_connection(ssl=args.tls) |
| 211 | |
| 212 | graph = db.select_graph("test") |
| 213 | |
| 214 | # Get info |
| 215 | result = graph.query("MATCH (n:Person) RETURN n.name") |
| 216 | |
| 217 | if len(result.result_set) == 0: |
| 218 | raise ValueError("No data found in the graph after upgrade") |
| 219 | |
| 220 | |
| 221 | def test_zero_downtime( |
| 222 | thread_signal: threading.Event, |
| 223 | error_signal: threading.Event, |
| 224 | instance: OmnistrateFleetInstance, |
| 225 | ssl=False, |
| 226 | ): |
| 227 | """This function should test the ability to read and write while a memory update happens""" |
| 228 | try: |
| 229 | db = instance.create_connection(ssl=ssl, force_reconnect=True) |
| 230 | |
| 231 | graph = db.select_graph("test") |
| 232 | |
| 233 | while not thread_signal.is_set(): |
| 234 | # Write some data to the DB |
| 235 | graph.query("CREATE (n:Person {name: 'Alice'})") |
| 236 | graph.ro_query("MATCH (n:Person {name: 'Alice'}) RETURN n") |
| 237 | time.sleep(3) |
| 238 | except Exception as e: |
| 239 | logging.exception(e) |
| 240 | error_signal.set() |
| 241 | raise e |
| 242 | |
| 243 | |
| 244 | if __name__ == "__main__": |
| 245 | test_upgrade_version() |