mirror of https://github.com/hashicorp/consul
Fix a couple inconsistencies in `operator usage instances` command (#16260)
parent
ab0d43e7f4
commit
dca7c18ec4
|
@ -33,8 +33,10 @@ type cmd struct {
|
||||||
|
|
||||||
func (c *cmd) init() {
|
func (c *cmd) init() {
|
||||||
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
|
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
|
||||||
c.flags.BoolVar(&c.onlyBillable, "billable", false, "Display only billable service info.")
|
c.flags.BoolVar(&c.onlyBillable, "billable", false, "Display only billable service info. "+
|
||||||
c.flags.BoolVar(&c.onlyConnect, "connect", false, "Display only Connect service info.")
|
"Cannot be used with -connect.")
|
||||||
|
c.flags.BoolVar(&c.onlyConnect, "connect", false, "Display only Connect service info."+
|
||||||
|
"Cannot be used with -billable.")
|
||||||
c.flags.BoolVar(&c.allDatacenters, "all-datacenters", false, "Display service counts from "+
|
c.flags.BoolVar(&c.allDatacenters, "all-datacenters", false, "Display service counts from "+
|
||||||
"all datacenters.")
|
"all datacenters.")
|
||||||
|
|
||||||
|
@ -54,6 +56,11 @@ func (c *cmd) Run(args []string) int {
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if c.onlyBillable && c.onlyConnect {
|
||||||
|
c.UI.Error("Cannot specify both -billable and -connect flags")
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
// Create and test the HTTP client
|
// Create and test the HTTP client
|
||||||
client, err := c.http.APIClient()
|
client, err := c.http.APIClient()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -219,22 +226,22 @@ func (c *cmd) Help() string {
|
||||||
const (
|
const (
|
||||||
synopsis = "Display service instance usage information"
|
synopsis = "Display service instance usage information"
|
||||||
help = `
|
help = `
|
||||||
Usage: consul usage instances [options]
|
Usage: consul operator usage instances [options]
|
||||||
|
|
||||||
Retrieves usage information about the number of services registered in a given
|
Retrieves usage information about the number of services registered in a given
|
||||||
datacenter. By default, the datacenter of the local agent is queried.
|
datacenter. By default, the datacenter of the local agent is queried.
|
||||||
|
|
||||||
To retrieve the service usage data:
|
To retrieve the service usage data:
|
||||||
|
|
||||||
$ consul usage instances
|
$ consul operator usage instances
|
||||||
|
|
||||||
To show only billable service instance counts:
|
To show only billable service instance counts:
|
||||||
|
|
||||||
$ consul usage instances -billable
|
$ consul operator usage instances -billable
|
||||||
|
|
||||||
To show only connect service instance counts:
|
To show only connect service instance counts:
|
||||||
|
|
||||||
$ consul usage instances -connect
|
$ consul operator usage instances -connect
|
||||||
|
|
||||||
For a full list of options and examples, please see the Consul documentation.
|
For a full list of options and examples, please see the Consul documentation.
|
||||||
`
|
`
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package instances
|
package instances
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/hashicorp/consul/agent"
|
"github.com/hashicorp/consul/agent"
|
||||||
|
@ -36,15 +37,40 @@ func TestUsageInstancesCommand(t *testing.T) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
ui := cli.NewMockUi()
|
cases := []struct {
|
||||||
c := New(ui)
|
name string
|
||||||
args := []string{
|
extraArgs []string
|
||||||
"-http-addr=" + a.HTTPAddr(),
|
output string
|
||||||
|
err error
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "basic output",
|
||||||
|
output: "Billable Service Instances Total: 2",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "billable and connect flags together are invalid",
|
||||||
|
extraArgs: []string{"-billable", "-connect"},
|
||||||
|
err: errors.New("Cannot specify both -billable and -connect"),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
code := c.Run(args)
|
|
||||||
if code != 0 {
|
for _, tc := range cases {
|
||||||
t.Fatalf("bad exit code %d: %s", code, ui.ErrorWriter.String())
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
ui := cli.NewMockUi()
|
||||||
|
c := New(ui)
|
||||||
|
args := []string{
|
||||||
|
"-http-addr=" + a.HTTPAddr(),
|
||||||
|
}
|
||||||
|
args = append(args, tc.extraArgs...)
|
||||||
|
|
||||||
|
code := c.Run(args)
|
||||||
|
if tc.err != nil {
|
||||||
|
require.Equal(t, 1, code)
|
||||||
|
require.Contains(t, ui.ErrorWriter.String(), tc.err.Error())
|
||||||
|
} else {
|
||||||
|
require.Equal(t, 0, code)
|
||||||
|
require.Contains(t, ui.OutputWriter.String(), tc.output)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
output := ui.OutputWriter.String()
|
|
||||||
require.Contains(t, output, "Billable Service Instances Total: 2")
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue