open-consul/command/intention/match/match_test.go
Daniel Nephin ef0999547a testing: skip slow tests with -short
Add a skip condition to all tests slower than 100ms.

This change was made using `gotestsum tool slowest` with data from the
last 3 CI runs of master.
See https://github.com/gotestyourself/gotestsum#finding-and-skipping-slow-tests

With this change:

```
$ time go test -count=1 -short ./agent
ok      github.com/hashicorp/consul/agent       0.743s

real    0m4.791s

$ time go test -count=1 -short ./agent/consul
ok      github.com/hashicorp/consul/agent/consul        4.229s

real    0m8.769s
```
2020-12-07 13:42:55 -05:00

167 lines
3.1 KiB
Go

package match
import (
"strings"
"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 TestIntentionMatch_noTabs(t *testing.T) {
t.Parallel()
if strings.ContainsRune(New(nil).Help(), '\t') {
t.Fatal("help has tabs")
}
}
func TestIntentionMatch_Validation(t *testing.T) {
t.Parallel()
ui := cli.NewMockUi()
c := New(ui)
cases := map[string]struct {
args []string
output string
}{
"0 args": {
[]string{},
"requires exactly one",
},
"3 args": {
[]string{"a", "b", "c"},
"requires exactly one",
},
"both source and dest": {
[]string{"-source", "-destination", "foo"},
"only one of -source",
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
require := require.New(t)
c.init()
// Ensure our buffer is always clear
if ui.ErrorWriter != nil {
ui.ErrorWriter.Reset()
}
if ui.OutputWriter != nil {
ui.OutputWriter.Reset()
}
require.Equal(1, c.Run(tc.args))
output := ui.ErrorWriter.String()
require.Contains(output, tc.output)
})
}
}
func TestIntentionMatch_matchDst(t *testing.T) {
if testing.Short() {
t.Skip("too slow for testing.Short")
}
t.Parallel()
require := require.New(t)
a := agent.NewTestAgent(t, ``)
defer a.Shutdown()
client := a.Client()
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
// Create some intentions
{
insert := [][]string{
{"foo", "db"},
{"web", "db"},
{"*", "db"},
}
for _, v := range insert {
//nolint:staticcheck
id, _, err := client.Connect().IntentionCreate(&api.Intention{
SourceName: v[0],
DestinationName: v[1],
Action: api.IntentionActionDeny,
}, nil)
require.NoError(err)
require.NotEmpty(id)
}
}
// Match it
{
ui := cli.NewMockUi()
c := New(ui)
args := []string{
"-http-addr=" + a.HTTPAddr(),
"db",
}
require.Equal(0, c.Run(args), ui.ErrorWriter.String())
require.Contains(ui.OutputWriter.String(), "web")
require.Contains(ui.OutputWriter.String(), "db")
require.Contains(ui.OutputWriter.String(), "*")
}
}
func TestIntentionMatch_matchSource(t *testing.T) {
if testing.Short() {
t.Skip("too slow for testing.Short")
}
t.Parallel()
require := require.New(t)
a := agent.NewTestAgent(t, ``)
defer a.Shutdown()
client := a.Client()
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
// Create some intentions
{
insert := [][]string{
{"foo", "db"},
{"web", "db"},
{"*", "db"},
}
for _, v := range insert {
//nolint:staticcheck
id, _, err := client.Connect().IntentionCreate(&api.Intention{
SourceName: v[0],
DestinationName: v[1],
Action: api.IntentionActionDeny,
}, nil)
require.NoError(err)
require.NotEmpty(id)
}
}
// Match it
{
ui := cli.NewMockUi()
c := New(ui)
args := []string{
"-http-addr=" + a.HTTPAddr(),
"-source",
"foo",
}
require.Equal(0, c.Run(args), ui.ErrorWriter.String())
require.Contains(ui.OutputWriter.String(), "db")
require.NotContains(ui.OutputWriter.String(), "web")
}
}