Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(340)

Unified Diff: infra/tools/send_monitoring_event/test/send_event_test.py

Issue 1427903002: send_monitoring_event: support for default event and gomastats (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@event-mon-no-default-kind
Patch Set: Rebased Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: infra/tools/send_monitoring_event/test/send_event_test.py
diff --git a/infra/tools/send_monitoring_event/test/send_event_test.py b/infra/tools/send_monitoring_event/test/send_event_test.py
index e4db55fd70aba3deaa6d39651f85a5a111708e8f..4cdee7ac9acdb5f4b1c311af27d43c150da63eb8 100755
--- a/infra/tools/send_monitoring_event/test/send_event_test.py
+++ b/infra/tools/send_monitoring_event/test/send_event_test.py
@@ -6,10 +6,15 @@ import argparse
import os
import unittest
+import google.protobuf
+
import infra_libs
from infra_libs import event_mon
+
from infra.tools.send_monitoring_event import send_event
+from infra_libs.event_mon import (BuildEvent, ServiceEvent,
+ ChromeInfraEvent, LogRequestLite)
DATA_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data')
@@ -88,11 +93,249 @@ class TestBuildEvent(SendingEventBaseTest):
'--build-event-type', 'SCHEDULER',
'--build-event-hostname', 'foo.bar.dns',
'--build-event-build-name', 'whatever'])
- self.assertEquals(args.event_mon_run_type, 'dry')
- send_event.send_build_event(args)
+ self.assertTrue(send_event.send_build_event(args))
+
+ def test_send_build_event_smoke_missing_goma_file(self):
+ args = send_event.get_arguments(
+ ['--event-mon-service-name', 'thing',
+ '--build-event-type', 'BUILD',
+ '--build-event-hostname', 'foo.bar.dns',
+ '--build-event-build-name', 'whatever',
+ '--build-event-goma-stats-path',
+ os.path.join(DATA_DIR, 'this-file-does-not-exist')])
+ with self.assertRaises(IOError):
+ send_event.send_build_event(args)
-class TestEventsFromFile(SendingEventBaseTest):
+class TestInputModesFile(unittest.TestCase):
+ # Test the various ways to pass information to send_monitoring_event
+ # TODO(pgervais): test precedence order.
+ def tearDown(self):
+ event_mon.close()
+
+ def test_send_build_event_with_goma_stats(self):
+ # Write a file to avoid mocks
+ with infra_libs.temporary_directory(prefix='send_event_test-') as tmpdir:
+ outfile = os.path.join(tmpdir, 'out.bin')
+ args = send_event.get_arguments(
+ ['--event-mon-run-type', 'file',
+ '--event-mon-output-file', outfile,
+ '--event-mon-service-name', 'thing',
+ '--build-event-type', 'BUILD',
+ '--build-event-hostname', 'foo.bar.dns',
+ '--build-event-build-name', 'whatever',
+ '--build-event-goma-stats-path',
+ os.path.join(DATA_DIR, 'goma_stats.bin')])
+ self.assertEquals(args.event_mon_run_type, 'file')
+ event_mon.process_argparse_options(args)
+ self.assertTrue(send_event.send_build_event(args))
+
+ # Now open the resulting file and check what was written
+ with open(outfile, 'rb') as f:
+ request = LogRequestLite.FromString(f.read())
+
+ self.assertEqual(len(request.log_event), 1)
+ event = ChromeInfraEvent.FromString(request.log_event[0].source_extension)
+ self.assertEqual(event.build_event.goma_stats.request_stats.total, 10)
+ self.assertEqual(event.build_event.goma_stats.request_stats.success, 9)
+ self.assertEqual(event.build_event.goma_stats.request_stats.failure, 1)
+ self.assertEqual(event.build_event.host_name, 'foo.bar.dns')
+
+ def test_send_build_event_with_invalid_goma_stats(self):
+ # Write a file to avoid mocks
+ with infra_libs.temporary_directory(prefix='send_event_test-') as tmpdir:
+ outfile = os.path.join(tmpdir, 'out.bin')
+ args = send_event.get_arguments(
+ ['--event-mon-run-type', 'file',
+ '--event-mon-output-file', outfile,
+ '--event-mon-service-name', 'thing',
+ '--build-event-type', 'BUILD',
+ '--build-event-hostname', 'foo.bar.dns',
+ '--build-event-build-name', 'whatever',
+ '--build-event-goma-stats-path',
+ os.path.join(DATA_DIR, 'garbage')])
+ self.assertEquals(args.event_mon_run_type, 'file')
+ event_mon.process_argparse_options(args)
+ with self.assertRaises(google.protobuf.message.DecodeError):
+ send_event.send_build_event(args)
+
+ # The default event used below (build-foo-builder.bin) has been generated by:
+ # ./run.py infra.tools.send_monitoring_event \
+ # --event-mon-run-type=file \
+ # --event-mon-output-file=./build-foo-builder.bin \
+ # --build-event-hostname=myhostname \
+ # --event-mon-timestamp-kind=BEGIN \
+ # --event-mon-event-timestamp=123 \
+ # --build-event-type=BUILD \
+ # --build-event-build-name=foo"
+ def test_logrequest_path_valid_build_event(self):
+ with infra_libs.temporary_directory(prefix='send_event_test-') as tmpdir:
+ outfile = os.path.join(tmpdir, 'out.bin')
+ args = send_event.get_arguments(
+ ['--event-mon-run-type', 'file',
+ '--event-mon-output-file', outfile,
+ '--event-mon-service-name', 'thing',
+ '--event-logrequest-path',
+ os.path.join(DATA_DIR, 'build-foo-builder.bin'),
+ '--build-event-build-number', '3'
+ ])
+ self.assertEquals(args.event_mon_run_type, 'file')
+ event_mon.process_argparse_options(args)
+ send_event._process_logrequest_path(args)
+ self.assertTrue(send_event.send_build_event(args))
+
+ # Now open the resulting file and check what was written
+ with open(outfile, 'rb') as f:
+ request = LogRequestLite.FromString(f.read())
+
+ self.assertEqual(len(request.log_event), 1)
+ event = ChromeInfraEvent.FromString(request.log_event[0].source_extension)
+ self.assertEqual(event.build_event.host_name, 'myhostname')
+ self.assertEqual(event.build_event.build_number, 3)
+ self.assertEqual(event.timestamp_kind, ChromeInfraEvent.BEGIN)
+
+ def test_logrequest_path_build_type_override(self):
+ # logrequest contains build event, overrid the type with an arg.
+ with infra_libs.temporary_directory(prefix='send_event_test-') as tmpdir:
+ outfile = os.path.join(tmpdir, 'out.bin')
+ args = send_event.get_arguments(
+ ['--event-mon-run-type', 'file',
+ '--event-mon-output-file', outfile,
+ '--event-mon-service-name', 'thing',
+ '--event-logrequest-path',
+ os.path.join(DATA_DIR, 'build-foo-builder.bin'),
+ '--build-event-build-number', '3',
+ '--build-event-type', 'STEP',
+ ])
+ self.assertEquals(args.event_mon_run_type, 'file')
+ event_mon.process_argparse_options(args)
+ send_event._process_logrequest_path(args)
+ self.assertTrue(send_event.send_build_event(args))
+
+ # Now open the resulting file and check what was written
+ with open(outfile, 'rb') as f:
+ request = LogRequestLite.FromString(f.read())
+
+ self.assertEqual(len(request.log_event), 1)
+ event = ChromeInfraEvent.FromString(request.log_event[0].source_extension)
+ self.assertEqual(event.build_event.host_name, 'myhostname')
+ self.assertEqual(event.build_event.type, BuildEvent.STEP)
+ self.assertEqual(event.build_event.build_number, 3)
+ self.assertEqual(event.timestamp_kind, ChromeInfraEvent.BEGIN)
+
+ def test_logrequest_path_build_service_conflicts(self):
+ # logrequest contains build event, provides service event as arg
+ with infra_libs.temporary_directory(prefix='send_event_test-') as tmpdir:
+ outfile = os.path.join(tmpdir, 'out.bin')
+ args = send_event.get_arguments(
+ ['--event-mon-run-type', 'file',
+ '--event-mon-output-file', outfile,
+ '--event-mon-service-name', 'thing',
+ '--event-logrequest-path',
+ os.path.join(DATA_DIR, 'build-foo-builder.bin'),
+ '--build-event-build-number', '3',
+ '--service-event-type', 'START',
+ ])
+ self.assertEquals(args.event_mon_run_type, 'file')
+ event_mon.process_argparse_options(args)
+ with self.assertRaises(ValueError):
+ send_event._process_logrequest_path(args)
+
+ # The default event used below has been generated using:
+ # ./run.py infra.tools.send_monitoring_event
+ # --event-mon-run-type=file
+ # --event-mon-output-file=./service-bar-service.bin
+ # --service-event-type=START
+ # --event-mon-service-name=bar
+ # --event-mon-hostname=myhostname
+ # --event-mon-timestamp-kind=BEGIN
+ # --event-mon-event-timestamp=123
+ def test_logrequest_path_valid_service_event(self):
+ with infra_libs.temporary_directory(prefix='send_event_test-') as tmpdir:
+ outfile = os.path.join(tmpdir, 'out.bin')
+ args = send_event.get_arguments(
+ ['--event-mon-run-type', 'file',
+ '--event-mon-output-file', outfile,
+ '--event-mon-service-name', 'thing',
+ '--event-logrequest-path',
+ os.path.join(DATA_DIR, 'service-bar-service.bin'),
+ ])
+ self.assertEquals(args.event_mon_run_type, 'file')
+ event_mon.process_argparse_options(args)
+ send_event._process_logrequest_path(args)
+ self.assertTrue(send_event.send_service_event(args))
+
+ # Now open the resulting file and check what was written
+ with open(outfile, 'rb') as f:
+ request = LogRequestLite.FromString(f.read())
+
+ self.assertEqual(len(request.log_event), 1)
+ event = ChromeInfraEvent.FromString(request.log_event[0].source_extension)
+ self.assertEqual(event.event_source.host_name, 'myhostname')
+ self.assertEqual(event.service_event.type, ServiceEvent.START)
+ self.assertEqual(event.timestamp_kind, ChromeInfraEvent.BEGIN)
+
+ def test_logrequest_path_service_type_override(self):
+ with infra_libs.temporary_directory(prefix='send_event_test-') as tmpdir:
+ outfile = os.path.join(tmpdir, 'out.bin')
+ args = send_event.get_arguments(
+ ['--event-mon-run-type', 'file',
+ '--event-mon-output-file', outfile,
+ '--event-mon-service-name', 'thing',
+ '--event-logrequest-path',
+ os.path.join(DATA_DIR, 'service-bar-service.bin'),
+ '--service-event-type', 'STOP',
+ ])
+ self.assertEquals(args.event_mon_run_type, 'file')
+ event_mon.process_argparse_options(args)
+ send_event._process_logrequest_path(args)
+ self.assertTrue(send_event.send_service_event(args))
+
+ # Now open the resulting file and check what was written
+ with open(outfile, 'rb') as f:
+ request = LogRequestLite.FromString(f.read())
+
+ self.assertEqual(len(request.log_event), 1)
+ event = ChromeInfraEvent.FromString(request.log_event[0].source_extension)
+ self.assertEqual(event.event_source.host_name, 'myhostname')
+ self.assertEqual(event.service_event.type, ServiceEvent.STOP)
+ self.assertEqual(event.timestamp_kind, ChromeInfraEvent.END)
+
+ def test_logrequest_path_service_build_conflict(self):
+ with infra_libs.temporary_directory(prefix='send_event_test-') as tmpdir:
+ outfile = os.path.join(tmpdir, 'out.bin')
+ args = send_event.get_arguments(
+ ['--event-mon-run-type', 'file',
+ '--event-mon-output-file', outfile,
+ '--event-mon-service-name', 'thing',
+ '--event-logrequest-path',
+ os.path.join(DATA_DIR, 'service-bar-service.bin'),
+ '--build-event-type', 'BUILD',
+ ])
+ self.assertEquals(args.event_mon_run_type, 'file')
+ event_mon.process_argparse_options(args)
+ with self.assertRaises(ValueError):
+ send_event._process_logrequest_path(args)
+
+ def test_logrequest_path_service_build_and_service(self):
+ # The logrequest provided contains both a service and a build type,
+ # which is invalid.
+ with infra_libs.temporary_directory(prefix='send_event_test-') as tmpdir:
+ outfile = os.path.join(tmpdir, 'out.bin')
+ args = send_event.get_arguments(
+ ['--event-mon-run-type', 'file',
+ '--event-mon-output-file', outfile,
+ '--event-mon-service-name', 'thing',
+ '--event-logrequest-path',
+ os.path.join(DATA_DIR, 'build-and-service-event.bin'),
+ ])
+ self.assertEquals(args.event_mon_run_type, 'file')
+ event_mon.process_argparse_options(args)
+ with self.assertRaises(ValueError):
+ send_event._process_logrequest_path(args)
+
+
+class TestEventsFromTextFile(SendingEventBaseTest):
def test_send_events_from_file_smoke(self):
# Create a temporary file because we don't want to risk deleting a
# checked-in file.
@@ -215,3 +458,53 @@ class TestGetEventsFileList(unittest.TestCase):
self.assertTrue(len(file_list) == 3)
for fname in file_list:
self.assertTrue(any(fname.endswith(filename) for filename in filenames))
+
+
+class TestProcessRequestPath(SendingEventBaseTest):
+ def test_logrequest_missing_args(self):
+ orig_event = event_mon.get_default_event()
+ self.assertIsNot(orig_event, None)
+
+ args = argparse.Namespace()
+ args.event_logrequest_path = None
+ send_event._process_logrequest_path(args)
+
+ self.assertEqual(orig_event, event_mon.get_default_event())
+
+ def test_logrequest_with_valid_file(self):
+ orig_event = event_mon.get_default_event()
+ self.assertIsNot(orig_event, None)
+
+ args = argparse.Namespace()
+ args.event_logrequest_path = os.path.join(DATA_DIR, 'logrequest-build.bin')
+ args.service_event_type = None
+ args.build_event_type = None
+ send_event._process_logrequest_path(args)
+
+ new_event = event_mon.get_default_event()
+ self.assertNotEqual(orig_event, new_event)
+ self.assertEqual(new_event.build_event.type, BuildEvent.BUILD)
+
+ def test_logrequest_with_no_log_event(self):
+ orig_event = event_mon.get_default_event()
+ self.assertIsNot(orig_event, None)
+
+ args = argparse.Namespace()
+ args.event_logrequest_path = os.path.join(DATA_DIR, 'logrequest-empty.bin')
+ with self.assertRaises(ValueError):
+ send_event._process_logrequest_path(args)
+
+ def test_logrequest_with_bad_content(self):
+ orig_event = event_mon.get_default_event()
+ self.assertIsNot(orig_event, None)
+
+ args = argparse.Namespace()
+ args.event_logrequest_path = os.path.join(DATA_DIR, 'garbage')
+ with self.assertRaises(google.protobuf.message.DecodeError):
+ send_event._process_logrequest_path(args)
+
+ def test_logrequest_with_missing_file(self):
+ args = argparse.Namespace()
+ args.event_logrequest_path = os.path.join(DATA_DIR, 'non-existent-file.bin')
+ with self.assertRaises(IOError):
+ send_event._process_logrequest_path(args)
« no previous file with comments | « infra/tools/send_monitoring_event/test/data/service-bar-service.bin ('k') | infra_libs/event_mon/__init__.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698