github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/metricsdebug/metrics_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  	stdtesting "testing"
     8  	"time"
     9  
    10  	"github.com/juju/cmd/cmdtesting"
    11  	"github.com/juju/errors"
    12  	"github.com/juju/testing"
    13  	jc "github.com/juju/testing/checkers"
    14  	gc "gopkg.in/check.v1"
    15  
    16  	"github.com/juju/juju/apiserver/params"
    17  	"github.com/juju/juju/cmd/juju/metricsdebug"
    18  	"github.com/juju/juju/cmd/modelcmd"
    19  	coretesting "github.com/juju/juju/testing"
    20  )
    21  
    22  func TestPackage(t *stdtesting.T) {
    23  	gc.TestingT(t)
    24  }
    25  
    26  type mockGetMetricsClient struct {
    27  	testing.Stub
    28  	metrics []params.MetricResult
    29  }
    30  
    31  func (m *mockGetMetricsClient) GetMetrics(tags ...string) ([]params.MetricResult, error) {
    32  	m.AddCall("GetMetrics", tags)
    33  	return m.metrics, m.NextErr()
    34  }
    35  
    36  func (m *mockGetMetricsClient) Close() error {
    37  	m.AddCall("Close")
    38  	return m.NextErr()
    39  }
    40  
    41  type metricsSuite struct {
    42  	coretesting.FakeJujuXDGDataHomeSuite
    43  
    44  	client *mockGetMetricsClient
    45  }
    46  
    47  var _ = gc.Suite(&metricsSuite{})
    48  
    49  func (s *metricsSuite) SetUpTest(c *gc.C) {
    50  	s.FakeJujuXDGDataHomeSuite.SetUpTest(c)
    51  	s.client = &mockGetMetricsClient{Stub: testing.Stub{}}
    52  	s.PatchValue(metricsdebug.NewClient, func(_ modelcmd.ModelCommandBase) (metricsdebug.GetMetricsClient, error) {
    53  		return s.client, nil
    54  	})
    55  }
    56  
    57  func (s *metricsSuite) TestSort(c *gc.C) {
    58  	s.client.metrics = []params.MetricResult{{
    59  		Unit:  "unit-metered-0",
    60  		Key:   "c-s",
    61  		Value: "5.0",
    62  		Time:  time.Date(2016, 8, 22, 12, 02, 04, 0, time.UTC),
    63  	}, {
    64  		Unit:  "unit-metered-0",
    65  		Key:   "b-s",
    66  		Value: "10.0",
    67  		Time:  time.Date(2016, 8, 22, 12, 02, 04, 0, time.UTC),
    68  	}, {
    69  		Unit:  "unit-metered-0",
    70  		Key:   "a-s",
    71  		Value: "15.0",
    72  		Time:  time.Date(2016, 8, 22, 12, 02, 04, 0, time.UTC),
    73  	}, {
    74  		Unit:   "unit-metered-0",
    75  		Key:    "a-s",
    76  		Value:  "5.0",
    77  		Labels: map[string]string{"quux": "baz"},
    78  		Time:   time.Date(2016, 8, 22, 12, 02, 04, 0, time.UTC),
    79  	}, {
    80  		Unit:   "unit-metered-0",
    81  		Key:    "a-s",
    82  		Value:  "10.0",
    83  		Labels: map[string]string{"foo": "bar"},
    84  		Time:   time.Date(2016, 8, 22, 12, 02, 04, 0, time.UTC),
    85  	}}
    86  	ctx, err := cmdtesting.RunCommand(c, metricsdebug.NewMetricsCommandForTest(), "metered/0")
    87  	c.Assert(err, jc.ErrorIsNil)
    88  	s.client.CheckCall(c, 0, "GetMetrics", []string{"unit-metered-0"})
    89  	c.Assert(cmdtesting.Stdout(ctx), gc.Equals, `UNIT          	           TIMESTAMP	METRIC	VALUE	  LABELS
    90  unit-metered-0	2016-08-22T12:02:04Z	   a-s	 15.0	        
    91  unit-metered-0	2016-08-22T12:02:04Z	   a-s	 10.0	 foo=bar
    92  unit-metered-0	2016-08-22T12:02:04Z	   a-s	  5.0	quux=baz
    93  unit-metered-0	2016-08-22T12:02:04Z	   b-s	 10.0	        
    94  unit-metered-0	2016-08-22T12:02:04Z	   c-s	  5.0	        
    95  `)
    96  }
    97  
    98  func (s *metricsSuite) TestDefaultTabularFormat(c *gc.C) {
    99  	s.client.metrics = []params.MetricResult{{
   100  		Unit:  "unit-metered-0",
   101  		Key:   "pongs",
   102  		Value: "15.0",
   103  		Time:  time.Date(2016, 8, 22, 12, 02, 04, 0, time.UTC),
   104  	}, {
   105  		Unit:  "unit-metered-0",
   106  		Key:   "pings",
   107  		Value: "5.0",
   108  		Time:  time.Date(2016, 8, 22, 12, 02, 03, 0, time.UTC),
   109  	}}
   110  	ctx, err := cmdtesting.RunCommand(c, metricsdebug.NewMetricsCommandForTest(), "metered/0")
   111  	c.Assert(err, jc.ErrorIsNil)
   112  	s.client.CheckCall(c, 0, "GetMetrics", []string{"unit-metered-0"})
   113  	c.Assert(cmdtesting.Stdout(ctx), gc.Equals, `UNIT          	           TIMESTAMP	METRIC	VALUE	LABELS
   114  unit-metered-0	2016-08-22T12:02:03Z	 pings	  5.0	      
   115  unit-metered-0	2016-08-22T12:02:04Z	 pongs	 15.0	      
   116  `)
   117  }
   118  
   119  func (s *metricsSuite) TestJSONFormat(c *gc.C) {
   120  	s.client.metrics = []params.MetricResult{{
   121  		Unit:  "unit-metered-0",
   122  		Key:   "pings",
   123  		Value: "5.0",
   124  		Time:  time.Date(2016, 8, 22, 12, 02, 03, 0, time.UTC),
   125  	}, {
   126  		Unit:  "unit-metered-0",
   127  		Key:   "pongs",
   128  		Value: "15.0",
   129  		Time:  time.Date(2016, 8, 22, 12, 02, 04, 0, time.UTC),
   130  	}, {
   131  		Unit:   "unit-metered-0",
   132  		Key:    "pongs",
   133  		Value:  "10.0",
   134  		Time:   time.Date(2016, 8, 22, 12, 02, 04, 0, time.UTC),
   135  		Labels: map[string]string{"foo": "bar"},
   136  	}}
   137  	ctx, err := cmdtesting.RunCommand(c, metricsdebug.NewMetricsCommandForTest(), "metered", "--format", "json")
   138  	c.Assert(err, jc.ErrorIsNil)
   139  	s.client.CheckCall(c, 0, "GetMetrics", []string{"application-metered"})
   140  	c.Assert(cmdtesting.Stdout(ctx), gc.Equals, `[{"unit":"unit-metered-0","timestamp":"2016-08-22T12:02:03Z","metric":"pings","value":"5.0"},{"unit":"unit-metered-0","timestamp":"2016-08-22T12:02:04Z","metric":"pongs","value":"15.0"},{"unit":"unit-metered-0","timestamp":"2016-08-22T12:02:04Z","metric":"pongs","value":"10.0","labels":{"foo":"bar"}}]
   141  `)
   142  }
   143  
   144  func (s *metricsSuite) TestYAMLFormat(c *gc.C) {
   145  	s.client.metrics = []params.MetricResult{{
   146  		Unit:  "unit-metered-0",
   147  		Key:   "pings",
   148  		Value: "5.0",
   149  		Time:  time.Date(2016, 8, 22, 12, 02, 03, 0, time.UTC),
   150  	}, {
   151  		Unit:  "unit-metered-0",
   152  		Key:   "pongs",
   153  		Value: "15.0",
   154  		Time:  time.Date(2016, 8, 22, 12, 02, 04, 0, time.UTC),
   155  	}, {
   156  		Unit:   "unit-metered-0",
   157  		Key:    "pongs",
   158  		Value:  "10.0",
   159  		Time:   time.Date(2016, 8, 22, 12, 02, 04, 0, time.UTC),
   160  		Labels: map[string]string{"foo": "bar"},
   161  	}}
   162  	ctx, err := cmdtesting.RunCommand(c, metricsdebug.NewMetricsCommandForTest(), "metered", "--format", "yaml")
   163  	c.Assert(err, jc.ErrorIsNil)
   164  	s.client.CheckCall(c, 0, "GetMetrics", []string{"application-metered"})
   165  	c.Assert(cmdtesting.Stdout(ctx), gc.Equals, `- unit: unit-metered-0
   166    timestamp: 2016-08-22T12:02:03Z
   167    metric: pings
   168    value: "5.0"
   169  - unit: unit-metered-0
   170    timestamp: 2016-08-22T12:02:04Z
   171    metric: pongs
   172    value: "15.0"
   173  - unit: unit-metered-0
   174    timestamp: 2016-08-22T12:02:04Z
   175    metric: pongs
   176    value: "10.0"
   177    labels:
   178      foo: bar
   179  `)
   180  }
   181  
   182  func (s *metricsSuite) TestAll(c *gc.C) {
   183  	_, err := cmdtesting.RunCommand(c, metricsdebug.NewMetricsCommandForTest(), "--all")
   184  	c.Assert(err, jc.ErrorIsNil)
   185  	s.client.CheckCall(c, 0, "GetMetrics", []string(nil))
   186  }
   187  
   188  func (s *metricsSuite) TestAllWithExtraArgs(c *gc.C) {
   189  	_, err := cmdtesting.RunCommand(c, metricsdebug.NewMetricsCommandForTest(), "--all", "metered")
   190  	c.Assert(err, gc.ErrorMatches, "cannot use --all with additional entities")
   191  }
   192  
   193  func (s *metricsSuite) TestInvalidUnitName(c *gc.C) {
   194  	_, err := cmdtesting.RunCommand(c, metricsdebug.NewMetricsCommandForTest(), "metered-/0")
   195  	c.Assert(err, gc.ErrorMatches, `"metered-/0" is not a valid unit or application`)
   196  }
   197  
   198  func (s *metricsSuite) TestAPIClientError(c *gc.C) {
   199  	s.client.SetErrors(errors.New("a silly error"))
   200  	_, err := cmdtesting.RunCommand(c, metricsdebug.NewMetricsCommandForTest(), "metered/0")
   201  	c.Assert(err, gc.ErrorMatches, `a silly error`)
   202  }
   203  
   204  func (s *metricsSuite) TestNoArgs(c *gc.C) {
   205  	_, err := cmdtesting.RunCommand(c, metricsdebug.NewMetricsCommandForTest())
   206  	c.Assert(err, gc.ErrorMatches, "you need to specify at least one unit or application")
   207  }