def setUp(self): self.empty_token = Token() self.test_token = Token('89c38542-0c78-41f1-bcd2-5226189ccab9', '89c38542-0c78-41f1-bcd2-5226189ddab1', '2013-04-01T21:58:16.995031Z') self.true_token_string = '89c38542-0c78-41f1-bcd2-5226189ccab9' self.previous_token_string = '89c38542-0c78-41f1-bcd2-5226189ddab1' self.false_token_string = '89c38542-0c78-41f1-bcd2-5226189d453sh' self.empty_token_string = ''
def setUp(self): self.cache_clear = MagicMock() self.cache_true = MagicMock(return_value=True) self.cache_false = MagicMock(return_value=False) self.cache_update = MagicMock() self.cache_set = MagicMock() self.cache_del = MagicMock() self.tenant_id = '101' self.token = Token() self.token_json = jsonutils.dumps(self.token.format()) self.cache_get_token = MagicMock(return_value=self.token_json)
class WhenTestingTokenObject(unittest.TestCase): def setUp(self): self.empty_token = Token() self.test_token = Token('89c38542-0c78-41f1-bcd2-5226189ccab9', '89c38542-0c78-41f1-bcd2-5226189ddab1', '2013-04-01T21:58:16.995031Z') self.true_token_string = '89c38542-0c78-41f1-bcd2-5226189ccab9' self.previous_token_string = '89c38542-0c78-41f1-bcd2-5226189ddab1' self.false_token_string = '89c38542-0c78-41f1-bcd2-5226189d453sh' self.empty_token_string = '' def test_token_new(self): self.assertIsNot(self.empty_token.valid, '89c38542-0c78-41f1-bcd2-5226189ccab9') self.assertEqual(self.empty_token.previous, None) self.assertIsNot(self.empty_token.last_changed, '2013-04-01T21:58:16.995031Z') def test_token_reset(self): self.test_token.reset_token() self.assertIsNot(self.test_token.valid, '89c38542-0c78-41f1-bcd2-5226189ccab9') self.assertEqual(self.test_token.previous, '89c38542-0c78-41f1-bcd2-5226189ccab9') self.assertIsNot(self.test_token.last_changed, '2013-04-01T21:58:16.995031Z') def test_token_reset_token_now(self): self.test_token.reset_token_now() self.assertIsNot(self.test_token.valid, '89c38542-0c78-41f1-bcd2-5226189ccab9') self.assertEqual(self.test_token.previous, None) self.assertIsNot(self.test_token.last_changed, '2013-04-01T21:58:16.995031Z') def test_token_format(self): token_dict = self.test_token.format() self.assertEqual(token_dict['valid'], '89c38542-0c78-41f1-bcd2-5226189ccab9') self.assertEqual(token_dict['previous'], '89c38542-0c78-41f1-bcd2-5226189ddab1') self.assertEqual(token_dict['last_changed'], '2013-04-01T21:58:16.995031Z') def test_token_validate(self): self.assertFalse( self.test_token.validate_token(self.empty_token_string)) self.assertTrue(self.test_token.validate_token(self.true_token_string)) self.assertTrue( self.test_token.validate_token(self.previous_token_string)) self.assertFalse( self.test_token.validate_token(self.false_token_string))
def find_tenant(ds_handler, tenant_id, create_on_missing=False): """ Retrieves a dictionary describing a tenant object and its Hosts, Profiles, and eventProducers and maps them to a tenant object """ # get the tenant dictionary form the data source tenant_dict = ds_handler.find_one('tenant', {'tenant_id': tenant_id}) if tenant_dict: tenant = load_tenant_from_dict(tenant_dict) return tenant if create_on_missing: #create new token for the tenant new_token = Token() new_tenant = Tenant(tenant_id, new_token) ds_handler.put('tenant', new_tenant.format()) ds_handler.create_sequence(new_tenant.tenant_id) tenant_dict = ds_handler.find_one('tenant', {'tenant_id': tenant_id}) tenant = load_tenant_from_dict(tenant_dict) return tenant return None
def setUp(self): self.timestamp = "2013-03-19T18:16:48.411029Z" self.producers = [ EventProducer(432, 'producer1', 'syslog', durable=True), EventProducer(433, 'producer2', 'syslog', durable=False) ] self.token = Token('ffe7104e-8d93-47dc-a49a-8fb0d39e5192', 'bbd6302e-8d93-47dc-a49a-8fb0d39e5192', "2013-03-19T18:16:48.411029Z") self.tenant_id = '1234' self.tenant = Tenant(self.tenant_id, self.token, event_producers=self.producers) self.tenant_found = MagicMock(return_value=self.tenant) self.cache = MagicMock() self.valid_message_token = 'ffe7104e-8d93-47dc-a49a-8fb0d39e5192' self.invalid_message_token = 'yyy7104e-8d93-47dc-a49a-8fb0d39e5192' self.get_token = MagicMock(return_value=self.token) self.get_tenant = MagicMock(return_value=self.tenant) self.get_none = MagicMock(return_value=None) self.config = WorkerConfiguration( personality='correlation', personality_module='meniscus.personas.worker.correlation.app', worker_id='fgc7104e-8d93-47dc-a49a-8fb0d39e5192', worker_token='bbd6307f-8d93-47dc-a49a-8fb0d39e5192', coordinator_uri='http://192.168.1.2/v1') self.get_config = MagicMock(return_value=self.config)
def before(self): self.db_handler = MagicMock() self.req = MagicMock() self.req.content_type = 'application/json' self.resp = MagicMock() self.producer_id = 432 self.producer_name = 'producer1' self.producer_id_2 = 432 self.producer_name_2 = 'producer2' self.not_valid_producer_id = 777 self.producers = [ EventProducer(self.producer_id, self.producer_name, 'syslog'), EventProducer(self.producer_id_2, self.producer_name_2, 'syslog') ] self.token_original = 'ffe7104e-8d93-47dc-a49a-8fb0d39e5192' self.token_previous = 'bbd6302e-8d93-47dc-a49a-8fb0d39e5192' self.token_invalid = 'xxxyyy33-8d93-47dc-a49a-8fb0d39e5192' self.timestamp_original = "2013-03-19T18:16:48.411029Z" self.token = Token(self.token_original, self.token_previous, self.timestamp_original) self.tenant_id = '1234' self.tenant_name = 'TenantName' self.tenant = Tenant(self.tenant_id, self.token, event_producers=self.producers) self.tenant_not_found = MagicMock(return_value=None) self.tenant_found = MagicMock(return_value=self.tenant) self._set_resource()
def test_iso_timestamp_format_should_throw_exception_for_time_limit(self): bad_time_format = "2013-03-19" new_token = Token('ffe7104e-8d93-47dc-a49a-8fb0d39e5192', None, bad_time_format) self.assertRaises(ValueError, self.resource._validate_token_min_time_limit_reached, new_token)
def setUp(self): self.test_token = Token('89c38542-0c78-41f1-bcd2-5226189ccab9', '89c38542-0c78-41f1-bcd2-5226189ddab1', '2013-04-01T21:58:16.995031Z') self.test_tenant_bare = Tenant('1022', self.test_token) self.test_tenant = Tenant('1022', self.test_token, [], 'MDBid', 'TenantName')
def create_tenant(tenant_id, tenant_name=None): """ Creates a new tenant and and persists to the datastore """ #create new token for the tenant new_token = Token() new_tenant = Tenant(tenant_id, new_token, tenant_name=tenant_name) #save the new tenant to the datastore _db_handler.put('tenant', new_tenant.format()) #create a new sequence for the tenant for creation of IDs on child objects _db_handler.create_sequence(new_tenant.tenant_id) #create an index for the tenant in the default sink # and enables time to live for the default doc_type mapping_tasks.create_index.delay(tenant_id)
def setUp(self): self.producers = [ EventProducer(432, 'producer1', 'syslog', durable=True, sinks=VALID_SINKS), EventProducer(433, 'producer2', 'syslog', durable=False) ] self.token = Token('ffe7104e-8d93-47dc-a49a-8fb0d39e5192', 'bbd6302e-8d93-47dc-a49a-8fb0d39e5192', "2013-03-19T18:16:48.411029Z") self.tenant_id = '1234' self.tenant_name = 'TenantName' self.tenant = Tenant(self.tenant_id, self.token, event_producers=self.producers, tenant_name=self.tenant_name) self.destination = {'transaction_id': None, 'transaction_time': None}
def on_post(self, req, resp, validated_body): body = validated_body['tenant'] tenant_id = str(body['tenant_id']) tenant_name = body.get('tenant_name', tenant_id) #validate that tenant does not already exists tenant = find_tenant(self.db, tenant_id=tenant_id) if tenant: abort( falcon.HTTP_400, 'Tenant with tenant_id {0} ' 'already exists'.format(tenant_id)) #create new token for the tenant new_token = Token() new_tenant = Tenant(tenant_id, new_token, tenant_name=tenant_name) self.db.put('tenant', new_tenant.format()) self.db.create_sequence(new_tenant.tenant_id) resp.status = falcon.HTTP_201 resp.set_header('Location', '/v1/{0}'.format(tenant_id))
class WhenTestingTokenCache(unittest.TestCase): def setUp(self): self.cache_clear = MagicMock() self.cache_true = MagicMock(return_value=True) self.cache_false = MagicMock(return_value=False) self.cache_update = MagicMock() self.cache_set = MagicMock() self.cache_del = MagicMock() self.tenant_id = '101' self.token = Token() self.token_json = jsonutils.dumps(self.token.format()) self.cache_get_token = MagicMock(return_value=self.token_json) def test_clear_calls_cache_clear(self): with patch.object(NativeProxy, 'cache_clear', self.cache_clear): token_cache = TokenCache() token_cache.clear() self.cache_clear.assert_called_once_with(CACHE_TOKEN) def test_set_token_calls_cache_update(self): with patch.object( NativeProxy, 'cache_exists', self.cache_true ), patch.object(NativeProxy, 'cache_update', self.cache_update): token_cache = TokenCache() token_cache.set_token(self.tenant_id, self.token) self.cache_update.assert_called_once_with( self.tenant_id, jsonutils.dumps(self.token.format()), DEFAULT_EXPIRES, CACHE_TOKEN) def test_set_token_calls_cache_set(self): with patch.object( NativeProxy, 'cache_exists', self.cache_false ), patch.object(NativeProxy, 'cache_set', self.cache_set): token_cache = TokenCache() token_cache.set_token(self.tenant_id, self.token) self.cache_set.assert_called_once_with( self.tenant_id, jsonutils.dumps(self.token.format()), DEFAULT_EXPIRES, CACHE_TOKEN) def test_get_token_calls_returns_tenant(self): with patch.object( NativeProxy, 'cache_exists', self.cache_true ), patch.object(NativeProxy, 'cache_get', self.cache_get_token): token_cache = TokenCache() token = token_cache.get_token(self.tenant_id) self.cache_get_token.assert_called_once_with( self.tenant_id, CACHE_TOKEN) self.assertIsInstance(token, Token) def test_get_token_calls_returns_none(self): with patch.object( NativeProxy, 'cache_exists', self.cache_false): token_cache = TokenCache() token = token_cache.get_token(self.tenant_id) self.assertIs(token, None) def test_delete_token_calls_cache_del(self): with patch.object( NativeProxy, 'cache_exists', self.cache_true ), patch.object(NativeProxy, 'cache_del', self.cache_del): token_cache = TokenCache() token_cache.delete_token(self.tenant_id) self.cache_del.assert_called_once_with( self.tenant_id, CACHE_TOKEN) def test_delete_token_does_not_call_cache_del(self): with patch.object( NativeProxy, 'cache_exists', self.cache_false ), patch.object(NativeProxy, 'cache_del', self.cache_del): token_cache = TokenCache() token_cache.delete_token(self.tenant_id) with self.assertRaises(AssertionError): self.cache_del.assert_called_once_with( self.tenant_id, CACHE_TOKEN)
def test_should_throw_exception_for_time_limit_not_reached(self): new_token = Token() self.assertRaises(falcon.HTTPError, self.resource._validate_token_min_time_limit_reached, new_token)
def load_token_from_dict(token_dict): token = Token(token_dict['valid'], token_dict['previous'], token_dict['last_changed']) return token
def setUp(self): self.tenant_id = '5164b8f4-16fb-4376-9d29-8a6cbaa02fa9' self.message_token = 'ffe7104e-8d93-47dc-a49a-8fb0d39e5192' self.producers = [ EventProducer(432, 'producer1', 'syslog', durable=True), EventProducer(433, 'producer2', 'syslog', durable=False) ] self.invalid_message_token = 'yyy7104e-8d93-47dc-a49a-8fb0d39e5192' self.token = Token('ffe7104e-8d93-47dc-a49a-8fb0d39e5192', 'bbd6302e-8d93-47dc-a49a-8fb0d39e5192', '2013-03-19T18:16:48.411029Z') self.tenant = Tenant(self.tenant_id, self.token, event_producers=self.producers) self.get_token = MagicMock(return_value=self.token) self.get_tenant = MagicMock(return_value=self.tenant) self.get_none = MagicMock(return_value=None) self.src_msg = { 'HOST': 'tohru', '_SDATA': { 'meniscus': { 'token': self.message_token, 'tenant': self.tenant_id } }, 'PRIORITY': 'info', 'MESSAGE': '127.0.0.1 - - [12/Jul/2013:19:40:58 +0000] ' '\'GET /test.html HTTP/1.1\' 404 466 \'-\' ' '\'curl/7.29.0\'', 'FACILITY': 'local1', 'MSGID': '345', 'ISODATE': '2013-07-12T14:17:00+00:00', 'PROGRAM': 'apache', 'DATE': '2013-07-12T14:17:00.134+00:00', 'PID': '234' } self.malformed_sys_msg = { 'HOST': 'tohru', '_SDATA': { 'meniscus': { 'token': '', 'tenant': '' } }, 'PRIORITY': 'info', 'MESSAGE': '127.0.0.1 - - [12/Jul/2013:19:40:58 +0000] ' '\'GET /test.html HTTP/1.1\' 404 466 \'-\' ' '\'curl/7.29.0\'', 'FACILITY': 'local1', 'MSGID': '345', 'ISODATE': '2013-07-12T14:17:00+00:00', 'PROGRAM': 'apache', 'DATE': '2013-07-12T14:17:00.134+00:00', 'PID': '234' } self.cee_msg = { 'host': 'tohru', 'pri': 'info', 'msg': '127.0.0.1 - - [12/Jul/2013:19:40:58 +0000] ' '\'GET /test.html HTTP/1.1\' 404 466 \'-\' ' '\'curl/7.29.0\'', 'msgid': '345', 'time': '2013-07-12T14:17:00+00:00', 'pname': 'apache', 'pid': '234', 'ver': '1', 'native': { 'meniscus': { 'token': 'ffe7104e-8d93-47dc-a49a-8fb0d39e5192', 'tenant': '5164b8f4-16fb-4376-9d29-8a6cbaa02fa9' } } } self.config = WorkerConfiguration( personality='worker', hostname='worker01', coordinator_uri='http://192.168.1.2/v1') self.get_config = MagicMock(return_value=self.config) self.tenant_found = MagicMock(return_value=self.tenant)
class WhenTestingTokenCache(unittest.TestCase): def setUp(self): self.cache_clear = MagicMock() self.cache_true = MagicMock(return_value=True) self.cache_false = MagicMock(return_value=False) self.cache_update = MagicMock() self.cache_set = MagicMock() self.cache_del = MagicMock() self.tenant_id = '101' self.token = Token() self.token_json = jsonutils.dumps(self.token.format()) self.cache_get_token = MagicMock(return_value=self.token_json) def test_clear_calls_cache_clear(self): with patch.object(NativeProxy, 'cache_clear', self.cache_clear): token_cache = TokenCache() token_cache.clear() self.cache_clear.assert_called_once_with(CACHE_TOKEN) def test_set_token_calls_cache_update(self): with patch.object(NativeProxy, 'cache_exists', self.cache_true), patch.object( NativeProxy, 'cache_update', self.cache_update): token_cache = TokenCache() token_cache.set_token(self.tenant_id, self.token) self.cache_update.assert_called_once_with( self.tenant_id, jsonutils.dumps(self.token.format()), DEFAULT_EXPIRES, CACHE_TOKEN) def test_set_token_calls_cache_set(self): with patch.object(NativeProxy, 'cache_exists', self.cache_false), patch.object( NativeProxy, 'cache_set', self.cache_set): token_cache = TokenCache() token_cache.set_token(self.tenant_id, self.token) self.cache_set.assert_called_once_with( self.tenant_id, jsonutils.dumps(self.token.format()), DEFAULT_EXPIRES, CACHE_TOKEN) def test_get_token_calls_returns_tenant(self): with patch.object(NativeProxy, 'cache_exists', self.cache_true), patch.object( NativeProxy, 'cache_get', self.cache_get_token): token_cache = TokenCache() token = token_cache.get_token(self.tenant_id) self.cache_get_token.assert_called_once_with(self.tenant_id, CACHE_TOKEN) self.assertIsInstance(token, Token) def test_get_token_calls_returns_none(self): with patch.object(NativeProxy, 'cache_exists', self.cache_false): token_cache = TokenCache() token = token_cache.get_token(self.tenant_id) self.assertIs(token, None) def test_delete_token_calls_cache_del(self): with patch.object(NativeProxy, 'cache_exists', self.cache_true), patch.object( NativeProxy, 'cache_del', self.cache_del): token_cache = TokenCache() token_cache.delete_token(self.tenant_id) self.cache_del.assert_called_once_with(self.tenant_id, CACHE_TOKEN) def test_delete_token_does_not_call_cache_del(self): with patch.object(NativeProxy, 'cache_exists', self.cache_false), patch.object( NativeProxy, 'cache_del', self.cache_del): token_cache = TokenCache() token_cache.delete_token(self.tenant_id) with self.assertRaises(AssertionError): self.cache_del.assert_called_once_with(self.tenant_id, CACHE_TOKEN)