Exemple #1
0
func (s *metricsdebugSuiteMock) TestGetMetrics(c *gc.C) {
	var called bool
	now := time.Now()
	apiCaller := basetesting.APICallerFunc(
		func(objType string,
			version int,
			id, request string,
			a, response interface{},
		) error {
			c.Assert(request, gc.Equals, "GetMetrics")
			result := response.(*params.MetricResults)
			result.Results = []params.EntityMetrics{{
				Metrics: []params.MetricResult{{
					Key:   "pings",
					Value: "5",
					Time:  now,
				}},
				Error: nil,
			}}
			called = true
			return nil
		})
	client := metricsdebug.NewClient(apiCaller)
	metrics, err := client.GetMetrics("unit-wordpress/0")
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(called, jc.IsTrue)
	c.Assert(metrics, gc.HasLen, 1)
	c.Assert(metrics[0].Key, gc.Equals, "pings")
	c.Assert(metrics[0].Value, gc.Equals, "5")
	c.Assert(metrics[0].Time, gc.Equals, now)
}
Exemple #2
0
func (s *metricsdebugSuiteMock) TestSetMeterStatusAPIServerError(c *gc.C) {
	var called bool
	apiCaller := basetesting.APICallerFunc(
		func(objType string,
			version int,
			id, request string,
			a, response interface{},
		) error {
			c.Assert(request, gc.Equals, "SetMeterStatus")
			c.Assert(a, gc.DeepEquals, params.MeterStatusParams{
				Statuses: []params.MeterStatusParam{{
					Tag:  "unit-metered/0",
					Code: "RED",
					Info: "test"},
				},
			})
			result := response.(*params.ErrorResults)
			result.Results = []params.ErrorResult{{
				Error: common.ServerError(errors.New("an error")),
			}}
			called = true
			return nil
		})
	client := metricsdebug.NewClient(apiCaller)
	err := client.SetMeterStatus("unit-metered/0", "RED", "test")
	c.Assert(err, gc.ErrorMatches, "an error")
	c.Assert(called, jc.IsTrue)
}
Exemple #3
0
func (s *metricsdebugSuiteMock) TestSetMeterStatusFacadeCallError(c *gc.C) {
	var called bool
	apiCaller := basetesting.APICallerFunc(
		func(objType string,
			version int,
			id, request string,
			a, response interface{},
		) error {
			called = true
			return errors.New("an error")
		})
	client := metricsdebug.NewClient(apiCaller)
	err := client.SetMeterStatus("unit-metered/0", "RED", "test")
	c.Assert(err, gc.ErrorMatches, "an error")
	c.Assert(called, jc.IsTrue)
}
Exemple #4
0
func (s *metricsdebugSuiteMock) TestGetMetricsFacadeCallError(c *gc.C) {
	var called bool
	apiCaller := basetesting.APICallerFunc(
		func(objType string,
			version int,
			id, request string,
			a, result interface{},
		) error {
			called = true
			return errors.New("an error")
		})
	client := metricsdebug.NewClient(apiCaller)
	metrics, err := client.GetMetrics("unit-wordpress/0")
	c.Assert(err, gc.ErrorMatches, "an error")
	c.Assert(metrics, gc.IsNil)
	c.Assert(called, jc.IsTrue)
}
Exemple #5
0
func (s *metricsdebugSuiteMock) TestGetMetricsForModelFails(c *gc.C) {
	var called bool
	apiCaller := basetesting.APICallerFunc(
		func(objType string,
			version int,
			id, request string,
			requestParam, response interface{},
		) error {
			called = true
			return errors.New("an error")
		})
	client := metricsdebug.NewClient(apiCaller)
	metrics, err := client.GetMetrics()
	c.Assert(called, jc.IsTrue)
	c.Assert(metrics, gc.IsNil)
	c.Assert(err, gc.ErrorMatches, "an error")
}
Exemple #6
0
func (s *metricsdebugSuiteMock) TestGetMetricsFails(c *gc.C) {
	var called bool
	apiCaller := basetesting.APICallerFunc(
		func(objType string,
			version int,
			id, request string,
			a, response interface{},
		) error {
			c.Assert(request, gc.Equals, "GetMetrics")
			result := response.(*params.MetricResults)
			result.Results = []params.EntityMetrics{{
				Error: common.ServerError(errors.New("an error")),
			}}
			called = true
			return nil
		})
	client := metricsdebug.NewClient(apiCaller)
	metrics, err := client.GetMetrics("unit-wordpress/0")
	c.Assert(err, gc.ErrorMatches, "an error")
	c.Assert(metrics, gc.IsNil)
	c.Assert(called, jc.IsTrue)
}
Exemple #7
0
	c.ModelCommandBase.SetFlags(f)
	f.StringVar(&c.StatusInfo, "info", "", "Set the meter status info to this string")
}

// SetMeterStatusClient defines the juju api required by the command.
type SetMeterStatusClient interface {
	SetMeterStatus(tag, status, info string) error
	Close() error
}

var newClient = func(env modelcmd.ModelCommandBase) (SetMeterStatusClient, error) {
	state, err := env.NewAPIRoot()
	if err != nil {
		return nil, errors.Trace(err)
	}
	return metricsdebug.NewClient(state), nil
}

// Run implements Command.Run.
func (c *SetMeterStatusCommand) Run(ctx *cmd.Context) error {
	client, err := newClient(c.ModelCommandBase)
	if err != nil {
		return errors.Trace(err)
	}
	defer client.Close()
	err = client.SetMeterStatus(c.Tag.String(), c.Status, c.StatusInfo)
	if err != nil {
		return errors.Trace(err)
	}
	return nil
}
Exemple #8
0
func (s *metricsdebugSuite) SetUpTest(c *gc.C) {
	s.JujuConnSuite.SetUpTest(c)
	s.manager = metricsdebug.NewClient(s.APIState)
	c.Assert(s.manager, gc.NotNil)
}