def test_parse_logline_returns_none_without_separators(self): """When no separators are found, parse_ci_logline returns None.""" expected_parse_ignores = [ '', '-', 'adsf-asdf', '2017-05-22 18:02:01,088', 'CLOUDINIT' ] for parse_ignores in expected_parse_ignores: self.assertIsNone(parse_ci_logline(parse_ignores))
def test_parse_logline_returns_event_for_finish_events(self): """parse_ci_logline returns a finish event for a parsed log line.""" line = ('2016-08-30 21:53:25.972325+00:00 y1 [CLOUDINIT]' ' handlers.py[DEBUG]: finish: modules-final: SUCCESS: running' ' modules for final') expected = { 'description': 'running modules for final', 'event_type': 'finish', 'name': 'modules-final', 'origin': 'cloudinit', 'result': 'SUCCESS', 'timestamp': 1472594005.972 } self.assertEqual(expected, parse_ci_logline(line))
def test_parse_logline_returns_event_for_cloud_init_logs(self): """parse_ci_logline returns an event parse from cloud-init format.""" line = ("2017-08-08 20:05:07,147 - util.py[DEBUG]: Cloud-init v. 0.7.9" " running 'init-local' at Tue, 08 Aug 2017 20:05:07 +0000. Up" " 6.26 seconds.") dt = datetime.strptime('2017-08-08 20:05:07,147', '%Y-%m-%d %H:%M:%S,%f') timestamp = float(dt.strftime('%s.%f')) expected = { 'description': 'starting search for local datasources', 'event_type': 'start', 'name': 'init-local', 'origin': 'cloudinit', 'timestamp': timestamp } self.assertEqual(expected, parse_ci_logline(line))
def test_parse_logline_returns_event_for_cloud_init_logs(self): """parse_ci_logline returns an event parse from cloud-init format.""" line = ("2017-08-08 20:05:07,147 - util.py[DEBUG]: Cloud-init v. 0.7.9" " running 'init-local' at Tue, 08 Aug 2017 20:05:07 +0000. Up" " 6.26 seconds.") dt = datetime.strptime("2017-08-08 20:05:07,147", "%Y-%m-%d %H:%M:%S,%f") timestamp = float(dt.strftime("%s.%f")) expected = { "description": "starting search for local datasources", "event_type": "start", "name": "init-local", "origin": "cloudinit", "timestamp": timestamp, } self.assertEqual(expected, parse_ci_logline(line))
def test_parse_logline_returns_event_for_journalctl_logs(self): """parse_ci_logline returns an event parse from journalctl format.""" line = ("Nov 03 06:51:06.074410 x2 cloud-init[106]: [CLOUDINIT]" " util.py[DEBUG]: Cloud-init v. 0.7.8 running 'init-local' at" " Thu, 03 Nov 2016 06:51:06 +0000. Up 1.0 seconds.") year = datetime.now().year dt = datetime.strptime( 'Nov 03 06:51:06.074410 %d' % year, '%b %d %H:%M:%S.%f %Y') timestamp = float(dt.strftime('%s.%f')) expected = { 'description': 'starting search for local datasources', 'event_type': 'start', 'name': 'init-local', 'origin': 'cloudinit', 'timestamp': timestamp} self.assertEqual(expected, parse_ci_logline(line))
def test_parse_logline_returns_event_for_cloud_init_logs(self): """parse_ci_logline returns an event parse from cloud-init format.""" line = ( "2017-08-08 20:05:07,147 - util.py[DEBUG]: Cloud-init v. 0.7.9" " running 'init-local' at Tue, 08 Aug 2017 20:05:07 +0000. Up" " 6.26 seconds.") dt = datetime.strptime( '2017-08-08 20:05:07,147', '%Y-%m-%d %H:%M:%S,%f') timestamp = float(dt.strftime('%s.%f')) expected = { 'description': 'starting search for local datasources', 'event_type': 'start', 'name': 'init-local', 'origin': 'cloudinit', 'timestamp': timestamp} self.assertEqual(expected, parse_ci_logline(line))
def test_parse_logline_returns_event_for_amazon_linux_2_line(self): line = ( "Apr 30 19:39:11 cloud-init[2673]: handlers.py[DEBUG]: start:" " init-local/check-cache: attempting to read from cache [check]") # Generate the expected value using `datetime`, so that TZ # determination is consistent with the code under test. timestamp_dt = datetime.strptime( "Apr 30 19:39:11", "%b %d %H:%M:%S" ).replace(year=datetime.now().year) expected = { 'description': 'attempting to read from cache [check]', 'event_type': 'start', 'name': 'init-local/check-cache', 'origin': 'cloudinit', 'timestamp': timestamp_dt.timestamp()} self.assertEqual(expected, parse_ci_logline(line))
def test_parse_logline_returns_event_for_journalctl_logs(self): """parse_ci_logline returns an event parse from journalctl format.""" line = ("Nov 03 06:51:06.074410 x2 cloud-init[106]: [CLOUDINIT]" " util.py[DEBUG]: Cloud-init v. 0.7.8 running 'init-local' at" " Thu, 03 Nov 2016 06:51:06 +0000. Up 1.0 seconds.") year = datetime.now().year dt = datetime.strptime("Nov 03 06:51:06.074410 %d" % year, "%b %d %H:%M:%S.%f %Y") timestamp = float(dt.strftime("%s.%f")) expected = { "description": "starting search for local datasources", "event_type": "start", "name": "init-local", "origin": "cloudinit", "timestamp": timestamp, } self.assertEqual(expected, parse_ci_logline(line))
def test_parse_logline_returns_event_for_finish_events(self, m_parse_from_date): """parse_ci_logline returns a finish event for a parsed log line.""" line = ('2016-08-30 21:53:25.972325+00:00 y1 [CLOUDINIT]' ' handlers.py[DEBUG]: finish: modules-final: SUCCESS: running' ' modules for final') expected = { 'description': 'running modules for final', 'event_type': 'finish', 'name': 'modules-final', 'origin': 'cloudinit', 'result': 'SUCCESS', 'timestamp': 1472594005.972} m_parse_from_date.return_value = "1472594005.972" self.assertEqual(expected, parse_ci_logline(line)) m_parse_from_date.assert_has_calls( [mock.call("2016-08-30 21:53:25.972325+00:00")])
def test_parse_logline_returns_event_for_finish_events( self, m_parse_from_date): """parse_ci_logline returns a finish event for a parsed log line.""" line = ("2016-08-30 21:53:25.972325+00:00 y1 [CLOUDINIT]" " handlers.py[DEBUG]: finish: modules-final: SUCCESS: running" " modules for final") expected = { "description": "running modules for final", "event_type": "finish", "name": "modules-final", "origin": "cloudinit", "result": "SUCCESS", "timestamp": 1472594005.972, } m_parse_from_date.return_value = "1472594005.972" self.assertEqual(expected, parse_ci_logline(line)) m_parse_from_date.assert_has_calls( [mock.call("2016-08-30 21:53:25.972325+00:00")])
def test_parse_logline_returns_none_without_separators(self): """When no separators are found, parse_ci_logline returns None.""" expected_parse_ignores = [ '', '-', 'adsf-asdf', '2017-05-22 18:02:01,088', 'CLOUDINIT'] for parse_ignores in expected_parse_ignores: self.assertIsNone(parse_ci_logline(parse_ignores))