func (s *metricsDebugSuite) TestGetMultipleMetricsNoMocksWithService(c *gc.C) { meteredCharm := s.Factory.MakeCharm(c, &factory.CharmParams{Name: "metered", URL: "local:quantal/metered"}) meteredService := s.Factory.MakeService(c, &factory.ServiceParams{ Charm: meteredCharm, }) unit0 := s.Factory.MakeUnit(c, &factory.UnitParams{Service: meteredService, SetCharmURL: true}) unit1 := s.Factory.MakeUnit(c, &factory.UnitParams{Service: meteredService, SetCharmURL: true}) metricUnit0 := s.Factory.MakeMetric(c, &factory.MetricParams{ Unit: unit0, }) metricUnit1 := s.Factory.MakeMetric(c, &factory.MetricParams{ Unit: unit1, }) args := params.Entities{Entities: []params.Entity{ {"service-metered"}, }} metrics, err := s.metricsdebug.GetMetrics(args) c.Assert(err, jc.ErrorIsNil) c.Assert(metrics.Results, gc.HasLen, 1) c.Assert(metrics.Results[0].Metrics, gc.HasLen, 2) c.Assert(metrics.Results[0].Metrics[0].Key, gc.Equals, metricUnit0.Metrics()[0].Key) c.Assert(metrics.Results[0].Metrics[0].Value, gc.Equals, metricUnit0.Metrics()[0].Value) c.Assert(metrics.Results[0].Metrics[0].Time, jc.TimeBetween(metricUnit0.Metrics()[0].Time, metricUnit0.Metrics()[0].Time)) c.Assert(metrics.Results[0].Metrics[1].Key, gc.Equals, metricUnit1.Metrics()[0].Key) c.Assert(metrics.Results[0].Metrics[1].Value, gc.Equals, metricUnit1.Metrics()[0].Value) c.Assert(metrics.Results[0].Metrics[1].Time, jc.TimeBetween(metricUnit1.Metrics()[0].Time, metricUnit1.Metrics()[0].Time)) }
func (s *metricsDebugSuite) TestGetModelNoMocks(c *gc.C) { meteredCharm := s.Factory.MakeCharm(c, &factory.CharmParams{Name: "metered", URL: "local:quantal/metered"}) meteredService := s.Factory.MakeApplication(c, &factory.ApplicationParams{ Charm: meteredCharm, }) unit0 := s.Factory.MakeUnit(c, &factory.UnitParams{Application: meteredService, SetCharmURL: true}) unit1 := s.Factory.MakeUnit(c, &factory.UnitParams{Application: meteredService, SetCharmURL: true}) metricUnit0 := s.Factory.MakeMetric(c, &factory.MetricParams{ Unit: unit0, }) metricUnit1 := s.Factory.MakeMetric(c, &factory.MetricParams{ Unit: unit1, }) args := params.Entities{Entities: []params.Entity{}} metrics, err := s.metricsdebug.GetMetrics(args) c.Assert(err, jc.ErrorIsNil) c.Assert(metrics.Results, gc.HasLen, 1) metric0 := metrics.Results[0].Metrics[0] metric1 := metrics.Results[0].Metrics[1] expected0 := metricUnit0.Metrics()[0] expected1 := metricUnit1.Metrics()[0] c.Assert(metric0.Key, gc.Equals, expected0.Key) c.Assert(metric0.Value, gc.Equals, expected0.Value) c.Assert(metric0.Time, jc.TimeBetween(expected0.Time, expected0.Time)) c.Assert(metric0.Unit, gc.Equals, metricUnit0.Unit()) c.Assert(metric1.Key, gc.Equals, expected1.Key) c.Assert(metric1.Value, gc.Equals, expected1.Value) c.Assert(metric1.Time, jc.TimeBetween(expected1.Time, expected1.Time)) c.Assert(metric1.Unit, gc.Equals, metricUnit1.Unit()) }
func (s *CheckerSuite) TestTimeBetween(c *gc.C) { now := time.Now() earlier := now.Add(-1 * time.Second) later := now.Add(time.Second) checkOK := func(value interface{}, start, end time.Time) { checker := jc.TimeBetween(start, end) value, msg := checker.Check([]interface{}{value}, nil) c.Check(value, jc.IsTrue) c.Check(msg, gc.Equals, "") } checkFails := func(value interface{}, start, end time.Time, match string) { checker := jc.TimeBetween(start, end) value, msg := checker.Check([]interface{}{value}, nil) c.Check(value, jc.IsFalse) c.Check(msg, gc.Matches, match) } checkOK(now, earlier, later) // Later can be before earlier... checkOK(now, later, earlier) // check at bounds checkOK(earlier, earlier, later) checkOK(later, earlier, later) checkFails(earlier, now, later, `obtained time .* is before start time .*`) checkFails(later, now, earlier, `obtained time .* is after end time .*`) checkFails(42, now, earlier, `obtained value type must be time.Time`) }
func (s *metricsDebugSuite) TestGetMultipleMetricsNoMocks(c *gc.C) { meteredCharm := s.Factory.MakeCharm(c, &factory.CharmParams{Name: "metered", URL: "local:quantal/metered"}) meteredService := s.Factory.MakeApplication(c, &factory.ApplicationParams{ Charm: meteredCharm, }) unit0 := s.Factory.MakeUnit(c, &factory.UnitParams{Application: meteredService, SetCharmURL: true}) unit1 := s.Factory.MakeUnit(c, &factory.UnitParams{Application: meteredService, SetCharmURL: true}) metricUnit0 := s.Factory.MakeMetric(c, &factory.MetricParams{ Unit: unit0, }) metricUnit1 := s.Factory.MakeMetric(c, &factory.MetricParams{ Unit: unit1, }) args0 := params.Entities{Entities: []params.Entity{ {"unit-metered/0"}, }} args1 := params.Entities{Entities: []params.Entity{ {"unit-metered/1"}, }} metrics0, err := s.metricsdebug.GetMetrics(args0) c.Assert(err, jc.ErrorIsNil) c.Assert(metrics0.Results, gc.HasLen, 1) c.Assert(metrics0.Results[0].Metrics[0].Key, gc.Equals, metricUnit0.Metrics()[0].Key) c.Assert(metrics0.Results[0].Metrics[0].Value, gc.Equals, metricUnit0.Metrics()[0].Value) c.Assert(metrics0.Results[0].Metrics[0].Time, jc.TimeBetween(metricUnit0.Metrics()[0].Time, metricUnit0.Metrics()[0].Time)) metrics1, err := s.metricsdebug.GetMetrics(args1) c.Assert(err, jc.ErrorIsNil) c.Assert(metrics1.Results, gc.HasLen, 1) c.Assert(metrics1.Results[0].Metrics[0].Key, gc.Equals, metricUnit1.Metrics()[0].Key) c.Assert(metrics1.Results[0].Metrics[0].Value, gc.Equals, metricUnit1.Metrics()[0].Value) c.Assert(metrics1.Results[0].Metrics[0].Time, jc.TimeBetween(metricUnit1.Metrics()[0].Time, metricUnit1.Metrics()[0].Time)) }
func (s *ActionSuite) TestAddAction(c *gc.C) { for i, t := range []struct { should string name string params map[string]interface{} whichUnit *state.Unit expectedErr string }{{ should: "enqueue normally", name: "snapshot", whichUnit: s.unit, //params: map[string]interface{}{"outfile": "outfile.tar.bz2"}, }, { should: "fail on actionless charms", name: "something", whichUnit: s.actionlessUnit, expectedErr: "no actions defined on charm \"local:quantal/quantal-actionless-1\"", }, { should: "fail on action not defined in schema", whichUnit: s.unit, name: "something-nonexistent", expectedErr: "action \"something-nonexistent\" not defined on unit \"dummy/0\"", }, { should: "invalidate with bad params", whichUnit: s.unit, name: "snapshot", params: map[string]interface{}{ "outfile": 5.0, }, expectedErr: "validation failed: \\(root\\)\\.outfile : must be of type string, given 5", }} { c.Logf("Test %d: should %s", i, t.should) before := state.NowToTheSecond() later := before.Add(testing.LongWait) // Copy params over into empty premade map for comparison later params := make(map[string]interface{}) for k, v := range t.params { params[k] = v } // Verify we can add an Action a, err := t.whichUnit.AddAction(t.name, params) if t.expectedErr == "" { c.Assert(err, jc.ErrorIsNil) curl, _ := t.whichUnit.CharmURL() ch, _ := s.State.Charm(curl) schema := ch.Actions() c.Logf("Schema for unit %q:\n%#v", t.whichUnit.Name(), schema) // verify we can get it back out by Id action, err := s.State.Action(a.Id()) c.Assert(err, jc.ErrorIsNil) c.Assert(action, gc.NotNil) c.Check(action.Id(), gc.Equals, a.Id()) // verify we get out what we put in c.Check(action.Name(), gc.Equals, t.name) c.Check(action.Parameters(), jc.DeepEquals, params) // Enqueued time should be within a reasonable time of the beginning // of the test now := state.NowToTheSecond() c.Check(action.Enqueued(), jc.TimeBetween(before, now)) c.Check(action.Enqueued(), jc.TimeBetween(before, later)) continue } c.Check(err, gc.ErrorMatches, t.expectedErr) } }
func assertSameMetric(c *gc.C, a params.MetricResult, b *state.MetricBatch) { c.Assert(a.Key, gc.Equals, b.Metrics()[0].Key) c.Assert(a.Value, gc.Equals, b.Metrics()[0].Value) c.Assert(a.Time, jc.TimeBetween(b.Metrics()[0].Time, b.Metrics()[0].Time)) }