Exemplo n.º 1
0
 def _run_test(config, exception, message):
     settings = {
         'DATABASES': TEST_DATABASES,
     }
     with override_settings(**settings), assert_raises(exception,
                                                       msg=message):
         PlProxyConfig.from_dict(config)
Exemplo n.º 2
0
    def test_get_standby_plproxy_config_misconfigured(self):
        databases = copy.deepcopy(PARTITION_CONFIG_WITH_STANDBYS)
        databases['db1_standby']['PLPROXY'] = {}  # add plproxy config to a standby

        primary_config = PlProxyConfig.from_dict(databases)
        with override_settings(DATABASES=databases), self.assertRaises(PartitionValidationError):
            _get_standby_plproxy_config(primary_config)
Exemplo n.º 3
0
    def test_get_standby_plproxy_config_missing_standby(self):
        databases = copy.deepcopy(PARTITION_CONFIG_WITH_STANDBYS)
        del databases['db1_standby']  # remove one standby

        primary_config = PlProxyConfig.from_dict(databases)
        with override_settings(DATABASES=databases), self.assertRaises(PartitionValidationError):
            _get_standby_plproxy_config(primary_config)
Exemplo n.º 4
0
 def test_host_map(self):
     config = PlProxyConfig.from_dict(TEST_PARTITION_CONFIG_HOST_MAP)
     shards = config.get_shards()
     self.assertEqual(shards, [
         ShardMeta(id=0, dbname='db1', host='localhost', port=5432),
         ShardMeta(id=1, dbname='db2', host='hqdb2', port=5432),
     ])
Exemplo n.º 5
0
def test_load_balanced_plproxy():
    primary_config = PlProxyConfig.from_dict(PARTITION_CONFIG_WITH_STANDBYS)
    standby_config = _get_standby_plproxy_config(primary_config)

    master_standby_mapping = {
        'db1': {'db1_standby'},
        'db2': {'db2_standby'},
    }

    def _test_load_balanced_plproxy(primary_db, ok_standbys, expected_db):
        with patch('corehq.sql_db.util.plproxy_config', primary_config), \
             patch('corehq.sql_db.util.plproxy_standby_config', standby_config), \
             patch('corehq.sql_db.util.get_standbys_with_acceptible_delay', return_value=set(ok_standbys)), \
             patch('corehq.sql_db.util.primary_to_standbys_mapping', return_value=master_standby_mapping):

            db_for_read = select_plproxy_db_for_read(primary_db)
            assert_equal(db_for_read, expected_db)

    plproxy_shard_0 = primary_config.form_processing_dbs[0]
    plproxy_shard_0_standby = standby_config.form_processing_dbs[0]

    test_cases = [
        # (primary_db, standbys_with_acceptible_delay, expected_return_db)
        (primary_config.proxy_db, standby_config.form_processing_dbs, standby_config.proxy_db),
        (primary_config.proxy_db, standby_config.form_processing_dbs[0:1], primary_config.proxy_db),
        (primary_config.proxy_db, [], primary_config.proxy_db),
        (plproxy_shard_0, standby_config.form_processing_dbs, plproxy_shard_0_standby),
        (plproxy_shard_0, standby_config.form_processing_dbs[1:], plproxy_shard_0),
        (plproxy_shard_0, [], plproxy_shard_0),
    ]
    for case in test_cases:
        yield (_test_load_balanced_plproxy,) + case
Exemplo n.º 6
0
 def test_get_standby_plproxy_config(self):
     primary_config = PlProxyConfig.from_dict(PARTITION_CONFIG_WITH_STANDBYS)
     with override_settings(DATABASES=PARTITION_CONFIG_WITH_STANDBYS):
         standby_config = _get_standby_plproxy_config(primary_config)
     self.assertEqual('commcarehq', standby_config.cluster_name)
     self.assertEqual('proxy_standby', standby_config.proxy_db)
     self.assertEqual({'db1_standby', 'db2_standby'}, set(standby_config.form_processing_dbs))
     self.assertEqual(primary_config.shard_count, standby_config.shard_count)
Exemplo n.º 7
0
 def test_shard_mapping(self):
     config = PlProxyConfig.from_dict(TEST_PARTITION_CONFIG)
     shards = config.get_shards()
     self.assertEqual(shards, [
         ShardMeta(id=0, dbname='db1', host='hqdb1', port=5432),
         ShardMeta(id=1, dbname='db1', host='hqdb1', port=5432),
         ShardMeta(id=2, dbname='db2', host='hqdb2', port=5432),
         ShardMeta(id=3, dbname='db2', host='hqdb2', port=5432),
     ])
Exemplo n.º 8
0
 def test_get_shards_on_db_not_found(self):
     config = PlProxyConfig.from_dict(TEST_PARTITION_CONFIG)
     with self.assertRaises(NoSuchShardDatabaseError):
         config.get_shards_on_db('db3')
Exemplo n.º 9
0
 def test_get_shards_on_db(self):
     config = PlProxyConfig.from_dict(TEST_PARTITION_CONFIG)
     self.assertEqual([0, 1], config.get_shards_on_db('db1'))
     self.assertEqual([2, 3], config.get_shards_on_db('db2'))
Exemplo n.º 10
0
 def test_dbs_by_group(self):
     config = PlProxyConfig.from_dict(TEST_PARTITION_CONFIG)
     self.assertIn('db1', config.shard_map)
     self.assertIn('db2', config.shard_map)