def test_construction(self): self.assertEqual(self.observer.session, None) self.assertEqual(self.observer.sessions, []) self.assertEqual(self.observer.log_type, LogType.User) self.assertLessEqual(now() - from_utc_string(self.observer.logs[0]['time']), timedelta(seconds=1)) del self.observer.logs[0]['time'] self.assertEqual(self.observer.logs, [{ 'level': 'info', 'source': 'mdstudio.logging.impl.session_observer.SessionLogObserver', 'message': 'Collecting logs on session "MagicMock"' }]) self.assertEqual(self.observer.flushing, False) self.assertEqual(os.path.abspath(self.observer.recovery_file(self.session)), os.path.abspath('/logs/recovery.json'))
def test_call2(self): t = now() self.observer({ 'message': 'hello test', 'log_namespace': 'test namespace', 'log_level': LogType.Group, 'log_time': timestamp(t), }) self.assertLessEqual(t - from_utc_string(self.observer.logs[1]['time']), timedelta(seconds=1)) del self.observer.logs[1]['time'] self.assertEqual(self.observer.logs[1], { 'level': 'Group', 'source': 'test namespace', 'message': 'hello test' })
def parse_date_time(self, val, sub, key, *args, **kwargs): if isinstance(val, (six.text_type, str)): return from_utc_string(val) elif isinstance(val, datetime.datetime): if not val.tzinfo: raise DatabaseException( "No timezone information found. All datetime info should be stored in UTC format, " "please use 'mdstudio.utc.now()' and 'mdstudio.utc.to_utc_string()'" ) if val.tzinfo != pytz.utc: val = val.astimezone(pytz.utc) return val else: raise DatabaseException( "Failed to parse datetime field '{}' with key '{}'".format( val, key))
def call_hello(self): """ call_hello Calling the 'hello' endpoint on our own microservice. """ send_time = now() response = yield self.group_context('mdgroup').call('mdgroup.hello_world.endpoint.hello', { 'greeting': 'Calling self', 'sendTime': send_time }) # Reporting some delay times of the round call receive_time = now() return_time = from_utc_string(response['returnTime']) self.report_delay('Component -> User', receive_time - return_time) self.report_delay('Total', receive_time - send_time)
def test_start_flushing(self): self.observer.session = self.session self.observer.sleep = mock.MagicMock() yield self.observer.start_flushing(self.session) self.assertLessEqual(now() - from_utc_string(self.observer.logs[0]['time']), timedelta(seconds=1)) del self.observer.logs[0]['time'] yield self.observer.flush_logs() self.assertFalse(os.path.isfile(self.observer.recovery_file(self.session))) self.assertEqual(self.observer.logs, []) self.session.flush_logs.assert_called_once_with([{ 'level': 'info', 'source': 'mdstudio.logging.impl.session_observer.SessionLogObserver', 'message': 'Collecting logs on session "MagicMock"' }]) self.assertEqual(self.observer.session, self.session)
def hello(self, request, claims): """ hello endpoint Accepts a 'request' dictionary as described by the hello-request.v1.json schema in <package>/schema/endpoints and returns a response dictionary described by hello-response.v1.json The response consist of the recieved message prefixed with 'Hello World!' and a return time stamp. """ # Service specific settings as defined in the package settings.*.yml/json are # exposed in self.component_config.settings if self.component_config.settings['printInEndpoint']: self.log.info('Endpoint request object:') pprint(request) return_time = now() # The 'sendTime' argument is not required. Set to current time if not provided if 'sendTime' not in request: self.log.info('No "sendTime" argument in request, set to current time') send_time = return_time else: send_time = from_utc_string(request['sendTime']) # Reuse the request dictionary as response request['greeting'] = 'Hello World!: {0}'.format(request['greeting']) request['sendTime'] = send_time.isoformat() request['returnTime'] = return_time.isoformat() # Log the call delay self.report_delay('User -> Component', return_time - send_time) if self.component_config.settings['printInEndpoint']: self.log.info('Endpoint response object:') pprint(request) # Return the request dictionary. This will be validated against the # hello-response.v1.json JSON schema return request
def test_store_recovery(self): with Patcher() as patcher: self.observer.session = self.session patcher.fs.MakeDirectory('/logs') self.assertLessEqual(now() - from_utc_string(self.observer.logs[0]['time']), timedelta(seconds=1)) del self.observer.logs[0]['time'] self.assertEqual(self.observer.logs, [{ 'level': 'info', 'source': 'mdstudio.logging.impl.session_observer.SessionLogObserver', 'message': 'Collecting logs on session "MagicMock"' }]) yield self.observer.store_recovery() self.assertEqual(self.observer.logs, []) with open(self.observer.recovery_file(self.session)) as f: self.assertEqual(json.load(f), [{ 'level': 'info', 'source': 'mdstudio.logging.impl.session_observer.SessionLogObserver', 'message': 'Collecting logs on session "MagicMock"' }])
def test_utc3(self): for i in range(100): n = self.faker.date_time() self.assertEqual(from_utc_string(n.isoformat()), n.replace(tzinfo=pytz.utc))
def test_utc2(self): for i in range(100): tz = pytz.timezone('Pacific/Johnston') n = self.faker.date_time(tzinfo=tz) self.assertEqual(from_utc_string(n.isoformat()), n)
def test_utc(self): for i in range(100): n = self.faker.iso8601(tzinfo=pytz.utc) self.assertEqual(to_utc_string(from_utc_string(n)), n)