github.hscsec.cn/hashicorp/consul@v1.4.5/command/intention/match/match_test.go (about) 1 package match 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/hashicorp/consul/agent" 8 "github.com/hashicorp/consul/api" 9 "github.com/mitchellh/cli" 10 "github.com/stretchr/testify/require" 11 ) 12 13 func TestCommand_noTabs(t *testing.T) { 14 t.Parallel() 15 if strings.ContainsRune(New(nil).Help(), '\t') { 16 t.Fatal("help has tabs") 17 } 18 } 19 20 func TestCommand_Validation(t *testing.T) { 21 t.Parallel() 22 23 ui := cli.NewMockUi() 24 c := New(ui) 25 26 cases := map[string]struct { 27 args []string 28 output string 29 }{ 30 "0 args": { 31 []string{}, 32 "requires exactly one", 33 }, 34 35 "3 args": { 36 []string{"a", "b", "c"}, 37 "requires exactly one", 38 }, 39 40 "both source and dest": { 41 []string{"-source", "-destination", "foo"}, 42 "only one of -source", 43 }, 44 } 45 46 for name, tc := range cases { 47 t.Run(name, func(t *testing.T) { 48 require := require.New(t) 49 50 c.init() 51 52 // Ensure our buffer is always clear 53 if ui.ErrorWriter != nil { 54 ui.ErrorWriter.Reset() 55 } 56 if ui.OutputWriter != nil { 57 ui.OutputWriter.Reset() 58 } 59 60 require.Equal(1, c.Run(tc.args)) 61 output := ui.ErrorWriter.String() 62 require.Contains(output, tc.output) 63 }) 64 } 65 } 66 67 func TestCommand_matchDst(t *testing.T) { 68 t.Parallel() 69 70 require := require.New(t) 71 a := agent.NewTestAgent(t, t.Name(), ``) 72 defer a.Shutdown() 73 client := a.Client() 74 75 // Create some intentions 76 { 77 insert := [][]string{ 78 {"foo", "db"}, 79 {"web", "db"}, 80 {"*", "db"}, 81 } 82 83 for _, v := range insert { 84 id, _, err := client.Connect().IntentionCreate(&api.Intention{ 85 SourceName: v[0], 86 DestinationName: v[1], 87 Action: api.IntentionActionDeny, 88 }, nil) 89 require.NoError(err) 90 require.NotEmpty(id) 91 } 92 } 93 94 // Match it 95 { 96 ui := cli.NewMockUi() 97 c := New(ui) 98 99 args := []string{ 100 "-http-addr=" + a.HTTPAddr(), 101 "db", 102 } 103 require.Equal(0, c.Run(args), ui.ErrorWriter.String()) 104 require.Contains(ui.OutputWriter.String(), "web") 105 require.Contains(ui.OutputWriter.String(), "db") 106 require.Contains(ui.OutputWriter.String(), "*") 107 } 108 } 109 110 func TestCommand_matchSource(t *testing.T) { 111 t.Parallel() 112 113 require := require.New(t) 114 a := agent.NewTestAgent(t, t.Name(), ``) 115 defer a.Shutdown() 116 client := a.Client() 117 118 // Create some intentions 119 { 120 insert := [][]string{ 121 {"foo", "db"}, 122 {"web", "db"}, 123 {"*", "db"}, 124 } 125 126 for _, v := range insert { 127 id, _, err := client.Connect().IntentionCreate(&api.Intention{ 128 SourceName: v[0], 129 DestinationName: v[1], 130 Action: api.IntentionActionDeny, 131 }, nil) 132 require.NoError(err) 133 require.NotEmpty(id) 134 } 135 } 136 137 // Match it 138 { 139 ui := cli.NewMockUi() 140 c := New(ui) 141 142 args := []string{ 143 "-http-addr=" + a.HTTPAddr(), 144 "-source", 145 "foo", 146 } 147 require.Equal(0, c.Run(args), ui.ErrorWriter.String()) 148 require.Contains(ui.OutputWriter.String(), "db") 149 require.NotContains(ui.OutputWriter.String(), "web") 150 } 151 }