github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/metricsdebug/metricsdebug_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package metricsdebug_test
     5  
     6  import (
     7  	"bytes"
     8  	"fmt"
     9  	stdtesting "testing"
    10  	"text/tabwriter"
    11  	"time"
    12  
    13  	"github.com/juju/cmd/cmdtesting"
    14  	"github.com/juju/testing"
    15  	jc "github.com/juju/testing/checkers"
    16  	gc "gopkg.in/check.v1"
    17  
    18  	"github.com/juju/juju/apiserver/params"
    19  	"github.com/juju/juju/cmd/juju/metricsdebug"
    20  	"github.com/juju/juju/cmd/modelcmd"
    21  	jujutesting "github.com/juju/juju/juju/testing"
    22  	"github.com/juju/juju/state"
    23  	coretesting "github.com/juju/juju/testing"
    24  	"github.com/juju/juju/testing/factory"
    25  )
    26  
    27  func TestPackage(t *stdtesting.T) {
    28  	coretesting.MgoTestPackage(t)
    29  }
    30  
    31  type MockGetMetricsClient struct {
    32  	testing.Stub
    33  }
    34  
    35  func (m *MockGetMetricsClient) GetMetrics(tag string) ([]params.MetricResult, error) {
    36  	m.Stub.MethodCall(m, "GetMetrics", tag)
    37  	return nil, nil
    38  }
    39  func (m *MockGetMetricsClient) Close() error {
    40  	m.Stub.MethodCall(m, "Close")
    41  	return nil
    42  }
    43  
    44  type DebugMetricsMockSuite struct {
    45  	coretesting.FakeJujuXDGDataHomeSuite
    46  }
    47  
    48  var _ = gc.Suite(&DebugMetricsMockSuite{})
    49  
    50  func (s *DebugMetricsMockSuite) TestUnit(c *gc.C) {
    51  	client := MockGetMetricsClient{testing.Stub{}}
    52  	s.PatchValue(metricsdebug.NewClient, func(_ modelcmd.ModelCommandBase) (metricsdebug.GetMetricsClient, error) {
    53  		return &client, nil
    54  	})
    55  	_, err := coretesting.RunCommand(c, metricsdebug.New(), "metered/0")
    56  	c.Assert(err, jc.ErrorIsNil)
    57  	client.CheckCall(c, 0, "GetMetrics", "unit-metered-0")
    58  }
    59  
    60  func (s *DebugMetricsMockSuite) TestService(c *gc.C) {
    61  	client := MockGetMetricsClient{testing.Stub{}}
    62  	s.PatchValue(metricsdebug.NewClient, func(_ modelcmd.ModelCommandBase) (metricsdebug.GetMetricsClient, error) {
    63  		return &client, nil
    64  	})
    65  	_, err := coretesting.RunCommand(c, metricsdebug.New(), "metered")
    66  	c.Assert(err, jc.ErrorIsNil)
    67  	client.CheckCall(c, 0, "GetMetrics", "service-metered")
    68  }
    69  
    70  func (s *DebugMetricsMockSuite) TestNotValidServiceOrUnit(c *gc.C) {
    71  	client := MockGetMetricsClient{testing.Stub{}}
    72  	s.PatchValue(metricsdebug.NewClient, func(_ modelcmd.ModelCommandBase) (metricsdebug.GetMetricsClient, error) {
    73  		return &client, nil
    74  	})
    75  	_, err := coretesting.RunCommand(c, metricsdebug.New(), "!!!!!!")
    76  	c.Assert(err, gc.ErrorMatches, `"!!!!!!" is not a valid unit or service`)
    77  }
    78  
    79  type DebugMetricsCommandSuite struct {
    80  	jujutesting.JujuConnSuite
    81  }
    82  
    83  var _ = gc.Suite(&DebugMetricsCommandSuite{})
    84  
    85  func (s *DebugMetricsCommandSuite) TestDebugNoArgs(c *gc.C) {
    86  	_, err := coretesting.RunCommand(c, metricsdebug.New())
    87  	c.Assert(err, gc.ErrorMatches, "you need to specify a unit or service.")
    88  }
    89  
    90  func (s *DebugMetricsCommandSuite) TestUnits(c *gc.C) {
    91  	meteredCharm := s.Factory.MakeCharm(c, &factory.CharmParams{Name: "metered", URL: "local:quantal/metered"})
    92  	meteredService := s.Factory.MakeService(c, &factory.ServiceParams{Charm: meteredCharm})
    93  	unit := s.Factory.MakeUnit(c, &factory.UnitParams{Service: meteredService, SetCharmURL: true})
    94  	unit2 := s.Factory.MakeUnit(c, &factory.UnitParams{Service: meteredService, SetCharmURL: true})
    95  	newTime := time.Now().Round(time.Second)
    96  	metricA := state.Metric{"pings", "5", newTime}
    97  	metricB := state.Metric{"pings", "10.5", newTime}
    98  	s.Factory.MakeMetric(c, &factory.MetricParams{Unit: unit, Metrics: []state.Metric{metricA}})
    99  	s.Factory.MakeMetric(c, &factory.MetricParams{Unit: unit2, Metrics: []state.Metric{metricA, metricB}})
   100  	outputTime := newTime.Format(time.RFC3339)
   101  	expectedOutput := bytes.Buffer{}
   102  	tw := tabwriter.NewWriter(&expectedOutput, 0, 1, 1, ' ', 0)
   103  	fmt.Fprintf(tw, "TIME\tMETRIC\tVALUE\n")
   104  	fmt.Fprintf(tw, "%v\tpings\t5\n", outputTime)
   105  	fmt.Fprintf(tw, "%v\tpings\t10.5\n", outputTime)
   106  	tw.Flush()
   107  	ctx, err := coretesting.RunCommand(c, metricsdebug.New(), "metered/1")
   108  	c.Assert(err, jc.ErrorIsNil)
   109  	c.Assert(cmdtesting.Stdout(ctx), gc.Equals, expectedOutput.String())
   110  }
   111  
   112  func (s *DebugMetricsCommandSuite) TestServiceWithNoption(c *gc.C) {
   113  	meteredCharm := s.Factory.MakeCharm(c, &factory.CharmParams{Name: "metered", URL: "local:quantal/metered"})
   114  	meteredService := s.Factory.MakeService(c, &factory.ServiceParams{Charm: meteredCharm})
   115  	unit := s.Factory.MakeUnit(c, &factory.UnitParams{Service: meteredService, SetCharmURL: true})
   116  	unit2 := s.Factory.MakeUnit(c, &factory.UnitParams{Service: meteredService, SetCharmURL: true})
   117  	newTime := time.Now().Round(time.Second)
   118  	metricA := state.Metric{"pings", "5", newTime}
   119  	metricB := state.Metric{"pings", "10.5", newTime}
   120  	s.Factory.MakeMetric(c, &factory.MetricParams{Unit: unit, Metrics: []state.Metric{metricA}})
   121  	s.Factory.MakeMetric(c, &factory.MetricParams{Unit: unit2, Metrics: []state.Metric{metricA, metricB}})
   122  	outputTime := newTime.Format(time.RFC3339)
   123  	expectedOutput := bytes.Buffer{}
   124  	tw := tabwriter.NewWriter(&expectedOutput, 0, 1, 1, ' ', 0)
   125  	fmt.Fprintf(tw, "TIME\tMETRIC\tVALUE\n")
   126  	fmt.Fprintf(tw, "%v\tpings\t5\n", outputTime)
   127  	fmt.Fprintf(tw, "%v\tpings\t5\n", outputTime)
   128  	tw.Flush()
   129  	ctx, err := coretesting.RunCommand(c, metricsdebug.New(), "metered", "-n", "2")
   130  	c.Assert(err, jc.ErrorIsNil)
   131  	c.Assert(cmdtesting.Stdout(ctx), gc.Equals, expectedOutput.String())
   132  }
   133  
   134  func (s *DebugMetricsCommandSuite) TestServiceWithNoptionGreaterThanMetricCount(c *gc.C) {
   135  	meteredCharm := s.Factory.MakeCharm(c, &factory.CharmParams{Name: "metered", URL: "local:quantal/metered"})
   136  	meteredService := s.Factory.MakeService(c, &factory.ServiceParams{Charm: meteredCharm})
   137  	unit := s.Factory.MakeUnit(c, &factory.UnitParams{Service: meteredService, SetCharmURL: true})
   138  	unit2 := s.Factory.MakeUnit(c, &factory.UnitParams{Service: meteredService, SetCharmURL: true})
   139  	newTime := time.Now().Round(time.Second)
   140  	metricA := state.Metric{"pings", "5", newTime}
   141  	metricB := state.Metric{"pings", "10.5", newTime}
   142  	s.Factory.MakeMetric(c, &factory.MetricParams{Unit: unit, Metrics: []state.Metric{metricA}})
   143  	s.Factory.MakeMetric(c, &factory.MetricParams{Unit: unit2, Metrics: []state.Metric{metricA, metricB}})
   144  	outputTime := newTime.Format(time.RFC3339)
   145  	expectedOutput := bytes.Buffer{}
   146  	tw := tabwriter.NewWriter(&expectedOutput, 0, 1, 1, ' ', 0)
   147  	fmt.Fprintf(tw, "TIME\tMETRIC\tVALUE\n")
   148  	fmt.Fprintf(tw, "%v\tpings\t5\n", outputTime)
   149  	fmt.Fprintf(tw, "%v\tpings\t5\n", outputTime)
   150  	fmt.Fprintf(tw, "%v\tpings\t10.5\n", outputTime)
   151  	tw.Flush()
   152  	ctx, err := coretesting.RunCommand(c, metricsdebug.New(), "metered", "-n", "42")
   153  	c.Assert(err, jc.ErrorIsNil)
   154  	c.Assert(cmdtesting.Stdout(ctx), gc.Equals, expectedOutput.String())
   155  }
   156  
   157  func (s *DebugMetricsCommandSuite) TestNoMetrics(c *gc.C) {
   158  	meteredCharm := s.Factory.MakeCharm(c, &factory.CharmParams{Name: "metered", URL: "local:quantal/metered"})
   159  	meteredService := s.Factory.MakeService(c, &factory.ServiceParams{Charm: meteredCharm})
   160  	s.Factory.MakeUnit(c, &factory.UnitParams{Service: meteredService, SetCharmURL: true})
   161  	ctx, err := coretesting.RunCommand(c, metricsdebug.New(), "metered", "-n", "2")
   162  	c.Assert(err, jc.ErrorIsNil)
   163  	c.Assert(cmdtesting.Stdout(ctx), gc.Equals, "")
   164  }
   165  
   166  func (s *DebugMetricsCommandSuite) TestUnitJsonOutput(c *gc.C) {
   167  	meteredCharm := s.Factory.MakeCharm(c, &factory.CharmParams{Name: "metered", URL: "local:quantal/metered"})
   168  	meteredService := s.Factory.MakeService(c, &factory.ServiceParams{Charm: meteredCharm})
   169  	unit := s.Factory.MakeUnit(c, &factory.UnitParams{Service: meteredService, SetCharmURL: true})
   170  	newTime := time.Now().Round(time.Second)
   171  	metricA := state.Metric{"pings", "5", newTime}
   172  	metricB := state.Metric{"pings", "10.5", newTime}
   173  	s.Factory.MakeMetric(c, &factory.MetricParams{Unit: unit, Metrics: []state.Metric{metricA}})
   174  	s.Factory.MakeMetric(c, &factory.MetricParams{Unit: unit, Metrics: []state.Metric{metricA, metricB}})
   175  	outputTime := newTime.Format(time.RFC3339)
   176  	expectedOutput := fmt.Sprintf(`[
   177      {
   178          "time": "%v",
   179          "key": "pings",
   180          "value": "5"
   181      },
   182      {
   183          "time": "%v",
   184          "key": "pings",
   185          "value": "5"
   186      },
   187      {
   188          "time": "%v",
   189          "key": "pings",
   190          "value": "10.5"
   191      }
   192  ]`, outputTime, outputTime, outputTime)
   193  	ctx, err := coretesting.RunCommand(c, metricsdebug.New(), "metered/0", "--json")
   194  	c.Assert(err, jc.ErrorIsNil)
   195  	c.Assert(cmdtesting.Stdout(ctx), gc.Equals, expectedOutput)
   196  }