func (s *MetricsReaderSuite) TestUnblockedReaders(c *gc.C) { r, err := metrics.NewJSONMetricReader(s.paths.GetMetricsSpoolDir()) c.Assert(err, jc.ErrorIsNil) err = r.Close() c.Assert(err, jc.ErrorIsNil) r2, err := metrics.NewJSONMetricReader(s.paths.GetMetricsSpoolDir()) c.Assert(err, jc.ErrorIsNil) c.Assert(r2, gc.NotNil) err = r2.Close() c.Assert(err, jc.ErrorIsNil) }
func (s *MetricsOperationSuite) TestSendingFails(c *gc.C) { apiSender := newTestAPIMetricSender() factory := operation.NewFactory(operation.FactoryParams{ MetricSender: apiSender, MetricSpoolDir: s.spoolDir, }) sendOperation, err := factory.NewSendMetrics() c.Assert(err, gc.IsNil) _, err = sendOperation.Prepare(operation.State{}) c.Assert(err, jc.ErrorIsNil) select { case apiSender.sendError <- errors.New("something went wrong"): default: c.Fatalf("blocked error channel") } _, err = sendOperation.Execute(operation.State{}) c.Assert(err, jc.ErrorIsNil) c.Assert(apiSender.batches, gc.HasLen, 1) reader, err := metrics.NewJSONMetricReader(s.spoolDir) c.Assert(err, gc.IsNil) batches, err := reader.Read() c.Assert(err, gc.IsNil) c.Assert(batches, gc.HasLen, 1) }
func (s *MetricsOperationSuite) TestSendingGetDuplicate(c *gc.C) { apiSender := newTestAPIMetricSender() factory := operation.NewFactory(operation.FactoryParams{ MetricSender: apiSender, MetricSpoolDir: s.spoolDir, }) sendOperation, err := factory.NewSendMetrics() c.Assert(err, gc.IsNil) _, err = sendOperation.Prepare(operation.State{}) c.Assert(err, jc.ErrorIsNil) apiErr := ¶ms.Error{Message: "already exists", Code: params.CodeAlreadyExists} select { case apiSender.errors <- apiErr: default: c.Fatalf("blocked error channel") } _, err = sendOperation.Execute(operation.State{}) c.Assert(err, jc.ErrorIsNil) c.Assert(apiSender.batches, gc.HasLen, 1) reader, err := metrics.NewJSONMetricReader(s.spoolDir) c.Assert(err, gc.IsNil) batches, err := reader.Read() c.Assert(err, gc.IsNil) c.Assert(batches, gc.HasLen, 0) }
func (s *MetricsOperationSuite) TestMetricSendingSuccess(c *gc.C) { apiSender := newTestAPIMetricSender() factory := operation.NewFactory(operation.FactoryParams{ MetricSender: apiSender, MetricSpoolDir: s.spoolDir, }) sendOperation, err := factory.NewSendMetrics() c.Assert(err, gc.IsNil) _, err = sendOperation.Prepare(operation.State{}) c.Assert(err, jc.ErrorIsNil) _, err = sendOperation.Execute(operation.State{}) c.Assert(err, jc.ErrorIsNil) c.Assert(apiSender.batches, gc.HasLen, 1) reader, err := metrics.NewJSONMetricReader(s.spoolDir) c.Assert(err, gc.IsNil) batches, err := reader.Read() c.Assert(err, gc.IsNil) c.Assert(batches, gc.HasLen, 0) }
func (s *FlushContextSuite) TestBuiltinMetricNotGeneratedIfNotDefined(c *gc.C) { uuid := utils.MustNewUUID() paths := NewRealPaths(c) ctx := s.getMeteredHookContext(c, uuid.String(), -1, "", noProxies, true, s.metricsDefinition("pings"), paths) reader, err := metrics.NewJSONMetricReader( paths.GetMetricsSpoolDir(), ) err = ctx.Flush("some badge", nil) c.Assert(err, jc.ErrorIsNil) batches, err := reader.Read() c.Assert(err, jc.ErrorIsNil) c.Assert(batches, gc.HasLen, 0) }
func (s *MetricsRecorderSuite) TestUnknownMetricKey(c *gc.C) { w, err := metrics.NewJSONMetricRecorder(s.paths.GetMetricsSpoolDir(), map[string]corecharm.Metric{}, "local:precise/wordpress") c.Assert(err, jc.ErrorIsNil) c.Assert(w, gc.NotNil) err = w.AddMetric("pings", "5", time.Now()) c.Assert(err, gc.ErrorMatches, `metric key "pings" not declared by the charm`) err = w.Close() c.Assert(err, jc.ErrorIsNil) r, err := metrics.NewJSONMetricReader(s.paths.GetMetricsSpoolDir()) c.Assert(err, jc.ErrorIsNil) batches, err := r.Read() c.Assert(err, jc.ErrorIsNil) c.Assert(batches, gc.HasLen, 0) }
func (s *FlushContextSuite) TestBuiltinMetric(c *gc.C) { uuid := utils.MustNewUUID() paths := NewRealPaths(c) ctx := s.getMeteredHookContext(c, uuid.String(), -1, "", noProxies, true, s.metricsDefinition("juju-units"), paths) reader, err := metrics.NewJSONMetricReader( paths.GetMetricsSpoolDir(), ) err = ctx.Flush("some badge", nil) c.Assert(err, jc.ErrorIsNil) batches, err := reader.Read() c.Assert(err, jc.ErrorIsNil) c.Assert(batches, gc.HasLen, 1) c.Assert(batches[0].Metrics, gc.HasLen, 1) c.Assert(batches[0].Metrics[0].Key, gc.Equals, "juju-units") c.Assert(batches[0].Metrics[0].Value, gc.Equals, "1") }
func (s *MetricsReaderSuite) TestRemoval(c *gc.C) { r, err := metrics.NewJSONMetricReader(s.paths.GetMetricsSpoolDir()) c.Assert(err, jc.ErrorIsNil) batches, err := r.Read() c.Assert(err, jc.ErrorIsNil) for _, batch := range batches { err := r.Remove(batch.UUID) c.Assert(err, jc.ErrorIsNil) } err = r.Close() c.Assert(err, jc.ErrorIsNil) batches, err = r.Read() c.Assert(err, jc.ErrorIsNil) c.Assert(batches, gc.HasLen, 0) err = r.Close() c.Assert(err, jc.ErrorIsNil) }
func (s *MetricsRecorderSuite) TestInit(c *gc.C) { w, err := metrics.NewJSONMetricRecorder(s.paths.GetMetricsSpoolDir(), map[string]corecharm.Metric{"pings": corecharm.Metric{}}, "local:precise/wordpress") c.Assert(err, jc.ErrorIsNil) c.Assert(w, gc.NotNil) err = w.AddMetric("pings", "5", time.Now()) c.Assert(err, jc.ErrorIsNil) err = w.Close() c.Assert(err, jc.ErrorIsNil) r, err := metrics.NewJSONMetricReader(s.paths.GetMetricsSpoolDir()) c.Assert(err, jc.ErrorIsNil) batches, err := r.Read() c.Assert(err, jc.ErrorIsNil) c.Assert(batches, gc.HasLen, 1) batch := batches[0] c.Assert(batch.CharmURL, gc.Equals, "local:precise/wordpress") c.Assert(batch.UUID, gc.Not(gc.Equals), "") c.Assert(batch.Metrics, gc.HasLen, 1) c.Assert(batch.Metrics[0].Key, gc.Equals, "pings") c.Assert(batch.Metrics[0].Value, gc.Equals, "5") err = r.Close() c.Assert(err, jc.ErrorIsNil) }