Beispiel #1
0
 def test_serialize(self):
   t = targets.DeviceTarget('reg', 'net', 'host')
   m = metrics.StringMetric('test', target=t, fields={'bar': 1})
   m.set('val', fields={'baz': False})
   p = metrics_pb2.MetricsCollection()
   m.serialize_to(p)
   e = textwrap.dedent('''\
       data {
         name: "test"
         metric_name_prefix: "/chrome/infra/"
         network_device {
           alertable: true
           realm: "ACQ_CHROME"
           metro: "reg"
           hostname: "host"
           hostgroup: "net"
         }
         fields {
           name: "bar"
           type: INT
           int_value: 1
         }
         fields {
           name: "baz"
           type: BOOL
           bool_value: false
         }
         string_value: "val"
       }
   ''')
   self.assertEquals(str(p), e)
Beispiel #2
0
 def test_reset(self):
   m = metrics.StringMetric('test')
   self.assertIsNone(m.get())
   m.set('foo')
   self.assertEqual('foo', m.get())
   m.reset()
   self.assertIsNone(m.get())
Beispiel #3
0
 def test_serialze_too_many_fields(self):
   t = targets.DeviceTarget('reg', 'net', 'host')
   m = metrics.StringMetric('test', target=t,
                           fields={'a': 1, 'b': 2, 'c': 3, 'd': 4})
   m.set('val', fields={'e': 5, 'f': 6, 'g': 7})
   with self.assertRaises(errors.MonitoringTooManyFieldsError):
     m.set('val', fields={'e': 5, 'f': 6, 'g': 7, 'h': 8})
Beispiel #4
0
 def test_serialize_multiple_values(self):
   t = targets.DeviceTarget('reg', 'net', 'host')
   m = metrics.StringMetric('test', target=t)
   m.set('val1', fields={'foo': 1})
   m.set('val2', fields={'foo': 2})
   p = metrics_pb2.MetricsCollection()
   loop_action = mock.Mock()
   m.serialize_to(p, loop_action=loop_action)
   e = textwrap.dedent('''\
       data {
         name: "test"
         metric_name_prefix: "/chrome/infra/"
         network_device {
           alertable: true
           realm: "ACQ_CHROME"
           metro: "reg"
           hostname: "host"
           hostgroup: "net"
         }
         fields {
           name: "foo"
           type: INT
           int_value: 2
         }
         string_value: "val2"
       }
       data {
         name: "test"
         metric_name_prefix: "/chrome/infra/"
         network_device {
           alertable: true
           realm: "ACQ_CHROME"
           metro: "reg"
           hostname: "host"
           hostgroup: "net"
         }
         fields {
           name: "foo"
           type: INT
           int_value: 1
         }
         string_value: "val1"
       }
   ''')
   self.assertEquals(str(p), e)
   self.assertEquals(2, loop_action.call_count)
Beispiel #5
0
 def test_serialize_default_target(self):
   t = targets.DeviceTarget('reg', 'net', 'host')
   m = metrics.StringMetric('test')
   m.set('val')
   p = metrics_pb2.MetricsCollection()
   m.serialize_to(p, default_target=t)
   e = textwrap.dedent('''\
       data {
         name: "test"
         metric_name_prefix: "/chrome/infra/"
         network_device {
           alertable: true
           realm: "ACQ_CHROME"
           metro: "reg"
           hostname: "host"
           hostgroup: "net"
         }
         string_value: "val"
       }
   ''')
   self.assertEquals(str(p), e)
Beispiel #6
0
 def test_non_string_raises(self):
   m = metrics.StringMetric('test')
   with self.assertRaises(errors.MonitoringInvalidValueTypeError):
     m.set(object())
Beispiel #7
0
 def test_set(self):
   m = metrics.StringMetric('test')
   m.set('hello world')
   self.assertEquals(m.get(), 'hello world')
   self.assertEquals(self.fake_send.call_count, 1)
Beispiel #8
0
 def test_populate_value(self):
   pb = metrics_pb2.MetricsData()
   m = metrics.StringMetric('test')
   m._populate_value(pb, 'foo')
   self.assertEquals(pb.string_value, 'foo')
Beispiel #9
0
 def test_serialize_no_target(self):
   m = metrics.StringMetric('test')
   m.set('val')
   with self.assertRaises(errors.MonitoringNoConfiguredTargetError):
     p = metrics_pb2.MetricsCollection()
     m.serialize_to(p)