func (s *MetricSuite) TestAddMetric(c *gc.C) { unit := s.assertAddUnit(c) now := state.NowToTheSecond() m := state.NewMetric("item", "5", now, []byte("creds")) metricBatch, err := unit.AddMetrics([]*state.Metric{m}) c.Assert(err, gc.IsNil) c.Assert(metricBatch.Unit(), gc.Equals, "wordpress/0") c.Assert(metricBatch.CharmURL(), gc.Equals, "local:quantal/quantal-wordpress-3") c.Assert(metricBatch.Sent(), gc.Equals, false) c.Assert(metricBatch.Metrics(), gc.HasLen, 1) metric := metricBatch.Metrics()[0] c.Assert(metric.Key(), gc.Equals, "item") c.Assert(metric.Value(), gc.Equals, "5") c.Assert(metric.Time().Equal(now), jc.IsTrue) c.Assert(metric.Credentials(), gc.DeepEquals, []byte("creds")) saved, err := s.State.MetricBatch(metricBatch.UUID()) c.Assert(err, gc.IsNil) c.Assert(saved.Unit(), gc.Equals, "wordpress/0") c.Assert(metricBatch.CharmURL(), gc.Equals, "local:quantal/quantal-wordpress-3") c.Assert(saved.Sent(), gc.Equals, false) c.Assert(saved.Metrics(), gc.HasLen, 1) metric = saved.Metrics()[0] c.Assert(metric.Key(), gc.Equals, "item") c.Assert(metric.Value(), gc.Equals, "5") c.Assert(metric.Time().Equal(now), jc.IsTrue) c.Assert(metric.Credentials(), gc.DeepEquals, []byte("creds")) }
func (s *factorySuite) TestMakeMetric(c *gc.C) { now := time.Now().Round(time.Second).UTC() unit := s.Factory.MakeUnit(c, nil) metric := s.Factory.MakeMetric(c, &factory.MetricParams{ Unit: unit, Time: &now, Sent: true, Metrics: []*state.Metric{state.NewMetric("itemA", "foo", now, []byte("somecreds"))}, }) c.Assert(metric, gc.NotNil) saved, err := s.State.MetricBatch(metric.UUID()) c.Assert(err, gc.IsNil) c.Assert(saved.UUID(), gc.Equals, metric.UUID()) c.Assert(saved.Unit(), gc.Equals, metric.Unit()) c.Assert(saved.CharmURL(), gc.Equals, metric.CharmURL()) c.Assert(metric.Sent(), jc.IsTrue) c.Assert(saved.Sent(), jc.IsTrue) c.Assert(saved.Metrics(), gc.HasLen, 1) c.Assert(saved.Metrics()[0].Key(), gc.Equals, "itemA") c.Assert(saved.Metrics()[0].Value(), gc.Equals, "foo") c.Assert(saved.Metrics()[0].Time().Equal(now), jc.IsTrue) c.Assert(saved.Metrics()[0].Credentials(), gc.DeepEquals, []byte("somecreds")) }
func (s *MetricSuite) TestAddMetricDeadUnit(c *gc.C) { unit := s.assertAddUnit(c) assertUnitDead(c, unit) now := state.NowToTheSecond() m := state.NewMetric("item", "5", now, []byte{}) _, err := unit.AddMetrics([]*state.Metric{m}) c.Assert(err, gc.ErrorMatches, `wordpress/0 not found`) }
func (s *MetricSuite) TestSetMetricSent(c *gc.C) { unit := s.assertAddUnit(c) now := state.NowToTheSecond() m := state.NewMetric("item", "5", now, []byte{}) added, err := unit.AddMetrics([]*state.Metric{m}) c.Assert(err, gc.IsNil) saved, err := s.State.MetricBatch(added.UUID()) c.Assert(err, gc.IsNil) err = saved.SetSent() c.Assert(err, gc.IsNil) c.Assert(saved.Sent(), jc.IsTrue) saved, err = s.State.MetricBatch(added.UUID()) c.Assert(err, gc.IsNil) c.Assert(saved.Sent(), jc.IsTrue) }
// MakeMetric makes a metric with specified params, filling in // sane defaults for missing values. // If params is not specified, defaults are used. If more than one // params stuct is passed to the function, it panics. func (factory *Factory) MakeMetric(c *gc.C, params *MetricParams) *state.MetricBatch { now := time.Now().Round(time.Second).UTC() if params == nil { params = &MetricParams{} } if params.Unit == nil { params.Unit = factory.MakeUnit(c, nil) } if params.Time == nil { params.Time = &now } if params.Metrics == nil { params.Metrics = []*state.Metric{state.NewMetric(factory.UniqueString("metric"), factory.UniqueString(""), now, []byte("creds"))} } metric, err := params.Unit.AddMetrics(params.Metrics) c.Assert(err, gc.IsNil) if params.Sent { err := metric.SetSent() c.Assert(err, gc.IsNil) } return metric }