def test_get_storage_config_with_os_env(): try: os.environ['CODEPACK_CONFIG_PATH'] = 'config/test.ini' config = Config() ret = config.get_storage_config('scheduler') ref = { 'collection': 'scheduler', 'db': 'codepack', 'source': 'mongodb', 'supervisor': 'http://localhost:8000', 'mongodb': { 'host': 'localhost', 'port': '27017' } } assert ret == ref os.environ['CODEPACK_SCHEDULER_DB'] = 'test' ret = config.get_storage_config('scheduler') ref['db'] = 'test' assert ret == ref os.environ['CODEPACK_MONGODB_HOST'] = '127.0.0.1' os.environ['CODEPACK_MONGODB_USER'] = '******' ret = config.get_storage_config('scheduler') ref['mongodb']['host'] = '127.0.0.1' ref['mongodb']['user'] = '******' assert ret == ref finally: os.environ.pop('CODEPACK_CONFIG_PATH', None) os.environ.pop('CODEPACK_SCHEDULER_DB', None) os.environ.pop('CODEPACK_MONGODB_HOST', None) os.environ.pop('CODEPACK_MONGODB_USER', None)
def test_get_storage_config_priority(): try: os.environ['CODEPACK_CONFIG_DIR'] = 'config' config = Config('test.ini') storage_config = config.get_storage_config('worker') ref = { 'group_id': 'codepack_worker_test', 'interval': '5', 'kafka': { 'bootstrap_servers': 'localhost:9092' }, 'script_path': 'scripts/run_snapshot.py', 'source': 'kafka', 'supervisor': 'http://localhost:8000', 'topic': 'test', 'logger': 'worker-logger' } assert storage_config == ref os.environ['CODEPACK_WORKER_SCRIPT'] = 'test_script.py' os.environ['CODEPACK_WORKER_TOPIC'] = 'test2' storage_config = config.get_storage_config('worker') assert storage_config == ref os.environ['CODEPACK_WORKER_TEST_KEY'] = 'test' storage_config = config.get_storage_config('worker') assert storage_config == ref finally: os.environ.pop('CODEPACK_CONFIG_DIR', None) os.environ.pop('CODEPACK_WORKER_SCRIPT', None) os.environ.pop('CODEPACK_WORKER_TOPIC', None) os.environ.pop('CODEPACK_WORKER_TEST_KEY', None)
def test_default_memory_delivery_service_with_os_env(): config = Config() storage_config = config.get_storage_config('delivery') assert storage_config == {'source': 'memory'} env_source = 'CODEPACK_DELIVERY_SOURCE' try: os.environ[env_source] = 'memory' mds = Default.get_service('delivery', 'delivery_service') assert isinstance(mds.storage, MemoryStorage) finally: for env in [env_source]: os.environ.pop(env, None)
def test_default_memory_code_storage_service_with_os_env(): config = Config() storage_config = config.get_storage_config('code') assert storage_config == {'source': 'memory'} env_source = 'CODEPACK_CODE_SOURCE' try: os.environ[env_source] = 'memory' mss = Default.get_service('code', 'storage_service') assert hasattr(mss.storage, 'memory') assert mss.storage.item_type == Code finally: if env_source in os.environ: os.environ.pop(env_source, None)
def test_default_file_delivery_service_with_os_env(): config = Config() storage_config = config.get_storage_config('delivery') assert storage_config == {'source': 'memory'} env_source = 'CODEPACK_DELIVERY_SOURCE' env_path = 'CODEPACK_DELIVERY_PATH' try: os.environ[env_source] = 'file' os.environ[env_path] = 'tmp/' fds = Default.get_service('delivery', 'delivery_service') assert hasattr(fds.storage, 'path') assert fds.storage.path == 'tmp/' finally: for env in [env_source, env_path]: os.environ.pop(env, None)
def test_default_file_code_snapshot_service_with_os_env(): config = Config() storage_config = config.get_storage_config('code_snapshot') assert storage_config == {'source': 'memory'} env_source = 'CODEPACK_CODESNAPSHOT_SOURCE' env_path = 'CODEPACK_CODESNAPSHOT_PATH' try: os.environ[env_source] = 'file' os.environ[env_path] = 'tmp/' fss = Default.get_service('code_snapshot', 'snapshot_service') assert hasattr(fss.storage, 'path') assert fss.storage.path == 'tmp/' finally: for env in [env_source, env_path]: os.environ.pop(env, None)
def test_default_mongo_code_snapshot_service_with_os_env(): config = Config() storage_config = config.get_storage_config('code_snapshot') assert storage_config == {'source': 'memory'} env_source = 'CODEPACK_CODESNAPSHOT_SOURCE' env_db = 'CODEPACK_CODESNAPSHOT_DB' env_collection = 'CODEPACK_CODESNAPSHOT_COLLECTION' mss = None try: os.environ[env_source] = 'mongodb' os.environ[env_db] = 'test' os.environ[env_collection] = 'snapshot' mss = Default.get_service('code_snapshot', 'snapshot_service') finally: if mss is not None and not mss.storage.mongodb.closed(): mss.storage.mongodb.close() for env in [env_source, env_db, env_collection]: os.environ.pop(env, None) assert hasattr(mss.storage, 'mongodb')
def test_default_memory_code_snapshot_service_with_os_env(fake_mongodb): mss = None try: config = Config() storage_config = config.get_storage_config('code_snapshot') assert storage_config == {'source': 'memory'} os.environ['CODEPACK_CODESNAPSHOT_SOURCE'] = 'mongodb' os.environ['CODEPACK_CODESNAPSHOT_DB'] = 'test_db' os.environ['CODEPACK_CODESNAPSHOT_COLLECTION'] = 'test_collection' mss = Default.get_service('code_snapshot', 'snapshot_service') assert hasattr(mss.storage, 'mongodb') assert mss.storage.db == 'test_db' assert mss.storage.collection == 'test_collection' finally: if mss is not None and not mss.storage.mongodb.closed(): mss.storage.mongodb.close() os.environ.pop('CODEPACK_CODESNAPSHOT_SOURCE', None) os.environ.pop('CODEPACK_CODESNAPSHOT_DB', None) os.environ.pop('CODEPACK_CODESNAPSHOT_COLLECTION', None)
def test_default_mongo_delivery_service_with_os_env(): config = Config() storage_config = config.get_storage_config('delivery') assert storage_config == {'source': 'memory'} env_source = 'CODEPACK_DELIVERY_SOURCE' env_db = 'CODEPACK_DELIVERY_DB' env_collection = 'CODEPACK_DELIVERY_COLLECTION' mds = None try: os.environ[env_source] = 'mongodb' os.environ[env_db] = 'test' os.environ[env_collection] = 'delivery' mds = Default.get_service('delivery', 'delivery_service') assert hasattr(mds.storage, 'mongodb') assert mds.storage.db == 'test' assert mds.storage.collection == 'delivery' finally: for env in [env_source, env_db, env_collection]: os.environ.pop(env, None) if mds is not None and not mds.storage.mongodb.closed(): mds.storage.mongodb.close()
def test_default_mongo_code_storage_service_with_os_env(): config = Config() storage_config = config.get_storage_config('code') assert storage_config == {'source': 'memory'} env_source = 'CODEPACK_CODE_SOURCE' env_db = 'CODEPACK_CODE_DB' env_collection = 'CODEPACK_CODE_COLLECTION' mss = None try: os.environ[env_source] = 'mongodb' os.environ[env_db] = 'test' os.environ[env_collection] = 'codes' mss = Default.get_service('code', 'storage_service') assert hasattr(mss.storage, 'mongodb') assert mss.storage.item_type == Code assert mss.storage.db == 'test' assert mss.storage.collection == 'codes' finally: for env in [env_source, env_db, env_collection]: os.environ.pop(env, None) if mss is not None and not mss.storage.mongodb.closed(): mss.storage.mongodb.close()