2023-03-28 19:12:30 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2023-02-08 20:07:21 +00:00
|
|
|
package instances
|
|
|
|
|
|
|
|
import (
|
2023-02-24 17:51:09 +00:00
|
|
|
"errors"
|
2023-02-08 20:07:21 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent"
|
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
"github.com/hashicorp/consul/testrpc"
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestUsageInstancesCommand(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Parallel()
|
|
|
|
a := agent.NewTestAgent(t, ``)
|
|
|
|
defer a.Shutdown()
|
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
|
|
|
|
|
|
|
// Add another 2 services for testing
|
|
|
|
if err := a.Client().Agent().ServiceRegister(&api.AgentServiceRegistration{
|
|
|
|
Name: "testing",
|
|
|
|
Port: 8080,
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := a.Client().Agent().ServiceRegister(&api.AgentServiceRegistration{
|
|
|
|
Name: "testing2",
|
|
|
|
Port: 8081,
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-02-24 17:51:09 +00:00
|
|
|
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"),
|
|
|
|
},
|
2023-02-08 20:07:21 +00:00
|
|
|
}
|
2023-02-24 17:51:09 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
2023-02-08 20:07:21 +00:00
|
|
|
}
|
|
|
|
}
|