Example #1
0
def init():
    '''Setup cobra
    '''

    path = getDefaultAppsConfigPath()
    appsConfig = AppsConfig(path)
    appsConfig.generateDefaultConfig()
def test_compose2():
    root = os.path.dirname(os.path.realpath(__file__))
    dataDir = os.path.join(root, 'test_data', 'apps_config')
    path = os.path.join(dataDir, 'apps_channel_builder.yaml')

    appsConfig = AppsConfig(path)
    appkey = 'health'
    rules = appsConfig.getChannelBuilderRules(appkey)

    msg = {
        'action': 'rtm/publish',
        'body': {
            'message': {
                'device': {
                    'game': 'ody'
                },
                'id': 'engine_fps_id'
            },
            'channels': ['sms_republished_v1_neo'],
        },
    }

    updatedMsg = updateMsg(rules, msg)

    channels = updatedMsg['body']['channels']
    assert len(channels) == 4

    assert 'sms_republished_v1_neo' in channels
    assert 'ody_engine_fps_id' in channels
    assert 'foo' in channels
    assert 'bar' in channels
Example #3
0
def makeRunner(debugMemory=False, enableStats=False, redisUrls=None):
    host = 'localhost'
    port = getFreePort()
    redisPassword = None
    redisCluster = False
    maxSubscriptions = -1
    idleTimeout = 10  # after 10 seconds it's a lost cause / FIXME(unused)

    if redisUrls is None:
        redisUrls = 'redis://localhost'

    appsConfigPath = tempfile.mktemp()
    appsConfig = AppsConfig(appsConfigPath)
    appsConfig.generateDefaultConfig()
    os.environ['COBRA_APPS_CONFIG'] = appsConfigPath

    runner = AppRunner(
        host,
        port,
        redisUrls,
        redisPassword,
        redisCluster,
        appsConfigPath,
        debugMemory,
        enableStats,
        maxSubscriptions,
        idleTimeout,
    )
    asyncio.get_event_loop().run_until_complete(runner.setup())
    return runner, appsConfigPath
def test_none_error():
    root = os.path.dirname(os.path.realpath(__file__))
    dataDir = os.path.join(root, 'test_data', 'apps_config')
    path = os.path.join(dataDir, 'apps_channel_builder_none_error.yaml')

    appsConfig = AppsConfig(path)
    appkey = '_pubsub'
    rules = appsConfig.getChannelBuilderRules(appkey)

    msg = {'action': 'rtm/publish', 'body': {'channels': ['a_channel']}}

    updatedMsg = updateMsg(rules, msg)

    channels = updatedMsg['body']['channels']
    assert len(channels) == 1

    assert 'a_channel' in channels
Example #5
0
def makeRunner(
    debugMemory=False, enableStats=False, redisUrls=None, probeRedisOnStartup=True
):
    host = 'localhost'
    port = getFreePort()
    redisPassword = None
    maxSubscriptions = -1
    idleTimeout = 10  # after 10 seconds it's a lost cause / FIXME(unused)
    debugMemoryNoTracemalloc = False
    debugMemoryPrintAllTasks = False

    redisCluster = False

    if redisUrls is None:
        if redisCluster:
            redisUrls = 'redis://localhost:11000'
        else:
            redisUrls = 'redis://localhost'

    appsConfigPath = tempfile.mktemp()
    appsConfig = AppsConfig(appsConfigPath)
    appsConfig.generateDefaultConfig()
    os.environ['COBRA_APPS_CONFIG'] = appsConfigPath

    runner = AppRunner(
        host,
        port,
        redisUrls,
        redisPassword,
        redisCluster,
        appsConfigPath,
        debugMemory,
        debugMemoryNoTracemalloc,
        debugMemoryPrintAllTasks,
        enableStats,
        maxSubscriptions,
        idleTimeout,
        probeRedisOnStartup,
        redisStartupProbingTimeout=5,
        messageMaxSize=getDefaultMessageMaxSize(),
    )
    asyncio.get_event_loop().run_until_complete(runner.setup())
    return runner, appsConfigPath
def test_remove_channel():
    root = os.path.dirname(os.path.realpath(__file__))
    dataDir = os.path.join(root, 'test_data', 'apps_config')
    path = os.path.join(dataDir, 'apps_channel_builder.yaml')

    appsConfig = AppsConfig(path)
    appkey = 'health'
    rules = appsConfig.getChannelBuilderRules(appkey)

    msg = {
        'action': 'rtm/publish',
        'body': {
            'message': {
                'data': {
                    'rate_control': {
                        'engine_fps_id': 60,
                        'engine_memory_used_id': 60,
                        'engine_message_loop_id': 86400,
                    }
                },
                'device': None,
                'id': 'sms_set_rate_control_id',
                'per_id_counter': 0,
                'session': '98c3eb2c042e42068fc4d2b39fe720f8',
                'timestamp': 1582066343026,
                'version': 1,
            },
            'channels': ['a_channel', 'sms_live_shard_v1.wiso.9'],
        },
    }

    updatedMsg = updateMsg(rules, msg)

    channels = updatedMsg['body']['channels']
    assert len(channels) == 3

    assert 'a_channel' in channels
    assert 'sms_live_shard_v1.wiso.9' not in channels
    assert 'bar' in channels
