github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/featuretests/cmd_juju_metrics.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package featuretests 5 6 import ( 7 "time" 8 9 "github.com/gosuri/uitable" 10 "github.com/juju/cmd/cmdtesting" 11 jc "github.com/juju/testing/checkers" 12 gc "gopkg.in/check.v1" 13 14 "github.com/juju/juju/cmd/juju/metricsdebug" 15 jujutesting "github.com/juju/juju/juju/testing" 16 "github.com/juju/juju/state" 17 "github.com/juju/juju/testing/factory" 18 ) 19 20 type cmdMetricsCommandSuite struct { 21 jujutesting.JujuConnSuite 22 } 23 24 func (s *cmdMetricsCommandSuite) TestDebugNoArgs(c *gc.C) { 25 _, err := cmdtesting.RunCommand(c, metricsdebug.New()) 26 c.Assert(err, gc.ErrorMatches, "you need to specify at least one unit or application") 27 } 28 29 type tabularMetric struct { 30 Unit string 31 Timestamp time.Time 32 Metric string 33 Value string 34 Labels string 35 } 36 37 type structuredMetric struct { 38 Unit string `json:"unit" yaml:"unit"` 39 Timestamp time.Time `json:"timestamp" yaml:"timestamp"` 40 Metric string `json:"metric" yaml:"metric"` 41 Value string `json:"value" yaml:"value"` 42 Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"` 43 } 44 45 func formatTabular(metrics ...tabularMetric) string { 46 table := uitable.New() 47 table.MaxColWidth = 50 48 table.Wrap = true 49 for _, col := range []int{1, 2, 3, 4} { 50 table.RightAlign(col) 51 } 52 table.AddRow("UNIT", "TIMESTAMP", "METRIC", "VALUE", "LABELS") 53 for _, m := range metrics { 54 table.AddRow(m.Unit, m.Timestamp.Format(time.RFC3339), m.Metric, m.Value, m.Labels) 55 } 56 return table.String() + "\n" 57 } 58 59 func (s *cmdMetricsCommandSuite) TestUnits(c *gc.C) { 60 meteredCharm := s.Factory.MakeCharm(c, &factory.CharmParams{Name: "metered", URL: "local:quantal/metered-1"}) 61 meteredApplication := s.Factory.MakeApplication(c, &factory.ApplicationParams{Charm: meteredCharm}) 62 unit := s.Factory.MakeUnit(c, &factory.UnitParams{Application: meteredApplication, SetCharmURL: true}) 63 unit2 := s.Factory.MakeUnit(c, &factory.UnitParams{Application: meteredApplication, SetCharmURL: true}) 64 newTime1 := time.Now().Round(time.Second) 65 newTime2 := newTime1.Add(time.Second) 66 metricA := state.Metric{Key: "pings", Value: "5", Time: newTime1, Labels: map[string]string{"foo": "bar"}} 67 metricB := state.Metric{Key: "pings", Value: "10.5", Time: newTime2, Labels: map[string]string{"baz": "quux"}} 68 s.Factory.MakeMetric(c, &factory.MetricParams{Unit: unit, Metrics: []state.Metric{metricA}}) 69 s.Factory.MakeMetric(c, &factory.MetricParams{Unit: unit2, Metrics: []state.Metric{metricA, metricB}}) 70 ctx, err := cmdtesting.RunCommand(c, metricsdebug.New(), "metered/1") 71 c.Assert(err, jc.ErrorIsNil) 72 73 c.Assert(cmdtesting.Stdout(ctx), gc.Equals, formatTabular([]tabularMetric{{ 74 Unit: unit2.Name(), 75 Timestamp: newTime2, 76 Metric: "pings", 77 Value: "10.5", 78 Labels: "baz=quux", 79 }, { 80 Unit: unit2.Name(), 81 Timestamp: newTime1, 82 Metric: "pings", 83 Value: "5", 84 Labels: "foo=bar", 85 }}...)) 86 ctx, err = cmdtesting.RunCommand(c, metricsdebug.New(), "metered/0") 87 c.Assert(err, jc.ErrorIsNil) 88 c.Assert(cmdtesting.Stdout(ctx), gc.Equals, 89 formatTabular(tabularMetric{ 90 Unit: unit.Name(), 91 Timestamp: newTime1, 92 Metric: "pings", 93 Value: "5", 94 Labels: "foo=bar", 95 }), 96 ) 97 } 98 99 func (s *cmdMetricsCommandSuite) TestAll(c *gc.C) { 100 meteredCharm := s.Factory.MakeCharm(c, &factory.CharmParams{Name: "metered", URL: "local:quantal/metered-1"}) 101 meteredApplication := s.Factory.MakeApplication(c, &factory.ApplicationParams{Charm: meteredCharm}) 102 unit := s.Factory.MakeUnit(c, &factory.UnitParams{Application: meteredApplication, SetCharmURL: true}) 103 unit2 := s.Factory.MakeUnit(c, &factory.UnitParams{Application: meteredApplication, SetCharmURL: true}) 104 newTime1 := time.Now().Round(time.Second) 105 newTime2 := newTime1.Add(time.Second) 106 metricA := state.Metric{Key: "pings", Value: "5", Time: newTime1} 107 metricB := state.Metric{Key: "pings", Value: "10.5", Time: newTime2, Labels: map[string]string{"foo": "bar", "baz": "quux"}} 108 s.Factory.MakeMetric(c, &factory.MetricParams{Unit: unit, Metrics: []state.Metric{metricA}}) 109 s.Factory.MakeMetric(c, &factory.MetricParams{Unit: unit2, Metrics: []state.Metric{metricA, metricB}}) 110 ctx, err := cmdtesting.RunCommand(c, metricsdebug.New(), "--all") 111 c.Assert(err, jc.ErrorIsNil) 112 c.Assert(cmdtesting.Stdout(ctx), gc.Equals, 113 formatTabular([]tabularMetric{{ 114 Unit: unit.Name(), 115 Timestamp: newTime1, 116 Metric: "pings", 117 Value: "5", 118 }, { 119 Unit: unit2.Name(), 120 Timestamp: newTime1, 121 Metric: "pings", 122 Value: "5", 123 }, { 124 Unit: unit2.Name(), 125 Timestamp: newTime2, 126 Metric: "pings", 127 Value: "10.5", 128 Labels: "baz=quux,foo=bar", 129 }}...), 130 ) 131 } 132 133 func (s *cmdMetricsCommandSuite) TestFormatJSON(c *gc.C) { 134 meteredCharm := s.Factory.MakeCharm(c, &factory.CharmParams{Name: "metered", URL: "local:quantal/metered-1"}) 135 meteredApplication := s.Factory.MakeApplication(c, &factory.ApplicationParams{Charm: meteredCharm}) 136 unit := s.Factory.MakeUnit(c, &factory.UnitParams{Application: meteredApplication, SetCharmURL: true}) 137 unit2 := s.Factory.MakeUnit(c, &factory.UnitParams{Application: meteredApplication, SetCharmURL: true}) 138 newTime1 := time.Now().Round(time.Second) 139 newTime2 := newTime1.Add(time.Second) 140 metricA := state.Metric{Key: "pings", Value: "5", Time: newTime1, Labels: map[string]string{"abc": "123"}} 141 metricB := state.Metric{Key: "pings", Value: "10.5", Time: newTime2} 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 ctx, err := cmdtesting.RunCommand(c, metricsdebug.New(), "metered/1", "--format", "json") 145 c.Assert(err, jc.ErrorIsNil) 146 expectedOutput := []structuredMetric{{ 147 Unit: unit2.Name(), 148 Timestamp: newTime2, 149 Metric: "pings", 150 Value: "10.5", 151 }, { 152 Unit: unit2.Name(), 153 Timestamp: newTime1, 154 Metric: "pings", 155 Value: "5", 156 Labels: map[string]string{"abc": "123"}, 157 }} 158 c.Assert(cmdtesting.Stdout(ctx), jc.JSONEquals, expectedOutput) 159 ctx, err = cmdtesting.RunCommand(c, metricsdebug.New(), "metered/0", "--format", "json") 160 c.Assert(err, jc.ErrorIsNil) 161 expectedOutput = []structuredMetric{{ 162 Unit: unit.Name(), 163 Timestamp: newTime1, 164 Metric: "pings", 165 Value: "5", 166 Labels: map[string]string{"abc": "123"}, 167 }} 168 c.Assert(cmdtesting.Stdout(ctx), jc.JSONEquals, expectedOutput) 169 } 170 171 func (s *cmdMetricsCommandSuite) TestFormatYAML(c *gc.C) { 172 meteredCharm := s.Factory.MakeCharm(c, &factory.CharmParams{Name: "metered", URL: "local:quantal/metered-1"}) 173 meteredApplication := s.Factory.MakeApplication(c, &factory.ApplicationParams{Charm: meteredCharm}) 174 unit := s.Factory.MakeUnit(c, &factory.UnitParams{Application: meteredApplication, SetCharmURL: true}) 175 unit2 := s.Factory.MakeUnit(c, &factory.UnitParams{Application: meteredApplication, SetCharmURL: true}) 176 newTime1 := time.Now().Round(time.Second) 177 newTime2 := newTime1.Add(time.Second) 178 metricA := state.Metric{Key: "pings", Value: "5", Time: newTime1} 179 metricB := state.Metric{Key: "pings", Value: "10.5", Time: newTime2} 180 s.Factory.MakeMetric(c, &factory.MetricParams{Unit: unit, Metrics: []state.Metric{metricA}}) 181 s.Factory.MakeMetric(c, &factory.MetricParams{Unit: unit2, Metrics: []state.Metric{metricA, metricB}}) 182 ctx, err := cmdtesting.RunCommand(c, metricsdebug.New(), "metered/1", "--format", "yaml") 183 c.Assert(err, jc.ErrorIsNil) 184 expectedOutput := []structuredMetric{{ 185 Unit: unit2.Name(), 186 Timestamp: newTime2, 187 Metric: "pings", 188 Value: "10.5", 189 }} 190 c.Assert(cmdtesting.Stdout(ctx), jc.YAMLEquals, expectedOutput) 191 } 192 193 func (s *cmdMetricsCommandSuite) TestNoMetrics(c *gc.C) { 194 meteredCharm := s.Factory.MakeCharm(c, &factory.CharmParams{Name: "metered", URL: "local:quantal/metered-1"}) 195 meteredApplication := s.Factory.MakeApplication(c, &factory.ApplicationParams{Charm: meteredCharm}) 196 s.Factory.MakeUnit(c, &factory.UnitParams{Application: meteredApplication, SetCharmURL: true}) 197 ctx, err := cmdtesting.RunCommand(c, metricsdebug.New(), "metered") 198 c.Assert(err, jc.ErrorIsNil) 199 c.Assert(cmdtesting.Stdout(ctx), gc.Equals, "") 200 }