def test_init_ds_args(self): """Make sure that just passing in data source information constructs an AggregatingDataSource with the expected attributes. """ expected_data_sources = [ { 'data_server_url': "http://a.com", 'data_source_hash': util.generate_ds_key("some.data.source"), 'secret_key': "TEST_SECRET_ONE" }, { 'data_server_url': "http://b.com", 'data_source_hash': util.generate_ds_key("another.data.source"), 'secret_key': "TEST_SECRET_TWO" }, { 'data_server_url': "http://c.com", 'data_source_hash': 'ae22d4c', 'secret_key': "TEST_SECRET_THREE" } ] T.assert_equal(self.data_source.data_sources, expected_data_sources) desc_data_source = AggregatingDataSource(data_sources=self.test_data_sources, desc=mock.sentinel.test_desc) T.assert_equal(desc_data_source.data_sources, expected_data_sources) T.assert_equal(desc_data_source.DESC, mock.sentinel.test_desc)
def test_init_ds_args(self): """Make sure that just passing in data source information constructs an AggregatingDataSource with the expected attributes. """ expected_data_sources = [{ 'data_server_url': "http://a.com", 'data_source_hash': util.generate_ds_key("some.data.source"), 'secret_key': "TEST_SECRET_ONE" }, { 'data_server_url': "http://b.com", 'data_source_hash': util.generate_ds_key("another.data.source"), 'secret_key': "TEST_SECRET_TWO" }, { 'data_server_url': "http://c.com", 'data_source_hash': 'ae22d4c', 'secret_key': "TEST_SECRET_THREE" }] T.assert_equal(self.data_source.data_sources, expected_data_sources) desc_data_source = AggregatingDataSource( data_sources=self.test_data_sources, desc=mock.sentinel.test_desc) T.assert_equal(desc_data_source.data_sources, expected_data_sources) T.assert_equal(desc_data_source.DESC, mock.sentinel.test_desc)
def test_find_data_source_for_stat_key(self): """Tests _find_data_source_for_stat_key when it's provided by one of the configured data sources. """ expected_data_source = { 'data_server_url': "http://b.com", 'data_source_hash': util.generate_ds_key("another.data.source"), 'secret_key': "TEST_SECRET_TWO" } test_key = 'src.our_key' def fake_paths_from_ds(data_source, path): if data_source == expected_data_source: return [{"name": test_key},] else: return [{"name": "src.not_our_key"},] with mock.patch.object(self.data_source, '_request_paths_from_ds', fake_paths_from_ds): actual_ds = self.data_source._find_data_source_for_stat_key(test_key) T.assert_equal(expected_data_source, actual_ds) T.assert_in(test_key, self.data_source.key_mapping_cache) T.assert_equal(expected_data_source, self.data_source.key_mapping_cache[test_key])
def __init__(self, *args, **kwargs): """Reads the configuration passed to it and holds on to the data sources that are available to this data source. """ super(AggregatingDataSource, self).__init__(*args, **kwargs) self.data_sources = [] self.DESC = kwargs.get('desc', self.DESC) # Keeps track of the first entry in a key, mapping it to a specific data # source. The first instance of a key is always authoritative. self.key_mapping_cache = {} data_sources = kwargs['data_sources'] for data_source in data_sources: data_server_url = data_source['data_server_url'] data_source_name = data_source['data_source_name'] name_is_hash = data_source.get('name_is_hash', False) secret_key = data_source['secret_key'] if name_is_hash: data_source_hash = data_source_name else: data_source_hash = util.generate_ds_key(data_source_name) self.data_sources.append({ "data_server_url": data_server_url, "data_source_hash": data_source_hash, "secret_key": secret_key })
def test_find_data_source_for_stat_key(self): """Tests _find_data_source_for_stat_key when it's provided by one of the configured data sources. """ expected_data_source = { 'data_server_url': "http://b.com", 'data_source_hash': util.generate_ds_key("another.data.source"), 'secret_key': "TEST_SECRET_TWO" } test_key = 'src.our_key' def fake_paths_from_ds(data_source, path): if data_source == expected_data_source: return [ { "name": test_key }, ] else: return [ { "name": "src.not_our_key" }, ] with mock.patch.object(self.data_source, '_request_paths_from_ds', fake_paths_from_ds): actual_ds = self.data_source._find_data_source_for_stat_key( test_key) T.assert_equal(expected_data_source, actual_ds) T.assert_in(test_key, self.data_source.key_mapping_cache) T.assert_equal(expected_data_source, self.data_source.key_mapping_cache[test_key])