Example #7
0
    def __init__(
        self,
        host,
        port,
        redisUrls,
        redisPassword,
        redisCluster,
        appsConfigPath,
        debugMemory,
        debugMemoryNoTracemalloc,
        debugMemoryPrintAllTasks,
        enableStats,
        maxSubscriptions,
        idleTimeout,
        probeRedisOnStartup,
        redisStartupProbingTimeout,
        messageMaxSize,
    ):
        self.app = {}
        self.app['connections'] = {}
        self.app['apps_config_path'] = appsConfigPath
        self.app['max_subscriptions'] = maxSubscriptions
        self.app['idle_timeout'] = idleTimeout

        self.app['memory_debugger'] = debugMemory
        self.app['memory_debugger_no_tracemalloc'] = debugMemoryNoTracemalloc
        self.app['memory_debugger_print_all_tasks'] = debugMemoryPrintAllTasks
        self.app['redis_urls'] = redisUrls
        self.app['redis_password'] = redisPassword
        self.app['redis_cluster'] = redisCluster

        self.host = host
        self.port = port
        self.redisUrls = redisUrls
        self.redisPassword = redisPassword
        self.enableStats = enableStats
        self.probeRedisOnStartup = probeRedisOnStartup
        self.redisStartupProbingTimeout = redisStartupProbingTimeout
        self.messageMaxSize = messageMaxSize

        appsConfig = AppsConfig(appsConfigPath)
        self.app['apps_config'] = appsConfig

        # Create app redis connection handler, one per apps to avoid one busy
        # app blocking others
        self.redisClients = RedisClients(redisUrls, redisPassword,
                                         redisCluster, appsConfig)
        self.app['redis_clients'] = self.redisClients

        try:
            appsConfig.validateConfig()
        except ValueError as e:
            logging.error(f'Invalid apps config file: {e}')
            pass

        self.app['batch_publish_size'] = appsConfig.getBatchPublishSize()
        self.app['channel_max_length'] = appsConfig.getChannelMaxLength()
        self.server = None
Example #8
0
def test_get_roles_and_secret():
    root = os.path.dirname(os.path.realpath(__file__))
    dataDir = os.path.join(root, 'test_data', 'apps_config')
    path = os.path.join(dataDir, 'apps.yaml')

    appsConfig = AppsConfig(path)

    role = appsConfig.getDefaultRoleForApp('app_that_does_not_exist')
    assert role == ''

    secret = appsConfig.getDefaultSecretForApp('app_that_does_not_exist')
    assert secret == ''

    role = appsConfig.getDefaultRoleForApp('health')
    assert role == 'health'

    secret = appsConfig.getDefaultSecretForApp('health')
    assert secret == 'e3Ae82633cd59b22daea958bbb82ac92'
Example #9
0
def getDefaultSecretForApp(app) -> str:
    path = os.getenv('COBRA_APPS_CONFIG', getDefaultAppsConfigPath())
    appsConfig = AppsConfig(path)
    return appsConfig.getDefaultSecretForApp(app)
Example #10
0
def test_answer():
    root = os.path.dirname(os.path.realpath(__file__))
    dataDir = os.path.join(root, 'test_data', 'apps_config')
    path = os.path.join(dataDir, 'apps.yaml')

    appsConfig = AppsConfig(path)
    assert not appsConfig.isAppKeyValid('ASDCSDC')
    assert appsConfig.isAppKeyValid('AAAAAAAAAAAAAAAABBBBBBBBBBBBBBBB')
    assert appsConfig.isAppKeyValid('eeeeeeeeeeeeeeeeffffffffffffffff')
    assert appsConfig.isAppKeyValid('aaaaaaaaaaaaaaaabbbbbbbbbbbbbbbb')
    assert appsConfig.isAppKeyValid('_health')

    with pytest.raises(KeyError):
        appsConfig.getRoleSecret('blah', 'blah')

    with pytest.raises(KeyError):
        appsConfig.getRoleSecret('C1bD8A3F2Da58C43F0CBB357fe9c5b9a', 'blah')

    secret = appsConfig.getRoleSecret('eeeeeeeeeeeeeeeeffffffffffffffff',
                                      'client_publisher')
    assert secret == b'ggggggggggggggggggghhhhhhhhhhhhH'

    batchPublish = appsConfig.isBatchPublishEnabled(
        'AAAAAAAAAAAAAAAABBBBBBBBBBBBBBBB')
    assert not batchPublish

    batchPublish = appsConfig.isBatchPublishEnabled(
        'eeeeeeeeeeeeeeeeffffffffffffffff')
    assert batchPublish
Example #11
0
def test_empty_apps_file():
    appsConfig = AppsConfig('')
    assert not appsConfig.isAppKeyValid('ASDCSDC')