Fix a couple inconsistencies in `operator usage instances` command (#16260)

This commit is contained in:
Kyle Havlovitz 2023-02-24 09:51:09 -08:00 committed by GitHub
parent db19b92ca9
commit 18954ccd3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 15 deletions

View File

@ -33,8 +33,10 @@ type cmd struct {
func (c *cmd) init() {
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
c.flags.BoolVar(&c.onlyBillable, "billable", false, "Display only billable service info.")
c.flags.BoolVar(&c.onlyConnect, "connect", false, "Display only Connect service info.")
c.flags.BoolVar(&c.onlyBillable, "billable", false, "Display only billable 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 "+
"all datacenters.")
@ -54,6 +56,11 @@ func (c *cmd) Run(args []string) int {
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
client, err := c.http.APIClient()
if err != nil {
@ -219,22 +226,22 @@ func (c *cmd) Help() string {
const (
synopsis = "Display service instance usage information"
help = `
Usage: consul usage instances [options]
Usage: consul operator usage instances [options]
Retrieves usage information about the number of services registered in a given
datacenter. By default, the datacenter of the local agent is queried.
To retrieve the service usage data:
$ consul usage instances
$ consul operator usage instances
To show only billable service instance counts:
$ consul usage instances -billable
$ consul operator usage instances -billable
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.
`

View File

@ -1,6 +1,7 @@
package instances
import (
"errors"
"testing"
"github.com/hashicorp/consul/agent"
@ -36,15 +37,40 @@ func TestUsageInstancesCommand(t *testing.T) {
t.Fatal(err)
}
ui := cli.NewMockUi()
c := New(ui)
args := []string{
"-http-addr=" + a.HTTPAddr(),
cases := []struct {
name string
extraArgs []string
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 {
t.Fatalf("bad exit code %d: %s", code, ui.ErrorWriter.String())
for _, tc := range cases {
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")
}